Skip to content

Commit

Permalink
Refactor azurerm_data_lake_* to use id parsers #13491
Browse files Browse the repository at this point in the history
  • Loading branch information
stephybun authored Sep 24, 2021
1 parent e9d8931 commit 0edd625
Show file tree
Hide file tree
Showing 24 changed files with 1,062 additions and 197 deletions.
71 changes: 32 additions & 39 deletions internal/services/datalake/data_lake_analytics_account_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/datalake/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/datalake/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tags"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
Expand All @@ -25,8 +26,10 @@ func resourceDataLakeAnalyticsAccount() *pluginsdk.Resource {
Update: resourceArmDateLakeAnalyticsAccountUpdate,
Delete: resourceArmDateLakeAnalyticsAccountDelete,

// TODO: replace this with an importer which validates the ID during import
Importer: pluginsdk.DefaultImporter(),
Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error {
_, err := parse.AnalyticsAccountID(id)
return err
}),

Timeouts: &pluginsdk.ResourceTimeout{
Create: pluginsdk.DefaultTimeout(30 * time.Minute),
Expand Down Expand Up @@ -79,16 +82,16 @@ func resourceDataLakeAnalyticsAccount() *pluginsdk.Resource {

func resourceArmDateLakeAnalyticsAccountCreate(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Datalake.AnalyticsAccountsClient
subscriptionId := meta.(*clients.Client).Datalake.AnalyticsAccountsClient.SubscriptionID
ctx, cancel := timeouts.ForCreate(meta.(*clients.Client).StopContext, d)
defer cancel()

name := d.Get("name").(string)
resourceGroup := d.Get("resource_group_name").(string)
id := parse.NewAnalyticsAccountID(subscriptionId, d.Get("resource_group_name").(string), d.Get("name").(string))

existing, err := client.Get(ctx, resourceGroup, name)
existing, err := client.Get(ctx, id.ResourceGroup, id.AccountName)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("checking for presence of existing Data Lake Analytics Account %q (Resource Group %q): %s", name, resourceGroup, err)
return fmt.Errorf("checking for presence of existing Data Lake Analytics Account %s: %+v", id, err)
}
}

Expand All @@ -101,7 +104,7 @@ func resourceArmDateLakeAnalyticsAccountCreate(d *pluginsdk.ResourceData, meta i
tier := d.Get("tier").(string)
t := d.Get("tags").(map[string]interface{})

log.Printf("[INFO] preparing arguments for Azure ARM Date Lake Store creation %q (Resource Group %q)", name, resourceGroup)
log.Printf("[INFO] preparing arguments for Azure ARM Date Lake Store creation %s", id)

dateLakeAnalyticsAccount := account.CreateDataLakeAnalyticsAccountParameters{
Location: &location,
Expand All @@ -117,24 +120,16 @@ func resourceArmDateLakeAnalyticsAccountCreate(d *pluginsdk.ResourceData, meta i
},
}

future, err := client.Create(ctx, resourceGroup, name, dateLakeAnalyticsAccount)
future, err := client.Create(ctx, id.ResourceGroup, id.AccountName, dateLakeAnalyticsAccount)
if err != nil {
return fmt.Errorf("issuing create request for Data Lake Analytics Account %q (Resource Group %q): %+v", name, resourceGroup, err)
return fmt.Errorf("issuing create request for Data Lake Analytics Account %s: %+v", id, err)
}

if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("creating Data Lake Analytics Account %q (Resource Group %q): %+v", name, resourceGroup, err)
return fmt.Errorf("creating Data Lake Analytics Account %s: %+v", id, err)
}

read, err := client.Get(ctx, resourceGroup, name)
if err != nil {
return fmt.Errorf("retrieving Data Lake Analytics Account %q (Resource Group %q): %+v", name, resourceGroup, err)
}
if read.ID == nil {
return fmt.Errorf("Cannot read Data Lake Analytics Account %s (resource group %s) ID", name, resourceGroup)
}

d.SetId(*read.ID)
d.SetId(id.ID())

return resourceArmDateLakeAnalyticsAccountRead(d, meta)
}
Expand All @@ -144,8 +139,11 @@ func resourceArmDateLakeAnalyticsAccountUpdate(d *pluginsdk.ResourceData, meta i
ctx, cancel := timeouts.ForUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()

name := d.Get("name").(string)
resourceGroup := d.Get("resource_group_name").(string)
id, err := parse.AnalyticsAccountID(d.Id())
if err != nil {
return err
}

storeAccountName := d.Get("default_store_account_name").(string)
newTier := d.Get("tier").(string)
newTags := d.Get("tags").(map[string]interface{})
Expand All @@ -162,13 +160,13 @@ func resourceArmDateLakeAnalyticsAccountUpdate(d *pluginsdk.ResourceData, meta i
},
}

future, err := client.Update(ctx, resourceGroup, name, props)
future, err := client.Update(ctx, id.ResourceGroup, id.AccountName, props)
if err != nil {
return fmt.Errorf("issuing update request for Data Lake Analytics Account %q (Resource Group %q): %+v", name, resourceGroup, err)
return fmt.Errorf("issuing update request for Data Lake Analytics Account %s: %+v", id, err)
}

if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("waiting for the update of Data Lake Analytics Account %q (Resource Group %q) to commplete: %+v", name, resourceGroup, err)
return fmt.Errorf("waiting for the update of Data Lake Analytics Account %s to complete: %+v", id, err)
}

return resourceArmDateLakeAnalyticsAccountRead(d, meta)
Expand All @@ -179,26 +177,23 @@ func resourceArmDateLakeAnalyticsAccountRead(d *pluginsdk.ResourceData, meta int
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := azure.ParseAzureResourceID(d.Id())
id, err := parse.AnalyticsAccountID(d.Id())
if err != nil {
return err
}

resourceGroup := id.ResourceGroup
name := id.Path["accounts"]

resp, err := client.Get(ctx, resourceGroup, name)
resp, err := client.Get(ctx, id.ResourceGroup, id.AccountName)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[WARN] DataLakeAnalyticsAccountAccount '%s' was not found (resource group '%s')", name, resourceGroup)
log.Printf("[WARN] DataLakeAnalyticsAccountAccount '%s'", id)
d.SetId("")
return nil
}
return fmt.Errorf("making Read request on Azure Data Lake Analytics Account %q (Resource Group %q): %+v", name, resourceGroup, err)
return fmt.Errorf("making Read request on Azure Data Lake Analytics Account %s: %+v", id, err)
}

d.Set("name", name)
d.Set("resource_group_name", resourceGroup)
d.Set("name", id.AccountName)
d.Set("resource_group_name", id.ResourceGroup)
if location := resp.Location; location != nil {
d.Set("location", azure.NormalizeLocation(*location))
}
Expand All @@ -216,20 +211,18 @@ func resourceArmDateLakeAnalyticsAccountDelete(d *pluginsdk.ResourceData, meta i
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := azure.ParseAzureResourceID(d.Id())
id, err := parse.AnalyticsAccountID(d.Id())
if err != nil {
return err
}

resourceGroup := id.ResourceGroup
name := id.Path["accounts"]
future, err := client.Delete(ctx, resourceGroup, name)
future, err := client.Delete(ctx, id.ResourceGroup, id.AccountName)
if err != nil {
return fmt.Errorf("deleting Data Lake Analytics Account %q (Resource Group %q): %+v", name, resourceGroup, err)
return fmt.Errorf("deleting Data Lake Analytics Account %s: %+v", id, err)
}

if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("waiting for the deletion of Data Lake Analytics Account %q (Resource Group %q): %+v", name, resourceGroup, err)
return fmt.Errorf("waiting for the deletion of Data Lake Analytics Account %s: %+v", id, err)
}

return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
"strconv"
"testing"

"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/datalake/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)
Expand Down Expand Up @@ -88,16 +88,14 @@ func TestAccDataLakeAnalyticsAccount_withTags(t *testing.T) {
}

func (t DataLakeAnalyticsAccountResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := azure.ParseAzureResourceID(state.ID)
id, err := parse.AnalyticsAccountID(state.ID)
if err != nil {
return nil, err
}

name := id.Path["accounts"]

resp, err := clients.Datalake.AnalyticsAccountsClient.Get(ctx, id.ResourceGroup, name)
resp, err := clients.Datalake.AnalyticsAccountsClient.Get(ctx, id.ResourceGroup, id.AccountName)
if err != nil {
return nil, fmt.Errorf("retrieving Date Lake Analytics Account %q (resource group: %q): %+v", name, id.ResourceGroup, err)
return nil, fmt.Errorf("retrieving Date Lake Analytics Account %s: %+v", id, err)
}

return utils.Bool(resp.DataLakeAnalyticsAccountProperties != nil), nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package datalake

import (
"fmt"

"log"
"time"

Expand All @@ -11,6 +12,7 @@ import (
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
commonValidate "github.com/hashicorp/terraform-provider-azurerm/helpers/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/datalake/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/datalake/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
Expand All @@ -24,8 +26,10 @@ func resourceDataLakeAnalyticsFirewallRule() *pluginsdk.Resource {
Update: resourceArmDateLakeAnalyticsFirewallRuleCreateUpdate,
Delete: resourceArmDateLakeAnalyticsFirewallRuleDelete,

// TODO: replace this with an importer which validates the ID during import
Importer: pluginsdk.DefaultImporter(),
Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error {
_, err := parse.AnalyticsFirewallRuleID(id)
return err
}),

Timeouts: &pluginsdk.ResourceTimeout{
Create: pluginsdk.DefaultTimeout(30 * time.Minute),
Expand Down Expand Up @@ -68,18 +72,17 @@ func resourceDataLakeAnalyticsFirewallRule() *pluginsdk.Resource {

func resourceArmDateLakeAnalyticsFirewallRuleCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Datalake.AnalyticsFirewallRulesClient
subscriptionId := meta.(*clients.Client).Datalake.AnalyticsFirewallRulesClient.SubscriptionID
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()

name := d.Get("name").(string)
accountName := d.Get("account_name").(string)
resourceGroup := d.Get("resource_group_name").(string)
id := parse.NewAnalyticsFirewallRuleID(subscriptionId, d.Get("resource_group_name").(string), d.Get("account_name").(string), d.Get("name").(string))

if d.IsNewResource() {
existing, err := client.Get(ctx, resourceGroup, accountName, name)
existing, err := client.Get(ctx, id.ResourceGroup, id.AccountName, id.FirewallRuleName)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("checking for presence of existing Data Lake Analytics Firewall Rule %q (Account %q / Resource Group %q): %s", name, accountName, resourceGroup, err)
return fmt.Errorf("checking for presence of existing Data Lake Analytics Firewall Rule %s %+v", id, err)
}
}

Expand All @@ -91,7 +94,7 @@ func resourceArmDateLakeAnalyticsFirewallRuleCreateUpdate(d *pluginsdk.ResourceD
startIPAddress := d.Get("start_ip_address").(string)
endIPAddress := d.Get("end_ip_address").(string)

log.Printf("[INFO] preparing arguments for Date Lake Analytics Firewall Rule creation %q (Account %q / Resource Group %q)", name, accountName, resourceGroup)
log.Printf("[INFO] preparing arguments for Date Lake Analytics Firewall Rule creation %s", id)

dateLakeStore := account.CreateOrUpdateFirewallRuleParameters{
CreateOrUpdateFirewallRuleProperties: &account.CreateOrUpdateFirewallRuleProperties{
Expand All @@ -100,19 +103,11 @@ func resourceArmDateLakeAnalyticsFirewallRuleCreateUpdate(d *pluginsdk.ResourceD
},
}

if _, err := client.CreateOrUpdate(ctx, resourceGroup, accountName, name, dateLakeStore); err != nil {
return fmt.Errorf("issuing create request for Data Lake Analytics %q (Resource Group %q): %+v", name, resourceGroup, err)
}

read, err := client.Get(ctx, resourceGroup, accountName, name)
if err != nil {
return fmt.Errorf("retrieving Data Lake Analytics Firewall Rule %q (Account %q / Resource Group %q): %+v", name, accountName, resourceGroup, err)
}
if read.ID == nil {
return fmt.Errorf("Cannot read Data Lake Analytics Firewall Rule %q (Account %q / Resource Group %q) ID", name, accountName, resourceGroup)
if _, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.AccountName, id.FirewallRuleName, dateLakeStore); err != nil {
return fmt.Errorf("issuing create request for Data Lake Analytics %q (Resource Group %q): %+v", id.AccountName, id.ResourceGroup, err)
}

d.SetId(*read.ID)
d.SetId(id.ID())

return resourceArmDateLakeAnalyticsFirewallRuleRead(d, meta)
}
Expand All @@ -122,27 +117,24 @@ func resourceArmDateLakeAnalyticsFirewallRuleRead(d *pluginsdk.ResourceData, met
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := azure.ParseAzureResourceID(d.Id())
id, err := parse.AnalyticsFirewallRuleID(d.Id())
if err != nil {
return err
}
resourceGroup := id.ResourceGroup
accountName := id.Path["accounts"]
name := id.Path["firewallRules"]

resp, err := client.Get(ctx, resourceGroup, accountName, name)
resp, err := client.Get(ctx, id.ResourceGroup, id.AccountName, id.FirewallRuleName)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[WARN] Data Lake Analytics Firewall Rule %q was not found (Account %q / Resource Group %q)", name, accountName, resourceGroup)
log.Printf("[WARN] Data Lake Analytics Firewall Rule %s", id)
d.SetId("")
return nil
}
return fmt.Errorf("making Read request on Azure Data Lake Analytics Firewall Rule %q (Account %q / Resource Group %q): %+v", name, accountName, resourceGroup, err)
return fmt.Errorf("making Read request on Azure Data Lake Analytics Firewall Rule %s: %+v", id, err)
}

d.Set("name", name)
d.Set("account_name", accountName)
d.Set("resource_group_name", resourceGroup)
d.Set("name", id.FirewallRuleName)
d.Set("account_name", id.AccountName)
d.Set("resource_group_name", id.ResourceGroup)

if props := resp.FirewallRuleProperties; props != nil {
d.Set("start_ip_address", props.StartIPAddress)
Expand All @@ -157,21 +149,17 @@ func resourceArmDateLakeAnalyticsFirewallRuleDelete(d *pluginsdk.ResourceData, m
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := azure.ParseAzureResourceID(d.Id())
id, err := parse.AnalyticsFirewallRuleID(d.Id())
if err != nil {
return err
}

resourceGroup := id.ResourceGroup
accountName := id.Path["accounts"]
name := id.Path["firewallRules"]

resp, err := client.Delete(ctx, resourceGroup, accountName, name)
resp, err := client.Delete(ctx, id.ResourceGroup, id.AccountName, id.FirewallRuleName)
if err != nil {
if response.WasNotFound(resp.Response) {
return nil
}
return fmt.Errorf("issuing delete request for Data Lake Analytics Firewall Rule %q (Account %q / Resource Group %q): %+v", name, accountName, resourceGroup, err)
return fmt.Errorf("issuing delete request for Data Lake Analytics Firewall Rule %s: %+v", id, err)
}

return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
"strconv"
"testing"

"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance"
"github.com/hashicorp/terraform-provider-azurerm/internal/acceptance/check"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/datalake/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)
Expand Down Expand Up @@ -101,17 +101,14 @@ func TestAccDataLakeAnalyticsFirewallRule_azureServices(t *testing.T) {
}

func (t DataLakeAnalyticsFirewallRuleResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := azure.ParseAzureResourceID(state.ID)
id, err := parse.AnalyticsFirewallRuleID(state.ID)
if err != nil {
return nil, err
}

accountName := id.Path["accounts"]
name := id.Path["firewallRules"]

resp, err := clients.Datalake.AnalyticsFirewallRulesClient.Get(ctx, id.ResourceGroup, accountName, name)
resp, err := clients.Datalake.AnalyticsFirewallRulesClient.Get(ctx, id.ResourceGroup, id.AccountName, id.FirewallRuleName)
if err != nil {
return nil, fmt.Errorf("retrieving Date Lake Analytics Firewall Rule %q (Account %q / Resource Group: %q): %v", name, accountName, id.ResourceGroup, err)
return nil, fmt.Errorf("retrieving Date Lake Analytics Firewall Rule %s: %v", id, err)
}

return utils.Bool(resp.FirewallRuleProperties != nil), nil
Expand Down
Loading

0 comments on commit 0edd625

Please sign in to comment.