Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added nsxt_license Resource #423

Merged
merged 5 commits into from
Oct 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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