From c5118361a529af78201e8ccad7ceb5e7c2f3690a Mon Sep 17 00:00:00 2001 From: Steph Date: Fri, 1 Oct 2021 14:29:28 +0200 Subject: [PATCH 1/4] refactor linked services --- ...ked_service_azure_blob_storage_resource.go | 38 ++++++++----------- ...ervice_azure_blob_storage_resource_test.go | 11 ++---- ...inked_service_azure_databricks_resource.go | 36 +++++++----------- ..._service_azure_databricks_resource_test.go | 11 ++---- ...ked_service_azure_file_storage_resource.go | 38 ++++++++----------- ...ervice_azure_file_storage_resource_test.go | 11 ++---- ..._linked_service_azure_function_resource.go | 6 ++- ...ed_service_azure_function_resource_test.go | 11 ++---- ...ry_linked_service_azure_search_resource.go | 6 ++- ...nked_service_azure_search_resource_test.go | 2 +- ...ked_service_azure_sql_database_resource.go | 36 +++++++----------- ...ervice_azure_sql_database_resource_test.go | 11 ++---- ...ed_service_azure_table_storage_resource.go | 38 ++++++++----------- ...rvice_azure_table_storage_resource_test.go | 11 ++---- ...actory_linked_service_cosmosdb_resource.go | 36 +++++++----------- ...y_linked_service_cosmosdb_resource_test.go | 11 ++---- ...service_data_lake_storage_gen2_resource.go | 36 +++++++----------- ...ce_data_lake_storage_gen2_resource_test.go | 11 ++---- ...ctory_linked_service_key_vault_resource.go | 37 ++++++++---------- ..._linked_service_key_vault_resource_test.go | 11 ++---- ...a_factory_linked_service_kusto_resource.go | 10 +++-- ...tory_linked_service_kusto_resource_test.go | 2 +- ...a_factory_linked_service_mysql_resource.go | 36 +++++++----------- ...tory_linked_service_mysql_resource_test.go | 11 ++---- ...a_factory_linked_service_odata_resource.go | 36 +++++++----------- ...tory_linked_service_odata_resource_test.go | 2 +- ...tory_linked_service_postgresql_resource.go | 34 +++++++---------- ...linked_service_postgresql_resource_test.go | 11 ++---- ...ta_factory_linked_service_sftp_resource.go | 34 +++++++---------- ...ctory_linked_service_sftp_resource_test.go | 11 ++---- ...ctory_linked_service_snowflake_resource.go | 36 +++++++----------- ..._linked_service_snowflake_resource_test.go | 11 ++---- ...tory_linked_service_sql_server_resource.go | 36 +++++++----------- ...linked_service_sql_server_resource_test.go | 11 ++---- ...factory_linked_service_synapse_resource.go | 36 +++++++----------- ...ry_linked_service_synapse_resource_test.go | 11 ++---- ...ata_factory_linked_service_web_resource.go | 36 +++++++----------- ...actory_linked_service_web_resource_test.go | 11 ++---- 38 files changed, 307 insertions(+), 476 deletions(-) diff --git a/internal/services/datafactory/data_factory_linked_service_azure_blob_storage_resource.go b/internal/services/datafactory/data_factory_linked_service_azure_blob_storage_resource.go index 0f59b5e5e647..3366752b2cb7 100644 --- a/internal/services/datafactory/data_factory_linked_service_azure_blob_storage_resource.go +++ b/internal/services/datafactory/data_factory_linked_service_azure_blob_storage_resource.go @@ -23,8 +23,10 @@ func resourceDataFactoryLinkedServiceAzureBlobStorage() *pluginsdk.Resource { Update: resourceDataFactoryLinkedServiceBlobStorageCreateUpdate, Delete: resourceDataFactoryLinkedServiceBlobStorageDelete, - // TODO: replace this with an importer which validates the ID during import - Importer: pluginsdk.DefaultImporter(), + Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error { + _, err := parse.LinkedServiceID(id) + return err + }), Timeouts: &pluginsdk.ResourceTimeout{ Create: pluginsdk.DefaultTimeout(30 * time.Minute), @@ -171,18 +173,17 @@ func resourceDataFactoryLinkedServiceAzureBlobStorage() *pluginsdk.Resource { func resourceDataFactoryLinkedServiceBlobStorageCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) error { client := meta.(*clients.Client).DataFactory.LinkedServiceClient + subscriptionId := meta.(*clients.Client).DataFactory.LinkedServiceClient.SubscriptionID ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d) defer cancel() - name := d.Get("name").(string) - dataFactoryName := d.Get("data_factory_name").(string) - resourceGroup := d.Get("resource_group_name").(string) + id := parse.NewLinkedServiceID(subscriptionId, d.Get("resource_group_name").(string), d.Get("data_factory_name").(string), d.Get("name").(string)) if d.IsNewResource() { - existing, err := client.Get(ctx, resourceGroup, dataFactoryName, name, "") + existing, err := client.Get(ctx, id.ResourceGroup, id.FactoryName, id.Name, "") if err != nil { if !utils.ResponseWasNotFound(existing.Response) { - return fmt.Errorf("checking for presence of existing Data Factory Linked Service BlobStorage Anonymous %q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err) + return fmt.Errorf("checking for presence of existing Data Factory Blob Storage Anonymous %s: %+v", id, err) } } @@ -254,20 +255,11 @@ func resourceDataFactoryLinkedServiceBlobStorageCreateUpdate(d *pluginsdk.Resour Properties: blobStorageLinkedService, } - if _, err := client.CreateOrUpdate(ctx, resourceGroup, dataFactoryName, name, linkedService, ""); err != nil { - return fmt.Errorf("creating/updating Data Factory Linked Service BlobStorage %q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err) + if _, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.FactoryName, id.Name, linkedService, ""); err != nil { + return fmt.Errorf("creating/updating Data Factory Blob Storage %s: %+v", id, err) } - resp, err := client.Get(ctx, resourceGroup, dataFactoryName, name, "") - if err != nil { - return fmt.Errorf("retrieving Data Factory Linked Service BlobStorage %q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err) - } - - if resp.ID == nil { - return fmt.Errorf("Cannot read Data Factory Linked Service BlobStorage %q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err) - } - - d.SetId(*resp.ID) + d.SetId(id.ID()) return resourceDataFactoryLinkedServiceBlobStorageRead(d, meta) } @@ -289,7 +281,7 @@ func resourceDataFactoryLinkedServiceBlobStorageRead(d *pluginsdk.ResourceData, return nil } - return fmt.Errorf("retrieving Data Factory Linked Service BlobStorage %q (Data Factory %q / Resource Group %q): %+v", id.Name, id.FactoryName, id.ResourceGroup, err) + return fmt.Errorf("retrieving Data Factory Blob Storage %s: %+v", *id, err) } d.Set("name", resp.Name) @@ -298,7 +290,7 @@ func resourceDataFactoryLinkedServiceBlobStorageRead(d *pluginsdk.ResourceData, blobStorage, ok := resp.Properties.AsAzureBlobStorageLinkedService() if !ok { - return fmt.Errorf("classifying Data Factory Linked Service BlobStorage %q (Data Factory %q / Resource Group %q): Expected: %q Received: %q", id.Name, id.FactoryName, id.ResourceGroup, datafactory.TypeBasicLinkedServiceTypeAzureBlobStorage, *resp.Type) + return fmt.Errorf("classifying Data Factory Blob Storage %s: Expected: %q Received: %q", *id, datafactory.TypeBasicLinkedServiceTypeAzureBlobStorage, *resp.Type) } if blobStorage != nil { @@ -330,7 +322,7 @@ func resourceDataFactoryLinkedServiceBlobStorageRead(d *pluginsdk.ResourceData, annotations := flattenDataFactoryAnnotations(blobStorage.Annotations) if err := d.Set("annotations", annotations); err != nil { - return fmt.Errorf("setting `annotations` for Data Factory Linked Service Azure Blob Storage %q (Data Factory %q) / Resource Group %q): %+v", id.Name, id.FactoryName, id.ResourceGroup, err) + return fmt.Errorf("setting `annotations` for Data Factory Azure Blob Storage %s: %+v", *id, err) } parameters := flattenDataFactoryParameters(blobStorage.Parameters) @@ -360,7 +352,7 @@ func resourceDataFactoryLinkedServiceBlobStorageDelete(d *pluginsdk.ResourceData response, err := client.Delete(ctx, id.ResourceGroup, id.FactoryName, id.Name) if err != nil { if !utils.ResponseWasNotFound(response) { - return fmt.Errorf("deleting Data Factory Linked Service BlobStorage %q (Data Factory %q / Resource Group %q): %+v", id.Name, id.FactoryName, id.ResourceGroup, err) + return fmt.Errorf("deleting Data Factory Blob Storage %s: %+v", *id, err) } } diff --git a/internal/services/datafactory/data_factory_linked_service_azure_blob_storage_resource_test.go b/internal/services/datafactory/data_factory_linked_service_azure_blob_storage_resource_test.go index 3af0e54114ae..29a067e8e6db 100644 --- a/internal/services/datafactory/data_factory_linked_service_azure_blob_storage_resource_test.go +++ b/internal/services/datafactory/data_factory_linked_service_azure_blob_storage_resource_test.go @@ -5,10 +5,10 @@ import ( "fmt" "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/datafactory/parse" "github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" "github.com/hashicorp/terraform-provider-azurerm/utils" ) @@ -110,17 +110,14 @@ func TestAccDataFactoryLinkedServiceAzureBlobStorage_update(t *testing.T) { } func (t LinkedServiceAzureBlobStorageResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) { - id, err := azure.ParseAzureResourceID(state.ID) + id, err := parse.LinkedServiceID(state.ID) if err != nil { return nil, err } - resourceGroup := id.ResourceGroup - dataFactoryName := id.Path["factories"] - name := id.Path["linkedservices"] - resp, err := clients.DataFactory.LinkedServiceClient.Get(ctx, resourceGroup, dataFactoryName, name, "") + resp, err := clients.DataFactory.LinkedServiceClient.Get(ctx, id.ResourceGroup, id.FactoryName, id.Name, "") if err != nil { - return nil, fmt.Errorf("reading Data Factory Linked Service Azure Storage Blob (%s): %+v", id, err) + return nil, fmt.Errorf("reading Data Factory Azure Storage Blob (%s): %+v", *id, err) } return utils.Bool(resp.ID != nil), nil diff --git a/internal/services/datafactory/data_factory_linked_service_azure_databricks_resource.go b/internal/services/datafactory/data_factory_linked_service_azure_databricks_resource.go index 274a4d560429..56c5ffaec8e7 100644 --- a/internal/services/datafactory/data_factory_linked_service_azure_databricks_resource.go +++ b/internal/services/datafactory/data_factory_linked_service_azure_databricks_resource.go @@ -26,8 +26,10 @@ func resourceDataFactoryLinkedServiceAzureDatabricks() *pluginsdk.Resource { Update: resourceDataFactoryLinkedServiceDatabricksCreateUpdate, Delete: resourceDataFactoryLinkedServiceDatabricksDelete, - // TODO: replace this with an importer which validates the ID during import - Importer: pluginsdk.DefaultImporter(), + Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error { + _, err := parse.LinkedServiceID(id) + return err + }), Timeouts: &pluginsdk.ResourceTimeout{ Create: pluginsdk.DefaultTimeout(30 * time.Minute), @@ -255,18 +257,17 @@ func resourceDataFactoryLinkedServiceAzureDatabricks() *pluginsdk.Resource { func resourceDataFactoryLinkedServiceDatabricksCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) error { client := meta.(*clients.Client).DataFactory.LinkedServiceClient + subscriptionId := meta.(*clients.Client).DataFactory.LinkedServiceClient.SubscriptionID ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d) defer cancel() - name := d.Get("name").(string) - dataFactoryName := d.Get("data_factory_name").(string) - resourceGroup := d.Get("resource_group_name").(string) + id := parse.NewLinkedServiceID(subscriptionId, d.Get("resource_group_name").(string), d.Get("data_factory_name").(string), d.Get("name").(string)) if d.IsNewResource() { - existing, err := client.Get(ctx, resourceGroup, dataFactoryName, name, "") + existing, err := client.Get(ctx, id.ResourceGroup, id.FactoryName, id.Name, "") if err != nil { if !utils.ResponseWasNotFound(existing.Response) { - return fmt.Errorf("checking for presence of existing Data Factory Linked Service Databricks %q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err) + return fmt.Errorf("checking for presence of existing Data Factory Databricks %s: %+v", id, err) } } @@ -410,20 +411,11 @@ func resourceDataFactoryLinkedServiceDatabricksCreateUpdate(d *pluginsdk.Resourc Properties: databricksLinkedService, } - if _, err := client.CreateOrUpdate(ctx, resourceGroup, dataFactoryName, name, linkedService, ""); err != nil { - return fmt.Errorf("creating/updating Data Factory Linked Service Azure Databricks %q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err) + if _, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.FactoryName, id.Name, linkedService, ""); err != nil { + return fmt.Errorf("creating/updating Data Factory Azure Databricks %s: %+v", id, err) } - resp, err := client.Get(ctx, resourceGroup, dataFactoryName, name, "") - if err != nil { - return fmt.Errorf("retrieving Data Factory Linked Service Databricks %q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err) - } - - if resp.ID == nil { - return fmt.Errorf("reading Data Factory Linked Service Databricks %q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err) - } - - d.SetId(*resp.ID) + d.SetId(id.ID()) return resourceDataFactoryLinkedServiceDatabricksRead(d, meta) } @@ -445,7 +437,7 @@ func resourceDataFactoryLinkedServiceDatabricksRead(d *pluginsdk.ResourceData, m return nil } - return fmt.Errorf("retrieving Data Factory Linked Service Databricks %q (Data Factory %q / Resource Group %q): %+v", id.Name, id.FactoryName, id.ResourceGroup, err) + return fmt.Errorf("retrieving Data Factory Databricks %s: %+v", *id, err) } d.Set("name", resp.Name) @@ -454,7 +446,7 @@ func resourceDataFactoryLinkedServiceDatabricksRead(d *pluginsdk.ResourceData, m databricks, ok := resp.Properties.AsAzureDatabricksLinkedService() if !ok { - return fmt.Errorf("classifying Data Factory Linked Service Databricks %q (Data Factory %q / Resource Group %q): Expected: %q Received: %q", id.Name, id.FactoryName, id.ResourceGroup, datafactory.TypeBasicLinkedServiceTypeAzureDatabricks, *resp.Type) + return fmt.Errorf("classifying Data Factory Databricks %s: Expected: %q Received: %q", *id, datafactory.TypeBasicLinkedServiceTypeAzureDatabricks, *resp.Type) } // Check the properties and verify if authentication is set to MSI @@ -587,7 +579,7 @@ func resourceDataFactoryLinkedServiceDatabricksDelete(d *pluginsdk.ResourceData, response, err := client.Delete(ctx, id.ResourceGroup, id.FactoryName, id.Name) if err != nil { if !utils.ResponseWasNotFound(response) { - return fmt.Errorf("deleting Data Factory Linked Service Databricks %q (Data Factory %q / Resource Group %q): %+v", id.Name, id.FactoryName, id.ResourceGroup, err) + return fmt.Errorf("deleting Data Factory Databricks %s: %+v", *id, err) } } return nil diff --git a/internal/services/datafactory/data_factory_linked_service_azure_databricks_resource_test.go b/internal/services/datafactory/data_factory_linked_service_azure_databricks_resource_test.go index 07436bead8a0..05c6acfaa091 100644 --- a/internal/services/datafactory/data_factory_linked_service_azure_databricks_resource_test.go +++ b/internal/services/datafactory/data_factory_linked_service_azure_databricks_resource_test.go @@ -5,10 +5,10 @@ import ( "fmt" "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/datafactory/parse" "github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" "github.com/hashicorp/terraform-provider-azurerm/utils" ) @@ -97,17 +97,14 @@ func TestAccDataFactoryLinkedServiceDatabricks_update(t *testing.T) { } func (t LinkedServiceDatabricksResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) { - id, err := azure.ParseAzureResourceID(state.ID) + id, err := parse.LinkedServiceID(state.ID) if err != nil { return nil, err } - resourceGroup := id.ResourceGroup - dataFactoryName := id.Path["factories"] - name := id.Path["linkedservices"] - resp, err := clients.DataFactory.LinkedServiceClient.Get(ctx, resourceGroup, dataFactoryName, name, "") + resp, err := clients.DataFactory.LinkedServiceClient.Get(ctx, id.ResourceGroup, id.FactoryName, id.Name, "") if err != nil { - return nil, fmt.Errorf("reading Data Factory LinkedServiceDatabricksResource (%s): %+v", id, err) + return nil, fmt.Errorf("reading Data Factory Databricks Resource (%s): %+v", *id, err) } return utils.Bool(resp.ID != nil), nil diff --git a/internal/services/datafactory/data_factory_linked_service_azure_file_storage_resource.go b/internal/services/datafactory/data_factory_linked_service_azure_file_storage_resource.go index f5151ea0c179..16e242db9f4d 100644 --- a/internal/services/datafactory/data_factory_linked_service_azure_file_storage_resource.go +++ b/internal/services/datafactory/data_factory_linked_service_azure_file_storage_resource.go @@ -23,8 +23,10 @@ func resourceDataFactoryLinkedServiceAzureFileStorage() *pluginsdk.Resource { Update: resourceDataFactoryLinkedServiceAzureFileStorageCreateUpdate, Delete: resourceDataFactoryLinkedServiceAzureFileStorageDelete, - // TODO: replace this with an importer which validates the ID during import - Importer: pluginsdk.DefaultImporter(), + Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error { + _, err := parse.LinkedServiceID(id) + return err + }), Timeouts: &pluginsdk.ResourceTimeout{ Create: pluginsdk.DefaultTimeout(30 * time.Minute), @@ -146,18 +148,17 @@ func resourceDataFactoryLinkedServiceAzureFileStorage() *pluginsdk.Resource { func resourceDataFactoryLinkedServiceAzureFileStorageCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) error { client := meta.(*clients.Client).DataFactory.LinkedServiceClient + subscriptionId := meta.(*clients.Client).DataFactory.LinkedServiceClient.SubscriptionID ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d) defer cancel() - name := d.Get("name").(string) - dataFactoryName := d.Get("data_factory_name").(string) - resourceGroup := d.Get("resource_group_name").(string) + id := parse.NewLinkedServiceID(subscriptionId, d.Get("resource_group_name").(string), d.Get("data_factory_name").(string), d.Get("name").(string)) if d.IsNewResource() { - existing, err := client.Get(ctx, resourceGroup, dataFactoryName, name, "") + existing, err := client.Get(ctx, id.ResourceGroup, id.FactoryName, id.Name, "") if err != nil { if !utils.ResponseWasNotFound(existing.Response) { - return fmt.Errorf("checking for presence of existing Data Factory Linked Service Azure File Storage Anonymous %q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err) + return fmt.Errorf("checking for presence of existing Data Factory Azure File Storage Anonymous %s: %+v", id, err) } } @@ -216,20 +217,11 @@ func resourceDataFactoryLinkedServiceAzureFileStorageCreateUpdate(d *pluginsdk.R Properties: fileStorageLinkedService, } - if _, err := client.CreateOrUpdate(ctx, resourceGroup, dataFactoryName, name, linkedService, ""); err != nil { - return fmt.Errorf("creating/updating Data Factory Linked Service Azure File Storage %q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err) + if _, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.FactoryName, id.Name, linkedService, ""); err != nil { + return fmt.Errorf("creating/updating Data Factory Azure File Storage %s: %+v", id, err) } - resp, err := client.Get(ctx, resourceGroup, dataFactoryName, name, "") - if err != nil { - return fmt.Errorf("retrieving Data Factory Linked Service Azure File Storage %q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err) - } - - if resp.ID == nil { - return fmt.Errorf("Cannot read Data Factory Linked Service Azure File Storage %q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err) - } - - d.SetId(*resp.ID) + d.SetId(id.ID()) return resourceDataFactoryLinkedServiceAzureFileStorageRead(d, meta) } @@ -251,7 +243,7 @@ func resourceDataFactoryLinkedServiceAzureFileStorageRead(d *pluginsdk.ResourceD return nil } - return fmt.Errorf("retrieving Data Factory Linked Service Azure File Storage %q (Data Factory %q / Resource Group %q): %+v", id.Name, id.FactoryName, id.ResourceGroup, err) + return fmt.Errorf("retrieving Data Factory Azure File Storage %s: %+v", *id, err) } d.Set("name", resp.Name) @@ -260,7 +252,7 @@ func resourceDataFactoryLinkedServiceAzureFileStorageRead(d *pluginsdk.ResourceD fileStorage, ok := resp.Properties.AsAzureFileStorageLinkedService() if !ok { - return fmt.Errorf("classifying Data Factory Linked Service Azure File Storage %q (Data Factory %q / Resource Group %q): Expected: %q Received: %q", id.Name, id.FactoryName, id.ResourceGroup, datafactory.TypeBasicLinkedServiceTypeAzureFileStorage, *resp.Type) + return fmt.Errorf("classifying Data Factory Azure File Storage %s: Expected: %q Received: %q", *id, datafactory.TypeBasicLinkedServiceTypeAzureFileStorage, *resp.Type) } d.Set("additional_properties", fileStorage.AdditionalProperties) @@ -276,7 +268,7 @@ func resourceDataFactoryLinkedServiceAzureFileStorageRead(d *pluginsdk.ResourceD annotations := flattenDataFactoryAnnotations(fileStorage.Annotations) if err := d.Set("annotations", annotations); err != nil { - return fmt.Errorf("setting `annotations` for Data Factory Linked Service Azure File Storage %q (Data Factory %q) / Resource Group %q): %+v", id.Name, id.FactoryName, id.ResourceGroup, err) + return fmt.Errorf("setting `annotations` for Data Factory Azure File Storage %s: %+v", *id, err) } parameters := flattenDataFactoryParameters(fileStorage.Parameters) @@ -312,7 +304,7 @@ func resourceDataFactoryLinkedServiceAzureFileStorageDelete(d *pluginsdk.Resourc response, err := client.Delete(ctx, id.ResourceGroup, id.FactoryName, id.Name) if err != nil { if !utils.ResponseWasNotFound(response) { - return fmt.Errorf("deleting Data Factory Linked Service Azure File Storage %q (Data Factory %q / Resource Group %q): %+v", id.Name, id.FactoryName, id.ResourceGroup, err) + return fmt.Errorf("deleting Data Factory Azure File Storage %s: %+v", *id, err) } } diff --git a/internal/services/datafactory/data_factory_linked_service_azure_file_storage_resource_test.go b/internal/services/datafactory/data_factory_linked_service_azure_file_storage_resource_test.go index ebc1547ae3c3..bbd089d91ed3 100644 --- a/internal/services/datafactory/data_factory_linked_service_azure_file_storage_resource_test.go +++ b/internal/services/datafactory/data_factory_linked_service_azure_file_storage_resource_test.go @@ -5,10 +5,10 @@ import ( "fmt" "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/datafactory/parse" "github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" "github.com/hashicorp/terraform-provider-azurerm/utils" ) @@ -96,17 +96,14 @@ func TestAccDataFactoryLinkedServiceAzureFileStorage_KeyVaultReference(t *testin } func (t LinkedServiceAzureFileStorageResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) { - id, err := azure.ParseAzureResourceID(state.ID) + id, err := parse.LinkedServiceID(state.ID) if err != nil { return nil, err } - resourceGroup := id.ResourceGroup - dataFactoryName := id.Path["factories"] - name := id.Path["linkedservices"] - resp, err := clients.DataFactory.LinkedServiceClient.Get(ctx, resourceGroup, dataFactoryName, name, "") + resp, err := clients.DataFactory.LinkedServiceClient.Get(ctx, id.ResourceGroup, id.FactoryName, id.Name, "") if err != nil { - return nil, fmt.Errorf("reading Data Factory Linked Service Azure File Blob (%s): %+v", id, err) + return nil, fmt.Errorf("reading Data Factory Azure File Blob (%s): %+v", *id, err) } return utils.Bool(resp.ID != nil), nil diff --git a/internal/services/datafactory/data_factory_linked_service_azure_function_resource.go b/internal/services/datafactory/data_factory_linked_service_azure_function_resource.go index 9f776242cfa5..8daab769ec2b 100644 --- a/internal/services/datafactory/data_factory_linked_service_azure_function_resource.go +++ b/internal/services/datafactory/data_factory_linked_service_azure_function_resource.go @@ -24,8 +24,10 @@ func resourceDataFactoryLinkedServiceAzureFunction() *pluginsdk.Resource { Update: resourceDataFactoryLinkedServiceAzureFunctionCreateUpdate, Delete: resourceDataFactoryLinkedServiceAzureFunctionDelete, - // TODO: replace this with an importer which validates the ID during import - Importer: pluginsdk.DefaultImporter(), + Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error { + _, err := parse.LinkedServiceID(id) + return err + }), Timeouts: &pluginsdk.ResourceTimeout{ Create: pluginsdk.DefaultTimeout(30 * time.Minute), diff --git a/internal/services/datafactory/data_factory_linked_service_azure_function_resource_test.go b/internal/services/datafactory/data_factory_linked_service_azure_function_resource_test.go index 4ef7b263a6f9..1d0519705760 100644 --- a/internal/services/datafactory/data_factory_linked_service_azure_function_resource_test.go +++ b/internal/services/datafactory/data_factory_linked_service_azure_function_resource_test.go @@ -5,10 +5,10 @@ import ( "fmt" "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/datafactory/parse" "github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" "github.com/hashicorp/terraform-provider-azurerm/utils" ) @@ -79,17 +79,14 @@ func TestAccDataFactoryLinkedServiceAzureFunction_KeyVaultReference(t *testing.T } func (t LinkedServiceAzureFunctionResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) { - id, err := azure.ParseAzureResourceID(state.ID) + id, err := parse.LinkedServiceID(state.ID) if err != nil { return nil, err } - resourceGroup := id.ResourceGroup - dataFactoryName := id.Path["factories"] - name := id.Path["linkedservices"] - resp, err := clients.DataFactory.LinkedServiceClient.Get(ctx, resourceGroup, dataFactoryName, name, "") + resp, err := clients.DataFactory.LinkedServiceClient.Get(ctx, id.ResourceGroup, id.FactoryName, id.Name, "") if err != nil { - return nil, fmt.Errorf("reading Data Factory Linked Service Azure Function (%s): %+v", id, err) + return nil, fmt.Errorf("reading Data Factory Azure Function (%s): %+v", *id, err) } return utils.Bool(resp.ID != nil), nil diff --git a/internal/services/datafactory/data_factory_linked_service_azure_search_resource.go b/internal/services/datafactory/data_factory_linked_service_azure_search_resource.go index c0207a92aca2..2b238bfc181a 100644 --- a/internal/services/datafactory/data_factory_linked_service_azure_search_resource.go +++ b/internal/services/datafactory/data_factory_linked_service_azure_search_resource.go @@ -22,8 +22,10 @@ func resourceDataFactoryLinkedServiceAzureSearch() *pluginsdk.Resource { Update: resourceDataFactoryLinkedServiceAzureSearchCreateUpdate, Delete: resourceDataFactoryLinkedServiceAzureSearchDelete, - // TODO: replace this with an importer which validates the ID during import - Importer: pluginsdk.DefaultImporter(), + Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error { + _, err := parse.LinkedServiceID(id) + return err + }), Timeouts: &pluginsdk.ResourceTimeout{ Create: pluginsdk.DefaultTimeout(30 * time.Minute), diff --git a/internal/services/datafactory/data_factory_linked_service_azure_search_resource_test.go b/internal/services/datafactory/data_factory_linked_service_azure_search_resource_test.go index f0b783b232a3..86c9df89d1da 100644 --- a/internal/services/datafactory/data_factory_linked_service_azure_search_resource_test.go +++ b/internal/services/datafactory/data_factory_linked_service_azure_search_resource_test.go @@ -99,7 +99,7 @@ func (t LinkedServiceSearchResource) Exists(ctx context.Context, clients *client resp, err := clients.DataFactory.LinkedServiceClient.Get(ctx, id.ResourceGroup, id.FactoryName, id.Name, "") if err != nil { - return nil, fmt.Errorf("reading %s: %+v", id, err) + return nil, fmt.Errorf("reading %s: %+v", *id, err) } return utils.Bool(resp.ID != nil), nil diff --git a/internal/services/datafactory/data_factory_linked_service_azure_sql_database_resource.go b/internal/services/datafactory/data_factory_linked_service_azure_sql_database_resource.go index 91ec58fd3589..f71821b9d142 100644 --- a/internal/services/datafactory/data_factory_linked_service_azure_sql_database_resource.go +++ b/internal/services/datafactory/data_factory_linked_service_azure_sql_database_resource.go @@ -23,8 +23,10 @@ func resourceDataFactoryLinkedServiceAzureSQLDatabase() *pluginsdk.Resource { Update: resourceDataFactoryLinkedServiceAzureSQLDatabaseCreateUpdate, Delete: resourceDataFactoryLinkedServiceAzureSQLDatabaseDelete, - // TODO: replace this with an importer which validates the ID during import - Importer: pluginsdk.DefaultImporter(), + Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error { + _, err := parse.LinkedServiceID(id) + return err + }), Timeouts: &pluginsdk.ResourceTimeout{ Create: pluginsdk.DefaultTimeout(30 * time.Minute), @@ -176,18 +178,17 @@ func resourceDataFactoryLinkedServiceAzureSQLDatabase() *pluginsdk.Resource { func resourceDataFactoryLinkedServiceAzureSQLDatabaseCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) error { client := meta.(*clients.Client).DataFactory.LinkedServiceClient + subscriptionId := meta.(*clients.Client).DataFactory.LinkedServiceClient.SubscriptionID ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d) defer cancel() - name := d.Get("name").(string) - dataFactoryName := d.Get("data_factory_name").(string) - resourceGroup := d.Get("resource_group_name").(string) + id := parse.NewLinkedServiceID(subscriptionId, d.Get("resource_group_name").(string), d.Get("data_factory_name").(string), d.Get("name").(string)) if d.IsNewResource() { - existing, err := client.Get(ctx, resourceGroup, dataFactoryName, name, "") + existing, err := client.Get(ctx, id.ResourceGroup, id.FactoryName, id.Name, "") if err != nil { if !utils.ResponseWasNotFound(existing.Response) { - return fmt.Errorf("checking for presence of existing Data Factory Linked Service AzureSQLDatabase %q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err) + return fmt.Errorf("checking for presence of existing Data Factory Azure SQL Database %s: %+v", id, err) } } @@ -258,20 +259,11 @@ func resourceDataFactoryLinkedServiceAzureSQLDatabaseCreateUpdate(d *pluginsdk.R Properties: azureSQLDatabaseLinkedService, } - if _, err := client.CreateOrUpdate(ctx, resourceGroup, dataFactoryName, name, linkedService, ""); err != nil { - return fmt.Errorf("creating/updating Data Factory Linked Service AzureSQLDatabase %q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err) + if _, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.FactoryName, id.Name, linkedService, ""); err != nil { + return fmt.Errorf("creating/updating Data Factory Azure SQL Database %s: %+v", id, err) } - resp, err := client.Get(ctx, resourceGroup, dataFactoryName, name, "") - if err != nil { - return fmt.Errorf("retrieving Data Factory Linked Service AzureSQLDatabase %q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err) - } - - if resp.ID == nil { - return fmt.Errorf("Cannot read Data Factory Linked Service AzureSQLDatabase %q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err) - } - - d.SetId(*resp.ID) + d.SetId(id.ID()) return resourceDataFactoryLinkedServiceAzureSQLDatabaseRead(d, meta) } @@ -293,7 +285,7 @@ func resourceDataFactoryLinkedServiceAzureSQLDatabaseRead(d *pluginsdk.ResourceD return nil } - return fmt.Errorf("retrieving Data Factory Linked Service AzureSQLDatabase %q (Data Factory %q / Resource Group %q): %+v", id.Name, id.FactoryName, id.ResourceGroup, err) + return fmt.Errorf("retrieving Data Factory Azure SQL Database %s: %+v", *id, err) } d.Set("name", resp.Name) @@ -302,7 +294,7 @@ func resourceDataFactoryLinkedServiceAzureSQLDatabaseRead(d *pluginsdk.ResourceD sql, ok := resp.Properties.AsAzureSQLDatabaseLinkedService() if !ok { - return fmt.Errorf("classifying Data Factory Linked Service AzureSQLDatabase %q (Data Factory %q / Resource Group %q): Expected: %q Received: %q", id.Name, id.FactoryName, id.ResourceGroup, datafactory.TypeBasicLinkedServiceTypeAzureSQLDatabase, *resp.Type) + return fmt.Errorf("classifying Data Factory Azure SQL Database %s: Expected: %q Received: %q", *id, datafactory.TypeBasicLinkedServiceTypeAzureSQLDatabase, *resp.Type) } if sql != nil { @@ -371,7 +363,7 @@ func resourceDataFactoryLinkedServiceAzureSQLDatabaseDelete(d *pluginsdk.Resourc response, err := client.Delete(ctx, id.ResourceGroup, id.FactoryName, id.Name) if err != nil { if !utils.ResponseWasNotFound(response) { - return fmt.Errorf("deleting Data Factory Linked Service AzureSQLDatabase %q (Data Factory %q / Resource Group %q): %+v", id.Name, id.FactoryName, id.ResourceGroup, err) + return fmt.Errorf("deleting Data Factory Azure SQL Database %s: %+v", *id, err) } } diff --git a/internal/services/datafactory/data_factory_linked_service_azure_sql_database_resource_test.go b/internal/services/datafactory/data_factory_linked_service_azure_sql_database_resource_test.go index d8286835fc76..f72aafed7c42 100644 --- a/internal/services/datafactory/data_factory_linked_service_azure_sql_database_resource_test.go +++ b/internal/services/datafactory/data_factory_linked_service_azure_sql_database_resource_test.go @@ -5,10 +5,10 @@ import ( "fmt" "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/datafactory/parse" "github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" "github.com/hashicorp/terraform-provider-azurerm/utils" ) @@ -114,17 +114,14 @@ func TestAccDataFactoryLinkedServiceAzureSQLDatabase_ConnectionStringKeyVaultRef } func (t LinkedServiceAzureSQLDatabaseResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) { - id, err := azure.ParseAzureResourceID(state.ID) + id, err := parse.LinkedServiceID(state.ID) if err != nil { return nil, err } - resourceGroup := id.ResourceGroup - dataFactoryName := id.Path["factories"] - name := id.Path["linkedservices"] - resp, err := clients.DataFactory.LinkedServiceClient.Get(ctx, resourceGroup, dataFactoryName, name, "") + resp, err := clients.DataFactory.LinkedServiceClient.Get(ctx, id.ResourceGroup, id.FactoryName, id.Name, "") if err != nil { - return nil, fmt.Errorf("reading Data Factory Linked Service Azure SQL Database (%s): %+v", id, err) + return nil, fmt.Errorf("reading Data Factory Azure SQL Database (%s): %+v", *id, err) } return utils.Bool(resp.ID != nil), nil diff --git a/internal/services/datafactory/data_factory_linked_service_azure_table_storage_resource.go b/internal/services/datafactory/data_factory_linked_service_azure_table_storage_resource.go index 11fa294a2ad8..771cec33ae1e 100644 --- a/internal/services/datafactory/data_factory_linked_service_azure_table_storage_resource.go +++ b/internal/services/datafactory/data_factory_linked_service_azure_table_storage_resource.go @@ -23,8 +23,10 @@ func resourceDataFactoryLinkedServiceAzureTableStorage() *pluginsdk.Resource { Update: resourceDataFactoryLinkedServiceTableStorageCreateUpdate, Delete: resourceDataFactoryLinkedServiceTableStorageDelete, - // TODO: replace this with an importer which validates the ID during import - Importer: pluginsdk.DefaultImporter(), + Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error { + _, err := parse.LinkedServiceID(id) + return err + }), Timeouts: &pluginsdk.ResourceTimeout{ Create: pluginsdk.DefaultTimeout(30 * time.Minute), @@ -100,18 +102,17 @@ func resourceDataFactoryLinkedServiceAzureTableStorage() *pluginsdk.Resource { func resourceDataFactoryLinkedServiceTableStorageCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) error { client := meta.(*clients.Client).DataFactory.LinkedServiceClient + subscriptionId := meta.(*clients.Client).DataFactory.LinkedServiceClient.SubscriptionID ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d) defer cancel() - name := d.Get("name").(string) - dataFactoryName := d.Get("data_factory_name").(string) - resourceGroup := d.Get("resource_group_name").(string) + id := parse.NewLinkedServiceID(subscriptionId, d.Get("resource_group_name").(string), d.Get("data_factory_name").(string), d.Get("name").(string)) if d.IsNewResource() { - existing, err := client.Get(ctx, resourceGroup, dataFactoryName, name, "") + existing, err := client.Get(ctx, id.ResourceGroup, id.FactoryName, id.Name, "") if err != nil { if !utils.ResponseWasNotFound(existing.Response) { - return fmt.Errorf("checking for presence of existing Data Factory Linked Service TableStorage Anonymous %q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err) + return fmt.Errorf("checking for presence of existing Data Factory Table Storage Anonymous %s: %+v", id, err) } } @@ -152,20 +153,11 @@ func resourceDataFactoryLinkedServiceTableStorageCreateUpdate(d *pluginsdk.Resou Properties: tableStorageLinkedService, } - if _, err := client.CreateOrUpdate(ctx, resourceGroup, dataFactoryName, name, linkedService, ""); err != nil { - return fmt.Errorf("creating/updating Data Factory Linked Service TableStorage %q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err) + if _, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.FactoryName, id.Name, linkedService, ""); err != nil { + return fmt.Errorf("creating/updating Data Factory Table Storage %s: %+v", id, err) } - resp, err := client.Get(ctx, resourceGroup, dataFactoryName, name, "") - if err != nil { - return fmt.Errorf("retrieving Data Factory Linked Service TableStorage %q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err) - } - - if resp.ID == nil { - return fmt.Errorf("Cannot read Data Factory Linked Service TableStorage %q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err) - } - - d.SetId(*resp.ID) + d.SetId(id.ID()) return resourceDataFactoryLinkedServiceTableStorageRead(d, meta) } @@ -187,7 +179,7 @@ func resourceDataFactoryLinkedServiceTableStorageRead(d *pluginsdk.ResourceData, return nil } - return fmt.Errorf("retrieving Data Factory Linked Service TableStorage %q (Data Factory %q / Resource Group %q): %+v", id.Name, id.FactoryName, id.ResourceGroup, err) + return fmt.Errorf("retrieving Data Factory Table Storage %s: %+v", *id, err) } d.Set("name", resp.Name) @@ -196,7 +188,7 @@ func resourceDataFactoryLinkedServiceTableStorageRead(d *pluginsdk.ResourceData, tableStorage, ok := resp.Properties.AsAzureTableStorageLinkedService() if !ok { - return fmt.Errorf("classifying Data Factory Linked Service TableStorage %q (Data Factory %q / Resource Group %q): Expected: %q Received: %q", id.Name, id.FactoryName, id.ResourceGroup, datafactory.TypeBasicLinkedServiceTypeAzureTableStorage, *resp.Type) + return fmt.Errorf("classifying Data Factory Table Storage %s: Expected: %q Received: %q", *id, datafactory.TypeBasicLinkedServiceTypeAzureTableStorage, *resp.Type) } d.Set("additional_properties", tableStorage.AdditionalProperties) @@ -204,7 +196,7 @@ func resourceDataFactoryLinkedServiceTableStorageRead(d *pluginsdk.ResourceData, annotations := flattenDataFactoryAnnotations(tableStorage.Annotations) if err := d.Set("annotations", annotations); err != nil { - return fmt.Errorf("setting `annotations` for Data Factory Linked Service Azure Table Storage %q (Data Factory %q) / Resource Group %q): %+v", id.Name, id.FactoryName, id.ResourceGroup, err) + return fmt.Errorf("setting `annotations` for Data Factory Azure Table Storage %s: %+v", *id, err) } parameters := flattenDataFactoryParameters(tableStorage.Parameters) @@ -234,7 +226,7 @@ func resourceDataFactoryLinkedServiceTableStorageDelete(d *pluginsdk.ResourceDat response, err := client.Delete(ctx, id.ResourceGroup, id.FactoryName, id.Name) if err != nil { if !utils.ResponseWasNotFound(response) { - return fmt.Errorf("deleting Data Factory Linked Service TableStorage %q (Data Factory %q / Resource Group %q): %+v", id.Name, id.FactoryName, id.ResourceGroup, err) + return fmt.Errorf("deleting Data Factory Table Storage %s: %+v", *id, err) } } diff --git a/internal/services/datafactory/data_factory_linked_service_azure_table_storage_resource_test.go b/internal/services/datafactory/data_factory_linked_service_azure_table_storage_resource_test.go index ea910a345880..c8b170bbb080 100644 --- a/internal/services/datafactory/data_factory_linked_service_azure_table_storage_resource_test.go +++ b/internal/services/datafactory/data_factory_linked_service_azure_table_storage_resource_test.go @@ -5,10 +5,10 @@ import ( "fmt" "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/datafactory/parse" "github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" "github.com/hashicorp/terraform-provider-azurerm/utils" ) @@ -62,17 +62,14 @@ func TestAccDataFactoryLinkedServiceAzureTableStorage_update(t *testing.T) { } func (t LinkedServiceAzureTableStorageResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) { - id, err := azure.ParseAzureResourceID(state.ID) + id, err := parse.LinkedServiceID(state.ID) if err != nil { return nil, err } - resourceGroup := id.ResourceGroup - dataFactoryName := id.Path["factories"] - name := id.Path["linkedservices"] - resp, err := clients.DataFactory.LinkedServiceClient.Get(ctx, resourceGroup, dataFactoryName, name, "") + resp, err := clients.DataFactory.LinkedServiceClient.Get(ctx, id.ResourceGroup, id.FactoryName, id.Name, "") if err != nil { - return nil, fmt.Errorf("reading Data Factory Linked Service Azure Table Storage (%s): %+v", id, err) + return nil, fmt.Errorf("reading Data Factory Azure Table Storage (%s): %+v", *id, err) } return utils.Bool(resp.ID != nil), nil diff --git a/internal/services/datafactory/data_factory_linked_service_cosmosdb_resource.go b/internal/services/datafactory/data_factory_linked_service_cosmosdb_resource.go index a3e5840c787e..7c19e1f7cb00 100644 --- a/internal/services/datafactory/data_factory_linked_service_cosmosdb_resource.go +++ b/internal/services/datafactory/data_factory_linked_service_cosmosdb_resource.go @@ -23,8 +23,10 @@ func resourceDataFactoryLinkedServiceCosmosDb() *pluginsdk.Resource { Update: resourceDataFactoryLinkedServiceCosmosDbCreateUpdate, Delete: resourceDataFactoryLinkedServiceCosmosDbDelete, - // TODO: replace this with an importer which validates the ID during import - Importer: pluginsdk.DefaultImporter(), + Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error { + _, err := parse.LinkedServiceID(id) + return err + }), Timeouts: &pluginsdk.ResourceTimeout{ Create: pluginsdk.DefaultTimeout(30 * time.Minute), @@ -123,18 +125,17 @@ func resourceDataFactoryLinkedServiceCosmosDb() *pluginsdk.Resource { func resourceDataFactoryLinkedServiceCosmosDbCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) error { client := meta.(*clients.Client).DataFactory.LinkedServiceClient + subscriptionId := meta.(*clients.Client).DataFactory.LinkedServiceClient.SubscriptionID ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d) defer cancel() - name := d.Get("name").(string) - dataFactoryName := d.Get("data_factory_name").(string) - resourceGroup := d.Get("resource_group_name").(string) + id := parse.NewLinkedServiceID(subscriptionId, d.Get("resource_group_name").(string), d.Get("data_factory_name").(string), d.Get("name").(string)) if d.IsNewResource() { - existing, err := client.Get(ctx, resourceGroup, dataFactoryName, name, "") + existing, err := client.Get(ctx, id.ResourceGroup, id.FactoryName, id.Name, "") if err != nil { if !utils.ResponseWasNotFound(existing.Response) { - return fmt.Errorf("checking for presence of existing Data Factory Linked Service CosmosDb %q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err) + return fmt.Errorf("checking for presence of existing Data Factory CosmosDb %s: %+v", id, err) } } @@ -198,20 +199,11 @@ func resourceDataFactoryLinkedServiceCosmosDbCreateUpdate(d *pluginsdk.ResourceD Properties: cosmosdbLinkedService, } - if _, err := client.CreateOrUpdate(ctx, resourceGroup, dataFactoryName, name, linkedService, ""); err != nil { - return fmt.Errorf("creating/updating Data Factory Linked Service CosmosDb %q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err) + if _, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.FactoryName, id.Name, linkedService, ""); err != nil { + return fmt.Errorf("creating/updating Data Factory CosmosDb %s: %+v", id, err) } - resp, err := client.Get(ctx, resourceGroup, dataFactoryName, name, "") - if err != nil { - return fmt.Errorf("retrieving Data Factory Linked Service CosmosDb %q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err) - } - - if resp.ID == nil { - return fmt.Errorf("Cannot read Data Factory Linked Service CosmosDb %q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err) - } - - d.SetId(*resp.ID) + d.SetId(id.ID()) return resourceDataFactoryLinkedServiceCosmosDbRead(d, meta) } @@ -233,7 +225,7 @@ func resourceDataFactoryLinkedServiceCosmosDbRead(d *pluginsdk.ResourceData, met return nil } - return fmt.Errorf("retrieving Data Factory Linked Service CosmosDB %q (Data Factory %q / Resource Group %q): %+v", id.Name, id.FactoryName, id.ResourceGroup, err) + return fmt.Errorf("retrieving Data Factory CosmosDB %s: %+v", *id, err) } d.Set("name", resp.Name) @@ -242,7 +234,7 @@ func resourceDataFactoryLinkedServiceCosmosDbRead(d *pluginsdk.ResourceData, met cosmosdb, ok := resp.Properties.AsCosmosDbLinkedService() if !ok { - return fmt.Errorf("classifying Data Factory Linked Service CosmosDb %q (Data Factory %q / Resource Group %q): Expected: %q Received: %q", id.Name, id.FactoryName, id.ResourceGroup, datafactory.TypeBasicLinkedServiceTypeCosmosDb, *resp.Type) + return fmt.Errorf("classifying Data Factory CosmosDb %s: Expected: %q Received: %q", *id, datafactory.TypeBasicLinkedServiceTypeCosmosDb, *resp.Type) } d.Set("additional_properties", cosmosdb.AdditionalProperties) @@ -288,7 +280,7 @@ func resourceDataFactoryLinkedServiceCosmosDbDelete(d *pluginsdk.ResourceData, m response, err := client.Delete(ctx, id.ResourceGroup, id.FactoryName, id.Name) if err != nil { if !utils.ResponseWasNotFound(response) { - return fmt.Errorf("deleting Data Factory Linked Service CosmosDb %q (Data Factory %q / Resource Group %q): %+v", id.Name, id.FactoryName, id.ResourceGroup, err) + return fmt.Errorf("deleting Data Factory CosmosDb %s: %+v", *id, err) } } diff --git a/internal/services/datafactory/data_factory_linked_service_cosmosdb_resource_test.go b/internal/services/datafactory/data_factory_linked_service_cosmosdb_resource_test.go index b02f20560821..10fce3800e68 100644 --- a/internal/services/datafactory/data_factory_linked_service_cosmosdb_resource_test.go +++ b/internal/services/datafactory/data_factory_linked_service_cosmosdb_resource_test.go @@ -5,10 +5,10 @@ import ( "fmt" "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/datafactory/parse" "github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" "github.com/hashicorp/terraform-provider-azurerm/utils" ) @@ -101,17 +101,14 @@ func TestAccDataFactoryLinkedServiceCosmosDb_update(t *testing.T) { } func (t LinkedServiceCosmosDBResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) { - id, err := azure.ParseAzureResourceID(state.ID) + id, err := parse.LinkedServiceID(state.ID) if err != nil { return nil, err } - resourceGroup := id.ResourceGroup - dataFactoryName := id.Path["factories"] - name := id.Path["linkedservices"] - resp, err := clients.DataFactory.LinkedServiceClient.Get(ctx, resourceGroup, dataFactoryName, name, "") + resp, err := clients.DataFactory.LinkedServiceClient.Get(ctx, id.ResourceGroup, id.FactoryName, id.Name, "") if err != nil { - return nil, fmt.Errorf("reading Data Factory Linked Service CosmosDB (%s): %+v", id, err) + return nil, fmt.Errorf("reading Data Factory CosmosDB (%s): %+v", *id, err) } return utils.Bool(resp.ID != nil), nil diff --git a/internal/services/datafactory/data_factory_linked_service_data_lake_storage_gen2_resource.go b/internal/services/datafactory/data_factory_linked_service_data_lake_storage_gen2_resource.go index 3df2c1b7b30b..b822b73645cf 100644 --- a/internal/services/datafactory/data_factory_linked_service_data_lake_storage_gen2_resource.go +++ b/internal/services/datafactory/data_factory_linked_service_data_lake_storage_gen2_resource.go @@ -23,8 +23,10 @@ func resourceDataFactoryLinkedServiceDataLakeStorageGen2() *pluginsdk.Resource { Update: resourceDataFactoryLinkedServiceDataLakeStorageGen2CreateUpdate, Delete: resourceDataFactoryLinkedServiceDataLakeStorageGen2Delete, - // TODO: replace this with an importer which validates the ID during import - Importer: pluginsdk.DefaultImporter(), + Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error { + _, err := parse.LinkedServiceID(id) + return err + }), Timeouts: &pluginsdk.ResourceTimeout{ Create: pluginsdk.DefaultTimeout(30 * time.Minute), @@ -141,18 +143,17 @@ func resourceDataFactoryLinkedServiceDataLakeStorageGen2() *pluginsdk.Resource { func resourceDataFactoryLinkedServiceDataLakeStorageGen2CreateUpdate(d *pluginsdk.ResourceData, meta interface{}) error { client := meta.(*clients.Client).DataFactory.LinkedServiceClient + subscriptionId := meta.(*clients.Client).DataFactory.LinkedServiceClient.SubscriptionID ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d) defer cancel() - name := d.Get("name").(string) - dataFactoryName := d.Get("data_factory_name").(string) - resourceGroup := d.Get("resource_group_name").(string) + id := parse.NewLinkedServiceID(subscriptionId, d.Get("resource_group_name").(string), d.Get("data_factory_name").(string), d.Get("name").(string)) if d.IsNewResource() { - existing, err := client.Get(ctx, resourceGroup, dataFactoryName, name, "") + existing, err := client.Get(ctx, id.ResourceGroup, id.FactoryName, id.Name, "") if err != nil { if !utils.ResponseWasNotFound(existing.Response) { - return fmt.Errorf("checking for presence of existing Data Factory Linked Service Data Lake Storage Gen2 %q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err) + return fmt.Errorf("checking for presence of existing Data Factory Data Lake Storage Gen2 %s: %+v", id, err) } } @@ -216,20 +217,11 @@ func resourceDataFactoryLinkedServiceDataLakeStorageGen2CreateUpdate(d *pluginsd Properties: datalakeStorageGen2LinkedService, } - if _, err := client.CreateOrUpdate(ctx, resourceGroup, dataFactoryName, name, linkedService, ""); err != nil { - return fmt.Errorf("creating/updating Data Factory Linked Service Data Lake Storage Gen2 %q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err) + if _, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.FactoryName, id.Name, linkedService, ""); err != nil { + return fmt.Errorf("creating/updating Data Factory Data Lake Storage Gen2 %s: %+v", id, err) } - resp, err := client.Get(ctx, resourceGroup, dataFactoryName, name, "") - if err != nil { - return fmt.Errorf("retrieving Data Factory Linked Service Data Lake Storage Gen2 %q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err) - } - - if resp.ID == nil { - return fmt.Errorf("Cannot read Data Factory Linked Service Data Lake Storage Gen2 %q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err) - } - - d.SetId(*resp.ID) + d.SetId(id.ID()) return resourceDataFactoryLinkedServiceDataLakeStorageGen2Read(d, meta) } @@ -251,7 +243,7 @@ func resourceDataFactoryLinkedServiceDataLakeStorageGen2Read(d *pluginsdk.Resour return nil } - return fmt.Errorf("retrieving Data Factory Linked Service Data Lake Storage Gen2 %q (Data Factory %q / Resource Group %q): %+v", id.Name, id.FactoryName, id.ResourceGroup, err) + return fmt.Errorf("retrieving Data Factory Data Lake Storage Gen2 %s: %+v", *id, err) } d.Set("name", resp.Name) @@ -261,7 +253,7 @@ func resourceDataFactoryLinkedServiceDataLakeStorageGen2Read(d *pluginsdk.Resour dataLakeStorageGen2, ok := resp.Properties.AsAzureBlobFSLinkedService() if !ok { - return fmt.Errorf("classifying Data Factory Linked Service Data Lake Storage Gen2 %q (Data Factory %q / Resource Group %q): Expected: %q Received: %q", id.Name, id.FactoryName, id.ResourceGroup, datafactory.TypeBasicLinkedServiceTypeAzureBlobFS, *resp.Type) + return fmt.Errorf("classifying Data Factory Data Lake Storage Gen2 %s: Expected: %q Received: %q", *id, datafactory.TypeBasicLinkedServiceTypeAzureBlobFS, *resp.Type) } if dataLakeStorageGen2.Tenant != nil { @@ -315,7 +307,7 @@ func resourceDataFactoryLinkedServiceDataLakeStorageGen2Delete(d *pluginsdk.Reso response, err := client.Delete(ctx, id.ResourceGroup, id.FactoryName, id.Name) if err != nil { if !utils.ResponseWasNotFound(response) { - return fmt.Errorf("deleting Data Factory Linked Service Data Lake Storage Gen2 %q (Data Factory %q / Resource Group %q): %+v", id.Name, id.FactoryName, id.ResourceGroup, err) + return fmt.Errorf("deleting Data Factory Data Lake Storage Gen2 %s: %+v", *id, err) } } diff --git a/internal/services/datafactory/data_factory_linked_service_data_lake_storage_gen2_resource_test.go b/internal/services/datafactory/data_factory_linked_service_data_lake_storage_gen2_resource_test.go index 26c2269420fa..c73e55859873 100644 --- a/internal/services/datafactory/data_factory_linked_service_data_lake_storage_gen2_resource_test.go +++ b/internal/services/datafactory/data_factory_linked_service_data_lake_storage_gen2_resource_test.go @@ -5,10 +5,10 @@ import ( "fmt" "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/datafactory/parse" "github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" "github.com/hashicorp/terraform-provider-azurerm/utils" ) @@ -91,17 +91,14 @@ func TestAccDataFactoryLinkedServiceDataLakeStorageGen2_update(t *testing.T) { } func (t LinkedServiceDataLakeStorageGen2Resource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) { - id, err := azure.ParseAzureResourceID(state.ID) + id, err := parse.LinkedServiceID(state.ID) if err != nil { return nil, err } - resourceGroup := id.ResourceGroup - dataFactoryName := id.Path["factories"] - name := id.Path["linkedservices"] - resp, err := clients.DataFactory.LinkedServiceClient.Get(ctx, resourceGroup, dataFactoryName, name, "") + resp, err := clients.DataFactory.LinkedServiceClient.Get(ctx, id.ResourceGroup, id.FactoryName, id.Name, "") if err != nil { - return nil, fmt.Errorf("reading Data Factory LinkedServiceDataLakeStorageGen2Resource (%s): %+v", id, err) + return nil, fmt.Errorf("reading Data Factory DataLake Storage Gen 2 Resource (%s): %+v", *id, err) } return utils.Bool(resp.ID != nil), nil diff --git a/internal/services/datafactory/data_factory_linked_service_key_vault_resource.go b/internal/services/datafactory/data_factory_linked_service_key_vault_resource.go index 8331b46630e8..5bff3da933d1 100644 --- a/internal/services/datafactory/data_factory_linked_service_key_vault_resource.go +++ b/internal/services/datafactory/data_factory_linked_service_key_vault_resource.go @@ -25,8 +25,10 @@ func resourceDataFactoryLinkedServiceKeyVault() *pluginsdk.Resource { Update: resourceDataFactoryLinkedServiceKeyVaultCreateUpdate, Delete: resourceDataFactoryLinkedServiceKeyVaultDelete, - // TODO: replace this with an importer which validates the ID during import - Importer: pluginsdk.DefaultImporter(), + Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error { + _, err := parse.LinkedServiceID(id) + return err + }), Timeouts: &pluginsdk.ResourceTimeout{ Create: pluginsdk.DefaultTimeout(30 * time.Minute), @@ -101,13 +103,13 @@ func resourceDataFactoryLinkedServiceKeyVault() *pluginsdk.Resource { func resourceDataFactoryLinkedServiceKeyVaultCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) error { client := meta.(*clients.Client).DataFactory.LinkedServiceClient + subscriptionId := meta.(*clients.Client).DataFactory.LinkedServiceClient.SubscriptionID keyVaultsClient := meta.(*clients.Client).KeyVault ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d) defer cancel() - name := d.Get("name").(string) - dataFactoryName := d.Get("data_factory_name").(string) - resourceGroup := d.Get("resource_group_name").(string) + id := parse.NewLinkedServiceID(subscriptionId, d.Get("resource_group_name").(string), d.Get("data_factory_name").(string), d.Get("name").(string)) + keyVaultId, err := keyVaultParse.VaultID(d.Get("key_vault_id").(string)) if err != nil { return err @@ -119,10 +121,10 @@ func resourceDataFactoryLinkedServiceKeyVaultCreateUpdate(d *pluginsdk.ResourceD } if d.IsNewResource() { - existing, err := client.Get(ctx, resourceGroup, dataFactoryName, name, "") + existing, err := client.Get(ctx, id.ResourceGroup, id.FactoryName, id.Name, "") if err != nil { if !utils.ResponseWasNotFound(existing.Response) { - return fmt.Errorf("checking for presence of existing Data Factory Linked Service Key Vault %q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err) + return fmt.Errorf("checking for presence of existing Data Factory Key Vault %s: %+v", id, err) } } @@ -162,20 +164,11 @@ func resourceDataFactoryLinkedServiceKeyVaultCreateUpdate(d *pluginsdk.ResourceD Properties: azureKeyVaultLinkedService, } - if _, err := client.CreateOrUpdate(ctx, resourceGroup, dataFactoryName, name, linkedService, ""); err != nil { - return fmt.Errorf("creating/updating Data Factory Linked Service Key Vault %q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err) - } - - resp, err := client.Get(ctx, resourceGroup, dataFactoryName, name, "") - if err != nil { - return fmt.Errorf("retrieving Data Factory Linked Service Key Vault %q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err) - } - - if resp.ID == nil { - return fmt.Errorf("Cannot read Data Factory Linked Service Key Vault %q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err) + if _, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.FactoryName, id.Name, linkedService, ""); err != nil { + return fmt.Errorf("creating/updating Data Factory Key Vault %s: %+v", id, err) } - d.SetId(*resp.ID) + d.SetId(id.ID()) return resourceDataFactoryLinkedServiceKeyVaultRead(d, meta) } @@ -199,7 +192,7 @@ func resourceDataFactoryLinkedServiceKeyVaultRead(d *pluginsdk.ResourceData, met return nil } - return fmt.Errorf("retrieving Data Factory Linked Service Key Vault %q (Data Factory %q / Resource Group %q): %+v", id.Name, id.FactoryName, id.ResourceGroup, err) + return fmt.Errorf("retrieving Data Factory Key Vault %s: %+v", *id, err) } d.Set("name", resp.Name) @@ -208,7 +201,7 @@ func resourceDataFactoryLinkedServiceKeyVaultRead(d *pluginsdk.ResourceData, met keyVault, ok := resp.Properties.AsAzureKeyVaultLinkedService() if !ok { - return fmt.Errorf("classifying Data Factory Linked Service Key Vault %q (Data Factory %q / Resource Group %q): Expected: %q Received: %q", id.Name, id.FactoryName, id.ResourceGroup, datafactory.TypeBasicLinkedServiceTypeAzureKeyVault, *resp.Type) + return fmt.Errorf("classifying Data Factory Key Vault %s: Expected: %q Received: %q", *id, datafactory.TypeBasicLinkedServiceTypeAzureKeyVault, *resp.Type) } d.Set("additional_properties", keyVault.AdditionalProperties) @@ -267,7 +260,7 @@ func resourceDataFactoryLinkedServiceKeyVaultDelete(d *pluginsdk.ResourceData, m response, err := client.Delete(ctx, id.ResourceGroup, id.FactoryName, id.Name) if err != nil { if !utils.ResponseWasNotFound(response) { - return fmt.Errorf("deleting Data Factory Linked Service Key Vault %q (Data Factory %q / Resource Group %q): %+v", id.Name, id.FactoryName, id.ResourceGroup, err) + return fmt.Errorf("deleting Data Factory Key Vault %s: %+v", *id, err) } } diff --git a/internal/services/datafactory/data_factory_linked_service_key_vault_resource_test.go b/internal/services/datafactory/data_factory_linked_service_key_vault_resource_test.go index 907fdbc5bab2..9c3754d94b1e 100644 --- a/internal/services/datafactory/data_factory_linked_service_key_vault_resource_test.go +++ b/internal/services/datafactory/data_factory_linked_service_key_vault_resource_test.go @@ -5,10 +5,10 @@ import ( "fmt" "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/datafactory/parse" "github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" "github.com/hashicorp/terraform-provider-azurerm/utils" ) @@ -61,17 +61,14 @@ func TestAccDataFactoryLinkedServiceKeyVault_update(t *testing.T) { } func (t LinkedServiceKeyVaultResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) { - id, err := azure.ParseAzureResourceID(state.ID) + id, err := parse.LinkedServiceID(state.ID) if err != nil { return nil, err } - resourceGroup := id.ResourceGroup - dataFactoryName := id.Path["factories"] - name := id.Path["linkedservices"] - resp, err := clients.DataFactory.LinkedServiceClient.Get(ctx, resourceGroup, dataFactoryName, name, "") + resp, err := clients.DataFactory.LinkedServiceClient.Get(ctx, id.ResourceGroup, id.FactoryName, id.Name, "") if err != nil { - return nil, fmt.Errorf("reading Data Factory LinkedServiceKeyVaultResource (%s): %+v", id, err) + return nil, fmt.Errorf("reading Data Factory Key Vault Resource (%s): %+v", *id, err) } return utils.Bool(resp.ID != nil), nil diff --git a/internal/services/datafactory/data_factory_linked_service_kusto_resource.go b/internal/services/datafactory/data_factory_linked_service_kusto_resource.go index 5779ff6255d2..3c07bc8362d4 100644 --- a/internal/services/datafactory/data_factory_linked_service_kusto_resource.go +++ b/internal/services/datafactory/data_factory_linked_service_kusto_resource.go @@ -22,8 +22,10 @@ func resourceDataFactoryLinkedServiceKusto() *pluginsdk.Resource { Update: resourceDataFactoryLinkedServiceKustoCreateUpdate, Delete: resourceDataFactoryLinkedServiceKustoDelete, - // TODO: replace this with an importer which validates the ID during import - Importer: pluginsdk.DefaultImporter(), + Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error { + _, err := parse.LinkedServiceID(id) + return err + }), Timeouts: &pluginsdk.ResourceTimeout{ Create: pluginsdk.DefaultTimeout(30 * time.Minute), @@ -234,7 +236,7 @@ func resourceDataFactoryLinkedServiceKustoRead(d *pluginsdk.ResourceData, meta i linkedService, ok := resp.Properties.AsAzureDataExplorerLinkedService() if !ok { - return fmt.Errorf("classifying %s: Expected: %q", id, datafactory.TypeBasicLinkedServiceTypeAzureDataExplorer) + return fmt.Errorf("classifying %s: Expected: %q", *id, datafactory.TypeBasicLinkedServiceTypeAzureDataExplorer) } d.Set("name", id.Name) @@ -281,7 +283,7 @@ func resourceDataFactoryLinkedServiceKustoDelete(d *pluginsdk.ResourceData, meta } if _, err := client.Delete(ctx, id.ResourceGroup, id.FactoryName, id.Name); err != nil { - return fmt.Errorf("deleting %s: %+v", id, err) + return fmt.Errorf("deleting %s: %+v", *id, err) } return nil diff --git a/internal/services/datafactory/data_factory_linked_service_kusto_resource_test.go b/internal/services/datafactory/data_factory_linked_service_kusto_resource_test.go index 1c3de82cf6c3..72042b83502f 100644 --- a/internal/services/datafactory/data_factory_linked_service_kusto_resource_test.go +++ b/internal/services/datafactory/data_factory_linked_service_kusto_resource_test.go @@ -113,7 +113,7 @@ func (t LinkedServiceKustoResource) Exists(ctx context.Context, clients *clients resp, err := clients.DataFactory.LinkedServiceClient.Get(ctx, id.ResourceGroup, id.FactoryName, id.Name, "") if err != nil { - return nil, fmt.Errorf("reading %s: %+v", id, err) + return nil, fmt.Errorf("reading %s: %+v", *id, err) } return utils.Bool(resp.ID != nil), nil diff --git a/internal/services/datafactory/data_factory_linked_service_mysql_resource.go b/internal/services/datafactory/data_factory_linked_service_mysql_resource.go index a038fcb851d6..340e7853e874 100644 --- a/internal/services/datafactory/data_factory_linked_service_mysql_resource.go +++ b/internal/services/datafactory/data_factory_linked_service_mysql_resource.go @@ -23,8 +23,10 @@ func resourceDataFactoryLinkedServiceMySQL() *pluginsdk.Resource { Update: resourceDataFactoryLinkedServiceMySQLCreateUpdate, Delete: resourceDataFactoryLinkedServiceMySQLDelete, - // TODO: replace this with an importer which validates the ID during import - Importer: pluginsdk.DefaultImporter(), + Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error { + _, err := parse.LinkedServiceID(id) + return err + }), Timeouts: &pluginsdk.ResourceTimeout{ Create: pluginsdk.DefaultTimeout(30 * time.Minute), @@ -100,18 +102,17 @@ func resourceDataFactoryLinkedServiceMySQL() *pluginsdk.Resource { func resourceDataFactoryLinkedServiceMySQLCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) error { client := meta.(*clients.Client).DataFactory.LinkedServiceClient + subscriptionId := meta.(*clients.Client).DataFactory.LinkedServiceClient.SubscriptionID ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d) defer cancel() - name := d.Get("name").(string) - dataFactoryName := d.Get("data_factory_name").(string) - resourceGroup := d.Get("resource_group_name").(string) + id := parse.NewLinkedServiceID(subscriptionId, d.Get("resource_group_name").(string), d.Get("data_factory_name").(string), d.Get("name").(string)) if d.IsNewResource() { - existing, err := client.Get(ctx, resourceGroup, dataFactoryName, name, "") + existing, err := client.Get(ctx, id.ResourceGroup, id.FactoryName, id.Name, "") if err != nil { if !utils.ResponseWasNotFound(existing.Response) { - return fmt.Errorf("checking for presence of existing Data Factory Linked Service MySQL %q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err) + return fmt.Errorf("checking for presence of existing Data Factory MySQL %s: %+v", id, err) } } @@ -159,20 +160,11 @@ func resourceDataFactoryLinkedServiceMySQLCreateUpdate(d *pluginsdk.ResourceData Properties: mysqlLinkedService, } - if _, err := client.CreateOrUpdate(ctx, resourceGroup, dataFactoryName, name, linkedService, ""); err != nil { - return fmt.Errorf("creating/updating Data Factory Linked Service MySQL %q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err) + if _, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.FactoryName, id.Name, linkedService, ""); err != nil { + return fmt.Errorf("creating/updating Data Factory MySQL %s: %+v", id, err) } - resp, err := client.Get(ctx, resourceGroup, dataFactoryName, name, "") - if err != nil { - return fmt.Errorf("retrieving Data Factory Linked Service MySQL %q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err) - } - - if resp.ID == nil { - return fmt.Errorf("Cannot read Data Factory Linked Service MySQL %q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err) - } - - d.SetId(*resp.ID) + d.SetId(id.ID()) return resourceDataFactoryLinkedServiceMySQLRead(d, meta) } @@ -194,7 +186,7 @@ func resourceDataFactoryLinkedServiceMySQLRead(d *pluginsdk.ResourceData, meta i return nil } - return fmt.Errorf("retrieving Data Factory Linked Service MySQL %q (Data Factory %q / Resource Group %q): %+v", id.Name, id.FactoryName, id.ResourceGroup, err) + return fmt.Errorf("retrieving Data Factory MySQL %s: %+v", *id, err) } d.Set("name", resp.Name) @@ -203,7 +195,7 @@ func resourceDataFactoryLinkedServiceMySQLRead(d *pluginsdk.ResourceData, meta i mysql, ok := resp.Properties.AsMySQLLinkedService() if !ok { - return fmt.Errorf("classifying Data Factory Linked Service MySQL %q (Data Factory %q / Resource Group %q): Expected: %q Received: %q", id.Name, id.FactoryName, id.ResourceGroup, datafactory.TypeBasicLinkedServiceTypeMySQL, *resp.Type) + return fmt.Errorf("classifying Data Factory MySQL %s: Expected: %q Received: %q", *id, datafactory.TypeBasicLinkedServiceTypeMySQL, *resp.Type) } d.Set("additional_properties", mysql.AdditionalProperties) @@ -241,7 +233,7 @@ func resourceDataFactoryLinkedServiceMySQLDelete(d *pluginsdk.ResourceData, meta response, err := client.Delete(ctx, id.ResourceGroup, id.FactoryName, id.Name) if err != nil { if !utils.ResponseWasNotFound(response) { - return fmt.Errorf("deleting Data Factory Linked Service MySQL %q (Data Factory %q / Resource Group %q): %+v", id.Name, id.FactoryName, id.ResourceGroup, err) + return fmt.Errorf("deleting Data Factory MySQL %s: %+v", *id, err) } } diff --git a/internal/services/datafactory/data_factory_linked_service_mysql_resource_test.go b/internal/services/datafactory/data_factory_linked_service_mysql_resource_test.go index dbaa16262cb6..96100de164a3 100644 --- a/internal/services/datafactory/data_factory_linked_service_mysql_resource_test.go +++ b/internal/services/datafactory/data_factory_linked_service_mysql_resource_test.go @@ -5,10 +5,10 @@ import ( "fmt" "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/datafactory/parse" "github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" "github.com/hashicorp/terraform-provider-azurerm/utils" ) @@ -61,17 +61,14 @@ func TestAccDataFactoryLinkedServiceMySQL_update(t *testing.T) { } func (t LinkedServiceMySQLResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) { - id, err := azure.ParseAzureResourceID(state.ID) + id, err := parse.LinkedServiceID(state.ID) if err != nil { return nil, err } - resourceGroup := id.ResourceGroup - dataFactoryName := id.Path["factories"] - name := id.Path["linkedservices"] - resp, err := clients.DataFactory.LinkedServiceClient.Get(ctx, resourceGroup, dataFactoryName, name, "") + resp, err := clients.DataFactory.LinkedServiceClient.Get(ctx, id.ResourceGroup, id.FactoryName, id.Name, "") if err != nil { - return nil, fmt.Errorf("reading Data Factory Linked Service MySQL (%s): %+v", id, err) + return nil, fmt.Errorf("reading Data Factory MySQL (%s): %+v", *id, err) } return utils.Bool(resp.ID != nil), nil diff --git a/internal/services/datafactory/data_factory_linked_service_odata_resource.go b/internal/services/datafactory/data_factory_linked_service_odata_resource.go index e633430c3dde..9821e125fce6 100644 --- a/internal/services/datafactory/data_factory_linked_service_odata_resource.go +++ b/internal/services/datafactory/data_factory_linked_service_odata_resource.go @@ -23,8 +23,10 @@ func resourceArmDataFactoryLinkedServiceOData() *pluginsdk.Resource { Update: resourceArmDataFactoryLinkedServiceODataCreateUpdate, Delete: resourceArmDataFactoryLinkedServiceODataDelete, - // TODO: add a custom importer for this - Importer: pluginsdk.DefaultImporter(), + Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error { + _, err := parse.LinkedServiceID(id) + return err + }), Timeouts: &pluginsdk.ResourceTimeout{ Create: pluginsdk.DefaultTimeout(30 * time.Minute), @@ -124,18 +126,17 @@ func resourceArmDataFactoryLinkedServiceOData() *pluginsdk.Resource { func resourceArmDataFactoryLinkedServiceODataCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) error { client := meta.(*clients.Client).DataFactory.LinkedServiceClient + subscriptionId := meta.(*clients.Client).DataFactory.LinkedServiceClient.SubscriptionID ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d) defer cancel() - name := d.Get("name").(string) - dataFactoryName := d.Get("data_factory_name").(string) - resourceGroup := d.Get("resource_group_name").(string) + id := parse.NewLinkedServiceID(subscriptionId, d.Get("resource_group_name").(string), d.Get("data_factory_name").(string), d.Get("name").(string)) if d.IsNewResource() { - existing, err := client.Get(ctx, resourceGroup, dataFactoryName, name, "") + existing, err := client.Get(ctx, id.ResourceGroup, id.FactoryName, id.Name, "") if err != nil { if !utils.ResponseWasNotFound(existing.Response) { - return fmt.Errorf("checking for presence of existing Data Factory Linked Service OData Anonymous %q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err) + return fmt.Errorf("checking for presence of existing Data Factory OData Anonymous %s: %+v", id, err) } } @@ -189,20 +190,11 @@ func resourceArmDataFactoryLinkedServiceODataCreateUpdate(d *pluginsdk.ResourceD Properties: odataLinkedService, } - if _, err := client.CreateOrUpdate(ctx, resourceGroup, dataFactoryName, name, linkedService, ""); err != nil { - return fmt.Errorf("creating/updating Data Factory Linked Service OData Anonymous %q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err) + if _, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.FactoryName, id.Name, linkedService, ""); err != nil { + return fmt.Errorf("creating/updating Data Factory OData Anonymous %s: %+v", id, err) } - resp, err := client.Get(ctx, resourceGroup, dataFactoryName, name, "") - if err != nil { - return fmt.Errorf("retrieving Data Factory Linked Service OData Anonymous %q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err) - } - - if resp.ID == nil { - return fmt.Errorf("Cannot read Data Factory Linked Service OData Anonymous %q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err) - } - - d.SetId(*resp.ID) + d.SetId(id.ID()) return resourceArmDataFactoryLinkedServiceODataRead(d, meta) } @@ -224,7 +216,7 @@ func resourceArmDataFactoryLinkedServiceODataRead(d *pluginsdk.ResourceData, met return nil } - return fmt.Errorf("retrieving Data Factory Linked Service OData %q (Data Factory %q / Resource Group %q): %+v", id.Name, id.FactoryName, id.ResourceGroup, err) + return fmt.Errorf("retrieving Data Factory OData %s: %+v", *id, err) } d.Set("name", resp.Name) @@ -233,7 +225,7 @@ func resourceArmDataFactoryLinkedServiceODataRead(d *pluginsdk.ResourceData, met odata, ok := resp.Properties.AsODataLinkedService() if !ok { - return fmt.Errorf("classifying Data Factory Linked Service OData %q (Data Factory %q / Resource Group %q): Expected: %q Received: %q", id.Name, id.FactoryName, id.ResourceGroup, datafactory.TypeBasicLinkedServiceTypeOData, *resp.Type) + return fmt.Errorf("classifying Data Factory OData %s: Expected: %q Received: %q", *id, datafactory.TypeBasicLinkedServiceTypeOData, *resp.Type) } props := odata.ODataLinkedServiceTypeProperties @@ -283,7 +275,7 @@ func resourceArmDataFactoryLinkedServiceODataDelete(d *pluginsdk.ResourceData, m response, err := client.Delete(ctx, id.ResourceGroup, id.FactoryName, id.Name) if err != nil { if !utils.ResponseWasNotFound(response) { - return fmt.Errorf("deleting Data Factory Linked Service OData %q (Data Factory %q / Resource Group %q): %+v", id.Name, id.FactoryName, id.ResourceGroup, err) + return fmt.Errorf("deleting Data Factory OData %s: %+v", *id, err) } } diff --git a/internal/services/datafactory/data_factory_linked_service_odata_resource_test.go b/internal/services/datafactory/data_factory_linked_service_odata_resource_test.go index 99cd3c146fad..9a8220298d64 100644 --- a/internal/services/datafactory/data_factory_linked_service_odata_resource_test.go +++ b/internal/services/datafactory/data_factory_linked_service_odata_resource_test.go @@ -76,7 +76,7 @@ func (t LinkedServiceODataResource) Exists(ctx context.Context, clients *clients resp, err := clients.DataFactory.LinkedServiceClient.Get(ctx, id.ResourceGroup, id.FactoryName, id.Name, "") if err != nil { - return nil, fmt.Errorf("reading Data Factory Linked Service OData (%s): %+v", id, err) + return nil, fmt.Errorf("reading Data Factory OData (%s): %+v", *id, err) } return utils.Bool(resp.ID != nil), nil diff --git a/internal/services/datafactory/data_factory_linked_service_postgresql_resource.go b/internal/services/datafactory/data_factory_linked_service_postgresql_resource.go index 412392c41514..dce1d06400d0 100644 --- a/internal/services/datafactory/data_factory_linked_service_postgresql_resource.go +++ b/internal/services/datafactory/data_factory_linked_service_postgresql_resource.go @@ -23,8 +23,10 @@ func resourceDataFactoryLinkedServicePostgreSQL() *pluginsdk.Resource { Update: resourceDataFactoryLinkedServicePostgreSQLCreateUpdate, Delete: resourceDataFactoryLinkedServicePostgreSQLDelete, - // TODO: replace this with an importer which validates the ID during import - Importer: pluginsdk.DefaultImporter(), + Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error { + _, err := parse.LinkedServiceID(id) + return err + }), Timeouts: &pluginsdk.ResourceTimeout{ Create: pluginsdk.DefaultTimeout(30 * time.Minute), @@ -100,18 +102,17 @@ func resourceDataFactoryLinkedServicePostgreSQL() *pluginsdk.Resource { func resourceDataFactoryLinkedServicePostgreSQLCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) error { client := meta.(*clients.Client).DataFactory.LinkedServiceClient + subscriptionId := meta.(*clients.Client).DataFactory.LinkedServiceClient.SubscriptionID ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d) defer cancel() - name := d.Get("name").(string) - dataFactoryName := d.Get("data_factory_name").(string) - resourceGroup := d.Get("resource_group_name").(string) + id := parse.NewLinkedServiceID(subscriptionId, d.Get("resource_group_name").(string), d.Get("data_factory_name").(string), d.Get("name").(string)) if d.IsNewResource() { - existing, err := client.Get(ctx, resourceGroup, dataFactoryName, name, "") + existing, err := client.Get(ctx, id.ResourceGroup, id.FactoryName, id.Name, "") if err != nil { if !utils.ResponseWasNotFound(existing.Response) { - return fmt.Errorf("checking for presence of existing Data Factory Linked Service PostgreSQL %q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err) + return fmt.Errorf("checking for presence of existing Data Factory PostgreSQL %s: %+v", id, err) } } @@ -159,20 +160,11 @@ func resourceDataFactoryLinkedServicePostgreSQLCreateUpdate(d *pluginsdk.Resourc Properties: postgresqlLinkedService, } - if _, err := client.CreateOrUpdate(ctx, resourceGroup, dataFactoryName, name, linkedService, ""); err != nil { - return fmt.Errorf("creating/updating Data Factory Linked Service PostgreSQL %q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err) + if _, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.FactoryName, id.Name, linkedService, ""); err != nil { + return fmt.Errorf("creating/updating Data Factory PostgreSQL %s: %+v", id, err) } - resp, err := client.Get(ctx, resourceGroup, dataFactoryName, name, "") - if err != nil { - return fmt.Errorf("retrieving Data Factory Linked Service PostgreSQL %q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err) - } - - if resp.ID == nil { - return fmt.Errorf("Cannot read Data Factory Linked Service PostgreSQL %q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err) - } - - d.SetId(*resp.ID) + d.SetId(id.ID()) return resourceDataFactoryLinkedServicePostgreSQLRead(d, meta) } @@ -194,7 +186,7 @@ func resourceDataFactoryLinkedServicePostgreSQLRead(d *pluginsdk.ResourceData, m return nil } - return fmt.Errorf("retrieving Data Factory Linked Service PostgreSQL %q (Data Factory %q / Resource Group %q): %+v", id.Name, id.FactoryName, id.ResourceGroup, err) + return fmt.Errorf("retrieving Data Factory PostgreSQL %s: %+v", *id, err) } d.Set("name", resp.Name) @@ -241,7 +233,7 @@ func resourceDataFactoryLinkedServicePostgreSQLDelete(d *pluginsdk.ResourceData, response, err := client.Delete(ctx, id.ResourceGroup, id.FactoryName, id.Name) if err != nil { if !utils.ResponseWasNotFound(response) { - return fmt.Errorf("deleting Data Factory Linked Service PostgreSQL %q (Data Factory %q / Resource Group %q): %+v", id.Name, id.FactoryName, id.ResourceGroup, err) + return fmt.Errorf("deleting Data Factory PostgreSQL %s: %+v", *id, err) } } diff --git a/internal/services/datafactory/data_factory_linked_service_postgresql_resource_test.go b/internal/services/datafactory/data_factory_linked_service_postgresql_resource_test.go index ec475406713f..ee2af4af684a 100644 --- a/internal/services/datafactory/data_factory_linked_service_postgresql_resource_test.go +++ b/internal/services/datafactory/data_factory_linked_service_postgresql_resource_test.go @@ -5,10 +5,10 @@ import ( "fmt" "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/datafactory/parse" "github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" "github.com/hashicorp/terraform-provider-azurerm/utils" ) @@ -61,17 +61,14 @@ func TestAccDataFactoryLinkedServicePostgreSQL_update(t *testing.T) { } func (t LinkedServicePostgreSQLResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) { - id, err := azure.ParseAzureResourceID(state.ID) + id, err := parse.LinkedServiceID(state.ID) if err != nil { return nil, err } - resourceGroup := id.ResourceGroup - dataFactoryName := id.Path["factories"] - name := id.Path["linkedservices"] - resp, err := clients.DataFactory.LinkedServiceClient.Get(ctx, resourceGroup, dataFactoryName, name, "") + resp, err := clients.DataFactory.LinkedServiceClient.Get(ctx, id.ResourceGroup, id.FactoryName, id.Name, "") if err != nil { - return nil, fmt.Errorf("reading Data Factory Linked Service PostgreSQL (%s): %+v", id, err) + return nil, fmt.Errorf("reading Data Factory PostgreSQL (%s): %+v", *id, err) } return utils.Bool(resp.ID != nil), nil diff --git a/internal/services/datafactory/data_factory_linked_service_sftp_resource.go b/internal/services/datafactory/data_factory_linked_service_sftp_resource.go index c8a0ccbcba40..768c79bbd6ea 100644 --- a/internal/services/datafactory/data_factory_linked_service_sftp_resource.go +++ b/internal/services/datafactory/data_factory_linked_service_sftp_resource.go @@ -23,8 +23,10 @@ func resourceDataFactoryLinkedServiceSFTP() *pluginsdk.Resource { Update: resourceDataFactoryLinkedServiceSFTPCreateUpdate, Delete: resourceDataFactoryLinkedServiceSFTPDelete, - // TODO: replace this with an importer which validates the ID during import - Importer: pluginsdk.DefaultImporter(), + Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error { + _, err := parse.LinkedServiceID(id) + return err + }), Timeouts: &pluginsdk.ResourceTimeout{ Create: pluginsdk.DefaultTimeout(30 * time.Minute), @@ -134,18 +136,17 @@ func resourceDataFactoryLinkedServiceSFTP() *pluginsdk.Resource { func resourceDataFactoryLinkedServiceSFTPCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) error { client := meta.(*clients.Client).DataFactory.LinkedServiceClient + subscriptionId := meta.(*clients.Client).DataFactory.LinkedServiceClient.SubscriptionID ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d) defer cancel() - name := d.Get("name").(string) - dataFactoryName := d.Get("data_factory_name").(string) - resourceGroup := d.Get("resource_group_name").(string) + id := parse.NewLinkedServiceID(subscriptionId, d.Get("resource_group_name").(string), d.Get("data_factory_name").(string), d.Get("name").(string)) if d.IsNewResource() { - existing, err := client.Get(ctx, resourceGroup, dataFactoryName, name, "") + existing, err := client.Get(ctx, id.ResourceGroup, id.FactoryName, id.Name, "") if err != nil { if !utils.ResponseWasNotFound(existing.Response) { - return fmt.Errorf("checking for presence of existing Data Factory Linked Service SFTP %q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err) + return fmt.Errorf("checking for presence of existing Data Factory SFTP %s: %+v", id, err) } } @@ -205,20 +206,11 @@ func resourceDataFactoryLinkedServiceSFTPCreateUpdate(d *pluginsdk.ResourceData, Properties: sftpLinkedService, } - if _, err := client.CreateOrUpdate(ctx, resourceGroup, dataFactoryName, name, linkedService, ""); err != nil { - return fmt.Errorf("creating/updating Data Factory Linked Service SFTP Anonymous %q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err) + if _, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.FactoryName, id.Name, linkedService, ""); err != nil { + return fmt.Errorf("creating/updating Data Factory SFTP Anonymous %s: %+v", id, err) } - resp, err := client.Get(ctx, resourceGroup, dataFactoryName, name, "") - if err != nil { - return fmt.Errorf("retrieving Data Factory Linked Service SFTP Anonymous %q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err) - } - - if resp.ID == nil { - return fmt.Errorf("Cannot read Data Factory Linked Service SFTP Anonymous %q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err) - } - - d.SetId(*resp.ID) + d.SetId(id.ID()) return resourceDataFactoryLinkedServiceSFTPRead(d, meta) } @@ -240,7 +232,7 @@ func resourceDataFactoryLinkedServiceSFTPRead(d *pluginsdk.ResourceData, meta in return nil } - return fmt.Errorf("retrieving Data Factory Linked Service SFTP %q (Data Factory %q / Resource Group %q): %+v", id.Name, id.FactoryName, id.ResourceGroup, err) + return fmt.Errorf("retrieving Data Factory SFTP %s: %+v", *id, err) } d.Set("name", resp.Name) @@ -302,7 +294,7 @@ func resourceDataFactoryLinkedServiceSFTPDelete(d *pluginsdk.ResourceData, meta response, err := client.Delete(ctx, id.ResourceGroup, id.FactoryName, id.Name) if err != nil { if !utils.ResponseWasNotFound(response) { - return fmt.Errorf("deleting Data Factory Linked Service SFTP %q (Data Factory %q / Resource Group %q): %+v", id.Name, id.FactoryName, id.ResourceGroup, err) + return fmt.Errorf("deleting Data Factory SFTP %s: %+v", *id, err) } } diff --git a/internal/services/datafactory/data_factory_linked_service_sftp_resource_test.go b/internal/services/datafactory/data_factory_linked_service_sftp_resource_test.go index 6d9dbd18da01..6f5d97296a54 100644 --- a/internal/services/datafactory/data_factory_linked_service_sftp_resource_test.go +++ b/internal/services/datafactory/data_factory_linked_service_sftp_resource_test.go @@ -5,10 +5,10 @@ import ( "fmt" "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/datafactory/parse" "github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" "github.com/hashicorp/terraform-provider-azurerm/utils" ) @@ -62,17 +62,14 @@ func TestAccDataFactoryLinkedServiceSFTP_update(t *testing.T) { } func (t LinkedServiceSFTPResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) { - id, err := azure.ParseAzureResourceID(state.ID) + id, err := parse.LinkedServiceID(state.ID) if err != nil { return nil, err } - resourceGroup := id.ResourceGroup - dataFactoryName := id.Path["factories"] - name := id.Path["linkedservices"] - resp, err := clients.DataFactory.LinkedServiceClient.Get(ctx, resourceGroup, dataFactoryName, name, "") + resp, err := clients.DataFactory.LinkedServiceClient.Get(ctx, id.ResourceGroup, id.FactoryName, id.Name, "") if err != nil { - return nil, fmt.Errorf("reading Data Factory Linked Service SFTP (%s): %+v", id, err) + return nil, fmt.Errorf("reading Data Factory SFTP (%s): %+v", *id, err) } return utils.Bool(resp.ID != nil), nil diff --git a/internal/services/datafactory/data_factory_linked_service_snowflake_resource.go b/internal/services/datafactory/data_factory_linked_service_snowflake_resource.go index 9d0a529a3185..628519917f2e 100644 --- a/internal/services/datafactory/data_factory_linked_service_snowflake_resource.go +++ b/internal/services/datafactory/data_factory_linked_service_snowflake_resource.go @@ -24,8 +24,10 @@ func resourceDataFactoryLinkedServiceSnowflake() *pluginsdk.Resource { Update: resourceDataFactoryLinkedServiceSnowflakeCreateUpdate, Delete: resourceDataFactoryLinkedServiceSnowflakeDelete, - // TODO: replace this with an importer which validates the ID during import - Importer: pluginsdk.DefaultImporter(), + Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error { + _, err := parse.LinkedServiceID(id) + return err + }), Timeouts: &pluginsdk.ResourceTimeout{ Create: pluginsdk.DefaultTimeout(30 * time.Minute), @@ -125,18 +127,17 @@ func resourceDataFactoryLinkedServiceSnowflake() *pluginsdk.Resource { func resourceDataFactoryLinkedServiceSnowflakeCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) error { client := meta.(*clients.Client).DataFactory.LinkedServiceClient + subscriptionId := meta.(*clients.Client).DataFactory.LinkedServiceClient.SubscriptionID ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d) defer cancel() - name := d.Get("name").(string) - dataFactoryName := d.Get("data_factory_name").(string) - resourceGroup := d.Get("resource_group_name").(string) + id := parse.NewLinkedServiceID(subscriptionId, d.Get("resource_group_name").(string), d.Get("data_factory_name").(string), d.Get("name").(string)) if d.IsNewResource() { - existing, err := client.Get(ctx, resourceGroup, dataFactoryName, name, "") + existing, err := client.Get(ctx, id.ResourceGroup, id.FactoryName, id.Name, "") if err != nil { if !utils.ResponseWasNotFound(existing.Response) { - return fmt.Errorf("checking for presence of existing Data Factory Linked Service Snowflake %q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err) + return fmt.Errorf("checking for presence of existing Data Factory Snowflake %s: %+v", id, err) } } @@ -177,20 +178,11 @@ func resourceDataFactoryLinkedServiceSnowflakeCreateUpdate(d *pluginsdk.Resource Properties: snowflakeLinkedService, } - if _, err := client.CreateOrUpdate(ctx, resourceGroup, dataFactoryName, name, linkedService, ""); err != nil { - return fmt.Errorf("creating/updating Data Factory Linked Service Snowflake %q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err) + if _, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.FactoryName, id.Name, linkedService, ""); err != nil { + return fmt.Errorf("creating/updating Data Factory Snowflake %s: %+v", id, err) } - resp, err := client.Get(ctx, resourceGroup, dataFactoryName, name, "") - if err != nil { - return fmt.Errorf("retrieving Data Factory Linked Service Snowflake %q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err) - } - - if resp.ID == nil { - return fmt.Errorf("Cannot read Data Factory Linked Service Snowflake %q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err) - } - - d.SetId(*resp.ID) + d.SetId(id.ID()) return resourceDataFactoryLinkedServiceSnowflakeRead(d, meta) } @@ -212,7 +204,7 @@ func resourceDataFactoryLinkedServiceSnowflakeRead(d *pluginsdk.ResourceData, me return nil } - return fmt.Errorf("retrieving Data Factory Linked Service Snowflake %q (Data Factory %q / Resource Group %q): %+v", id.Name, id.FactoryName, id.ResourceGroup, err) + return fmt.Errorf("retrieving Data Factory Snowflake %s: %+v", *id, err) } d.Set("name", id.Name) @@ -221,7 +213,7 @@ func resourceDataFactoryLinkedServiceSnowflakeRead(d *pluginsdk.ResourceData, me snowflake, ok := resp.Properties.AsSnowflakeLinkedService() if !ok { - return fmt.Errorf("classifying Data Factory Linked Service Snowflake %q (Data Factory %q / Resource Group %q): Expected: %q Received: %q", id.Name, id.FactoryName, id.ResourceGroup, datafactory.TypeBasicLinkedServiceTypeSnowflake, *resp.Type) + return fmt.Errorf("classifying Data Factory Snowflake %s: Expected: %q Received: %q", *id, datafactory.TypeBasicLinkedServiceTypeSnowflake, *resp.Type) } d.Set("additional_properties", snowflake.AdditionalProperties) @@ -278,7 +270,7 @@ func resourceDataFactoryLinkedServiceSnowflakeDelete(d *pluginsdk.ResourceData, response, err := client.Delete(ctx, id.ResourceGroup, id.FactoryName, id.Name) if err != nil { if !utils.ResponseWasNotFound(response) { - return fmt.Errorf("deleting Data Factory Linked Service Snowflake %q (Data Factory %q / Resource Group %q): %+v", id.Name, id.FactoryName, id.ResourceGroup, err) + return fmt.Errorf("deleting Data Factory Snowflake %s: %+v", *id, err) } } diff --git a/internal/services/datafactory/data_factory_linked_service_snowflake_resource_test.go b/internal/services/datafactory/data_factory_linked_service_snowflake_resource_test.go index 73d865980bb8..a31221e27fd1 100644 --- a/internal/services/datafactory/data_factory_linked_service_snowflake_resource_test.go +++ b/internal/services/datafactory/data_factory_linked_service_snowflake_resource_test.go @@ -5,10 +5,10 @@ import ( "fmt" "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/datafactory/parse" "github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" "github.com/hashicorp/terraform-provider-azurerm/utils" ) @@ -66,17 +66,14 @@ func TestAccDataFactoryLinkedServiceSnowflake_KeyVaultReference(t *testing.T) { } func (t LinkedServiceSnowflakeResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) { - id, err := azure.ParseAzureResourceID(state.ID) + id, err := parse.LinkedServiceID(state.ID) if err != nil { return nil, err } - resourceGroup := id.ResourceGroup - dataFactoryName := id.Path["factories"] - name := id.Path["linkedservices"] - resp, err := clients.DataFactory.LinkedServiceClient.Get(ctx, resourceGroup, dataFactoryName, name, "") + resp, err := clients.DataFactory.LinkedServiceClient.Get(ctx, id.ResourceGroup, id.FactoryName, id.Name, "") if err != nil { - return nil, fmt.Errorf("reading Data Factory Linked Service Snowflake(%s): %+v", id, err) + return nil, fmt.Errorf("reading Data Factory Snowflake(%s): %+v", *id, err) } return utils.Bool(resp.ID != nil), nil diff --git a/internal/services/datafactory/data_factory_linked_service_sql_server_resource.go b/internal/services/datafactory/data_factory_linked_service_sql_server_resource.go index 50c901f427e9..6a04d03e3210 100644 --- a/internal/services/datafactory/data_factory_linked_service_sql_server_resource.go +++ b/internal/services/datafactory/data_factory_linked_service_sql_server_resource.go @@ -23,8 +23,10 @@ func resourceDataFactoryLinkedServiceSQLServer() *pluginsdk.Resource { Update: resourceDataFactoryLinkedServiceSQLServerCreateUpdate, Delete: resourceDataFactoryLinkedServiceSQLServerDelete, - // TODO: replace this with an importer which validates the ID during import - Importer: pluginsdk.DefaultImporter(), + Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error { + _, err := parse.LinkedServiceID(id) + return err + }), Timeouts: &pluginsdk.ResourceTimeout{ Create: pluginsdk.DefaultTimeout(30 * time.Minute), @@ -147,18 +149,17 @@ func resourceDataFactoryLinkedServiceSQLServer() *pluginsdk.Resource { func resourceDataFactoryLinkedServiceSQLServerCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) error { client := meta.(*clients.Client).DataFactory.LinkedServiceClient + subscriptionId := meta.(*clients.Client).DataFactory.LinkedServiceClient.SubscriptionID ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d) defer cancel() - name := d.Get("name").(string) - dataFactoryName := d.Get("data_factory_name").(string) - resourceGroup := d.Get("resource_group_name").(string) + id := parse.NewLinkedServiceID(subscriptionId, d.Get("resource_group_name").(string), d.Get("data_factory_name").(string), d.Get("name").(string)) if d.IsNewResource() { - existing, err := client.Get(ctx, resourceGroup, dataFactoryName, name, "") + existing, err := client.Get(ctx, id.ResourceGroup, id.FactoryName, id.Name, "") if err != nil { if !utils.ResponseWasNotFound(existing.Response) { - return fmt.Errorf("checking for presence of existing Data Factory Linked Service SQL Server %q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err) + return fmt.Errorf("checking for presence of existing Data Factory SQL Server %s: %+v", id, err) } } @@ -206,20 +207,11 @@ func resourceDataFactoryLinkedServiceSQLServerCreateUpdate(d *pluginsdk.Resource Properties: sqlServerLinkedService, } - if _, err := client.CreateOrUpdate(ctx, resourceGroup, dataFactoryName, name, linkedService, ""); err != nil { - return fmt.Errorf("creating/updating Data Factory Linked Service SQL Server %q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err) + if _, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.FactoryName, id.Name, linkedService, ""); err != nil { + return fmt.Errorf("creating/updating Data Factory SQL Server %s: %+v", id, err) } - resp, err := client.Get(ctx, resourceGroup, dataFactoryName, name, "") - if err != nil { - return fmt.Errorf("retrieving Data Factory Linked Service SQL Server %q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err) - } - - if resp.ID == nil { - return fmt.Errorf("Cannot read Data Factory Linked Service SQL Server %q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err) - } - - d.SetId(*resp.ID) + d.SetId(id.ID()) return resourceDataFactoryLinkedServiceSQLServerRead(d, meta) } @@ -241,7 +233,7 @@ func resourceDataFactoryLinkedServiceSQLServerRead(d *pluginsdk.ResourceData, me return nil } - return fmt.Errorf("retrieving Data Factory Linked Service SQL Server %q (Data Factory %q / Resource Group %q): %+v", id.Name, id.FactoryName, id.ResourceGroup, err) + return fmt.Errorf("retrieving Data Factory SQL Server %s: %+v", *id, err) } d.Set("name", id.Name) @@ -250,7 +242,7 @@ func resourceDataFactoryLinkedServiceSQLServerRead(d *pluginsdk.ResourceData, me sqlServer, ok := resp.Properties.AsSQLServerLinkedService() if !ok { - return fmt.Errorf("classifying Data Factory Linked Service SQL Server %q (Data Factory %q / Resource Group %q): Expected: %q Received: %q", id.Name, id.FactoryName, id.ResourceGroup, datafactory.TypeBasicLinkedServiceTypeSQLServer, *resp.Type) + return fmt.Errorf("classifying Data Factory SQL Server %s: Expected: %q Received: %q", *id, datafactory.TypeBasicLinkedServiceTypeSQLServer, *resp.Type) } d.Set("additional_properties", sqlServer.AdditionalProperties) @@ -310,7 +302,7 @@ func resourceDataFactoryLinkedServiceSQLServerDelete(d *pluginsdk.ResourceData, response, err := client.Delete(ctx, id.ResourceGroup, id.FactoryName, id.Name) if err != nil { if !utils.ResponseWasNotFound(response) { - return fmt.Errorf("deleting Data Factory Linked Service SQL Server %q (Data Factory %q / Resource Group %q): %+v", id.Name, id.FactoryName, id.ResourceGroup, err) + return fmt.Errorf("deleting Data Factory SQL Server %s: %+v", *id, err) } } diff --git a/internal/services/datafactory/data_factory_linked_service_sql_server_resource_test.go b/internal/services/datafactory/data_factory_linked_service_sql_server_resource_test.go index 9f131c9ec0a8..ff4497f2f5bb 100644 --- a/internal/services/datafactory/data_factory_linked_service_sql_server_resource_test.go +++ b/internal/services/datafactory/data_factory_linked_service_sql_server_resource_test.go @@ -5,10 +5,10 @@ import ( "fmt" "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/datafactory/parse" "github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" "github.com/hashicorp/terraform-provider-azurerm/utils" ) @@ -85,17 +85,14 @@ func TestAccDataFactoryLinkedServiceSQLServer_ConnectionStringKeyVaultReference( } func (t LinkedServiceSQLServerResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) { - id, err := azure.ParseAzureResourceID(state.ID) + id, err := parse.LinkedServiceID(state.ID) if err != nil { return nil, err } - resourceGroup := id.ResourceGroup - dataFactoryName := id.Path["factories"] - name := id.Path["linkedservices"] - resp, err := clients.DataFactory.LinkedServiceClient.Get(ctx, resourceGroup, dataFactoryName, name, "") + resp, err := clients.DataFactory.LinkedServiceClient.Get(ctx, id.ResourceGroup, id.FactoryName, id.Name, "") if err != nil { - return nil, fmt.Errorf("reading Data Factory Linked Service SQL Server(%s): %+v", id, err) + return nil, fmt.Errorf("reading Data Factory SQL Server(%s): %+v", *id, err) } return utils.Bool(resp.ID != nil), nil diff --git a/internal/services/datafactory/data_factory_linked_service_synapse_resource.go b/internal/services/datafactory/data_factory_linked_service_synapse_resource.go index f08fce6affa0..8b75c81afbce 100644 --- a/internal/services/datafactory/data_factory_linked_service_synapse_resource.go +++ b/internal/services/datafactory/data_factory_linked_service_synapse_resource.go @@ -24,8 +24,10 @@ func resourceDataFactoryLinkedServiceSynapse() *pluginsdk.Resource { Update: resourceDataFactoryLinkedServiceSynapseCreateUpdate, Delete: resourceDataFactoryLinkedServiceSynapseDelete, - // TODO: replace this with an importer which validates the ID during import - Importer: pluginsdk.DefaultImporter(), + Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error { + _, err := parse.LinkedServiceID(id) + return err + }), Timeouts: &pluginsdk.ResourceTimeout{ Create: pluginsdk.DefaultTimeout(30 * time.Minute), @@ -125,18 +127,17 @@ func resourceDataFactoryLinkedServiceSynapse() *pluginsdk.Resource { func resourceDataFactoryLinkedServiceSynapseCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) error { client := meta.(*clients.Client).DataFactory.LinkedServiceClient + subscriptionId := meta.(*clients.Client).DataFactory.LinkedServiceClient.SubscriptionID ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d) defer cancel() - name := d.Get("name").(string) - dataFactoryName := d.Get("data_factory_name").(string) - resourceGroup := d.Get("resource_group_name").(string) + id := parse.NewLinkedServiceID(subscriptionId, d.Get("resource_group_name").(string), d.Get("data_factory_name").(string), d.Get("name").(string)) if d.IsNewResource() { - existing, err := client.Get(ctx, resourceGroup, dataFactoryName, name, "") + existing, err := client.Get(ctx, id.ResourceGroup, id.FactoryName, id.Name, "") if err != nil { if !utils.ResponseWasNotFound(existing.Response) { - return fmt.Errorf("checking for presence of existing Data Factory Linked Service Synapse%q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err) + return fmt.Errorf("checking for presence of existing Data Factory Synapse %s: %+v", id, err) } } @@ -177,20 +178,11 @@ func resourceDataFactoryLinkedServiceSynapseCreateUpdate(d *pluginsdk.ResourceDa Properties: sqlDWLinkedService, } - if _, err := client.CreateOrUpdate(ctx, resourceGroup, dataFactoryName, name, linkedService, ""); err != nil { - return fmt.Errorf("creating/updating Data Factory Linked Service Synapse %q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err) + if _, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.FactoryName, id.Name, linkedService, ""); err != nil { + return fmt.Errorf("creating/updating Data Factory Synapse %s: %+v", id, err) } - resp, err := client.Get(ctx, resourceGroup, dataFactoryName, name, "") - if err != nil { - return fmt.Errorf("retrieving Data Factory Linked Service Synapse %q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err) - } - - if resp.ID == nil { - return fmt.Errorf("Cannot read Data Factory Linked Service Synapse %q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err) - } - - d.SetId(*resp.ID) + d.SetId(id.ID()) return resourceDataFactoryLinkedServiceSynapseRead(d, meta) } @@ -212,7 +204,7 @@ func resourceDataFactoryLinkedServiceSynapseRead(d *pluginsdk.ResourceData, meta return nil } - return fmt.Errorf("retrieving Data Factory Linked Service Synapse %q (Data Factory %q / Resource Group %q): %+v", id.Name, id.FactoryName, id.ResourceGroup, err) + return fmt.Errorf("retrieving Data Factory Synapse %s: %+v", *id, err) } d.Set("name", id.Name) @@ -221,7 +213,7 @@ func resourceDataFactoryLinkedServiceSynapseRead(d *pluginsdk.ResourceData, meta sqlDW, ok := resp.Properties.AsAzureSQLDWLinkedService() if !ok { - return fmt.Errorf("classifying Data Factory Linked Service Synapse %q (Data Factory %q / Resource Group %q): Expected: %q Received: %q", id.Name, id.FactoryName, id.ResourceGroup, datafactory.TypeBasicLinkedServiceTypeAzureSQLDW, *resp.Type) + return fmt.Errorf("classifying Data Factory Synapse %s: Expected: %q Received: %q", *id, datafactory.TypeBasicLinkedServiceTypeAzureSQLDW, *resp.Type) } d.Set("additional_properties", sqlDW.AdditionalProperties) @@ -274,7 +266,7 @@ func resourceDataFactoryLinkedServiceSynapseDelete(d *pluginsdk.ResourceData, me response, err := client.Delete(ctx, id.ResourceGroup, id.FactoryName, id.Name) if err != nil { if !utils.ResponseWasNotFound(response) { - return fmt.Errorf("deleting Data Factory Linked Service Synapse %q (Data Factory %q / Resource Group %q): %+v", id.Name, id.FactoryName, id.ResourceGroup, err) + return fmt.Errorf("deleting Data Factory Synapse %s: %+v", *id, err) } } diff --git a/internal/services/datafactory/data_factory_linked_service_synapse_resource_test.go b/internal/services/datafactory/data_factory_linked_service_synapse_resource_test.go index 9f0a0d9c4980..d4b75f7d3d65 100644 --- a/internal/services/datafactory/data_factory_linked_service_synapse_resource_test.go +++ b/internal/services/datafactory/data_factory_linked_service_synapse_resource_test.go @@ -5,10 +5,10 @@ import ( "fmt" "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/datafactory/parse" "github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" "github.com/hashicorp/terraform-provider-azurerm/utils" ) @@ -59,17 +59,14 @@ func TestAccDataFactoryLinkedServiceSynapse_KeyVaultReference(t *testing.T) { } func (t LinkedServiceSynapseResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) { - id, err := azure.ParseAzureResourceID(state.ID) + id, err := parse.LinkedServiceID(state.ID) if err != nil { return nil, err } - resourceGroup := id.ResourceGroup - dataFactoryName := id.Path["factories"] - name := id.Path["linkedservices"] - resp, err := clients.DataFactory.LinkedServiceClient.Get(ctx, resourceGroup, dataFactoryName, name, "") + resp, err := clients.DataFactory.LinkedServiceClient.Get(ctx, id.ResourceGroup, id.FactoryName, id.Name, "") if err != nil { - return nil, fmt.Errorf("reading Data Factory Linked Service Synapse (%s): %+v", id, err) + return nil, fmt.Errorf("reading Data Factory Synapse (%s): %+v", *id, err) } return utils.Bool(resp.ID != nil), nil diff --git a/internal/services/datafactory/data_factory_linked_service_web_resource.go b/internal/services/datafactory/data_factory_linked_service_web_resource.go index 09b6c1e3430a..e3e597224222 100644 --- a/internal/services/datafactory/data_factory_linked_service_web_resource.go +++ b/internal/services/datafactory/data_factory_linked_service_web_resource.go @@ -23,8 +23,10 @@ func resourceDataFactoryLinkedServiceWeb() *pluginsdk.Resource { Update: resourceDataFactoryLinkedServiceWebCreateUpdate, Delete: resourceDataFactoryLinkedServiceWebDelete, - // TODO: replace this with an importer which validates the ID during import - Importer: pluginsdk.DefaultImporter(), + Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error { + _, err := parse.LinkedServiceID(id) + return err + }), Timeouts: &pluginsdk.ResourceTimeout{ Create: pluginsdk.DefaultTimeout(30 * time.Minute), @@ -118,18 +120,17 @@ func resourceDataFactoryLinkedServiceWeb() *pluginsdk.Resource { func resourceDataFactoryLinkedServiceWebCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) error { client := meta.(*clients.Client).DataFactory.LinkedServiceClient + subscriptionId := meta.(*clients.Client).DataFactory.LinkedServiceClient.SubscriptionID ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d) defer cancel() - name := d.Get("name").(string) - dataFactoryName := d.Get("data_factory_name").(string) - resourceGroup := d.Get("resource_group_name").(string) + id := parse.NewLinkedServiceID(subscriptionId, d.Get("resource_group_name").(string), d.Get("data_factory_name").(string), d.Get("name").(string)) if d.IsNewResource() { - existing, err := client.Get(ctx, resourceGroup, dataFactoryName, name, "") + existing, err := client.Get(ctx, id.ResourceGroup, id.FactoryName, id.Name, "") if err != nil { if !utils.ResponseWasNotFound(existing.Response) { - return fmt.Errorf("checking for presence of existing Data Factory Linked Service Web Anonymous %q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err) + return fmt.Errorf("checking for presence of existing Data Factory Web Anonymous %s: %+v", id, err) } } @@ -193,20 +194,11 @@ func resourceDataFactoryLinkedServiceWebCreateUpdate(d *pluginsdk.ResourceData, Properties: webLinkedService, } - if _, err := client.CreateOrUpdate(ctx, resourceGroup, dataFactoryName, name, linkedService, ""); err != nil { - return fmt.Errorf("creating/updating Data Factory Linked Service Web Anonymous %q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err) + if _, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.FactoryName, id.Name, linkedService, ""); err != nil { + return fmt.Errorf("creating/updating Data Factory Web Anonymous %s: %+v", id, err) } - resp, err := client.Get(ctx, resourceGroup, dataFactoryName, name, "") - if err != nil { - return fmt.Errorf("retrieving Data Factory Linked Service Web Anonymous %q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err) - } - - if resp.ID == nil { - return fmt.Errorf("Cannot read Data Factory Linked Service Web Anonymous %q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err) - } - - d.SetId(*resp.ID) + d.SetId(id.ID()) return resourceDataFactoryLinkedServiceWebRead(d, meta) } @@ -228,7 +220,7 @@ func resourceDataFactoryLinkedServiceWebRead(d *pluginsdk.ResourceData, meta int return nil } - return fmt.Errorf("retrieving Data Factory Linked Service Web %q (Data Factory %q / Resource Group %q): %+v", id.Name, id.FactoryName, id.ResourceGroup, err) + return fmt.Errorf("retrieving Data Factory Web %s: %+v", *id, err) } d.Set("name", resp.Name) @@ -237,7 +229,7 @@ func resourceDataFactoryLinkedServiceWebRead(d *pluginsdk.ResourceData, meta int web, ok := resp.Properties.AsWebLinkedService() if !ok { - return fmt.Errorf("classifying Data Factory Linked Service Web %q (Data Factory %q / Resource Group %q): Expected: %q Received: %q", id.Name, id.FactoryName, id.ResourceGroup, datafactory.TypeBasicLinkedServiceTypeWeb, *resp.Type) + return fmt.Errorf("classifying Data Factory Web %s: Expected: %q Received: %q", *id, datafactory.TypeBasicLinkedServiceTypeWeb, *resp.Type) } isWebPropertiesLoaded := false @@ -294,7 +286,7 @@ func resourceDataFactoryLinkedServiceWebDelete(d *pluginsdk.ResourceData, meta i response, err := client.Delete(ctx, id.ResourceGroup, id.FactoryName, id.Name) if err != nil { if !utils.ResponseWasNotFound(response) { - return fmt.Errorf("deleting Data Factory Linked Service Web %q (Data Factory %q / Resource Group %q): %+v", id.Name, id.FactoryName, id.ResourceGroup, err) + return fmt.Errorf("deleting Data Factory Web %s: %+v", *id, err) } } diff --git a/internal/services/datafactory/data_factory_linked_service_web_resource_test.go b/internal/services/datafactory/data_factory_linked_service_web_resource_test.go index f390e469c67f..d00a80b4e586 100644 --- a/internal/services/datafactory/data_factory_linked_service_web_resource_test.go +++ b/internal/services/datafactory/data_factory_linked_service_web_resource_test.go @@ -5,10 +5,10 @@ import ( "fmt" "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/datafactory/parse" "github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" "github.com/hashicorp/terraform-provider-azurerm/utils" ) @@ -85,17 +85,14 @@ func TestAccDataFactoryLinkedServiceWeb_update(t *testing.T) { } func (t LinkedServiceWebResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) { - id, err := azure.ParseAzureResourceID(state.ID) + id, err := parse.LinkedServiceID(state.ID) if err != nil { return nil, err } - resourceGroup := id.ResourceGroup - dataFactoryName := id.Path["factories"] - name := id.Path["linkedservices"] - resp, err := clients.DataFactory.LinkedServiceClient.Get(ctx, resourceGroup, dataFactoryName, name, "") + resp, err := clients.DataFactory.LinkedServiceClient.Get(ctx, id.ResourceGroup, id.FactoryName, id.Name, "") if err != nil { - return nil, fmt.Errorf("reading Data Factory Linked Service Web (%s): %+v", id, err) + return nil, fmt.Errorf("reading Data Factory Web (%s): %+v", *id, err) } return utils.Bool(resp.ID != nil), nil From aef215e65dd2e44b82ef3029ac35bfdecb2f2222 Mon Sep 17 00:00:00 2001 From: Steph Date: Tue, 5 Oct 2021 13:15:51 +0200 Subject: [PATCH 2/4] use custom importer --- ...ked_service_azure_blob_storage_resource.go | 4 +- ...inked_service_azure_databricks_resource.go | 4 +- ...ked_service_azure_file_storage_resource.go | 4 +- ..._linked_service_azure_function_resource.go | 4 +- ...ry_linked_service_azure_search_resource.go | 4 +- ...ked_service_azure_sql_database_resource.go | 4 +- ...ed_service_azure_table_storage_resource.go | 4 +- ...actory_linked_service_cosmosdb_resource.go | 4 +- ...service_data_lake_storage_gen2_resource.go | 4 +- ...ctory_linked_service_key_vault_resource.go | 4 +- ...a_factory_linked_service_kusto_resource.go | 4 +- ...a_factory_linked_service_mysql_resource.go | 4 +- ...a_factory_linked_service_odata_resource.go | 4 +- ...tory_linked_service_postgresql_resource.go | 4 +- ...ta_factory_linked_service_sftp_resource.go | 4 +- ...ctory_linked_service_snowflake_resource.go | 4 +- ...tory_linked_service_sql_server_resource.go | 4 +- ...factory_linked_service_synapse_resource.go | 4 +- ...ata_factory_linked_service_web_resource.go | 4 +- .../datafactory/datafactory_linked_service.go | 58 +++++++++++++++++++ 20 files changed, 96 insertions(+), 38 deletions(-) create mode 100644 internal/services/datafactory/datafactory_linked_service.go diff --git a/internal/services/datafactory/data_factory_linked_service_azure_blob_storage_resource.go b/internal/services/datafactory/data_factory_linked_service_azure_blob_storage_resource.go index 3366752b2cb7..a5a66273771e 100644 --- a/internal/services/datafactory/data_factory_linked_service_azure_blob_storage_resource.go +++ b/internal/services/datafactory/data_factory_linked_service_azure_blob_storage_resource.go @@ -23,10 +23,10 @@ func resourceDataFactoryLinkedServiceAzureBlobStorage() *pluginsdk.Resource { Update: resourceDataFactoryLinkedServiceBlobStorageCreateUpdate, Delete: resourceDataFactoryLinkedServiceBlobStorageDelete, - Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error { + Importer: pluginsdk.ImporterValidatingResourceIdThen(func(id string) error { _, err := parse.LinkedServiceID(id) return err - }), + }, importDataFactoryLinkedService(datafactory.TypeBasicLinkedServiceTypeAzureBlobStorage)), Timeouts: &pluginsdk.ResourceTimeout{ Create: pluginsdk.DefaultTimeout(30 * time.Minute), diff --git a/internal/services/datafactory/data_factory_linked_service_azure_databricks_resource.go b/internal/services/datafactory/data_factory_linked_service_azure_databricks_resource.go index 56c5ffaec8e7..aa4af0fcc2d1 100644 --- a/internal/services/datafactory/data_factory_linked_service_azure_databricks_resource.go +++ b/internal/services/datafactory/data_factory_linked_service_azure_databricks_resource.go @@ -26,10 +26,10 @@ func resourceDataFactoryLinkedServiceAzureDatabricks() *pluginsdk.Resource { Update: resourceDataFactoryLinkedServiceDatabricksCreateUpdate, Delete: resourceDataFactoryLinkedServiceDatabricksDelete, - Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error { + Importer: pluginsdk.ImporterValidatingResourceIdThen(func(id string) error { _, err := parse.LinkedServiceID(id) return err - }), + }, importDataFactoryLinkedService(datafactory.TypeBasicLinkedServiceTypeAzureDatabricks)), Timeouts: &pluginsdk.ResourceTimeout{ Create: pluginsdk.DefaultTimeout(30 * time.Minute), diff --git a/internal/services/datafactory/data_factory_linked_service_azure_file_storage_resource.go b/internal/services/datafactory/data_factory_linked_service_azure_file_storage_resource.go index 16e242db9f4d..c480fab76518 100644 --- a/internal/services/datafactory/data_factory_linked_service_azure_file_storage_resource.go +++ b/internal/services/datafactory/data_factory_linked_service_azure_file_storage_resource.go @@ -23,10 +23,10 @@ func resourceDataFactoryLinkedServiceAzureFileStorage() *pluginsdk.Resource { Update: resourceDataFactoryLinkedServiceAzureFileStorageCreateUpdate, Delete: resourceDataFactoryLinkedServiceAzureFileStorageDelete, - Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error { + Importer: pluginsdk.ImporterValidatingResourceIdThen(func(id string) error { _, err := parse.LinkedServiceID(id) return err - }), + }, importDataFactoryLinkedService(datafactory.TypeBasicLinkedServiceTypeAzureFileStorage)), Timeouts: &pluginsdk.ResourceTimeout{ Create: pluginsdk.DefaultTimeout(30 * time.Minute), diff --git a/internal/services/datafactory/data_factory_linked_service_azure_function_resource.go b/internal/services/datafactory/data_factory_linked_service_azure_function_resource.go index 8daab769ec2b..e2812bca50b4 100644 --- a/internal/services/datafactory/data_factory_linked_service_azure_function_resource.go +++ b/internal/services/datafactory/data_factory_linked_service_azure_function_resource.go @@ -24,10 +24,10 @@ func resourceDataFactoryLinkedServiceAzureFunction() *pluginsdk.Resource { Update: resourceDataFactoryLinkedServiceAzureFunctionCreateUpdate, Delete: resourceDataFactoryLinkedServiceAzureFunctionDelete, - Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error { + Importer: pluginsdk.ImporterValidatingResourceIdThen(func(id string) error { _, err := parse.LinkedServiceID(id) return err - }), + }, importDataFactoryLinkedService(datafactory.TypeBasicLinkedServiceTypeAzureFunction)), Timeouts: &pluginsdk.ResourceTimeout{ Create: pluginsdk.DefaultTimeout(30 * time.Minute), diff --git a/internal/services/datafactory/data_factory_linked_service_azure_search_resource.go b/internal/services/datafactory/data_factory_linked_service_azure_search_resource.go index 2b238bfc181a..421ad9af8f5d 100644 --- a/internal/services/datafactory/data_factory_linked_service_azure_search_resource.go +++ b/internal/services/datafactory/data_factory_linked_service_azure_search_resource.go @@ -22,10 +22,10 @@ func resourceDataFactoryLinkedServiceAzureSearch() *pluginsdk.Resource { Update: resourceDataFactoryLinkedServiceAzureSearchCreateUpdate, Delete: resourceDataFactoryLinkedServiceAzureSearchDelete, - Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error { + Importer: pluginsdk.ImporterValidatingResourceIdThen(func(id string) error { _, err := parse.LinkedServiceID(id) return err - }), + }, importDataFactoryLinkedService(datafactory.TypeBasicLinkedServiceTypeAzureSearch)), Timeouts: &pluginsdk.ResourceTimeout{ Create: pluginsdk.DefaultTimeout(30 * time.Minute), diff --git a/internal/services/datafactory/data_factory_linked_service_azure_sql_database_resource.go b/internal/services/datafactory/data_factory_linked_service_azure_sql_database_resource.go index f71821b9d142..bd4a63bc58d9 100644 --- a/internal/services/datafactory/data_factory_linked_service_azure_sql_database_resource.go +++ b/internal/services/datafactory/data_factory_linked_service_azure_sql_database_resource.go @@ -23,10 +23,10 @@ func resourceDataFactoryLinkedServiceAzureSQLDatabase() *pluginsdk.Resource { Update: resourceDataFactoryLinkedServiceAzureSQLDatabaseCreateUpdate, Delete: resourceDataFactoryLinkedServiceAzureSQLDatabaseDelete, - Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error { + Importer: pluginsdk.ImporterValidatingResourceIdThen(func(id string) error { _, err := parse.LinkedServiceID(id) return err - }), + }, importDataFactoryLinkedService(datafactory.TypeBasicLinkedServiceTypeAzureSQLDatabase)), Timeouts: &pluginsdk.ResourceTimeout{ Create: pluginsdk.DefaultTimeout(30 * time.Minute), diff --git a/internal/services/datafactory/data_factory_linked_service_azure_table_storage_resource.go b/internal/services/datafactory/data_factory_linked_service_azure_table_storage_resource.go index 771cec33ae1e..1e72df6fcd0d 100644 --- a/internal/services/datafactory/data_factory_linked_service_azure_table_storage_resource.go +++ b/internal/services/datafactory/data_factory_linked_service_azure_table_storage_resource.go @@ -23,10 +23,10 @@ func resourceDataFactoryLinkedServiceAzureTableStorage() *pluginsdk.Resource { Update: resourceDataFactoryLinkedServiceTableStorageCreateUpdate, Delete: resourceDataFactoryLinkedServiceTableStorageDelete, - Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error { + Importer: pluginsdk.ImporterValidatingResourceIdThen(func(id string) error { _, err := parse.LinkedServiceID(id) return err - }), + }, importDataFactoryLinkedService(datafactory.TypeBasicLinkedServiceTypeAzureTableStorage)), Timeouts: &pluginsdk.ResourceTimeout{ Create: pluginsdk.DefaultTimeout(30 * time.Minute), diff --git a/internal/services/datafactory/data_factory_linked_service_cosmosdb_resource.go b/internal/services/datafactory/data_factory_linked_service_cosmosdb_resource.go index 7c19e1f7cb00..8a591215f8e9 100644 --- a/internal/services/datafactory/data_factory_linked_service_cosmosdb_resource.go +++ b/internal/services/datafactory/data_factory_linked_service_cosmosdb_resource.go @@ -23,10 +23,10 @@ func resourceDataFactoryLinkedServiceCosmosDb() *pluginsdk.Resource { Update: resourceDataFactoryLinkedServiceCosmosDbCreateUpdate, Delete: resourceDataFactoryLinkedServiceCosmosDbDelete, - Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error { + Importer: pluginsdk.ImporterValidatingResourceIdThen(func(id string) error { _, err := parse.LinkedServiceID(id) return err - }), + }, importDataFactoryLinkedService(datafactory.TypeBasicLinkedServiceTypeCosmosDb)), Timeouts: &pluginsdk.ResourceTimeout{ Create: pluginsdk.DefaultTimeout(30 * time.Minute), diff --git a/internal/services/datafactory/data_factory_linked_service_data_lake_storage_gen2_resource.go b/internal/services/datafactory/data_factory_linked_service_data_lake_storage_gen2_resource.go index b822b73645cf..1dea204b582c 100644 --- a/internal/services/datafactory/data_factory_linked_service_data_lake_storage_gen2_resource.go +++ b/internal/services/datafactory/data_factory_linked_service_data_lake_storage_gen2_resource.go @@ -23,10 +23,10 @@ func resourceDataFactoryLinkedServiceDataLakeStorageGen2() *pluginsdk.Resource { Update: resourceDataFactoryLinkedServiceDataLakeStorageGen2CreateUpdate, Delete: resourceDataFactoryLinkedServiceDataLakeStorageGen2Delete, - Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error { + Importer: pluginsdk.ImporterValidatingResourceIdThen(func(id string) error { _, err := parse.LinkedServiceID(id) return err - }), + }, importDataFactoryLinkedService(datafactory.TypeBasicLinkedServiceTypeAzureBlobFS)), Timeouts: &pluginsdk.ResourceTimeout{ Create: pluginsdk.DefaultTimeout(30 * time.Minute), diff --git a/internal/services/datafactory/data_factory_linked_service_key_vault_resource.go b/internal/services/datafactory/data_factory_linked_service_key_vault_resource.go index 5bff3da933d1..20c092bb0ba2 100644 --- a/internal/services/datafactory/data_factory_linked_service_key_vault_resource.go +++ b/internal/services/datafactory/data_factory_linked_service_key_vault_resource.go @@ -25,10 +25,10 @@ func resourceDataFactoryLinkedServiceKeyVault() *pluginsdk.Resource { Update: resourceDataFactoryLinkedServiceKeyVaultCreateUpdate, Delete: resourceDataFactoryLinkedServiceKeyVaultDelete, - Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error { + Importer: pluginsdk.ImporterValidatingResourceIdThen(func(id string) error { _, err := parse.LinkedServiceID(id) return err - }), + }, importDataFactoryLinkedService(datafactory.TypeBasicLinkedServiceTypeAzureKeyVault)), Timeouts: &pluginsdk.ResourceTimeout{ Create: pluginsdk.DefaultTimeout(30 * time.Minute), diff --git a/internal/services/datafactory/data_factory_linked_service_kusto_resource.go b/internal/services/datafactory/data_factory_linked_service_kusto_resource.go index 3c07bc8362d4..b9379bc6f363 100644 --- a/internal/services/datafactory/data_factory_linked_service_kusto_resource.go +++ b/internal/services/datafactory/data_factory_linked_service_kusto_resource.go @@ -22,10 +22,10 @@ func resourceDataFactoryLinkedServiceKusto() *pluginsdk.Resource { Update: resourceDataFactoryLinkedServiceKustoCreateUpdate, Delete: resourceDataFactoryLinkedServiceKustoDelete, - Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error { + Importer: pluginsdk.ImporterValidatingResourceIdThen(func(id string) error { _, err := parse.LinkedServiceID(id) return err - }), + }, importDataFactoryLinkedService(datafactory.TypeBasicLinkedServiceTypeAzureDataExplorer)), Timeouts: &pluginsdk.ResourceTimeout{ Create: pluginsdk.DefaultTimeout(30 * time.Minute), diff --git a/internal/services/datafactory/data_factory_linked_service_mysql_resource.go b/internal/services/datafactory/data_factory_linked_service_mysql_resource.go index 340e7853e874..1ab4b2579de6 100644 --- a/internal/services/datafactory/data_factory_linked_service_mysql_resource.go +++ b/internal/services/datafactory/data_factory_linked_service_mysql_resource.go @@ -23,10 +23,10 @@ func resourceDataFactoryLinkedServiceMySQL() *pluginsdk.Resource { Update: resourceDataFactoryLinkedServiceMySQLCreateUpdate, Delete: resourceDataFactoryLinkedServiceMySQLDelete, - Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error { + Importer: pluginsdk.ImporterValidatingResourceIdThen(func(id string) error { _, err := parse.LinkedServiceID(id) return err - }), + }, importDataFactoryLinkedService(datafactory.TypeBasicLinkedServiceTypeMySQL)), Timeouts: &pluginsdk.ResourceTimeout{ Create: pluginsdk.DefaultTimeout(30 * time.Minute), diff --git a/internal/services/datafactory/data_factory_linked_service_odata_resource.go b/internal/services/datafactory/data_factory_linked_service_odata_resource.go index 9821e125fce6..56d2517526d7 100644 --- a/internal/services/datafactory/data_factory_linked_service_odata_resource.go +++ b/internal/services/datafactory/data_factory_linked_service_odata_resource.go @@ -23,10 +23,10 @@ func resourceArmDataFactoryLinkedServiceOData() *pluginsdk.Resource { Update: resourceArmDataFactoryLinkedServiceODataCreateUpdate, Delete: resourceArmDataFactoryLinkedServiceODataDelete, - Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error { + Importer: pluginsdk.ImporterValidatingResourceIdThen(func(id string) error { _, err := parse.LinkedServiceID(id) return err - }), + }, importDataFactoryLinkedService(datafactory.TypeBasicLinkedServiceTypeOData)), Timeouts: &pluginsdk.ResourceTimeout{ Create: pluginsdk.DefaultTimeout(30 * time.Minute), diff --git a/internal/services/datafactory/data_factory_linked_service_postgresql_resource.go b/internal/services/datafactory/data_factory_linked_service_postgresql_resource.go index dce1d06400d0..32bd2354c821 100644 --- a/internal/services/datafactory/data_factory_linked_service_postgresql_resource.go +++ b/internal/services/datafactory/data_factory_linked_service_postgresql_resource.go @@ -23,10 +23,10 @@ func resourceDataFactoryLinkedServicePostgreSQL() *pluginsdk.Resource { Update: resourceDataFactoryLinkedServicePostgreSQLCreateUpdate, Delete: resourceDataFactoryLinkedServicePostgreSQLDelete, - Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error { + Importer: pluginsdk.ImporterValidatingResourceIdThen(func(id string) error { _, err := parse.LinkedServiceID(id) return err - }), + }, importDataFactoryLinkedService(datafactory.TypeBasicLinkedServiceTypePostgreSQL)), Timeouts: &pluginsdk.ResourceTimeout{ Create: pluginsdk.DefaultTimeout(30 * time.Minute), diff --git a/internal/services/datafactory/data_factory_linked_service_sftp_resource.go b/internal/services/datafactory/data_factory_linked_service_sftp_resource.go index 768c79bbd6ea..00a232355104 100644 --- a/internal/services/datafactory/data_factory_linked_service_sftp_resource.go +++ b/internal/services/datafactory/data_factory_linked_service_sftp_resource.go @@ -23,10 +23,10 @@ func resourceDataFactoryLinkedServiceSFTP() *pluginsdk.Resource { Update: resourceDataFactoryLinkedServiceSFTPCreateUpdate, Delete: resourceDataFactoryLinkedServiceSFTPDelete, - Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error { + Importer: pluginsdk.ImporterValidatingResourceIdThen(func(id string) error { _, err := parse.LinkedServiceID(id) return err - }), + }, importDataFactoryLinkedService(datafactory.TypeBasicLinkedServiceTypeSftp)), Timeouts: &pluginsdk.ResourceTimeout{ Create: pluginsdk.DefaultTimeout(30 * time.Minute), diff --git a/internal/services/datafactory/data_factory_linked_service_snowflake_resource.go b/internal/services/datafactory/data_factory_linked_service_snowflake_resource.go index 628519917f2e..2c7122dcf106 100644 --- a/internal/services/datafactory/data_factory_linked_service_snowflake_resource.go +++ b/internal/services/datafactory/data_factory_linked_service_snowflake_resource.go @@ -24,10 +24,10 @@ func resourceDataFactoryLinkedServiceSnowflake() *pluginsdk.Resource { Update: resourceDataFactoryLinkedServiceSnowflakeCreateUpdate, Delete: resourceDataFactoryLinkedServiceSnowflakeDelete, - Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error { + Importer: pluginsdk.ImporterValidatingResourceIdThen(func(id string) error { _, err := parse.LinkedServiceID(id) return err - }), + }, importDataFactoryLinkedService(datafactory.TypeBasicLinkedServiceTypeSnowflake)), Timeouts: &pluginsdk.ResourceTimeout{ Create: pluginsdk.DefaultTimeout(30 * time.Minute), diff --git a/internal/services/datafactory/data_factory_linked_service_sql_server_resource.go b/internal/services/datafactory/data_factory_linked_service_sql_server_resource.go index 6a04d03e3210..8d0ced69eb32 100644 --- a/internal/services/datafactory/data_factory_linked_service_sql_server_resource.go +++ b/internal/services/datafactory/data_factory_linked_service_sql_server_resource.go @@ -23,10 +23,10 @@ func resourceDataFactoryLinkedServiceSQLServer() *pluginsdk.Resource { Update: resourceDataFactoryLinkedServiceSQLServerCreateUpdate, Delete: resourceDataFactoryLinkedServiceSQLServerDelete, - Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error { + Importer: pluginsdk.ImporterValidatingResourceIdThen(func(id string) error { _, err := parse.LinkedServiceID(id) return err - }), + }, importDataFactoryLinkedService(datafactory.TypeBasicLinkedServiceTypeSQLServer)), Timeouts: &pluginsdk.ResourceTimeout{ Create: pluginsdk.DefaultTimeout(30 * time.Minute), diff --git a/internal/services/datafactory/data_factory_linked_service_synapse_resource.go b/internal/services/datafactory/data_factory_linked_service_synapse_resource.go index 8b75c81afbce..691fa50db8c6 100644 --- a/internal/services/datafactory/data_factory_linked_service_synapse_resource.go +++ b/internal/services/datafactory/data_factory_linked_service_synapse_resource.go @@ -24,10 +24,10 @@ func resourceDataFactoryLinkedServiceSynapse() *pluginsdk.Resource { Update: resourceDataFactoryLinkedServiceSynapseCreateUpdate, Delete: resourceDataFactoryLinkedServiceSynapseDelete, - Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error { + Importer: pluginsdk.ImporterValidatingResourceIdThen(func(id string) error { _, err := parse.LinkedServiceID(id) return err - }), + }, importDataFactoryLinkedService(datafactory.TypeBasicLinkedServiceTypeAzureSQLDW)), Timeouts: &pluginsdk.ResourceTimeout{ Create: pluginsdk.DefaultTimeout(30 * time.Minute), diff --git a/internal/services/datafactory/data_factory_linked_service_web_resource.go b/internal/services/datafactory/data_factory_linked_service_web_resource.go index e3e597224222..a215e5869922 100644 --- a/internal/services/datafactory/data_factory_linked_service_web_resource.go +++ b/internal/services/datafactory/data_factory_linked_service_web_resource.go @@ -23,10 +23,10 @@ func resourceDataFactoryLinkedServiceWeb() *pluginsdk.Resource { Update: resourceDataFactoryLinkedServiceWebCreateUpdate, Delete: resourceDataFactoryLinkedServiceWebDelete, - Importer: pluginsdk.ImporterValidatingResourceId(func(id string) error { + Importer: pluginsdk.ImporterValidatingResourceIdThen(func(id string) error { _, err := parse.LinkedServiceID(id) return err - }), + }, importDataFactoryLinkedService(datafactory.TypeBasicLinkedServiceTypeWeb)), Timeouts: &pluginsdk.ResourceTimeout{ Create: pluginsdk.DefaultTimeout(30 * time.Minute), diff --git a/internal/services/datafactory/datafactory_linked_service.go b/internal/services/datafactory/datafactory_linked_service.go new file mode 100644 index 000000000000..49975d64d84a --- /dev/null +++ b/internal/services/datafactory/datafactory_linked_service.go @@ -0,0 +1,58 @@ +package datafactory + +import ( + "context" + "encoding/json" + "fmt" + "log" + + "github.com/Azure/azure-sdk-for-go/services/datafactory/mgmt/2018-06-01/datafactory" + "github.com/hashicorp/terraform-provider-azurerm/internal/clients" + "github.com/hashicorp/terraform-provider-azurerm/internal/services/datafactory/parse" + "github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" +) + +func importDataFactoryLinkedService(expectType datafactory.TypeBasicLinkedService) pluginsdk.ImporterFunc { + return func(ctx context.Context, d *pluginsdk.ResourceData, meta interface{}) (data []*pluginsdk.ResourceData, err error) { + id, err := parse.LinkedServiceID(d.Id()) + if err != nil { + return nil, err + } + + client := meta.(*clients.Client).DataFactory.LinkedServiceClient + resp, err := client.Get(ctx, id.ResourceGroup, id.FactoryName, id.Name, "") + if err != nil { + return nil, fmt.Errorf("retrieving Data Factory %s: %+v", *id, err) + } + + byteArr, err := json.Marshal(resp.Properties) + if err != nil { + return nil, err + } + + var m map[string]*json.RawMessage + if err = json.Unmarshal(byteArr, &m); err != nil { + return nil, err + } + + t := "" + if v, ok := m["type"]; ok && v != nil { + if err := json.Unmarshal(*v, &t); err != nil { + return nil, err + } + delete(m, "type") + } + + if datafactory.TypeBasicLinkedService(t) != expectType { + return nil, fmt.Errorf("data factory linked service has mismatched type, expected: %q, got %q", expectType, t) + } + log.Printf("[DEBUG] ls_importer type is: %s", t) + log.Printf("[DEBUG] ls_importer expectType is: %s", expectType) + + if datafactory.TypeBasicLinkedService(t) != expectType { + return nil, fmt.Errorf("data factory linked service has mismatched type, expected: %q, got %q", expectType, t) + } + + return []*pluginsdk.ResourceData{d}, nil + } +} From 64cb2ded04e7ced06503e5e82de7c76ef8700027 Mon Sep 17 00:00:00 2001 From: Steph Date: Tue, 5 Oct 2021 14:10:21 +0200 Subject: [PATCH 3/4] rename file and remove debug prints --- ...ctory_linked_service.go => data_factory_linked_service.go} | 4 ---- 1 file changed, 4 deletions(-) rename internal/services/datafactory/{datafactory_linked_service.go => data_factory_linked_service.go} (93%) diff --git a/internal/services/datafactory/datafactory_linked_service.go b/internal/services/datafactory/data_factory_linked_service.go similarity index 93% rename from internal/services/datafactory/datafactory_linked_service.go rename to internal/services/datafactory/data_factory_linked_service.go index 49975d64d84a..0f419232d8a6 100644 --- a/internal/services/datafactory/datafactory_linked_service.go +++ b/internal/services/datafactory/data_factory_linked_service.go @@ -4,8 +4,6 @@ import ( "context" "encoding/json" "fmt" - "log" - "github.com/Azure/azure-sdk-for-go/services/datafactory/mgmt/2018-06-01/datafactory" "github.com/hashicorp/terraform-provider-azurerm/internal/clients" "github.com/hashicorp/terraform-provider-azurerm/internal/services/datafactory/parse" @@ -46,8 +44,6 @@ func importDataFactoryLinkedService(expectType datafactory.TypeBasicLinkedServic if datafactory.TypeBasicLinkedService(t) != expectType { return nil, fmt.Errorf("data factory linked service has mismatched type, expected: %q, got %q", expectType, t) } - log.Printf("[DEBUG] ls_importer type is: %s", t) - log.Printf("[DEBUG] ls_importer expectType is: %s", expectType) if datafactory.TypeBasicLinkedService(t) != expectType { return nil, fmt.Errorf("data factory linked service has mismatched type, expected: %q, got %q", expectType, t) From fd8ba4090a303abff87861a3d81c46c4609b096f Mon Sep 17 00:00:00 2001 From: Steph Date: Tue, 5 Oct 2021 14:49:34 +0200 Subject: [PATCH 4/4] fix lint --- internal/services/datafactory/data_factory_linked_service.go | 1 + 1 file changed, 1 insertion(+) diff --git a/internal/services/datafactory/data_factory_linked_service.go b/internal/services/datafactory/data_factory_linked_service.go index 0f419232d8a6..07bdc89498b1 100644 --- a/internal/services/datafactory/data_factory_linked_service.go +++ b/internal/services/datafactory/data_factory_linked_service.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "github.com/Azure/azure-sdk-for-go/services/datafactory/mgmt/2018-06-01/datafactory" "github.com/hashicorp/terraform-provider-azurerm/internal/clients" "github.com/hashicorp/terraform-provider-azurerm/internal/services/datafactory/parse"