Skip to content

Commit

Permalink
add tags to host and volume resource (#59)
Browse files Browse the repository at this point in the history
* add tags to host volume resource

* minor cleanup.
ron's comments

* neil comments

* lint errors

* lint error

Co-authored-by: Neil Gierman <neil.gierman@hpe.com>
  • Loading branch information
AnuBose and neilgierman authored Aug 11, 2022
1 parent 4f775a5 commit 2a684b2
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/resources/hpegl_metal_host/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,5 @@ resource "hpegl_metal_host" "terra_host_new_ssh" {
network_route = "Public"
location = var.location
description = "Hello from Terraform"
labels = { "ServiceType" = "BMaaS" }
}
21 changes: 21 additions & 0 deletions internal/resources/resource_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const (
hSubState = "sub_state"
hPortalCommOkay = "portal_comm_okay"
hPwrState = "power_state"
hLabels = "labels"

// allowedImageLength is number of Image related attributes that can be provided in the from of 'image@version'.
allowedImageLength = 2
Expand Down Expand Up @@ -202,6 +203,11 @@ func hostSchema() map[string]*schema.Schema {
Schema: volumeInfoSchema(),
},
},
hLabels: {
Type: schema.TypeMap,
Optional: true,
Description: "map of label name to label value for this host",
},
}
}

Expand Down Expand Up @@ -396,6 +402,11 @@ func resourceMetalHostCreate(d *schema.ResourceData, meta interface{}) (err erro
host.PreAllocatedIPs = convertStringArr(ips)
}

// add tags
if m, ok := (d.Get(hLabels).(map[string]interface{})); ok {
host.Labels = convertMap(m)
}

// Create it
ctx := p.GetContext()

Expand Down Expand Up @@ -474,6 +485,16 @@ func resourceMetalHostRead(d *schema.ResourceData, meta interface{}) (err error)
return err
}

tags := make(map[string]string, len(host.Labels))

for k, v := range host.Labels {
tags[k] = v
}

if err := d.Set(hLabels, tags); err != nil {
return fmt.Errorf("set labels: %v", err)
}

return nil
}

Expand Down
21 changes: 21 additions & 0 deletions internal/resources/resource_volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const (
vShareable = "shareable"
vState = "state"
vStatus = "status"
vLabels = "labels"

// volume Info constants.
vID = "id"
Expand Down Expand Up @@ -112,6 +113,11 @@ func volumeSchema() map[string]*schema.Schema {
Computed: true,
Description: "The volume provisioning status.",
},
vLabels: {
Type: schema.TypeMap,
Optional: true,
Description: "volume labels as (name, value) pairs",
},
}
}

Expand Down Expand Up @@ -195,6 +201,11 @@ func resourceMetalVolumeCreate(d *schema.ResourceData, meta interface{}) (err er
return fmt.Errorf("location %q not found in %q", targetLocation, locations)
}

// add tags
if m, ok := d.Get(vLabels).(map[string]interface{}); ok {
volume.Labels = convertMap(m)
}

ctx := p.GetContext()
v, _, err := p.Client.VolumesApi.Add(ctx, volume)
if err != nil {
Expand Down Expand Up @@ -252,6 +263,16 @@ func resourceMetalVolumeRead(d *schema.ResourceData, meta interface{}) (err erro
d.Set(vState, volume.State)
d.Set(vStatus, volume.Status)

tags := make(map[string]string, len(volume.Labels))

for k, v := range volume.Labels {
tags[k] = v
}

if err := d.Set(vLabels, tags); err != nil {
return fmt.Errorf("set labels: %v", err)
}

return nil
}

Expand Down
16 changes: 16 additions & 0 deletions internal/resources/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,22 @@ func wrapResourceError(err *error, msg string) {
*err = fmt.Errorf("%s %w", msg, *err)
}

// convertMap returns map of string key to string value.
func convertMap(in map[string]interface{}) map[string]string {
ret := make(map[string]string, len(in))
if len(in) == 0 {
return ret
}

for k, v := range in {
if s, ok := v.(string); ok {
ret[k] = s
}
}

return ret
}

// expandStringList takes []interfaces and returns []strings.
func expandStringList(list []interface{}) []string {
vs := make([]string, 0, len(list))
Expand Down

0 comments on commit 2a684b2

Please sign in to comment.