Skip to content

Commit

Permalink
SCALRCORE-25049 fix scalr_provider_configuration import
Browse files Browse the repository at this point in the history
  • Loading branch information
RomanMytsko committed Apr 28, 2023
1 parent acb2bac commit 1181904
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 2 deletions.
72 changes: 70 additions & 2 deletions scalr/resource_scalr_provider_configuration.go
Expand Up @@ -37,7 +37,7 @@ func resourceScalrProviderConfiguration() *schema.Resource {
},
),
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
StateContext: resourceScalrProviderConfigurationImport,
},
SchemaVersion: 0,
Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -677,7 +677,75 @@ func resourceScalrProviderConfigurationDelete(ctx context.Context, d *schema.Res
return nil
}

// changeParameters is used to change parameters for provider configuratio.
func resourceScalrProviderConfigurationImport(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
id := d.Id()
scalrClient := meta.(*scalr.Client)

providerConfiguration, err := scalrClient.ProviderConfigurations.Read(ctx, id)
if err != nil {
return nil, fmt.Errorf("unable to retrieve provider configuration %s: %v", id, err)
}

switch providerConfiguration.ProviderName {
case "aws":
awsParams := make(map[string]interface{})

awsParams["account_type"] = providerConfiguration.AwsAccountType
awsParams["credentials_type"] = providerConfiguration.AwsCredentialsType
awsParams["access_key"] = providerConfiguration.AwsAccessKey
awsParams["secret_key"] = providerConfiguration.AwsSecretKey
awsParams["trusted_entity_type"] = providerConfiguration.AwsTrustedEntityType
awsParams["role_arn"] = providerConfiguration.AwsRoleArn
awsParams["external_id"] = providerConfiguration.AwsExternalId

_ = d.Set("aws", []map[string]interface{}{awsParams})
case "google":
googleParams := make(map[string]interface{})

googleParams["credentials"] = providerConfiguration.GoogleCredentials
googleParams["project"] = providerConfiguration.GoogleProject

_ = d.Set("google", []map[string]interface{}{googleParams})
case "scalr":
scalrParams := make(map[string]interface{})

scalrParams["hostname"] = providerConfiguration.ScalrHostname
scalrParams["token"] = providerConfiguration.ScalrToken

_ = d.Set("scalr", []map[string]interface{}{scalrParams})
case "azurerm":
azurermParams := make(map[string]interface{})

azurermParams["client_id"] = providerConfiguration.AzurermClientId
azurermParams["client_secret"] = providerConfiguration.AzurermClientSecret
azurermParams["subscription_id"] = providerConfiguration.AzurermSubscriptionId
azurermParams["tenant_id"] = providerConfiguration.AzurermTenantId

_ = d.Set("azurerm", []map[string]interface{}{azurermParams})
default:
var currentArguments []map[string]interface{}
for _, argument := range providerConfiguration.Parameters {
currentArgument := map[string]interface{}{
"name": argument.Key,
"sensitive": argument.Sensitive,
"value": argument.Value,
"description": argument.Description,
}
currentArguments = append(currentArguments, currentArgument)
}

_ = d.Set("custom", []map[string]interface{}{
{
"provider_name": providerConfiguration.ProviderName,
"argument": currentArguments,
},
})
}

return []*schema.ResourceData{d}, nil
}

// changeParameters is used to change parameters for provider configuration.
func changeParameters(
ctx context.Context,
client *scalr.Client,
Expand Down
51 changes: 51 additions & 0 deletions scalr/resource_scalr_provider_configuration_test.go
Expand Up @@ -14,6 +14,28 @@ import (
"github.com/scalr/go-scalr"
)

func TestAccProviderConfiguration_import(t *testing.T) {
rName := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)

resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
},
ProviderFactories: testAccProviderFactories,
CheckDestroy: testAccCheckProviderConfigurationResourceDestroy,
Steps: []resource.TestStep{
{
Config: testAccScalrProviderConfigurationCustomImportConfig(rName),
},
{
ResourceName: "scalr_provider_configuration.kubernetes",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccProviderConfiguration_custom(t *testing.T) {
var providerConfiguration scalr.ProviderConfiguration
rName := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
Expand Down Expand Up @@ -641,6 +663,35 @@ resource "scalr_provider_configuration" "kubernetes" {
}
`, name, defaultAccount)
}

func testAccScalrProviderConfigurationCustomImportConfig(name string) string {
return fmt.Sprintf(`
resource "scalr_provider_configuration" "kubernetes" {
name = "%s"
account_id = "%s"
environments = ["*"]
custom {
provider_name = "kubernetes"
argument {
name = "config_path"
value = "~/.kube/config"
sensitive = false
description = "A path to a kube config file. some typo..."
}
argument {
name = "client_id"
value = "ID18021989"
sensitive = false
}
argument {
name = "host"
value = "my-host"
}
}
}
`, name, defaultAccount)
}

func testAccScalrProviderConfigurationCustomConfigUpdated(name string) string {
return fmt.Sprintf(`
resource "scalr_environment" "test" {
Expand Down

0 comments on commit 1181904

Please sign in to comment.