Skip to content

Commit

Permalink
use container_id to get all necessary parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
ms-henglu committed May 6, 2021
1 parent fe791da commit 2a8cccc
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 74 deletions.
48 changes: 15 additions & 33 deletions azurerm/internal/services/cosmos/cosmosdb_sql_trigger_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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": {
Expand All @@ -74,7 +57,7 @@ func resourceCosmosDbSQLTrigger() *schema.Resource {
ValidateFunc: validation.StringIsNotEmpty,
},

"trigger_operation": {
"operation": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
Expand All @@ -86,7 +69,7 @@ func resourceCosmosDbSQLTrigger() *schema.Resource {
}, false),
},

"trigger_type": {
"type": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
Expand All @@ -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)

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
})
}

Expand Down Expand Up @@ -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)
}
Expand All @@ -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_container.test.id
body = "function trigger(){}"
operation = "All"
type = "Pre"
}
`, config)
}
Expand All @@ -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 = <<BODY
name = "acctest-%d"
container_id = azurerm_cosmosdb_sql_container.test.id
body = <<BODY
function test() {
var context = getContext();
var response = context.getResponse();
response.setBody('Hello, World');
}
BODY
trigger_operation = "All"
trigger_type = "Pre"
operation = "All"
type = "Pre"
}
`, template, data.RandomInteger)
}
25 changes: 8 additions & 17 deletions website/docs/r/cosmosdb_sql_trigger.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,11 @@ resource "azurerm_cosmosdb_sql_container" "example" {
}
resource "azurerm_cosmosdb_sql_trigger" "example" {
name = "test-trigger"
resource_group_name = azurerm_cosmosdb_account.example.resource_group_name
account_name = azurerm_cosmosdb_account.example.name
database_name = azurerm_cosmosdb_sql_database.example.name
container_name = azurerm_cosmosdb_sql_container.example.name
body = "function trigger(){}"
trigger_operation = "Delete"
trigger_type = "Post"
name = "test-trigger"
container_id = azurerm_cosmosdb_sql_container.example.id
body = "function trigger(){}"
operation = "Delete"
type = "Post"
}
```

Expand All @@ -50,19 +47,13 @@ resource "azurerm_cosmosdb_sql_trigger" "example" {
The following arguments are supported:
* `name` - (Required) The name which should be used for this SQL Trigger. Changing this forces a new SQL Trigger to be created.

* `resource_group_name` - (Required) The name of the Resource Group where the SQL Trigger should exist. Changing this forces a new SQL Trigger to be created.

* `account_name` - (Required) The name of the Cosmos DB Account to create the SQL Trigger within. Changing this forces a new SQL Trigger to be created.

* `container_name` - (Required) The name of the Cosmos DB SQL Container to create the SQL Trigger within. Changing this forces a new SQL Trigger to be created.
* `container_id` - (Required) The id of the Cosmos DB SQL Container to create the SQL Trigger within. Changing this forces a new SQL Trigger to be created.

* `database_name` - (Required) The name of the Cosmos DB SQL Database to create the SQL Trigger within. Changing this forces a new SQL Trigger to be created.

* `body` - (Required) Body of the Trigger.

* `trigger_operation` - (Required) The operation the trigger is associated with. Possible values are `All`, `Create`, `Update`, `Delete` and `Replace`.
* `operation` - (Required) The operation the trigger is associated with. Possible values are `All`, `Create`, `Update`, `Delete` and `Replace`.

* `trigger_type` - (Required) Type of the Trigger. Possible values are `Pre` and `Post`.
* `type` - (Required) Type of the Trigger. Possible values are `Pre` and `Post`.

## Attributes Reference

Expand Down

0 comments on commit 2a8cccc

Please sign in to comment.