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

[DO NOT MERGE] Adds Throughput to CosmosDB Resources #4616

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
39 changes: 37 additions & 2 deletions azurerm/resource_arm_cosmosdb_cassandra_keyspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ import (

func resourceArmCosmosDbCassandraKeyspace() *schema.Resource {
return &schema.Resource{
Create: resourceArmCosmosDbCassandraKeyspaceCreate,
Create: resourceArmCosmosDbCassandraKeyspaceCreateUpdate,
Read: resourceArmCosmosDbCassandraKeyspaceRead,
Update: resourceArmCosmosDbCassandraKeyspaceCreateUpdate,
Delete: resourceArmCosmosDbCassandraKeyspaceDelete,

Importer: &schema.ResourceImporter{
Expand All @@ -41,18 +42,26 @@ func resourceArmCosmosDbCassandraKeyspace() *schema.Resource {
ForceNew: true,
ValidateFunc: validate.CosmosAccountName,
},

"throughput": {
Type: schema.TypeInt,
Optional: true,
Default: 400,
ValidateFunc: validate.CosmosThroughput,
},
},
}
}

func resourceArmCosmosDbCassandraKeyspaceCreate(d *schema.ResourceData, meta interface{}) error {
func resourceArmCosmosDbCassandraKeyspaceCreateUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).Cosmos.DatabaseClient
ctx, cancel := timeouts.ForCreate(meta.(*ArmClient).StopContext, d)
defer cancel()

name := d.Get("name").(string)
resourceGroup := d.Get("resource_group_name").(string)
account := d.Get("account_name").(string)
throughput := d.Get("throughput").(int)

if features.ShouldResourcesBeImported() && d.IsNewResource() {
existing, err := client.GetCassandraKeyspace(ctx, resourceGroup, account, name)
Expand Down Expand Up @@ -88,6 +97,23 @@ func resourceArmCosmosDbCassandraKeyspaceCreate(d *schema.ResourceData, meta int
return fmt.Errorf("Error waiting on create/update future for Cosmos Cassandra Keyspace %s (Account %s): %+v", name, account, err)
}

throughputParameters := documentdb.ThroughputUpdateParameters{
ThroughputUpdateProperties: &documentdb.ThroughputUpdateProperties{
Resource: &documentdb.ThroughputResource{
Throughput: utils.Int32(int32(throughput)),
},
},
}

throughputFuture, err := client.UpdateCassandraKeyspaceThroughput(ctx, resourceGroup, account, name, throughputParameters)
if err != nil {
return fmt.Errorf("Error setting Throughput for Cosmos Cassandra Keyspace %s (Account %s): %+v", name, account, err)
}

if err = throughputFuture.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("Error waiting on ThroughputUpdate future for Cosmos Cassandra Keyspace %s (Account %s): %+v", name, account, err)
}

resp, err := client.GetCassandraKeyspace(ctx, resourceGroup, account, name)
if err != nil {
return fmt.Errorf("Error making get request for Cosmos Cassandra Keyspace %s (Account %s): %+v", name, account, err)
Expand Down Expand Up @@ -129,6 +155,15 @@ func resourceArmCosmosDbCassandraKeyspaceRead(d *schema.ResourceData, meta inter
d.Set("name", props.ID)
}

throughputResp, err := client.GetCassandraKeyspaceThroughput(ctx, id.ResourceGroup, id.Account, id.Keyspace)
if err != nil {
return fmt.Errorf("Error reading Throughput on Cosmos Cassandra Keyspace %s (Account %s): %+v", id.Keyspace, id.Account, err)
}

if throughput := throughputResp.Throughput; throughput != nil {
d.Set("throughput", int(*throughput))
}

return nil
}

Expand Down
63 changes: 63 additions & 0 deletions azurerm/resource_arm_cosmosdb_cassandra_keyspace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,43 @@ func TestAccAzureRMCosmosDbCassandraKeyspace_basic(t *testing.T) {
})
}

func TestAccAzureRMCosmosDbCassandraKeyspace_update(t *testing.T) {
ri := tf.AccRandTimeInt()
resourceName := "azurerm_cosmosdb_cassandra_keyspace.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMCosmosDbCassandraKeyspaceDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMCosmosDbCassandraKeyspace_complete(ri, testLocation()),
Check: resource.ComposeAggregateTestCheckFunc(
testCheckAzureRMCosmosDbCassandraKeyspaceExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "throughput", "600"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccAzureRMCosmosDbCassandraKeyspace_update(ri, testLocation()),
Check: resource.ComposeAggregateTestCheckFunc(
testCheckAzureRMCosmosDbCassandraKeyspaceExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "throughput", "400"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testCheckAzureRMCosmosDbCassandraKeyspaceDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*ArmClient).Cosmos.DatabaseClient
ctx := testAccProvider.Meta().(*ArmClient).StopContext
Expand Down Expand Up @@ -102,3 +139,29 @@ resource "azurerm_cosmosdb_cassandra_keyspace" "test" {
}
`, testAccAzureRMCosmosDBAccount_capabilityCassandra(rInt, location), rInt)
}

func testAccAzureRMCosmosDbCassandraKeyspace_complete(rInt int, location string) string {
return fmt.Sprintf(`
%[1]s

resource "azurerm_cosmosdb_cassandra_keyspace" "test" {
name = "acctest-%[2]d"
resource_group_name = "${azurerm_cosmosdb_account.test.resource_group_name}"
account_name = "${azurerm_cosmosdb_account.test.name}"
throughput = 600
}
`, testAccAzureRMCosmosDBAccount_capabilityCassandra(rInt, location), rInt)
}

func testAccAzureRMCosmosDbCassandraKeyspace_update(rInt int, location string) string {
return fmt.Sprintf(`
%[1]s

resource "azurerm_cosmosdb_cassandra_keyspace" "test" {
name = "acctest-%[2]d"
resource_group_name = "${azurerm_cosmosdb_account.test.resource_group_name}"
account_name = "${azurerm_cosmosdb_account.test.name}"
throughput = 400
}
`, testAccAzureRMCosmosDBAccount_capabilityCassandra(rInt, location), rInt)
}
39 changes: 37 additions & 2 deletions azurerm/resource_arm_cosmosdb_mongo_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ import (

func resourceArmCosmosDbMongoDatabase() *schema.Resource {
return &schema.Resource{
Create: resourceArmCosmosDbMongoDatabaseCreate,
Create: resourceArmCosmosDbMongoDatabaseCreateUpdate,
Read: resourceArmCosmosDbMongoDatabaseRead,
Update: resourceArmCosmosDbMongoDatabaseCreateUpdate,
Delete: resourceArmCosmosDbMongoDatabaseDelete,

Importer: &schema.ResourceImporter{
Expand All @@ -41,18 +42,26 @@ func resourceArmCosmosDbMongoDatabase() *schema.Resource {
ForceNew: true,
ValidateFunc: validate.CosmosAccountName,
},

"throughput": {
Type: schema.TypeInt,
Optional: true,
Default: 400,
ValidateFunc: validate.CosmosThroughput,
},
},
}
}

func resourceArmCosmosDbMongoDatabaseCreate(d *schema.ResourceData, meta interface{}) error {
func resourceArmCosmosDbMongoDatabaseCreateUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).Cosmos.DatabaseClient
ctx, cancel := timeouts.ForCreate(meta.(*ArmClient).StopContext, d)
defer cancel()

name := d.Get("name").(string)
resourceGroup := d.Get("resource_group_name").(string)
account := d.Get("account_name").(string)
throughput := d.Get("throughput").(int)

if features.ShouldResourcesBeImported() && d.IsNewResource() {
existing, err := client.GetMongoDBDatabase(ctx, resourceGroup, account, name)
Expand Down Expand Up @@ -88,6 +97,23 @@ func resourceArmCosmosDbMongoDatabaseCreate(d *schema.ResourceData, meta interfa
return fmt.Errorf("Error waiting on create/update future for Cosmos Mongo Database %s (Account %s): %+v", name, account, err)
}

throughputParameters := documentdb.ThroughputUpdateParameters{
ThroughputUpdateProperties: &documentdb.ThroughputUpdateProperties{
Resource: &documentdb.ThroughputResource{
Throughput: utils.Int32(int32(throughput)),
},
},
}

throughputFuture, err := client.UpdateMongoDBDatabaseThroughput(ctx, resourceGroup, account, name, throughputParameters)
if err != nil {
return fmt.Errorf("Error setting Throughput for Cosmos MongoDB Database %s (Account %s): %+v", name, account, err)
}

if err = throughputFuture.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("Error waiting on ThroughputUpdate future for Cosmos Mongo Database %s (Account %s): %+v", name, account, err)
}

resp, err := client.GetMongoDBDatabase(ctx, resourceGroup, account, name)
if err != nil {
return fmt.Errorf("Error making get request for Cosmos Mongo Database %s (Account %s): %+v", name, account, err)
Expand Down Expand Up @@ -129,6 +155,15 @@ func resourceArmCosmosDbMongoDatabaseRead(d *schema.ResourceData, meta interface
d.Set("name", props.ID)
}

throughputResp, err := client.GetMongoDBDatabaseThroughput(ctx, id.ResourceGroup, id.Account, id.Database)
if err != nil {
return fmt.Errorf("Error reading Throughput on Cosmos Mongo Database %s (Account %s): %+v", id.Database, id.Account, err)
}

if throughput := throughputResp.Throughput; throughput != nil {
d.Set("throughput", int(*throughput))
}

return nil
}

Expand Down
63 changes: 63 additions & 0 deletions azurerm/resource_arm_cosmosdb_mongo_database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,43 @@ func TestAccAzureRMCosmosDbMongoDatabase_basic(t *testing.T) {
})
}

func TestAccAzureRMCosmosDbMongoDatabase_update(t *testing.T) {
ri := tf.AccRandTimeInt()
resourceName := "azurerm_cosmosdb_mongo_database.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMCosmosDbMongoDatabaseDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMCosmosDbMongoDatabase_complete(ri, testLocation()),
Check: resource.ComposeAggregateTestCheckFunc(
testCheckAzureRMCosmosDbMongoDatabaseExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "throughput", "600"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccAzureRMCosmosDbMongoDatabase_update(ri, testLocation()),
Check: resource.ComposeAggregateTestCheckFunc(
testCheckAzureRMCosmosDbMongoDatabaseExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "throughput", "400"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testCheckAzureRMCosmosDbMongoDatabaseDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*ArmClient).Cosmos.DatabaseClient
ctx := testAccProvider.Meta().(*ArmClient).StopContext
Expand Down Expand Up @@ -102,3 +139,29 @@ resource "azurerm_cosmosdb_mongo_database" "test" {
}
`, testAccAzureRMCosmosDBAccount_mongoDB(rInt, location), rInt)
}

func testAccAzureRMCosmosDbMongoDatabase_complete(rInt int, location string) string {
return fmt.Sprintf(`
%[1]s

resource "azurerm_cosmosdb_mongo_database" "test" {
name = "acctest-%[2]d"
resource_group_name = "${azurerm_cosmosdb_account.test.resource_group_name}"
account_name = "${azurerm_cosmosdb_account.test.name}"
throughput = 600
}
`, testAccAzureRMCosmosDBAccount_mongoDB(rInt, location), rInt)
}

func testAccAzureRMCosmosDbMongoDatabase_update(rInt int, location string) string {
return fmt.Sprintf(`
%[1]s

resource "azurerm_cosmosdb_mongo_database" "test" {
name = "acctest-%[2]d"
resource_group_name = "${azurerm_cosmosdb_account.test.resource_group_name}"
account_name = "${azurerm_cosmosdb_account.test.name}"
throughput = 400
}
`, testAccAzureRMCosmosDBAccount_mongoDB(rInt, location), rInt)
}
39 changes: 37 additions & 2 deletions azurerm/resource_arm_cosmosdb_sql_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ import (

func resourceArmCosmosDbSQLContainer() *schema.Resource {
return &schema.Resource{
Create: resourceArmCosmosDbSQLContainerCreate,
Create: resourceArmCosmosDbSQLContainerCreateUpdate,
Read: resourceArmCosmosDbSQLContainerRead,
Update: resourceArmCosmosDbSQLContainerCreateUpdate,
Delete: resourceArmCosmosDbSQLContainerDelete,

Importer: &schema.ResourceImporter{
Expand Down Expand Up @@ -74,11 +75,18 @@ func resourceArmCosmosDbSQLContainer() *schema.Resource {
},
},
},

"throughput": {
Type: schema.TypeInt,
Optional: true,
Default: 400,
ValidateFunc: validate.CosmosThroughput,
},
},
}
}

func resourceArmCosmosDbSQLContainerCreate(d *schema.ResourceData, meta interface{}) error {
func resourceArmCosmosDbSQLContainerCreateUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).Cosmos.DatabaseClient
ctx, cancel := timeouts.ForCreate(meta.(*ArmClient).StopContext, d)
defer cancel()
Expand All @@ -88,6 +96,7 @@ func resourceArmCosmosDbSQLContainerCreate(d *schema.ResourceData, meta interfac
database := d.Get("database_name").(string)
account := d.Get("account_name").(string)
partitionkeypaths := d.Get("partition_key_path").(string)
throughput := d.Get("throughput").(int)

if features.ShouldResourcesBeImported() && d.IsNewResource() {
existing, err := client.GetSQLContainer(ctx, resourceGroup, account, database, name)
Expand Down Expand Up @@ -136,6 +145,23 @@ func resourceArmCosmosDbSQLContainerCreate(d *schema.ResourceData, meta interfac
return fmt.Errorf("Error waiting on create/update future for Cosmos SQL Container %s (Account: %s, Database:%s): %+v", name, account, database, err)
}

throughputParameters := documentdb.ThroughputUpdateParameters{
ThroughputUpdateProperties: &documentdb.ThroughputUpdateProperties{
Resource: &documentdb.ThroughputResource{
Throughput: utils.Int32(int32(throughput)),
},
},
}

throughputFuture, err := client.UpdateSQLContainerThroughput(ctx, resourceGroup, account, database, name, throughputParameters)
if err != nil {
return fmt.Errorf("Error setting Throughput for Cosmos SQL Container %s (Account: %s, Database:%s): %+v", name, account, database, err)
}

if err = throughputFuture.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("Error waiting on ThroughputUpdate future for Cosmos SQL Container %s (Account: %s, Database:%s): %+v", name, account, database, err)
}

resp, err := client.GetSQLContainer(ctx, resourceGroup, account, database, name)
if err != nil {
return fmt.Errorf("Error making get request for Cosmos SQL Container %s (Account: %s, Database:%s): %+v", name, account, database, err)
Expand Down Expand Up @@ -194,6 +220,15 @@ func resourceArmCosmosDbSQLContainerRead(d *schema.ResourceData, meta interface{
}
}

throughputResp, err := client.GetSQLContainerThroughput(ctx, id.ResourceGroup, id.Account, id.Database, id.Container)
if err != nil {
return fmt.Errorf("Error reading Throughput on Cosmos SQL Container '%s' (Account: %s, Database:%s) ID: %v", id.Container, id.Account, id.Database, err)
}

if throughput := throughputResp.Throughput; throughput != nil {
d.Set("throughput", int(*throughput))
}

return nil
}

Expand Down
Loading