Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Refactor azurerm_data_factory_linked_service_* to use id parser #13602

Merged
merged 4 commits into from
Oct 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions internal/services/datafactory/data_factory_linked_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package datafactory

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"
"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)
}

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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.ImporterValidatingResourceIdThen(func(id string) error {
_, err := parse.LinkedServiceID(id)
return err
stephybun marked this conversation as resolved.
Show resolved Hide resolved
}, importDataFactoryLinkedService(datafactory.TypeBasicLinkedServiceTypeAzureBlobStorage)),

Timeouts: &pluginsdk.ResourceTimeout{
Create: pluginsdk.DefaultTimeout(30 * time.Minute),
Expand Down Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.ImporterValidatingResourceIdThen(func(id string) error {
_, err := parse.LinkedServiceID(id)
return err
}, importDataFactoryLinkedService(datafactory.TypeBasicLinkedServiceTypeAzureDatabricks)),

Timeouts: &pluginsdk.ResourceTimeout{
Create: pluginsdk.DefaultTimeout(30 * time.Minute),
Expand Down Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
Expand Down
Loading