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_cosmos_db_account: allow setting the Kind to MongoDB/GlobalDocumentDB #299

Merged
merged 2 commits into from
Sep 1, 2017
Merged
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
24 changes: 24 additions & 0 deletions azurerm/import_arm_cosmosdb_account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,30 @@ func TestAccAzureRMCosmosDBAccount_importEventualConsistency(t *testing.T) {
})
}

func TestAccAzureRMCosmosDBAccount_importMongoDB(t *testing.T) {
resourceName := "azurerm_cosmosdb_account.test"

ri := acctest.RandInt()
config := testAccAzureRMCosmosDBAccount_mongoDB(ri, testLocation())

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMCosmosDBAccountDestroy,
Steps: []resource.TestStep{
{
Config: config,
},

{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccAzureRMCosmosDBAccount_importSession(t *testing.T) {
resourceName := "azurerm_cosmosdb_account.test"

Expand Down
21 changes: 18 additions & 3 deletions azurerm/resource_arm_cosmos_db_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@ func resourceArmCosmosDBAccount() *schema.Resource {
}, true),
},

"kind": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As this is a ForceNew field, are you sure we don't need a state migration? Technically the old state will have empty kind (or no kind to be precise) and we're setting default here, so "" => "global-document-db" would cause recreation, unless I'm mistaken?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just verified - seems good since there's a default value and this field was missing previously:

$ envchain azurerm terraform plan
Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.

azurerm_resource_group.test: Refreshing state... (ID: /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/tharvey-dev)
azurerm_cosmosdb_account.test: Refreshing state... (ID: /subscriptions/00000000-0000-0000-0000-...ntDB/databaseAccounts/tharvey-cosmosdb)
No changes. Infrastructure is up-to-date.

This means that Terraform did not detect any differences between your
configuration and real physical resources that exist. As a result, Terraform
doesn't need to do anything.

Default: string(cosmosdb.GlobalDocumentDB),
ValidateFunc: validation.StringInSlice([]string{
string(cosmosdb.GlobalDocumentDB),
string(cosmosdb.MongoDB),
}, true),
DiffSuppressFunc: ignoreCaseDiffSuppressFunc,
},

"ip_range_filter": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -152,6 +164,7 @@ func resourceArmCosmosDBAccountCreateUpdate(d *schema.ResourceData, meta interfa
name := d.Get("name").(string)
location := d.Get("location").(string)
resGroup := d.Get("resource_group_name").(string)
kind := d.Get("kind").(string)
offerType := d.Get("offer_type").(string)
ipRangeFilter := d.Get("ip_range_filter").(string)

Expand All @@ -163,12 +176,13 @@ func resourceArmCosmosDBAccountCreateUpdate(d *schema.ResourceData, meta interfa
tags := d.Get("tags").(map[string]interface{})

parameters := cosmosdb.DatabaseAccountCreateUpdateParameters{
Location: &location,
Location: utils.String(location),
Kind: cosmosdb.DatabaseAccountKind(kind),
DatabaseAccountCreateUpdateProperties: &cosmosdb.DatabaseAccountCreateUpdateProperties{
ConsistencyPolicy: &consistencyPolicy,
DatabaseAccountOfferType: &offerType,
Locations: &failoverPolicies,
IPRangeFilter: &ipRangeFilter,
DatabaseAccountOfferType: utils.String(offerType),
IPRangeFilter: utils.String(ipRangeFilter),
},
Tags: expandTags(tags),
}
Expand Down Expand Up @@ -215,6 +229,7 @@ func resourceArmCosmosDBAccountRead(d *schema.ResourceData, meta interface{}) er
d.Set("name", resp.Name)
d.Set("location", azureRMNormalizeLocation(*resp.Location))
d.Set("resource_group_name", resGroup)
d.Set("kind", string(resp.Kind))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would have thought d.Set can do the casting, but maybe not... 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could, however I think it's worth being explicit that this value is coming from an external type?

d.Set("offer_type", string(resp.DatabaseAccountOfferType))
d.Set("ip_range_filter", resp.IPRangeFilter)
flattenAndSetAzureRmCosmosDBAccountConsistencyPolicy(d, resp.ConsistencyPolicy)
Expand Down
52 changes: 50 additions & 2 deletions azurerm/resource_arm_cosmos_db_account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func TestAccAzureRMCosmosDBAccountName_validation(t *testing.T) {
}

func TestAccAzureRMCosmosDBAccount_boundedStaleness(t *testing.T) {

resourceName := "azurerm_cosmosdb_account.test"
ri := acctest.RandInt()
config := testAccAzureRMCosmosDBAccount_boundedStaleness(ri, testLocation())

Expand All @@ -64,7 +64,8 @@ func TestAccAzureRMCosmosDBAccount_boundedStaleness(t *testing.T) {
{
Config: config,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMCosmosDBAccountExists("azurerm_cosmosdb_account.test"),
testCheckAzureRMCosmosDBAccountExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "kind", "GlobalDocumentDB"),
),
},
},
Expand Down Expand Up @@ -110,6 +111,27 @@ func TestAccAzureRMCosmosDBAccount_eventualConsistency(t *testing.T) {
})
}

func TestAccAzureRMCosmosDBAccount_mongoDB(t *testing.T) {
resourceName := "azurerm_cosmosdb_account.test"
ri := acctest.RandInt()
config := testAccAzureRMCosmosDBAccount_mongoDB(ri, testLocation())

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMCosmosDBAccountDestroy,
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMCosmosDBAccountExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "kind", "MongoDB"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to add a similar check to existing tests - just to verify that we expose the default kind.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

),
},
},
})
}

func TestAccAzureRMCosmosDBAccount_session(t *testing.T) {
ri := acctest.RandInt()
config := testAccAzureRMCosmosDBAccount_session(ri, testLocation())
Expand Down Expand Up @@ -324,6 +346,32 @@ resource "azurerm_cosmosdb_account" "test" {
`, rInt, location, rInt)
}

func testAccAzureRMCosmosDBAccount_mongoDB(rInt int, location string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "%s"
}

resource "azurerm_cosmosdb_account" "test" {
name = "acctest-%d"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
kind = "MongoDB"
offer_type = "Standard"

consistency_policy {
consistency_level = "BoundedStaleness"
}

failover_policy {
location = "${azurerm_resource_group.test.location}"
priority = 0
}
}
`, rInt, location, rInt)
}

func testAccAzureRMCosmosDBAccount_strong(rInt int, location string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
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 @@ -55,6 +55,8 @@ The following arguments are supported:

* `offer_type` - (Required) Specifies the Offer Type to use for this CosmosDB Account - currently this can only be set to `Standard`.

* `kind` - (Optional) Specifies the Kind of CosmosDB to create - possible values are `GlobalDocumentDB` and `MongoDB`. Defaults to `GlobalDocumentDB`. Changing this forces a new resource to be created.

* `consistency_policy` - (Required) Specifies a `consistency_policy` resource, used to define the consistency policy for this CosmosDB account.

* `failover_policy` - (Required) Specifies a `failover_policy` resource, used to define where data should be replicated.
Expand Down