From 64e145b4017bffddbcf70adb08e880923c412124 Mon Sep 17 00:00:00 2001 From: neil-yechenwei Date: Mon, 16 May 2022 14:51:01 +0800 Subject: [PATCH 01/10] azurerm_cosmosdb_cassandra_datacenter - support new properties --- .../cosmosdb_cassandra_datacenter_resource.go | 90 ++++-- ...osdb_cassandra_datacenter_resource_test.go | 266 ++++++++++++++++-- ...osmosdb_cassandra_datacenter.html.markdown | 12 +- 3 files changed, 317 insertions(+), 51 deletions(-) diff --git a/internal/services/cosmos/cosmosdb_cassandra_datacenter_resource.go b/internal/services/cosmos/cosmosdb_cassandra_datacenter_resource.go index f32ecef312e6..0f4f296a72b5 100644 --- a/internal/services/cosmos/cosmosdb_cassandra_datacenter_resource.go +++ b/internal/services/cosmos/cosmosdb_cassandra_datacenter_resource.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "log" - "strconv" "time" "github.com/Azure/azure-sdk-for-go/services/cosmos-db/mgmt/2021-10-15/documentdb" @@ -16,6 +15,7 @@ import ( "github.com/hashicorp/terraform-provider-azurerm/internal/clients" "github.com/hashicorp/terraform-provider-azurerm/internal/services/cosmos/parse" "github.com/hashicorp/terraform-provider-azurerm/internal/services/cosmos/validate" + keyVaultValidate "github.com/hashicorp/terraform-provider-azurerm/internal/services/keyvault/validate" networkValidate "github.com/hashicorp/terraform-provider-azurerm/internal/services/network/validate" "github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" "github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation" @@ -36,10 +36,10 @@ func resourceCassandraDatacenter() *pluginsdk.Resource { }), Timeouts: &pluginsdk.ResourceTimeout{ - Create: pluginsdk.DefaultTimeout(30 * time.Minute), + Create: pluginsdk.DefaultTimeout(60 * time.Minute), Read: pluginsdk.DefaultTimeout(5 * time.Minute), - Update: pluginsdk.DefaultTimeout(30 * time.Minute), - Delete: pluginsdk.DefaultTimeout(30 * time.Minute), + Update: pluginsdk.DefaultTimeout(60 * time.Minute), + Delete: pluginsdk.DefaultTimeout(60 * time.Minute), }, Schema: map[string]*pluginsdk.Schema{ @@ -66,6 +66,25 @@ func resourceCassandraDatacenter() *pluginsdk.Resource { ValidateFunc: networkValidate.SubnetID, }, + "backup_storage_customer_key_uri": { + Type: pluginsdk.TypeString, + Optional: true, + ValidateFunc: keyVaultValidate.NestedItemId, + }, + + "base64_encoded_yaml_fragment": { + Type: pluginsdk.TypeString, + Optional: true, + ValidateFunc: validation.StringIsNotEmpty, + }, + + "disk_sku": { + Type: pluginsdk.TypeString, + Optional: true, + Default: "P30", + ValidateFunc: validation.StringIsNotEmpty, + }, + "node_count": { Type: pluginsdk.TypeInt, Optional: true, @@ -116,10 +135,19 @@ func resourceCassandraDatacenterCreate(d *pluginsdk.ResourceData, meta interface Sku: utils.String(d.Get("sku_name").(string)), AvailabilityZone: utils.Bool(d.Get("availability_zones_enabled").(bool)), DiskCapacity: utils.Int32(int32(d.Get("disk_count").(int))), + DiskSku: utils.String(d.Get("disk_sku").(string)), DataCenterLocation: utils.String(azure.NormalizeLocation(d.Get("location").(string))), }, } + if v, ok := d.GetOk("backup_storage_customer_key_uri"); ok { + body.Properties.BackupStorageCustomerKeyURI = utils.String(v.(string)) + } + + if v, ok := d.GetOk("base64_encoded_yaml_fragment"); ok { + body.Properties.Base64EncodedCassandraYamlFragment = utils.String(v.(string)) + } + future, err := client.CreateUpdate(ctx, id.ResourceGroup, id.CassandraClusterName, id.DataCenterName, body) if err != nil { return fmt.Errorf("creating %q: %+v", id, err) @@ -161,8 +189,11 @@ func resourceCassandraDatacenterRead(d *pluginsdk.ResourceData, meta interface{} if res := props; res != nil { d.Set("delegated_management_subnet_id", props.DelegatedSubnetID) d.Set("location", location.NormalizeNilable(props.DataCenterLocation)) + d.Set("backup_storage_customer_key_uri", props.BackupStorageCustomerKeyURI) + d.Set("base64_encoded_yaml_fragment", props.Base64EncodedCassandraYamlFragment) d.Set("node_count", props.NodeCount) d.Set("disk_count", int(*props.DiskCapacity)) + d.Set("disk_sku", props.DiskSku) d.Set("sku_name", props.Sku) d.Set("availability_zones_enabled", props.AvailabilityZone) } @@ -185,9 +216,18 @@ func resourceCassandraDatacenterUpdate(d *pluginsdk.ResourceData, meta interface DelegatedSubnetID: utils.String(d.Get("delegated_management_subnet_id").(string)), NodeCount: utils.Int32(int32(d.Get("node_count").(int))), DataCenterLocation: utils.String(azure.NormalizeLocation(d.Get("location").(string))), + DiskSku: utils.String(d.Get("disk_sku").(string)), }, } + if v, ok := d.GetOk("backup_storage_customer_key_uri"); ok { + body.Properties.BackupStorageCustomerKeyURI = utils.String(v.(string)) + } + + if v, ok := d.GetOk("base64_encoded_yaml_fragment"); ok { + body.Properties.Base64EncodedCassandraYamlFragment = utils.String(v.(string)) + } + future, err := client.CreateUpdate(ctx, id.ResourceGroup, id.CassandraClusterName, id.DataCenterName, body) if err != nil { return fmt.Errorf("updating %q: %+v", id, err) @@ -197,21 +237,21 @@ func resourceCassandraDatacenterUpdate(d *pluginsdk.ResourceData, meta interface return fmt.Errorf("waiting on update for %q: %+v", id, err) } - // In case the node_count is changed, we need to further poll the node count until the update takes effect. - if d.HasChange("node_count") { - oldNodeCountRaw, _ := d.GetChange("node_count") - oldNodeCount := oldNodeCountRaw.(int) - stateConf := &pluginsdk.StateChangeConf{ - Pending: []string{"-1", strconv.Itoa(oldNodeCount)}, - Target: []string{strconv.Itoa(d.Get("node_count").(int))}, - Refresh: cassandraDatacenterStateRefreshFunc(ctx, client, *id), - MinTimeout: 30 * time.Second, - Timeout: d.Timeout(pluginsdk.TimeoutUpdate), - } + // Issue: https://github.com/Azure/azure-rest-api-specs/issues/19078 + // There is a long running issue on updating this resource. + // The API cannot update the property after WaitForCompletionRef is returned. + // It has to wait a while after that. Then the property can be updated successfully. + stateConf := &pluginsdk.StateChangeConf{ + Delay: 1 * time.Minute, + Pending: []string{string(documentdb.ManagedCassandraProvisioningStateUpdating)}, + Target: []string{string(documentdb.ManagedCassandraProvisioningStateSucceeded)}, + Refresh: cassandraDatacenterStateRefreshFunc(ctx, client, *id), + MinTimeout: 15 * time.Second, + Timeout: d.Timeout(pluginsdk.TimeoutUpdate), + } - if _, err = stateConf.WaitForStateContext(ctx); err != nil { - return fmt.Errorf("waiting for the updating of node_count in %q to take effect: %+v", id, err) - } + if _, err := stateConf.WaitForStateContext(ctx); err != nil { + return fmt.Errorf("waiting for update of %s: %+v", id, err) } return resourceCassandraDatacenterRead(d, meta) @@ -244,18 +284,14 @@ func resourceCassandraDatacenterDelete(d *pluginsdk.ResourceData, meta interface func cassandraDatacenterStateRefreshFunc(ctx context.Context, client *documentdb.CassandraDataCentersClient, id parse.CassandraDatacenterId) pluginsdk.StateRefreshFunc { return func() (interface{}, string, error) { - nodeCount := "-1" - resp, err := client.Get(ctx, id.ResourceGroup, id.CassandraClusterName, id.DataCenterName) + res, err := client.Get(ctx, id.ResourceGroup, id.CassandraClusterName, id.DataCenterName) if err != nil { - return resp, nodeCount, fmt.Errorf("retrieving %q while waiting for node count to update: %+v", id, err) + return nil, "", fmt.Errorf("polling for %s: %+v", id, err) } - if props := resp.Properties; props != nil { - if props.NodeCount != nil { - nodeCount = strconv.Itoa(int(*props.NodeCount)) - } + if res.Properties != nil && res.Properties.ProvisioningState != "" { + return res, string(res.Properties.ProvisioningState), nil } - - return resp, nodeCount, nil + return nil, "", fmt.Errorf("unable to read provisioning state") } } diff --git a/internal/services/cosmos/cosmosdb_cassandra_datacenter_resource_test.go b/internal/services/cosmos/cosmosdb_cassandra_datacenter_resource_test.go index 39b572a357b5..5f6e3a1075ef 100644 --- a/internal/services/cosmos/cosmosdb_cassandra_datacenter_resource_test.go +++ b/internal/services/cosmos/cosmosdb_cassandra_datacenter_resource_test.go @@ -30,20 +30,35 @@ func TestAccCassandraDatacenter_basic(t *testing.T) { }) } +func TestAccCassandraDatacenter_complete(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_cosmosdb_cassandra_datacenter", "test") + r := CassandraDatacenterResource{} + + data.ResourceTest(t, r, []acceptance.TestStep{ + { + Config: r.complete(data, 3), + Check: acceptance.ComposeAggregateTestCheckFunc( + check.That(data.ResourceName).ExistsInAzure(r), + ), + }, + data.ImportStep(), + }) +} + func TestAccCassandraDatacenter_update(t *testing.T) { data := acceptance.BuildTestData(t, "azurerm_cosmosdb_cassandra_datacenter", "test") r := CassandraDatacenterResource{} data.ResourceTest(t, r, []acceptance.TestStep{ { - Config: r.basic(data, 3), + Config: r.complete(data, 3), Check: acceptance.ComposeAggregateTestCheckFunc( check.That(data.ResourceName).ExistsInAzure(r), ), }, data.ImportStep(), { - Config: r.basic(data, 5), + Config: r.update(data, 5), Check: acceptance.ComposeAggregateTestCheckFunc( check.That(data.ResourceName).ExistsInAzure(r), ), @@ -66,26 +81,246 @@ func (t CassandraDatacenterResource) Exists(ctx context.Context, clients *client return utils.Bool(resp.ID != nil), nil } -func (CassandraDatacenterResource) basic(data acceptance.TestData, nodeCount int) string { +func (r CassandraDatacenterResource) basic(data acceptance.TestData, nodeCount int) string { + return fmt.Sprintf(` +%s + +resource "azurerm_cosmosdb_cassandra_datacenter" "test" { + name = "acctca-mi-dc-%d" + cassandra_cluster_id = azurerm_cosmosdb_cassandra_cluster.test.id + location = azurerm_cosmosdb_cassandra_cluster.test.location + delegated_management_subnet_id = azurerm_subnet.test.id + node_count = %d + disk_count = 4 + sku_name = "Standard_DS14_v2" + availability_zones_enabled = false +} +`, r.template(data), data.RandomInteger, nodeCount) +} + +func (r CassandraDatacenterResource) complete(data acceptance.TestData, nodeCount int) string { + return fmt.Sprintf(` +%s + +resource "azurerm_key_vault" "test" { + name = "acctestkv-%d" + location = azurerm_resource_group.test.location + resource_group_name = azurerm_resource_group.test.name + tenant_id = data.azurerm_client_config.current.tenant_id + sku_name = "standard" + soft_delete_retention_days = 7 + purge_protection_enabled = true +} + +resource "azurerm_key_vault_access_policy" "current_user" { + key_vault_id = azurerm_key_vault.test.id + + tenant_id = azurerm_key_vault.test.tenant_id + object_id = data.azurerm_client_config.current.object_id + + key_permissions = [ + "Create", + "Delete", + "Get", + "Purge", + "Recover", + "Update", + "WrapKey", + "UnwrapKey" + ] +} + +resource "azurerm_key_vault_access_policy" "system_identity" { + key_vault_id = azurerm_key_vault.test.id + + tenant_id = azurerm_key_vault.test.tenant_id + object_id = azurerm_cosmosdb_cassandra_cluster.test.identity.0.principal_id + + key_permissions = [ + "Create", + "Delete", + "Get", + "Purge", + "Recover", + "Update", + "WrapKey", + "UnwrapKey" + ] +} + +resource "azurerm_key_vault_key" "test" { + name = "acctestkey-%d" + key_vault_id = azurerm_key_vault.test.id + key_type = "RSA" + key_size = 2048 + + key_opts = [ + "decrypt", + "encrypt", + "sign", + "unwrapKey", + "verify", + "wrapKey", + ] + + depends_on = [ + azurerm_key_vault_access_policy.current_user, + azurerm_key_vault_access_policy.system_identity + ] +} + +resource "azurerm_cosmosdb_cassandra_datacenter" "test" { + name = "acctca-mi-dc-%d" + cassandra_cluster_id = azurerm_cosmosdb_cassandra_cluster.test.id + location = azurerm_cosmosdb_cassandra_cluster.test.location + delegated_management_subnet_id = azurerm_subnet.test.id + node_count = %d + disk_count = 4 + sku_name = "Standard_DS14_v2" + availability_zones_enabled = false + disk_sku = "P30" + backup_storage_customer_key_uri = azurerm_key_vault_key.test.id + base64_encoded_yaml_fragment = "Y29tcGFjdGlvbl90aHJvdWdocHV0X21iX3Blcl9zZWM6IDMyCmNvbXBhY3Rpb25fbGFyZ2VfcGFydGl0aW9uX3dhcm5pbmdfdGhyZXNob2xkX21iOiAxMDA=" + + depends_on = [ + azurerm_key_vault_key.test + ] +} +`, r.template(data), data.RandomInteger, data.RandomInteger, data.RandomInteger, nodeCount) +} + +func (r CassandraDatacenterResource) update(data acceptance.TestData, nodeCount int) string { + return fmt.Sprintf(` +%s + +resource "azurerm_key_vault" "test" { + name = "acctestkv-%d" + location = azurerm_resource_group.test.location + resource_group_name = azurerm_resource_group.test.name + tenant_id = data.azurerm_client_config.current.tenant_id + sku_name = "standard" + soft_delete_retention_days = 7 + purge_protection_enabled = true +} + +resource "azurerm_key_vault_access_policy" "current_user" { + key_vault_id = azurerm_key_vault.test.id + + tenant_id = azurerm_key_vault.test.tenant_id + object_id = data.azurerm_client_config.current.object_id + + key_permissions = [ + "Create", + "Delete", + "Get", + "Purge", + "Recover", + "Update", + "WrapKey", + "UnwrapKey" + ] +} + +resource "azurerm_key_vault_access_policy" "system_identity" { + key_vault_id = azurerm_key_vault.test.id + + tenant_id = azurerm_key_vault.test.tenant_id + object_id = azurerm_cosmosdb_cassandra_cluster.test.identity.0.principal_id + + key_permissions = [ + "Create", + "Delete", + "Get", + "Purge", + "Recover", + "Update", + "WrapKey", + "UnwrapKey" + ] +} + +resource "azurerm_key_vault_key" "test" { + name = "acctestkey-%d" + key_vault_id = azurerm_key_vault.test.id + key_type = "RSA" + key_size = 2048 + + key_opts = [ + "decrypt", + "encrypt", + "sign", + "unwrapKey", + "verify", + "wrapKey", + ] + + depends_on = [ + azurerm_key_vault_access_policy.current_user, + azurerm_key_vault_access_policy.system_identity + ] +} + +resource "azurerm_key_vault_key" "test2" { + name = "acctestkey2-%d" + key_vault_id = azurerm_key_vault.test.id + key_type = "RSA" + key_size = 2048 + + key_opts = [ + "decrypt", + "encrypt", + "sign", + "unwrapKey", + "verify", + "wrapKey", + ] + + depends_on = [azurerm_key_vault_key.test] +} + +resource "azurerm_cosmosdb_cassandra_datacenter" "test" { + name = "acctca-mi-dc-%d" + cassandra_cluster_id = azurerm_cosmosdb_cassandra_cluster.test.id + location = azurerm_cosmosdb_cassandra_cluster.test.location + delegated_management_subnet_id = azurerm_subnet.test.id + node_count = %d + disk_count = 4 + sku_name = "Standard_DS14_v2" + availability_zones_enabled = false + base64_encoded_yaml_fragment = "Z29tcGFjdGlvbl90aHJvdWdocHV0X21iX3Blcl9zZWM6IDMyCmNvbXBhY3Rpb25fbGFyZ2VfcGFydGl0aW9uX3dhcm5pbmdfdGhyZXNob2xkX21iOiAxMDA=" + + depends_on = [ + azurerm_key_vault_key.test, + azurerm_key_vault_key.test2 + ] +} +`, r.template(data), data.RandomInteger, data.RandomInteger, data.RandomInteger, data.RandomInteger, nodeCount) +} + +func (CassandraDatacenterResource) template(data acceptance.TestData) string { return fmt.Sprintf(` provider "azurerm" { - features {} + features { + key_vault { + purge_soft_delete_on_destroy = false + } + } } resource "azurerm_resource_group" "test" { - name = "acctestRG-ca-%[1]d" - location = "%[2]s" + name = "acctestRG-ca-%d" + location = "%s" } resource "azurerm_virtual_network" "test" { - name = "acctvn-%[1]d" + name = "acctvn-%d" location = azurerm_resource_group.test.location resource_group_name = azurerm_resource_group.test.name address_space = ["10.0.0.0/16"] } resource "azurerm_subnet" "test" { - name = "acctsub-%[1]d" + name = "acctsub-%d" resource_group_name = azurerm_resource_group.test.name virtual_network_name = azurerm_virtual_network.test.name address_prefixes = ["10.0.1.0/24"] @@ -98,22 +333,11 @@ resource "azurerm_role_assignment" "test" { } resource "azurerm_cosmosdb_cassandra_cluster" "test" { - name = "acctca-mi-cluster-%[1]d" + name = "acctca-mi-cluster-%d" resource_group_name = azurerm_resource_group.test.name location = azurerm_resource_group.test.location delegated_management_subnet_id = azurerm_subnet.test.id default_admin_password = "Password1234" } - -resource "azurerm_cosmosdb_cassandra_datacenter" "test" { - name = "acctca-mi-dc-%[1]d" - cassandra_cluster_id = azurerm_cosmosdb_cassandra_cluster.test.id - location = azurerm_cosmosdb_cassandra_cluster.test.location - delegated_management_subnet_id = azurerm_subnet.test.id - node_count = %[3]d - disk_count = 4 - sku_name = "Standard_DS14_v2" - availability_zones_enabled = false -} -`, data.RandomInteger, data.Locations.Secondary, nodeCount) +`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger, data.RandomInteger) } diff --git a/website/docs/r/cosmosdb_cassandra_datacenter.html.markdown b/website/docs/r/cosmosdb_cassandra_datacenter.html.markdown index 8b32408a7928..f2f290742d9b 100644 --- a/website/docs/r/cosmosdb_cassandra_datacenter.html.markdown +++ b/website/docs/r/cosmosdb_cassandra_datacenter.html.markdown @@ -78,6 +78,12 @@ The following arguments are supported: --- +* `backup_storage_customer_key_uri` - (Optional) The key URI of the customer key to use for the encryption of the backup Storage Account. + +* `base64_encoded_yaml_fragment` - (Optional) The fragment of the cassandra.yaml configuration file to be included in the cassandra.yaml for all nodes in this Cassandra Datacenter. The fragment should be Base64 encoded and only a subset of keys is allowed. + +* `disk_sku` - (Optional) The Disk SKU that is used for this Cassandra Datacenter. Defaults to `P30`. + * `sku_name` - (Optional) Determines the selected sku. Defaults to Standard_DS14_v2. * `disk_count` - (Optional) Determines the number of p30 disks that are attached to each node. Defaults to `4`. @@ -94,10 +100,10 @@ In addition to the Arguments listed above - the following Attributes are exporte 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 Cassandra Datacenter. +* `create` - (Defaults to 60 minutes) Used when creating the Cassandra Datacenter. * `read` - (Defaults to 5 minutes) Used when retrieving the Cassandra Datacenter. -* `update` - (Defaults to 30 minutes) Used when updating the Cassandra Datacenter. -* `delete` - (Defaults to 30 minutes) Used when deleting the Cassandra Datacenter. +* `update` - (Defaults to 60 minutes) Used when updating the Cassandra Datacenter. +* `delete` - (Defaults to 60 minutes) Used when deleting the Cassandra Datacenter. ## Import From f5ad2455bd23ae2e981a415618b6c412bf442830 Mon Sep 17 00:00:00 2001 From: neil-yechenwei Date: Mon, 16 May 2022 16:09:02 +0800 Subject: [PATCH 02/10] update code --- ...osdb_cassandra_datacenter_resource_test.go | 30 +++++++++---------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/internal/services/cosmos/cosmosdb_cassandra_datacenter_resource_test.go b/internal/services/cosmos/cosmosdb_cassandra_datacenter_resource_test.go index 5f6e3a1075ef..0db23e8426cb 100644 --- a/internal/services/cosmos/cosmosdb_cassandra_datacenter_resource_test.go +++ b/internal/services/cosmos/cosmosdb_cassandra_datacenter_resource_test.go @@ -163,10 +163,10 @@ resource "azurerm_key_vault_key" "test" { "wrapKey", ] - depends_on = [ - azurerm_key_vault_access_policy.current_user, - azurerm_key_vault_access_policy.system_identity - ] + depends_on = [ + azurerm_key_vault_access_policy.current_user, + azurerm_key_vault_access_policy.system_identity + ] } resource "azurerm_cosmosdb_cassandra_datacenter" "test" { @@ -180,11 +180,11 @@ resource "azurerm_cosmosdb_cassandra_datacenter" "test" { availability_zones_enabled = false disk_sku = "P30" backup_storage_customer_key_uri = azurerm_key_vault_key.test.id - base64_encoded_yaml_fragment = "Y29tcGFjdGlvbl90aHJvdWdocHV0X21iX3Blcl9zZWM6IDMyCmNvbXBhY3Rpb25fbGFyZ2VfcGFydGl0aW9uX3dhcm5pbmdfdGhyZXNob2xkX21iOiAxMDA=" + base64_encoded_yaml_fragment = "Y29tcGFjdGlvbl90aHJvdWdocHV0X21iX3Blcl9zZWM6IDMyCmNvbXBhY3Rpb25fbGFyZ2VfcGFydGl0aW9uX3dhcm5pbmdfdGhyZXNob2xkX21iOiAxMDA=" depends_on = [ - azurerm_key_vault_key.test - ] + azurerm_key_vault_key.test + ] } `, r.template(data), data.RandomInteger, data.RandomInteger, data.RandomInteger, nodeCount) } @@ -254,10 +254,10 @@ resource "azurerm_key_vault_key" "test" { "wrapKey", ] - depends_on = [ - azurerm_key_vault_access_policy.current_user, - azurerm_key_vault_access_policy.system_identity - ] + depends_on = [ + azurerm_key_vault_access_policy.current_user, + azurerm_key_vault_access_policy.system_identity + ] } resource "azurerm_key_vault_key" "test2" { @@ -275,7 +275,7 @@ resource "azurerm_key_vault_key" "test2" { "wrapKey", ] - depends_on = [azurerm_key_vault_key.test] + depends_on = [azurerm_key_vault_key.test] } resource "azurerm_cosmosdb_cassandra_datacenter" "test" { @@ -290,9 +290,9 @@ resource "azurerm_cosmosdb_cassandra_datacenter" "test" { base64_encoded_yaml_fragment = "Z29tcGFjdGlvbl90aHJvdWdocHV0X21iX3Blcl9zZWM6IDMyCmNvbXBhY3Rpb25fbGFyZ2VfcGFydGl0aW9uX3dhcm5pbmdfdGhyZXNob2xkX21iOiAxMDA=" depends_on = [ - azurerm_key_vault_key.test, - azurerm_key_vault_key.test2 - ] + azurerm_key_vault_key.test, + azurerm_key_vault_key.test2 + ] } `, r.template(data), data.RandomInteger, data.RandomInteger, data.RandomInteger, data.RandomInteger, nodeCount) } From eea1d2c4c33a44f16dfef138589dff9296c2920a Mon Sep 17 00:00:00 2001 From: neil-yechenwei Date: Wed, 18 May 2022 09:33:46 +0800 Subject: [PATCH 03/10] update code --- .../cosmos/cosmosdb_cassandra_datacenter_resource_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/internal/services/cosmos/cosmosdb_cassandra_datacenter_resource_test.go b/internal/services/cosmos/cosmosdb_cassandra_datacenter_resource_test.go index e2281785f29d..fdaa86e5ab5a 100644 --- a/internal/services/cosmos/cosmosdb_cassandra_datacenter_resource_test.go +++ b/internal/services/cosmos/cosmosdb_cassandra_datacenter_resource_test.go @@ -19,7 +19,7 @@ func testAccCassandraDatacenter_basic(t *testing.T) { data := acceptance.BuildTestData(t, "azurerm_cosmosdb_cassandra_datacenter", "test") r := CassandraDatacenterResource{} - data.ResourceTest(t, r, []acceptance.TestStep{ + data.ResourceSequentialTest(t, r, []acceptance.TestStep{ { Config: r.basic(data, 3), Check: acceptance.ComposeAggregateTestCheckFunc( @@ -34,7 +34,7 @@ func testAccCassandraDatacenter_complete(t *testing.T) { data := acceptance.BuildTestData(t, "azurerm_cosmosdb_cassandra_datacenter", "test") r := CassandraDatacenterResource{} - data.ResourceTest(t, r, []acceptance.TestStep{ + data.ResourceSequentialTest(t, r, []acceptance.TestStep{ { Config: r.complete(data, 3), Check: acceptance.ComposeAggregateTestCheckFunc( @@ -49,7 +49,7 @@ func testAccCassandraDatacenter_update(t *testing.T) { data := acceptance.BuildTestData(t, "azurerm_cosmosdb_cassandra_datacenter", "test") r := CassandraDatacenterResource{} - data.ResourceTest(t, r, []acceptance.TestStep{ + data.ResourceSequentialTest(t, r, []acceptance.TestStep{ { Config: r.complete(data, 3), Check: acceptance.ComposeAggregateTestCheckFunc( From 9c419b056a3fc5c750009b0bb83a0154972f478c Mon Sep 17 00:00:00 2001 From: neil-yechenwei Date: Wed, 18 May 2022 13:59:47 +0800 Subject: [PATCH 04/10] update code --- .../network/virtual_network_gateway_nat_rule_resource.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/services/network/virtual_network_gateway_nat_rule_resource.go b/internal/services/network/virtual_network_gateway_nat_rule_resource.go index 8eb54509771b..7d77a96848cd 100644 --- a/internal/services/network/virtual_network_gateway_nat_rule_resource.go +++ b/internal/services/network/virtual_network_gateway_nat_rule_resource.go @@ -5,7 +5,7 @@ import ( "log" "time" - "github.com/Azure/azure-sdk-for-go/services/network/mgmt/2021-05-01/network" + "github.com/Azure/azure-sdk-for-go/services/network/mgmt/2021-08-01/network" "github.com/hashicorp/terraform-provider-azurerm/helpers/azure" "github.com/hashicorp/terraform-provider-azurerm/helpers/tf" "github.com/hashicorp/terraform-provider-azurerm/internal/clients" From c6140f29b713daf498fafc0b22100dfb61569320 Mon Sep 17 00:00:00 2001 From: neil-yechenwei Date: Wed, 18 May 2022 14:03:09 +0800 Subject: [PATCH 05/10] update code --- .../services/cosmos/cosmosdb_cassandra_resource_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/internal/services/cosmos/cosmosdb_cassandra_resource_test.go b/internal/services/cosmos/cosmosdb_cassandra_resource_test.go index 96ba78fbf6fe..833f76de637f 100644 --- a/internal/services/cosmos/cosmosdb_cassandra_resource_test.go +++ b/internal/services/cosmos/cosmosdb_cassandra_resource_test.go @@ -15,9 +15,9 @@ func TestAccCassandraSequential(t *testing.T) { "tags": testAccCassandraCluster_tags, }, "dataCenter": { - "basic": testAccCassandraDatacenter_basic, - "complete": testAccCassandraDatacenter_complete, - "requiresImport": testAccCassandraDatacenter_update, + "basic": testAccCassandraDatacenter_basic, + "complete": testAccCassandraDatacenter_complete, + "update": testAccCassandraDatacenter_update, }, }) } From 77a278d9c43df0840f4c98bf476c9b50f045febf Mon Sep 17 00:00:00 2001 From: neil-yechenwei Date: Wed, 18 May 2022 14:08:35 +0800 Subject: [PATCH 06/10] update code --- .../cosmosdb_cassandra_datacenter_resource_test.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/internal/services/cosmos/cosmosdb_cassandra_datacenter_resource_test.go b/internal/services/cosmos/cosmosdb_cassandra_datacenter_resource_test.go index fdaa86e5ab5a..1ea4b1789452 100644 --- a/internal/services/cosmos/cosmosdb_cassandra_datacenter_resource_test.go +++ b/internal/services/cosmos/cosmosdb_cassandra_datacenter_resource_test.go @@ -302,7 +302,8 @@ func (CassandraDatacenterResource) template(data acceptance.TestData) string { provider "azurerm" { features { key_vault { - purge_soft_delete_on_destroy = false + purge_soft_delete_on_destroy = false + purge_soft_deleted_keys_on_destroy = false } } } @@ -326,10 +327,14 @@ resource "azurerm_subnet" "test" { address_prefixes = ["10.0.1.0/24"] } +data "azuread_service_principal" "test" { + display_name = "Azure Cosmos DB" +} + resource "azurerm_role_assignment" "test" { scope = azurerm_virtual_network.test.id role_definition_name = "Network Contributor" - principal_id = "255f3c8e-0c3d-4f06-ba9d-2fb68af0faed" + principal_id = data.azuread_service_principal.test.object_id } resource "azurerm_cosmosdb_cassandra_cluster" "test" { From 8441ec69b1bfaec17faff73214c86d28c43ff66b Mon Sep 17 00:00:00 2001 From: neil-yechenwei Date: Wed, 18 May 2022 14:09:30 +0800 Subject: [PATCH 07/10] update code --- .../cosmos/cosmosdb_cassandra_datacenter_resource_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/internal/services/cosmos/cosmosdb_cassandra_datacenter_resource_test.go b/internal/services/cosmos/cosmosdb_cassandra_datacenter_resource_test.go index 1ea4b1789452..56188ae9297b 100644 --- a/internal/services/cosmos/cosmosdb_cassandra_datacenter_resource_test.go +++ b/internal/services/cosmos/cosmosdb_cassandra_datacenter_resource_test.go @@ -343,6 +343,8 @@ resource "azurerm_cosmosdb_cassandra_cluster" "test" { location = azurerm_resource_group.test.location delegated_management_subnet_id = azurerm_subnet.test.id default_admin_password = "Password1234" + + depends_on = [azurerm_role_assignment.test] } `, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger, data.RandomInteger) } From e30b1e9347d22aa983b41a293bd030e7b2f382aa Mon Sep 17 00:00:00 2001 From: neil-yechenwei Date: Thu, 9 Jun 2022 12:32:44 +0800 Subject: [PATCH 08/10] update code --- ...osdb_cassandra_datacenter_resource_test.go | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/internal/services/cosmos/cosmosdb_cassandra_datacenter_resource_test.go b/internal/services/cosmos/cosmosdb_cassandra_datacenter_resource_test.go index 56188ae9297b..9e1f5203d84f 100644 --- a/internal/services/cosmos/cosmosdb_cassandra_datacenter_resource_test.go +++ b/internal/services/cosmos/cosmosdb_cassandra_datacenter_resource_test.go @@ -102,8 +102,10 @@ func (r CassandraDatacenterResource) complete(data acceptance.TestData, nodeCoun return fmt.Sprintf(` %s +data "azurerm_client_config" "current" {} + resource "azurerm_key_vault" "test" { - name = "acctestkv-%d" + name = "acctestkv-%s" location = azurerm_resource_group.test.location resource_group_name = azurerm_resource_group.test.name tenant_id = data.azurerm_client_config.current.tenant_id @@ -149,7 +151,7 @@ resource "azurerm_key_vault_access_policy" "system_identity" { } resource "azurerm_key_vault_key" "test" { - name = "acctestkey-%d" + name = "acctestkey-%s" key_vault_id = azurerm_key_vault.test.id key_type = "RSA" key_size = 2048 @@ -186,15 +188,17 @@ resource "azurerm_cosmosdb_cassandra_datacenter" "test" { azurerm_key_vault_key.test ] } -`, r.template(data), data.RandomInteger, data.RandomInteger, data.RandomInteger, nodeCount) +`, r.template(data), data.RandomString, data.RandomString, data.RandomInteger, nodeCount) } func (r CassandraDatacenterResource) update(data acceptance.TestData, nodeCount int) string { return fmt.Sprintf(` %s +data "azurerm_client_config" "current" {} + resource "azurerm_key_vault" "test" { - name = "acctestkv-%d" + name = "acctestkv-%s" location = azurerm_resource_group.test.location resource_group_name = azurerm_resource_group.test.name tenant_id = data.azurerm_client_config.current.tenant_id @@ -240,7 +244,7 @@ resource "azurerm_key_vault_access_policy" "system_identity" { } resource "azurerm_key_vault_key" "test" { - name = "acctestkey-%d" + name = "acctestkey-%s" key_vault_id = azurerm_key_vault.test.id key_type = "RSA" key_size = 2048 @@ -261,7 +265,7 @@ resource "azurerm_key_vault_key" "test" { } resource "azurerm_key_vault_key" "test2" { - name = "acctestkey2-%d" + name = "acctestkey2-%s" key_vault_id = azurerm_key_vault.test.id key_type = "RSA" key_size = 2048 @@ -294,7 +298,7 @@ resource "azurerm_cosmosdb_cassandra_datacenter" "test" { azurerm_key_vault_key.test2 ] } -`, r.template(data), data.RandomInteger, data.RandomInteger, data.RandomInteger, data.RandomInteger, nodeCount) +`, r.template(data), data.RandomString, data.RandomString, data.RandomString, data.RandomInteger, nodeCount) } func (CassandraDatacenterResource) template(data acceptance.TestData) string { @@ -344,6 +348,10 @@ resource "azurerm_cosmosdb_cassandra_cluster" "test" { delegated_management_subnet_id = azurerm_subnet.test.id default_admin_password = "Password1234" + identity { + type = "SystemAssigned" + } + depends_on = [azurerm_role_assignment.test] } `, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger, data.RandomInteger) From af299521e3837200f949519df46c987b4099c3e3 Mon Sep 17 00:00:00 2001 From: neil-yechenwei Date: Thu, 9 Jun 2022 13:40:54 +0800 Subject: [PATCH 09/10] update code --- ...osdb_cassandra_datacenter_resource_test.go | 34 ++++++------------- .../cosmosdb_cassandra_resource_test.go | 5 ++- 2 files changed, 12 insertions(+), 27 deletions(-) diff --git a/internal/services/cosmos/cosmosdb_cassandra_datacenter_resource_test.go b/internal/services/cosmos/cosmosdb_cassandra_datacenter_resource_test.go index 9e1f5203d84f..fc5515ff9397 100644 --- a/internal/services/cosmos/cosmosdb_cassandra_datacenter_resource_test.go +++ b/internal/services/cosmos/cosmosdb_cassandra_datacenter_resource_test.go @@ -30,21 +30,6 @@ func testAccCassandraDatacenter_basic(t *testing.T) { }) } -func testAccCassandraDatacenter_complete(t *testing.T) { - data := acceptance.BuildTestData(t, "azurerm_cosmosdb_cassandra_datacenter", "test") - r := CassandraDatacenterResource{} - - data.ResourceSequentialTest(t, r, []acceptance.TestStep{ - { - Config: r.complete(data, 3), - Check: acceptance.ComposeAggregateTestCheckFunc( - check.That(data.ResourceName).ExistsInAzure(r), - ), - }, - data.ImportStep(), - }) -} - func testAccCassandraDatacenter_update(t *testing.T) { data := acceptance.BuildTestData(t, "azurerm_cosmosdb_cassandra_datacenter", "test") r := CassandraDatacenterResource{} @@ -283,15 +268,16 @@ resource "azurerm_key_vault_key" "test2" { } resource "azurerm_cosmosdb_cassandra_datacenter" "test" { - name = "acctca-mi-dc-%d" - cassandra_cluster_id = azurerm_cosmosdb_cassandra_cluster.test.id - location = azurerm_cosmosdb_cassandra_cluster.test.location - delegated_management_subnet_id = azurerm_subnet.test.id - node_count = %d - disk_count = 4 - sku_name = "Standard_DS14_v2" - availability_zones_enabled = false - base64_encoded_yaml_fragment = "Z29tcGFjdGlvbl90aHJvdWdocHV0X21iX3Blcl9zZWM6IDMyCmNvbXBhY3Rpb25fbGFyZ2VfcGFydGl0aW9uX3dhcm5pbmdfdGhyZXNob2xkX21iOiAxMDA=" + name = "acctca-mi-dc-%d" + cassandra_cluster_id = azurerm_cosmosdb_cassandra_cluster.test.id + location = azurerm_cosmosdb_cassandra_cluster.test.location + delegated_management_subnet_id = azurerm_subnet.test.id + node_count = %d + disk_count = 4 + sku_name = "Standard_DS14_v2" + availability_zones_enabled = false + backup_storage_customer_key_uri = azurerm_key_vault_key.test2.id + base64_encoded_yaml_fragment = "Z29tcGFjdGlvbl90aHJvdWdocHV0X21iX3Blcl9zZWM6IDMyCmNvbXBhY3Rpb25fbGFyZ2VfcGFydGl0aW9uX3dhcm5pbmdfdGhyZXNob2xkX21iOiAxMDA=" depends_on = [ azurerm_key_vault_key.test, diff --git a/internal/services/cosmos/cosmosdb_cassandra_resource_test.go b/internal/services/cosmos/cosmosdb_cassandra_resource_test.go index 9d61db6e7efe..aa2029cd63ef 100644 --- a/internal/services/cosmos/cosmosdb_cassandra_resource_test.go +++ b/internal/services/cosmos/cosmosdb_cassandra_resource_test.go @@ -16,9 +16,8 @@ func TestAccCassandraSequential(t *testing.T) { "requiresImport": testAccCassandraCluster_requiresImport, }, "dataCenter": { - "basic": testAccCassandraDatacenter_basic, - "complete": testAccCassandraDatacenter_complete, - "update": testAccCassandraDatacenter_update, + "basic": testAccCassandraDatacenter_basic, + "update": testAccCassandraDatacenter_update, }, }) } From fc2888bedc2449a391b70e3f2a3252d982627475 Mon Sep 17 00:00:00 2001 From: neil-yechenwei Date: Mon, 20 Jun 2022 16:43:05 +0800 Subject: [PATCH 10/10] update code --- .../cosmosdb_cassandra_datacenter_resource.go | 15 +++++++++++++++ ...cosmosdb_cassandra_datacenter_resource_test.go | 2 ++ .../r/cosmosdb_cassandra_datacenter.html.markdown | 2 ++ 3 files changed, 19 insertions(+) diff --git a/internal/services/cosmos/cosmosdb_cassandra_datacenter_resource.go b/internal/services/cosmos/cosmosdb_cassandra_datacenter_resource.go index 0f4f296a72b5..e2cc64b701d7 100644 --- a/internal/services/cosmos/cosmosdb_cassandra_datacenter_resource.go +++ b/internal/services/cosmos/cosmosdb_cassandra_datacenter_resource.go @@ -85,6 +85,12 @@ func resourceCassandraDatacenter() *pluginsdk.Resource { ValidateFunc: validation.StringIsNotEmpty, }, + "managed_disk_customer_key_uri": { + Type: pluginsdk.TypeString, + Optional: true, + ValidateFunc: keyVaultValidate.NestedItemId, + }, + "node_count": { Type: pluginsdk.TypeInt, Optional: true, @@ -148,6 +154,10 @@ func resourceCassandraDatacenterCreate(d *pluginsdk.ResourceData, meta interface body.Properties.Base64EncodedCassandraYamlFragment = utils.String(v.(string)) } + if v, ok := d.GetOk("managed_disk_customer_key_uri"); ok { + body.Properties.ManagedDiskCustomerKeyURI = utils.String(v.(string)) + } + future, err := client.CreateUpdate(ctx, id.ResourceGroup, id.CassandraClusterName, id.DataCenterName, body) if err != nil { return fmt.Errorf("creating %q: %+v", id, err) @@ -191,6 +201,7 @@ func resourceCassandraDatacenterRead(d *pluginsdk.ResourceData, meta interface{} d.Set("location", location.NormalizeNilable(props.DataCenterLocation)) d.Set("backup_storage_customer_key_uri", props.BackupStorageCustomerKeyURI) d.Set("base64_encoded_yaml_fragment", props.Base64EncodedCassandraYamlFragment) + d.Set("managed_disk_customer_key_uri", props.ManagedDiskCustomerKeyURI) d.Set("node_count", props.NodeCount) d.Set("disk_count", int(*props.DiskCapacity)) d.Set("disk_sku", props.DiskSku) @@ -228,6 +239,10 @@ func resourceCassandraDatacenterUpdate(d *pluginsdk.ResourceData, meta interface body.Properties.Base64EncodedCassandraYamlFragment = utils.String(v.(string)) } + if v, ok := d.GetOk("managed_disk_customer_key_uri"); ok { + body.Properties.ManagedDiskCustomerKeyURI = utils.String(v.(string)) + } + future, err := client.CreateUpdate(ctx, id.ResourceGroup, id.CassandraClusterName, id.DataCenterName, body) if err != nil { return fmt.Errorf("updating %q: %+v", id, err) diff --git a/internal/services/cosmos/cosmosdb_cassandra_datacenter_resource_test.go b/internal/services/cosmos/cosmosdb_cassandra_datacenter_resource_test.go index fc5515ff9397..7d61290bb067 100644 --- a/internal/services/cosmos/cosmosdb_cassandra_datacenter_resource_test.go +++ b/internal/services/cosmos/cosmosdb_cassandra_datacenter_resource_test.go @@ -167,6 +167,7 @@ resource "azurerm_cosmosdb_cassandra_datacenter" "test" { availability_zones_enabled = false disk_sku = "P30" backup_storage_customer_key_uri = azurerm_key_vault_key.test.id + managed_disk_customer_key_uri = azurerm_key_vault_key.test.id base64_encoded_yaml_fragment = "Y29tcGFjdGlvbl90aHJvdWdocHV0X21iX3Blcl9zZWM6IDMyCmNvbXBhY3Rpb25fbGFyZ2VfcGFydGl0aW9uX3dhcm5pbmdfdGhyZXNob2xkX21iOiAxMDA=" depends_on = [ @@ -277,6 +278,7 @@ resource "azurerm_cosmosdb_cassandra_datacenter" "test" { sku_name = "Standard_DS14_v2" availability_zones_enabled = false backup_storage_customer_key_uri = azurerm_key_vault_key.test2.id + managed_disk_customer_key_uri = azurerm_key_vault_key.test2.id base64_encoded_yaml_fragment = "Z29tcGFjdGlvbl90aHJvdWdocHV0X21iX3Blcl9zZWM6IDMyCmNvbXBhY3Rpb25fbGFyZ2VfcGFydGl0aW9uX3dhcm5pbmdfdGhyZXNob2xkX21iOiAxMDA=" depends_on = [ diff --git a/website/docs/r/cosmosdb_cassandra_datacenter.html.markdown b/website/docs/r/cosmosdb_cassandra_datacenter.html.markdown index de3518e0317b..a89f0bd2a14d 100644 --- a/website/docs/r/cosmosdb_cassandra_datacenter.html.markdown +++ b/website/docs/r/cosmosdb_cassandra_datacenter.html.markdown @@ -96,6 +96,8 @@ The following arguments are supported: * `disk_sku` - (Optional) The Disk SKU that is used for this Cassandra Datacenter. Defaults to `P30`. +* `managed_disk_customer_key_uri` - (Optional) The key URI of the customer key to use for the encryption of the Managed Disk. + * `sku_name` - (Optional) Determines the selected sku. Defaults to Standard_DS14_v2. * `disk_count` - (Optional) Determines the number of p30 disks that are attached to each node. Defaults to `4`.