Skip to content

Commit

Permalink
data_source_key_vault, resource_arm_key_vault, resource_arm_automatio…
Browse files Browse the repository at this point in the history
…n_account, resource_arm_notification_hub_namespace, resource_arm_relay_namespace: Flatten SKU (#3119)

related to #1500
  • Loading branch information
WodansSon authored and katbyte committed Jun 27, 2019
1 parent 3b0cbe6 commit 5686e47
Show file tree
Hide file tree
Showing 24 changed files with 705 additions and 242 deletions.
20 changes: 18 additions & 2 deletions azurerm/data_source_key_vault.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func dataSourceArmKeyVault() *schema.Resource {

"location": azure.SchemaLocationForDataSource(),

// Remove in 2.0
"sku": {
Type: schema.TypeList,
Computed: true,
Expand All @@ -39,6 +40,11 @@ func dataSourceArmKeyVault() *schema.Resource {
},
},

"sku_name": {
Type: schema.TypeString,
Computed: true,
},

"vault_uri": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -177,8 +183,17 @@ func dataSourceArmKeyVaultRead(d *schema.ResourceData, meta interface{}) error {
d.Set("enabled_for_template_deployment", props.EnabledForTemplateDeployment)
d.Set("vault_uri", props.VaultURI)

if err := d.Set("sku", flattenKeyVaultDataSourceSku(props.Sku)); err != nil {
return fmt.Errorf("Error setting `sku` for KeyVault %q: %+v", *resp.Name, err)
if sku := props.Sku; sku != nil {
// Remove in 2.0
if err := d.Set("sku", flattenKeyVaultDataSourceSku(sku)); err != nil {
return fmt.Errorf("Error setting `sku` for KeyVault %q: %+v", *resp.Name, err)
}

if err := d.Set("sku_name", string(sku.Name)); err != nil {
return fmt.Errorf("Error setting `sku_name` for KeyVault %q: %+v", *resp.Name, err)
}
} else {
return fmt.Errorf("Error making Read request on KeyVault %q: Unable to retrieve 'sku' value", *resp.Name)
}

flattenedPolicies := azure.FlattenKeyVaultAccessPolicies(props.AccessPolicies)
Expand All @@ -196,6 +211,7 @@ func dataSourceArmKeyVaultRead(d *schema.ResourceData, meta interface{}) error {
return nil
}

// Remove in 2.0
func flattenKeyVaultDataSourceSku(sku *keyvault.Sku) []interface{} {
result := map[string]interface{}{
"name": string(sku.Name),
Expand Down
28 changes: 28 additions & 0 deletions azurerm/data_source_key_vault_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,34 @@ func TestAccDataSourceAzureRMKeyVault_basic(t *testing.T) {
location := testLocation()
config := testAccDataSourceAzureRMKeyVault_basic(ri, location)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMKeyVaultDestroy,
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMKeyVaultExists(dataSourceName),
resource.TestCheckResourceAttrSet(dataSourceName, "tenant_id"),
resource.TestCheckResourceAttrSet(dataSourceName, "sku_name"),
resource.TestCheckResourceAttrSet(dataSourceName, "access_policy.0.tenant_id"),
resource.TestCheckResourceAttrSet(dataSourceName, "access_policy.0.object_id"),
resource.TestCheckResourceAttr(dataSourceName, "access_policy.0.key_permissions.0", "create"),
resource.TestCheckResourceAttr(dataSourceName, "access_policy.0.secret_permissions.0", "set"),
resource.TestCheckResourceAttr(dataSourceName, "tags.%", "0"),
),
},
},
})
}

func TestAccDataSourceAzureRMKeyVault_basicClassic(t *testing.T) {
dataSourceName := "data.azurerm_key_vault.test"
ri := tf.AccRandTimeInt()
location := testLocation()
config := testAccDataSourceAzureRMKeyVault_basic(ri, location)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Expand Down
108 changes: 70 additions & 38 deletions azurerm/resource_arm_automation_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,19 @@ func resourceArmAutomationAccount() *schema.Resource {

"resource_group_name": azure.SchemaResourceGroupName(),

// Remove in 2.0
"sku": {
Type: schema.TypeList,
Required: true,
MaxItems: 1,
Type: schema.TypeList,
Optional: true,
Computed: true,
Deprecated: "This property has been deprecated in favour of the 'sku_name' property and will be removed in version 2.0 of the provider",
ConflictsWith: []string{"sku_name"},
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Optional: true,
Default: string(automation.Basic),
DiffSuppressFunc: suppress.CaseDifference,
ValidateFunc: validation.StringInSlice([]string{
string(automation.Basic),
Expand All @@ -60,6 +63,17 @@ func resourceArmAutomationAccount() *schema.Resource {
},
},

"sku_name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ConflictsWith: []string{"sku"},
ValidateFunc: validation.StringInSlice([]string{
string(automation.Basic),
string(automation.Free),
}, false),
},

"tags": tagsSchema(),

"dsc_server_endpoint": {
Expand All @@ -82,16 +96,37 @@ func resourceArmAutomationAccountCreateUpdate(d *schema.ResourceData, meta inter
client := meta.(*ArmClient).automation.AccountClient
ctx := meta.(*ArmClient).StopContext

// Remove in 2.0
var sku automation.Sku

if inputs := d.Get("sku").([]interface{}); len(inputs) != 0 {
input := inputs[0].(map[string]interface{})
v := input["name"].(string)

sku = automation.Sku{
Name: automation.SkuNameEnum(v),
}
} else {
// Keep in 2.0
sku = automation.Sku{
Name: automation.SkuNameEnum(d.Get("sku_name").(string)),
}
}

if sku.Name == "" {
return fmt.Errorf("either 'sku_name' or 'sku' must be defined in the configuration file")
}

log.Printf("[INFO] preparing arguments for Automation Account create/update.")

name := d.Get("name").(string)
resGroup := d.Get("resource_group_name").(string)
resourceGroup := d.Get("resource_group_name").(string)

if requireResourcesToBeImported && d.IsNewResource() {
existing, err := client.Get(ctx, resGroup, name)
existing, err := client.Get(ctx, resourceGroup, name)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("Error checking for presence of existing Automation Account %q (Resource Group %q): %s", name, resGroup, err)
return fmt.Errorf("Error checking for presence of existing Automation Account %q (Resource Group %q): %s", name, resourceGroup, err)
}
}

Expand All @@ -102,27 +137,26 @@ func resourceArmAutomationAccountCreateUpdate(d *schema.ResourceData, meta inter

location := azure.NormalizeLocation(d.Get("location").(string))
tags := d.Get("tags").(map[string]interface{})
sku := expandAutomationAccountSku(d)

parameters := automation.AccountCreateOrUpdateParameters{
AccountCreateOrUpdateProperties: &automation.AccountCreateOrUpdateProperties{
Sku: sku,
Sku: &sku,
},
Location: utils.String(location),
Tags: expandTags(tags),
}

if _, err := client.CreateOrUpdate(ctx, resGroup, name, parameters); err != nil {
return fmt.Errorf("Error creating/updating Automation Account %q (Resource Group %q) %+v", name, resGroup, err)
if _, err := client.CreateOrUpdate(ctx, resourceGroup, name, parameters); err != nil {
return fmt.Errorf("Error creating/updating Automation Account %q (Resource Group %q) %+v", name, resourceGroup, err)
}

read, err := client.Get(ctx, resGroup, name)
read, err := client.Get(ctx, resourceGroup, name)
if err != nil {
return fmt.Errorf("Error retrieving Automation Account %q (Resource Group %q) %+v", name, resGroup, err)
return fmt.Errorf("Error retrieving Automation Account %q (Resource Group %q) %+v", name, resourceGroup, err)
}

if read.ID == nil {
return fmt.Errorf("Cannot read Automation Account %q (Resource Group %q) ID", name, resGroup)
return fmt.Errorf("Cannot read Automation Account %q (Resource Group %q) ID", name, resourceGroup)
}

d.SetId(*read.ID)
Expand All @@ -139,39 +173,48 @@ func resourceArmAutomationAccountRead(d *schema.ResourceData, meta interface{})
if err != nil {
return err
}
resGroup := id.ResourceGroup
resourceGroup := id.ResourceGroup
name := id.Path["automationAccounts"]

resp, err := client.Get(ctx, resGroup, name)
resp, err := client.Get(ctx, resourceGroup, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[DEBUG] Automation Account %q was not found in Resource Group %q - removing from state!", name, resGroup)
log.Printf("[DEBUG] Automation Account %q was not found in Resource Group %q - removing from state!", name, resourceGroup)
d.SetId("")
return nil
}

return fmt.Errorf("Error making Read request on Automation Account %q (Resource Group %q): %+v", name, resGroup, err)
return fmt.Errorf("Error making Read request on Automation Account %q (Resource Group %q): %+v", name, resourceGroup, err)
}

keysResp, err := registrationClient.Get(ctx, resGroup, name)
keysResp, err := registrationClient.Get(ctx, resourceGroup, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[DEBUG] Agent Registration Info for Automation Account %q was not found in Resource Group %q - removing from state!", name, resGroup)
log.Printf("[DEBUG] Agent Registration Info for Automation Account %q was not found in Resource Group %q - removing from state!", name, resourceGroup)
d.SetId("")
return nil
}

return fmt.Errorf("Error making Read request for Agent Registration Info for Automation Account %q (Resource Group %q): %+v", name, resGroup, err)
return fmt.Errorf("Error making Read request for Agent Registration Info for Automation Account %q (Resource Group %q): %+v", name, resourceGroup, err)
}

d.Set("name", resp.Name)
d.Set("resource_group_name", resGroup)
d.Set("resource_group_name", resourceGroup)
if location := resp.Location; location != nil {
d.Set("location", azure.NormalizeLocation(*location))
}

if err := d.Set("sku", flattenAutomationAccountSku(resp.Sku)); err != nil {
return fmt.Errorf("Error setting `sku`: %+v", err)
if sku := resp.Sku; sku != nil {
// Remove in 2.0
if err := d.Set("sku", flattenAutomationAccountSku(sku)); err != nil {
return fmt.Errorf("Error setting 'sku': %+v", err)
}

if err := d.Set("sku_name", string(sku.Name)); err != nil {
return fmt.Errorf("Error setting 'sku_name': %+v", err)
}
} else {
return fmt.Errorf("Error making Read request on Automation Account %q (Resource Group %q): Unable to retrieve 'sku' value", name, resourceGroup)
}

d.Set("dsc_server_endpoint", keysResp.Endpoint)
Expand All @@ -195,10 +238,10 @@ func resourceArmAutomationAccountDelete(d *schema.ResourceData, meta interface{}
if err != nil {
return err
}
resGroup := id.ResourceGroup
resourceGroup := id.ResourceGroup
name := id.Path["automationAccounts"]

resp, err := client.Delete(ctx, resGroup, name)
resp, err := client.Delete(ctx, resourceGroup, name)

if err != nil {
if utils.ResponseWasNotFound(resp) {
Expand All @@ -211,6 +254,7 @@ func resourceArmAutomationAccountDelete(d *schema.ResourceData, meta interface{}
return nil
}

// Remove in 2.0
func flattenAutomationAccountSku(sku *automation.Sku) []interface{} {
if sku == nil {
return []interface{}{}
Expand All @@ -220,15 +264,3 @@ func flattenAutomationAccountSku(sku *automation.Sku) []interface{} {
result["name"] = string(sku.Name)
return []interface{}{result}
}

func expandAutomationAccountSku(d *schema.ResourceData) *automation.Sku {
inputs := d.Get("sku").([]interface{})
input := inputs[0].(map[string]interface{})
name := automation.SkuNameEnum(input["name"].(string))

sku := automation.Sku{
Name: name,
}

return &sku
}
Loading

0 comments on commit 5686e47

Please sign in to comment.