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

New resource: azurerm_data_factory_linked_service_cosmosdb_mongoapi #13636

Merged
merged 9 commits into from
Oct 12, 2021
Merged
Show file tree
Hide file tree
Changes from 5 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,258 @@
package datafactory

import (
"fmt"
"time"

"github.com/Azure/azure-sdk-for-go/services/datafactory/mgmt/2018-06-01/datafactory"
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/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"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)

func resourceDataFactoryLinkedServiceCosmosDbMongoAPI() *pluginsdk.Resource {
return &pluginsdk.Resource{
Create: resourceDataFactoryLinkedServiceCosmosDbMongoAPICreateUpdate,
Read: resourceDataFactoryLinkedServiceCosmosDbMongoAPIRead,
Update: resourceDataFactoryLinkedServiceCosmosDbMongoAPICreateUpdate,
Delete: resourceDataFactoryLinkedServiceCosmosDbMongoAPIDelete,

Importer: pluginsdk.ImporterValidatingResourceIdThen(func(id string) error {
_, err := parse.LinkedServiceID(id)
return err
}, importDataFactoryLinkedService(datafactory.TypeBasicLinkedServiceTypeCosmosDbMongoDbAPI)),

Timeouts: &pluginsdk.ResourceTimeout{
Create: pluginsdk.DefaultTimeout(30 * time.Minute),
Read: pluginsdk.DefaultTimeout(5 * time.Minute),
Update: pluginsdk.DefaultTimeout(30 * time.Minute),
Delete: pluginsdk.DefaultTimeout(30 * time.Minute),
},

Schema: map[string]*pluginsdk.Schema{
"name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.LinkedServiceDatasetName,
},

"data_factory_name": {
Type: pluginsdk.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.DataFactoryName(),
},

// There's a bug in the Azure API where this is returned in lower-case
// BUG: https://github.com/Azure/azure-rest-api-specs/issues/5788
"resource_group_name": azure.SchemaResourceGroupNameDiffSuppress(),

"connection_string": {
Type: pluginsdk.TypeString,
Optional: true,
Sensitive: true,
DiffSuppressFunc: azureRmDataFactoryLinkedServiceConnectionStringDiff,
ValidateFunc: validation.StringIsNotEmpty,
},

"database": {
Type: pluginsdk.TypeString,
Optional: true,
ValidateFunc: validation.StringIsNotEmpty,
},

"server_version_is_32_or_higher": {
Type: pluginsdk.TypeBool,
Optional: true,
Default: false,
},

"description": {
Type: pluginsdk.TypeString,
Optional: true,
ValidateFunc: validation.StringIsNotEmpty,
},

"integration_runtime_name": {
Type: pluginsdk.TypeString,
Optional: true,
ValidateFunc: validation.StringIsNotEmpty,
},

"parameters": {
Type: pluginsdk.TypeMap,
Optional: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},

"annotations": {
Type: pluginsdk.TypeList,
Optional: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},

"additional_properties": {
Type: pluginsdk.TypeMap,
Optional: true,
Elem: &pluginsdk.Schema{
Type: pluginsdk.TypeString,
},
},
},
}
}

func resourceDataFactoryLinkedServiceCosmosDbMongoAPICreateUpdate(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()

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, 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", id.Name, id.FactoryName, id.ResourceGroup, err)
}
}

if existing.ID != nil && *existing.ID != "" {
return tf.ImportAsExistsError("azurerm_data_factory_linked_service_cosmosdb", *existing.ID)
}
}

cosmosdbProperties := &datafactory.CosmosDbMongoDbAPILinkedServiceTypeProperties{}

connectionStringSecureString := datafactory.SecureString{
Value: utils.String(d.Get("connection_string").(string)),
Type: datafactory.TypeSecureString,
}
cosmosdbProperties.ConnectionString = connectionStringSecureString
cosmosdbProperties.Database = d.Get("database").(string)
cosmosdbProperties.IsServerVersionAbove32 = d.Get("server_version_is_32_or_higher").(bool)

cosmosdbLinkedService := &datafactory.CosmosDbMongoDbAPILinkedService{
Description: utils.String(d.Get("description").(string)),
CosmosDbMongoDbAPILinkedServiceTypeProperties: cosmosdbProperties,
Type: datafactory.TypeBasicLinkedServiceTypeCosmosDbMongoDbAPI,
}

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

if v, ok := d.GetOk("integration_runtime_name"); ok {
cosmosdbLinkedService.ConnectVia = expandDataFactoryLinkedServiceIntegrationRuntime(v.(string))
}

if v, ok := d.GetOk("additional_properties"); ok {
cosmosdbLinkedService.AdditionalProperties = v.(map[string]interface{})
}

if v, ok := d.GetOk("annotations"); ok {
annotations := v.([]interface{})
cosmosdbLinkedService.Annotations = &annotations
}

linkedService := datafactory.LinkedServiceResource{
Properties: cosmosdbLinkedService,
}

if _, err := client.CreateOrUpdate(ctx, id.ResourceGroup, id.FactoryName, id.Name, linkedService, ""); err != nil {
return fmt.Errorf("creating/updating Data Factory Linked Service CosmosDb %q (Data Factory %q / Resource Group %q): %+v", id.Name, id.FactoryName, id.ResourceGroup, err)
}

d.SetId(id.ID())

return resourceDataFactoryLinkedServiceCosmosDbMongoAPIRead(d, meta)
}

func resourceDataFactoryLinkedServiceCosmosDbMongoAPIRead(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).DataFactory.LinkedServiceClient
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := parse.LinkedServiceID(d.Id())
if err != nil {
return err
}

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("retrieving Data Factory Linked Service CosmosDB %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", id.ResourceGroup)
d.Set("data_factory_name", id.FactoryName)

cosmosdb, ok := resp.Properties.AsCosmosDbMongoDbAPILinkedService()
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.TypeBasicLinkedServiceTypeCosmosDbMongoDbAPI, *resp.Type)
}

d.Set("additional_properties", cosmosdb.AdditionalProperties)
d.Set("description", cosmosdb.Description)

annotations := flattenDataFactoryAnnotations(cosmosdb.Annotations)
if err := d.Set("annotations", annotations); err != nil {
return fmt.Errorf("setting `annotations`: %+v", err)
}

parameters := flattenDataFactoryParameters(cosmosdb.Parameters)
if err := d.Set("parameters", parameters); err != nil {
return fmt.Errorf("setting `parameters`: %+v", err)
}

if connectVia := cosmosdb.ConnectVia; connectVia != nil {
if connectVia.ReferenceName != nil {
d.Set("integration_runtime_name", connectVia.ReferenceName)
}
}

databaseName := cosmosdb.CosmosDbMongoDbAPILinkedServiceTypeProperties.Database
d.Set("database", databaseName)

versionAbove32 := cosmosdb.CosmosDbMongoDbAPILinkedServiceTypeProperties.IsServerVersionAbove32
d.Set("server_version_is_32_or_higher", versionAbove32)

return nil
}

func resourceDataFactoryLinkedServiceCosmosDbMongoAPIDelete(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).DataFactory.LinkedServiceClient
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := parse.LinkedServiceID(d.Id())
if err != nil {
return err
}

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 nil
}
Loading