From 0edd6258698c594cb7143e6574d10c49ca125c05 Mon Sep 17 00:00:00 2001 From: stephybun Date: Fri, 24 Sep 2021 21:10:25 +0200 Subject: [PATCH] Refactor `azurerm_data_lake_*` to use id parsers #13491 --- .../data_lake_analytics_account_resource.go | 71 +++++----- ...ta_lake_analytics_account_resource_test.go | 10 +- ...a_lake_analytics_firewall_rule_resource.go | 60 ++++---- ...e_analytics_firewall_rule_resource_test.go | 11 +- .../datalake/data_lake_store_data_source.go | 17 +-- .../data_lake_store_firewall_rule_resource.go | 60 ++++---- ..._lake_store_firewall_rule_resource_test.go | 11 +- .../datalake/data_lake_store_resource.go | 72 +++++----- .../datalake/data_lake_store_resource_test.go | 10 +- ...ake_store_virtual_network_rule_resource.go | 24 ++-- ...tore_virtual_network_rule_resource_test.go | 2 +- .../datalake/parse/analytics_account.go | 69 ++++++++++ .../datalake/parse/analytics_account_test.go | 112 +++++++++++++++ .../datalake/parse/analytics_firewall_rule.go | 75 ++++++++++ .../parse/analytics_firewall_rule_test.go | 128 ++++++++++++++++++ .../services/datalake/parse/firewall_rule.go | 75 ++++++++++ .../datalake/parse/firewall_rule_test.go | 128 ++++++++++++++++++ internal/services/datalake/resourceids.go | 3 + .../datalake/validate/analytics_account_id.go | 23 ++++ .../validate/analytics_account_id_test.go | 76 +++++++++++ .../validate/analytics_firewall_rule_id.go | 23 ++++ .../analytics_firewall_rule_id_test.go | 88 ++++++++++++ .../datalake/validate/firewall_rule_id.go | 23 ++++ .../validate/firewall_rule_id_test.go | 88 ++++++++++++ 24 files changed, 1062 insertions(+), 197 deletions(-) create mode 100644 internal/services/datalake/parse/analytics_account.go create mode 100644 internal/services/datalake/parse/analytics_account_test.go create mode 100644 internal/services/datalake/parse/analytics_firewall_rule.go create mode 100644 internal/services/datalake/parse/analytics_firewall_rule_test.go create mode 100644 internal/services/datalake/parse/firewall_rule.go create mode 100644 internal/services/datalake/parse/firewall_rule_test.go create mode 100644 internal/services/datalake/validate/analytics_account_id.go create mode 100644 internal/services/datalake/validate/analytics_account_id_test.go create mode 100644 internal/services/datalake/validate/analytics_firewall_rule_id.go create mode 100644 internal/services/datalake/validate/analytics_firewall_rule_id_test.go create mode 100644 internal/services/datalake/validate/firewall_rule_id.go create mode 100644 internal/services/datalake/validate/firewall_rule_id_test.go diff --git a/internal/services/datalake/data_lake_analytics_account_resource.go b/internal/services/datalake/data_lake_analytics_account_resource.go index d57aa703fa6d..d1beb06de5fa 100644 --- a/internal/services/datalake/data_lake_analytics_account_resource.go +++ b/internal/services/datalake/data_lake_analytics_account_resource.go @@ -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" @@ -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), @@ -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) } } @@ -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, @@ -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) } @@ -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{}) @@ -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) @@ -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)) } @@ -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 diff --git a/internal/services/datalake/data_lake_analytics_account_resource_test.go b/internal/services/datalake/data_lake_analytics_account_resource_test.go index 91d5504ecaee..ad5e9669ff6a 100644 --- a/internal/services/datalake/data_lake_analytics_account_resource_test.go +++ b/internal/services/datalake/data_lake_analytics_account_resource_test.go @@ -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" ) @@ -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 diff --git a/internal/services/datalake/data_lake_analytics_firewall_rule_resource.go b/internal/services/datalake/data_lake_analytics_firewall_rule_resource.go index 002afbe498be..453d1b486dce 100644 --- a/internal/services/datalake/data_lake_analytics_firewall_rule_resource.go +++ b/internal/services/datalake/data_lake_analytics_firewall_rule_resource.go @@ -2,6 +2,7 @@ package datalake import ( "fmt" + "log" "time" @@ -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" @@ -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), @@ -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) } } @@ -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{ @@ -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) } @@ -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) @@ -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 diff --git a/internal/services/datalake/data_lake_analytics_firewall_rule_resource_test.go b/internal/services/datalake/data_lake_analytics_firewall_rule_resource_test.go index 0461b37639da..ac89696ceaca 100644 --- a/internal/services/datalake/data_lake_analytics_firewall_rule_resource_test.go +++ b/internal/services/datalake/data_lake_analytics_firewall_rule_resource_test.go @@ -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" ) @@ -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 diff --git a/internal/services/datalake/data_lake_store_data_source.go b/internal/services/datalake/data_lake_store_data_source.go index 4ca4b031096d..e91d9b2fabdf 100644 --- a/internal/services/datalake/data_lake_store_data_source.go +++ b/internal/services/datalake/data_lake_store_data_source.go @@ -6,6 +6,7 @@ import ( "github.com/hashicorp/terraform-provider-azurerm/helpers/azure" "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/tags" "github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" "github.com/hashicorp/terraform-provider-azurerm/internal/timeouts" @@ -62,24 +63,24 @@ func dataSourceDataLakeStoreAccount() *pluginsdk.Resource { func dataSourceArmDateLakeStoreAccountRead(d *pluginsdk.ResourceData, meta interface{}) error { client := meta.(*clients.Client).Datalake.StoreAccountsClient + subscriptionId := meta.(*clients.Client).Datalake.StoreAccountsClient.SubscriptionID ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d) defer cancel() - name := d.Get("name").(string) - resourceGroup := d.Get("resource_group_name").(string) + id := parse.NewAccountID(subscriptionId, d.Get("resource_group_name").(string), d.Get("name").(string)) - resp, err := client.Get(ctx, resourceGroup, name) + resp, err := client.Get(ctx, id.ResourceGroup, id.Name) if err != nil { if utils.ResponseWasNotFound(resp.Response) { - return fmt.Errorf("Data Lake Store Account %q was not found in Resource Group %q", name, resourceGroup) + return fmt.Errorf("Data Lake Store Account %s was not found", id) } - return fmt.Errorf("retrieving Data Lake Store Account %q (Resource Group %q): %+v", name, resourceGroup, err) + return fmt.Errorf("retrieving Data Lake Store Account %s: %+v", id, err) } - d.SetId(*resp.ID) + d.SetId(id.ID()) - d.Set("name", name) - d.Set("resource_group_name", resourceGroup) + d.Set("name", id.Name) + d.Set("resource_group_name", id.ResourceGroup) if location := resp.Location; location != nil { d.Set("location", azure.NormalizeLocation(*location)) } diff --git a/internal/services/datalake/data_lake_store_firewall_rule_resource.go b/internal/services/datalake/data_lake_store_firewall_rule_resource.go index 96c894a858da..210cf2ff1f97 100644 --- a/internal/services/datalake/data_lake_store_firewall_rule_resource.go +++ b/internal/services/datalake/data_lake_store_firewall_rule_resource.go @@ -11,6 +11,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" @@ -23,8 +24,11 @@ func resourceDataLakeStoreFirewallRule() *pluginsdk.Resource { Read: resourceArmDateLakeStoreAccountFirewallRuleRead, Update: resourceArmDateLakeStoreAccountFirewallRuleCreateUpdate, Delete: resourceArmDateLakeStoreAccountFirewallRuleDelete, - // TODO: replace this with an importer which validates the ID during import - Importer: pluginsdk.DefaultImporter(), + + Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error { + _, err := parse.FirewallRuleID(id) + return err + }), Timeouts: &pluginsdk.ResourceTimeout{ Create: pluginsdk.DefaultTimeout(30 * time.Minute), @@ -67,18 +71,17 @@ func resourceDataLakeStoreFirewallRule() *pluginsdk.Resource { func resourceArmDateLakeStoreAccountFirewallRuleCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) error { client := meta.(*clients.Client).Datalake.StoreFirewallRulesClient + subscriptionId := meta.(*clients.Client).Datalake.StoreFirewallRulesClient.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.NewFirewallRuleID(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.Name) if err != nil { if !utils.ResponseWasNotFound(existing.Response) { - return fmt.Errorf("checking for presence of existing Date Lake Store Firewall Rule %q (Account %q / Resource Group %q): %s", name, accountName, resourceGroup, err) + return fmt.Errorf("checking for presence of existing Date Lake Store Firewall Rule %s: %+v", id, err) } } @@ -90,7 +93,7 @@ func resourceArmDateLakeStoreAccountFirewallRuleCreateUpdate(d *pluginsdk.Resour startIPAddress := d.Get("start_ip_address").(string) endIPAddress := d.Get("end_ip_address").(string) - log.Printf("[INFO] preparing arguments for Date Lake Store Firewall Rule creation %q (Resource Group %q)", name, resourceGroup) + log.Printf("[INFO] preparing arguments for Date Lake Store Firewall Rule creation %s", id) dateLakeStore := account.CreateOrUpdateFirewallRuleParameters{ CreateOrUpdateFirewallRuleProperties: &account.CreateOrUpdateFirewallRuleProperties{ @@ -99,19 +102,11 @@ func resourceArmDateLakeStoreAccountFirewallRuleCreateUpdate(d *pluginsdk.Resour }, } - if _, err := client.CreateOrUpdate(ctx, resourceGroup, accountName, name, dateLakeStore); err != nil { - return fmt.Errorf("issuing create request for Data Lake Store %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 Store Firewall Rule %q (Account %q / Resource Group %q): %+v", name, accountName, resourceGroup, err) - } - if read.ID == nil { - return fmt.Errorf("Cannot read Data Lake Store %q (Account %q / Resource Group %q) ID", name, accountName, resourceGroup) + if _, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.AccountName, id.Name, dateLakeStore); err != nil { + return fmt.Errorf("issuing create request for Data Lake Store %q (Resource Group %q): %+v", id.AccountName, id.ResourceGroup, err) } - d.SetId(*read.ID) + d.SetId(id.ID()) return resourceArmDateLakeStoreAccountFirewallRuleRead(d, meta) } @@ -121,27 +116,24 @@ func resourceArmDateLakeStoreAccountFirewallRuleRead(d *pluginsdk.ResourceData, ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d) defer cancel() - id, err := azure.ParseAzureResourceID(d.Id()) + id, err := parse.FirewallRuleID(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.Name) if err != nil { if utils.ResponseWasNotFound(resp.Response) { - log.Printf("[WARN] Data Lake Store Firewall Rule %q was not found (Account %q / Resource Group %q)", name, accountName, resourceGroup) + log.Printf("[WARN] Data Lake Store Firewall Rule %s was not found", id) d.SetId("") return nil } - return fmt.Errorf("making Read request on Azure Data Lake Store Firewall Rule %q (Account %q / Resource Group %q): %+v", name, accountName, resourceGroup, err) + return fmt.Errorf("making Read request on Azure Data Lake Store 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.Name) + 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) @@ -156,21 +148,17 @@ func resourceArmDateLakeStoreAccountFirewallRuleDelete(d *pluginsdk.ResourceData ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d) defer cancel() - id, err := azure.ParseAzureResourceID(d.Id()) + id, err := parse.FirewallRuleID(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.Name) if err != nil { if response.WasNotFound(resp.Response) { return nil } - return fmt.Errorf("issuing delete request for Data Lake Store Firewall Rule %q (Account %q / Resource Group %q): %+v", name, accountName, resourceGroup, err) + return fmt.Errorf("issuing delete request for Data Lake Store Firewall Rule %s: %+v", id, err) } return nil diff --git a/internal/services/datalake/data_lake_store_firewall_rule_resource_test.go b/internal/services/datalake/data_lake_store_firewall_rule_resource_test.go index ed5b56c62aeb..aa9397a86a72 100644 --- a/internal/services/datalake/data_lake_store_firewall_rule_resource_test.go +++ b/internal/services/datalake/data_lake_store_firewall_rule_resource_test.go @@ -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" ) @@ -101,17 +101,14 @@ func TestAccDataLakeStoreFirewallRule_azureServices(t *testing.T) { } func (t DataLakeStoreFirewallRuleResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) { - id, err := azure.ParseAzureResourceID(state.ID) + id, err := parse.FirewallRuleID(state.ID) if err != nil { return nil, err } - resourceGroup := id.ResourceGroup - accountName := id.Path["accounts"] - name := id.Path["firewallRules"] - resp, err := clients.Datalake.StoreFirewallRulesClient.Get(ctx, resourceGroup, accountName, name) + resp, err := clients.Datalake.StoreFirewallRulesClient.Get(ctx, id.ResourceGroup, id.AccountName, id.Name) if err != nil { - return nil, fmt.Errorf("retrieving Date Lake Store Firewall Rule %q (Account %q / Resource Group: %q): %v", name, accountName, resourceGroup, err) + return nil, fmt.Errorf("retrieving Date Lake Store Firewall Rule %s: %v", id, err) } return utils.Bool(resp.FirewallRuleProperties != nil), nil diff --git a/internal/services/datalake/data_lake_store_resource.go b/internal/services/datalake/data_lake_store_resource.go index ff0143ce13b6..c037cd258bd7 100644 --- a/internal/services/datalake/data_lake_store_resource.go +++ b/internal/services/datalake/data_lake_store_resource.go @@ -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" @@ -25,8 +26,10 @@ func resourceDataLakeStore() *pluginsdk.Resource { Update: resourceArmDateLakeStoreUpdate, Delete: resourceArmDateLakeStoreDelete, - // TODO: replace this with an importer which validates the ID during import - Importer: pluginsdk.DefaultImporter(), + Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error { + _, err := parse.AccountID(id) + return err + }), Timeouts: &pluginsdk.ResourceTimeout{ Create: pluginsdk.DefaultTimeout(30 * time.Minute), @@ -145,17 +148,17 @@ func resourceDataLakeStore() *pluginsdk.Resource { func resourceArmDateLakeStoreCreate(d *pluginsdk.ResourceData, meta interface{}) error { client := meta.(*clients.Client).Datalake.StoreAccountsClient + subscriptionId := meta.(*clients.Client).Datalake.StoreAccountsClient.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.NewAccountID(subscriptionId, d.Get("resource_group_name").(string), d.Get("name").(string)) if d.IsNewResource() { - existing, err := client.Get(ctx, resourceGroup, name) + existing, err := client.Get(ctx, id.ResourceGroup, id.Name) if err != nil { if !utils.ResponseWasNotFound(existing.Response) { - return fmt.Errorf("checking for presence of existing Data Lake Store %q (Resource Group %q): %s", name, resourceGroup, err) + return fmt.Errorf("checking for presence of existing Data Lake Store %s: %+v", id, err) } } @@ -173,7 +176,7 @@ func resourceArmDateLakeStoreCreate(d *pluginsdk.ResourceData, meta interface{}) firewallAllowAzureIPs := account.FirewallAllowAzureIpsState(d.Get("firewall_allow_azure_ips").(string)) t := d.Get("tags").(map[string]interface{}) - log.Printf("[INFO] preparing arguments for Data Lake Store creation %q (Resource Group %q)", name, resourceGroup) + log.Printf("[INFO] preparing arguments for Data Lake Store creation %s", id) dateLakeStore := account.CreateDataLakeStoreAccountParameters{ Location: &location, @@ -191,24 +194,16 @@ func resourceArmDateLakeStoreCreate(d *pluginsdk.ResourceData, meta interface{}) }, } - future, err := client.Create(ctx, resourceGroup, name, dateLakeStore) + future, err := client.Create(ctx, id.ResourceGroup, id.Name, dateLakeStore) if err != nil { - return fmt.Errorf("issuing create request for Data Lake Store %q (Resource Group %q): %+v", name, resourceGroup, err) + return fmt.Errorf("issuing create request for Data Lake Store %s: %+v", id, err) } if err = future.WaitForCompletionRef(ctx, client.Client); err != nil { - return fmt.Errorf("creating Data Lake Store %q (Resource Group %q): %+v", name, resourceGroup, err) + return fmt.Errorf("creating Data Lake Store %s: %+v", id, err) } - read, err := client.Get(ctx, resourceGroup, name) - if err != nil { - return fmt.Errorf("retrieving Data Lake Store %q (Resource Group %q): %+v", name, resourceGroup, err) - } - if read.ID == nil { - return fmt.Errorf("Cannot read Data Lake Store %s (resource group %s) ID", name, resourceGroup) - } - - d.SetId(*read.ID) + d.SetId(id.ID()) return resourceArmDateLakeStoreRead(d, meta) } @@ -218,8 +213,11 @@ func resourceArmDateLakeStoreUpdate(d *pluginsdk.ResourceData, meta interface{}) 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.AccountID(d.Id()) + if err != nil { + return err + } + tier := d.Get("tier").(string) firewallState := account.FirewallState(d.Get("firewall_state").(string)) firewallAllowAzureIPs := account.FirewallAllowAzureIpsState(d.Get("firewall_allow_azure_ips").(string)) @@ -234,13 +232,13 @@ func resourceArmDateLakeStoreUpdate(d *pluginsdk.ResourceData, meta interface{}) Tags: tags.Expand(t), } - future, err := client.Update(ctx, resourceGroup, name, props) + future, err := client.Update(ctx, id.ResourceGroup, id.Name, props) if err != nil { - return fmt.Errorf("issuing update request for Data Lake Store %q (Resource Group %q): %+v", name, resourceGroup, err) + return fmt.Errorf("issuing update request for Data Lake Store %s: %+v", id, err) } if err = future.WaitForCompletionRef(ctx, client.Client); err != nil { - return fmt.Errorf("waiting for the update of Data Lake Store %q (Resource Group %q) to commplete: %+v", name, resourceGroup, err) + return fmt.Errorf("waiting for the update of Data Lake Store %s to complete: %+v", id, err) } return resourceArmDateLakeStoreRead(d, meta) @@ -251,32 +249,30 @@ func resourceArmDateLakeStoreRead(d *pluginsdk.ResourceData, meta interface{}) e ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d) defer cancel() - id, err := azure.ParseAzureResourceID(d.Id()) + id, err := parse.AccountID(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.Name) if err != nil { if utils.ResponseWasNotFound(resp.Response) { - log.Printf("[WARN] Data Lake Store Account %q was not found (Resource Group %q)", name, resourceGroup) + log.Printf("[WARN] Data Lake Store Account %s was not found", id) d.SetId("") return nil } - return fmt.Errorf("making Read request on Azure Data Lake Store %q (Resource Group %q): %+v", name, resourceGroup, err) + return fmt.Errorf("making Read request on Azure Data Lake Store %s: %+v", id, err) } - d.Set("name", name) - d.Set("resource_group_name", resourceGroup) + d.Set("name", id.Name) + d.Set("resource_group_name", id.ResourceGroup) if location := resp.Location; location != nil { d.Set("location", azure.NormalizeLocation(*location)) } if err := d.Set("identity", flattenDataLakeStoreIdentity(resp.Identity)); err != nil { - return fmt.Errorf("flattening identity on Data Lake Store %q (Resource Group %q): %+v", name, resourceGroup, err) + return fmt.Errorf("flattening identity on Data Lake Store %s: %+v", id, err) } if properties := resp.DataLakeStoreAccountProperties; properties != nil { @@ -301,20 +297,18 @@ func resourceArmDateLakeStoreDelete(d *pluginsdk.ResourceData, meta interface{}) ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d) defer cancel() - id, err := azure.ParseAzureResourceID(d.Id()) + id, err := parse.AccountID(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.Name) if err != nil { - return fmt.Errorf("deleting Data Lake Store %q (Resource Group %q): %+v", name, resourceGroup, err) + return fmt.Errorf("deleting Data Lake Store %s: %+v", id, err) } if err = future.WaitForCompletionRef(ctx, client.Client); err != nil { - return fmt.Errorf("waiting for deletion of Data Lake Store %q (Resource Group %q): %+v", name, resourceGroup, err) + return fmt.Errorf("waiting for deletion of Data Lake Store %s: %+v", id, err) } return nil diff --git a/internal/services/datalake/data_lake_store_resource_test.go b/internal/services/datalake/data_lake_store_resource_test.go index 23fd4405c218..9995b1a85a41 100644 --- a/internal/services/datalake/data_lake_store_resource_test.go +++ b/internal/services/datalake/data_lake_store_resource_test.go @@ -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/datashare/parse" "github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" "github.com/hashicorp/terraform-provider-azurerm/utils" ) @@ -172,16 +172,14 @@ func TestAccDataLakeStore_withTags(t *testing.T) { } func (t DataLakeStoreResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) { - id, err := azure.ParseAzureResourceID(state.ID) + id, err := parse.AccountID(state.ID) if err != nil { return nil, err } - name := id.Path["accounts"] - - resp, err := clients.Datalake.StoreAccountsClient.Get(ctx, id.ResourceGroup, name) + resp, err := clients.Datalake.StoreAccountsClient.Get(ctx, id.ResourceGroup, id.Name) if err != nil { - return nil, fmt.Errorf("retrieving Date Lake Store %q (resource group: %q): %+v", name, id.ResourceGroup, err) + return nil, fmt.Errorf("retrieving Date Lake Store %s: %+v", id, err) } return utils.Bool(resp.DataLakeStoreAccountProperties != nil), nil diff --git a/internal/services/datalake/data_lake_store_virtual_network_rule_resource.go b/internal/services/datalake/data_lake_store_virtual_network_rule_resource.go index ce85cd81a890..94bf01ad4422 100644 --- a/internal/services/datalake/data_lake_store_virtual_network_rule_resource.go +++ b/internal/services/datalake/data_lake_store_virtual_network_rule_resource.go @@ -25,8 +25,10 @@ func resourceDataLakeStoreVirtualNetworkRule() *pluginsdk.Resource { Update: resourceDataLakeStoreVirtualNetworkRuleCreateUpdate, Delete: resourceDataLakeStoreVirtualNetworkRuleDelete, - // TODO: replace this with an importer which validates the ID during import - Importer: pluginsdk.DefaultImporter(), + Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error { + _, err := parse.VirtualNetworkRuleID(id) + return err + }), Timeouts: &pluginsdk.ResourceTimeout{ Create: pluginsdk.DefaultTimeout(30 * time.Minute), @@ -63,21 +65,19 @@ func resourceDataLakeStoreVirtualNetworkRule() *pluginsdk.Resource { func resourceDataLakeStoreVirtualNetworkRuleCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) error { client := meta.(*clients.Client).Datalake.VirtualNetworkRulesClient + subscriptionId := meta.(*clients.Client).Datalake.VirtualNetworkRulesClient.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.NewVirtualNetworkRuleID(subscriptionId, d.Get("resource_group_name").(string), d.Get("account_name").(string), d.Get("name").(string)) + virtualNetworkSubnetId := d.Get("subnet_id").(string) - subscriptionId := meta.(*clients.Client).Account.SubscriptionId - id := parse.NewVirtualNetworkRuleID(subscriptionId, resourceGroup, accountName, name) if d.IsNewResource() { existing, err := client.Get(ctx, id.ResourceGroup, id.AccountName, id.Name) if err != nil { if !utils.ResponseWasNotFound(existing.Response) { - return fmt.Errorf("checking for presence of existing Data Lake Store Virtual Network Rule %q (Account: %q, Resource Group: %q): %+v", id.Name, id.AccountName, id.ResourceGroup, err) + return fmt.Errorf("checking for presence of existing Data Lake Store Virtual Network Rule %s: %+v", id, err) } } @@ -93,7 +93,7 @@ func resourceDataLakeStoreVirtualNetworkRuleCreateUpdate(d *pluginsdk.ResourceDa } if _, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.AccountName, id.Name, parameters); err != nil { - return fmt.Errorf("creating Data Lake Store Virtual Network Rule %q (Account: %q, Resource Group: %q): %+v", id.Name, id.AccountName, id.ResourceGroup, err) + return fmt.Errorf("creating Data Lake Store Virtual Network Rule %s: %+v", id, err) } d.SetId(id.ID()) @@ -114,12 +114,12 @@ func resourceDataLakeStoreVirtualNetworkRuleRead(d *pluginsdk.ResourceData, meta resp, err := client.Get(ctx, id.ResourceGroup, id.AccountName, id.Name) if err != nil { if utils.ResponseWasNotFound(resp.Response) { - log.Printf("[INFO] Data Lake Store Virtual Network Rule %q (Account: %q / Resource Group %q) was not found - removing from state", id.Name, id.AccountName, id.ResourceGroup) + log.Printf("[INFO] Data Lake Store Virtual Network Rule %s was not found - removing from state", id) d.SetId("") return nil } - return fmt.Errorf("retrieving Virtual Network Rule %q (Account: %q / Resource Group: %q): %+v", id.Name, id.AccountName, id.ResourceGroup, err) + return fmt.Errorf("retrieving Virtual Network Rule %s: %+v", id, err) } d.Set("name", id.Name) @@ -148,7 +148,7 @@ func resourceDataLakeStoreVirtualNetworkRuleDelete(d *pluginsdk.ResourceData, me if response.WasNotFound(resp.Response) { return nil } - return fmt.Errorf("deleting Data Lake Store Virtual Network Rule %q (Account: %q / Resource Group: %q): %+v", id.Name, id.AccountName, id.ResourceGroup, err) + return fmt.Errorf("deleting Data Lake Store Virtual Network Rule %s: %+v", id, err) } return nil diff --git a/internal/services/datalake/data_lake_store_virtual_network_rule_resource_test.go b/internal/services/datalake/data_lake_store_virtual_network_rule_resource_test.go index 93b73a51f2bd..fe58150d0b38 100644 --- a/internal/services/datalake/data_lake_store_virtual_network_rule_resource_test.go +++ b/internal/services/datalake/data_lake_store_virtual_network_rule_resource_test.go @@ -309,7 +309,7 @@ func (r DataLakeStoreVirtualNetworkRuleResource) Exists(ctx context.Context, cli if utils.ResponseWasNotFound(resp.Response) { return utils.Bool(false), nil } - return nil, fmt.Errorf("retrieving Data Lake Store Virtual Network Rule %q (Account %q / Resource Group %q): %+v", id.Name, id.AccountName, id.ResourceGroup, err) + return nil, fmt.Errorf("retrieving Data Lake Store Virtual Network Rule %s: %+v", id, err) } return utils.Bool(true), nil } diff --git a/internal/services/datalake/parse/analytics_account.go b/internal/services/datalake/parse/analytics_account.go new file mode 100644 index 000000000000..813496ae9754 --- /dev/null +++ b/internal/services/datalake/parse/analytics_account.go @@ -0,0 +1,69 @@ +package parse + +// NOTE: this file is generated via 'go:generate' - manual changes will be overwritten + +import ( + "fmt" + "strings" + + "github.com/hashicorp/terraform-provider-azurerm/helpers/azure" +) + +type AnalyticsAccountId struct { + SubscriptionId string + ResourceGroup string + AccountName string +} + +func NewAnalyticsAccountID(subscriptionId, resourceGroup, accountName string) AnalyticsAccountId { + return AnalyticsAccountId{ + SubscriptionId: subscriptionId, + ResourceGroup: resourceGroup, + AccountName: accountName, + } +} + +func (id AnalyticsAccountId) String() string { + segments := []string{ + fmt.Sprintf("Account Name %q", id.AccountName), + fmt.Sprintf("Resource Group %q", id.ResourceGroup), + } + segmentsStr := strings.Join(segments, " / ") + return fmt.Sprintf("%s: (%s)", "Analytics Account", segmentsStr) +} + +func (id AnalyticsAccountId) ID() string { + fmtString := "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.DataLakeAnalytics/accounts/%s" + return fmt.Sprintf(fmtString, id.SubscriptionId, id.ResourceGroup, id.AccountName) +} + +// AnalyticsAccountID parses a AnalyticsAccount ID into an AnalyticsAccountId struct +func AnalyticsAccountID(input string) (*AnalyticsAccountId, error) { + id, err := azure.ParseAzureResourceID(input) + if err != nil { + return nil, err + } + + resourceId := AnalyticsAccountId{ + SubscriptionId: id.SubscriptionID, + ResourceGroup: id.ResourceGroup, + } + + if resourceId.SubscriptionId == "" { + return nil, fmt.Errorf("ID was missing the 'subscriptions' element") + } + + if resourceId.ResourceGroup == "" { + return nil, fmt.Errorf("ID was missing the 'resourceGroups' element") + } + + if resourceId.AccountName, err = id.PopSegment("accounts"); err != nil { + return nil, err + } + + if err := id.ValidateNoEmptySegments(input); err != nil { + return nil, err + } + + return &resourceId, nil +} diff --git a/internal/services/datalake/parse/analytics_account_test.go b/internal/services/datalake/parse/analytics_account_test.go new file mode 100644 index 000000000000..08cbe3a8c73d --- /dev/null +++ b/internal/services/datalake/parse/analytics_account_test.go @@ -0,0 +1,112 @@ +package parse + +// NOTE: this file is generated via 'go:generate' - manual changes will be overwritten + +import ( + "testing" + + "github.com/hashicorp/terraform-provider-azurerm/internal/resourceid" +) + +var _ resourceid.Formatter = AnalyticsAccountId{} + +func TestAnalyticsAccountIDFormatter(t *testing.T) { + actual := NewAnalyticsAccountID("12345678-1234-9876-4563-123456789012", "resGroup1", "account1").ID() + expected := "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.DataLakeAnalytics/accounts/account1" + if actual != expected { + t.Fatalf("Expected %q but got %q", expected, actual) + } +} + +func TestAnalyticsAccountID(t *testing.T) { + testData := []struct { + Input string + Error bool + Expected *AnalyticsAccountId + }{ + + { + // empty + Input: "", + Error: true, + }, + + { + // missing SubscriptionId + Input: "/", + Error: true, + }, + + { + // missing value for SubscriptionId + Input: "/subscriptions/", + Error: true, + }, + + { + // missing ResourceGroup + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/", + Error: true, + }, + + { + // missing value for ResourceGroup + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/", + Error: true, + }, + + { + // missing AccountName + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.DataLakeAnalytics/", + Error: true, + }, + + { + // missing value for AccountName + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.DataLakeAnalytics/accounts/", + Error: true, + }, + + { + // valid + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.DataLakeAnalytics/accounts/account1", + Expected: &AnalyticsAccountId{ + SubscriptionId: "12345678-1234-9876-4563-123456789012", + ResourceGroup: "resGroup1", + AccountName: "account1", + }, + }, + + { + // upper-cased + Input: "/SUBSCRIPTIONS/12345678-1234-9876-4563-123456789012/RESOURCEGROUPS/RESGROUP1/PROVIDERS/MICROSOFT.DATALAKEANALYTICS/ACCOUNTS/ACCOUNT1", + Error: true, + }, + } + + for _, v := range testData { + t.Logf("[DEBUG] Testing %q", v.Input) + + actual, err := AnalyticsAccountID(v.Input) + if err != nil { + if v.Error { + continue + } + + t.Fatalf("Expect a value but got an error: %s", err) + } + if v.Error { + t.Fatal("Expect an error but didn't get one") + } + + if actual.SubscriptionId != v.Expected.SubscriptionId { + t.Fatalf("Expected %q but got %q for SubscriptionId", v.Expected.SubscriptionId, actual.SubscriptionId) + } + if actual.ResourceGroup != v.Expected.ResourceGroup { + t.Fatalf("Expected %q but got %q for ResourceGroup", v.Expected.ResourceGroup, actual.ResourceGroup) + } + if actual.AccountName != v.Expected.AccountName { + t.Fatalf("Expected %q but got %q for AccountName", v.Expected.AccountName, actual.AccountName) + } + } +} diff --git a/internal/services/datalake/parse/analytics_firewall_rule.go b/internal/services/datalake/parse/analytics_firewall_rule.go new file mode 100644 index 000000000000..b96d4d2b69dd --- /dev/null +++ b/internal/services/datalake/parse/analytics_firewall_rule.go @@ -0,0 +1,75 @@ +package parse + +// NOTE: this file is generated via 'go:generate' - manual changes will be overwritten + +import ( + "fmt" + "strings" + + "github.com/hashicorp/terraform-provider-azurerm/helpers/azure" +) + +type AnalyticsFirewallRuleId struct { + SubscriptionId string + ResourceGroup string + AccountName string + FirewallRuleName string +} + +func NewAnalyticsFirewallRuleID(subscriptionId, resourceGroup, accountName, firewallRuleName string) AnalyticsFirewallRuleId { + return AnalyticsFirewallRuleId{ + SubscriptionId: subscriptionId, + ResourceGroup: resourceGroup, + AccountName: accountName, + FirewallRuleName: firewallRuleName, + } +} + +func (id AnalyticsFirewallRuleId) String() string { + segments := []string{ + fmt.Sprintf("Firewall Rule Name %q", id.FirewallRuleName), + fmt.Sprintf("Account Name %q", id.AccountName), + fmt.Sprintf("Resource Group %q", id.ResourceGroup), + } + segmentsStr := strings.Join(segments, " / ") + return fmt.Sprintf("%s: (%s)", "Analytics Firewall Rule", segmentsStr) +} + +func (id AnalyticsFirewallRuleId) ID() string { + fmtString := "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.DataLakeAnalytics/accounts/%s/firewallRules/%s" + return fmt.Sprintf(fmtString, id.SubscriptionId, id.ResourceGroup, id.AccountName, id.FirewallRuleName) +} + +// AnalyticsFirewallRuleID parses a AnalyticsFirewallRule ID into an AnalyticsFirewallRuleId struct +func AnalyticsFirewallRuleID(input string) (*AnalyticsFirewallRuleId, error) { + id, err := azure.ParseAzureResourceID(input) + if err != nil { + return nil, err + } + + resourceId := AnalyticsFirewallRuleId{ + SubscriptionId: id.SubscriptionID, + ResourceGroup: id.ResourceGroup, + } + + if resourceId.SubscriptionId == "" { + return nil, fmt.Errorf("ID was missing the 'subscriptions' element") + } + + if resourceId.ResourceGroup == "" { + return nil, fmt.Errorf("ID was missing the 'resourceGroups' element") + } + + if resourceId.AccountName, err = id.PopSegment("accounts"); err != nil { + return nil, err + } + if resourceId.FirewallRuleName, err = id.PopSegment("firewallRules"); err != nil { + return nil, err + } + + if err := id.ValidateNoEmptySegments(input); err != nil { + return nil, err + } + + return &resourceId, nil +} diff --git a/internal/services/datalake/parse/analytics_firewall_rule_test.go b/internal/services/datalake/parse/analytics_firewall_rule_test.go new file mode 100644 index 000000000000..dad5661a2c15 --- /dev/null +++ b/internal/services/datalake/parse/analytics_firewall_rule_test.go @@ -0,0 +1,128 @@ +package parse + +// NOTE: this file is generated via 'go:generate' - manual changes will be overwritten + +import ( + "testing" + + "github.com/hashicorp/terraform-provider-azurerm/internal/resourceid" +) + +var _ resourceid.Formatter = AnalyticsFirewallRuleId{} + +func TestAnalyticsFirewallRuleIDFormatter(t *testing.T) { + actual := NewAnalyticsFirewallRuleID("12345678-1234-9876-4563-123456789012", "resGroup1", "account1", "rule1").ID() + expected := "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.DataLakeAnalytics/accounts/account1/firewallRules/rule1" + if actual != expected { + t.Fatalf("Expected %q but got %q", expected, actual) + } +} + +func TestAnalyticsFirewallRuleID(t *testing.T) { + testData := []struct { + Input string + Error bool + Expected *AnalyticsFirewallRuleId + }{ + + { + // empty + Input: "", + Error: true, + }, + + { + // missing SubscriptionId + Input: "/", + Error: true, + }, + + { + // missing value for SubscriptionId + Input: "/subscriptions/", + Error: true, + }, + + { + // missing ResourceGroup + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/", + Error: true, + }, + + { + // missing value for ResourceGroup + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/", + Error: true, + }, + + { + // missing AccountName + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.DataLakeAnalytics/", + Error: true, + }, + + { + // missing value for AccountName + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.DataLakeAnalytics/accounts/", + Error: true, + }, + + { + // missing FirewallRuleName + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.DataLakeAnalytics/accounts/account1/", + Error: true, + }, + + { + // missing value for FirewallRuleName + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.DataLakeAnalytics/accounts/account1/firewallRules/", + Error: true, + }, + + { + // valid + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.DataLakeAnalytics/accounts/account1/firewallRules/rule1", + Expected: &AnalyticsFirewallRuleId{ + SubscriptionId: "12345678-1234-9876-4563-123456789012", + ResourceGroup: "resGroup1", + AccountName: "account1", + FirewallRuleName: "rule1", + }, + }, + + { + // upper-cased + Input: "/SUBSCRIPTIONS/12345678-1234-9876-4563-123456789012/RESOURCEGROUPS/RESGROUP1/PROVIDERS/MICROSOFT.DATALAKEANALYTICS/ACCOUNTS/ACCOUNT1/FIREWALLRULES/RULE1", + Error: true, + }, + } + + for _, v := range testData { + t.Logf("[DEBUG] Testing %q", v.Input) + + actual, err := AnalyticsFirewallRuleID(v.Input) + if err != nil { + if v.Error { + continue + } + + t.Fatalf("Expect a value but got an error: %s", err) + } + if v.Error { + t.Fatal("Expect an error but didn't get one") + } + + if actual.SubscriptionId != v.Expected.SubscriptionId { + t.Fatalf("Expected %q but got %q for SubscriptionId", v.Expected.SubscriptionId, actual.SubscriptionId) + } + if actual.ResourceGroup != v.Expected.ResourceGroup { + t.Fatalf("Expected %q but got %q for ResourceGroup", v.Expected.ResourceGroup, actual.ResourceGroup) + } + if actual.AccountName != v.Expected.AccountName { + t.Fatalf("Expected %q but got %q for AccountName", v.Expected.AccountName, actual.AccountName) + } + if actual.FirewallRuleName != v.Expected.FirewallRuleName { + t.Fatalf("Expected %q but got %q for FirewallRuleName", v.Expected.FirewallRuleName, actual.FirewallRuleName) + } + } +} diff --git a/internal/services/datalake/parse/firewall_rule.go b/internal/services/datalake/parse/firewall_rule.go new file mode 100644 index 000000000000..1b22e5dec81c --- /dev/null +++ b/internal/services/datalake/parse/firewall_rule.go @@ -0,0 +1,75 @@ +package parse + +// NOTE: this file is generated via 'go:generate' - manual changes will be overwritten + +import ( + "fmt" + "strings" + + "github.com/hashicorp/terraform-provider-azurerm/helpers/azure" +) + +type FirewallRuleId struct { + SubscriptionId string + ResourceGroup string + AccountName string + Name string +} + +func NewFirewallRuleID(subscriptionId, resourceGroup, accountName, name string) FirewallRuleId { + return FirewallRuleId{ + SubscriptionId: subscriptionId, + ResourceGroup: resourceGroup, + AccountName: accountName, + Name: name, + } +} + +func (id FirewallRuleId) String() string { + segments := []string{ + fmt.Sprintf("Name %q", id.Name), + fmt.Sprintf("Account Name %q", id.AccountName), + fmt.Sprintf("Resource Group %q", id.ResourceGroup), + } + segmentsStr := strings.Join(segments, " / ") + return fmt.Sprintf("%s: (%s)", "Firewall Rule", segmentsStr) +} + +func (id FirewallRuleId) ID() string { + fmtString := "/subscriptions/%s/resourceGroups/%s/providers/Microsoft.DataLakeStore/accounts/%s/firewallRules/%s" + return fmt.Sprintf(fmtString, id.SubscriptionId, id.ResourceGroup, id.AccountName, id.Name) +} + +// FirewallRuleID parses a FirewallRule ID into an FirewallRuleId struct +func FirewallRuleID(input string) (*FirewallRuleId, error) { + id, err := azure.ParseAzureResourceID(input) + if err != nil { + return nil, err + } + + resourceId := FirewallRuleId{ + SubscriptionId: id.SubscriptionID, + ResourceGroup: id.ResourceGroup, + } + + if resourceId.SubscriptionId == "" { + return nil, fmt.Errorf("ID was missing the 'subscriptions' element") + } + + if resourceId.ResourceGroup == "" { + return nil, fmt.Errorf("ID was missing the 'resourceGroups' element") + } + + if resourceId.AccountName, err = id.PopSegment("accounts"); err != nil { + return nil, err + } + if resourceId.Name, err = id.PopSegment("firewallRules"); err != nil { + return nil, err + } + + if err := id.ValidateNoEmptySegments(input); err != nil { + return nil, err + } + + return &resourceId, nil +} diff --git a/internal/services/datalake/parse/firewall_rule_test.go b/internal/services/datalake/parse/firewall_rule_test.go new file mode 100644 index 000000000000..69461b76f49b --- /dev/null +++ b/internal/services/datalake/parse/firewall_rule_test.go @@ -0,0 +1,128 @@ +package parse + +// NOTE: this file is generated via 'go:generate' - manual changes will be overwritten + +import ( + "testing" + + "github.com/hashicorp/terraform-provider-azurerm/internal/resourceid" +) + +var _ resourceid.Formatter = FirewallRuleId{} + +func TestFirewallRuleIDFormatter(t *testing.T) { + actual := NewFirewallRuleID("12345678-1234-9876-4563-123456789012", "resGroup1", "account1", "rule1").ID() + expected := "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.DataLakeStore/accounts/account1/firewallRules/rule1" + if actual != expected { + t.Fatalf("Expected %q but got %q", expected, actual) + } +} + +func TestFirewallRuleID(t *testing.T) { + testData := []struct { + Input string + Error bool + Expected *FirewallRuleId + }{ + + { + // empty + Input: "", + Error: true, + }, + + { + // missing SubscriptionId + Input: "/", + Error: true, + }, + + { + // missing value for SubscriptionId + Input: "/subscriptions/", + Error: true, + }, + + { + // missing ResourceGroup + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/", + Error: true, + }, + + { + // missing value for ResourceGroup + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/", + Error: true, + }, + + { + // missing AccountName + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.DataLakeStore/", + Error: true, + }, + + { + // missing value for AccountName + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.DataLakeStore/accounts/", + Error: true, + }, + + { + // missing Name + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.DataLakeStore/accounts/account1/", + Error: true, + }, + + { + // missing value for Name + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.DataLakeStore/accounts/account1/firewallRules/", + Error: true, + }, + + { + // valid + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.DataLakeStore/accounts/account1/firewallRules/rule1", + Expected: &FirewallRuleId{ + SubscriptionId: "12345678-1234-9876-4563-123456789012", + ResourceGroup: "resGroup1", + AccountName: "account1", + Name: "rule1", + }, + }, + + { + // upper-cased + Input: "/SUBSCRIPTIONS/12345678-1234-9876-4563-123456789012/RESOURCEGROUPS/RESGROUP1/PROVIDERS/MICROSOFT.DATALAKESTORE/ACCOUNTS/ACCOUNT1/FIREWALLRULES/RULE1", + Error: true, + }, + } + + for _, v := range testData { + t.Logf("[DEBUG] Testing %q", v.Input) + + actual, err := FirewallRuleID(v.Input) + if err != nil { + if v.Error { + continue + } + + t.Fatalf("Expect a value but got an error: %s", err) + } + if v.Error { + t.Fatal("Expect an error but didn't get one") + } + + if actual.SubscriptionId != v.Expected.SubscriptionId { + t.Fatalf("Expected %q but got %q for SubscriptionId", v.Expected.SubscriptionId, actual.SubscriptionId) + } + if actual.ResourceGroup != v.Expected.ResourceGroup { + t.Fatalf("Expected %q but got %q for ResourceGroup", v.Expected.ResourceGroup, actual.ResourceGroup) + } + if actual.AccountName != v.Expected.AccountName { + t.Fatalf("Expected %q but got %q for AccountName", v.Expected.AccountName, actual.AccountName) + } + if actual.Name != v.Expected.Name { + t.Fatalf("Expected %q but got %q for Name", v.Expected.Name, actual.Name) + } + } +} diff --git a/internal/services/datalake/resourceids.go b/internal/services/datalake/resourceids.go index feaf6864adbe..71da00913307 100644 --- a/internal/services/datalake/resourceids.go +++ b/internal/services/datalake/resourceids.go @@ -2,3 +2,6 @@ package datalake //go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=Account -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.DataLakeStore/accounts/account1 //go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=VirtualNetworkRule -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.DataLakeStore/accounts/account1/virtualNetworkRules/virtualNetworkRule1 +//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=FirewallRule -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.DataLakeStore/accounts/account1/firewallRules/rule1 +//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=AnalyticsFirewallRule -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.DataLakeAnalytics/accounts/account1/firewallRules/rule1 +//go:generate go run ../../tools/generator-resource-id/main.go -path=./ -name=AnalyticsAccount -id=/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.DataLakeAnalytics/accounts/account1 diff --git a/internal/services/datalake/validate/analytics_account_id.go b/internal/services/datalake/validate/analytics_account_id.go new file mode 100644 index 000000000000..6ba675de8dc3 --- /dev/null +++ b/internal/services/datalake/validate/analytics_account_id.go @@ -0,0 +1,23 @@ +package validate + +// NOTE: this file is generated via 'go:generate' - manual changes will be overwritten + +import ( + "fmt" + + "github.com/hashicorp/terraform-provider-azurerm/internal/services/datalake/parse" +) + +func AnalyticsAccountID(input interface{}, key string) (warnings []string, errors []error) { + v, ok := input.(string) + if !ok { + errors = append(errors, fmt.Errorf("expected %q to be a string", key)) + return + } + + if _, err := parse.AnalyticsAccountID(v); err != nil { + errors = append(errors, err) + } + + return +} diff --git a/internal/services/datalake/validate/analytics_account_id_test.go b/internal/services/datalake/validate/analytics_account_id_test.go new file mode 100644 index 000000000000..b75646b48ebd --- /dev/null +++ b/internal/services/datalake/validate/analytics_account_id_test.go @@ -0,0 +1,76 @@ +package validate + +// NOTE: this file is generated via 'go:generate' - manual changes will be overwritten + +import "testing" + +func TestAnalyticsAccountID(t *testing.T) { + cases := []struct { + Input string + Valid bool + }{ + + { + // empty + Input: "", + Valid: false, + }, + + { + // missing SubscriptionId + Input: "/", + Valid: false, + }, + + { + // missing value for SubscriptionId + Input: "/subscriptions/", + Valid: false, + }, + + { + // missing ResourceGroup + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/", + Valid: false, + }, + + { + // missing value for ResourceGroup + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/", + Valid: false, + }, + + { + // missing AccountName + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.DataLakeAnalytics/", + Valid: false, + }, + + { + // missing value for AccountName + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.DataLakeAnalytics/accounts/", + Valid: false, + }, + + { + // valid + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.DataLakeAnalytics/accounts/account1", + Valid: true, + }, + + { + // upper-cased + Input: "/SUBSCRIPTIONS/12345678-1234-9876-4563-123456789012/RESOURCEGROUPS/RESGROUP1/PROVIDERS/MICROSOFT.DATALAKEANALYTICS/ACCOUNTS/ACCOUNT1", + Valid: false, + }, + } + for _, tc := range cases { + t.Logf("[DEBUG] Testing Value %s", tc.Input) + _, errors := AnalyticsAccountID(tc.Input, "test") + valid := len(errors) == 0 + + if tc.Valid != valid { + t.Fatalf("Expected %t but got %t", tc.Valid, valid) + } + } +} diff --git a/internal/services/datalake/validate/analytics_firewall_rule_id.go b/internal/services/datalake/validate/analytics_firewall_rule_id.go new file mode 100644 index 000000000000..474f1b304801 --- /dev/null +++ b/internal/services/datalake/validate/analytics_firewall_rule_id.go @@ -0,0 +1,23 @@ +package validate + +// NOTE: this file is generated via 'go:generate' - manual changes will be overwritten + +import ( + "fmt" + + "github.com/hashicorp/terraform-provider-azurerm/internal/services/datalake/parse" +) + +func AnalyticsFirewallRuleID(input interface{}, key string) (warnings []string, errors []error) { + v, ok := input.(string) + if !ok { + errors = append(errors, fmt.Errorf("expected %q to be a string", key)) + return + } + + if _, err := parse.AnalyticsFirewallRuleID(v); err != nil { + errors = append(errors, err) + } + + return +} diff --git a/internal/services/datalake/validate/analytics_firewall_rule_id_test.go b/internal/services/datalake/validate/analytics_firewall_rule_id_test.go new file mode 100644 index 000000000000..537f85aaee26 --- /dev/null +++ b/internal/services/datalake/validate/analytics_firewall_rule_id_test.go @@ -0,0 +1,88 @@ +package validate + +// NOTE: this file is generated via 'go:generate' - manual changes will be overwritten + +import "testing" + +func TestAnalyticsFirewallRuleID(t *testing.T) { + cases := []struct { + Input string + Valid bool + }{ + + { + // empty + Input: "", + Valid: false, + }, + + { + // missing SubscriptionId + Input: "/", + Valid: false, + }, + + { + // missing value for SubscriptionId + Input: "/subscriptions/", + Valid: false, + }, + + { + // missing ResourceGroup + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/", + Valid: false, + }, + + { + // missing value for ResourceGroup + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/", + Valid: false, + }, + + { + // missing AccountName + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.DataLakeAnalytics/", + Valid: false, + }, + + { + // missing value for AccountName + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.DataLakeAnalytics/accounts/", + Valid: false, + }, + + { + // missing FirewallRuleName + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.DataLakeAnalytics/accounts/account1/", + Valid: false, + }, + + { + // missing value for FirewallRuleName + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.DataLakeAnalytics/accounts/account1/firewallRules/", + Valid: false, + }, + + { + // valid + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.DataLakeAnalytics/accounts/account1/firewallRules/rule1", + Valid: true, + }, + + { + // upper-cased + Input: "/SUBSCRIPTIONS/12345678-1234-9876-4563-123456789012/RESOURCEGROUPS/RESGROUP1/PROVIDERS/MICROSOFT.DATALAKEANALYTICS/ACCOUNTS/ACCOUNT1/FIREWALLRULES/RULE1", + Valid: false, + }, + } + for _, tc := range cases { + t.Logf("[DEBUG] Testing Value %s", tc.Input) + _, errors := AnalyticsFirewallRuleID(tc.Input, "test") + valid := len(errors) == 0 + + if tc.Valid != valid { + t.Fatalf("Expected %t but got %t", tc.Valid, valid) + } + } +} diff --git a/internal/services/datalake/validate/firewall_rule_id.go b/internal/services/datalake/validate/firewall_rule_id.go new file mode 100644 index 000000000000..4a85daabb4d1 --- /dev/null +++ b/internal/services/datalake/validate/firewall_rule_id.go @@ -0,0 +1,23 @@ +package validate + +// NOTE: this file is generated via 'go:generate' - manual changes will be overwritten + +import ( + "fmt" + + "github.com/hashicorp/terraform-provider-azurerm/internal/services/datalake/parse" +) + +func FirewallRuleID(input interface{}, key string) (warnings []string, errors []error) { + v, ok := input.(string) + if !ok { + errors = append(errors, fmt.Errorf("expected %q to be a string", key)) + return + } + + if _, err := parse.FirewallRuleID(v); err != nil { + errors = append(errors, err) + } + + return +} diff --git a/internal/services/datalake/validate/firewall_rule_id_test.go b/internal/services/datalake/validate/firewall_rule_id_test.go new file mode 100644 index 000000000000..1a80a7ff5146 --- /dev/null +++ b/internal/services/datalake/validate/firewall_rule_id_test.go @@ -0,0 +1,88 @@ +package validate + +// NOTE: this file is generated via 'go:generate' - manual changes will be overwritten + +import "testing" + +func TestFirewallRuleID(t *testing.T) { + cases := []struct { + Input string + Valid bool + }{ + + { + // empty + Input: "", + Valid: false, + }, + + { + // missing SubscriptionId + Input: "/", + Valid: false, + }, + + { + // missing value for SubscriptionId + Input: "/subscriptions/", + Valid: false, + }, + + { + // missing ResourceGroup + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/", + Valid: false, + }, + + { + // missing value for ResourceGroup + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/", + Valid: false, + }, + + { + // missing AccountName + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.DataLakeStore/", + Valid: false, + }, + + { + // missing value for AccountName + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.DataLakeStore/accounts/", + Valid: false, + }, + + { + // missing Name + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.DataLakeStore/accounts/account1/", + Valid: false, + }, + + { + // missing value for Name + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.DataLakeStore/accounts/account1/firewallRules/", + Valid: false, + }, + + { + // valid + Input: "/subscriptions/12345678-1234-9876-4563-123456789012/resourceGroups/resGroup1/providers/Microsoft.DataLakeStore/accounts/account1/firewallRules/rule1", + Valid: true, + }, + + { + // upper-cased + Input: "/SUBSCRIPTIONS/12345678-1234-9876-4563-123456789012/RESOURCEGROUPS/RESGROUP1/PROVIDERS/MICROSOFT.DATALAKESTORE/ACCOUNTS/ACCOUNT1/FIREWALLRULES/RULE1", + Valid: false, + }, + } + for _, tc := range cases { + t.Logf("[DEBUG] Testing Value %s", tc.Input) + _, errors := FirewallRuleID(tc.Input, "test") + valid := len(errors) == 0 + + if tc.Valid != valid { + t.Fatalf("Expected %t but got %t", tc.Valid, valid) + } + } +}