Skip to content

Commit

Permalink
Adding the namespace_labels field to the GKE Hub Scope resource (#9972)…
Browse files Browse the repository at this point in the history
… (#17421)

* initial commit for scope-level namespace labels

* Add validation exceptions for the  field of the GKEHub Scope and Namespace resources to be of type

* Undoing unnecessary changes

* Fixing the type of Scope namespace_labels field

[upstream:3e13564ac5d3ac814949b399e0fd56058445a717]

Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored Feb 26, 2024
1 parent 8e717c4 commit e45a120
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/9972.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
gkehub2: added `namespace_labels` field to `google_gke_hub_scope` resource
```
25 changes: 25 additions & 0 deletions google/services/gkehub2/iam_gke_hub_scope_generated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ func testAccGKEHub2ScopeIamMember_basicGenerated(context map[string]interface{})
return acctest.Nprintf(`
resource "google_gke_hub_scope" "scope" {
scope_id = "tf-test-my-scope%{random_suffix}"
namespace_labels = {
keyb = "valueb"
keya = "valuea"
keyc = "valuec"
}
labels = {
keyb = "valueb"
keya = "valuea"
Expand All @@ -150,6 +155,11 @@ func testAccGKEHub2ScopeIamPolicy_basicGenerated(context map[string]interface{})
return acctest.Nprintf(`
resource "google_gke_hub_scope" "scope" {
scope_id = "tf-test-my-scope%{random_suffix}"
namespace_labels = {
keyb = "valueb"
keya = "valuea"
keyc = "valuec"
}
labels = {
keyb = "valueb"
keya = "valuea"
Expand Down Expand Up @@ -184,6 +194,11 @@ func testAccGKEHub2ScopeIamPolicy_emptyBinding(context map[string]interface{}) s
return acctest.Nprintf(`
resource "google_gke_hub_scope" "scope" {
scope_id = "tf-test-my-scope%{random_suffix}"
namespace_labels = {
keyb = "valueb"
keya = "valuea"
keyc = "valuec"
}
labels = {
keyb = "valueb"
keya = "valuea"
Expand All @@ -206,6 +221,11 @@ func testAccGKEHub2ScopeIamBinding_basicGenerated(context map[string]interface{}
return acctest.Nprintf(`
resource "google_gke_hub_scope" "scope" {
scope_id = "tf-test-my-scope%{random_suffix}"
namespace_labels = {
keyb = "valueb"
keya = "valuea"
keyc = "valuec"
}
labels = {
keyb = "valueb"
keya = "valuea"
Expand All @@ -226,6 +246,11 @@ func testAccGKEHub2ScopeIamBinding_updateGenerated(context map[string]interface{
return acctest.Nprintf(`
resource "google_gke_hub_scope" "scope" {
scope_id = "tf-test-my-scope%{random_suffix}"
namespace_labels = {
keyb = "valueb"
keya = "valuea"
keyc = "valuec"
}
labels = {
keyb = "valueb"
keya = "valuea"
Expand Down
44 changes: 44 additions & 0 deletions google/services/gkehub2/resource_gke_hub_scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ func ResourceGKEHub2Scope() *schema.Resource {
Please refer to the field 'effective_labels' for all of the labels present on the resource.`,
Elem: &schema.Schema{Type: schema.TypeString},
},
"namespace_labels": {
Type: schema.TypeMap,
Optional: true,
Description: `Scope-level cluster namespace labels. For the member clusters bound
to the Scope, these labels are applied to each namespace under the
Scope. Scope-level labels take precedence over Namespace-level
labels ('namespace_labels' in the Fleet Namespace resource) if they
share a key. Keys and values must be Kubernetes-conformant.`,
Elem: &schema.Schema{Type: schema.TypeString},
},
"create_time": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -141,6 +151,12 @@ func resourceGKEHub2ScopeCreate(d *schema.ResourceData, meta interface{}) error
}

obj := make(map[string]interface{})
namespaceLabelsProp, err := expandGKEHub2ScopeNamespaceLabels(d.Get("namespace_labels"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("namespace_labels"); !tpgresource.IsEmptyValue(reflect.ValueOf(namespaceLabelsProp)) && (ok || !reflect.DeepEqual(v, namespaceLabelsProp)) {
obj["namespaceLabels"] = namespaceLabelsProp
}
labelsProp, err := expandGKEHub2ScopeEffectiveLabels(d.Get("effective_labels"), d, config)
if err != nil {
return err
Expand Down Expand Up @@ -274,6 +290,9 @@ func resourceGKEHub2ScopeRead(d *schema.ResourceData, meta interface{}) error {
if err := d.Set("state", flattenGKEHub2ScopeState(res["state"], d, config)); err != nil {
return fmt.Errorf("Error reading Scope: %s", err)
}
if err := d.Set("namespace_labels", flattenGKEHub2ScopeNamespaceLabels(res["namespaceLabels"], d, config)); err != nil {
return fmt.Errorf("Error reading Scope: %s", err)
}
if err := d.Set("labels", flattenGKEHub2ScopeLabels(res["labels"], d, config)); err != nil {
return fmt.Errorf("Error reading Scope: %s", err)
}
Expand Down Expand Up @@ -303,6 +322,12 @@ func resourceGKEHub2ScopeUpdate(d *schema.ResourceData, meta interface{}) error
billingProject = project

obj := make(map[string]interface{})
namespaceLabelsProp, err := expandGKEHub2ScopeNamespaceLabels(d.Get("namespace_labels"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("namespace_labels"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, namespaceLabelsProp)) {
obj["namespaceLabels"] = namespaceLabelsProp
}
labelsProp, err := expandGKEHub2ScopeEffectiveLabels(d.Get("effective_labels"), d, config)
if err != nil {
return err
Expand All @@ -318,6 +343,10 @@ func resourceGKEHub2ScopeUpdate(d *schema.ResourceData, meta interface{}) error
log.Printf("[DEBUG] Updating Scope %q: %#v", d.Id(), obj)
updateMask := []string{}

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

if d.HasChange("effective_labels") {
updateMask = append(updateMask, "labels")
}
Expand Down Expand Up @@ -473,6 +502,10 @@ func flattenGKEHub2ScopeStateCode(v interface{}, d *schema.ResourceData, config
return v
}

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

func flattenGKEHub2ScopeLabels(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
if v == nil {
return v
Expand Down Expand Up @@ -507,6 +540,17 @@ func flattenGKEHub2ScopeEffectiveLabels(v interface{}, d *schema.ResourceData, c
return v
}

func expandGKEHub2ScopeNamespaceLabels(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 expandGKEHub2ScopeEffectiveLabels(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 @@ -61,6 +61,11 @@ func testAccGKEHub2Scope_gkehubScopeBasicExample(context map[string]interface{})
return acctest.Nprintf(`
resource "google_gke_hub_scope" "scope" {
scope_id = "tf-test-my-scope%{random_suffix}"
namespace_labels = {
keyb = "valueb"
keya = "valuea"
keyc = "valuec"
}
labels = {
keyb = "valueb"
keya = "valuea"
Expand Down
10 changes: 10 additions & 0 deletions google/services/gkehub2/resource_gke_hub_scope_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ func testAccGKEHub2Scope_gkehubScopeBasicExample_basic(context map[string]interf
return acctest.Nprintf(`
resource "google_gke_hub_scope" "scope" {
scope_id = "tf-test-scope%{random_suffix}"
namespace_labels = {
keyb = "valueb"
keya = "valuea"
keyc = "valuec"
}
labels = {
keyb = "valueb"
keya = "valuea"
Expand All @@ -62,6 +67,11 @@ func testAccGKEHub2Scope_gkehubScopeBasicExample_update(context map[string]inter
return acctest.Nprintf(`
resource "google_gke_hub_scope" "scope" {
scope_id = "tf-test-scope%{random_suffix}"
namespace_labels = {
updated_keyb = "updated_valueb"
updated_keya = "updated_valuea"
updated_keyc = "updated_valuec"
}
labels = {
updated_keyb = "updated_valueb"
updated_keya = "updated_valuea"
Expand Down
13 changes: 13 additions & 0 deletions website/docs/r/gke_hub_scope.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ To get more information about Scope, see:
```hcl
resource "google_gke_hub_scope" "scope" {
scope_id = "my-scope"
namespace_labels = {
keyb = "valueb"
keya = "valuea"
keyc = "valuec"
}
labels = {
keyb = "valueb"
keya = "valuea"
Expand All @@ -55,6 +60,14 @@ The following arguments are supported:
- - -


* `namespace_labels` -
(Optional)
Scope-level cluster namespace labels. For the member clusters bound
to the Scope, these labels are applied to each namespace under the
Scope. Scope-level labels take precedence over Namespace-level
labels (`namespace_labels` in the Fleet Namespace resource) if they
share a key. Keys and values must be Kubernetes-conformant.

* `labels` -
(Optional)
Labels for this Scope.
Expand Down

0 comments on commit e45a120

Please sign in to comment.