Skip to content

Commit

Permalink
refactor: use parse.LinkedServiceID (#11740)
Browse files Browse the repository at this point in the history
Co-authored-by: Hong Viet Le <[email protected]>
  • Loading branch information
pomverte and pomverte authored May 16, 2021
1 parent 9d92f91 commit 1003f42
Show file tree
Hide file tree
Showing 12 changed files with 124 additions and 184 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/datafactory/parse"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/datafactory/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tf/pluginsdk"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
Expand Down Expand Up @@ -251,31 +252,28 @@ func resourceDataFactoryLinkedServiceBlobStorageRead(d *schema.ResourceData, met
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := azure.ParseAzureResourceID(d.Id())
id, err := parse.LinkedServiceID(d.Id())
if err != nil {
return err
}
resourceGroup := id.ResourceGroup
dataFactoryName := id.Path["factories"]
name := id.Path["linkedservices"]

resp, err := client.Get(ctx, resourceGroup, dataFactoryName, name, "")
resp, err := client.Get(ctx, id.ResourceGroup, id.FactoryName, id.Name, "")
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
d.SetId("")
return nil
}

return fmt.Errorf("Error retrieving Data Factory Linked Service BlobStorage %q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err)
return fmt.Errorf("Error retrieving Data Factory Linked Service BlobStorage %q (Data Factory %q / Resource Group %q): %+v", id.Name, id.FactoryName, id.ResourceGroup, err)
}

d.Set("name", resp.Name)
d.Set("resource_group_name", resourceGroup)
d.Set("data_factory_name", dataFactoryName)
d.Set("resource_group_name", id.ResourceGroup)
d.Set("data_factory_name", id.FactoryName)

blobStorage, ok := resp.Properties.AsAzureBlobStorageLinkedService()
if !ok {
return fmt.Errorf("Error classifiying Data Factory Linked Service BlobStorage %q (Data Factory %q / Resource Group %q): Expected: %q Received: %q", name, dataFactoryName, resourceGroup, datafactory.TypeBasicLinkedServiceTypeAzureBlobStorage, *resp.Type)
return fmt.Errorf("Error classifiying 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)
}

if blobStorage != nil {
Expand All @@ -297,7 +295,7 @@ func resourceDataFactoryLinkedServiceBlobStorageRead(d *schema.ResourceData, met

annotations := flattenDataFactoryAnnotations(blobStorage.Annotations)
if err := d.Set("annotations", annotations); err != nil {
return fmt.Errorf("Error setting `annotations` for Data Factory Linked Service Azure Blob Storage %q (Data Factory %q) / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err)
return fmt.Errorf("Error 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)
}

parameters := flattenDataFactoryParameters(blobStorage.Parameters)
Expand All @@ -319,18 +317,15 @@ func resourceDataFactoryLinkedServiceBlobStorageDelete(d *schema.ResourceData, m
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := azure.ParseAzureResourceID(d.Id())
id, err := parse.LinkedServiceID(d.Id())
if err != nil {
return err
}
resourceGroup := id.ResourceGroup
dataFactoryName := id.Path["factories"]
name := id.Path["linkedservices"]

response, err := client.Delete(ctx, resourceGroup, dataFactoryName, name)
response, err := client.Delete(ctx, id.ResourceGroup, id.FactoryName, id.Name)
if err != nil {
if !utils.ResponseWasNotFound(response) {
return fmt.Errorf("Error deleting Data Factory Linked Service BlobStorage %q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err)
return fmt.Errorf("Error deleting Data Factory Linked Service BlobStorage %q (Data Factory %q / Resource Group %q): %+v", id.Name, id.FactoryName, id.ResourceGroup, err)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
databricksValidator "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/databricks/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/datafactory/parse"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/datafactory/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tf/pluginsdk"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
Expand Down Expand Up @@ -432,31 +433,28 @@ func resourceDataFactoryLinkedServiceDatabricksRead(d *schema.ResourceData, meta
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := azure.ParseAzureResourceID(d.Id())
id, err := parse.LinkedServiceID(d.Id())
if err != nil {
return err
}
resourceGroup := id.ResourceGroup
dataFactoryName := id.Path["factories"]
name := id.Path["linkedservices"]

resp, err := client.Get(ctx, resourceGroup, dataFactoryName, name, "")
resp, err := client.Get(ctx, id.ResourceGroup, id.FactoryName, id.Name, "")
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
d.SetId("")
return nil
}

return fmt.Errorf("Error retrieving Data Factory Linked Service Databricks %q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err)
return fmt.Errorf("Error retrieving Data Factory Linked Service Databricks %q (Data Factory %q / Resource Group %q): %+v", id.Name, id.FactoryName, id.ResourceGroup, err)
}

d.Set("name", resp.Name)
d.Set("resource_group_name", resourceGroup)
d.Set("data_factory_name", dataFactoryName)
d.Set("resource_group_name", id.ResourceGroup)
d.Set("data_factory_name", id.FactoryName)

databricks, ok := resp.Properties.AsAzureDatabricksLinkedService()
if !ok {
return fmt.Errorf("classifiying Data Factory Linked Service Databricks %q (Data Factory %q / Resource Group %q): Expected: %q Received: %q", name, dataFactoryName, resourceGroup, datafactory.TypeBasicLinkedServiceTypeAzureDatabricks, *resp.Type)
return fmt.Errorf("classifiying 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)
}

// Check the properties and verify if authentication is set to MSI
Expand Down Expand Up @@ -581,18 +579,15 @@ func resourceDataFactoryLinkedServiceDatabricksDelete(d *schema.ResourceData, me
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := azure.ParseAzureResourceID(d.Id())
id, err := parse.LinkedServiceID(d.Id())
if err != nil {
return err
}
resourceGroup := id.ResourceGroup
dataFactoryName := id.Path["factories"]
name := id.Path["linkedservices"]

response, err := client.Delete(ctx, resourceGroup, dataFactoryName, name)
response, err := client.Delete(ctx, id.ResourceGroup, id.FactoryName, id.Name)
if err != nil {
if !utils.ResponseWasNotFound(response) {
return fmt.Errorf("Error deleting Data Factory Linked Service Databricks %q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err)
return fmt.Errorf("Error deleting Data Factory Linked Service Databricks %q (Data Factory %q / Resource Group %q): %+v", id.Name, id.FactoryName, id.ResourceGroup, err)
}
}
return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/datafactory/parse"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/datafactory/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tf/pluginsdk"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
Expand Down Expand Up @@ -239,31 +240,28 @@ func resourceDataFactoryLinkedServiceAzureFileStorageRead(d *schema.ResourceData
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := azure.ParseAzureResourceID(d.Id())
id, err := parse.LinkedServiceID(d.Id())
if err != nil {
return err
}
resourceGroup := id.ResourceGroup
dataFactoryName := id.Path["factories"]
name := id.Path["linkedservices"]

resp, err := client.Get(ctx, resourceGroup, dataFactoryName, name, "")
resp, err := client.Get(ctx, id.ResourceGroup, id.FactoryName, id.Name, "")
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
d.SetId("")
return nil
}

return fmt.Errorf("Error retrieving Data Factory Linked Service Azure File Storage %q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err)
return fmt.Errorf("Error retrieving Data Factory Linked Service Azure File Storage %q (Data Factory %q / Resource Group %q): %+v", id.Name, id.FactoryName, id.ResourceGroup, err)
}

d.Set("name", resp.Name)
d.Set("resource_group_name", resourceGroup)
d.Set("data_factory_name", dataFactoryName)
d.Set("resource_group_name", id.ResourceGroup)
d.Set("data_factory_name", id.FactoryName)

fileStorage, ok := resp.Properties.AsAzureFileStorageLinkedService()
if !ok {
return fmt.Errorf("Error classifiying Data Factory Linked Service Azure File Storage %q (Data Factory %q / Resource Group %q): Expected: %q Received: %q", name, dataFactoryName, resourceGroup, datafactory.TypeBasicLinkedServiceTypeAzureFileStorage, *resp.Type)
return fmt.Errorf("Error classifiying 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)
}

d.Set("additional_properties", fileStorage.AdditionalProperties)
Expand All @@ -279,7 +277,7 @@ func resourceDataFactoryLinkedServiceAzureFileStorageRead(d *schema.ResourceData

annotations := flattenDataFactoryAnnotations(fileStorage.Annotations)
if err := d.Set("annotations", annotations); err != nil {
return fmt.Errorf("Error setting `annotations` for Data Factory Linked Service Azure File Storage %q (Data Factory %q) / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err)
return fmt.Errorf("Error 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)
}

parameters := flattenDataFactoryParameters(fileStorage.Parameters)
Expand Down Expand Up @@ -307,18 +305,15 @@ func resourceDataFactoryLinkedServiceAzureFileStorageDelete(d *schema.ResourceDa
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := azure.ParseAzureResourceID(d.Id())
id, err := parse.LinkedServiceID(d.Id())
if err != nil {
return err
}
resourceGroup := id.ResourceGroup
dataFactoryName := id.Path["factories"]
name := id.Path["linkedservices"]

response, err := client.Delete(ctx, resourceGroup, dataFactoryName, name)
response, err := client.Delete(ctx, id.ResourceGroup, id.FactoryName, id.Name)
if err != nil {
if !utils.ResponseWasNotFound(response) {
return fmt.Errorf("Error deleting Data Factory Linked Service Azure File Storage %q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err)
return fmt.Errorf("Error deleting Data Factory Linked Service Azure File Storage %q (Data Factory %q / Resource Group %q): %+v", id.Name, id.FactoryName, id.ResourceGroup, err)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/datafactory/parse"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/datafactory/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tf/pluginsdk"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
Expand Down Expand Up @@ -182,31 +183,28 @@ func resourceDataFactoryLinkedServiceAzureFunctionRead(d *schema.ResourceData, m
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := azure.ParseAzureResourceID(d.Id())
id, err := parse.LinkedServiceID(d.Id())
if err != nil {
return err
}
resourceGroup := id.ResourceGroup
dataFactoryName := id.Path["factories"]
name := id.Path["linkedservices"]

resp, err := client.Get(ctx, resourceGroup, dataFactoryName, name, "")
resp, err := client.Get(ctx, id.ResourceGroup, id.FactoryName, id.Name, "")
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
d.SetId("")
return nil
}

return fmt.Errorf("Error retrieving Data Factory Linked Service Azure Function %q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err)
return fmt.Errorf("Error retrieving Data Factory Linked Service Azure Function %q (Data Factory %q / Resource Group %q): %+v", id.Name, id.FactoryName, id.ResourceGroup, err)
}

d.Set("name", resp.Name)
d.Set("resource_group_name", resourceGroup)
d.Set("data_factory_name", dataFactoryName)
d.Set("resource_group_name", id.ResourceGroup)
d.Set("data_factory_name", id.FactoryName)

azureFunction, ok := resp.Properties.AsAzureFunctionLinkedService()
if !ok {
return fmt.Errorf("Error classifiying Data Factory Linked Service Azure Function %q (Data Factory %q / Resource Group %q): Expected: %q Received: %q", name, dataFactoryName, resourceGroup, datafactory.TypeBasicLinkedServiceTypeAzureFunction, *resp.Type)
return fmt.Errorf("Error classifiying Data Factory Linked Service Azure Function %q (Data Factory %q / Resource Group %q): Expected: %q Received: %q", id.Name, id.FactoryName, id.ResourceGroup, datafactory.TypeBasicLinkedServiceTypeAzureFunction, *resp.Type)
}

d.Set("url", azureFunction.AzureFunctionLinkedServiceTypeProperties.FunctionAppURL)
Expand All @@ -216,7 +214,7 @@ func resourceDataFactoryLinkedServiceAzureFunctionRead(d *schema.ResourceData, m

annotations := flattenDataFactoryAnnotations(azureFunction.Annotations)
if err := d.Set("annotations", annotations); err != nil {
return fmt.Errorf("Error setting `annotations` for Data Factory Linked Service Azure Function %q (Data Factory %q) / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err)
return fmt.Errorf("Error setting `annotations` for Data Factory Linked Service Azure Function %q (Data Factory %q) / Resource Group %q): %+v", id.Name, id.FactoryName, id.ResourceGroup, err)
}

parameters := flattenDataFactoryParameters(azureFunction.Parameters)
Expand All @@ -238,18 +236,15 @@ func resourceDataFactoryLinkedServiceAzureFunctionDelete(d *schema.ResourceData,
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := azure.ParseAzureResourceID(d.Id())
id, err := parse.LinkedServiceID(d.Id())
if err != nil {
return err
}
resourceGroup := id.ResourceGroup
dataFactoryName := id.Path["factories"]
name := id.Path["linkedservices"]

response, err := client.Delete(ctx, resourceGroup, dataFactoryName, name)
response, err := client.Delete(ctx, id.ResourceGroup, id.FactoryName, id.Name)
if err != nil {
if !utils.ResponseWasNotFound(response) {
return fmt.Errorf("Error deleting Data Factory Linked Service Azure Function %q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err)
return fmt.Errorf("Error deleting Data Factory Linked Service Azure Function %q (Data Factory %q / Resource Group %q): %+v", id.Name, id.FactoryName, id.ResourceGroup, err)
}
}

Expand Down
Loading

0 comments on commit 1003f42

Please sign in to comment.