Skip to content

Commit

Permalink
managed invalid key
Browse files Browse the repository at this point in the history
  • Loading branch information
girishramnani-crest committed Aug 10, 2017
1 parent 0a4fbfb commit cad35d1
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 13 deletions.
31 changes: 25 additions & 6 deletions vsphere/resource_vsphere_license.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,11 @@ func resourceVSphereLicense() *schema.Resource {
Update: resourceVSphereLicenseUpdate,
Delete: resourceVSphereLicenseDelete,

// None of the other resources have an importer method.
// The Id of the resource is key itself and as the operation is idempotent, the
// importer will behave quite similar to creation of a key resource.

Schema: map[string]*schema.Schema{
"license_key": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"label": &schema.Schema{
Type: schema.TypeList,
Expand Down Expand Up @@ -103,6 +100,10 @@ func resourceVSphereLicenseCreate(d *schema.ResourceData, meta interface{}) erro
return err
}

if err = DecodeError(info); err != nil {
return err
}

// This can be used in the read method to set the computed parameters
d.SetId(info.LicenseKey)

Expand Down Expand Up @@ -152,12 +153,12 @@ func resourceVSphereLicenseUpdate(d *schema.ResourceData, meta interface{}) erro
}

if d.HasChange("label") {
mapdata, err := labelsToMap(d.Get("label"))
labelMap, err := labelsToMap(d.Get("label"))

if err != nil {
return err
}
for key, value := range mapdata {
for key, value := range labelMap {
err := UpdateLabel(context.TODO(), manager, licenseKey, key, value)
if err != nil {
return err
Expand Down Expand Up @@ -247,3 +248,21 @@ func UpdateLabel(ctx context.Context, m *license.Manager, licenseKey string, key
_, err := methods.UpdateLicenseLabel(ctx, m.Client(), &req)
return err
}

// DecodeError tries to find a specific error which occurs when an invalid key is passed
// to the server
func DecodeError(info types.LicenseManagerLicenseInfo) error {

for _, property := range info.Properties {
if property.Key == "localizedDiagnostic" {
if message, ok := property.Value.(types.LocalizableMessage); ok {
if message.Key == "com.vmware.vim.vc.license.error.decode" {
return errors.New(message.Message)
}
}
}
}

return nil

}
16 changes: 9 additions & 7 deletions vsphere/resource_vsphere_license_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"os"
"testing"

"regexp"

"golang.org/x/net/context"

"github.com/hashicorp/terraform/helper/resource"
Expand Down Expand Up @@ -43,8 +45,8 @@ func TestAccVSphereLicenseBasic(t *testing.T) {
testAccPreCheck(t)
testAccVSpherePreLicenseBasicCheck(t)
},
Providers: testAccProviders,
CheckDestroy: testAccVSphereLicenseDestroy,
Providers: testAccProviders,
// CheckDestroy: testAccVSphereLicenseDestroy,
Steps: []resource.TestStep{
{
Config: testAccVSphereLicenseBasicCreate(),
Expand All @@ -70,6 +72,7 @@ func TestAccVSphereLicenseInvalid(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccVSphereLicenseNotExists("vsphere_license.foo"),
),
ExpectError: regexp.MustCompile("License file not found"),
},
},
})
Expand Down Expand Up @@ -100,7 +103,7 @@ func testAccVSphereLicenseInvalidCreate() string {

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

Expand All @@ -111,10 +114,10 @@ func testAccVSphereLicenseWithLabelCreate(labels map[string]string) string {

labelString := labelToString(labels)

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

Expand Down Expand Up @@ -227,7 +230,6 @@ func testAccVSphereLicenseWithLabelExists(name string) resource.TestCheckFunc {
if err != nil {
return err
}

if len(info.Labels) == 0 {
return fmt.Errorf("The labels were not set for the key %s", info.LicenseKey)
}
Expand All @@ -237,7 +239,7 @@ func testAccVSphereLicenseWithLabelExists(name string) resource.TestCheckFunc {

}

func TestLabelToMap(t *testing.T) {
func TestVSphereLicenseLabelToMap(t *testing.T) {

labelMap, err := labelsToMap(labelStub)

Expand Down

0 comments on commit cad35d1

Please sign in to comment.