Skip to content

Commit

Permalink
Clean up and call out existing issue
Browse files Browse the repository at this point in the history
  • Loading branch information
jdoldis committed Sep 29, 2024
1 parent 1704a50 commit c3417a2
Showing 1 changed file with 47 additions and 43 deletions.
90 changes: 47 additions & 43 deletions pkg/resources/external_volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func ImportExternalVolume(ctx context.Context, d *schema.ResourceData, meta any)
return []*schema.ResourceData{d}, nil
}

func extractStorageLocations(v any, withAzureEncryptionCheck bool) ([]sdk.ExternalVolumeStorageLocation, error) {
func extractStorageLocations(v any, withAzureEncryptionTypeCheck bool) ([]sdk.ExternalVolumeStorageLocation, error) {
_, ok := v.([]any)
if v == nil || !ok {
return nil, fmt.Errorf("unable to extract storage locations, input is either nil or non expected type (%T): %v", v, v)
Expand All @@ -212,7 +212,7 @@ func extractStorageLocations(v any, withAzureEncryptionCheck bool) ([]sdk.Extern
return nil, fmt.Errorf("unable to extract storage location, missing storage_provider key in storage location")
}

storage_base_url, ok := storageLocationConfig["storage_base_url"].(string)
storageBaseUrl, ok := storageLocationConfig["storage_base_url"].(string)
if !ok {
return nil, fmt.Errorf("unable to extract storage location, missing storage_base_url key in storage location")
}
Expand All @@ -227,13 +227,13 @@ func extractStorageLocations(v any, withAzureEncryptionCheck bool) ([]sdk.Extern
case sdk.StorageProviderS3, sdk.StorageProviderS3GOV:
// Test that azure_tenant_id is not given
// If given non empty plans will be produced
azure_tenant_id, ok := storageLocationConfig["azure_tenant_id"].(string)
if ok && len(azure_tenant_id) > 0 {
azureTenantId, ok := storageLocationConfig["azure_tenant_id"].(string)
if ok && len(azureTenantId) > 0 {
return nil, fmt.Errorf("unable to extract storage location, azure_tenant_id provided for s3 storage location")
}

storage_aws_role_arn, ok := storageLocationConfig["storage_aws_role_arn"].(string)
if !ok || len(storage_aws_role_arn) == 0 {
storageAwsRoleArn, ok := storageLocationConfig["storage_aws_role_arn"].(string)
if !ok || len(storageAwsRoleArn) == 0 {
return nil, fmt.Errorf("unable to extract storage location, missing storage_aws_role_arn key in an s3 storage location")
}

Expand All @@ -245,22 +245,22 @@ func extractStorageLocations(v any, withAzureEncryptionCheck bool) ([]sdk.Extern
s3StorageLocation := &sdk.S3StorageLocationParams{
Name: name,
StorageProvider: s3StorageProvider,
StorageBaseUrl: storage_base_url,
StorageAwsRoleArn: storage_aws_role_arn,
StorageBaseUrl: storageBaseUrl,
StorageAwsRoleArn: storageAwsRoleArn,
}

encryption_type, ok := storageLocationConfig["encryption_type"].(string)
if ok && len(encryption_type) > 0 {
encryptionTypeParsed, err := sdk.ToS3EncryptionType(encryption_type)
encryptionType, ok := storageLocationConfig["encryption_type"].(string)
if ok && len(encryptionType) > 0 {
encryptionTypeParsed, err := sdk.ToS3EncryptionType(encryptionType)
if err != nil {
return nil, err
}

encryption_kms_key_id, ok := storageLocationConfig["encryption_kms_key_id"].(string)
if ok && len(encryption_kms_key_id) > 0 {
encryptionKmsKeyId, ok := storageLocationConfig["encryption_kms_key_id"].(string)
if ok && len(encryptionKmsKeyId) > 0 {
s3StorageLocation.Encryption = &sdk.ExternalVolumeS3Encryption{
Type: encryptionTypeParsed,
KmsKeyId: &encryption_kms_key_id,
KmsKeyId: &encryptionKmsKeyId,
}
} else {
s3StorageLocation.Encryption = &sdk.ExternalVolumeS3Encryption{
Expand All @@ -275,30 +275,30 @@ func extractStorageLocations(v any, withAzureEncryptionCheck bool) ([]sdk.Extern
case sdk.StorageProviderGCS:
// Test that azure_tenant_id and storage_aws_role_arn are not given
// If given non empty plans will be produced
azure_tenant_id, ok := storageLocationConfig["azure_tenant_id"].(string)
storage_aws_role_arn, ok := storageLocationConfig["storage_aws_role_arn"].(string)
if ok && len(azure_tenant_id) > 0 {
azureTenantId, ok := storageLocationConfig["azure_tenant_id"].(string)
storageAwsRoleArn, ok := storageLocationConfig["storage_aws_role_arn"].(string)
if ok && len(azureTenantId) > 0 {
return nil, fmt.Errorf("unable to extract storage location, azure_tenant_id provided for gcs storage location")
}
if ok && len(storage_aws_role_arn) > 0 {
if ok && len(storageAwsRoleArn) > 0 {
return nil, fmt.Errorf("unable to extract storage location, storage_aws_role_arn provided for gcs storage location")
}

gcsStorageLocation := &sdk.GCSStorageLocationParams{
Name: name,
StorageBaseUrl: storage_base_url,
StorageBaseUrl: storageBaseUrl,
}
encryption_type, ok := storageLocationConfig["encryption_type"].(string)
if ok && len(encryption_type) > 0 {
encryptionTypeParsed, err := sdk.ToGCSEncryptionType(encryption_type)
encryptionType, ok := storageLocationConfig["encryption_type"].(string)
if ok && len(encryptionType) > 0 {
encryptionTypeParsed, err := sdk.ToGCSEncryptionType(encryptionType)
if err != nil {
return nil, err
}
encryption_kms_key_id, ok := storageLocationConfig["encryption_kms_key_id"].(string)
if ok && len(encryption_kms_key_id) > 0 {
encryptionKmsKeyId, ok := storageLocationConfig["encryption_kms_key_id"].(string)
if ok && len(encryptionKmsKeyId) > 0 {
gcsStorageLocation.Encryption = &sdk.ExternalVolumeGCSEncryption{
Type: encryptionTypeParsed,
KmsKeyId: &encryption_kms_key_id,
KmsKeyId: &encryptionKmsKeyId,
}
} else {
gcsStorageLocation.Encryption = &sdk.ExternalVolumeGCSEncryption{
Expand All @@ -311,37 +311,41 @@ func extractStorageLocations(v any, withAzureEncryptionCheck bool) ([]sdk.Extern
GCSStorageLocationParams: gcsStorageLocation,
}
case sdk.StorageProviderAzure:
// Test that storage_aws_role_arn is not given
// Test that storage_aws_role_arn and encryption_kms_key_id is not given
// If given non empty plans will be produced
storage_aws_role_arn, ok := storageLocationConfig["storage_aws_role_arn"].(string)
if ok && len(storage_aws_role_arn) > 0 {
storageAwsRolArn, ok := storageLocationConfig["storage_aws_role_arn"].(string)
encryptionKmsKeyId, ok := storageLocationConfig["encryption_kms_key_id"].(string)
if ok && len(storageAwsRolArn) > 0 {
return nil, fmt.Errorf("unable to extract storage location, storage_aws_role_arn provided for azure storage location")
}
if ok && len(encryptionKmsKeyId) > 0 {
return nil, fmt.Errorf("unable to extract storage location, encryption_kms_key_id provided for azure storage location")
}

// Encryption fields are set on describe requests, although they are not a valid parameter.
// Allowing optional checking of these fields as they will be present in some cases.
if withAzureEncryptionCheck {
encryption_type, ok := storageLocationConfig["encryption_type"].(string)
if ok && len(encryption_type) > 0 {
// TODO
// For some reason, the new storage_locations list in the update function has NONE as the value
// for encryption_type for azure storage locations, even when it is not specified in the config.
// To get the acceptance tests passing for now the check has been turned off for this case,
// but why it's happning in the first place needs to be investigated.
// To reproduce, turn on the check for extracting new external volumes in the update function and
// run the TestAcc_External_Volume_Multiple acceptance test.
if withAzureEncryptionTypeCheck {
encryptionType, ok := storageLocationConfig["encryption_type"].(string)
if ok && len(encryptionType) > 0 {
return nil, fmt.Errorf("unable to extract storage location, encryption_type provided for azure storage location")
}

encryption_kms_key_id, ok := storageLocationConfig["encryption_kms_key_id"].(string)
if ok && len(encryption_kms_key_id) > 0 {
return nil, fmt.Errorf("unable to extract storage location, encryption_kms_key_id provided for azure storage location")
}
}

azure_tenant_id, ok := storageLocationConfig["azure_tenant_id"].(string)
if !ok || len(azure_tenant_id) == 0 {
azureTenantId, ok := storageLocationConfig["azure_tenant_id"].(string)
if !ok || len(azureTenantId) == 0 {
return nil, fmt.Errorf("unable to extract storage location, missing azure_tenant_id provider key in an azure storage location")
}

storageLocation = sdk.ExternalVolumeStorageLocation{
AzureStorageLocationParams: &sdk.AzureStorageLocationParams{
Name: name,
AzureTenantId: azure_tenant_id,
StorageBaseUrl: storage_base_url,
AzureTenantId: azureTenantId,
StorageBaseUrl: storageBaseUrl,
},
}
}
Expand Down Expand Up @@ -607,7 +611,7 @@ func UpdateContextExternalVolume(ctx context.Context, d *schema.ResourceData, me

if d.HasChange("storage_location") {
old, new := d.GetChange("storage_location")
oldLocations, err := extractStorageLocations(old, false)
oldLocations, err := extractStorageLocations(old, true)
if err != nil {
return diag.FromErr(err)
}
Expand Down

0 comments on commit c3417a2

Please sign in to comment.