This repository was archived by the owner on Oct 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor(gcp): refactor estimator to v2 * refactor(gcp): add Kind method to provider * refactor(gcp): add discoverer * refactor(gcp): move discoverer config one step higher * refactor(gcp): refactor scanner logic * refactor(gcp): add unit tests * fix(gcp): run make fix * fix(gcp): add headers to files * fix(gcp): fix linter * refactor(gcp): rename common to utils * fix(gcp): import orders
- Loading branch information
1 parent
5d37bf0
commit 0e3bb90
Showing
12 changed files
with
1,150 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
// 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 gcp | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/mitchellh/mapstructure" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
const ( | ||
DefaultEnvPrefix = "VMCLARITY_GCP" | ||
|
||
projectID = "project_id" | ||
scannerZone = "scanner_zone" | ||
scannerSubnetwork = "scanner_subnetwork" | ||
scannerMachineType = "scanner_machine_type" | ||
scannerSourceImage = "scanner_source_image" | ||
scannerSSHPublicKey = "scanner_ssh_public_key" | ||
) | ||
|
||
type Config struct { | ||
ProjectID string `mapstructure:"project_id"` | ||
ScannerZone string `mapstructure:"scanner_zone"` | ||
ScannerSubnetwork string `mapstructure:"scanner_subnetwork"` | ||
ScannerMachineType string `mapstructure:"scanner_machine_type"` | ||
ScannerSourceImage string `mapstructure:"scanner_source_image"` | ||
ScannerSSHPublicKey string `mapstructure:"scanner_ssh_public_key"` | ||
} | ||
|
||
func NewConfig() (*Config, error) { | ||
// Avoid modifying the global instance | ||
v := viper.New() | ||
|
||
v.SetEnvPrefix(DefaultEnvPrefix) | ||
v.AllowEmptyEnv(true) | ||
v.AutomaticEnv() | ||
|
||
_ = v.BindEnv(projectID) | ||
_ = v.BindEnv(scannerZone) | ||
_ = v.BindEnv(scannerSubnetwork) | ||
_ = v.BindEnv(scannerMachineType) | ||
_ = v.BindEnv(scannerSourceImage) | ||
_ = v.BindEnv(scannerSSHPublicKey) | ||
|
||
config := &Config{} | ||
if err := v.Unmarshal(&config, viper.DecodeHook(mapstructure.TextUnmarshallerHookFunc())); err != nil { | ||
return nil, fmt.Errorf("failed to parse provider configuration. Provider=GCP: %w", err) | ||
} | ||
return config, nil | ||
} | ||
|
||
// nolint:cyclop | ||
func (c Config) Validate() error { | ||
if c.ProjectID == "" { | ||
return fmt.Errorf("parameter ProjectID must be provided by setting %v_%v environment variable", DefaultEnvPrefix, strings.ToUpper(projectID)) | ||
} | ||
|
||
if c.ScannerZone == "" { | ||
return fmt.Errorf("parameter ScannerZone must be provided by setting %v_%v environment variable", DefaultEnvPrefix, strings.ToUpper(scannerZone)) | ||
} | ||
|
||
if c.ScannerSubnetwork == "" { | ||
return fmt.Errorf("parameter ScannerSubnetwork must be provided by setting %v_%v environment variable", DefaultEnvPrefix, strings.ToUpper(scannerSubnetwork)) | ||
} | ||
|
||
if c.ScannerMachineType == "" { | ||
return fmt.Errorf("parameter ScannerMachineType must be provided by setting %v_%v environment variable", DefaultEnvPrefix, strings.ToUpper(scannerMachineType)) | ||
} | ||
|
||
if c.ScannerSourceImage == "" { | ||
return fmt.Errorf("parameter ScannerSourceImage must be provided by setting %v_%v environment variable", DefaultEnvPrefix, strings.ToUpper(scannerSourceImage)) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// 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 discoverer | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
|
||
apitypes "github.com/openclarity/vmclarity/api/types" | ||
) | ||
|
||
func Test_getZonesLastPart(t *testing.T) { | ||
type args struct { | ||
zones []string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want []string | ||
}{ | ||
{ | ||
name: "empty", | ||
args: args{ | ||
zones: []string{}, | ||
}, | ||
want: []string{}, | ||
}, | ||
{ | ||
name: "get two zones", | ||
args: args{ | ||
zones: []string{"https://www.googleapis.com/compute/v1/projects/gcp-etigcp-nprd-12855/zones/us-central1-c", "https://www.googleapis.com/compute/v1/projects/gcp-etigcp-nprd-12855/zones/us-central1-a"}, | ||
}, | ||
want: []string{"us-central1-c", "us-central1-a"}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
got := getZonesLastPart(tt.args.zones) | ||
if diff := cmp.Diff(tt.want, got); diff != "" { | ||
t.Errorf("getZonesLastPart() mismatch (-want, +got):\n%s", diff) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func Test_convertLabelsToTags(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
args map[string]string | ||
want []apitypes.Tag | ||
}{ | ||
{ | ||
name: "sanity", | ||
args: map[string]string{ | ||
"valid-tag": "valid-value", | ||
}, | ||
want: []apitypes.Tag{{ | ||
Key: "valid-tag", Value: "valid-value", | ||
}}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := convertLabelsToTags(tt.args); !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("convertLabelsToTags() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.