From cad35d1266d9bcde832719fac2ee58098f0e3af9 Mon Sep 17 00:00:00 2001 From: "girish.ramnani" Date: Mon, 24 Jul 2017 14:57:11 +0530 Subject: [PATCH] managed invalid key --- vsphere/resource_vsphere_license.go | 31 +++++++++++++++++++----- vsphere/resource_vsphere_license_test.go | 16 ++++++------ 2 files changed, 34 insertions(+), 13 deletions(-) diff --git a/vsphere/resource_vsphere_license.go b/vsphere/resource_vsphere_license.go index b43001a9a..17fd3d66e 100644 --- a/vsphere/resource_vsphere_license.go +++ b/vsphere/resource_vsphere_license.go @@ -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, @@ -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) @@ -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 @@ -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 + +} diff --git a/vsphere/resource_vsphere_license_test.go b/vsphere/resource_vsphere_license_test.go index ac01e3d1c..710a02674 100644 --- a/vsphere/resource_vsphere_license_test.go +++ b/vsphere/resource_vsphere_license_test.go @@ -6,6 +6,8 @@ import ( "os" "testing" + "regexp" + "golang.org/x/net/context" "github.com/hashicorp/terraform/helper/resource" @@ -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(), @@ -70,6 +72,7 @@ func TestAccVSphereLicenseInvalid(t *testing.T) { Check: resource.ComposeTestCheckFunc( testAccVSphereLicenseNotExists("vsphere_license.foo"), ), + ExpectError: regexp.MustCompile("License file not found"), }, }, }) @@ -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" }` } @@ -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) } @@ -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) } @@ -237,7 +239,7 @@ func testAccVSphereLicenseWithLabelExists(name string) resource.TestCheckFunc { } -func TestLabelToMap(t *testing.T) { +func TestVSphereLicenseLabelToMap(t *testing.T) { labelMap, err := labelsToMap(labelStub)