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

azurerm_cosmosdb_cassandra_datacenter - sku_name is now updatable #23419

Merged
merged 5 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"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/features"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/cosmos/validate"
keyVaultValidate "github.com/hashicorp/terraform-provider-azurerm/internal/services/keyvault/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
Expand All @@ -26,7 +27,7 @@ import (
)

func resourceCassandraDatacenter() *pluginsdk.Resource {
return &pluginsdk.Resource{
resource := &pluginsdk.Resource{
Create: resourceCassandraDatacenterCreate,
Read: resourceCassandraDatacenterRead,
Update: resourceCassandraDatacenterUpdate,
Expand Down Expand Up @@ -99,11 +100,6 @@ func resourceCassandraDatacenter() *pluginsdk.Resource {
ValidateFunc: validation.IntAtLeast(3),
Default: 3,
},
"sku_name": {
Type: pluginsdk.TypeString,
Optional: true,
ValidateFunc: validation.StringIsNotEmpty,
},
"disk_count": {
Type: pluginsdk.TypeInt,
Optional: true,
Expand All @@ -116,6 +112,27 @@ func resourceCassandraDatacenter() *pluginsdk.Resource {
},
},
}

if !features.FourPointOhBeta() {
// NOTE: The API does not expose a constant for the Sku so I had to hardcode it here...
// Per the service team, the current default Sku is 'Standard_DS14_v2' but moving forward
// the new default value should be 'Standard_E16s_v5'.
resource.Schema["sku_name"] = &pluginsdk.Schema{
Type: pluginsdk.TypeString,
Optional: true,
Default: "Standard_DS14_v2",
ValidateFunc: validation.StringIsNotEmpty,
}
} else {
resource.Schema["sku_name"] = &pluginsdk.Schema{
Type: pluginsdk.TypeString,
Optional: true,
Default: "Standard_E16s_v5",
ValidateFunc: validation.StringIsNotEmpty,
}
}

return resource
}

func resourceCassandraDatacenterCreate(d *pluginsdk.ResourceData, meta interface{}) error {
Expand All @@ -139,7 +156,7 @@ func resourceCassandraDatacenterCreate(d *pluginsdk.ResourceData, meta interface
return tf.ImportAsExistsError("azurerm_cosmosdb_cassandra_datacenter", id.ID())
}

body := managedcassandras.DataCenterResource{
payload := managedcassandras.DataCenterResource{
Properties: &managedcassandras.DataCenterResourceProperties{
DelegatedSubnetId: utils.String(d.Get("delegated_management_subnet_id").(string)),
NodeCount: utils.Int64(int64(d.Get("node_count").(int))),
Expand All @@ -152,18 +169,18 @@ func resourceCassandraDatacenterCreate(d *pluginsdk.ResourceData, meta interface
}

if v, ok := d.GetOk("backup_storage_customer_key_uri"); ok {
body.Properties.BackupStorageCustomerKeyUri = utils.String(v.(string))
payload.Properties.BackupStorageCustomerKeyUri = utils.String(v.(string))
}

if v, ok := d.GetOk("base64_encoded_yaml_fragment"); ok {
body.Properties.Base64EncodedCassandraYamlFragment = utils.String(v.(string))
payload.Properties.Base64EncodedCassandraYamlFragment = utils.String(v.(string))
}

if v, ok := d.GetOk("managed_disk_customer_key_uri"); ok {
body.Properties.ManagedDiskCustomerKeyUri = utils.String(v.(string))
payload.Properties.ManagedDiskCustomerKeyUri = utils.String(v.(string))
}

if err = client.CassandraDataCentersCreateUpdateThenPoll(ctx, id, body); err != nil {
if err = client.CassandraDataCentersCreateUpdateThenPoll(ctx, id, payload); err != nil {
return fmt.Errorf("creating %q: %+v", id, err)
}

Expand Down Expand Up @@ -222,28 +239,29 @@ func resourceCassandraDatacenterUpdate(d *pluginsdk.ResourceData, meta interface
return err
}

body := managedcassandras.DataCenterResource{
payload := managedcassandras.DataCenterResource{
Properties: &managedcassandras.DataCenterResourceProperties{
DelegatedSubnetId: utils.String(d.Get("delegated_management_subnet_id").(string)),
NodeCount: utils.Int64(int64(d.Get("node_count").(int))),
Sku: utils.String(d.Get("sku_name").(string)),
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))
payload.Properties.BackupStorageCustomerKeyUri = utils.String(v.(string))
}

if v, ok := d.GetOk("base64_encoded_yaml_fragment"); ok {
body.Properties.Base64EncodedCassandraYamlFragment = utils.String(v.(string))
payload.Properties.Base64EncodedCassandraYamlFragment = utils.String(v.(string))
}

if v, ok := d.GetOk("managed_disk_customer_key_uri"); ok {
body.Properties.ManagedDiskCustomerKeyUri = utils.String(v.(string))
payload.Properties.ManagedDiskCustomerKeyUri = utils.String(v.(string))
}

if err := client.CassandraDataCentersCreateUpdateThenPoll(ctx, *id, body); err != nil {
if err := client.CassandraDataCentersCreateUpdateThenPoll(ctx, *id, payload); err != nil {
return fmt.Errorf("updating %q: %+v", id, err)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"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/features"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)
Expand All @@ -24,7 +25,7 @@ func testAccCassandraDatacenter_basic(t *testing.T) {

data.ResourceSequentialTest(t, r, []acceptance.TestStep{
{
Config: r.basic(data, 3),
Config: r.basic(data),
Check: acceptance.ComposeAggregateTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
Expand Down Expand Up @@ -55,6 +56,68 @@ func testAccCassandraDatacenter_update(t *testing.T) {
})
}

func testAccCassandraDatacenter_updateSku(t *testing.T) {
// Regression test case for MS IcM issue
data := acceptance.BuildTestData(t, "azurerm_cosmosdb_cassandra_datacenter", "test")
r := CassandraDatacenterResource{}

if !features.FourPointOhBeta() {
data.ResourceSequentialTest(t, r, []acceptance.TestStep{
{
Config: r.basic(data),
Check: acceptance.ComposeAggregateTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("sku_name").HasValue("Standard_DS14_v2"),
),
},
data.ImportStep(),
{
Config: r.updateSku(data, "Standard_DS13_v2"),
Check: acceptance.ComposeAggregateTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("sku_name").HasValue("Standard_DS13_v2"),
),
},
data.ImportStep(),
{
Config: r.basic(data),
Check: acceptance.ComposeAggregateTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("sku_name").HasValue("Standard_DS14_v2"),
),
},
data.ImportStep(),
})
} else {
data.ResourceSequentialTest(t, r, []acceptance.TestStep{
{
Config: r.basic(data),
Check: acceptance.ComposeAggregateTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("sku_name").HasValue("Standard_E16s_v5"),
),
},
data.ImportStep(),
{
Config: r.updateSku(data, "Standard_E2_v5"),
Check: acceptance.ComposeAggregateTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("sku_name").HasValue("Standard_E2_v5"),
),
},
data.ImportStep(),
{
Config: r.basic(data),
Check: acceptance.ComposeAggregateTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("sku_name").HasValue("Standard_E16s_v5"),
),
},
data.ImportStep(),
})
}
}

func (t CassandraDatacenterResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := managedcassandras.ParseDataCenterID(state.ID)
if err != nil {
Expand All @@ -69,7 +132,7 @@ func (t CassandraDatacenterResource) Exists(ctx context.Context, clients *client
return utils.Bool(resp.Model != nil), nil
}

func (r CassandraDatacenterResource) basic(data acceptance.TestData, nodeCount int) string {
func (r CassandraDatacenterResource) basic(data acceptance.TestData) string {
return fmt.Sprintf(`
%s

Expand All @@ -78,12 +141,11 @@ resource "azurerm_cosmosdb_cassandra_datacenter" "test" {
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
node_count = 3
disk_count = 4
sku_name = "Standard_DS14_v2"
availability_zones_enabled = false
}
`, r.template(data), data.RandomInteger, nodeCount)
`, r.template(data), data.RandomInteger)
}

func (r CassandraDatacenterResource) complete(data acceptance.TestData, nodeCount int) string {
Expand Down Expand Up @@ -308,7 +370,7 @@ provider "azurerm" {
}

resource "azurerm_resource_group" "test" {
name = "acctestRG-ca-%d"
name = "acctestRG-cassandra-%d"
location = "%s"
}

Expand Down Expand Up @@ -351,3 +413,20 @@ resource "azurerm_cosmosdb_cassandra_cluster" "test" {
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger, data.RandomInteger)
}

func (r CassandraDatacenterResource) updateSku(data acceptance.TestData, skuName string) 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 = 3
disk_count = 4
sku_name = "%s"
availability_zones_enabled = false
}
`, r.template(data), data.RandomInteger, skuName)
}
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func resourceCosmosDbCassandraKeyspaceUpdate(d *pluginsdk.ResourceData, meta int
if err != nil {
if response.WasNotFound(throughputFuture.Response()) {
return fmt.Errorf("setting Throughput for Cosmos Cassandra Keyspace %q (Account: %q): %+v - "+
"If the collection has not been created with an initial throughput, you cannot configure it later.", id.Name, id.DatabaseAccountName, err)
"If the collection has not been created with an initial throughput, you cannot configure it later", id.Name, id.DatabaseAccountName, err)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ func TestAccCassandraSequential(t *testing.T) {
"requiresImport": testAccCassandraCluster_requiresImport,
},
"dataCenter": {
"basic": testAccCassandraDatacenter_basic,
"update": testAccCassandraDatacenter_update,
"basic": testAccCassandraDatacenter_basic,
"update": testAccCassandraDatacenter_update,
"updateSku": testAccCassandraDatacenter_updateSku,
},
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func resourceCosmosGremlinDatabaseUpdate(d *pluginsdk.ResourceData, meta interfa
if err != nil {
if response.WasNotFound(throughputFuture.HttpResponse) {
return fmt.Errorf("setting Throughput for Cosmos Gremlin Database %q (Account: %q): %+v - "+
"If the collection has not been created with and initial throughput, you cannot configure it later.", id.GremlinDatabaseName, id.DatabaseAccountName, err)
"If the collection has not been created with and initial throughput, you cannot configure it later", id.GremlinDatabaseName, id.DatabaseAccountName, err)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ func resourceCosmosDbGremlinGraphUpdate(d *pluginsdk.ResourceData, meta interfac
if err != nil {
if response.WasNotFound(throughputFuture.HttpResponse) {
return fmt.Errorf("setting Throughput for Cosmos Gremlin Graph %q (Account: %q, Database: %q): %+v - "+
"If the graph has not been created with an initial throughput, you cannot configure it later.", id.GraphName, id.DatabaseAccountName, id.GremlinDatabaseName, err)
"If the graph has not been created with an initial throughput, you cannot configure it later", id.GraphName, id.DatabaseAccountName, id.GremlinDatabaseName, err)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func resourceCosmosDbMongoDatabaseUpdate(d *pluginsdk.ResourceData, meta interfa
if err != nil {
if response.WasNotFound(throughputFuture.Response()) {
return fmt.Errorf("setting Throughput for Cosmos MongoDB Database %q (Account: %q): %+v - "+
"If the collection has not been created with an initial throughput, you cannot configure it later.", id.Name, id.DatabaseAccountName, err)
"If the collection has not been created with an initial throughput, you cannot configure it later", id.Name, id.DatabaseAccountName, err)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func resourceCosmosDbSQLDatabaseUpdate(d *pluginsdk.ResourceData, meta interface
if err != nil {
if response.WasNotFound(throughputFuture.Response()) {
return fmt.Errorf("setting Throughput for Cosmos SQL Database %q (Account: %q) %+v - "+
"If the collection has not been created with an initial throughput, you cannot configure it later.", id.Name, id.DatabaseAccountName, err)
"If the collection has not been created with an initial throughput, you cannot configure it later", id.Name, id.DatabaseAccountName, err)
}
}

Expand Down
2 changes: 1 addition & 1 deletion internal/services/cosmos/cosmosdb_table_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func resourceCosmosDbTableUpdate(d *pluginsdk.ResourceData, meta interface{}) er
if err != nil {
if response.WasNotFound(throughputFuture.Response()) {
return fmt.Errorf("setting Throughput for Cosmos Table %q (Account: %q): %+v - "+
"If the collection has not been created with an initial throughput, you cannot configure it later.", id.Name, id.DatabaseAccountName, err)
"If the collection has not been created with an initial throughput, you cannot configure it later", id.Name, id.DatabaseAccountName, err)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ The following arguments are supported:

* `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.
* `sku_name` - (Optional) Determines the selected sku. Defaults to `Standard_DS14_v2`.

-> **NOTE:** In v4.0 of the provider the default value for the `sku_name` field will be changed to `Standard_E16s_v5`.

* `disk_count` - (Optional) Determines the number of p30 disks that are attached to each node.

Expand Down
Loading