Skip to content

Commit

Permalink
Add support for new properties of azurerm_cosmosdb_account (#14346)
Browse files Browse the repository at this point in the history
  • Loading branch information
Neil Ye authored Nov 26, 2021
1 parent a865c6d commit ce649d3
Show file tree
Hide file tree
Showing 3 changed files with 419 additions and 0 deletions.
152 changes: 152 additions & 0 deletions internal/services/cosmos/cosmosdb_account_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,53 @@ func resourceCosmosDbAccount() *pluginsdk.Resource {
}, true),
},

"analytical_storage": {
Type: pluginsdk.TypeList,
Optional: true,
Computed: true,
MaxItems: 1,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"schema_type": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
string(documentdb.AnalyticalStorageSchemaTypeWellDefined),
string(documentdb.AnalyticalStorageSchemaTypeFullFidelity),
}, false),
},
},
},
},

"capacity": {
Type: pluginsdk.TypeList,
Optional: true,
MaxItems: 1,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"total_throughput_limit": {
Type: pluginsdk.TypeInt,
Required: true,
ValidateFunc: validation.IntAtLeast(-1),
},
},
},
},

"default_identity_type": {
Type: pluginsdk.TypeString,
Optional: true,
Default: "FirstPartyIdentity",
ValidateFunc: validation.Any(
validation.StringMatch(regexp.MustCompile(`^UserAssignedIdentity(.)+$`), "It may start with `UserAssignedIdentity`"),
validation.StringInSlice([]string{
"FirstPartyIdentity",
"SystemAssignedIdentity",
}, false),
),
},

"kind": {
Type: pluginsdk.TypeString,
Optional: true,
Expand Down Expand Up @@ -371,6 +418,17 @@ func resourceCosmosDbAccount() *pluginsdk.Resource {
Computed: true,
ValidateFunc: validation.IntBetween(8, 720),
},

"storage_redundancy": {
Type: pluginsdk.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.StringInSlice([]string{
string(documentdb.BackupStorageRedundancyGeo),
string(documentdb.BackupStorageRedundancyLocal),
string(documentdb.BackupStorageRedundancyZone),
}, false),
},
},
},
},
Expand Down Expand Up @@ -576,10 +634,19 @@ func resourceCosmosDbAccountCreate(d *pluginsdk.ResourceData, meta interface{})
NetworkACLBypass: networkByPass,
NetworkACLBypassResourceIds: utils.ExpandStringSlice(d.Get("network_acl_bypass_ids").([]interface{})),
DisableLocalAuth: utils.Bool(disableLocalAuthentication),
DefaultIdentity: utils.String(d.Get("default_identity_type").(string)),
},
Tags: tags.Expand(t),
}

if v, ok := d.GetOk("analytical_storage"); ok {
account.DatabaseAccountCreateUpdateProperties.AnalyticalStorageConfiguration = expandCosmosDBAccountAnalyticalStorageConfiguration(v.([]interface{}))
}

if v, ok := d.GetOk("capacity"); ok {
account.DatabaseAccountCreateUpdateProperties.Capacity = expandCosmosDBAccountCapacity(v.([]interface{}))
}

if v, ok := d.GetOk("mongo_server_version"); ok {
account.DatabaseAccountCreateUpdateProperties.APIProperties = &documentdb.APIProperties{
ServerVersion: documentdb.ServerVersion(v.(string)),
Expand Down Expand Up @@ -710,10 +777,19 @@ func resourceCosmosDbAccountUpdate(d *pluginsdk.ResourceData, meta interface{})
NetworkACLBypass: networkByPass,
NetworkACLBypassResourceIds: utils.ExpandStringSlice(d.Get("network_acl_bypass_ids").([]interface{})),
DisableLocalAuth: utils.Bool(disableLocalAuthentication),
DefaultIdentity: utils.String(d.Get("default_identity_type").(string)),
},
Tags: tags.Expand(t),
}

if v, ok := d.GetOk("analytical_storage"); ok {
account.DatabaseAccountCreateUpdateProperties.AnalyticalStorageConfiguration = expandCosmosDBAccountAnalyticalStorageConfiguration(v.([]interface{}))
}

if v, ok := d.GetOk("capacity"); ok {
account.DatabaseAccountCreateUpdateProperties.Capacity = expandCosmosDBAccountCapacity(v.([]interface{}))
}

if keyVaultKeyIDRaw, ok := d.GetOk("key_vault_key_id"); ok {
keyVaultKey, err := keyVaultParse.ParseOptionallyVersionedNestedItemID(keyVaultKeyIDRaw.(string))
if err != nil {
Expand Down Expand Up @@ -832,6 +908,7 @@ func resourceCosmosDbAccountRead(d *pluginsdk.ResourceData, meta interface{}) er
d.Set("enable_free_tier", props.EnableFreeTier)
d.Set("analytical_storage_enabled", props.EnableAnalyticalStorage)
d.Set("public_network_access_enabled", props.PublicNetworkAccess == documentdb.PublicNetworkAccessEnabled)
d.Set("default_identity_type", props.DefaultIdentity)

if v := resp.IsVirtualNetworkFilterEnabled; v != nil {
d.Set("is_virtual_network_filter_enabled", props.IsVirtualNetworkFilterEnabled)
Expand All @@ -849,6 +926,14 @@ func resourceCosmosDbAccountRead(d *pluginsdk.ResourceData, meta interface{}) er
d.Set("enable_multiple_write_locations", props.EnableMultipleWriteLocations)
}

if err := d.Set("analytical_storage", flattenCosmosDBAccountAnalyticalStorageConfiguration(props.AnalyticalStorageConfiguration)); err != nil {
return fmt.Errorf("setting `analytical_storage`: %+v", err)
}

if err := d.Set("capacity", flattenCosmosDBAccountCapacity(props.Capacity)); err != nil {
return fmt.Errorf("setting `capacity`: %+v", err)
}

if err = d.Set("consistency_policy", flattenAzureRmCosmosDBAccountConsistencyPolicy(props.ConsistencyPolicy)); err != nil {
return fmt.Errorf("setting CosmosDB Account %q `consistency_policy` (Resource Group %q): %+v", id.Name, id.ResourceGroup, err)
}
Expand Down Expand Up @@ -1317,6 +1402,9 @@ func expandCosmosdbAccountBackup(input []interface{}, backupHasChange bool) (doc
if v := attr["retention_in_hours"].(int); v != 0 && !backupHasChange {
return nil, fmt.Errorf("`retention_in_hours` can not be set when `type` in `backup` is `Continuous`")
}
if v := attr["storage_redundancy"].(string); v != "" && !backupHasChange {
return nil, fmt.Errorf("`storage_redundancy` can not be set when `type` in `backup` is `Continuous`")
}
return documentdb.ContinuousModeBackupPolicy{
Type: documentdb.TypeContinuous,
}, nil
Expand All @@ -1327,6 +1415,7 @@ func expandCosmosdbAccountBackup(input []interface{}, backupHasChange bool) (doc
PeriodicModeProperties: &documentdb.PeriodicModeProperties{
BackupIntervalInMinutes: utils.Int32(int32(attr["interval_in_minutes"].(int))),
BackupRetentionIntervalInHours: utils.Int32(int32(attr["retention_in_hours"].(int))),
BackupStorageRedundancy: documentdb.BackupStorageRedundancy(attr["storage_redundancy"].(string)),
},
}, nil

Expand Down Expand Up @@ -1360,11 +1449,16 @@ func flattenCosmosdbAccountBackup(input documentdb.BasicBackupPolicy) ([]interfa
if v := policy.PeriodicModeProperties.BackupRetentionIntervalInHours; v != nil {
retention = int(*v)
}
var storageRedundancy documentdb.BackupStorageRedundancy
if policy.PeriodicModeProperties.BackupStorageRedundancy != "" {
storageRedundancy = policy.PeriodicModeProperties.BackupStorageRedundancy
}
return []interface{}{
map[string]interface{}{
"type": string(documentdb.TypePeriodic),
"interval_in_minutes": interval,
"retention_in_hours": retention,
"storage_redundancy": storageRedundancy,
},
}, nil

Expand Down Expand Up @@ -1408,3 +1502,61 @@ func flattenAzureRmdocumentdbMachineIdentity(identity *documentdb.ManagedService
},
}
}

func expandCosmosDBAccountAnalyticalStorageConfiguration(input []interface{}) *documentdb.AnalyticalStorageConfiguration {
if len(input) == 0 {
return nil
}

v := input[0].(map[string]interface{})

return &documentdb.AnalyticalStorageConfiguration{
SchemaType: documentdb.AnalyticalStorageSchemaType(v["schema_type"].(string)),
}
}

func expandCosmosDBAccountCapacity(input []interface{}) *documentdb.Capacity {
if len(input) == 0 {
return nil
}

v := input[0].(map[string]interface{})

return &documentdb.Capacity{
TotalThroughputLimit: utils.Int32(int32(v["total_throughput_limit"].(int))),
}
}

func flattenCosmosDBAccountAnalyticalStorageConfiguration(input *documentdb.AnalyticalStorageConfiguration) []interface{} {
if input == nil {
return make([]interface{}, 0)
}

var schemaType documentdb.AnalyticalStorageSchemaType
if input.SchemaType != "" {
schemaType = input.SchemaType
}

return []interface{}{
map[string]interface{}{
"schema_type": schemaType,
},
}
}

func flattenCosmosDBAccountCapacity(input *documentdb.Capacity) []interface{} {
if input == nil {
return make([]interface{}, 0)
}

var totalThroughputLimit int32
if input.TotalThroughputLimit != nil {
totalThroughputLimit = *input.TotalThroughputLimit
}

return []interface{}{
map[string]interface{}{
"total_throughput_limit": totalThroughputLimit,
},
}
}
Loading

0 comments on commit ce649d3

Please sign in to comment.