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

add partition_key_version attribute to resource azurerm_cosmosdb_sql_container #9496

Merged
merged 9 commits into from
Dec 7, 2020
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,13 @@ func resourceArmCosmosDbSQLContainer() *schema.Resource {
ValidateFunc: validation.StringIsNotEmpty,
},

"partition_key_version": {
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
ValidateFunc: validation.IntBetween(1, 2),
},

"throughput": {
Type: schema.TypeInt,
Optional: true,
Expand Down Expand Up @@ -162,6 +169,10 @@ func resourceArmCosmosDbSQLContainerCreate(d *schema.ResourceData, meta interfac
Paths: &[]string{partitionkeypaths},
Kind: documentdb.PartitionKindHash,
}

if partitionKeyVersion, ok := d.GetOk("partition_key_version"); ok {
db.SQLContainerCreateUpdateProperties.Resource.PartitionKey.Version = utils.Int32(int32(partitionKeyVersion.(int)))
}
}

if keys := expandCosmosSQLContainerUniqueKeys(d.Get("unique_key").(*schema.Set)); keys != nil {
Expand Down Expand Up @@ -245,6 +256,10 @@ func resourceArmCosmosDbSQLContainerUpdate(d *schema.ResourceData, meta interfac
Paths: &[]string{partitionkeypaths},
Kind: documentdb.PartitionKindHash,
}

if partitionKeyVersion, ok := d.GetOk("partition_key_version"); ok {
db.SQLContainerCreateUpdateProperties.Resource.PartitionKey.Version = utils.Int32(int32(partitionKeyVersion.(int)))
}
}

if keys := expandCosmosSQLContainerUniqueKeys(d.Get("unique_key").(*schema.Set)); keys != nil {
Expand Down Expand Up @@ -321,6 +336,9 @@ func resourceArmCosmosDbSQLContainerRead(d *schema.ResourceData, meta interface{
d.Set("partition_key_path", (*paths)[0])
}
}
if version := pk.Version; version != nil {
d.Set("partition_key_version", version)
}
}

if ukp := res.UniqueKeyPolicy; ukp != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,27 @@ func TestAccAzureRMCosmosDbSqlContainer_indexing_policy(t *testing.T) {
})
}

func TestAccAzureRMCosmosDbSqlContainer_partition_key_version(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_cosmosdb_sql_container", "test")

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.PreCheck(t) },
Providers: acceptance.SupportedProviders,
CheckDestroy: testCheckAzureRMCosmosDbSqlContainerDestroy,
Steps: []resource.TestStep{
{

Config: testAccAzureRMCosmosDbSqlContainer_partition_key_version(data, 2),
Check: resource.ComposeAggregateTestCheckFunc(
testCheckAzureRMCosmosDbSqlContainerExists(data.ResourceName),
resource.TestCheckResourceAttr(data.ResourceName, "partition_key_version", "2"),
),
},
data.ImportStep(),
},
})
}

func testCheckAzureRMCosmosDbSqlContainerDestroy(s *terraform.State) error {
client := acceptance.AzureProvider.Meta().(*clients.Client).Cosmos.SqlClient
ctx := acceptance.AzureProvider.Meta().(*clients.Client).StopContext
Expand Down Expand Up @@ -440,3 +461,17 @@ resource "azurerm_cosmosdb_sql_container" "test" {
}
`, testAccAzureRMCosmosDbSqlDatabase_basic(data), data.RandomInteger, includedPath, excludedPath)
}

func testAccAzureRMCosmosDbSqlContainer_partition_key_version(data acceptance.TestData, version int) string {
return fmt.Sprintf(`
%[1]s
resource "azurerm_cosmosdb_sql_container" "test" {
name = "acctest-CSQLC-%[2]d"
resource_group_name = azurerm_cosmosdb_account.test.resource_group_name
account_name = azurerm_cosmosdb_account.test.name
database_name = azurerm_cosmosdb_sql_database.test.name
partition_key_path = "/definition/id"
partition_key_version = %[3]d
}
`, testAccAzureRMCosmosDbSqlDatabase_basic(data), data.RandomInteger, version)
}
15 changes: 9 additions & 6 deletions website/docs/r/cosmosdb_sql_container.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ Manages a SQL Container within a Cosmos DB Account.

```hcl
resource "azurerm_cosmosdb_sql_container" "example" {
name = "example-container"
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
partition_key_path = "/definition/id"
throughput = 400
name = "example-container"
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
partition_key_path = "/definition/id"
partition_key_version = 1
throughput = 400

indexing_policy {
indexing_mode = "Consistent"
Expand Down Expand Up @@ -57,6 +58,8 @@ The following arguments are supported:

* `partition_key_path` - (Optional) Define a partition key. Changing this forces a new resource to be created.

* `partition_key_version` - (Optional) Define a partition key version. Changing this forces a new resource to be created. Possible values are `1 `and `2`. This should be set to `2` in order to use large partition keys.

* `unique_key` - (Optional) One or more `unique_key` blocks as defined below. Changing this forces a new resource to be created.

* `throughput` - (Optional) The throughput of SQL container (RU/s). Must be set in increments of `100`. The minimum value is `400`. This must be set upon container creation otherwise it cannot be updated without a manual terraform destroy-apply.
Expand Down