Skip to content

Commit

Permalink
Merge pull request #423 from ColemanJohnston/resource_license
Browse files Browse the repository at this point in the history
Added nsxt_license Resource
  • Loading branch information
annakhm authored Oct 14, 2020
2 parents 6d50c85 + 1606233 commit 0ad7c8b
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
49 changes: 49 additions & 0 deletions nsxt/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ import (
"io/ioutil"
"log"
"net/http"
"regexp"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
api "github.com/vmware/go-vmware-nsxt"
"github.com/vmware/go-vmware-nsxt/licensing"
"github.com/vmware/vsphere-automation-sdk-go/runtime/core"
"github.com/vmware/vsphere-automation-sdk-go/runtime/protocol/client"
"github.com/vmware/vsphere-automation-sdk-go/runtime/security"
Expand Down Expand Up @@ -152,6 +155,19 @@ func Provider() terraform.ResourceProvider {
Description: "Is this a policy global manager endpoint",
DefaultFunc: schema.EnvDefaultFunc("NSXT_GLOBAL_MANAGER", false),
},
"license_keys": {
Type: schema.TypeList,
Optional: true,
Description: "license keys",
ConflictsWith: []string{"vmc_token"},
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.StringMatch(
regexp.MustCompile(
"^[A-Z0-9]{5}-[A-Z0-9]{5}-[A-Z0-9]{5}-[A-Z0-9]{5}-[A-Z0-9]{5}$"),
"Must be a valid nsx license key matching: ^[A-Z0-9]{5}-[A-Z0-9]{5}-[A-Z0-9]{5}-[A-Z0-9]{5}-[A-Z0-9]{5}$"),
},
},
},

DataSourcesMap: map[string]*schema.Resource{
Expand Down Expand Up @@ -547,6 +563,34 @@ func (processor remoteBasicAuthHeaderProcessor) Process(req *http.Request) error
return nil
}

func applyLicense(c *api.APIClient, licenseKey string) error {
if c == nil {
return fmt.Errorf("API client not configured")
}

license := licensing.License{LicenseKey: licenseKey}
_, resp, err := c.LicensingApi.CreateLicense(c.Context, license)
if err != nil {
return fmt.Errorf("Error during license create: %v", err)
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("Unexpected status returned during license create: %v", resp.StatusCode)
}

return nil
}

// license keys are applied on terraform plan and are not removed
func configureLicenses(d *schema.ResourceData, clients *nsxtClients) error {
for _, licKey := range d.Get("license_keys").([]interface{}) {
err := applyLicense(clients.NsxtClient, licKey.(string))
if err != nil {
return fmt.Errorf("Error applying license key: %s. %s", licKey, err.Error())
}
}
return nil
}

func initCommonConfig(d *schema.ResourceData) commonProviderConfig {
remoteAuth := d.Get("remote_auth").(bool)
toleratePartialSuccess := d.Get("tolerate_partial_success").(bool)
Expand All @@ -573,6 +617,11 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
return nil, err
}

err = configureLicenses(d, &clients)
if err != nil {
return nil, err
}

return clients, nil
}

Expand Down
2 changes: 2 additions & 0 deletions website/docs/index.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ The following arguments are used to configure the VMware NSX-T Provider:
For on-prem deployments, this setting should not be specified.
* `global_manager` - (Optional) True if this is a global manager endpoint.
False by default.
* `license_keys` - (Optional) List of NSX-T license keys. License keys are applied
during plan and will not be deleted if they are removed from the configuration.

## NSX Logical Networking

Expand Down

0 comments on commit 0ad7c8b

Please sign in to comment.