Skip to content

Commit

Permalink
Fix lables tests and apply new models to alloydb resources (#9123) (#…
Browse files Browse the repository at this point in the history
…6413)

Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored and c2thorn committed Sep 29, 2023
1 parent 52fedf6 commit 967fd28
Show file tree
Hide file tree
Showing 79 changed files with 416 additions and 153 deletions.
3 changes: 3 additions & 0 deletions .changelog/9123.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:none

```
7 changes: 4 additions & 3 deletions google-beta/provider/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,10 @@ func TestAccProviderUserProjectOverride(t *testing.T) {
Config: testAccProviderUserProjectOverride_step2(accessToken, pid, true, topicName),
},
{
ResourceName: "google_pubsub_topic.project-2-topic",
ImportState: true,
ImportStateVerify: true,
ResourceName: "google_pubsub_topic.project-2-topic",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"labels", "terraform_labels"},
},
{
Config: testAccProviderUserProjectOverride_step3(accessToken, true),
Expand Down
161 changes: 122 additions & 39 deletions google-beta/services/alloydb/resource_alloydb_backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ func ResourceAlloydbBackup() *schema.Resource {
},

CustomizeDiff: customdiff.All(
tpgresource.SetLabelsDiff,
tpgresource.SetAnnotationsDiff,
tpgresource.DefaultProviderProject,
),

Expand Down Expand Up @@ -77,7 +79,11 @@ func ResourceAlloydbBackup() *schema.Resource {
Type: schema.TypeMap,
Optional: true,
Description: `Annotations to allow client tools to store small amount of arbitrary data. This is distinct from labels. https://google.aip.dev/128
An object containing a list of "key": value pairs. Example: { "name": "wrench", "mass": "1.3kg", "count": "3" }.`,
An object containing a list of "key": value pairs. Example: { "name": "wrench", "mass": "1.3kg", "count": "3" }.
**Note**: This field is non-authoritative, and will only manage the annotations present in your configuration.
Please refer to the field 'effective_annotations' for all of the annotations present on the resource.`,
Elem: &schema.Schema{Type: schema.TypeString},
},
"description": {
Expand Down Expand Up @@ -107,10 +113,14 @@ An object containing a list of "key": value pairs. Example: { "name": "wrench",
},
},
"labels": {
Type: schema.TypeMap,
Optional: true,
Description: `User-defined labels for the alloydb backup. An object containing a list of "key": value pairs. Example: { "name": "wrench", "mass": "1.3kg", "count": "3" }.`,
Elem: &schema.Schema{Type: schema.TypeString},
Type: schema.TypeMap,
Optional: true,
Description: `User-defined labels for the alloydb backup. An object containing a list of "key": value pairs. Example: { "name": "wrench", "mass": "1.3kg", "count": "3" }.
**Note**: This field is non-authoritative, and will only manage the labels present in your configuration.
Please refer to the field 'effective_labels' for all of the labels present on the resource.`,
Elem: &schema.Schema{Type: schema.TypeString},
},
"type": {
Type: schema.TypeString,
Expand All @@ -136,6 +146,18 @@ Examples: "2014-10-02T15:01:23Z" and "2014-10-02T15:01:23.045123456Z".`,
Description: `Output only. Delete time stamp. A timestamp in RFC3339 UTC "Zulu" format, with nanosecond resolution and up to nine fractional digits.
Examples: "2014-10-02T15:01:23Z" and "2014-10-02T15:01:23.045123456Z".`,
},
"effective_annotations": {
Type: schema.TypeMap,
Computed: true,
Description: `All of annotations (key/value pairs) present on the resource in GCP, including the annotations configured through Terraform, other clients and services.`,
Elem: &schema.Schema{Type: schema.TypeString},
},
"effective_labels": {
Type: schema.TypeMap,
Computed: true,
Description: `All of labels (key/value pairs) present on the resource in GCP, including the labels configured through Terraform, other clients and services.`,
Elem: &schema.Schema{Type: schema.TypeString},
},
"encryption_info": {
Type: schema.TypeList,
Computed: true,
Expand Down Expand Up @@ -210,6 +232,13 @@ This can happen due to user-triggered updates or system actions like failover or
Computed: true,
Description: `Output only. The current state of the backup.`,
},
"terraform_labels": {
Type: schema.TypeMap,
Computed: true,
Description: `The combination of labels configured directly on the resource
and default labels configured on the provider.`,
Elem: &schema.Schema{Type: schema.TypeString},
},
"uid": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -246,12 +275,6 @@ func resourceAlloydbBackupCreate(d *schema.ResourceData, meta interface{}) error
} 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 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 err
Expand All @@ -276,10 +299,16 @@ func resourceAlloydbBackupCreate(d *schema.ResourceData, meta interface{}) error
} 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)
labelsProp, err := expandAlloydbBackupEffectiveLabels(d.Get("effective_labels"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("annotations"); !tpgresource.IsEmptyValue(reflect.ValueOf(annotationsProp)) && (ok || !reflect.DeepEqual(v, annotationsProp)) {
} else if v, ok := d.GetOkExists("effective_labels"); !tpgresource.IsEmptyValue(reflect.ValueOf(labelsProp)) && (ok || !reflect.DeepEqual(v, labelsProp)) {
obj["labels"] = labelsProp
}
annotationsProp, err := expandAlloydbBackupEffectiveAnnotations(d.Get("effective_annotations"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("effective_annotations"); !tpgresource.IsEmptyValue(reflect.ValueOf(annotationsProp)) && (ok || !reflect.DeepEqual(v, annotationsProp)) {
obj["annotations"] = annotationsProp
}

Expand Down Expand Up @@ -442,6 +471,15 @@ func resourceAlloydbBackupRead(d *schema.ResourceData, meta interface{}) error {
if err := d.Set("expiry_quantity", flattenAlloydbBackupExpiryQuantity(res["expiryQuantity"], d, config)); err != nil {
return fmt.Errorf("Error reading Backup: %s", err)
}
if err := d.Set("terraform_labels", flattenAlloydbBackupTerraformLabels(res["labels"], d, config)); err != nil {
return fmt.Errorf("Error reading Backup: %s", err)
}
if err := d.Set("effective_labels", flattenAlloydbBackupEffectiveLabels(res["labels"], d, config)); err != nil {
return fmt.Errorf("Error reading Backup: %s", err)
}
if err := d.Set("effective_annotations", flattenAlloydbBackupEffectiveAnnotations(res["annotations"], d, config)); err != nil {
return fmt.Errorf("Error reading Backup: %s", err)
}

return nil
}
Expand All @@ -468,12 +506,6 @@ func resourceAlloydbBackupUpdate(d *schema.ResourceData, meta interface{}) error
} else if v, ok := d.GetOkExists("display_name"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, displayNameProp)) {
obj["displayName"] = displayNameProp
}
labelsProp, err := expandAlloydbBackupLabels(d.Get("labels"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("labels"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, labelsProp)) {
obj["labels"] = labelsProp
}
typeProp, err := expandAlloydbBackupType(d.Get("type"), d, config)
if err != nil {
return err
Expand All @@ -492,10 +524,16 @@ func resourceAlloydbBackupUpdate(d *schema.ResourceData, meta interface{}) error
} else if v, ok := d.GetOkExists("encryption_config"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, encryptionConfigProp)) {
obj["encryptionConfig"] = encryptionConfigProp
}
annotationsProp, err := expandAlloydbBackupAnnotations(d.Get("annotations"), d, config)
labelsProp, err := expandAlloydbBackupEffectiveLabels(d.Get("effective_labels"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("effective_labels"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, labelsProp)) {
obj["labels"] = labelsProp
}
annotationsProp, err := expandAlloydbBackupEffectiveAnnotations(d.Get("effective_annotations"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("annotations"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, annotationsProp)) {
} else if v, ok := d.GetOkExists("effective_annotations"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, annotationsProp)) {
obj["annotations"] = annotationsProp
}

Expand All @@ -516,10 +554,6 @@ func resourceAlloydbBackupUpdate(d *schema.ResourceData, meta interface{}) error
updateMask = append(updateMask, "displayName")
}

if d.HasChange("labels") {
updateMask = append(updateMask, "labels")
}

if d.HasChange("type") {
updateMask = append(updateMask, "type")
}
Expand All @@ -532,7 +566,11 @@ func resourceAlloydbBackupUpdate(d *schema.ResourceData, meta interface{}) error
updateMask = append(updateMask, "encryptionConfig")
}

if d.HasChange("annotations") {
if d.HasChange("effective_labels") {
updateMask = append(updateMask, "labels")
}

if d.HasChange("effective_annotations") {
updateMask = append(updateMask, "annotations")
}
// updateMask is a URL parameter but not present in the schema, so ReplaceVars
Expand Down Expand Up @@ -672,7 +710,18 @@ func flattenAlloydbBackupDeleteTime(v interface{}, d *schema.ResourceData, confi
}

func flattenAlloydbBackupLabels(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
return v
if v == nil {
return v
}

transformed := make(map[string]interface{})
if l, ok := d.GetOkExists("labels"); ok {
for k := range l.(map[string]interface{}) {
transformed[k] = v.(map[string]interface{})[k]
}
}

return transformed
}

func flattenAlloydbBackupState(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
Expand Down Expand Up @@ -744,7 +793,18 @@ func flattenAlloydbBackupEtag(v interface{}, d *schema.ResourceData, config *tra
}

func flattenAlloydbBackupAnnotations(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
return v
if v == nil {
return v
}

transformed := make(map[string]interface{})
if l, ok := d.GetOkExists("annotations"); ok {
for k := range l.(map[string]interface{}) {
transformed[k] = v.(map[string]interface{})[k]
}
}

return transformed
}

func flattenAlloydbBackupSizeBytes(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
Expand Down Expand Up @@ -804,19 +864,31 @@ func flattenAlloydbBackupExpiryQuantityTotalRetentionCount(v interface{}, d *sch
return v // let terraform core handle it otherwise
}

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

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

transformed := make(map[string]interface{})
if l, ok := d.GetOkExists("terraform_labels"); ok {
for k := range l.(map[string]interface{}) {
transformed[k] = v.(map[string]interface{})[k]
}
}
return m, nil

return transformed
}

func flattenAlloydbBackupEffectiveLabels(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
return v
}

func flattenAlloydbBackupEffectiveAnnotations(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
return v
}

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

func expandAlloydbBackupType(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
Expand Down Expand Up @@ -854,7 +926,18 @@ func expandAlloydbBackupEncryptionConfigKmsKeyName(v interface{}, d tpgresource.
return v, nil
}

func expandAlloydbBackupAnnotations(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (map[string]string, error) {
func expandAlloydbBackupEffectiveLabels(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 expandAlloydbBackupEffectiveAnnotations(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (map[string]string, error) {
if v == nil {
return map[string]string{}, nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func TestAccAlloydbBackup_alloydbBackupBasicExample(t *testing.T) {
ResourceName: "google_alloydb_backup.default",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"backup_id", "location", "reconciling", "update_time"},
ImportStateVerifyIgnore: []string{"backup_id", "location", "reconciling", "update_time", "labels", "annotations", "terraform_labels"},
},
},
})
Expand Down Expand Up @@ -118,7 +118,7 @@ func TestAccAlloydbBackup_alloydbBackupFullExample(t *testing.T) {
ResourceName: "google_alloydb_backup.default",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"backup_id", "location", "reconciling", "update_time"},
ImportStateVerifyIgnore: []string{"backup_id", "location", "reconciling", "update_time", "labels", "annotations", "terraform_labels"},
},
},
})
Expand Down
Loading

0 comments on commit 967fd28

Please sign in to comment.