Skip to content

Commit

Permalink
Implement license resource
Browse files Browse the repository at this point in the history
Signed-off-by: Kobi Samoray <[email protected]>
  • Loading branch information
ksamoray committed Jul 13, 2023
1 parent 08cb840 commit 1adf6bf
Show file tree
Hide file tree
Showing 5 changed files with 274 additions and 0 deletions.
1 change: 1 addition & 0 deletions nsxt/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@ func Provider() *schema.Provider {
"nsxt_policy_project": resourceNsxtPolicyProject(),
"nsxt_edge_cluster": resourceNsxtEdgeCluster(),
"nsxt_compute_manager": resourceNsxtComputeManager(),
"nsxt_license": resourceNsxtLicense(),
},

ConfigureFunc: providerConfigure,
Expand Down
103 changes: 103 additions & 0 deletions nsxt/resource_nsxt_license.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/* Copyright © 2023 VMware, Inc. All Rights Reserved.
SPDX-License-Identifier: MPL-2.0 */

package nsxt

import (
"fmt"
"log"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/vmware/vsphere-automation-sdk-go/runtime/protocol/client"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt-mp/nsx"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt-mp/nsx/model"
)

func resourceNsxtLicense() *schema.Resource {
return &schema.Resource{
Create: resourceNsxtLicenseCreate,
Read: resourceNsxtLicenseRead,
Delete: resourceNsxtLicenseDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"license_key": {
Type: schema.TypeString,
Description: "license key",
Required: true,
ForceNew: true,
},
},
}
}

func resourceNsxtLicenseExists(licenseKey string, connector client.Connector) (bool, error) {
client := nsx.NewLicensesClient(connector)

objList, err := client.List()

if err != nil {
return false, fmt.Errorf("error during License read: %v", err)
}

for _, obj := range objList.Results {
if *obj.LicenseKey == licenseKey {
return true, nil
}
}
return false, nil
}

func resourceNsxtLicenseCreate(d *schema.ResourceData, m interface{}) error {
connector := getPolicyConnector(m)
client := nsx.NewLicensesClient(connector)

licenseKey := d.Get("license_key").(string)

obj := model.License{
LicenseKey: &licenseKey,
}

obj, err := client.Create(obj)
if err != nil {
return handleCreateError("License", licenseKey, err)
}

log.Printf("[INFO] Creating License with ID %s", *obj.LicenseKey)

d.SetId(licenseKey)
return resourceNsxtLicenseRead(d, m)
}

func resourceNsxtLicenseRead(d *schema.ResourceData, m interface{}) error {
connector := getPolicyConnector(m)
id := d.Id()
if id == "" {
return fmt.Errorf("error obtaining logical object id")
}

_, err := resourceNsxtLicenseExists(id, connector)
if err != nil {
return err
}
d.Set("license_key", id)
return nil
}

func resourceNsxtLicenseDelete(d *schema.ResourceData, m interface{}) error {
connector := getPolicyConnector(m)

id := d.Id()
if id == "" {
return fmt.Errorf("error obtaining logical object id")
}

client := nsx.NewLicensesClient(connector)

err := client.Delete(id)
if err != nil {
return fmt.Errorf("error during License delete: %v", err)
}
return nil
}
124 changes: 124 additions & 0 deletions nsxt/resource_nsxt_license_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/* Copyright © 2023 VMware, Inc. All Rights Reserved.
SPDX-License-Identifier: MPL-2.0 */

package nsxt

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

func TestAccResourceNsxtLicense_basic(t *testing.T) {
licenseKey := getTestNSXLicense()
testResourceName := "nsxt_license.test"

resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccOnlyLocalManager(t)
testAccTestFabric(t)
testAccTestLicense(t)
testAccPreCheck(t)
},
Providers: testAccProviders,
CheckDestroy: func(state *terraform.State) error {
return testAccNSXLicenseCheckDestroy(state, licenseKey)
},
Steps: []resource.TestStep{
{
Config: testAccNSXLicenseCreateTemplate(licenseKey),
Check: resource.ComposeTestCheckFunc(
testAccNSXLicenseExists(testResourceName, licenseKey),
resource.TestCheckResourceAttr(testResourceName, "license_key", licenseKey),
),
},
},
})
}

func TestAccResourceNsxtLicense_importBasic(t *testing.T) {
licenseKey := getTestNSXLicense()
testResourceName := "nsxt_license.test"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccOnlyLocalManager(t); testAccTestFabric(t); testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: func(state *terraform.State) error {
return testAccNSXLicenseCheckDestroy(state, licenseKey)
},
Steps: []resource.TestStep{
{
Config: testAccNSXLicenseCreateTemplate(licenseKey),
},
{
ResourceName: testResourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccNSXLicenseExists(resourceName, licenseKey string) resource.TestCheckFunc {
return func(state *terraform.State) error {

connector := getPolicyConnector(testAccProvider.Meta().(nsxtClients))

mymap := state.RootModule().Resources
keys := make([]string, 0, len(mymap))
for k := range mymap {
keys = append(keys, k)
}
rs, ok := state.RootModule().Resources[resourceName]
if !ok {
return fmt.Errorf("NSX License resource %s not found in resources", licenseKey)
}

resourceID := rs.Primary.ID
if resourceID == "" {
return fmt.Errorf("NSX License resource ID not set in resources ")
}

exists, err := resourceNsxtLicenseExists(resourceID, connector)
if err != nil {
return fmt.Errorf("error while retrieving License ID %s. Error: %v", resourceID, err)
} else if exists {
return nil
}

return fmt.Errorf("NSX License %s wasn't found", licenseKey)
}
}

func testAccNSXLicenseCheckDestroy(state *terraform.State, licenseKey string) error {
connector := getPolicyConnector(testAccProvider.Meta().(nsxtClients))

for _, rs := range state.RootModule().Resources {
if rs.Type != "nsxt_license" {
continue
}

resourceID := rs.Primary.Attributes["license_key"]
exists, err := resourceNsxtLicenseExists(resourceID, connector)

if err != nil {
return fmt.Errorf("error while retrieving License ID %s. Error: %v", resourceID, err)
}

if exists {
return fmt.Errorf("NSX License %s still exists", licenseKey)
}
}

return nil
}

func testAccNSXLicenseCreateTemplate(licenseKey string) string {
return fmt.Sprintf(`
resource "nsxt_license" "test" {
license_key = "%s"
}
`, licenseKey)
}
10 changes: 10 additions & 0 deletions nsxt/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,16 @@ func testAccTestVCCredentials(t *testing.T) {
}
}

func getTestNSXLicense() string {
return os.Getenv("NSXT_TEST_LICENSE")
}

func testAccTestLicense(t *testing.T) {
if getTestNSXLicense() == "" {
t.Skipf("This test requires a NSX license")
}
}

func testAccOnlyMultitenancy(t *testing.T) {
testAccNSXVersion(t, "4.1.0")
if !testAccIsMultitenancy() {
Expand Down
36 changes: 36 additions & 0 deletions website/docs/r/license.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---
subcategory: "Fabric"
layout: "nsxt"
page_title: "NSXT: nsxt_license"
description: A resource to configure a License.
---

# nsxt_license

This resource provides a method for the management of a License.
This resource is supported with NSX 4.1.0 onwards.

## Example Usage

```hcl
resource "nsxt_license" "test" {
license_key = "11011-12121-12345-00000-11111"
}
```

## Argument Reference

The following arguments are supported:

* `license_key` - (Required) license key.

## Importing

An existing License can be [imported][docs-import] into this resource, via the following command:

[docs-import]: https://www.terraform.io/cli/import

```
terraform import nsxt_license.test LICENSE_KEY
```
The above command imports License named `test` with the key `LICENSE_KEY`.

0 comments on commit 1adf6bf

Please sign in to comment.