Skip to content

Commit

Permalink
azurerm_cosmosdb_account: add capabilities property (#1424)
Browse files Browse the repository at this point in the history
* azurerm_cosmosdb_account: add capabilities property for gremlin and table support

* cosmos db capabilities: updates to address PR comments
  • Loading branch information
katbyte authored and tombuildsstuff committed Jul 2, 2018
1 parent 0bf7cbf commit dbf12bd
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 19 deletions.
12 changes: 12 additions & 0 deletions azurerm/data_source_cosmos_db_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,14 @@ func dataSourceArmCosmosDBAccount() *schema.Resource {
},
},

"capabilities": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},

"endpoint": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -190,6 +198,10 @@ func dataSourceArmCosmosDBAccountRead(d *schema.ResourceData, meta interface{})
return fmt.Errorf("Error setting `geo_location`: %+v", err)
}

if err := d.Set("capabilities", flattenAzureRmCosmosDBAccountCapabilities(resp.Capabilities)); err != nil {
return fmt.Errorf("Error setting `capabilities`: %+v", err)
}

readEndpoints := make([]string, 0)
if locations := props.ReadLocations; locations != nil {
for _, l := range *locations {
Expand Down
97 changes: 79 additions & 18 deletions azurerm/resource_arm_cosmos_db_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,24 +48,24 @@ func resourceArmCosmosDBAccount() *schema.Resource {

//resource fields
"offer_type": {
Type: schema.TypeString,
Required: true,
Type: schema.TypeString,
Required: true,
DiffSuppressFunc: ignoreCaseDiffSuppressFunc,
ValidateFunc: validation.StringInSlice([]string{
string(documentdb.Standard),
}, true),
DiffSuppressFunc: ignoreCaseDiffSuppressFunc,
},

"kind": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Default: string(documentdb.GlobalDocumentDB),
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Default: string(documentdb.GlobalDocumentDB),
DiffSuppressFunc: ignoreCaseDiffSuppressFunc,
ValidateFunc: validation.StringInSlice([]string{
string(documentdb.GlobalDocumentDB),
string(documentdb.MongoDB),
}, true),
DiffSuppressFunc: ignoreCaseDiffSuppressFunc,
},

"ip_range_filter": {
Expand Down Expand Up @@ -173,12 +173,7 @@ func resourceArmCosmosDBAccount() *schema.Resource {
Computed: true,
},

"location": {
Type: schema.TypeString,
Required: true,
StateFunc: azureRMNormalizeLocation,
DiffSuppressFunc: azureRMSuppressLocationDiff,
},
"location": locationSchema(),

"failover_priority": {
Type: schema.TypeInt,
Expand All @@ -190,6 +185,26 @@ func resourceArmCosmosDBAccount() *schema.Resource {
Set: resourceAzureRMCosmosDBAccountGeoLocationHash,
},

"capabilities": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
DiffSuppressFunc: ignoreCaseDiffSuppressFunc,
ValidateFunc: validation.StringInSlice([]string{
"EnableTable",
"EnableGremlin",
`EnableCassandra`,
}, true),
},
},
},
Set: resourceAzureRMCosmosDBAccountCapabilitiesHash,
},

//computed
"endpoint": {
Type: schema.TypeString,
Expand Down Expand Up @@ -291,11 +306,12 @@ func resourceArmCosmosDBAccountCreate(d *schema.ResourceData, meta interface{})
Location: utils.String(location),
Kind: documentdb.DatabaseAccountKind(kind),
DatabaseAccountCreateUpdateProperties: &documentdb.DatabaseAccountCreateUpdateProperties{
ConsistencyPolicy: expandAzureRmCosmosDBAccountConsistencyPolicy(d),
Locations: &geoLocations,
DatabaseAccountOfferType: utils.String(offerType),
IPRangeFilter: utils.String(ipRangeFilter),
EnableAutomaticFailover: utils.Bool(enableAutomaticFailover),
ConsistencyPolicy: expandAzureRmCosmosDBAccountConsistencyPolicy(d),
Locations: &geoLocations,
Capabilities: expandAzureRmCosmosDBAccountCapabilities(d),
},
Tags: expandTags(tags),
}
Expand Down Expand Up @@ -374,11 +390,12 @@ func resourceArmCosmosDBAccountUpdate(d *schema.ResourceData, meta interface{})
Location: utils.String(location),
Kind: documentdb.DatabaseAccountKind(kind),
DatabaseAccountCreateUpdateProperties: &documentdb.DatabaseAccountCreateUpdateProperties{
ConsistencyPolicy: expandAzureRmCosmosDBAccountConsistencyPolicy(d),
Locations: &oldLocations,
DatabaseAccountOfferType: utils.String(offerType),
IPRangeFilter: utils.String(ipRangeFilter),
EnableAutomaticFailover: utils.Bool(enableAutomaticFailover),
Capabilities: expandAzureRmCosmosDBAccountCapabilities(d),
ConsistencyPolicy: expandAzureRmCosmosDBAccountConsistencyPolicy(d),
Locations: &oldLocations,
},
Tags: expandTags(tags),
}
Expand Down Expand Up @@ -494,6 +511,10 @@ func resourceArmCosmosDBAccountRead(d *schema.ResourceData, meta interface{}) er
}
}

if err := d.Set("capabilities", flattenAzureRmCosmosDBAccountCapabilities(resp.Capabilities)); err != nil {
return fmt.Errorf("Error setting `capabilities`: %+v", err)
}

if p := resp.ReadLocations; p != nil {
readEndpoints := []string{}
for _, l := range *p {
Expand Down Expand Up @@ -739,6 +760,19 @@ func expandAzureRmCosmosDBAccountFailoverPolicy(databaseName string, d *schema.R
return locations, nil
}

func expandAzureRmCosmosDBAccountCapabilities(d *schema.ResourceData) *[]documentdb.Capability {

capabilities := d.Get("capabilities").(*schema.Set).List()
s := make([]documentdb.Capability, 0, 0)

for _, c := range capabilities {
m := c.(map[string]interface{})
s = append(s, documentdb.Capability{Name: utils.String(m["name"].(string))})
}

return &s
}

func flattenAzureRmCosmosDBAccountConsistencyPolicy(policy *documentdb.ConsistencyPolicy) []interface{} {

result := map[string]interface{}{}
Expand Down Expand Up @@ -805,6 +839,23 @@ func flattenAzureRmCosmosDBAccountGeoLocations(d *schema.ResourceData, account d
return &locationSet
}

func flattenAzureRmCosmosDBAccountCapabilities(capabilities *[]documentdb.Capability) *schema.Set {
s := schema.Set{
F: resourceAzureRMCosmosDBAccountCapabilitiesHash,
}

for _, c := range *capabilities {
if v := c.Name; v != nil {
e := map[string]interface{}{
"name": *v,
}
s.Add(e)
}
}

return &s
}

//todo remove once deprecated field `failover_policy` is removed
func resourceAzureRMCosmosDBAccountFailoverPolicyHash(v interface{}) int {
var buf bytes.Buffer
Expand Down Expand Up @@ -835,3 +886,13 @@ func resourceAzureRMCosmosDBAccountGeoLocationHash(v interface{}) int {

return hashcode.String(buf.String())
}

func resourceAzureRMCosmosDBAccountCapabilitiesHash(v interface{}) int {
var buf bytes.Buffer

if m, ok := v.(map[string]interface{}); ok {
buf.WriteString(fmt.Sprintf("%s-", m["name"].(string)))
}

return hashcode.String(buf.String())
}
72 changes: 71 additions & 1 deletion azurerm/resource_arm_cosmos_db_account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,56 @@ func TestAccAzureRMCosmosDBAccount_mongoDB(t *testing.T) {
})
}

func TestAccAzureRMCosmosDBAccount_gremlin(t *testing.T) {
ri := acctest.RandInt()
resourceName := "azurerm_cosmosdb_account.test"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMCosmosDBAccountDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMCosmosDBAccount_gremlin(ri, testLocation()),
Check: resource.ComposeAggregateTestCheckFunc(
checkAccAzureRMCosmosDBAccount_basic(resourceName, testLocation(), string(documentdb.BoundedStaleness), 1),
resource.TestCheckResourceAttr(resourceName, "kind", "GlobalDocumentDB"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccAzureRMCosmosDBAccount_table(t *testing.T) {
ri := acctest.RandInt()
resourceName := "azurerm_cosmosdb_account.test"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMCosmosDBAccountDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMCosmosDBAccount_table(ri, testLocation()),
Check: resource.ComposeAggregateTestCheckFunc(
checkAccAzureRMCosmosDBAccount_basic(resourceName, testLocation(), string(documentdb.BoundedStaleness), 1),
resource.TestCheckResourceAttr(resourceName, "kind", "GlobalDocumentDB"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAbcAzureRMCosmosDBAccount_updatePropertiesAndLocation(t *testing.T) {
ri := acctest.RandInt()
resourceName := "azurerm_cosmosdb_account.test"
Expand Down Expand Up @@ -477,7 +527,7 @@ resource "azurerm_cosmosdb_account" "test" {
consistency_policy {
consistency_level = "%s"
%s
%s
}
geo_location {
Expand All @@ -504,6 +554,26 @@ func testAccAzureRMCosmosDBAccount_mongoDB(rInt int, location string) string {
`)
}

func testAccAzureRMCosmosDBAccount_gremlin(rInt int, location string) string {
return testAccAzureRMCosmosDBAccount_basic(rInt, location, string(documentdb.BoundedStaleness), "", `
kind = "GlobalDocumentDB"
capabilities = {
name = "EnableGremlin"
}
`)
}

func testAccAzureRMCosmosDBAccount_table(rInt int, location string) string {
return testAccAzureRMCosmosDBAccount_basic(rInt, location, string(documentdb.BoundedStaleness), "", `
kind = "GlobalDocumentDB"
capabilities = {
name = "EnableTable"
}
`)
}

func testAccAzureRMCosmosDBAccount_geoReplicated(rInt int, location string, altLocation string) string {
return testAccAzureRMCosmosDBAccount_basic(rInt, location, string(documentdb.BoundedStaleness), "", fmt.Sprintf(`
geo_location {
Expand Down
2 changes: 2 additions & 0 deletions website/docs/d/cosmosdb_account.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ The following attributes are exported:

* `enable_automatic_failover` - If automatic failover is enabled for this CosmosDB Account.

* `capabilities` - Capabilities enabled on this Cosmos DB account.

`consistency_policy` The current consistency Settings for this CosmosDB account with the following properties:

* `consistency_level` - The Consistency Level used by this CosmosDB Account.
Expand Down
2 changes: 2 additions & 0 deletions website/docs/r/cosmosdb_account.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ The following arguments are supported:

* `enable_automatic_failover` - (Optional) Enable automatic fail over for this Cosmos DB account.

* `capabilities` - (Optional) Enable capabilities for this Cosmos DB account. Possible values are `EnableTable` and `EnableGremlin`.

`consistency_policy` Configures the database consistency and supports the following:

* `consistency_level` - (Required) The Consistency Level to use for this CosmosDB Account - can be either `BoundedStaleness`, `Eventual`, `Session`, `Strong` or `ConsistentPrefix`.
Expand Down

0 comments on commit dbf12bd

Please sign in to comment.