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

Commit

Permalink
refactor: common.go to utils, config.go to scanner package
Browse files Browse the repository at this point in the history
Signed-off-by: András Jáky <[email protected]>
  • Loading branch information
akijakya committed Feb 26, 2024
1 parent c034ac0 commit a8ae5bd
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 41 deletions.
11 changes: 5 additions & 6 deletions provider/v2/azure/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -78,7 +77,7 @@ func New(_ context.Context) (*Provider, error) {
DisksClient: computeClientFactory.NewDisksClient(),
InterfacesClient: networkClientFactory.NewInterfacesClient(),

Config: config,
Config: scannerConfig,
},
Estimator: &estimator.Estimator{},
}, nil
Expand Down
20 changes: 10 additions & 10 deletions provider/v2/azure/scanner/blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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
}
Expand All @@ -89,21 +89,21 @@ 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
}

accessURL := *res.AccessURI.AccessSAS

_, 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
}

Expand All @@ -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
}
Expand All @@ -131,15 +131,15 @@ 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")
}

_, 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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
8 changes: 4 additions & 4 deletions provider/v2/azure/scanner/networkInterface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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
}
Expand All @@ -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
}

Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions provider/v2/azure/scanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -43,7 +43,7 @@ type Scanner struct {
DisksClient *armcompute.DisksClient
InterfacesClient *armnetwork.InterfacesClient

Config *common.Config
Config *Config
}

// nolint:cyclop
Expand All @@ -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
}

Expand Down
12 changes: 6 additions & 6 deletions provider/v2/azure/scanner/scannerVm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}

Expand All @@ -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)
Expand Down Expand Up @@ -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
}

Expand Down
8 changes: 4 additions & 4 deletions provider/v2/azure/scanner/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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
}
Expand All @@ -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
}

Expand All @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions provider/v2/azure/scanner/targetDisk.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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
}
Expand All @@ -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
}

Expand All @@ -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
}
Expand All @@ -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")
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package common
package utils

import (
"errors"
Expand Down

0 comments on commit a8ae5bd

Please sign in to comment.