From 99157c92d53c8bf2109a7751d662b79a739825d5 Mon Sep 17 00:00:00 2001 From: Vladimir Lazarenko Date: Wed, 6 Oct 2021 09:53:25 +0200 Subject: [PATCH 1/9] New resource: `azurerm_data_factory_linked_service_cosmosdb_mongoapi` 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 ``` --- ...nked_service_cosmosdb_mongoapi_resource.go | 272 ++++++++++++++++++ ...service_cosmosdb_mongoapi_resource_test.go | 223 ++++++++++++++ internal/services/datafactory/registration.go | 1 + ...tory_linked_service_cosmosdb.html.markdown | 4 +- ...ed_service_cosmosdb_mongoapi.html.markdown | 92 ++++++ 5 files changed, 590 insertions(+), 2 deletions(-) create mode 100644 internal/services/datafactory/data_factory_linked_service_cosmosdb_mongoapi_resource.go create mode 100644 internal/services/datafactory/data_factory_linked_service_cosmosdb_mongoapi_resource_test.go create mode 100644 website/docs/r/data_factory_linked_service_cosmosdb_mongoapi.html.markdown diff --git a/internal/services/datafactory/data_factory_linked_service_cosmosdb_mongoapi_resource.go b/internal/services/datafactory/data_factory_linked_service_cosmosdb_mongoapi_resource.go new file mode 100644 index 000000000000..11f4a38b8289 --- /dev/null +++ b/internal/services/datafactory/data_factory_linked_service_cosmosdb_mongoapi_resource.go @@ -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 +} diff --git a/internal/services/datafactory/data_factory_linked_service_cosmosdb_mongoapi_resource_test.go b/internal/services/datafactory/data_factory_linked_service_cosmosdb_mongoapi_resource_test.go new file mode 100644 index 000000000000..76d5caf96f5c --- /dev/null +++ b/internal/services/datafactory/data_factory_linked_service_cosmosdb_mongoapi_resource_test.go @@ -0,0 +1,223 @@ +package datafactory_test + +import ( + "context" + "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/tf/pluginsdk" + "github.com/hashicorp/terraform-provider-azurerm/utils" +) + +type LinkedServiceCosmosDBMongoAPIResource struct { +} + +func TestAccDataFactoryLinkedServiceCosmosDbMongoAPI_basic(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_data_factory_linked_service_cosmosdb_mongoapi", "test") + r := LinkedServiceCosmosDBMongoAPIResource{} + + data.ResourceTest(t, r, []acceptance.TestStep{ + { + Config: r.basic(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + data.ImportStep("connection_string"), + }) +} + +func TestAccDataFactoryLinkedServiceCosmosDbMongoAPI_serverVersionAbove32(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_data_factory_linked_service_cosmosdb_mongoapi", "test") + r := LinkedServiceCosmosDBMongoAPIResource{} + + data.ResourceTest(t, r, []acceptance.TestStep{ + { + Config: r.serverVersionAbove32(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + data.ImportStep("connection_string"), + }) +} + +func TestAccDataFactoryLinkedServiceCosmosDbMongoAPI_update(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_data_factory_linked_service_cosmosdb_mongoapi", "test") + r := LinkedServiceCosmosDBMongoAPIResource{} + + data.ResourceTest(t, r, []acceptance.TestStep{ + { + Config: r.update1(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + check.That(data.ResourceName).Key("parameters.%").HasValue("2"), + check.That(data.ResourceName).Key("annotations.#").HasValue("3"), + check.That(data.ResourceName).Key("additional_properties.%").HasValue("2"), + check.That(data.ResourceName).Key("description").HasValue("test description"), + ), + }, + data.ImportStep("connection_string"), + { + Config: r.update2(data), + Check: acceptance.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + check.That(data.ResourceName).Key("parameters.%").HasValue("3"), + check.That(data.ResourceName).Key("annotations.#").HasValue("2"), + check.That(data.ResourceName).Key("additional_properties.%").HasValue("1"), + check.That(data.ResourceName).Key("description").HasValue("test description 2"), + ), + }, + data.ImportStep("connection_string"), + }) +} + +func (t LinkedServiceCosmosDBMongoAPIResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) { + id, err := azure.ParseAzureResourceID(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, "") + if err != nil { + return nil, fmt.Errorf("reading Data Factory Linked Service CosmosDB (%s): %+v", id, err) + } + + return utils.Bool(resp.ID != nil), nil +} + +func (LinkedServiceCosmosDBMongoAPIResource) basic(data acceptance.TestData) string { + return fmt.Sprintf(` +provider "azurerm" { + features {} +} + +resource "azurerm_resource_group" "test" { + name = "acctestRG-df-%d" + location = "%s" +} + +resource "azurerm_data_factory" "test" { + name = "acctestdf%d" + location = azurerm_resource_group.test.location + resource_group_name = azurerm_resource_group.test.name +} + +resource "azurerm_data_factory_linked_service_cosmosdb_mongoapi" "test" { + name = "acctestlscosmosdb%d" + resource_group_name = azurerm_resource_group.test.name + data_factory_name = azurerm_data_factory.test.name + connection_string = "mongodb://testinstance:testkey@testinstance.documents.azure.com:10255/?ssl=true" +} +`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger) +} + +func (LinkedServiceCosmosDBMongoAPIResource) serverVersionAbove32(data acceptance.TestData) string { + return fmt.Sprintf(` +provider "azurerm" { + features {} +} + +resource "azurerm_resource_group" "test" { + name = "acctestRG-df-%d" + location = "%s" +} + +resource "azurerm_data_factory" "test" { + name = "acctestdf%d" + location = azurerm_resource_group.test.location + resource_group_name = azurerm_resource_group.test.name +} + +resource "azurerm_data_factory_linked_service_cosmosdb_mongoapi" "test" { + name = "acctestlscosmosdb%d" + resource_group_name = azurerm_resource_group.test.name + data_factory_name = azurerm_data_factory.test.name + connection_string = "mongodb://testinstance:testkey@testinstance.documents.azure.com:10255/?ssl=true" + server_version_above_32 = true +} +`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger) +} + +func (LinkedServiceCosmosDBMongoAPIResource) update1(data acceptance.TestData) string { + return fmt.Sprintf(` +provider "azurerm" { + features {} +} + +resource "azurerm_resource_group" "test" { + name = "acctestRG-df-%d" + location = "%s" +} + +resource "azurerm_data_factory" "test" { + name = "acctestdf%d" + location = azurerm_resource_group.test.location + resource_group_name = azurerm_resource_group.test.name +} + +resource "azurerm_data_factory_linked_service_cosmosdb_mongoapi" "test" { + name = "acctestlscosmosdb%d" + resource_group_name = azurerm_resource_group.test.name + data_factory_name = azurerm_data_factory.test.name + connection_string = "mongodb://testinstance:testkey@testinstance.documents.azure.com:10255/?ssl=true" + annotations = ["test1", "test2", "test3"] + description = "test description" + + parameters = { + foo = "test1" + bar = "test2" + } + + additional_properties = { + foo = "test1" + bar = "test2" + } +} +`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger) +} + +func (LinkedServiceCosmosDBMongoAPIResource) update2(data acceptance.TestData) string { + return fmt.Sprintf(` +provider "azurerm" { + features {} +} + +resource "azurerm_resource_group" "test" { + name = "acctestRG-df-%d" + location = "%s" +} + +resource "azurerm_data_factory" "test" { + name = "acctestdf%d" + location = azurerm_resource_group.test.location + resource_group_name = azurerm_resource_group.test.name +} + +resource "azurerm_data_factory_linked_service_cosmosdb_mongoapi" "test" { + name = "acctestlscosmosdb%d" + resource_group_name = azurerm_resource_group.test.name + data_factory_name = azurerm_data_factory.test.name + connection_string = "mongodb://testinstance:testkey@testinstance.documents.azure.com:10255/?ssl=true" + annotations = ["test1", "test2"] + description = "test description 2" + + parameters = { + foo = "test1" + bar = "test2" + buzz = "test3" + } + + additional_properties = { + foo = "test1" + } +} +`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger) +} diff --git a/internal/services/datafactory/registration.go b/internal/services/datafactory/registration.go index 362e8c42b615..bbfb08e2dcb9 100644 --- a/internal/services/datafactory/registration.go +++ b/internal/services/datafactory/registration.go @@ -55,6 +55,7 @@ func (r Registration) SupportedResources() map[string]*pluginsdk.Resource { "azurerm_data_factory_linked_service_azure_sql_database": resourceDataFactoryLinkedServiceAzureSQLDatabase(), "azurerm_data_factory_linked_service_azure_table_storage": resourceDataFactoryLinkedServiceAzureTableStorage(), "azurerm_data_factory_linked_service_cosmosdb": resourceDataFactoryLinkedServiceCosmosDb(), + "azurerm_data_factory_linked_service_cosmosdb_mongoapi": resourceDataFactoryLinkedServiceCosmosDbMongoAPI(), "azurerm_data_factory_linked_service_data_lake_storage_gen2": resourceDataFactoryLinkedServiceDataLakeStorageGen2(), "azurerm_data_factory_linked_service_key_vault": resourceDataFactoryLinkedServiceKeyVault(), "azurerm_data_factory_linked_service_kusto": resourceDataFactoryLinkedServiceKusto(), diff --git a/website/docs/r/data_factory_linked_service_cosmosdb.html.markdown b/website/docs/r/data_factory_linked_service_cosmosdb.html.markdown index 3c2767b02926..efd5d8d7d55f 100644 --- a/website/docs/r/data_factory_linked_service_cosmosdb.html.markdown +++ b/website/docs/r/data_factory_linked_service_cosmosdb.html.markdown @@ -3,12 +3,12 @@ subcategory: "Data Factory" layout: "azurerm" page_title: "Azure Resource Manager: azurerm_data_factory_linked_service_cosmosdb" description: |- - Manages a Linked Service (connection) between an SFTP Server and Azure Data Factory. + Manages a Linked Service (connection) between a CosmosDB and Azure Data Factory using SQL API. --- # azurerm_data_factory_linked_service_cosmosdb -Manages a Linked Service (connection) between a SFTP Server and Azure Data Factory. +Manages a Linked Service (connection) between a CosmosDB and Azure Data Factory using SQL API. ~> **Note:** All arguments including the client secret will be stored in the raw state as plain-text. [Read more about sensitive data in state](/docs/state/sensitive-data.html). diff --git a/website/docs/r/data_factory_linked_service_cosmosdb_mongoapi.html.markdown b/website/docs/r/data_factory_linked_service_cosmosdb_mongoapi.html.markdown new file mode 100644 index 000000000000..29a5b0ceb623 --- /dev/null +++ b/website/docs/r/data_factory_linked_service_cosmosdb_mongoapi.html.markdown @@ -0,0 +1,92 @@ +--- +subcategory: "Data Factory" +layout: "azurerm" +page_title: "Azure Resource Manager: azurerm_data_factory_linked_service_cosmosdb_mongoapi" +description: |- +Manages a Linked Service (connection) between a CosmosDB and Azure Data Factory using Mongo API. +--- + +# azurerm_data_factory_linked_service_cosmosdb_mongoapi + +Manages a Linked Service (connection) between a CosmosDB and Azure Data Factory using Mongo API. + +~> **Note:** All arguments including the client secret will be stored in the raw state as plain-text. [Read more about sensitive data in state](/docs/state/sensitive-data.html). + +## Example Usage + +```hcl +resource "azurerm_resource_group" "example" { + name = "example-resources" + location = "West Europe" +} + +data "azurerm_cosmosdb_account" "example" { + name = "tfex-cosmosdb-account" + resource_group_name = "tfex-cosmosdb-account-rg" +} + +resource "azurerm_data_factory" "example" { + name = "example" + location = azurerm_resource_group.example.location + resource_group_name = azurerm_resource_group.example.name +} + +resource "azurerm_data_factory_linked_service_cosmosdb_mongoapi" "example" { + name = "example" + resource_group_name = azurerm_resource_group.example.name + data_factory_name = azurerm_data_factory.example.name + connection_string = "mongodb://testinstance:testkey@testinstance.documents.azure.com:10255/?ssl=true" + database = "foo" + +} +``` + +## Argument Reference + +The following supported arguments are common across all Azure Data Factory Linked Services: + +* `name` - (Required) Specifies the name of the Data Factory Linked Service. Changing this forces a new resource to be created. Must be unique within a data + factory. See the [Microsoft documentation](https://docs.microsoft.com/en-us/azure/data-factory/naming-rules) for all restrictions. + +* `resource_group_name` - (Required) The name of the resource group in which to create the Data Factory Linked Service. Changing this forces a new resource + +* `data_factory_name` - (Required) The Data Factory name in which to associate the Linked Service with. Changing this forces a new resource. + +* `description` - (Optional) The description for the Data Factory Linked Service. + +* `integration_runtime_name` - (Optional) The integration runtime reference to associate with the Data Factory Linked Service. + +* `annotations` - (Optional) List of tags that can be used for describing the Data Factory Linked Service. + +* `parameters` - (Optional) A map of parameters to associate with the Data Factory Linked Service. + +* `additional_properties` - (Optional) A map of additional properties to associate with the Data Factory Linked Service. + +The following supported arguments are specific to CosmosDB Linked Service: + +* `database` - (Optional) The name of the database. + +* `connection_string` - (Required) The connection string. + +## Attributes Reference + +The following attributes are exported: + +* `id` - The ID of the Data Factory Linked Service. + +## Timeouts + +The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/docs/configuration/resources.html#timeouts) for certain actions: + +* `create` - (Defaults to 30 minutes) Used when creating the Data Factory Linked Service. +* `update` - (Defaults to 30 minutes) Used when updating the Data Factory Linked Service. +* `read` - (Defaults to 5 minutes) Used when retrieving the Data Factory Linked Service. +* `delete` - (Defaults to 30 minutes) Used when deleting the Data Factory Linked Service. + +## Import + +Data Factory Linked Service's can be imported using the `resource id`, e.g. + +```shell +terraform import azurerm_data_factory_linked_service_cosmosdb_mongoapi.example /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/example/providers/Microsoft.DataFactory/factories/example/linkedservices/example +``` From d9faeee78cd1e353d7ad8eed3d3e4aea087fc712 Mon Sep 17 00:00:00 2001 From: Vladimir Lazarenko Date: Wed, 6 Oct 2021 09:57:43 +0200 Subject: [PATCH 2/9] weblint --- .../data_factory_linked_service_cosmosdb_mongoapi.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/r/data_factory_linked_service_cosmosdb_mongoapi.html.markdown b/website/docs/r/data_factory_linked_service_cosmosdb_mongoapi.html.markdown index 29a5b0ceb623..df0bc4d5e639 100644 --- a/website/docs/r/data_factory_linked_service_cosmosdb_mongoapi.html.markdown +++ b/website/docs/r/data_factory_linked_service_cosmosdb_mongoapi.html.markdown @@ -3,7 +3,7 @@ subcategory: "Data Factory" layout: "azurerm" page_title: "Azure Resource Manager: azurerm_data_factory_linked_service_cosmosdb_mongoapi" description: |- -Manages a Linked Service (connection) between a CosmosDB and Azure Data Factory using Mongo API. + Manages a Linked Service (connection) between a CosmosDB and Azure Data Factory using Mongo API. --- # azurerm_data_factory_linked_service_cosmosdb_mongoapi From baca0a9a5a88952c9e23e545d5f2996cfd732461 Mon Sep 17 00:00:00 2001 From: Vladimir Lazarenko Date: Mon, 11 Oct 2021 10:17:10 +0200 Subject: [PATCH 3/9] Address feedback --- ...nked_service_cosmosdb_mongoapi_resource.go | 48 +++++++------------ 1 file changed, 17 insertions(+), 31 deletions(-) diff --git a/internal/services/datafactory/data_factory_linked_service_cosmosdb_mongoapi_resource.go b/internal/services/datafactory/data_factory_linked_service_cosmosdb_mongoapi_resource.go index 11f4a38b8289..3507ae885afd 100644 --- a/internal/services/datafactory/data_factory_linked_service_cosmosdb_mongoapi_resource.go +++ b/internal/services/datafactory/data_factory_linked_service_cosmosdb_mongoapi_resource.go @@ -23,8 +23,10 @@ func resourceDataFactoryLinkedServiceCosmosDbMongoAPI() *pluginsdk.Resource { Update: resourceDataFactoryLinkedServiceCosmosDbMongoAPICreateUpdate, Delete: resourceDataFactoryLinkedServiceCosmosDbMongoAPIDelete, - // 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.TypeBasicLinkedServiceTypeCosmosDbMongoDbAPI)), Timeouts: &pluginsdk.ResourceTimeout{ Create: pluginsdk.DefaultTimeout(30 * time.Minute), @@ -66,7 +68,7 @@ func resourceDataFactoryLinkedServiceCosmosDbMongoAPI() *pluginsdk.Resource { ValidateFunc: validation.StringIsNotEmpty, }, - "server_version_above_32": { + "server_version_is_32_or_higher": { Type: pluginsdk.TypeBool, Optional: true, Default: false, @@ -113,18 +115,17 @@ func resourceDataFactoryLinkedServiceCosmosDbMongoAPI() *pluginsdk.Resource { 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() - 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 CosmosDb %q (Data Factory %q / Resource Group %q): %+v", name, dataFactoryName, resourceGroup, err) + 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) } } @@ -135,22 +136,16 @@ func resourceDataFactoryLinkedServiceCosmosDbMongoAPICreateUpdate(d *pluginsdk.R 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, + Value: utils.String(d.Get("connection_string").(string)), Type: datafactory.TypeSecureString, } cosmosdbProperties.ConnectionString = connectionStringSecureString - cosmosdbProperties.Database = databaseName - cosmosdbProperties.IsServerVersionAbove32 = versionAbove32 - - description := d.Get("description").(string) + cosmosdbProperties.Database = d.Get("database").(string) + cosmosdbProperties.IsServerVersionAbove32 = d.Get("server_version_is_32_or_higher").(bool) cosmosdbLinkedService := &datafactory.CosmosDbMongoDbAPILinkedService{ - Description: &description, + Description: utils.String(d.Get("description").(string)), CosmosDbMongoDbAPILinkedServiceTypeProperties: cosmosdbProperties, Type: datafactory.TypeBasicLinkedServiceTypeCosmosDbMongoDbAPI, } @@ -176,20 +171,11 @@ func resourceDataFactoryLinkedServiceCosmosDbMongoAPICreateUpdate(d *pluginsdk.R 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) + 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(*resp.ID) + d.SetId(id.ID()) return resourceDataFactoryLinkedServiceCosmosDbMongoAPIRead(d, meta) } @@ -246,7 +232,7 @@ func resourceDataFactoryLinkedServiceCosmosDbMongoAPIRead(d *pluginsdk.ResourceD d.Set("database", databaseName) versionAbove32 := cosmosdb.CosmosDbMongoDbAPILinkedServiceTypeProperties.IsServerVersionAbove32 - d.Set("server_version_above_32", versionAbove32) + d.Set("server_version_is_32_or_higher", versionAbove32) return nil } From 9e1f8301a7aea7aadcac4930f911c0a3e7831f69 Mon Sep 17 00:00:00 2001 From: Vladimir Lazarenko Date: Mon, 11 Oct 2021 10:19:25 +0200 Subject: [PATCH 4/9] Docs --- ...ata_factory_linked_service_cosmosdb_mongoapi.html.markdown | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/website/docs/r/data_factory_linked_service_cosmosdb_mongoapi.html.markdown b/website/docs/r/data_factory_linked_service_cosmosdb_mongoapi.html.markdown index df0bc4d5e639..f5071e2247b4 100644 --- a/website/docs/r/data_factory_linked_service_cosmosdb_mongoapi.html.markdown +++ b/website/docs/r/data_factory_linked_service_cosmosdb_mongoapi.html.markdown @@ -66,7 +66,9 @@ The following supported arguments are specific to CosmosDB Linked Service: * `database` - (Optional) The name of the database. -* `connection_string` - (Required) The connection string. +* `connection_string` - (Required) The connection string. + +* `server_version_is_32_or_higher` - (Optional) Whether API server version is 3.2 or higher. Defaults to `false`. ## Attributes Reference From b014902dc7c026bd932631a72ed43b9ba790ed70 Mon Sep 17 00:00:00 2001 From: Vladimir Lazarenko Date: Mon, 11 Oct 2021 10:53:53 +0200 Subject: [PATCH 5/9] Rename property in tests --- ...y_linked_service_cosmosdb_mongoapi_resource_test.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/internal/services/datafactory/data_factory_linked_service_cosmosdb_mongoapi_resource_test.go b/internal/services/datafactory/data_factory_linked_service_cosmosdb_mongoapi_resource_test.go index 76d5caf96f5c..3cca745bc2cf 100644 --- a/internal/services/datafactory/data_factory_linked_service_cosmosdb_mongoapi_resource_test.go +++ b/internal/services/datafactory/data_factory_linked_service_cosmosdb_mongoapi_resource_test.go @@ -137,11 +137,11 @@ resource "azurerm_data_factory" "test" { } resource "azurerm_data_factory_linked_service_cosmosdb_mongoapi" "test" { - name = "acctestlscosmosdb%d" - resource_group_name = azurerm_resource_group.test.name - data_factory_name = azurerm_data_factory.test.name - connection_string = "mongodb://testinstance:testkey@testinstance.documents.azure.com:10255/?ssl=true" - server_version_above_32 = true + name = "acctestlscosmosdb%d" + resource_group_name = azurerm_resource_group.test.name + data_factory_name = azurerm_data_factory.test.name + connection_string = "mongodb://testinstance:testkey@testinstance.documents.azure.com:10255/?ssl=true" + server_version_is_32_or_higher = true } `, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger) } From 33da8a35df615044b67e78c229bef3f1d4e1da62 Mon Sep 17 00:00:00 2001 From: Vladimir Lazarenko Date: Tue, 12 Oct 2021 12:04:49 +0200 Subject: [PATCH 6/9] Apply suggestions from code review Co-authored-by: stephybun --- ...ory_linked_service_cosmosdb_mongoapi_resource_test.go | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/internal/services/datafactory/data_factory_linked_service_cosmosdb_mongoapi_resource_test.go b/internal/services/datafactory/data_factory_linked_service_cosmosdb_mongoapi_resource_test.go index 3cca745bc2cf..15cb8368611b 100644 --- a/internal/services/datafactory/data_factory_linked_service_cosmosdb_mongoapi_resource_test.go +++ b/internal/services/datafactory/data_factory_linked_service_cosmosdb_mongoapi_resource_test.go @@ -77,17 +77,14 @@ func TestAccDataFactoryLinkedServiceCosmosDbMongoAPI_update(t *testing.T) { } func (t LinkedServiceCosmosDBMongoAPIResource) 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 CosmosDB (%s): %+v", id, err) + return nil, fmt.Errorf("reading Data Factory CosmosDB (%s): %+v", *id, err) } return utils.Bool(resp.ID != nil), nil From 96552b0bf4974f0cd9659c5e4cf8e0a090821cee Mon Sep 17 00:00:00 2001 From: Vladimir Lazarenko Date: Tue, 12 Oct 2021 12:06:02 +0200 Subject: [PATCH 7/9] Remove unnecessary resource from doc example --- ...ta_factory_linked_service_cosmosdb_mongoapi.html.markdown | 5 ----- 1 file changed, 5 deletions(-) diff --git a/website/docs/r/data_factory_linked_service_cosmosdb_mongoapi.html.markdown b/website/docs/r/data_factory_linked_service_cosmosdb_mongoapi.html.markdown index f5071e2247b4..b6f690db503e 100644 --- a/website/docs/r/data_factory_linked_service_cosmosdb_mongoapi.html.markdown +++ b/website/docs/r/data_factory_linked_service_cosmosdb_mongoapi.html.markdown @@ -20,11 +20,6 @@ resource "azurerm_resource_group" "example" { location = "West Europe" } -data "azurerm_cosmosdb_account" "example" { - name = "tfex-cosmosdb-account" - resource_group_name = "tfex-cosmosdb-account-rg" -} - resource "azurerm_data_factory" "example" { name = "example" location = azurerm_resource_group.example.location From d8abb4df4e62108f89efcf54704cccf0613ab850 Mon Sep 17 00:00:00 2001 From: stephybun Date: Tue, 12 Oct 2021 12:46:50 +0200 Subject: [PATCH 8/9] Sorry that was my bad --- ...factory_linked_service_cosmosdb_mongoapi_resource_test.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/internal/services/datafactory/data_factory_linked_service_cosmosdb_mongoapi_resource_test.go b/internal/services/datafactory/data_factory_linked_service_cosmosdb_mongoapi_resource_test.go index 15cb8368611b..60e44cf48441 100644 --- a/internal/services/datafactory/data_factory_linked_service_cosmosdb_mongoapi_resource_test.go +++ b/internal/services/datafactory/data_factory_linked_service_cosmosdb_mongoapi_resource_test.go @@ -4,8 +4,7 @@ import ( "context" "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" @@ -82,7 +81,7 @@ func (t LinkedServiceCosmosDBMongoAPIResource) Exists(ctx context.Context, clien return nil, err } - resp, err := clients.DataFactory.LinkedServiceClient.Get(ctx, id.ResourceGroup, id.FactoryName, id.Name "") + resp, err := clients.DataFactory.LinkedServiceClient.Get(ctx, id.ResourceGroup, id.FactoryName, id.Name, "") if err != nil { return nil, fmt.Errorf("reading Data Factory CosmosDB (%s): %+v", *id, err) } From 516165f525727b7e2d2b32280ac2980872af602c Mon Sep 17 00:00:00 2001 From: Steph Date: Tue, 12 Oct 2021 16:03:43 +0200 Subject: [PATCH 9/9] fix lint --- ...a_factory_linked_service_cosmosdb_mongoapi_resource_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/internal/services/datafactory/data_factory_linked_service_cosmosdb_mongoapi_resource_test.go b/internal/services/datafactory/data_factory_linked_service_cosmosdb_mongoapi_resource_test.go index 60e44cf48441..04020866723a 100644 --- a/internal/services/datafactory/data_factory_linked_service_cosmosdb_mongoapi_resource_test.go +++ b/internal/services/datafactory/data_factory_linked_service_cosmosdb_mongoapi_resource_test.go @@ -4,10 +4,11 @@ import ( "context" "fmt" "testing" - + "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" )