Skip to content

Commit

Permalink
extracted updateLabels function and changed the create license proced…
Browse files Browse the repository at this point in the history
…ure for vcenter
  • Loading branch information
girishramnani-crest committed Aug 17, 2017
1 parent f76eba8 commit 44a3744
Showing 1 changed file with 24 additions and 14 deletions.
38 changes: 24 additions & 14 deletions vsphere/resource_vsphere_license.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,27 +74,29 @@ func resourceVSphereLicenseCreate(d *schema.ResourceData, meta interface{}) erro
key := d.Get("license_key").(string)

log.Println(" [INFO] Reading the key from the resource data")
finalLabels := make(map[string]string)

var labelMap map[string]interface{}
if labels, ok := d.GetOk("labels"); ok {
labelMap := labels.(map[string]interface{})
for key, value := range labelMap {
finalLabels[key] = value.(string)
}
labelMap = labels.(map[string]interface{})
}

var info types.LicenseManagerLicenseInfo
var err error
switch t := client.ServiceContent.About.ApiType; t {
case "HostAgent":
// Labels are not allowed in ESXi
if len(finalLabels) != 0 {
if len(labelMap) != 0 {
return errors.New("Labels are not allowed in ESXi")
}
info, err = manager.Update(context.TODO(), key, nil)

info, err = manager.Update(context.TODO(), key, finalLabels)
case "VirtualCenter":
info, err = manager.Add(context.TODO(), key, finalLabels)
info, err = manager.Add(context.TODO(), key, nil)

err := updateLabels(manager, key, labelMap)
if err != nil {
return err
}

default:
return fmt.Errorf("unsupported ApiType: %s", t)
}
Expand Down Expand Up @@ -150,18 +152,26 @@ func resourceVSphereLicenseUpdate(d *schema.ResourceData, meta interface{}) erro
if d.HasChange("labels") {
labelMap := d.Get("labels").(map[string]interface{})

for key, value := range labelMap {
err := UpdateLabel(context.TODO(), manager, licenseKey, key, value.(string))
if err != nil {
return err
}
err := updateLabels(manager, licenseKey, labelMap)
if err != nil {
return err
}
}
}

return resourceVSphereLicenseRead(d, meta)
}

func updateLabels(manager *license.Manager, licenseKey string, labelMap map[string]interface{}) error {
for key, value := range labelMap {
err := UpdateLabel(context.TODO(), manager, licenseKey, key, value.(string))
if err != nil {
return err
}
}
return nil
}

func resourceVSphereLicenseDelete(d *schema.ResourceData, meta interface{}) error {
log.Println("[INFO] Running the delete method")

Expand Down

0 comments on commit 44a3744

Please sign in to comment.