Skip to content

Commit

Permalink
updating to include #23419
Browse files Browse the repository at this point in the history
* Initial Check-in...

* Change default sku_name value for 4.0...

* Fix lint error...

* Update 3.x to be computed...

* Make Sku conditional in Create function...
  • Loading branch information
WodansSon authored Sep 29, 2023
1 parent ff98670 commit 58aab27
Show file tree
Hide file tree
Showing 10 changed files with 135 additions and 32 deletions.
56 changes: 38 additions & 18 deletions internal/services/cosmos/cosmosdb_cassandra_datacenter_resource.go
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 @@ -92,18 +93,12 @@ func resourceCassandraDatacenter() *pluginsdk.Resource {
Optional: true,
ValidateFunc: keyVaultValidate.NestedItemId,
},

"node_count": {
Type: pluginsdk.TypeInt,
Optional: true,
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 +111,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,
Computed: true,
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,11 +155,10 @@ 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))),
Sku: utils.String(d.Get("sku_name").(string)),
AvailabilityZone: utils.Bool(d.Get("availability_zones_enabled").(bool)),
DiskCapacity: utils.Int64(int64(d.Get("disk_count").(int))),
DiskSku: utils.String(d.Get("disk_sku").(string)),
Expand All @@ -152,18 +167,22 @@ 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 v, ok := d.GetOk("sku_name"); ok {
payload.Properties.Sku = utils.String(v.(string))
}

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

Expand Down Expand Up @@ -222,28 +241,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,9 +25,10 @@ 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),
check.That(data.ResourceName).Key("sku_name").IsNotEmpty(),
),
},
data.ImportStep(),
Expand Down Expand Up @@ -55,6 +57,68 @@ func testAccCassandraDatacenter_update(t *testing.T) {
})
}

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

if !features.FourPointOhBeta() {
data.ResourceSequentialTest(t, r, []acceptance.TestStep{
{
Config: r.basicSku(data, "Standard_DS14_v2"),
Check: acceptance.ComposeAggregateTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("sku_name").HasValue("Standard_DS14_v2"),
),
},
data.ImportStep(),
{
Config: r.basicSku(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.basicSku(data, "Standard_DS14_v2"),
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.basicSku(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 +133,23 @@ 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
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
availability_zones_enabled = false
}
`, r.template(data), data.RandomInteger)
}

func (r CassandraDatacenterResource) basicSku(data acceptance.TestData, skuName string) string {
return fmt.Sprintf(`
%s
Expand All @@ -78,12 +158,12 @@ 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"
sku_name = "%s"
availability_zones_enabled = false
}
`, r.template(data), data.RandomInteger, nodeCount)
`, r.template(data), data.RandomInteger, skuName)
}

func (r CassandraDatacenterResource) complete(data acceptance.TestData, nodeCount int) string {
Expand Down Expand Up @@ -308,7 +388,7 @@ provider "azurerm" {
}
resource "azurerm_resource_group" "test" {
name = "acctestRG-ca-%d"
name = "acctestRG-cassandra-%d"
location = "%s"
}
Expand Down
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
5 changes: 3 additions & 2 deletions internal/services/cosmos/cosmosdb_cassandra_resource_test.go
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
2 changes: 1 addition & 1 deletion internal/services/cosmos/cosmosdb_sql_database_resource.go
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
2 changes: 2 additions & 0 deletions website/docs/r/cosmosdb_cassandra_datacenter.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ The following arguments are supported:

* `sku_name` - (Optional) Determines the selected sku.

-> **NOTE:** In v4.0 of the provider the `sku_name` will have a default value of `Standard_E16s_v5`.

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

* `availability_zones_enabled` - (Optional) Determines whether availability zones are enabled. Defaults to `true`.
Expand Down

0 comments on commit 58aab27

Please sign in to comment.