Skip to content

Commit

Permalink
changed labels to TypeMap and updated the readme
Browse files Browse the repository at this point in the history
  • Loading branch information
girishramnani-crest committed Aug 10, 2017
1 parent 4339e00 commit 322c3e8
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 102 deletions.
58 changes: 12 additions & 46 deletions vsphere/resource_vsphere_license.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,9 @@ func resourceVSphereLicense() *schema.Resource {
Required: true,
ForceNew: true,
},
"label": &schema.Schema{
Type: schema.TypeList,
"labels": &schema.Schema{
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"key": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"value": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
},
},
},

// computed properties returned by the API
Expand Down Expand Up @@ -88,18 +76,17 @@ func resourceVSphereLicenseCreate(d *schema.ResourceData, meta interface{}) erro
key := d.Get("license_key").(string)

log.Println(" [INFO] Reading the key from the resource data")
var finalLabels map[string]string
var err error
if labels, ok := d.GetOk("label"); ok {
finalLabels, err = labelsToMap(labels)
if err != nil {
// If errors are printed by terraform then no need to log errors here
return err
finalLabels := make(map[string]string)

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

var info types.LicenseManagerLicenseInfo

var err error
switch t := client.ServiceContent.About.ApiType; t {
case "HostAgent":
info, err = manager.Update(context.TODO(), key, finalLabels)
Expand Down Expand Up @@ -159,21 +146,15 @@ func resourceVSphereLicenseUpdate(d *schema.ResourceData, meta interface{}) erro
return ErrNoSuchKeyFound
}

if d.HasChange("label") {
labelMap, err := labelsToMap(d.Get("label"))
if d.HasChange("labels") {
labelMap := d.Get("labels").(map[string]interface{})

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

Expand Down Expand Up @@ -207,21 +188,6 @@ func resourceVSphereLicenseDelete(d *schema.ResourceData, meta interface{}) erro

}

// labelsToMap is an adapter method that takes labels and gives a map that
// can be used with the key creation method.
func labelsToMap(labels interface{}) (map[string]string, error) {

finalLabels := make(map[string]string)
labelList := labels.([]interface{})
for _, label := range labelList {
labelMap := label.(map[string]interface{})
finalLabels[labelMap["key"].(string)] = labelMap["value"].(string)
}

return finalLabels, nil

}

func getLicenseInfoFromKey(key string, manager *license.Manager) *types.LicenseManagerLicenseInfo {

// Use of decode is not returning labels so using list instead
Expand Down
58 changes: 15 additions & 43 deletions vsphere/resource_vsphere_license_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,6 @@ var (
"VpxClientLicenseLabel": "Hello World",
"TestTitle": "FooBar",
}

labelStub = []interface{}{
map[string]interface{}{
"key": "Hello",
"value": "World",
},
map[string]interface{}{
"key": "Working",
"value": "This",
},
map[string]interface{}{
"key": "Testing",
"value": "Labels",
},
}
)

func TestAccVSphereLicenseBasic(t *testing.T) {
Expand All @@ -47,7 +32,7 @@ func TestAccVSphereLicenseBasic(t *testing.T) {
CheckDestroy: testAccVSphereLicenseDestroy,
Steps: []resource.TestStep{
{
Config: testAccVSphereLicenseBasicCreate(),
Config: testAccVSphereLicenseBasicConfig(),
Check: resource.ComposeTestCheckFunc(
testAccVSphereLicenseExists("vsphere_license.foo"),
),
Expand All @@ -66,7 +51,7 @@ func TestAccVSphereLicenseInvalid(t *testing.T) {
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccVSphereLicenseInvalidCreate(),
Config: testAccVSphereLicenseInvalidConfig(),
Check: resource.ComposeTestCheckFunc(
testAccVSphereLicenseNotExists("vsphere_license.foo"),
),
Expand All @@ -87,7 +72,7 @@ func TestAccVSphereLicenseWithLabels(t *testing.T) {
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccVSphereLicenseWithLabelCreate(testAccLabels),
Config: testAccVSphereLicenseWithLabelConfig(testAccLabels),
Check: resource.ComposeTestCheckFunc(
testAccVSphereLicenseWithLabelExists("vsphere_license.foo"),
),
Expand All @@ -97,43 +82,43 @@ func TestAccVSphereLicenseWithLabels(t *testing.T) {

}

func testAccVSphereLicenseInvalidCreate() string {
func testAccVSphereLicenseInvalidConfig() string {

// quite sure this key cannot be valid
return `resource "vsphere_license" "foo" {
license_key = "HN422-47193-58V7M-03086-0JAN2"
}`
}

func testAccVSphereLicenseWithLabelCreate(labels map[string]string) string {
func testAccVSphereLicenseWithLabelConfig(labels map[string]string) string {

// precheck already checks if this is present or not
key := os.Getenv("VSPHERE_LICENSE")

labelString := labelToString(labels)

return fmt.Sprintf(`resource "vsphere_license" "foo" {
license_key = "%s"
license_key = "%s"
%s
}`, key, labelString)
labels {
%s
}
}`,
key, labelString)
}

func labelToString(labels map[string]string) string {
val := ""
for key, value := range labels {
val += fmt.Sprintf(`
label {
key = "%s"
value = "%s"
}
%s = "%s"
`, key, value)

}
return val
}

func testAccVSphereLicenseBasicCreate() string {
func testAccVSphereLicenseBasicConfig() string {

// precheck already checks if this is present or not
key := os.Getenv("VSPHERE_LICENSE")
Expand Down Expand Up @@ -230,24 +215,11 @@ func testAccVSphereLicenseWithLabelExists(name string) resource.TestCheckFunc {
}

if len(info.Labels) != 2 {
return fmt.Errorf("The number of labels on the server are incorrect", info.Labels)
return fmt.Errorf(`The number of labels on the server are incorrect. Expected 2 Got %d`,
len(info.Labels))
}

return nil
}

}

func TestVSphereLicenseLabelsToMap(t *testing.T) {

labelMap, err := labelsToMap(labelStub)

if err != nil {
t.Fatal("Error ", err)
}

if value, ok := labelMap["Hello"]; !ok || value != "World" {
t.Fatal("The label map doesn't contain labels as expected")
}

}
22 changes: 9 additions & 13 deletions website/docs/r/license.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,9 @@ Provides a VMware vSphere license resource. This can be used to add and remove l
resource "vsphere_license" "licenseKey" {
license_key = "452CQ-2EK54-K8742-00000-00000"
label {
key = "VpxClientLicenseLabel"
value ="Hello World"
}
label {
key = "Workflow"
value ="Hello World"
labels {
VpxClientLicenseLabel = "Hello World"
Workflow = "Hello World"
}
}
Expand All @@ -32,15 +28,15 @@ resource "vsphere_license" "licenseKey" {

The following arguments are supported:

* `license_key` - (Required) The key of the license which is needed to be added to vpshere
* `label` - (Optional) The key value pair of labels that has to be attached to the license key. One key can have multiple labels.
* `license_key` - (Required) The license key to add.
* `labels` - (Optional) The key value pair of labels that has to be attached to the license key.


## Attributes Reference

The following attributes are exported:

* `edition_key` - The product edition of the license key
* `total` - Total numbers of VCPUs supported by the license
* `used` - Total VCPUs currently being used
* `name` - Name of the license
* `edition_key` - The product edition of the license key.
* `total` - Total number of units (example: vCPUs) contained in the license.
* `used` - The number of units (example: vCPUs) assigned to this license.
* `name` - The display name for the license.

0 comments on commit 322c3e8

Please sign in to comment.