From 58a5a9313697a4771dddbf5d565cb4152693624c Mon Sep 17 00:00:00 2001 From: ms-henglu Date: Thu, 6 May 2021 10:38:39 +0800 Subject: [PATCH] use container_id to get all necessary parameters --- .../cosmos/cosmosdb_sql_trigger_resource.go | 48 ++++++------------- .../cosmosdb_sql_trigger_resource_test.go | 46 +++++++++--------- .../docs/r/cosmosdb_sql_trigger.html.markdown | 25 ++++------ 3 files changed, 45 insertions(+), 74 deletions(-) diff --git a/azurerm/internal/services/cosmos/cosmosdb_sql_trigger_resource.go b/azurerm/internal/services/cosmos/cosmosdb_sql_trigger_resource.go index f6d6c87147cd0..b8e8280367831 100644 --- a/azurerm/internal/services/cosmos/cosmosdb_sql_trigger_resource.go +++ b/azurerm/internal/services/cosmos/cosmosdb_sql_trigger_resource.go @@ -8,7 +8,6 @@ import ( "github.com/Azure/azure-sdk-for-go/services/cosmos-db/mgmt/2021-01-15/documentdb" "github.com/hashicorp/terraform-plugin-sdk/helper/schema" "github.com/hashicorp/terraform-plugin-sdk/helper/validation" - "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/cosmos/parse" @@ -45,27 +44,11 @@ func resourceCosmosDbSQLTrigger() *schema.Resource { ValidateFunc: validate.CosmosEntityName, }, - "resource_group_name": azure.SchemaResourceGroupName(), - - "account_name": { + "container_id": { Type: schema.TypeString, Required: true, ForceNew: true, - ValidateFunc: validate.CosmosAccountName, - }, - - "container_name": { - Type: schema.TypeString, - Required: true, - ForceNew: true, - ValidateFunc: validate.CosmosEntityName, - }, - - "database_name": { - Type: schema.TypeString, - Required: true, - ForceNew: true, - ValidateFunc: validate.CosmosEntityName, + ValidateFunc: validate.SqlContainerID, }, "body": { @@ -74,7 +57,7 @@ func resourceCosmosDbSQLTrigger() *schema.Resource { ValidateFunc: validation.StringIsNotEmpty, }, - "trigger_operation": { + "operation": { Type: schema.TypeString, Required: true, ValidateFunc: validation.StringInSlice([]string{ @@ -86,7 +69,7 @@ func resourceCosmosDbSQLTrigger() *schema.Resource { }, false), }, - "trigger_type": { + "type": { Type: schema.TypeString, Required: true, ValidateFunc: validation.StringInSlice([]string{ @@ -104,13 +87,14 @@ func resourceCosmosDbSQLTriggerCreateUpdate(d *schema.ResourceData, meta interfa defer cancel() name := d.Get("name").(string) - resourceGroup := d.Get("resource_group_name").(string) - accountName := d.Get("account_name").(string) - containerName := d.Get("container_name").(string) - databaseName := d.Get("database_name").(string) + containerId, _ := parse.SqlContainerID(d.Get("container_id").(string)) + resourceGroup := containerId.ResourceGroup + accountName := containerId.DatabaseAccountName + containerName := containerId.ContainerName + databaseName := containerId.SqlDatabaseName body := d.Get("body").(string) - triggerOperation := d.Get("trigger_operation").(string) - triggerType := d.Get("trigger_type").(string) + triggerOperation := d.Get("operation").(string) + triggerType := d.Get("type").(string) id := parse.NewSqlTriggerID(subscriptionId, resourceGroup, accountName, databaseName, containerName, name) @@ -169,16 +153,14 @@ func resourceCosmosDbSQLTriggerRead(d *schema.ResourceData, meta interface{}) er } return fmt.Errorf("retrieving CosmosDb SQLTrigger %q: %+v", id, err) } + containerId := parse.NewSqlContainerID(id.SubscriptionId, id.ResourceGroup, id.DatabaseAccountName, id.SqlDatabaseName, id.ContainerName) d.Set("name", id.TriggerName) - d.Set("resource_group_name", id.ResourceGroup) - d.Set("account_name", id.DatabaseAccountName) - d.Set("container_name", id.ContainerName) - d.Set("database_name", id.SqlDatabaseName) + d.Set("container_id", containerId.ID()) if props := resp.SQLTriggerGetProperties; props != nil { if props.Resource != nil { d.Set("body", props.Resource.Body) - d.Set("trigger_operation", props.Resource.TriggerOperation) - d.Set("trigger_type", props.Resource.TriggerType) + d.Set("operation", props.Resource.TriggerOperation) + d.Set("type", props.Resource.TriggerType) } } return nil diff --git a/azurerm/internal/services/cosmos/cosmosdb_sql_trigger_resource_test.go b/azurerm/internal/services/cosmos/cosmosdb_sql_trigger_resource_test.go index 81814af704968..63978db320f71 100644 --- a/azurerm/internal/services/cosmos/cosmosdb_sql_trigger_resource_test.go +++ b/azurerm/internal/services/cosmos/cosmosdb_sql_trigger_resource_test.go @@ -63,6 +63,13 @@ func TestAccCosmosDbSQLTrigger_update(t *testing.T) { ), }, data.ImportStep(), + { + Config: r.basic(data), + Check: resource.ComposeTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + data.ImportStep(), }) } @@ -131,14 +138,11 @@ func (r CosmosDbSQLTriggerResource) basic(data acceptance.TestData) string { %s resource "azurerm_cosmosdb_sql_trigger" "test" { - name = "acctest-%d" - resource_group_name = azurerm_resource_group.test.name - account_name = azurerm_cosmosdb_account.test.name - container_name = azurerm_cosmosdb_sql_container.test.name - database_name = azurerm_cosmosdb_sql_database.test.name - body = "function trigger(){}" - trigger_operation = "All" - trigger_type = "Pre" + name = "acctest-%d" + container_id = azurerm_cosmosdb_sql_container.test.id + body = "function trigger(){}" + operation = "All" + type = "Pre" } `, template, data.RandomInteger) } @@ -149,14 +153,11 @@ func (r CosmosDbSQLTriggerResource) requiresImport(data acceptance.TestData) str %s resource "azurerm_cosmosdb_sql_trigger" "import" { - name = azurerm_cosmosdb_sql_trigger.test.name - resource_group_name = azurerm_cosmosdb_sql_trigger.test.resource_group_name - account_name = azurerm_cosmosdb_sql_trigger.test.account_name - container_name = azurerm_cosmosdb_sql_trigger.test.container_name - database_name = azurerm_cosmosdb_sql_trigger.test.database_name - body = "function trigger(){}" - trigger_operation = "All" - trigger_type = "Pre" + name = azurerm_cosmosdb_sql_trigger.test.name + container_id = azurerm_cosmosdb_sql_trigger.test.container_id + body = "function trigger(){}" + operation = "All" + type = "Pre" } `, config) } @@ -167,20 +168,17 @@ func (r CosmosDbSQLTriggerResource) update(data acceptance.TestData) string { %s resource "azurerm_cosmosdb_sql_trigger" "test" { - name = "acctest-%d" - resource_group_name = azurerm_resource_group.test.name - account_name = azurerm_cosmosdb_account.test.name - container_name = azurerm_cosmosdb_sql_container.test.name - database_name = azurerm_cosmosdb_sql_database.test.name - body = <