Skip to content

Commit

Permalink
Merge pull request #189 from vasilsatanasov/topic/vasilsatanasov/issu…
Browse files Browse the repository at this point in the history
…e-187

Fix Failed with Invalid Microsoft Licensing Config
  • Loading branch information
vasilsatanasov authored Aug 3, 2023
2 parents d7162df + 0a30a6e commit 5789e5a
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 31 deletions.
82 changes: 53 additions & 29 deletions vmc/resource_vmc_sddc.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/vmware/terraform-provider-vmc/vmc/constants"
task "github.com/vmware/terraform-provider-vmc/vmc/task"
"github.com/vmware/vsphere-automation-sdk-go/services/nsxt-vmc-aws-integration/nsx_vmc_app/infra/external"
"github.com/vmware/vsphere-automation-sdk-go/services/vmc/orgs/sddcs/clusters/msft_licensing"
"log"
"strings"
"time"
Expand All @@ -23,7 +24,6 @@ import (
"github.com/vmware/vsphere-automation-sdk-go/services/vmc/model"
"github.com/vmware/vsphere-automation-sdk-go/services/vmc/orgs"
"github.com/vmware/vsphere-automation-sdk-go/services/vmc/orgs/sddcs"
"github.com/vmware/vsphere-automation-sdk-go/services/vmc/orgs/sddcs/clusters/msft_licensing"
)

func resourceSddc() *schema.Resource {
Expand Down Expand Up @@ -235,6 +235,11 @@ func sddcSchema() map[string]*schema.Schema {
ValidateFunc: validation.StringInSlice([]string{
constants.LicenseConfigEnabled, constants.LicenseConfigDisabled, constants.CapitalLicenseConfigEnabled, constants.CapitalLicenseConfigDisabled}, false),
},
"academic_license": {
Type: schema.TypeBool,
Optional: true,
Description: "Flag to identify if it is Academic Standard or Commercial Standard License.",
},
},
},
Optional: true,
Expand Down Expand Up @@ -378,6 +383,8 @@ func resourceSddcCreate(d *schema.ResourceData, m interface{}) error {

sddcID := sddcCreateTask.ResourceId
d.SetId(*sddcID)
msftLicensingConfig := expandMsftLicenseConfig(d.Get("microsoft_licensing_config").([]interface{}))

return resource.RetryContext(context.Background(), d.Timeout(schema.TimeoutCreate), func() *resource.RetryError {
taskErr := task.RetryTaskUntilFinished(connectorWrapper, func() (model.Task, error) {
return task.GetTask(connectorWrapper, sddcCreateTask.Id)
Expand All @@ -386,10 +393,21 @@ func resourceSddcCreate(d *schema.ResourceData, m interface{}) error {
return taskErr
}
err = resourceSddcRead(d, m)
if err == nil {
return nil
if err != nil {
return resource.NonRetryableError(err)
}
return resource.NonRetryableError(err)

// Updating the microsoft_license_config after creation since
// the backend API throws an error when non nil microsoft_licensing_config
// is present in the sddc spec
if msftLicensingConfig != nil {
err = updateMsftLicenseConfig(d, m, msftLicensingConfig)
if err != nil {
return resource.NonRetryableError(err)
}
}

return nil
})
}

Expand Down Expand Up @@ -718,33 +736,40 @@ func resourceSddcUpdate(d *schema.ResourceData, m interface{}) error {
// Update Microsoft licensing config
if d.HasChange("microsoft_licensing_config") {
configChangeParam := expandMsftLicenseConfig(d.Get("microsoft_licensing_config").([]interface{}))
primaryClusterClient := sddcs.NewPrimaryclusterClient(connectorWrapper)
primaryCluster, err := primaryClusterClient.Get(orgID, sddcID)
if err != nil {
return HandleReadError(d, "Primary Cluster", sddcID, err)
}
publishClient := msft_licensing.NewPublishClient(connectorWrapper)
microsoftLicensingUpdateTask, err := publishClient.Post(orgID, sddcID, primaryCluster.ClusterId, *configChangeParam)
if err != nil {
return fmt.Errorf("error updating license : %s", err)
}
return resource.RetryContext(context.Background(), d.Timeout(schema.TimeoutCreate), func() *resource.RetryError {
taskErr := task.RetryTaskUntilFinished(connectorWrapper, func() (model.Task, error) {
return task.GetTask(connectorWrapper, microsoftLicensingUpdateTask.Id)
}, "failed updating Microsoft licensing configuration", nil)
if taskErr != nil {
return taskErr
}
err = resourceSddcRead(d, m)
if err == nil {
return nil
}
return resource.NonRetryableError(err)
})
return updateMsftLicenseConfig(d, m, configChangeParam)
}
return resourceSddcRead(d, m)
}

func updateMsftLicenseConfig(d *schema.ResourceData, m interface{}, msftLicenseConfig *model.MsftLicensingConfig) error {
connectorWrapper := m.(*connector.Wrapper)
sddcID := d.Id()
orgID := (m.(*connector.Wrapper)).OrgID
primaryClusterClient := sddcs.NewPrimaryclusterClient(connectorWrapper)
primaryCluster, err := primaryClusterClient.Get(orgID, sddcID)
if err != nil {
return HandleReadError(d, "Primary Cluster", sddcID, err)
}
publishClient := msft_licensing.NewPublishClient(connectorWrapper)
microsoftLicensingUpdateTask, err := publishClient.Post(orgID, sddcID, primaryCluster.ClusterId, *msftLicenseConfig)
if err != nil {
return fmt.Errorf("error updating license : %s", err)
}
return resource.RetryContext(context.Background(), d.Timeout(schema.TimeoutCreate), func() *resource.RetryError {
taskErr := task.RetryTaskUntilFinished(connectorWrapper, func() (model.Task, error) {
return task.GetTask(connectorWrapper, microsoftLicensingUpdateTask.Id)
}, "failed updating Microsoft licensing configuration", nil)
if taskErr != nil {
return taskErr
}
err = resourceSddcRead(d, m)
if err == nil {
return nil
}
return resource.NonRetryableError(err)
})
}

// buildAwsSddcConfig extracts the creation of the model.AwsSddcConfig, so that it's
// available for testing
func buildAwsSddcConfig(d *schema.ResourceData) (*model.AwsSddcConfig, error) {
Expand Down Expand Up @@ -794,7 +819,6 @@ func buildAwsSddcConfig(d *schema.ResourceData) (*model.AwsSddcConfig, error) {
if err != nil {
return nil, err
}
msftLicensingConfig := expandMsftLicenseConfig(d.Get("microsoft_licensing_config").([]interface{}))

return &model.AwsSddcConfig{
StorageCapacity: &storageCapacityConverted,
Expand All @@ -813,7 +837,7 @@ func buildAwsSddcConfig(d *schema.ResourceData) (*model.AwsSddcConfig, error) {
Region: region,
HostInstanceType: &hostInstanceType,
Size: &sddcSize,
MsftLicenseConfig: msftLicensingConfig,
MsftLicenseConfig: nil,
}, nil
}

Expand Down
7 changes: 6 additions & 1 deletion vmc/resource_vmc_sddc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,12 @@ resource "vmc_sddc" "sddc_zerocloud" {
create = "300m"
update = "300m"
delete = "180m"
}
}
microsoft_licensing_config {
mssql_licensing = "ENABLED"
windows_licensing = "DISABLED"
}
}
`,
sddcName,
Expand Down
3 changes: 2 additions & 1 deletion vmc/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ func expandMsftLicenseConfig(config []interface{}) *model.MsftLicensingConfig {
licenseConfigMap := config[0].(map[string]interface{})
mssqlLicensing := strings.ToUpper(licenseConfigMap["mssql_licensing"].(string))
windowsLicensing := strings.ToUpper(licenseConfigMap["windows_licensing"].(string))
licenseConfig = model.MsftLicensingConfig{MssqlLicensing: &mssqlLicensing, WindowsLicensing: &windowsLicensing}
academicLicense := licenseConfigMap["academic_license"].(bool)
licenseConfig = model.MsftLicensingConfig{MssqlLicensing: &mssqlLicensing, WindowsLicensing: &windowsLicensing, AcademicLicense: &academicLicense}
return &licenseConfig
}

Expand Down

0 comments on commit 5789e5a

Please sign in to comment.