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: porting parts of datafactory over to hashicorp/go-azure-sdk #22409

Merged
merged 14 commits into from
Jul 13, 2023
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
4 changes: 3 additions & 1 deletion internal/clients/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,9 @@ func (client *Client) Build(ctx context.Context, o *common.ClientOptions) error
if client.Datadog, err = datadog.NewClient(o); err != nil {
return fmt.Errorf("building clients for Datadog: %+v", err)
}
client.DataFactory = datafactory.NewClient(o)
if client.DataFactory, err = datafactory.NewClient(o); err != nil {
return fmt.Errorf("building clients for DataFactory: %+v", err)
}
if client.DataProtection, err = dataprotection.NewClient(o); err != nil {
return fmt.Errorf("building clients for DataProtection: %+v", err)
}
Expand Down
77 changes: 48 additions & 29 deletions internal/services/datafactory/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,59 +4,78 @@
package client

import (
"fmt"

"github.com/Azure/azure-sdk-for-go/services/datafactory/mgmt/2018-06-01/datafactory" // nolint: staticcheck
"github.com/hashicorp/go-azure-sdk/resource-manager/datafactory/2018-06-01/factories"
"github.com/hashicorp/go-azure-sdk/resource-manager/datafactory/2018-06-01/managedprivateendpoints"
"github.com/hashicorp/go-azure-sdk/resource-manager/datafactory/2018-06-01/managedvirtualnetworks"
"github.com/hashicorp/terraform-provider-azurerm/internal/common"
)

type Client struct {
DataFlowClient *datafactory.DataFlowsClient
DatasetClient *datafactory.DatasetsClient
FactoriesClient *datafactory.FactoriesClient
IntegrationRuntimesClient *datafactory.IntegrationRuntimesClient
LinkedServiceClient *datafactory.LinkedServicesClient
ManagedPrivateEndpointsClient *datafactory.ManagedPrivateEndpointsClient
ManagedVirtualNetworksClient *datafactory.ManagedVirtualNetworksClient
PipelinesClient *datafactory.PipelinesClient
TriggersClient *datafactory.TriggersClient
Factories *factories.FactoriesClient
ManagedPrivateEndpoints *managedprivateendpoints.ManagedPrivateEndpointsClient
ManagedVirtualNetworks *managedvirtualnetworks.ManagedVirtualNetworksClient

// TODO: convert to using hashicorp/go-azure-sdk
DataFlowClient *datafactory.DataFlowsClient
DatasetClient *datafactory.DatasetsClient
IntegrationRuntimesClient *datafactory.IntegrationRuntimesClient
LinkedServiceClient *datafactory.LinkedServicesClient
PipelinesClient *datafactory.PipelinesClient
TriggersClient *datafactory.TriggersClient
}

func NewClient(o *common.ClientOptions) *Client {
func NewClient(o *common.ClientOptions) (*Client, error) {
factoriesClient, err := factories.NewFactoriesClientWithBaseURI(o.Environment.ResourceManager)
if err != nil {
return nil, fmt.Errorf("building Factories client: %+v", err)
}
o.Configure(factoriesClient.Client, o.Authorizers.ResourceManager)

managedPrivateEndpointsClient, err := managedprivateendpoints.NewManagedPrivateEndpointsClientWithBaseURI(o.Environment.ResourceManager)
if err != nil {
return nil, fmt.Errorf("building ManagedPrivateEndpoints client: %+v", err)
}
o.Configure(managedPrivateEndpointsClient.Client, o.Authorizers.ResourceManager)

managedVirtualNetworksClient, err := managedvirtualnetworks.NewManagedVirtualNetworksClientWithBaseURI(o.Environment.ResourceManager)
if err != nil {
return nil, fmt.Errorf("building ManagedVirtualNetworks client: %+v", err)
}
o.Configure(managedVirtualNetworksClient.Client, o.Authorizers.ResourceManager)

// TODO: port the below operations to use `hashicorp/go-azure-sdk` in time
dataFlowClient := datafactory.NewDataFlowsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&dataFlowClient.Client, o.ResourceManagerAuthorizer)

DatasetClient := datafactory.NewDatasetsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&DatasetClient.Client, o.ResourceManagerAuthorizer)

FactoriesClient := datafactory.NewFactoriesClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&FactoriesClient.Client, o.ResourceManagerAuthorizer)

IntegrationRuntimesClient := datafactory.NewIntegrationRuntimesClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&IntegrationRuntimesClient.Client, o.ResourceManagerAuthorizer)

LinkedServiceClient := datafactory.NewLinkedServicesClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&LinkedServiceClient.Client, o.ResourceManagerAuthorizer)

ManagedPrivateEndpointsClient := datafactory.NewManagedPrivateEndpointsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&ManagedPrivateEndpointsClient.Client, o.ResourceManagerAuthorizer)

ManagedVirtualNetworksClient := datafactory.NewManagedVirtualNetworksClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&ManagedVirtualNetworksClient.Client, o.ResourceManagerAuthorizer)

PipelinesClient := datafactory.NewPipelinesClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&PipelinesClient.Client, o.ResourceManagerAuthorizer)

TriggersClient := datafactory.NewTriggersClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&TriggersClient.Client, o.ResourceManagerAuthorizer)

return &Client{
DataFlowClient: &dataFlowClient,
DatasetClient: &DatasetClient,
FactoriesClient: &FactoriesClient,
IntegrationRuntimesClient: &IntegrationRuntimesClient,
LinkedServiceClient: &LinkedServiceClient,
ManagedPrivateEndpointsClient: &ManagedPrivateEndpointsClient,
ManagedVirtualNetworksClient: &ManagedVirtualNetworksClient,
PipelinesClient: &PipelinesClient,
TriggersClient: &TriggersClient,
}
Factories: factoriesClient,
ManagedPrivateEndpoints: managedPrivateEndpointsClient,
ManagedVirtualNetworks: managedVirtualNetworksClient,

// TODO: port to `hashicorp/go-azure-sdk`
DataFlowClient: &dataFlowClient,
DatasetClient: &DatasetClient,
IntegrationRuntimesClient: &IntegrationRuntimesClient,
LinkedServiceClient: &LinkedServiceClient,
PipelinesClient: &PipelinesClient,
TriggersClient: &TriggersClient,
}, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"time"

"github.com/Azure/azure-sdk-for-go/services/datafactory/mgmt/2018-06-01/datafactory" // nolint: staticcheck
"github.com/hashicorp/go-azure-sdk/resource-manager/datafactory/2018-06-01/factories"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/datafactory/parse"
Expand Down Expand Up @@ -50,7 +51,7 @@ func resourceDataFactoryCustomDataset() *pluginsdk.Resource {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.DataFactoryID,
ValidateFunc: factories.ValidateFactoryID,
},

"linked_service": {
Expand Down Expand Up @@ -141,12 +142,12 @@ func resourceDataFactoryCustomDatasetCreateUpdate(d *pluginsdk.ResourceData, met
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()

dataFactoryId, err := parse.DataFactoryID(d.Get("data_factory_id").(string))
dataFactoryId, err := factories.ParseFactoryID(d.Get("data_factory_id").(string))
if err != nil {
return err
}

id := parse.NewDataSetID(subscriptionId, dataFactoryId.ResourceGroup, dataFactoryId.FactoryName, d.Get("name").(string))
id := parse.NewDataSetID(subscriptionId, dataFactoryId.ResourceGroupName, dataFactoryId.FactoryName, d.Get("name").(string))
if d.IsNewResource() {
existing, err := client.Get(ctx, id.ResourceGroup, id.FactoryName, id.Name, "")
if err != nil {
Expand Down Expand Up @@ -189,7 +190,7 @@ func resourceDataFactoryCustomDatasetCreateUpdate(d *pluginsdk.ResourceData, met
}

if v, ok := d.GetOk("parameters"); ok {
props["parameters"] = expandDataFactoryParameters(v.(map[string]interface{}))
props["parameters"] = expandDataSetParameters(v.(map[string]interface{}))
}

if v, ok := d.GetOk("schema_json"); ok {
Expand Down Expand Up @@ -242,7 +243,7 @@ func resourceDataFactoryCustomDatasetRead(d *pluginsdk.ResourceData, meta interf
}

d.Set("name", id.Name)
d.Set("data_factory_id", parse.NewDataFactoryID(subscriptionId, id.ResourceGroup, id.FactoryName).ID())
d.Set("data_factory_id", factories.NewFactoryID(subscriptionId, id.ResourceGroup, id.FactoryName).ID())

byteArr, err := json.Marshal(resp.Properties)
if err != nil {
Expand Down Expand Up @@ -301,7 +302,7 @@ func resourceDataFactoryCustomDatasetRead(d *pluginsdk.ResourceData, meta interf
}
delete(m, "parameters")
}
if err := d.Set("parameters", flattenDataFactoryParameters(parameters)); err != nil {
if err := d.Set("parameters", flattenDataSetParameters(parameters)); err != nil {
return fmt.Errorf("setting `parameters`: %+v", err)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import (
"time"

"github.com/Azure/azure-sdk-for-go/services/datafactory/mgmt/2018-06-01/datafactory" // nolint: staticcheck
"github.com/hashicorp/go-azure-sdk/resource-manager/datafactory/2018-06-01/factories"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"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/services/datafactory/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
Expand Down Expand Up @@ -48,7 +48,7 @@ func resourceDataFactoryDataFlow() *pluginsdk.Resource {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.DataFactoryID,
ValidateFunc: factories.ValidateFactoryID,
},

"script": {
Expand Down Expand Up @@ -103,12 +103,12 @@ func resourceDataFactoryDataFlowCreateUpdate(d *pluginsdk.ResourceData, meta int
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()

dataFactoryId, err := parse.DataFactoryID(d.Get("data_factory_id").(string))
dataFactoryId, err := factories.ParseFactoryID(d.Get("data_factory_id").(string))
if err != nil {
return err
}

id := parse.NewDataFlowID(subscriptionId, dataFactoryId.ResourceGroup, dataFactoryId.FactoryName, d.Get("name").(string))
id := parse.NewDataFlowID(subscriptionId, dataFactoryId.ResourceGroupName, dataFactoryId.FactoryName, d.Get("name").(string))
if d.IsNewResource() {
existing, err := client.Get(ctx, id.ResourceGroup, id.FactoryName, id.Name, "")
if err != nil {
Expand Down Expand Up @@ -187,7 +187,7 @@ func resourceDataFactoryDataFlowRead(d *pluginsdk.ResourceData, meta interface{}
}

d.Set("name", id.Name)
d.Set("data_factory_id", parse.NewDataFactoryID(id.SubscriptionId, id.ResourceGroup, id.FactoryName).ID())
d.Set("data_factory_id", factories.NewFactoryID(id.SubscriptionId, id.ResourceGroup, id.FactoryName).ID())
d.Set("description", mappingDataFlow.Description)

if err := d.Set("annotations", flattenDataFactoryAnnotations(mappingDataFlow.Annotations)); err != nil {
Expand Down
104 changes: 72 additions & 32 deletions internal/services/datafactory/data_factory_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ import (
"regexp"
"time"

"github.com/Azure/azure-sdk-for-go/services/datafactory/mgmt/2018-06-01/datafactory" // nolint: staticcheck
"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/go-azure-helpers/resourcemanager/identity"
"github.com/hashicorp/go-azure-helpers/resourcemanager/location"
"github.com/hashicorp/go-azure-helpers/resourcemanager/tags"
"github.com/hashicorp/go-azure-sdk/resource-manager/datafactory/2018-06-01/factories"
"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/tags"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)

func dataSourceDataFactory() *pluginsdk.Resource {
Expand All @@ -34,7 +34,7 @@ func dataSourceDataFactory() *pluginsdk.Resource {
Required: true,
ValidateFunc: validation.StringMatch(
regexp.MustCompile(`^[A-Za-z0-9]+(?:-[A-Za-z0-9]+)*$`),
`Invalid name for Data Factory, see https://docs.microsoft.com/en-us/azure/data-factory/naming-rules`,
`Invalid name for Data Factory, see https://docs.microsoft.com/azure/data-factory/naming-rules`,
),
},

Expand Down Expand Up @@ -106,56 +106,96 @@ func dataSourceDataFactory() *pluginsdk.Resource {
},
},

"tags": tags.SchemaDataSource(),
"tags": commonschema.TagsDataSource(),
},
}
}

func dataSourceDataFactoryRead(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).DataFactory.FactoriesClient
client := meta.(*clients.Client).DataFactory.Factories
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

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

resp, err := client.Get(ctx, id.ResourceGroup, id.FactoryName, "")
id := factories.NewFactoryID(subscriptionId, d.Get("resource_group_name").(string), d.Get("name").(string))
resp, err := client.Get(ctx, id, factories.DefaultGetOperationOptions())
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
if response.WasNotFound(resp.HttpResponse) {
return fmt.Errorf("%s was not found", id)
}
return fmt.Errorf("retrieving %s: %+v", id, err)
}

d.SetId(id.ID())

d.Set("location", location.NormalizeNilable(resp.Location))
if model := resp.Model; model != nil {
d.Set("location", location.NormalizeNilable(model.Location))

d.Set("vsts_configuration", []interface{}{})
d.Set("github_configuration", []interface{}{})
repoType, repo := flattenDataFactoryRepoConfiguration(&resp)
if repoType == datafactory.TypeBasicFactoryRepoConfigurationTypeFactoryVSTSConfiguration {
if err := d.Set("vsts_configuration", repo); err != nil {
return fmt.Errorf("setting `vsts_configuration`: %+v", err)
identity, err := identity.FlattenLegacySystemAndUserAssignedMap(model.Identity)
if err != nil {
return fmt.Errorf("flattening `identity`: %+v", err)
}
}
if repoType == datafactory.TypeBasicFactoryRepoConfigurationTypeFactoryGitHubConfiguration {
if err := d.Set("github_configuration", repo); err != nil {
return fmt.Errorf("setting `github_configuration`: %+v", err)
if err := d.Set("identity", identity); err != nil {
return fmt.Errorf("setting `identity`: %+v", err)
}

if props := model.Properties; props != nil {
githubConfiguration := flattenGitHubRepoConfigurationDataSource(props.RepoConfiguration)
if err := d.Set("github_configuration", githubConfiguration); err != nil {
return fmt.Errorf("setting `github_configuration`: %+v", err)
}

vstsConfiguration := flattenVSTSRepoConfigurationDataSource(props.RepoConfiguration)
if err := d.Set("vsts_configuration", vstsConfiguration); err != nil {
return fmt.Errorf("setting `vsts_configuration`: %+v", err)
}
}

if err := tags.FlattenAndSet(d, model.Tags); err != nil {
return fmt.Errorf("setting `tags`: %+v", err)
}
}
if repoType == datafactory.TypeBasicFactoryRepoConfigurationTypeFactoryRepoConfiguration {
d.Set("vsts_configuration", repo)
d.Set("github_configuration", repo)
}

identity, err := flattenIdentity(resp.Identity)
if err != nil {
return fmt.Errorf("flattening `identity`: %+v", err)
return nil
}

func flattenGitHubRepoConfigurationDataSource(input factories.FactoryRepoConfiguration) []interface{} {
output := make([]interface{}, 0)

if v, ok := input.(factories.FactoryGitHubConfiguration); ok {
gitUrl := ""
if v.HostName != nil {
gitUrl = *v.HostName
}
output = append(output, map[string]interface{}{
"account_name": v.AccountName,
"branch_name": v.CollaborationBranch,
"git_url": gitUrl,
"repository_name": v.RepositoryName,
"root_folder": v.RootFolder,
})
}
if err := d.Set("identity", identity); err != nil {
return fmt.Errorf("setting `identity`: %+v", err)

return output
}

func flattenVSTSRepoConfigurationDataSource(input factories.FactoryRepoConfiguration) []interface{} {
output := make([]interface{}, 0)

if v, ok := input.(factories.FactoryVSTSConfiguration); ok {
tenantId := ""
if v.TenantId != nil {
tenantId = *v.TenantId
}
output = append(output, map[string]interface{}{
"account_name": v.AccountName,
"branch_name": v.CollaborationBranch,
"project_name": v.ProjectName,
"repository_name": v.RepositoryName,
"root_folder": v.RootFolder,
"tenant_id": tenantId,
})
}

return tags.FlattenAndSet(d, resp.Tags)
return output
}
Loading