Skip to content

Commit

Permalink
Apply new labels model for edgecontainer resources (#9193) (#6459)
Browse files Browse the repository at this point in the history
[upstream:7b062fdbb90c203f278ea2dc46b41f959c686397]

Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored and melinath committed Oct 9, 2023
1 parent 9a19431 commit 18bbb2b
Show file tree
Hide file tree
Showing 9 changed files with 283 additions and 76 deletions.
3 changes: 3 additions & 0 deletions .changelog/9193.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:none

```
100 changes: 76 additions & 24 deletions google-beta/services/edgecontainer/resource_edgecontainer_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func ResourceEdgecontainerCluster() *schema.Resource {
},

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

Expand Down Expand Up @@ -334,11 +335,13 @@ Kubernetes default value will be used.`,
},
},
"labels": {
Type: schema.TypeMap,
Computed: true,
Optional: true,
Description: `User-defined labels for the edgecloud cluster.`,
Elem: &schema.Schema{Type: schema.TypeString},
Type: schema.TypeMap,
Optional: true,
Description: `User-defined labels for the edgecloud cluster.
**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},
},
"maintenance_policy": {
Type: schema.TypeList,
Expand Down Expand Up @@ -468,6 +471,12 @@ consists of istiod and istio-ingress.`,
Computed: true,
Description: `The time the cluster was created, in RFC3339 text format.`,
},
"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},
},
"endpoint": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -554,6 +563,13 @@ if the cluster does not have any worker nodes.`,
Computed: true,
Description: `Indicates the status of the cluster.`,
},
"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},
},
"update_time": {
Type: schema.TypeString,
Computed: true,
Expand All @@ -578,12 +594,6 @@ func resourceEdgecontainerClusterCreate(d *schema.ResourceData, meta interface{}
}

obj := make(map[string]interface{})
labelsProp, err := expandEdgecontainerClusterLabels(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
}
fleetProp, err := expandEdgecontainerClusterFleet(d.Get("fleet"), d, config)
if err != nil {
return err
Expand Down Expand Up @@ -650,6 +660,12 @@ func resourceEdgecontainerClusterCreate(d *schema.ResourceData, meta interface{}
} else if v, ok := d.GetOkExists("release_channel"); !tpgresource.IsEmptyValue(reflect.ValueOf(releaseChannelProp)) && (ok || !reflect.DeepEqual(v, releaseChannelProp)) {
obj["releaseChannel"] = releaseChannelProp
}
labelsProp, err := expandEdgecontainerClusterEffectiveLabels(d.Get("effective_labels"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("effective_labels"); !tpgresource.IsEmptyValue(reflect.ValueOf(labelsProp)) && (ok || !reflect.DeepEqual(v, labelsProp)) {
obj["labels"] = labelsProp
}

url, err := tpgresource.ReplaceVars(d, config, "{{EdgecontainerBasePath}}projects/{{project}}/locations/{{location}}/clusters?cluster_id={{name}}")
if err != nil {
Expand Down Expand Up @@ -808,6 +824,12 @@ func resourceEdgecontainerClusterRead(d *schema.ResourceData, meta interface{})
if err := d.Set("release_channel", flattenEdgecontainerClusterReleaseChannel(res["releaseChannel"], d, config)); err != nil {
return fmt.Errorf("Error reading Cluster: %s", err)
}
if err := d.Set("terraform_labels", flattenEdgecontainerClusterTerraformLabels(res["labels"], d, config)); err != nil {
return fmt.Errorf("Error reading Cluster: %s", err)
}
if err := d.Set("effective_labels", flattenEdgecontainerClusterEffectiveLabels(res["labels"], d, config)); err != nil {
return fmt.Errorf("Error reading Cluster: %s", err)
}

return nil
}
Expand All @@ -828,12 +850,6 @@ func resourceEdgecontainerClusterUpdate(d *schema.ResourceData, meta interface{}
billingProject = project

obj := make(map[string]interface{})
labelsProp, err := expandEdgecontainerClusterLabels(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
}
fleetProp, err := expandEdgecontainerClusterFleet(d.Get("fleet"), d, config)
if err != nil {
return err
Expand Down Expand Up @@ -894,6 +910,12 @@ func resourceEdgecontainerClusterUpdate(d *schema.ResourceData, meta interface{}
} else if v, ok := d.GetOkExists("release_channel"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, releaseChannelProp)) {
obj["releaseChannel"] = releaseChannelProp
}
labelsProp, err := expandEdgecontainerClusterEffectiveLabels(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
}

url, err := tpgresource.ReplaceVars(d, config, "{{EdgecontainerBasePath}}projects/{{project}}/locations/{{location}}/clusters/{{name}}")
if err != nil {
Expand Down Expand Up @@ -1062,7 +1084,18 @@ func flattenEdgecontainerClusterUpdateTime(v interface{}, d *schema.ResourceData
}

func flattenEdgecontainerClusterLabels(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 flattenEdgecontainerClusterFleet(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
Expand Down Expand Up @@ -1543,15 +1576,23 @@ func flattenEdgecontainerClusterReleaseChannel(v interface{}, d *schema.Resource
return v
}

func expandEdgecontainerClusterLabels(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (map[string]string, error) {
func flattenEdgecontainerClusterTerraformLabels(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 flattenEdgecontainerClusterEffectiveLabels(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
return v
}

func expandEdgecontainerClusterFleet(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
Expand Down Expand Up @@ -2058,3 +2099,14 @@ func expandEdgecontainerClusterTargetVersion(v interface{}, d tpgresource.Terraf
func expandEdgecontainerClusterReleaseChannel(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
return v, nil
}

func expandEdgecontainerClusterEffectiveLabels(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 TestAccEdgecontainerCluster_edgecontainerClusterExample(t *testing.T) {
ResourceName: "google_edgecontainer_cluster.default",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"location", "name"},
ImportStateVerifyIgnore: []string{"location", "name", "labels", "terraform_labels"},
},
},
})
Expand All @@ -75,6 +75,11 @@ resource "google_edgecontainer_cluster" "default" {
fleet {
project = "projects/${data.google_project.project.number}"
}
labels = {
my_key = "my_val"
other_key = "other_val"
}
}
data "google_project" "project" {}
Expand All @@ -100,7 +105,7 @@ func TestAccEdgecontainerCluster_edgecontainerClusterWithMaintenanceWindowExampl
ResourceName: "google_edgecontainer_cluster.default",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"location", "name"},
ImportStateVerifyIgnore: []string{"location", "name", "labels", "terraform_labels"},
},
},
})
Expand Down
100 changes: 76 additions & 24 deletions google-beta/services/edgecontainer/resource_edgecontainer_node_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func ResourceEdgecontainerNodePool() *schema.Resource {
},

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

Expand Down Expand Up @@ -82,11 +83,13 @@ func ResourceEdgecontainerNodePool() *schema.Resource {
Description: `Name of the Google Distributed Cloud Edge zone where this node pool will be created. For example: 'us-central1-edge-customer-a'.`,
},
"labels": {
Type: schema.TypeMap,
Computed: true,
Optional: true,
Description: `Labels associated with this resource.`,
Elem: &schema.Schema{Type: schema.TypeString},
Type: schema.TypeMap,
Optional: true,
Description: `Labels associated with this resource.
**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},
},
"local_disk_encryption": {
Type: schema.TypeList,
Expand Down Expand Up @@ -147,11 +150,24 @@ documented in more detail in [AIP-160](https://google.aip.dev/160).`,
Computed: true,
Description: `The time when the node pool was created.`,
},
"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},
},
"node_version": {
Type: schema.TypeString,
Computed: true,
Description: `The lowest release version among all worker nodes.`,
},
"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},
},
"update_time": {
Type: schema.TypeString,
Computed: true,
Expand All @@ -176,12 +192,6 @@ func resourceEdgecontainerNodePoolCreate(d *schema.ResourceData, meta interface{
}

obj := make(map[string]interface{})
labelsProp, err := expandEdgecontainerNodePoolLabels(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
}
nodeLocationProp, err := expandEdgecontainerNodePoolNodeLocation(d.Get("node_location"), d, config)
if err != nil {
return err
Expand Down Expand Up @@ -212,6 +222,12 @@ func resourceEdgecontainerNodePoolCreate(d *schema.ResourceData, meta interface{
} else if v, ok := d.GetOkExists("node_config"); !tpgresource.IsEmptyValue(reflect.ValueOf(nodeConfigProp)) && (ok || !reflect.DeepEqual(v, nodeConfigProp)) {
obj["nodeConfig"] = nodeConfigProp
}
labelsProp, err := expandEdgecontainerNodePoolEffectiveLabels(d.Get("effective_labels"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("effective_labels"); !tpgresource.IsEmptyValue(reflect.ValueOf(labelsProp)) && (ok || !reflect.DeepEqual(v, labelsProp)) {
obj["labels"] = labelsProp
}

url, err := tpgresource.ReplaceVars(d, config, "{{EdgecontainerBasePath}}projects/{{project}}/locations/{{location}}/clusters/{{cluster}}/nodePools?nodePoolId={{name}}")
if err != nil {
Expand Down Expand Up @@ -334,6 +350,12 @@ func resourceEdgecontainerNodePoolRead(d *schema.ResourceData, meta interface{})
if err := d.Set("node_config", flattenEdgecontainerNodePoolNodeConfig(res["nodeConfig"], d, config)); err != nil {
return fmt.Errorf("Error reading NodePool: %s", err)
}
if err := d.Set("terraform_labels", flattenEdgecontainerNodePoolTerraformLabels(res["labels"], d, config)); err != nil {
return fmt.Errorf("Error reading NodePool: %s", err)
}
if err := d.Set("effective_labels", flattenEdgecontainerNodePoolEffectiveLabels(res["labels"], d, config)); err != nil {
return fmt.Errorf("Error reading NodePool: %s", err)
}

return nil
}
Expand All @@ -354,12 +376,6 @@ func resourceEdgecontainerNodePoolUpdate(d *schema.ResourceData, meta interface{
billingProject = project

obj := make(map[string]interface{})
labelsProp, err := expandEdgecontainerNodePoolLabels(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
}
nodeLocationProp, err := expandEdgecontainerNodePoolNodeLocation(d.Get("node_location"), d, config)
if err != nil {
return err
Expand Down Expand Up @@ -390,6 +406,12 @@ func resourceEdgecontainerNodePoolUpdate(d *schema.ResourceData, meta interface{
} else if v, ok := d.GetOkExists("node_config"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, nodeConfigProp)) {
obj["nodeConfig"] = nodeConfigProp
}
labelsProp, err := expandEdgecontainerNodePoolEffectiveLabels(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
}

url, err := tpgresource.ReplaceVars(d, config, "{{EdgecontainerBasePath}}projects/{{project}}/locations/{{location}}/clusters/{{cluster}}/nodePools/{{name}}")
if err != nil {
Expand Down Expand Up @@ -512,7 +534,18 @@ func flattenEdgecontainerNodePoolUpdateTime(v interface{}, d *schema.ResourceDat
}

func flattenEdgecontainerNodePoolLabels(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 flattenEdgecontainerNodePoolNodeLocation(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
Expand Down Expand Up @@ -590,15 +623,23 @@ func flattenEdgecontainerNodePoolNodeConfigLabels(v interface{}, d *schema.Resou
return v
}

func expandEdgecontainerNodePoolLabels(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (map[string]string, error) {
func flattenEdgecontainerNodePoolTerraformLabels(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 flattenEdgecontainerNodePoolEffectiveLabels(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
return v
}

func expandEdgecontainerNodePoolNodeLocation(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
Expand Down Expand Up @@ -687,3 +728,14 @@ func expandEdgecontainerNodePoolNodeConfigLabels(v interface{}, d tpgresource.Te
}
return m, nil
}

func expandEdgecontainerNodePoolEffectiveLabels(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 18bbb2b

Please sign in to comment.