Skip to content

Commit

Permalink
Bridge gap between Terraform and API for AlloyDB (#8781) (#1494)
Browse files Browse the repository at this point in the history
* Add display name to backup

* Add alll the fields for backup

* Fix indentation

* Fix indentation

* Fix documentation and  attribute type

* Add attributes for cluster and instance with tests

* Remove annotations and displayName as not supported by GET API

* Misc

* Mark queryInsightsConfig as default_from_api instead of its sub-fields

* Mark Backup type as default_from_api

* Delete ignore_only section in alloydb_backup_basic

* Re-run tests

* Comment out tests failing due to networking service bug

Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored Sep 25, 2023
1 parent a47509a commit 9eaf13d
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,30 +54,48 @@ func GetAlloydbBackupCaiObject(d tpgresource.TerraformResourceData, config *tran

func GetAlloydbBackupApiObject(d tpgresource.TerraformResourceData, config *transport_tpg.Config) (map[string]interface{}, error) {
obj := make(map[string]interface{})
clusterNameProp, err := expandAlloydbBackupClusterName(d.Get("cluster_name"), d, config)
displayNameProp, err := expandAlloydbBackupDisplayName(d.Get("display_name"), d, config)
if err != nil {
return nil, err
} else if v, ok := d.GetOkExists("cluster_name"); !tpgresource.IsEmptyValue(reflect.ValueOf(clusterNameProp)) && (ok || !reflect.DeepEqual(v, clusterNameProp)) {
obj["clusterName"] = clusterNameProp
} else if v, ok := d.GetOkExists("display_name"); !tpgresource.IsEmptyValue(reflect.ValueOf(displayNameProp)) && (ok || !reflect.DeepEqual(v, displayNameProp)) {
obj["displayName"] = displayNameProp
}
labelsProp, err := expandAlloydbBackupLabels(d.Get("labels"), d, config)
if err != nil {
return nil, err
} else if v, ok := d.GetOkExists("labels"); !tpgresource.IsEmptyValue(reflect.ValueOf(labelsProp)) && (ok || !reflect.DeepEqual(v, labelsProp)) {
obj["labels"] = labelsProp
}
typeProp, err := expandAlloydbBackupType(d.Get("type"), d, config)
if err != nil {
return nil, err
} else if v, ok := d.GetOkExists("type"); !tpgresource.IsEmptyValue(reflect.ValueOf(typeProp)) && (ok || !reflect.DeepEqual(v, typeProp)) {
obj["type"] = typeProp
}
descriptionProp, err := expandAlloydbBackupDescription(d.Get("description"), d, config)
if err != nil {
return nil, err
} else if v, ok := d.GetOkExists("description"); !tpgresource.IsEmptyValue(reflect.ValueOf(descriptionProp)) && (ok || !reflect.DeepEqual(v, descriptionProp)) {
obj["description"] = descriptionProp
}
clusterNameProp, err := expandAlloydbBackupClusterName(d.Get("cluster_name"), d, config)
if err != nil {
return nil, err
} else if v, ok := d.GetOkExists("cluster_name"); !tpgresource.IsEmptyValue(reflect.ValueOf(clusterNameProp)) && (ok || !reflect.DeepEqual(v, clusterNameProp)) {
obj["clusterName"] = clusterNameProp
}
encryptionConfigProp, err := expandAlloydbBackupEncryptionConfig(d.Get("encryption_config"), d, config)
if err != nil {
return nil, err
} else if v, ok := d.GetOkExists("encryption_config"); !tpgresource.IsEmptyValue(reflect.ValueOf(encryptionConfigProp)) && (ok || !reflect.DeepEqual(v, encryptionConfigProp)) {
obj["encryptionConfig"] = encryptionConfigProp
}
annotationsProp, err := expandAlloydbBackupAnnotations(d.Get("annotations"), d, config)
if err != nil {
return nil, err
} else if v, ok := d.GetOkExists("annotations"); !tpgresource.IsEmptyValue(reflect.ValueOf(annotationsProp)) && (ok || !reflect.DeepEqual(v, annotationsProp)) {
obj["annotations"] = annotationsProp
}

return resourceAlloydbBackupEncoder(d, config, obj)
}
Expand All @@ -88,7 +106,7 @@ func resourceAlloydbBackupEncoder(d tpgresource.TerraformResourceData, meta inte
return obj, nil
}

func expandAlloydbBackupClusterName(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
func expandAlloydbBackupDisplayName(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
return v, nil
}

Expand All @@ -103,10 +121,18 @@ func expandAlloydbBackupLabels(v interface{}, d tpgresource.TerraformResourceDat
return m, nil
}

func expandAlloydbBackupType(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
return v, nil
}

func expandAlloydbBackupDescription(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
return v, nil
}

func expandAlloydbBackupClusterName(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
return v, nil
}

func expandAlloydbBackupEncryptionConfig(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
l := v.([]interface{})
if len(l) == 0 || l[0] == nil {
Expand All @@ -129,3 +155,14 @@ func expandAlloydbBackupEncryptionConfig(v interface{}, d tpgresource.TerraformR
func expandAlloydbBackupEncryptionConfigKmsKeyName(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
return v, nil
}

func expandAlloydbBackupAnnotations(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (map[string]string, error) {
if v == nil {
return map[string]string{}, nil
}
m := make(map[string]string)
for k, val := range v.(map[string]interface{}) {
m[k] = val.(string)
}
return m, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,18 @@ func GetAlloydbClusterApiObject(d tpgresource.TerraformResourceData, config *tra
} else if v, ok := d.GetOkExists("display_name"); !tpgresource.IsEmptyValue(reflect.ValueOf(displayNameProp)) && (ok || !reflect.DeepEqual(v, displayNameProp)) {
obj["displayName"] = displayNameProp
}
etagProp, err := expandAlloydbClusterEtag(d.Get("etag"), d, config)
if err != nil {
return nil, err
} else if v, ok := d.GetOkExists("etag"); !tpgresource.IsEmptyValue(reflect.ValueOf(etagProp)) && (ok || !reflect.DeepEqual(v, etagProp)) {
obj["etag"] = etagProp
}
annotationsProp, err := expandAlloydbClusterAnnotations(d.Get("annotations"), d, config)
if err != nil {
return nil, err
} else if v, ok := d.GetOkExists("annotations"); !tpgresource.IsEmptyValue(reflect.ValueOf(annotationsProp)) && (ok || !reflect.DeepEqual(v, annotationsProp)) {
obj["annotations"] = annotationsProp
}
initialUserProp, err := expandAlloydbClusterInitialUser(d.Get("initial_user"), d, config)
if err != nil {
return nil, err
Expand Down Expand Up @@ -194,6 +206,21 @@ func expandAlloydbClusterDisplayName(v interface{}, d tpgresource.TerraformResou
return v, nil
}

func expandAlloydbClusterEtag(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
return v, nil
}

func expandAlloydbClusterAnnotations(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (map[string]string, error) {
if v == nil {
return map[string]string{}, nil
}
m := make(map[string]string)
for k, val := range v.(map[string]interface{}) {
m[k] = val.(string)
}
return m, nil
}

func expandAlloydbClusterInitialUser(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
l := v.([]interface{})
if len(l) == 0 || l[0] == nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ func GetAlloydbInstanceApiObject(d tpgresource.TerraformResourceData, config *tr
} else if v, ok := d.GetOkExists("instance_type"); !tpgresource.IsEmptyValue(reflect.ValueOf(instanceTypeProp)) && (ok || !reflect.DeepEqual(v, instanceTypeProp)) {
obj["instanceType"] = instanceTypeProp
}
queryInsightsConfigProp, err := expandAlloydbInstanceQueryInsightsConfig(d.Get("query_insights_config"), d, config)
if err != nil {
return nil, err
} else if v, ok := d.GetOkExists("query_insights_config"); !tpgresource.IsEmptyValue(reflect.ValueOf(queryInsightsConfigProp)) && (ok || !reflect.DeepEqual(v, queryInsightsConfigProp)) {
obj["queryInsightsConfig"] = queryInsightsConfigProp
}
readPoolConfigProp, err := expandAlloydbInstanceReadPoolConfig(d.Get("read_pool_config"), d, config)
if err != nil {
return nil, err
Expand Down Expand Up @@ -161,6 +167,62 @@ func expandAlloydbInstanceInstanceType(v interface{}, d tpgresource.TerraformRes
return v, nil
}

func expandAlloydbInstanceQueryInsightsConfig(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
l := v.([]interface{})
if len(l) == 0 || l[0] == nil {
return nil, nil
}
raw := l[0]
original := raw.(map[string]interface{})
transformed := make(map[string]interface{})

transformedQueryStringLength, err := expandAlloydbInstanceQueryInsightsConfigQueryStringLength(original["query_string_length"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedQueryStringLength); val.IsValid() && !tpgresource.IsEmptyValue(val) {
transformed["queryStringLength"] = transformedQueryStringLength
}

transformedRecordApplicationTags, err := expandAlloydbInstanceQueryInsightsConfigRecordApplicationTags(original["record_application_tags"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedRecordApplicationTags); val.IsValid() && !tpgresource.IsEmptyValue(val) {
transformed["recordApplicationTags"] = transformedRecordApplicationTags
}

transformedRecordClientAddress, err := expandAlloydbInstanceQueryInsightsConfigRecordClientAddress(original["record_client_address"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedRecordClientAddress); val.IsValid() && !tpgresource.IsEmptyValue(val) {
transformed["recordClientAddress"] = transformedRecordClientAddress
}

transformedQueryPlansPerMinute, err := expandAlloydbInstanceQueryInsightsConfigQueryPlansPerMinute(original["query_plans_per_minute"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedQueryPlansPerMinute); val.IsValid() && !tpgresource.IsEmptyValue(val) {
transformed["queryPlansPerMinute"] = transformedQueryPlansPerMinute
}

return transformed, nil
}

func expandAlloydbInstanceQueryInsightsConfigQueryStringLength(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
return v, nil
}

func expandAlloydbInstanceQueryInsightsConfigRecordApplicationTags(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
return v, nil
}

func expandAlloydbInstanceQueryInsightsConfigRecordClientAddress(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
return v, nil
}

func expandAlloydbInstanceQueryInsightsConfigQueryPlansPerMinute(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
return v, nil
}

func expandAlloydbInstanceReadPoolConfig(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
l := v.([]interface{})
if len(l) == 0 || l[0] == nil {
Expand Down

0 comments on commit 9eaf13d

Please sign in to comment.