Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

data_source_key_vault, resource_arm_key_vault, resource_arm_automation_account, resource_arm_notification_hub_namespace, resource_arm_relay_namespace: Flatten SKU #3119

Merged
merged 30 commits into from
Jun 27, 2019
Merged
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
ed02bc9
Flatten azurerm_automation_account sku
WodansSon Mar 22, 2019
4376934
Flatten azurerm_key_vault sku
WodansSon Mar 22, 2019
857900f
Flatten azurerm_notification_hub sku
WodansSon Mar 22, 2019
f0f0f7d
Flatten azurerm_relay_namespace sku
WodansSon Mar 23, 2019
8e73fe9
Fix lint error
WodansSon Mar 25, 2019
d630381
Update for backward compatibility
WodansSon Apr 3, 2019
a68fd36
Updates for backwards compatibility
WodansSon Apr 3, 2019
96cf7e3
Merge branch 'master' into e_flatten_sku
WodansSon Apr 22, 2019
106e746
Update website/docs/r/automation_account.html.markdown
WodansSon Jun 20, 2019
2764db9
Update website/docs/r/key_vault.html.markdown
WodansSon Jun 20, 2019
d15ee4c
Update website/docs/r/notification_hub_namespace.html.markdown
WodansSon Jun 20, 2019
907f35e
Update website/docs/r/relay_namespace.html.markdown
WodansSon Jun 20, 2019
2bd0c80
Addressed PR comments...
WodansSon Jun 20, 2019
699cd49
Merge branch 'master' into e_flatten_sku
WodansSon Jun 20, 2019
a2f3528
Fix lint and build errors
WodansSon Jun 20, 2019
c9a9fae
Merge branch 'e_flatten_sku' of https://github.com/terraform-provider…
WodansSon Jun 20, 2019
f907f33
Fix CI\CD issues
WodansSon Jun 21, 2019
9781279
Ran gofmt on files...
WodansSon Jun 21, 2019
c642cdc
Updates per PR
WodansSon Jun 26, 2019
465a3ee
Update for PR
WodansSon Jun 26, 2019
c7cb43b
Fix lint error
WodansSon Jun 26, 2019
a406b85
Updated CreateUpdate for all resources
WodansSon Jun 27, 2019
4bd21fa
Fixed relay.SkuName issue
WodansSon Jun 27, 2019
7ac5cef
Fixed Travis Errors
WodansSon Jun 27, 2019
0c8c18b
Update azurerm/resource_arm_relay_namespace.go
WodansSon Jun 27, 2019
962aec1
Update azurerm/resource_arm_notification_hub_namespace.go
WodansSon Jun 27, 2019
d5e7a70
Set both Sku and Sku_Name in datasource
WodansSon Jun 27, 2019
c6ebd0f
Update azurerm/resource_arm_notification_hub_namespace_test.go
WodansSon Jun 27, 2019
86bebe7
Update Key Vault Datasource and test case
WodansSon Jun 27, 2019
832d630
Merge branch 'master' into e_flatten_sku
WodansSon Jun 27, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 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": {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we keep this in untill 2.0?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not in the data source, since it is read only... when it writes to the state file it will persist the new attribute instead of the deprecated attribute to be consistent.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

However if someone is relying on this property currently it will break them in weird and wonderful ways with no deprecation notice. We should keep it around until 2.0.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As about we should be setting both sku and sku_name here

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,10 +183,15 @@ func dataSourceArmKeyVaultRead(d *schema.ResourceData, meta interface{}) error {
d.Set("enabled_for_template_deployment", props.EnabledForTemplateDeployment)
d.Set("vault_uri", props.VaultURI)

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

if err := d.Set("sku_name", string(props.Sku.Name)); err != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll need to check that props.Sku is not nil here:

if sku := props.Sku; sku != nil {
  d.Set("sku_name", sku.Name))
}

return fmt.Errorf("Error setting `sku_name` for KeyVault %q: %+v", *resp.Name, err)
}

flattenedPolicies := azure.FlattenKeyVaultAccessPolicies(props.AccessPolicies)
if err := d.Set("access_policy", flattenedPolicies); err != nil {
return fmt.Errorf("Error setting `access_policy` for KeyVault %q: %+v", *resp.Name, err)
Expand All @@ -196,6 +207,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
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