Skip to content

Commit aad5569

Browse files
mahhanifihelayotyMahsa Hanifi
authored
Updated the Cosmosdb module (#404)
* Add ADF module * added the test harness for cosmosdb and resolved merge issued * updated the Cosmosdb module * deleted data factory * removed the adf file from test harness from another PR and updated the httpGet in test to get nil in addition * added default value for vnet_subnet_id Co-authored-by: helayoty <[email protected]> Co-authored-by: Mahsa Hanifi <[email protected]>
1 parent eaf1224 commit aad5569

File tree

10 files changed

+124
-69
lines changed

10 files changed

+124
-69
lines changed
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
COSMOSDB_ACCOUNT_NAME="..."
2-
RESOURCE_GROUP_NAME="..."
3-
PRIMARY_REPLICA_LOCATION="..."
1+
COSMOSDB_ACCOUNT_NAME=""
2+
RESOURCE_GROUP_NAME=""
3+
PRIMARY_REPLICA_LOCATION=""
4+
ARM_SUBSCRIPTION_ID=""

infra/modules/providers/azure/cosmosdb/README.md

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -43,29 +43,4 @@ Supported arguments for this module are available in [variables.tf](./variables.
4343

4444
## Outputs
4545

46-
Once the deployments are completed successfully, the output for the current module will be in the format mentioned below:
47-
48-
``` json
49-
output "properties" {
50-
description = "Properties of the deployed CosmosDB account."
51-
type = object({
52-
cosmosdb = object({
53-
id = string
54-
endpoint = string
55-
primary_master_key = string
56-
connection_strings = string
57-
})
58-
})
59-
}
60-
61-
output "resource_group_name" {
62-
description = "The name of the CosmosDB account."
63-
type = string
64-
}
65-
66-
output "account_name" {
67-
description = "Properties of the deployed cluster"
68-
type = string
69-
}
70-
```
71-
46+
The output values for this module are available in [output.tf](output.tf)

infra/modules/providers/azure/cosmosdb/main.tf

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,30 @@
1+
module "azure-provider" {
2+
source = "../provider"
3+
}
4+
15
data "azurerm_resource_group" "cosmosdb" {
26
name = var.resource_group_name
37
}
48

9+
locals {
10+
offer_type = "Standard"
11+
ip_range_filter_Allow_Azure_Portal = "0.0.0.0"
12+
}
13+
514
resource "azurerm_cosmosdb_account" "cosmosdb" {
6-
name = var.name
15+
name = var.account_name
716
location = data.azurerm_resource_group.cosmosdb.location
817
resource_group_name = data.azurerm_resource_group.cosmosdb.name
9-
offer_type = "Standard"
18+
offer_type = local.offer_type
1019
kind = var.kind
1120

12-
enable_automatic_failover = var.automatic_failover
21+
enable_automatic_failover = var.automatic_failover
22+
is_virtual_network_filter_enabled = true
23+
ip_range_filter = local.ip_range_filter_Allow_Azure_Portal
24+
25+
virtual_network_rule {
26+
id = var.vnet_subnet_id
27+
}
1328

1429
consistency_policy {
1530
consistency_level = var.consistency_level
@@ -25,7 +40,7 @@ resource "azurerm_cosmosdb_sql_database" "cosmos_dbs" {
2540
depends_on = [azurerm_cosmosdb_account.cosmosdb]
2641
count = length(var.databases)
2742
name = var.databases[count.index].name
28-
account_name = var.name
43+
account_name = var.account_name
2944
resource_group_name = data.azurerm_resource_group.cosmosdb.name
3045
throughput = var.databases[count.index].throughput
3146
}
@@ -34,7 +49,7 @@ resource "azurerm_cosmosdb_sql_container" "cosmos_collections" {
3449
depends_on = [azurerm_cosmosdb_sql_database.cosmos_dbs]
3550
count = length(var.sql_collections)
3651
name = var.sql_collections[count.index].name
37-
account_name = var.name
52+
account_name = var.account_name
3853
database_name = var.sql_collections[count.index].database_name
3954
resource_group_name = data.azurerm_resource_group.cosmosdb.name
4055
partition_key_path = var.sql_collections[count.index].partition_key_path
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
resource_group_name = ""
2+
name = ""
3+
primary_replica_location = ""
4+
databases = [{
5+
name = ""
6+
throughput = [400]
7+
}]
8+
sql_collections = [{
9+
name = ""
10+
database_name = ""
11+
partition_key_path = ""
12+
throughput = [400]
13+
}]
14+
vnet_subnet_id = ""

infra/modules/providers/azure/cosmosdb/tests/integration/cosmos.go

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
package integration
22

33
import (
4-
"os"
54
"testing"
65

76
"github.com/Azure/azure-sdk-for-go/services/cosmos-db/mgmt/2015-04-08/documentdb"
87
httpClient "github.com/gruntwork-io/terratest/modules/http-helper"
9-
"github.com/microsoft/cobalt/test-harness/infratests"
108
"github.com/microsoft/cobalt/test-harness/terratest-extensions/modules/azure"
9+
"github.com/microsoft/terratest-abstraction/integration"
1110
"github.com/stretchr/testify/require"
1211
)
1312

14-
var subscription = os.Getenv("ARM_SUBSCRIPTION_ID")
15-
1613
// validateOutputs - Asserts that expected output values are present.
1714
func validateOutputs(t *testing.T, id string, endpoint string, primaryMasterKey string, connectionStrings []interface{}) {
1815
require.NotEqual(t, "", id, "ID not returned.")
@@ -37,7 +34,7 @@ func validateFailOverPriority(t *testing.T, failOverPolicy documentdb.FailoverPo
3734
}
3835

3936
// getModuleOutputs - Extracts the output variables from property map.
40-
func getModuleOutputs(output infratests.TerraformOutput, outputName string) (id string, endpoint string, primaryMasterKey string, connectionStrings []interface{}) {
37+
func getModuleOutputs(output integration.TerraformOutput, outputName string) (id string, endpoint string, primaryMasterKey string, connectionStrings []interface{}) {
4138
properties := output[outputName].(map[string]interface{})
4239
cosmosDBProperties := properties["cosmosdb"].(map[string]interface{})
4340

@@ -50,20 +47,20 @@ func getModuleOutputs(output infratests.TerraformOutput, outputName string) (id
5047
}
5148

5249
// validateServiceResponse - Attempt to perform a HTTP request to the live endpoint.
53-
func validateServiceResponse(t *testing.T, output infratests.TerraformOutput, outputName string) {
50+
func validateServiceResponse(t *testing.T, output integration.TerraformOutput, outputName string) {
5451
_, endpoint, _, _ := getModuleOutputs(output, outputName)
55-
statusCode, _ := httpClient.HttpGet(t, endpoint)
52+
statusCode, _ := httpClient.HttpGet(t, endpoint, nil)
5653

5754
require.Equal(t, 401, statusCode, "Service did not respond with the expected Unauthorized status code.")
5855
}
5956

6057
// InspectProvisionedCosmosDBAccount - Runs test assertions to validate that a provisioned CosmosDB Account
6158
// is operational.
62-
func InspectProvisionedCosmosDBAccount(resourceGroupOutputName, accountName, outputName string) func(t *testing.T, output infratests.TerraformOutput) {
63-
return func(t *testing.T, output infratests.TerraformOutput) {
59+
func InspectProvisionedCosmosDBAccount(subscriptionID, resourceGroupOutputName, accountName, outputName string) func(t *testing.T, output integration.TerraformOutput) {
60+
return func(t *testing.T, output integration.TerraformOutput) {
6461
resourceGroupName := output[resourceGroupOutputName].(string)
6562
accountName := output[accountName].(string)
66-
result := azure.GetCosmosDBAccount(t, subscription, resourceGroupName, accountName)
63+
result := azure.GetCosmosDBAccount(t, subscriptionID, resourceGroupName, accountName)
6764

6865
healthCheck(t, result.ProvisioningState)
6966

@@ -78,8 +75,8 @@ func InspectProvisionedCosmosDBAccount(resourceGroupOutputName, accountName, out
7875
}
7976

8077
// InspectCosmosDBModuleOutputs - Runs test assertions to validate that the module outputs are valid.
81-
func InspectCosmosDBModuleOutputs(outputName string) func(t *testing.T, output infratests.TerraformOutput) {
82-
return func(t *testing.T, output infratests.TerraformOutput) {
78+
func InspectCosmosDBModuleOutputs(outputName string) func(t *testing.T, output integration.TerraformOutput) {
79+
return func(t *testing.T, output integration.TerraformOutput) {
8380
id, endpoint, primaryMasterKey, connectionStrings := getModuleOutputs(output, outputName)
8481
validateOutputs(t, id, endpoint, primaryMasterKey, connectionStrings)
8582
}
Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
package integration
22

33
import (
4+
"os"
45
"testing"
56

67
"github.com/microsoft/cobalt/infra/modules/providers/azure/cosmosdb/tests"
7-
"github.com/microsoft/cobalt/test-harness/infratests"
8+
"github.com/microsoft/terratest-abstraction/integration"
89
)
910

11+
var subscriptionID = os.Getenv("ARM_SUBSCRIPTION_ID")
12+
1013
func TestCosmosDeployment(t *testing.T) {
1114

12-
testFixture := infratests.IntegrationTestFixture{
15+
testFixture := integration.IntegrationTestFixture{
1316
GoTest: t,
14-
TfOptions: tests.CosmosDBOptions,
17+
TfOptions: tests.CosmosDbTFOptions,
1518
ExpectedTfOutputCount: 3,
16-
TfOutputAssertions: []infratests.TerraformOutputValidation{
19+
TfOutputAssertions: []integration.TerraformOutputValidation{
1720
InspectCosmosDBModuleOutputs("properties"),
18-
InspectProvisionedCosmosDBAccount("resource_group_name", "account_name", "properties"),
21+
InspectProvisionedCosmosDBAccount(subscriptionID, "resource_group_name", "account_name", "properties"),
1922
},
2023
}
21-
infratests.RunIntegrationTests(&testFixture)
24+
integration.RunIntegrationTests(&testFixture)
2225
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
COSMOSDB_ACCOUNT_NAME = ""
2+
RESOURCE_GROUP_NAME = ""
3+
PRIMARY_REPLICA_LOCATION = ""
4+
ARM_SUBSCRIPTION_ID = ""

infra/modules/providers/azure/cosmosdb/tests/tf_options.go

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,11 @@ import (
66
"github.com/gruntwork-io/terratest/modules/terraform"
77
)
88

9-
// CosmosDBAccountName the name of the Database account in CosmosDB
10-
var cosmosDBAccountName = os.Getenv("COSMOSDB_ACCOUNT_NAME")
9+
// ResourceGroupName - The Resource Group Name
10+
var ResourceGroupName = os.Getenv("RESOURCE_GROUP_NAME")
1111

12-
// PrimaryReplicaLocation the Azure region for the primary replica
13-
var primaryReplicaLocation = os.Getenv("PRIMARY_REPLICA_LOCATION")
14-
15-
// ResourceGroupName the name of the resource group
16-
var resourceGroupName = os.Getenv("RESOURCE_GROUP_NAME")
17-
18-
// CosmosDBOptions terraform options used for cosmos integration testing
19-
var CosmosDBOptions = &terraform.Options{
12+
// CosmosDbTFOptions common terraform options used for unit and integration testing
13+
var CosmosDbTFOptions = &terraform.Options{
2014
TerraformDir: "../../",
21-
Upgrade: true,
22-
Vars: map[string]interface{}{
23-
"name": cosmosDBAccountName,
24-
"resource_group_name": resourceGroupName,
25-
"primary_replica_location": primaryReplicaLocation,
26-
},
15+
VarFiles: []string{"./tests/test.tfvars"},
2716
}

infra/modules/providers/azure/cosmosdb/variables.tf

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ variable "resource_group_name" {
33
type = string
44
}
55

6-
variable "name" {
6+
variable "account_name" {
77
description = "The name that CosmosDB will be created with."
88
type = string
99
}
@@ -48,4 +48,11 @@ variable "databases" {
4848
throughput = number
4949
}))
5050
default = []
51+
}
52+
53+
# VNET Restriction (optional)
54+
variable "vnet_subnet_id" {
55+
description = "The VNet integration subnet gateway identifier."
56+
type = string
57+
default = "10.2.0.0/16"
5158
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package azure
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/Azure/azure-sdk-for-go/services/cosmos-db/mgmt/2015-04-08/documentdb"
8+
)
9+
10+
// cosmosClientE - Connects to the cosmos client
11+
func cosmosClientE(subscriptionID string) (*documentdb.DatabaseAccountsClient, error) {
12+
authorizer, err := DeploymentServicePrincipalAuthorizer()
13+
if err != nil {
14+
return nil, err
15+
}
16+
17+
client := documentdb.NewDatabaseAccountsClient(subscriptionID)
18+
client.Authorizer = authorizer
19+
// Appends given user agent value to header for all future http calls to cosmos server for duration of integ tests.
20+
client.AddToUserAgent("integration-test-harness")
21+
return &client, err
22+
}
23+
24+
func getCosmosDBAccountE(subscriptionID string, resourceGroupName string, accountName string) (*documentdb.DatabaseAccount, error) {
25+
client, err := cosmosClientE(subscriptionID)
26+
27+
if err != nil {
28+
return nil, err
29+
}
30+
31+
ctx := context.Background()
32+
response, err := client.Get(ctx, resourceGroupName, accountName)
33+
34+
if err != nil {
35+
return nil, err
36+
}
37+
38+
return &response, nil
39+
}
40+
41+
// GetCosmosDBAccount - Retrieves the properties of a CosmosDB Database Account.
42+
func GetCosmosDBAccount(t *testing.T, subscriptionID string, resourceGroupName string, accountName string) *documentdb.DatabaseAccount {
43+
resource, err := getCosmosDBAccountE(subscriptionID, resourceGroupName, accountName)
44+
45+
if err != nil {
46+
t.Fatal(err)
47+
}
48+
49+
return resource
50+
}

0 commit comments

Comments
 (0)