From a8ae5bd52f19641a4483b2d162bb784fde4f8281 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A1s=20J=C3=A1ky?= Date: Mon, 26 Feb 2024 14:21:54 +0100 Subject: [PATCH] refactor: common.go to utils, config.go to scanner package MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: András Jáky --- provider/v2/azure/provider.go | 11 +++++----- provider/v2/azure/scanner/blob.go | 20 +++++++++---------- .../v2/azure/{common => scanner}/config.go | 2 +- provider/v2/azure/scanner/networkInterface.go | 8 ++++---- provider/v2/azure/scanner/scanner.go | 6 +++--- provider/v2/azure/scanner/scannerVm.go | 12 +++++------ provider/v2/azure/scanner/snapshot.go | 8 ++++---- provider/v2/azure/scanner/targetDisk.go | 12 +++++------ .../{common/common.go => utils/utils.go} | 2 +- 9 files changed, 40 insertions(+), 41 deletions(-) rename provider/v2/azure/{common => scanner}/config.go (99%) rename provider/v2/azure/{common/common.go => utils/utils.go} (99%) diff --git a/provider/v2/azure/provider.go b/provider/v2/azure/provider.go index 944223135..a55f560c7 100644 --- a/provider/v2/azure/provider.go +++ b/provider/v2/azure/provider.go @@ -24,7 +24,6 @@ import ( "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v5" apitypes "github.com/openclarity/vmclarity/api/types" - "github.com/openclarity/vmclarity/provider/v2/azure/common" "github.com/openclarity/vmclarity/provider/v2/azure/discoverer" "github.com/openclarity/vmclarity/provider/v2/azure/estimator" "github.com/openclarity/vmclarity/provider/v2/azure/scanner" @@ -41,12 +40,12 @@ func (p *Provider) Kind() apitypes.CloudProvider { } func New(_ context.Context) (*Provider, error) { - config, err := common.NewConfig() + scannerConfig, err := scanner.NewConfig() if err != nil { return nil, fmt.Errorf("failed to load configuration: %w", err) } - err = config.Validate() + err = scannerConfig.Validate() if err != nil { return nil, fmt.Errorf("failed to validate configuration: %w", err) } @@ -56,12 +55,12 @@ func New(_ context.Context) (*Provider, error) { return nil, fmt.Errorf("failed create managed identity credential: %w", err) } - networkClientFactory, err := armnetwork.NewClientFactory(config.SubscriptionID, cred, nil) + networkClientFactory, err := armnetwork.NewClientFactory(scannerConfig.SubscriptionID, cred, nil) if err != nil { return nil, fmt.Errorf("failed to create network client factory: %w", err) } - computeClientFactory, err := armcompute.NewClientFactory(config.SubscriptionID, cred, nil) + computeClientFactory, err := armcompute.NewClientFactory(scannerConfig.SubscriptionID, cred, nil) if err != nil { return nil, fmt.Errorf("failed to create compute client factory: %w", err) } @@ -78,7 +77,7 @@ func New(_ context.Context) (*Provider, error) { DisksClient: computeClientFactory.NewDisksClient(), InterfacesClient: networkClientFactory.NewInterfacesClient(), - Config: config, + Config: scannerConfig, }, Estimator: &estimator.Estimator{}, }, nil diff --git a/provider/v2/azure/scanner/blob.go b/provider/v2/azure/scanner/blob.go index ff2465e52..04fbee523 100644 --- a/provider/v2/azure/scanner/blob.go +++ b/provider/v2/azure/scanner/blob.go @@ -27,7 +27,7 @@ import ( "github.com/openclarity/vmclarity/core/to" "github.com/openclarity/vmclarity/provider" - "github.com/openclarity/vmclarity/provider/v2/azure/common" + "github.com/openclarity/vmclarity/provider/v2/azure/utils" ) var ( @@ -63,19 +63,19 @@ func (s *Scanner) ensureBlobFromSnapshot(ctx context.Context, config *provider.S revokepoller, err := s.SnapshotsClient.BeginRevokeAccess(ctx, s.Config.ScannerResourceGroup, *snapshot.Name, nil) if err != nil { - _, err := common.HandleAzureRequestError(err, "revoking SAS access for snapshot %s", *snapshot.Name) + _, err := utils.HandleAzureRequestError(err, "revoking SAS access for snapshot %s", *snapshot.Name) return blobURL, err } _, err = revokepoller.PollUntilDone(ctx, nil) if err != nil { - _, err := common.HandleAzureRequestError(err, "waiting for SAS access to be revoked for snapshot %s", *snapshot.Name) + _, err := utils.HandleAzureRequestError(err, "waiting for SAS access to be revoked for snapshot %s", *snapshot.Name) return blobURL, err } return blobURL, nil } - notFound, err := common.HandleAzureRequestError(err, "getting blob %s", blobName) + notFound, err := utils.HandleAzureRequestError(err, "getting blob %s", blobName) if !notFound { return blobURL, err } @@ -89,13 +89,13 @@ func (s *Scanner) ensureBlobFromSnapshot(ctx context.Context, config *provider.S DurationInSeconds: to.Ptr[int32](int32(snapshotSASAccessSeconds)), }, nil) if err != nil { - _, err := common.HandleAzureRequestError(err, "granting SAS access to snapshot %s", *snapshot.Name) + _, err := utils.HandleAzureRequestError(err, "granting SAS access to snapshot %s", *snapshot.Name) return blobURL, err } res, err := poller.PollUntilDone(ctx, nil) if err != nil { - _, err := common.HandleAzureRequestError(err, "waiting for SAS access to snapshot %s be granted", *snapshot.Name) + _, err := utils.HandleAzureRequestError(err, "waiting for SAS access to snapshot %s be granted", *snapshot.Name) return blobURL, err } @@ -103,7 +103,7 @@ func (s *Scanner) ensureBlobFromSnapshot(ctx context.Context, config *provider.S _, err = blobClient.StartCopyFromURL(ctx, accessURL, nil) if err != nil { - _, err := common.HandleAzureRequestError(err, "starting copy from URL operation for blob %s", blobName) + _, err := utils.HandleAzureRequestError(err, "starting copy from URL operation for blob %s", blobName) return blobURL, err } @@ -120,7 +120,7 @@ func (s *Scanner) ensureBlobDeleted(ctx context.Context, config *provider.ScanJo getMetadata, err := blobClient.GetProperties(ctx, nil) if err != nil { - notFound, err := common.HandleAzureRequestError(err, "getting blob %s", blobName) + notFound, err := utils.HandleAzureRequestError(err, "getting blob %s", blobName) if notFound { return nil } @@ -131,7 +131,7 @@ func (s *Scanner) ensureBlobDeleted(ctx context.Context, config *provider.ScanJo if copyStatus == blob.CopyStatusTypePending { _, err = blobClient.AbortCopyFromURL(ctx, *getMetadata.CopyID, nil) if err != nil { - _, err := common.HandleAzureRequestError(err, "aborting copy from url for blob %s", blobName) + _, err := utils.HandleAzureRequestError(err, "aborting copy from url for blob %s", blobName) return err } return provider.RetryableErrorf(estimatedBlobAbortTime, "blob copy aborting") @@ -139,7 +139,7 @@ func (s *Scanner) ensureBlobDeleted(ctx context.Context, config *provider.ScanJo _, err = blobClient.Delete(ctx, nil) if err != nil { - _, err := common.HandleAzureRequestError(err, "deleting blob %s", blobName) + _, err := utils.HandleAzureRequestError(err, "deleting blob %s", blobName) return err } diff --git a/provider/v2/azure/common/config.go b/provider/v2/azure/scanner/config.go similarity index 99% rename from provider/v2/azure/common/config.go rename to provider/v2/azure/scanner/config.go index 939be22e5..0a973d9ac 100644 --- a/provider/v2/azure/common/config.go +++ b/provider/v2/azure/scanner/config.go @@ -13,7 +13,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package common +package scanner import ( "encoding/base64" diff --git a/provider/v2/azure/scanner/networkInterface.go b/provider/v2/azure/scanner/networkInterface.go index 21dc9be0e..87b33ffaa 100644 --- a/provider/v2/azure/scanner/networkInterface.go +++ b/provider/v2/azure/scanner/networkInterface.go @@ -24,7 +24,7 @@ import ( "github.com/openclarity/vmclarity/core/to" "github.com/openclarity/vmclarity/provider" - "github.com/openclarity/vmclarity/provider/v2/azure/common" + "github.com/openclarity/vmclarity/provider/v2/azure/utils" ) var ( @@ -48,7 +48,7 @@ func (s *Scanner) ensureNetworkInterface(ctx context.Context, config *provider.S return nicResp.Interface, nil } - notFound, err := common.HandleAzureRequestError(err, "getting interface %s", nicName) + notFound, err := utils.HandleAzureRequestError(err, "getting interface %s", nicName) if !notFound { return armnetwork.Interface{}, err } @@ -75,7 +75,7 @@ func (s *Scanner) ensureNetworkInterface(ctx context.Context, config *provider.S _, err = s.InterfacesClient.BeginCreateOrUpdate(ctx, s.Config.ScannerResourceGroup, nicName, parameters, nil) if err != nil { - _, err := common.HandleAzureRequestError(err, "creating interface %s", nicName) + _, err := utils.HandleAzureRequestError(err, "creating interface %s", nicName) return armnetwork.Interface{}, err } @@ -85,7 +85,7 @@ func (s *Scanner) ensureNetworkInterface(ctx context.Context, config *provider.S func (s *Scanner) ensureNetworkInterfaceDeleted(ctx context.Context, config *provider.ScanJobConfig) error { nicName := networkInterfaceNameFromJobConfig(config) - return common.EnsureDeleted( + return utils.EnsureDeleted( "interface", func() error { _, err := s.InterfacesClient.Get(ctx, s.Config.ScannerResourceGroup, nicName, nil) diff --git a/provider/v2/azure/scanner/scanner.go b/provider/v2/azure/scanner/scanner.go index 8a30231c6..b3dd36f33 100644 --- a/provider/v2/azure/scanner/scanner.go +++ b/provider/v2/azure/scanner/scanner.go @@ -26,7 +26,7 @@ import ( "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/network/armnetwork/v5" "github.com/openclarity/vmclarity/provider" - "github.com/openclarity/vmclarity/provider/v2/azure/common" + "github.com/openclarity/vmclarity/provider/v2/azure/utils" ) const ( @@ -43,7 +43,7 @@ type Scanner struct { DisksClient *armcompute.DisksClient InterfacesClient *armnetwork.InterfacesClient - Config *common.Config + Config *Config } // nolint:cyclop @@ -60,7 +60,7 @@ func (s *Scanner) RunAssetScan(ctx context.Context, config *provider.ScanJobConf assetVM, err := s.VMClient.Get(ctx, resourceGroup, vmName, nil) if err != nil { - _, err = common.HandleAzureRequestError(err, "getting asset virtual machine %s", vmName) + _, err = utils.HandleAzureRequestError(err, "getting asset virtual machine %s", vmName) return err } diff --git a/provider/v2/azure/scanner/scannerVm.go b/provider/v2/azure/scanner/scannerVm.go index 480511fc1..96d355ed7 100644 --- a/provider/v2/azure/scanner/scannerVm.go +++ b/provider/v2/azure/scanner/scannerVm.go @@ -28,7 +28,7 @@ import ( "github.com/openclarity/vmclarity/core/to" "github.com/openclarity/vmclarity/provider" "github.com/openclarity/vmclarity/provider/cloudinit" - "github.com/openclarity/vmclarity/provider/v2/azure/common" + "github.com/openclarity/vmclarity/provider/v2/azure/utils" ) var ( @@ -52,7 +52,7 @@ func (s *Scanner) ensureScannerVirtualMachine(ctx context.Context, config *provi return vmResp.VirtualMachine, nil } - notFound, err := common.HandleAzureRequestError(err, "getting scanner virtual machine: %s", vmName) + notFound, err := utils.HandleAzureRequestError(err, "getting scanner virtual machine: %s", vmName) if !notFound { return armcompute.VirtualMachine{}, err } @@ -124,7 +124,7 @@ func (s *Scanner) ensureScannerVirtualMachine(ctx context.Context, config *provi _, err = s.VMClient.BeginCreateOrUpdate(ctx, s.Config.ScannerResourceGroup, vmName, parameters, nil) if err != nil { - _, err = common.HandleAzureRequestError(err, "creating virtual machine") + _, err = utils.HandleAzureRequestError(err, "creating virtual machine") return armcompute.VirtualMachine{}, err } @@ -134,7 +134,7 @@ func (s *Scanner) ensureScannerVirtualMachine(ctx context.Context, config *provi func (s *Scanner) ensureScannerVirtualMachineDeleted(ctx context.Context, config *provider.ScanJobConfig) error { vmName := scannerVMNameFromJobConfig(config) - return common.EnsureDeleted( + return utils.EnsureDeleted( "virtual machine", func() error { _, err := s.VMClient.Get(ctx, s.Config.ScannerResourceGroup, vmName, nil) @@ -171,14 +171,14 @@ func (s *Scanner) ensureDiskAttachedToScannerVM(ctx context.Context, vm armcompu _, err := s.VMClient.BeginCreateOrUpdate(ctx, s.Config.ScannerResourceGroup, *vm.Name, vm, nil) if err != nil { - _, err := common.HandleAzureRequestError(err, "attaching disk %s to VM %s", *disk.Name, *vm.Name) + _, err := utils.HandleAzureRequestError(err, "attaching disk %s to VM %s", *disk.Name, *vm.Name) return err } } diskResp, err := s.DisksClient.Get(ctx, s.Config.ScannerResourceGroup, *disk.Name, nil) if err != nil { - _, err := common.HandleAzureRequestError(err, "getting disk %s", *disk.Name) + _, err := utils.HandleAzureRequestError(err, "getting disk %s", *disk.Name) return err } diff --git a/provider/v2/azure/scanner/snapshot.go b/provider/v2/azure/scanner/snapshot.go index 9518b7638..237adf80e 100644 --- a/provider/v2/azure/scanner/snapshot.go +++ b/provider/v2/azure/scanner/snapshot.go @@ -24,7 +24,7 @@ import ( "github.com/openclarity/vmclarity/core/to" "github.com/openclarity/vmclarity/provider" - "github.com/openclarity/vmclarity/provider/v2/azure/common" + "github.com/openclarity/vmclarity/provider/v2/azure/utils" ) var ( @@ -49,7 +49,7 @@ func (s *Scanner) ensureSnapshotForVMRootVolume(ctx context.Context, config *pro return snapshotRes.Snapshot, nil } - notFound, err := common.HandleAzureRequestError(err, "getting snapshot %s", snapshotName) + notFound, err := utils.HandleAzureRequestError(err, "getting snapshot %s", snapshotName) if !notFound { return armcompute.Snapshot{}, err } @@ -64,7 +64,7 @@ func (s *Scanner) ensureSnapshotForVMRootVolume(ctx context.Context, config *pro }, }, nil) if err != nil { - _, err := common.HandleAzureRequestError(err, "creating snapshot %s", snapshotName) + _, err := utils.HandleAzureRequestError(err, "creating snapshot %s", snapshotName) return armcompute.Snapshot{}, err } @@ -75,7 +75,7 @@ func (s *Scanner) ensureSnapshotDeleted(ctx context.Context, config *provider.Sc snapshotName := snapshotNameFromJobConfig(config) // nolint:wrapcheck - return common.EnsureDeleted( + return utils.EnsureDeleted( "snapshot", func() error { _, err := s.SnapshotsClient.Get(ctx, s.Config.ScannerResourceGroup, snapshotName, nil) diff --git a/provider/v2/azure/scanner/targetDisk.go b/provider/v2/azure/scanner/targetDisk.go index c5ebb5344..9d1c6c49a 100644 --- a/provider/v2/azure/scanner/targetDisk.go +++ b/provider/v2/azure/scanner/targetDisk.go @@ -25,7 +25,7 @@ import ( "github.com/openclarity/vmclarity/core/to" "github.com/openclarity/vmclarity/provider" - "github.com/openclarity/vmclarity/provider/v2/azure/common" + "github.com/openclarity/vmclarity/provider/v2/azure/utils" ) var ( @@ -49,7 +49,7 @@ func (s *Scanner) ensureManagedDiskFromSnapshot(ctx context.Context, config *pro return volumeRes.Disk, nil } - notFound, err := common.HandleAzureRequestError(err, "getting volume %s", volumeName) + notFound, err := utils.HandleAzureRequestError(err, "getting volume %s", volumeName) if !notFound { return armcompute.Disk{}, err } @@ -67,7 +67,7 @@ func (s *Scanner) ensureManagedDiskFromSnapshot(ctx context.Context, config *pro }, }, nil) if err != nil { - _, err := common.HandleAzureRequestError(err, "creating disk %s", volumeName) + _, err := utils.HandleAzureRequestError(err, "creating disk %s", volumeName) return armcompute.Disk{}, err } @@ -91,7 +91,7 @@ func (s *Scanner) ensureManagedDiskFromSnapshotInDifferentRegion(ctx context.Con return volumeRes.Disk, nil } - notFound, err := common.HandleAzureRequestError(err, "getting volume %s", volumeName) + notFound, err := utils.HandleAzureRequestError(err, "getting volume %s", volumeName) if !notFound { return armcompute.Disk{}, err } @@ -110,7 +110,7 @@ func (s *Scanner) ensureManagedDiskFromSnapshotInDifferentRegion(ctx context.Con }, }, nil) if err != nil { - _, err := common.HandleAzureRequestError(err, "creating disk %s", volumeName) + _, err := utils.HandleAzureRequestError(err, "creating disk %s", volumeName) return armcompute.Disk{}, err } return armcompute.Disk{}, provider.RetryableErrorf(DiskEstimateProvisionTime, "disk creating") @@ -119,7 +119,7 @@ func (s *Scanner) ensureManagedDiskFromSnapshotInDifferentRegion(ctx context.Con func (s *Scanner) ensureTargetDiskDeleted(ctx context.Context, config *provider.ScanJobConfig) error { volumeName := volumeNameFromJobConfig(config) - return common.EnsureDeleted( + return utils.EnsureDeleted( "target disk", func() error { _, err := s.DisksClient.Get(ctx, s.Config.ScannerResourceGroup, volumeName, nil) diff --git a/provider/v2/azure/common/common.go b/provider/v2/azure/utils/utils.go similarity index 99% rename from provider/v2/azure/common/common.go rename to provider/v2/azure/utils/utils.go index d459910f1..86cec8b3b 100644 --- a/provider/v2/azure/common/common.go +++ b/provider/v2/azure/utils/utils.go @@ -13,7 +13,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package common +package utils import ( "errors"