Skip to content

Commit

Permalink
Apply new annotations model to more resources (#8931) (#6280)
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored Sep 15, 2023
1 parent 16d2d27 commit aaccf52
Show file tree
Hide file tree
Showing 42 changed files with 694 additions and 364 deletions.
3 changes: 3 additions & 0 deletions .changelog/8931.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:none

```
71 changes: 48 additions & 23 deletions google-beta/services/cloudrunv2/resource_cloud_run_v2_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func ResourceCloudRunV2Job() *schema.Resource {
},

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

Expand Down Expand Up @@ -546,6 +547,12 @@ A timestamp in RFC3339 UTC "Zulu" format, with nanosecond resolution and up to n
Computed: true,
Description: `The deletion time.`,
},
"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},
},
"etag": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -701,12 +708,6 @@ func resourceCloudRunV2JobCreate(d *schema.ResourceData, meta interface{}) error
} else if v, ok := d.GetOkExists("labels"); !tpgresource.IsEmptyValue(reflect.ValueOf(labelsProp)) && (ok || !reflect.DeepEqual(v, labelsProp)) {
obj["labels"] = labelsProp
}
annotationsProp, err := expandCloudRunV2JobAnnotations(d.Get("annotations"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("annotations"); !tpgresource.IsEmptyValue(reflect.ValueOf(annotationsProp)) && (ok || !reflect.DeepEqual(v, annotationsProp)) {
obj["annotations"] = annotationsProp
}
clientProp, err := expandCloudRunV2JobClient(d.Get("client"), d, config)
if err != nil {
return err
Expand Down Expand Up @@ -737,6 +738,12 @@ func resourceCloudRunV2JobCreate(d *schema.ResourceData, meta interface{}) error
} else if v, ok := d.GetOkExists("template"); !tpgresource.IsEmptyValue(reflect.ValueOf(templateProp)) && (ok || !reflect.DeepEqual(v, templateProp)) {
obj["template"] = templateProp
}
annotationsProp, err := expandCloudRunV2JobEffectiveAnnotations(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
}

url, err := tpgresource.ReplaceVars(d, config, "{{CloudRunV2BasePath}}projects/{{project}}/locations/{{location}}/jobs?jobId={{name}}")
if err != nil {
Expand Down Expand Up @@ -908,6 +915,9 @@ func resourceCloudRunV2JobRead(d *schema.ResourceData, meta interface{}) error {
if err := d.Set("etag", flattenCloudRunV2JobEtag(res["etag"], d, config)); err != nil {
return fmt.Errorf("Error reading Job: %s", err)
}
if err := d.Set("effective_annotations", flattenCloudRunV2JobEffectiveAnnotations(res["annotations"], d, config)); err != nil {
return fmt.Errorf("Error reading Job: %s", err)
}

return nil
}
Expand All @@ -934,12 +944,6 @@ func resourceCloudRunV2JobUpdate(d *schema.ResourceData, meta interface{}) error
} else if v, ok := d.GetOkExists("labels"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, labelsProp)) {
obj["labels"] = labelsProp
}
annotationsProp, err := expandCloudRunV2JobAnnotations(d.Get("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)) {
obj["annotations"] = annotationsProp
}
clientProp, err := expandCloudRunV2JobClient(d.Get("client"), d, config)
if err != nil {
return err
Expand Down Expand Up @@ -970,6 +974,12 @@ func resourceCloudRunV2JobUpdate(d *schema.ResourceData, meta interface{}) error
} else if v, ok := d.GetOkExists("template"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, templateProp)) {
obj["template"] = templateProp
}
annotationsProp, err := expandCloudRunV2JobEffectiveAnnotations(d.Get("effective_annotations"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("effective_annotations"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, annotationsProp)) {
obj["annotations"] = annotationsProp
}

url, err := tpgresource.ReplaceVars(d, config, "{{CloudRunV2BasePath}}projects/{{project}}/locations/{{location}}/jobs/{{name}}")
if err != nil {
Expand Down Expand Up @@ -1096,7 +1106,18 @@ func flattenCloudRunV2JobLabels(v interface{}, d *schema.ResourceData, config *t
}

func flattenCloudRunV2JobAnnotations(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 flattenCloudRunV2JobCreateTime(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
Expand Down Expand Up @@ -1820,18 +1841,11 @@ func flattenCloudRunV2JobEtag(v interface{}, d *schema.ResourceData, config *tra
return v
}

func expandCloudRunV2JobLabels(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 flattenCloudRunV2JobEffectiveAnnotations(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
return v
}

func expandCloudRunV2JobAnnotations(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (map[string]string, error) {
func expandCloudRunV2JobLabels(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (map[string]string, error) {
if v == nil {
return map[string]string{}, nil
}
Expand Down Expand Up @@ -2578,3 +2592,14 @@ func expandCloudRunV2JobTemplateTemplateVpcAccessEgress(v interface{}, d tpgreso
func expandCloudRunV2JobTemplateTemplateMaxRetries(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
return v, nil
}

func expandCloudRunV2JobEffectiveAnnotations(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 @@ -49,7 +49,7 @@ func TestAccCloudRunV2Job_cloudrunv2JobBasicExample(t *testing.T) {
ResourceName: "google_cloud_run_v2_job.default",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"name", "location"},
ImportStateVerifyIgnore: []string{"name", "annotations", "location"},
},
},
})
Expand Down Expand Up @@ -98,7 +98,7 @@ func TestAccCloudRunV2Job_cloudrunv2JobSqlExample(t *testing.T) {
ResourceName: "google_cloud_run_v2_job.default",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"name", "location"},
ImportStateVerifyIgnore: []string{"name", "annotations", "location"},
},
},
})
Expand Down Expand Up @@ -204,7 +204,7 @@ func TestAccCloudRunV2Job_cloudrunv2JobVpcaccessExample(t *testing.T) {
ResourceName: "google_cloud_run_v2_job.default",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"name", "location"},
ImportStateVerifyIgnore: []string{"name", "annotations", "location"},
},
},
})
Expand Down Expand Up @@ -277,7 +277,7 @@ func TestAccCloudRunV2Job_cloudrunv2JobSecretExample(t *testing.T) {
ResourceName: "google_cloud_run_v2_job.default",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"name", "location"},
ImportStateVerifyIgnore: []string{"name", "annotations", "location"},
},
},
})
Expand Down Expand Up @@ -368,7 +368,7 @@ func TestAccCloudRunV2Job_cloudrunv2JobEmptydirExample(t *testing.T) {
ResourceName: "google_cloud_run_v2_job.default",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"name", "location"},
ImportStateVerifyIgnore: []string{"name", "annotations", "location"},
},
},
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestAccCloudRunV2Job_cloudrunv2JobFullUpdate(t *testing.T) {
ResourceName: "google_cloud_run_v2_job.default",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"location", "launch_stage"},
ImportStateVerifyIgnore: []string{"location", "launch_stage", "annotations"},
},
{
Config: testAccCloudRunV2Job_cloudrunv2JobFullUpdate(context),
Expand All @@ -37,7 +37,7 @@ func TestAccCloudRunV2Job_cloudrunv2JobFullUpdate(t *testing.T) {
ResourceName: "google_cloud_run_v2_job.default",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"location", "launch_stage"},
ImportStateVerifyIgnore: []string{"location", "launch_stage", "annotations"},
},
},
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func ResourceGkeonpremBareMetalAdminCluster() *schema.Resource {
},

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

Expand Down Expand Up @@ -516,6 +517,12 @@ automatically created during cluster creation.`,
Computed: true,
Description: `The time the cluster was deleted, in RFC3339 text format.`,
},
"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},
},
"endpoint": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -731,12 +738,6 @@ func resourceGkeonpremBareMetalAdminClusterCreate(d *schema.ResourceData, meta i
} else if v, ok := d.GetOkExists("bare_metal_version"); !tpgresource.IsEmptyValue(reflect.ValueOf(bareMetalVersionProp)) && (ok || !reflect.DeepEqual(v, bareMetalVersionProp)) {
obj["bareMetalVersion"] = bareMetalVersionProp
}
annotationsProp, err := expandGkeonpremBareMetalAdminClusterAnnotations(d.Get("annotations"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("annotations"); !tpgresource.IsEmptyValue(reflect.ValueOf(annotationsProp)) && (ok || !reflect.DeepEqual(v, annotationsProp)) {
obj["annotations"] = annotationsProp
}
networkConfigProp, err := expandGkeonpremBareMetalAdminClusterNetworkConfig(d.Get("network_config"), d, config)
if err != nil {
return err
Expand Down Expand Up @@ -797,6 +798,12 @@ func resourceGkeonpremBareMetalAdminClusterCreate(d *schema.ResourceData, meta i
} else if v, ok := d.GetOkExists("security_config"); !tpgresource.IsEmptyValue(reflect.ValueOf(securityConfigProp)) && (ok || !reflect.DeepEqual(v, securityConfigProp)) {
obj["securityConfig"] = securityConfigProp
}
annotationsProp, err := expandGkeonpremBareMetalAdminClusterEffectiveAnnotations(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
}

url, err := tpgresource.ReplaceVars(d, config, "{{GkeonpremBasePath}}projects/{{project}}/locations/{{location}}/bareMetalAdminClusters?bare_metal_admin_cluster_id={{name}}")
if err != nil {
Expand Down Expand Up @@ -974,6 +981,9 @@ func resourceGkeonpremBareMetalAdminClusterRead(d *schema.ResourceData, meta int
if err := d.Set("security_config", flattenGkeonpremBareMetalAdminClusterSecurityConfig(res["securityConfig"], d, config)); err != nil {
return fmt.Errorf("Error reading BareMetalAdminCluster: %s", err)
}
if err := d.Set("effective_annotations", flattenGkeonpremBareMetalAdminClusterEffectiveAnnotations(res["annotations"], d, config)); err != nil {
return fmt.Errorf("Error reading BareMetalAdminCluster: %s", err)
}

return nil
}
Expand Down Expand Up @@ -1006,12 +1016,6 @@ func resourceGkeonpremBareMetalAdminClusterUpdate(d *schema.ResourceData, meta i
} else if v, ok := d.GetOkExists("bare_metal_version"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, bareMetalVersionProp)) {
obj["bareMetalVersion"] = bareMetalVersionProp
}
annotationsProp, err := expandGkeonpremBareMetalAdminClusterAnnotations(d.Get("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)) {
obj["annotations"] = annotationsProp
}
networkConfigProp, err := expandGkeonpremBareMetalAdminClusterNetworkConfig(d.Get("network_config"), d, config)
if err != nil {
return err
Expand Down Expand Up @@ -1072,6 +1076,12 @@ func resourceGkeonpremBareMetalAdminClusterUpdate(d *schema.ResourceData, meta i
} else if v, ok := d.GetOkExists("security_config"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, securityConfigProp)) {
obj["securityConfig"] = securityConfigProp
}
annotationsProp, err := expandGkeonpremBareMetalAdminClusterEffectiveAnnotations(d.Get("effective_annotations"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("effective_annotations"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, annotationsProp)) {
obj["annotations"] = annotationsProp
}

url, err := tpgresource.ReplaceVars(d, config, "{{GkeonpremBasePath}}projects/{{project}}/locations/{{location}}/bareMetalAdminClusters/{{name}}")
if err != nil {
Expand All @@ -1089,10 +1099,6 @@ func resourceGkeonpremBareMetalAdminClusterUpdate(d *schema.ResourceData, meta i
updateMask = append(updateMask, "bareMetalVersion")
}

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

if d.HasChange("network_config") {
updateMask = append(updateMask, "networkConfig")
}
Expand Down Expand Up @@ -1132,6 +1138,10 @@ func resourceGkeonpremBareMetalAdminClusterUpdate(d *schema.ResourceData, meta i
if d.HasChange("security_config") {
updateMask = append(updateMask, "securityConfig")
}

if d.HasChange("effective_annotations") {
updateMask = append(updateMask, "annotations")
}
// updateMask is a URL parameter but not present in the schema, so ReplaceVars
// won't set it
url, err = transport_tpg.AddQueryParams(url, map[string]string{"updateMask": strings.Join(updateMask, ",")})
Expand Down Expand Up @@ -1289,7 +1299,18 @@ func flattenGkeonpremBareMetalAdminClusterEtag(v interface{}, d *schema.Resource
}

func flattenGkeonpremBareMetalAdminClusterAnnotations(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 flattenGkeonpremBareMetalAdminClusterNetworkConfig(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
Expand Down Expand Up @@ -1953,6 +1974,10 @@ func flattenGkeonpremBareMetalAdminClusterSecurityConfigAuthorizationAdminUsersU
return v
}

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

func expandGkeonpremBareMetalAdminClusterDescription(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
return v, nil
}
Expand All @@ -1961,17 +1986,6 @@ func expandGkeonpremBareMetalAdminClusterBareMetalVersion(v interface{}, d tpgre
return v, nil
}

func expandGkeonpremBareMetalAdminClusterAnnotations(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 expandGkeonpremBareMetalAdminClusterNetworkConfig(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
l := v.([]interface{})
if len(l) == 0 || l[0] == nil {
Expand Down Expand Up @@ -2669,3 +2683,14 @@ func expandGkeonpremBareMetalAdminClusterSecurityConfigAuthorizationAdminUsers(v
func expandGkeonpremBareMetalAdminClusterSecurityConfigAuthorizationAdminUsersUsername(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
return v, nil
}

func expandGkeonpremBareMetalAdminClusterEffectiveAnnotations(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
}
Loading

0 comments on commit aaccf52

Please sign in to comment.