Skip to content
This repository was archived by the owner on Oct 14, 2024. It is now read-only.

Commit

Permalink
refactor: aws provider to provider v2 (#1324)
Browse files Browse the repository at this point in the history
* refactor: copy all aws code to v2

* refactor: implement new aws provider

* refactor: implement discoverer for aws provider

* refactor: implement estimator for aws provider

* refactor: implement scanner for aws provider

* refactor: implement utils for aws provider

* refactor: move new scanestimation to estimator

* refactor: move config up and add types package

* refactor: init scanestimator with provider init
  • Loading branch information
paralta authored Feb 22, 2024
1 parent 8a72a89 commit 655a318
Show file tree
Hide file tree
Showing 19 changed files with 3,795 additions and 17 deletions.
98 changes: 98 additions & 0 deletions provider/v2/aws/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// Copyright © 2023 Cisco Systems, Inc. and its affiliates.
// All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package aws

import (
"errors"
"fmt"

"github.com/spf13/viper"
)

const (
DefaultEnvPrefix = "VMCLARITY_AWS"
DefaultScannerInstanceType = "t2.large"
DefaultBlockDeviceName = "xvdh"
)

type Config struct {
// Region where the Scanner instance needs to be created
ScannerRegion string `mapstructure:"scanner_region"`
// SubnetID where the Scanner instance needs to be created
SubnetID string `mapstructure:"subnet_id"`
// SecurityGroupID which needs to be attached to the Scanner instance
SecurityGroupID string `mapstructure:"security_group_id"`
// KeyPairName is the name of the SSH KeyPair to use for Scanner instance launch
KeyPairName string `mapstructure:"keypair_name"`
// ScannerImage is the AMI image used for creating Scanner instance
ScannerImage string `mapstructure:"scanner_ami_id"`
// ScannerInstanceType is the instance type used for Scanner instance
ScannerInstanceType string `mapstructure:"scanner_instance_type"`
// BlockDeviceName contains the block device name used for attaching Scanner volume to the Scanner instance
BlockDeviceName string `mapstructure:"block_device_name"`
}

func (c *Config) Validate() error {
if c.ScannerRegion == "" {
return errors.New("parameter Region must be provided")
}

if c.SubnetID == "" {
return errors.New("parameter SubnetID must be provided")
}

if c.SecurityGroupID == "" {
return errors.New("parameter SecurityGroupID must be provided")
}

if c.ScannerImage == "" {
return errors.New("parameter ScannerImage must be provided")
}

if c.ScannerInstanceType == "" {
return errors.New("parameter ScannerInstanceType must be provided")
}

return nil
}

func NewConfig() (*Config, error) {
// Avoid modifying the global instance
v := viper.New()

v.SetEnvPrefix(DefaultEnvPrefix)
v.AllowEmptyEnv(true)
v.AutomaticEnv()

_ = v.BindEnv("scanner_region")
_ = v.BindEnv("subnet_id")
_ = v.BindEnv("security_group_id")
_ = v.BindEnv("keypair_name")
_ = v.BindEnv("scanner_ami_id")

_ = v.BindEnv("scanner_instance_type")
v.SetDefault("scanner_instance_type", DefaultScannerInstanceType)

_ = v.BindEnv("block_device_name")
v.SetDefault("block_device_name", DefaultBlockDeviceName)

config := &Config{}
if err := v.Unmarshal(config); err != nil {
return nil, fmt.Errorf("failed to parse provider configuration. Provider=AWS: %w", err)
}

return config, nil
}
79 changes: 79 additions & 0 deletions provider/v2/aws/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Copyright © 2023 Cisco Systems, Inc. and its affiliates.
// All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package aws

import (
"os"
"testing"

. "github.com/onsi/gomega"
"github.com/onsi/gomega/types"
)

func TestConfig(t *testing.T) {
tests := []struct {
Name string
EnvVars map[string]string

ExpectedNewErrorMatcher types.GomegaMatcher
ExpectedConfig *Config
ExpectedValidateErrorMatcher types.GomegaMatcher
}{
{
Name: "Valid config",
EnvVars: map[string]string{
"VMCLARITY_AWS_SCANNER_REGION": "eu-west-1",
"VMCLARITY_AWS_SUBNET_ID": "subnet-038f85dc621fd5b5d",
"VMCLARITY_AWS_SECURITY_GROUP_ID": "sg-02cfdc854e18664d4",
"VMCLARITY_AWS_KEYPAIR_NAME": "vmclarity-ssh-key",
"VMCLARITY_AWS_SCANNER_AMI_ID": "ami-0568773882d492fc8",
"VMCLARITY_AWS_SCANNER_INSTANCE_TYPE": "t3.large",
"VMCLARITY_AWS_BLOCK_DEVICE_NAME": "xvdh",
},
ExpectedNewErrorMatcher: Not(HaveOccurred()),
ExpectedConfig: &Config{
ScannerRegion: "eu-west-1",
SubnetID: "subnet-038f85dc621fd5b5d",
SecurityGroupID: "sg-02cfdc854e18664d4",
KeyPairName: "vmclarity-ssh-key",
ScannerImage: "ami-0568773882d492fc8",
ScannerInstanceType: "t3.large",
BlockDeviceName: "xvdh",
},
ExpectedValidateErrorMatcher: Not(HaveOccurred()),
},
}

for _, test := range tests {
t.Run(test.Name, func(t *testing.T) {
g := NewGomegaWithT(t)

os.Clearenv()
for k, v := range test.EnvVars {
err := os.Setenv(k, v)
g.Expect(err).Should(Not(HaveOccurred()))
}

config, err := NewConfig()

g.Expect(err).Should(test.ExpectedNewErrorMatcher)
g.Expect(config).Should(BeEquivalentTo(test.ExpectedConfig))

err = config.Validate()
g.Expect(err).Should(test.ExpectedValidateErrorMatcher)
})
}
}
Loading

0 comments on commit 655a318

Please sign in to comment.