Skip to content

Commit

Permalink
New resource: azurerm_data_factory_linked_service_cosmosdb_mongoapi
Browse files Browse the repository at this point in the history
Fixes #8905

```
$ TF_ACC=1 go test -v ./internal/services/datafactory -timeout=1000m -run='TestAccDataFactoryLinkedServiceCosmosDbMongoAPI'
=== RUN   TestAccDataFactoryLinkedServiceCosmosDbMongoAPI_basic
=== PAUSE TestAccDataFactoryLinkedServiceCosmosDbMongoAPI_basic
=== RUN   TestAccDataFactoryLinkedServiceCosmosDbMongoAPI_serverVersionAbove32
=== PAUSE TestAccDataFactoryLinkedServiceCosmosDbMongoAPI_serverVersionAbove32
=== RUN   TestAccDataFactoryLinkedServiceCosmosDbMongoAPI_update
=== PAUSE TestAccDataFactoryLinkedServiceCosmosDbMongoAPI_update
=== CONT  TestAccDataFactoryLinkedServiceCosmosDbMongoAPI_basic
=== CONT  TestAccDataFactoryLinkedServiceCosmosDbMongoAPI_update
=== CONT  TestAccDataFactoryLinkedServiceCosmosDbMongoAPI_serverVersionAbove32
--- PASS: TestAccDataFactoryLinkedServiceCosmosDbMongoAPI_serverVersionAbove32 (142.39s)
--- PASS: TestAccDataFactoryLinkedServiceCosmosDbMongoAPI_basic (144.43s)
--- PASS: TestAccDataFactoryLinkedServiceCosmosDbMongoAPI_update (189.73s)
PASS
ok  	github.com/hashicorp/terraform-provider-azurerm/internal/services/datafactory	191.256s
```
  • Loading branch information
favoretti committed Oct 6, 2021
1 parent bc6076f commit 0f70cb2
Show file tree
Hide file tree
Showing 7 changed files with 592 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,272 @@
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,

// TODO: replace this with an importer which validates the ID during import
Importer: pluginsdk.DefaultImporter(),

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_above_32": {
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
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)

if d.IsNewResource() {
existing, err := client.Get(ctx, resourceGroup, dataFactoryName, 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)
}
}

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

cosmosdbProperties := &datafactory.CosmosDbMongoDbAPILinkedServiceTypeProperties{}

databaseName := d.Get("database").(string)
versionAbove32 := d.Get("server_version_above_32").(bool)

connectionString := d.Get("connection_string").(string)
connectionStringSecureString := datafactory.SecureString{
Value: &connectionString,
Type: datafactory.TypeSecureString,
}
cosmosdbProperties.ConnectionString = connectionStringSecureString
cosmosdbProperties.Database = databaseName
cosmosdbProperties.IsServerVersionAbove32 = versionAbove32

description := d.Get("description").(string)

cosmosdbLinkedService := &datafactory.CosmosDbMongoDbAPILinkedService{
Description: &description,
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, 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)
}

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)

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_above_32", 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

0 comments on commit 0f70cb2

Please sign in to comment.