Skip to content

Commit

Permalink
workbench instances support for static external IP (#10934) (#18737)
Browse files Browse the repository at this point in the history
[upstream:ef027b1a25205b7b981c568dd0c489838dc53b75]

Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored Jul 15, 2024
1 parent 346c0a1 commit 33a8d91
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .changelog/10934.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
workbench: added `access_configs` to `google_workbench_instance` resource
```
86 changes: 83 additions & 3 deletions google/services/workbench/resource_workbench_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,30 @@ https://cloud.google.com/vpc/docs/using-routes#canipforward`,
Description: `The network interfaces for the VM. Supports only one interface.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"access_configs": {
Type: schema.TypeList,
Computed: true,
Optional: true,
ForceNew: true,
Description: `Optional. An array of configurations for this interface. Currently, only one access
config, ONE_TO_ONE_NAT, is supported. If no accessConfigs specified, the
instance will have an external internet access through an ephemeral
external IP address.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"external_ip": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: `An external IP address associated with this instance. Specify an unused
static external IP address available to the project or leave this field
undefined to use an IP from a shared ephemeral IP address pool. If you
specify a static external IP address, it must live in the same region as
the zone of the instance.`,
},
},
},
},
"network": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -1488,9 +1512,10 @@ func flattenWorkbenchInstanceGceSetupNetworkInterfaces(v interface{}, d *schema.
continue
}
transformed = append(transformed, map[string]interface{}{
"network": flattenWorkbenchInstanceGceSetupNetworkInterfacesNetwork(original["network"], d, config),
"subnet": flattenWorkbenchInstanceGceSetupNetworkInterfacesSubnet(original["subnet"], d, config),
"nic_type": flattenWorkbenchInstanceGceSetupNetworkInterfacesNicType(original["nicType"], d, config),
"network": flattenWorkbenchInstanceGceSetupNetworkInterfacesNetwork(original["network"], d, config),
"subnet": flattenWorkbenchInstanceGceSetupNetworkInterfacesSubnet(original["subnet"], d, config),
"nic_type": flattenWorkbenchInstanceGceSetupNetworkInterfacesNicType(original["nicType"], d, config),
"access_configs": flattenWorkbenchInstanceGceSetupNetworkInterfacesAccessConfigs(original["accessConfigs"], d, config),
})
}
return transformed
Expand All @@ -1507,6 +1532,28 @@ func flattenWorkbenchInstanceGceSetupNetworkInterfacesNicType(v interface{}, d *
return v
}

func flattenWorkbenchInstanceGceSetupNetworkInterfacesAccessConfigs(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
if v == nil {
return v
}
l := v.([]interface{})
transformed := make([]interface{}, 0, len(l))
for _, raw := range l {
original := raw.(map[string]interface{})
if len(original) < 1 {
// Do not include empty json objects coming back from the api
continue
}
transformed = append(transformed, map[string]interface{}{
"external_ip": flattenWorkbenchInstanceGceSetupNetworkInterfacesAccessConfigsExternalIp(original["externalIp"], d, config),
})
}
return transformed
}
func flattenWorkbenchInstanceGceSetupNetworkInterfacesAccessConfigsExternalIp(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
return v
}

func flattenWorkbenchInstanceGceSetupDisablePublicIp(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
return v
}
Expand Down Expand Up @@ -2115,6 +2162,13 @@ func expandWorkbenchInstanceGceSetupNetworkInterfaces(v interface{}, d tpgresour
transformed["nicType"] = transformedNicType
}

transformedAccessConfigs, err := expandWorkbenchInstanceGceSetupNetworkInterfacesAccessConfigs(original["access_configs"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedAccessConfigs); val.IsValid() && !tpgresource.IsEmptyValue(val) {
transformed["accessConfigs"] = transformedAccessConfigs
}

req = append(req, transformed)
}
return req, nil
Expand All @@ -2132,6 +2186,32 @@ func expandWorkbenchInstanceGceSetupNetworkInterfacesNicType(v interface{}, d tp
return v, nil
}

func expandWorkbenchInstanceGceSetupNetworkInterfacesAccessConfigs(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
l := v.([]interface{})
req := make([]interface{}, 0, len(l))
for _, raw := range l {
if raw == nil {
continue
}
original := raw.(map[string]interface{})
transformed := make(map[string]interface{})

transformedExternalIp, err := expandWorkbenchInstanceGceSetupNetworkInterfacesAccessConfigsExternalIp(original["external_ip"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedExternalIp); val.IsValid() && !tpgresource.IsEmptyValue(val) {
transformed["externalIp"] = transformedExternalIp
}

req = append(req, transformed)
}
return req, nil
}

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

func expandWorkbenchInstanceGceSetupDisablePublicIp(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) {
return v, nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,10 @@ resource "google_compute_subnetwork" "my_subnetwork" {
ip_cidr_range = "10.0.1.0/24"
}
resource "google_compute_address" "static" {
name = "tf-test-wbi-test-default%{random_suffix}"
}
resource "google_workbench_instance" "instance" {
name = "tf-test-workbench-instance%{random_suffix}"
location = "us-central1-a"
Expand Down Expand Up @@ -296,6 +300,9 @@ resource "google_workbench_instance" "instance" {
network = google_compute_network.my_network.id
subnet = google_compute_subnetwork.my_subnetwork.id
nic_type = "GVNIC"
access_configs {
external_ip = google_compute_address.static.address
}
}
metadata = {
Expand Down
26 changes: 26 additions & 0 deletions website/docs/r/workbench_instance.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ resource "google_compute_subnetwork" "my_subnetwork" {
ip_cidr_range = "10.0.1.0/24"
}
resource "google_compute_address" "static" {
name = "wbi-test-default"
}
resource "google_workbench_instance" "instance" {
name = "workbench-instance"
location = "us-central1-a"
Expand Down Expand Up @@ -177,6 +181,9 @@ resource "google_workbench_instance" "instance" {
network = google_compute_network.my_network.id
subnet = google_compute_subnetwork.my_subnetwork.id
nic_type = "GVNIC"
access_configs {
external_ip = google_compute_address.static.address
}
}
metadata = {
Expand Down Expand Up @@ -459,6 +466,25 @@ The following arguments are supported:
may be gVNIC or VirtioNet.
Possible values are: `VIRTIO_NET`, `GVNIC`.

* `access_configs` -
(Optional)
Optional. An array of configurations for this interface. Currently, only one access
config, ONE_TO_ONE_NAT, is supported. If no accessConfigs specified, the
instance will have an external internet access through an ephemeral
external IP address.
Structure is [documented below](#nested_access_configs).


<a name="nested_access_configs"></a>The `access_configs` block supports:

* `external_ip` -
(Required)
An external IP address associated with this instance. Specify an unused
static external IP address available to the project or leave this field
undefined to use an IP from a shared ephemeral IP address pool. If you
specify a static external IP address, it must live in the same region as
the zone of the instance.

## Attributes Reference

In addition to the arguments listed above, the following computed attributes are exported:
Expand Down

0 comments on commit 33a8d91

Please sign in to comment.