Skip to content

Commit

Permalink
Add LabelFingerprint and Label to Address and GlobalAddress.
Browse files Browse the repository at this point in the history
  • Loading branch information
nat-henderson authored and modular-magician committed Jul 24, 2018
1 parent 0a87092 commit e84beda
Show file tree
Hide file tree
Showing 4 changed files with 217 additions and 3 deletions.
98 changes: 98 additions & 0 deletions google/resource_compute_address.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func resourceComputeAddress() *schema.Resource {
return &schema.Resource{
Create: resourceComputeAddressCreate,
Read: resourceComputeAddressRead,
Update: resourceComputeAddressUpdate,
Delete: resourceComputeAddressDelete,

Importer: &schema.ResourceImporter{
Expand All @@ -37,6 +38,7 @@ func resourceComputeAddress() *schema.Resource {

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(240 * time.Second),
Update: schema.DefaultTimeout(240 * time.Second),
Delete: schema.DefaultTimeout(240 * time.Second),
},

Expand Down Expand Up @@ -79,6 +81,11 @@ func resourceComputeAddress() *schema.Resource {
ForceNew: true,
DiffSuppressFunc: compareSelfLinkOrResourceName,
},
"labels": {
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"region": {
Type: schema.TypeString,
Computed: true,
Expand All @@ -90,6 +97,10 @@ func resourceComputeAddress() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"label_fingerprint": {
Type: schema.TypeString,
Computed: true,
},
"users": {
Type: schema.TypeList,
Computed: true,
Expand Down Expand Up @@ -156,6 +167,12 @@ func resourceComputeAddressCreate(d *schema.ResourceData, meta interface{}) erro
} else if v, ok := d.GetOkExists("subnetwork"); !isEmptyValue(reflect.ValueOf(subnetworkProp)) && (ok || !reflect.DeepEqual(v, subnetworkProp)) {
obj["subnetwork"] = subnetworkProp
}
labelsProp, err := expandComputeAddressLabels(d.Get("labels"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("labels"); !isEmptyValue(reflect.ValueOf(labelsProp)) && (ok || !reflect.DeepEqual(v, labelsProp)) {
obj["labels"] = labelsProp
}
regionProp, err := expandComputeAddressRegion(d.Get("region"), d, config)
if err != nil {
return err
Expand Down Expand Up @@ -244,6 +261,12 @@ func resourceComputeAddressRead(d *schema.ResourceData, meta interface{}) error
if err := d.Set("users", flattenComputeAddressUsers(res["users"])); err != nil {
return fmt.Errorf("Error reading Address: %s", err)
}
if err := d.Set("labels", flattenComputeAddressLabels(res["labels"])); err != nil {
return fmt.Errorf("Error reading Address: %s", err)
}
if err := d.Set("label_fingerprint", flattenComputeAddressLabelFingerprint(res["labelFingerprint"])); err != nil {
return fmt.Errorf("Error reading Address: %s", err)
}
if err := d.Set("region", flattenComputeAddressRegion(res["region"])); err != nil {
return fmt.Errorf("Error reading Address: %s", err)
}
Expand All @@ -257,6 +280,62 @@ func resourceComputeAddressRead(d *schema.ResourceData, meta interface{}) error
return nil
}

func resourceComputeAddressUpdate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

project, err := getProject(d, config)
if err != nil {
return err
}

var url string
var res map[string]interface{}
op := &compute.Operation{}

d.Partial(true)

if d.HasChange("labels") || d.HasChange("label_fingerprint") {
obj := make(map[string]interface{})
labelsProp, err := expandComputeAddressLabels(d.Get("labels"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("labels"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, labelsProp)) {
obj["labels"] = labelsProp
}
labelFingerprintProp := d.Get("label_fingerprint")
obj["labelFingerprint"] = labelFingerprintProp

url, err = replaceVars(d, config, "https://www.googleapis.com/compute/beta/projects/{{project}}/regions/{{region}}/addresses/{{name}}/setLabels")
if err != nil {
return err
}
res, err = sendRequest(config, "POST", url, obj)
if err != nil {
return fmt.Errorf("Error updating Address %q: %s", d.Id(), err)
}

err = Convert(res, op)
if err != nil {
return err
}

err = computeOperationWaitTime(
config.clientCompute, op, project, "Updating Address",
int(d.Timeout(schema.TimeoutUpdate).Minutes()))

if err != nil {
return err
}

d.SetPartial("labels")
d.SetPartial("label_fingerprint")
}

d.Partial(false)

return resourceComputeAddressRead(d, meta)
}

func resourceComputeAddressDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

Expand Down Expand Up @@ -346,6 +425,14 @@ func flattenComputeAddressUsers(v interface{}) interface{} {
return v
}

func flattenComputeAddressLabels(v interface{}) interface{} {
return v
}

func flattenComputeAddressLabelFingerprint(v interface{}) interface{} {
return v
}

func flattenComputeAddressRegion(v interface{}) interface{} {
if v == nil {
return v
Expand Down Expand Up @@ -381,6 +468,17 @@ func expandComputeAddressSubnetwork(v interface{}, d *schema.ResourceData, confi
return f.RelativeLink(), nil
}

func expandComputeAddressLabels(v interface{}, d *schema.ResourceData, config *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 expandComputeAddressRegion(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) {
f, err := parseGlobalFieldValue("regions", v.(string), "project", d, config, true)
if err != nil {
Expand Down
104 changes: 101 additions & 3 deletions google/resource_compute_global_address.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func resourceComputeGlobalAddress() *schema.Resource {
return &schema.Resource{
Create: resourceComputeGlobalAddressCreate,
Read: resourceComputeGlobalAddressRead,
Update: resourceComputeGlobalAddressUpdate,
Delete: resourceComputeGlobalAddressDelete,

Importer: &schema.ResourceImporter{
Expand All @@ -37,6 +38,7 @@ func resourceComputeGlobalAddress() *schema.Resource {

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(240 * time.Second),
Update: schema.DefaultTimeout(240 * time.Second),
Delete: schema.DefaultTimeout(240 * time.Second),
},

Expand All @@ -51,6 +53,11 @@ func resourceComputeGlobalAddress() *schema.Resource {
Optional: true,
ForceNew: true,
},
"labels": {
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"ip_version": {
Type: schema.TypeString,
Optional: true,
Expand All @@ -66,6 +73,10 @@ func resourceComputeGlobalAddress() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"label_fingerprint": {
Type: schema.TypeString,
Computed: true,
},
"project": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -101,14 +112,20 @@ func resourceComputeGlobalAddressCreate(d *schema.ResourceData, meta interface{}
} else if v, ok := d.GetOkExists("name"); !isEmptyValue(reflect.ValueOf(nameProp)) && (ok || !reflect.DeepEqual(v, nameProp)) {
obj["name"] = nameProp
}
labelsProp, err := expandComputeGlobalAddressLabels(d.Get("labels"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("labels"); !isEmptyValue(reflect.ValueOf(labelsProp)) && (ok || !reflect.DeepEqual(v, labelsProp)) {
obj["labels"] = labelsProp
}
ipVersionProp, err := expandComputeGlobalAddressIpVersion(d.Get("ip_version"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("ip_version"); !isEmptyValue(reflect.ValueOf(ipVersionProp)) && (ok || !reflect.DeepEqual(v, ipVersionProp)) {
obj["ipVersion"] = ipVersionProp
}

url, err := replaceVars(d, config, "https://www.googleapis.com/compute/v1/projects/{{project}}/global/addresses")
url, err := replaceVars(d, config, "https://www.googleapis.com/compute/beta/projects/{{project}}/global/addresses")
if err != nil {
return err
}
Expand Down Expand Up @@ -155,7 +172,7 @@ func resourceComputeGlobalAddressRead(d *schema.ResourceData, meta interface{})
return err
}

url, err := replaceVars(d, config, "https://www.googleapis.com/compute/v1/projects/{{project}}/global/addresses/{{name}}")
url, err := replaceVars(d, config, "https://www.googleapis.com/compute/beta/projects/{{project}}/global/addresses/{{name}}")
if err != nil {
return err
}
Expand All @@ -177,6 +194,12 @@ func resourceComputeGlobalAddressRead(d *schema.ResourceData, meta interface{})
if err := d.Set("name", flattenComputeGlobalAddressName(res["name"])); err != nil {
return fmt.Errorf("Error reading GlobalAddress: %s", err)
}
if err := d.Set("labels", flattenComputeGlobalAddressLabels(res["labels"])); err != nil {
return fmt.Errorf("Error reading GlobalAddress: %s", err)
}
if err := d.Set("label_fingerprint", flattenComputeGlobalAddressLabelFingerprint(res["labelFingerprint"])); err != nil {
return fmt.Errorf("Error reading GlobalAddress: %s", err)
}
if err := d.Set("ip_version", flattenComputeGlobalAddressIpVersion(res["ipVersion"])); err != nil {
return fmt.Errorf("Error reading GlobalAddress: %s", err)
}
Expand All @@ -190,6 +213,62 @@ func resourceComputeGlobalAddressRead(d *schema.ResourceData, meta interface{})
return nil
}

func resourceComputeGlobalAddressUpdate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

project, err := getProject(d, config)
if err != nil {
return err
}

var url string
var res map[string]interface{}
op := &compute.Operation{}

d.Partial(true)

if d.HasChange("labels") || d.HasChange("label_fingerprint") {
obj := make(map[string]interface{})
labelsProp, err := expandComputeGlobalAddressLabels(d.Get("labels"), d, config)
if err != nil {
return err
} else if v, ok := d.GetOkExists("labels"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, labelsProp)) {
obj["labels"] = labelsProp
}
labelFingerprintProp := d.Get("label_fingerprint")
obj["labelFingerprint"] = labelFingerprintProp

url, err = replaceVars(d, config, "https://www.googleapis.com/compute/beta/projects/{{project}}/global/addresses/{{name}}/setLabels")
if err != nil {
return err
}
res, err = sendRequest(config, "POST", url, obj)
if err != nil {
return fmt.Errorf("Error updating GlobalAddress %q: %s", d.Id(), err)
}

err = Convert(res, op)
if err != nil {
return err
}

err = computeOperationWaitTime(
config.clientCompute, op, project, "Updating GlobalAddress",
int(d.Timeout(schema.TimeoutUpdate).Minutes()))

if err != nil {
return err
}

d.SetPartial("labels")
d.SetPartial("label_fingerprint")
}

d.Partial(false)

return resourceComputeGlobalAddressRead(d, meta)
}

func resourceComputeGlobalAddressDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)

Expand All @@ -198,7 +277,7 @@ func resourceComputeGlobalAddressDelete(d *schema.ResourceData, meta interface{}
return err
}

url, err := replaceVars(d, config, "https://www.googleapis.com/compute/v1/projects/{{project}}/global/addresses/{{name}}")
url, err := replaceVars(d, config, "https://www.googleapis.com/compute/beta/projects/{{project}}/global/addresses/{{name}}")
if err != nil {
return err
}
Expand Down Expand Up @@ -257,6 +336,14 @@ func flattenComputeGlobalAddressName(v interface{}) interface{} {
return v
}

func flattenComputeGlobalAddressLabels(v interface{}) interface{} {
return v
}

func flattenComputeGlobalAddressLabelFingerprint(v interface{}) interface{} {
return v
}

func flattenComputeGlobalAddressIpVersion(v interface{}) interface{} {
return v
}
Expand All @@ -269,6 +356,17 @@ func expandComputeGlobalAddressName(v interface{}, d *schema.ResourceData, confi
return v, nil
}

func expandComputeGlobalAddressLabels(v interface{}, d *schema.ResourceData, config *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 expandComputeGlobalAddressIpVersion(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) {
return v, nil
}
9 changes: 9 additions & 0 deletions website/docs/r/compute_address.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ The following arguments are supported:
This field can only be used with INTERNAL type with
GCE_ENDPOINT/DNS_RESOLVER purposes.

* `labels` -
(Optional)
Labels to apply to this address. A list of key->value pairs.

* `region` -
(Optional)
The Region in which the created address should reside.
Expand All @@ -134,6 +138,10 @@ In addition to the arguments listed above, the following computed attributes are

* `users` -
The URLs of the resources that are using this address.

* `label_fingerprint` -
The fingerprint used for optimistic locking of this resource. Used
internally during updates.
* `self_link` - The URI of the created resource.


Expand All @@ -145,6 +153,7 @@ This resource provides the following
[Timeouts](/docs/configuration/resources.html#timeouts) configuration options:

- `create` - Default is 4 minutes.
- `update` - Default is 4 minutes.
- `delete` - Default is 4 minutes.

## Import
Expand Down
Loading

0 comments on commit e84beda

Please sign in to comment.