Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Label support for Google Managed Notebooks #16783

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/9625.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
notebooks: added support for `labels` to `google_notebooks_runtime`
```
99 changes: 99 additions & 0 deletions google/services/notebooks/resource_notebooks_runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ func ResourceNotebooksRuntime() *schema.Resource {
},

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

Expand Down Expand Up @@ -117,6 +118,21 @@ Currently supports one owner only.`,
},
},
},
"labels": {
Type: schema.TypeMap,
Optional: true,
Description: `The labels to associate with this runtime. Label **keys** must
contain 1 to 63 characters, and must conform to [RFC 1035]
(https://www.ietf.org/rfc/rfc1035.txt). Label **values** may be
empty, but, if present, must contain 1 to 63 characters, and must
conform to [RFC 1035](https://www.ietf.org/rfc/rfc1035.txt). No
more than 32 labels can be associated with a 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},
},
"software_config": {
Type: schema.TypeList,
Computed: true,
Expand Down Expand Up @@ -594,6 +610,12 @@ storing-retrieving-metadata#guest_attributes)).`,
},
ExactlyOneOf: []string{"virtual_machine"},
},
"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},
},
"health_state": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -623,6 +645,13 @@ sessions stats.`,
Computed: true,
Description: `The state of this runtime.`,
},
"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},
},
"project": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -660,6 +689,12 @@ func resourceNotebooksRuntimeCreate(d *schema.ResourceData, meta interface{}) er
} else if v, ok := d.GetOkExists("software_config"); !tpgresource.IsEmptyValue(reflect.ValueOf(softwareConfigProp)) && (ok || !reflect.DeepEqual(v, softwareConfigProp)) {
obj["softwareConfig"] = softwareConfigProp
}
labelsProp, err := expandNotebooksRuntimeEffectiveLabels(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, "{{NotebooksBasePath}}projects/{{project}}/locations/{{location}}/runtimes?runtimeId={{name}}")
if err != nil {
Expand Down Expand Up @@ -783,6 +818,15 @@ func resourceNotebooksRuntimeRead(d *schema.ResourceData, meta interface{}) erro
if err := d.Set("metrics", flattenNotebooksRuntimeMetrics(res["metrics"], d, config)); err != nil {
return fmt.Errorf("Error reading Runtime: %s", err)
}
if err := d.Set("labels", flattenNotebooksRuntimeLabels(res["labels"], d, config)); err != nil {
return fmt.Errorf("Error reading Runtime: %s", err)
}
if err := d.Set("terraform_labels", flattenNotebooksRuntimeTerraformLabels(res["labels"], d, config)); err != nil {
return fmt.Errorf("Error reading Runtime: %s", err)
}
if err := d.Set("effective_labels", flattenNotebooksRuntimeEffectiveLabels(res["labels"], d, config)); err != nil {
return fmt.Errorf("Error reading Runtime: %s", err)
}

return nil
}
Expand Down Expand Up @@ -821,6 +865,12 @@ func resourceNotebooksRuntimeUpdate(d *schema.ResourceData, meta interface{}) er
} else if v, ok := d.GetOkExists("software_config"); !tpgresource.IsEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, softwareConfigProp)) {
obj["softwareConfig"] = softwareConfigProp
}
labelsProp, err := expandNotebooksRuntimeEffectiveLabels(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, "{{NotebooksBasePath}}projects/{{project}}/locations/{{location}}/runtimes/{{name}}")
if err != nil {
Expand All @@ -844,6 +894,10 @@ func resourceNotebooksRuntimeUpdate(d *schema.ResourceData, meta interface{}) er
"softwareConfig.customGpuDriverPath",
"softwareConfig.postStartupScript")
}

if d.HasChange("effective_labels") {
updateMask = append(updateMask, "labels")
}
// 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 @@ -1487,6 +1541,40 @@ func flattenNotebooksRuntimeMetricsSystemMetrics(v interface{}, d *schema.Resour
return v
}

func flattenNotebooksRuntimeLabels(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
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 flattenNotebooksRuntimeTerraformLabels(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
if v == nil {
return v
}

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 transformed
}

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

func expandNotebooksRuntimeVirtualMachine(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 @@ -2269,3 +2357,14 @@ func expandNotebooksRuntimeSoftwareConfigKernelsRepository(v interface{}, d tpgr
func expandNotebooksRuntimeSoftwareConfigKernelsTag(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
return v, nil
}

func expandNotebooksRuntimeEffectiveLabels(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 TestAccNotebooksRuntime_notebookRuntimeBasicExample(t *testing.T) {
ResourceName: "google_notebooks_runtime.runtime",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"name", "location"},
ImportStateVerifyIgnore: []string{"name", "location", "labels", "terraform_labels"},
},
},
})
Expand Down Expand Up @@ -98,7 +98,7 @@ func TestAccNotebooksRuntime_notebookRuntimeBasicGpuExample(t *testing.T) {
ResourceName: "google_notebooks_runtime.runtime_gpu",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"name", "location"},
ImportStateVerifyIgnore: []string{"name", "location", "labels", "terraform_labels"},
},
},
})
Expand Down Expand Up @@ -154,7 +154,7 @@ func TestAccNotebooksRuntime_notebookRuntimeBasicContainerExample(t *testing.T)
ResourceName: "google_notebooks_runtime.runtime_container",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"name", "location"},
ImportStateVerifyIgnore: []string{"name", "location", "labels", "terraform_labels"},
},
},
})
Expand Down Expand Up @@ -211,7 +211,7 @@ func TestAccNotebooksRuntime_notebookRuntimeKernelsExample(t *testing.T) {
ResourceName: "google_notebooks_runtime.runtime_container",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"name", "location"},
ImportStateVerifyIgnore: []string{"name", "location", "labels", "terraform_labels"},
},
},
})
Expand Down Expand Up @@ -243,6 +243,9 @@ resource "google_notebooks_runtime" "runtime_container" {
}
}
}
labels = {
k = "val"
}
}
`, context)
}
Expand All @@ -266,7 +269,7 @@ func TestAccNotebooksRuntime_notebookRuntimeScriptExample(t *testing.T) {
ResourceName: "google_notebooks_runtime.runtime_container",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"name", "location"},
ImportStateVerifyIgnore: []string{"name", "location", "labels", "terraform_labels"},
},
},
})
Expand Down Expand Up @@ -295,6 +298,9 @@ resource "google_notebooks_runtime" "runtime_container" {
}
}
}
labels = {
k = "val"
}
}
`, context)
}
Expand Down
25 changes: 25 additions & 0 deletions website/docs/r/notebooks_runtime.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,9 @@ resource "google_notebooks_runtime" "runtime_container" {
}
}
}
labels = {
k = "val"
}
}
```
<div class = "oics-button" style="float: right; margin: 0 0 -15px">
Expand Down Expand Up @@ -200,6 +203,9 @@ resource "google_notebooks_runtime" "runtime_container" {
}
}
}
labels = {
k = "val"
}
}
```

Expand Down Expand Up @@ -235,6 +241,18 @@ The following arguments are supported:
The config settings for software inside the runtime.
Structure is [documented below](#nested_software_config).

* `labels` -
(Optional)
The labels to associate with this runtime. Label **keys** must
contain 1 to 63 characters, and must conform to [RFC 1035]
(https://www.ietf.org/rfc/rfc1035.txt). Label **values** may be
empty, but, if present, must contain 1 to 63 characters, and must
conform to [RFC 1035](https://www.ietf.org/rfc/rfc1035.txt). No
more than 32 labels can be associated with a 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.

* `project` - (Optional) The ID of the project in which the resource belongs.
If it is not provided, the provider project is used.

Expand Down Expand Up @@ -638,6 +656,13 @@ In addition to the arguments listed above, the following computed attributes are
status
Structure is [documented below](#nested_metrics).

* `terraform_labels` -
The combination of labels configured directly on the resource
and default labels configured on the provider.

* `effective_labels` -
All of labels (key/value pairs) present on the resource in GCP, including the labels configured through Terraform, other clients and services.


<a name="nested_metrics"></a>The `metrics` block contains:

Expand Down