Skip to content

Commit

Permalink
cosmosdb - add throughput to remaining resources (#5203)
Browse files Browse the repository at this point in the history
continuation of #4616
fixes #3623
  • Loading branch information
katbyte authored Dec 18, 2019
1 parent 0a30de7 commit 5a4a32a
Show file tree
Hide file tree
Showing 14 changed files with 559 additions and 13 deletions.
81 changes: 80 additions & 1 deletion azurerm/resource_arm_cosmosdb_cassandra_keyspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package azurerm
import (
"fmt"
"log"
"strconv"
"time"

"github.com/Azure/azure-sdk-for-go/services/cosmos-db/mgmt/2015-04-08/documentdb"
Expand All @@ -20,6 +21,7 @@ func resourceArmCosmosDbCassandraKeyspace() *schema.Resource {
return &schema.Resource{
Create: resourceArmCosmosDbCassandraKeyspaceCreate,
Read: resourceArmCosmosDbCassandraKeyspaceRead,
Update: resourceArmCosmosDbCassandraKeyspaceUpdate,
Delete: resourceArmCosmosDbCassandraKeyspaceDelete,

Importer: &schema.ResourceImporter{
Expand Down Expand Up @@ -49,6 +51,13 @@ func resourceArmCosmosDbCassandraKeyspace() *schema.Resource {
ForceNew: true,
ValidateFunc: validate.CosmosAccountName,
},

"throughput": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
ValidateFunc: validate.CosmosThroughput,
},
},
}
}
Expand All @@ -62,7 +71,7 @@ func resourceArmCosmosDbCassandraKeyspaceCreate(d *schema.ResourceData, meta int
resourceGroup := d.Get("resource_group_name").(string)
account := d.Get("account_name").(string)

if features.ShouldResourcesBeImported() && d.IsNewResource() {
if features.ShouldResourcesBeImported() {
existing, err := client.GetCassandraKeyspace(ctx, resourceGroup, account, name)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
Expand All @@ -87,6 +96,12 @@ func resourceArmCosmosDbCassandraKeyspaceCreate(d *schema.ResourceData, meta int
},
}

if throughput, hasThroughput := d.GetOk("throughput"); hasThroughput {
db.CassandraKeyspaceCreateUpdateProperties.Options = map[string]*string{
"throughput": utils.String(strconv.Itoa(throughput.(int))),
}
}

future, err := client.CreateUpdateCassandraKeyspace(ctx, resourceGroup, account, name, db)
if err != nil {
return fmt.Errorf("Error issuing create/update request for Cosmos Cassandra Keyspace %s (Account %s): %+v", name, account, err)
Expand All @@ -110,6 +125,59 @@ func resourceArmCosmosDbCassandraKeyspaceCreate(d *schema.ResourceData, meta int
return resourceArmCosmosDbCassandraKeyspaceRead(d, meta)
}

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

id, err := azure.ParseCosmosKeyspaceID(d.Id())
if err != nil {
return err
}

db := documentdb.CassandraKeyspaceCreateUpdateParameters{
CassandraKeyspaceCreateUpdateProperties: &documentdb.CassandraKeyspaceCreateUpdateProperties{
Resource: &documentdb.CassandraKeyspaceResource{
ID: &id.Keyspace,
},
Options: map[string]*string{},
},
}

future, err := client.CreateUpdateCassandraKeyspace(ctx, id.ResourceGroup, id.Account, id.Keyspace, db)
if err != nil {
return fmt.Errorf("Error issuing create/update request for Cosmos Cassandra Keyspace %s (Account %s): %+v", id.ResourceGroup, id.Account, err)
}

if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("Error waiting on create/update future for Cosmos Cassandra Keyspace %s (Account %s): %+v", id.ResourceGroup, id.Account, err)
}

if d.HasChange("throughput") {
throughputParameters := documentdb.ThroughputUpdateParameters{
ThroughputUpdateProperties: &documentdb.ThroughputUpdateProperties{
Resource: &documentdb.ThroughputResource{
Throughput: utils.Int32(int32(d.Get("throughput").(int))),
},
},
}

throughputFuture, err := client.UpdateCassandraKeyspaceThroughput(ctx, id.ResourceGroup, id.Account, id.Keyspace, throughputParameters)
if err != nil {
if response.WasNotFound(throughputFuture.Response()) {
return fmt.Errorf("Error setting Throughput for Cosmos Cassandra Keyspace %s (Account %s): %+v - "+
"If the collection has not been created with an initial throughput, you cannot configure it later.", id.Keyspace, id.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", id.Keyspace, id.Account, err)
}
}

return resourceArmCosmosDbCassandraKeyspaceRead(d, meta)
}

func resourceArmCosmosDbCassandraKeyspaceRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).Cosmos.DatabaseClient
ctx, cancel := timeouts.ForRead(meta.(*ArmClient).StopContext, d)
Expand Down Expand Up @@ -137,6 +205,17 @@ 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 {
if !utils.ResponseWasNotFound(throughputResp.Response) {
return fmt.Errorf("Error reading Throughput on Cosmos Cassandra Keyspace %s (Account %s): %+v", id.Keyspace, id.Account, err)
} else {
d.Set("throughput", nil)
}
} else {
d.Set("throughput", throughputResp.Throughput)
}

return nil
}

Expand Down
76 changes: 76 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,68 @@ func TestAccAzureRMCosmosDbCassandraKeyspace_basic(t *testing.T) {
})
}

func TestAccAzureRMCosmosDbCassandraKeyspace_complete(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_throughput(ri, testLocation(), 700),
Check: resource.ComposeAggregateTestCheckFunc(
testCheckAzureRMCosmosDbCassandraKeyspaceExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "throughput", "700"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

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_throughput(ri, testLocation(), 700),
Check: resource.ComposeAggregateTestCheckFunc(
testCheckAzureRMCosmosDbCassandraKeyspaceExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "throughput", "700"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccAzureRMCosmosDbCassandraKeyspace_throughput(ri, testLocation(), 1700),
Check: resource.ComposeAggregateTestCheckFunc(
testCheckAzureRMCosmosDbCassandraKeyspaceExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "throughput", "1700"),
),
},
{
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 +164,17 @@ resource "azurerm_cosmosdb_cassandra_keyspace" "test" {
}
`, testAccAzureRMCosmosDBAccount_capabilityCassandra(rInt, location), rInt)
}

func testAccAzureRMCosmosDbCassandraKeyspace_throughput(rInt int, location string, throughput int) 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 = %[3]d
}
`, testAccAzureRMCosmosDBAccount_capabilityCassandra(rInt, location), rInt, throughput)
}
4 changes: 1 addition & 3 deletions azurerm/resource_arm_cosmosdb_mongo_collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package azurerm
import (
"fmt"
"log"
"net/http"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -238,8 +237,7 @@ func resourceArmCosmosDbMongoCollectionUpdate(d *schema.ResourceData, meta inter

throughputFuture, err := client.UpdateMongoDBCollectionThroughput(ctx, id.ResourceGroup, id.Account, id.Database, id.Collection, throughputParameters)
if err != nil {
if throughputFuture.Response().StatusCode == http.StatusNotFound {
d.Set("throughput", nil)
if response.WasNotFound(throughputFuture.Response()) {
return fmt.Errorf("Error setting Throughput for Cosmos MongoDB Collection %s (Account %s, Database %s): %+v - "+
"If the collection has not been created with an initial throughput, you cannot configure it later.", id.Collection, id.Account, id.Database, err)
}
Expand Down
4 changes: 1 addition & 3 deletions azurerm/resource_arm_cosmosdb_mongo_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package azurerm
import (
"fmt"
"log"
"net/http"
"strconv"
"time"

Expand Down Expand Up @@ -165,8 +164,7 @@ func resourceArmCosmosDbMongoDatabaseUpdate(d *schema.ResourceData, meta interfa

throughputFuture, err := client.UpdateMongoDBDatabaseThroughput(ctx, id.ResourceGroup, id.Account, id.Database, throughputParameters)
if err != nil {
if throughputFuture.Response().StatusCode == http.StatusNotFound {
d.Set("throughput", nil)
if response.WasNotFound(throughputFuture.Response()) {
return fmt.Errorf("Error setting Throughput for Cosmos MongoDB Database %s (Account %s): %+v - "+
"If the collection has not been created with an initial throughput, you cannot configure it later.", id.Database, id.Account, err)
}
Expand Down
96 changes: 95 additions & 1 deletion azurerm/resource_arm_cosmosdb_sql_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package azurerm
import (
"fmt"
"log"
"strconv"
"time"

"github.com/Azure/azure-sdk-for-go/services/cosmos-db/mgmt/2015-04-08/documentdb"
Expand All @@ -20,6 +21,7 @@ func resourceArmCosmosDbSQLContainer() *schema.Resource {
return &schema.Resource{
Create: resourceArmCosmosDbSQLContainerCreate,
Read: resourceArmCosmosDbSQLContainerRead,
Update: resourceArmCosmosDbSQLContainerUpdate,
Delete: resourceArmCosmosDbSQLContainerDelete,

Importer: &schema.ResourceImporter{
Expand Down Expand Up @@ -64,6 +66,13 @@ func resourceArmCosmosDbSQLContainer() *schema.Resource {
ValidateFunc: validate.NoEmptyStrings,
},

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

"unique_key": {
Type: schema.TypeSet,
Optional: true,
Expand Down Expand Up @@ -97,7 +106,7 @@ func resourceArmCosmosDbSQLContainerCreate(d *schema.ResourceData, meta interfac
account := d.Get("account_name").(string)
partitionkeypaths := d.Get("partition_key_path").(string)

if features.ShouldResourcesBeImported() && d.IsNewResource() {
if features.ShouldResourcesBeImported() {
existing, err := client.GetSQLContainer(ctx, resourceGroup, account, database, name)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
Expand Down Expand Up @@ -135,6 +144,12 @@ func resourceArmCosmosDbSQLContainerCreate(d *schema.ResourceData, meta interfac
}
}

if throughput, hasThroughput := d.GetOk("throughput"); hasThroughput {
db.SQLContainerCreateUpdateProperties.Options = map[string]*string{
"throughput": utils.String(strconv.Itoa(throughput.(int))),
}
}

future, err := client.CreateUpdateSQLContainer(ctx, resourceGroup, account, database, name, db)
if err != nil {
return fmt.Errorf("Error issuing create/update request for Cosmos SQL Container %s (Account: %s, Database:%s): %+v", name, account, database, err)
Expand All @@ -158,6 +173,74 @@ func resourceArmCosmosDbSQLContainerCreate(d *schema.ResourceData, meta interfac
return resourceArmCosmosDbSQLContainerRead(d, meta)
}

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

id, err := azure.ParseCosmosDatabaseContainerID(d.Id())
if err != nil {
return err
}

partitionkeypaths := d.Get("partition_key_path").(string)

db := documentdb.SQLContainerCreateUpdateParameters{
SQLContainerCreateUpdateProperties: &documentdb.SQLContainerCreateUpdateProperties{
Resource: &documentdb.SQLContainerResource{
ID: &id.Container,
},
Options: map[string]*string{},
},
}

if partitionkeypaths != "" {
db.SQLContainerCreateUpdateProperties.Resource.PartitionKey = &documentdb.ContainerPartitionKey{
Paths: &[]string{partitionkeypaths},
Kind: documentdb.PartitionKindHash,
}
}

if keys := expandCosmosSQLContainerUniqueKeys(d.Get("unique_key").(*schema.Set)); keys != nil {
db.SQLContainerCreateUpdateProperties.Resource.UniqueKeyPolicy = &documentdb.UniqueKeyPolicy{
UniqueKeys: keys,
}
}

future, err := client.CreateUpdateSQLContainer(ctx, id.Container, id.Account, id.Database, id.Container, db)
if err != nil {
return fmt.Errorf("Error issuing create/update request for Cosmos SQL Container %s (Account: %s, Database:%s): %+v", id.Container, id.Account, id.Database, err)
}

if err = future.WaitForCompletionRef(ctx, client.Client); err != nil {
return fmt.Errorf("Error waiting on create/update future for Cosmos SQL Container %s (Account: %s, Database:%s): %+v", id.Container, id.Account, id.Database, err)
}

if d.HasChange("throughput") {
throughputParameters := documentdb.ThroughputUpdateParameters{
ThroughputUpdateProperties: &documentdb.ThroughputUpdateProperties{
Resource: &documentdb.ThroughputResource{
Throughput: utils.Int32(int32(d.Get("throughput").(int))),
},
},
}

throughputFuture, err := client.UpdateSQLContainerThroughput(ctx, id.ResourceGroup, id.Account, id.Database, id.Container, throughputParameters)
if err != nil {
if response.WasNotFound(throughputFuture.Response()) {
return fmt.Errorf("Error setting Throughput for Cosmos SQL Container %s (Account: %s, Database:%s): %+v - "+
"If the collection has not been created with an initial throughput, you cannot configure it later.", id.Container, id.Account, id.Database, err)
}
}

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

return resourceArmCosmosDbSQLContainerRead(d, meta)
}

func resourceArmCosmosDbSQLContainerRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).Cosmos.DatabaseClient
ctx, cancel := timeouts.ForRead(meta.(*ArmClient).StopContext, d)
Expand Down Expand Up @@ -202,6 +285,17 @@ func resourceArmCosmosDbSQLContainerRead(d *schema.ResourceData, meta interface{
}
}

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

return nil
}

Expand Down
Loading

0 comments on commit 5a4a32a

Please sign in to comment.