diff --git a/nsxt/provider.go b/nsxt/provider.go index 362719659..4150aad07 100644 --- a/nsxt/provider.go +++ b/nsxt/provider.go @@ -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" @@ -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{ @@ -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) @@ -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 } diff --git a/website/docs/index.html.markdown b/website/docs/index.html.markdown index b7469be5b..415798791 100644 --- a/website/docs/index.html.markdown +++ b/website/docs/index.html.markdown @@ -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