Skip to content

Commit

Permalink
Improve ValidateConfig in machine catalog resource
Browse files Browse the repository at this point in the history
Add panic handler in machine catalog ValidateConfig
  • Loading branch information
zhuolun-citrix committed May 15, 2024
1 parent 9565743 commit c5dbc4f
Showing 1 changed file with 60 additions and 55 deletions.
115 changes: 60 additions & 55 deletions internal/daas/machine_catalog/machine_catalog_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,8 @@ func (r *machineCatalogResource) ImportState(ctx context.Context, req resource.I
}

func (r *machineCatalogResource) ValidateConfig(ctx context.Context, req resource.ValidateConfigRequest, resp *resource.ValidateConfigResponse) {
defer util.PanicHandler(&resp.Diagnostics)

var data MachineCatalogResourceModel
diags := req.Config.Get(ctx, &data)
resp.Diagnostics.Append(diags...)
Expand Down Expand Up @@ -416,24 +418,66 @@ func (r *machineCatalogResource) ValidateConfig(ctx context.Context, req resourc
"Missing Attribute Configuration",
fmt.Sprintf("Expected provisioning_scheme to be configured when value of provisioning_type is %s.", provisioningTypeMcs),
)
}
} else {
// Validate Provisioning Scheme
if data.ProvisioningScheme.AzureMachineConfig != nil {
// Validate Azure Machine Config
if data.ProvisioningScheme.AzureMachineConfig.WritebackCache != nil {
// Validate Writeback Cache
wbc := data.ProvisioningScheme.AzureMachineConfig.WritebackCache
if !wbc.PersistOsDisk.ValueBool() && wbc.PersistVm.ValueBool() {
resp.Diagnostics.AddAttributeError(
path.Root("persist_vm"),
"Incorrect Attribute Configuration",
"persist_os_disk must be enabled to enable persist_vm.",
)
}

if data.ProvisioningScheme != nil && data.ProvisioningScheme.AzureMachineConfig != nil && data.ProvisioningScheme.AzureMachineConfig.WritebackCache != nil {
wbc := data.ProvisioningScheme.AzureMachineConfig.WritebackCache
if !wbc.PersistOsDisk.ValueBool() && wbc.PersistVm.ValueBool() {
resp.Diagnostics.AddAttributeError(
path.Root("persist_vm"),
"Incorrect Attribute Configuration",
"persist_os_disk must be enabled to enable persist_vm.",
)
}
if !wbc.PersistWBC.ValueBool() && wbc.StorageCostSaving.ValueBool() {
resp.Diagnostics.AddAttributeError(
path.Root("storage_cost_saving"),
"Incorrect Attribute Configuration",
"persist_wbc must be enabled to enable storage_cost_saving.",
)
}
}

if !wbc.PersistWBC.ValueBool() && wbc.StorageCostSaving.ValueBool() {
resp.Diagnostics.AddAttributeError(
path.Root("storage_cost_saving"),
"Incorrect Attribute Configuration",
"persist_wbc must be enabled to enable storage_cost_saving.",
)
// Validate Azure Intune Enrollment
if data.ProvisioningScheme.IdentityType.ValueString() != azureAD &&
!data.ProvisioningScheme.AzureMachineConfig.EnrollInIntune.IsNull() {
resp.Diagnostics.AddAttributeError(
path.Root("enroll_in_intune"),
"Incorrect Attribute Configuration",
"enroll_in_intune can only be configured when identity_type is Azure AD.",
)
}
if data.AllocationType.ValueString() != allocationTypeStatic &&
data.ProvisioningScheme.AzureMachineConfig.EnrollInIntune.ValueBool() {
resp.Diagnostics.AddAttributeError(
path.Root("enroll_in_intune"),
"Incorrect Attribute Configuration",
fmt.Sprintf("Azure Intune auto enrollment is only supported when `allocation_type` is %s.", allocationTypeStatic),
)
}

if data.ProvisioningScheme.AzureMachineConfig.StorageType.ValueString() == util.AzureEphemeralOSDisk {
// Validate Azure Ephemeral OS Disk
if !data.ProvisioningScheme.AzureMachineConfig.UseManagedDisks.ValueBool() {
resp.Diagnostics.AddAttributeError(
path.Root("use_managed_disks"),
"Incorrect Attribute Configuration",
fmt.Sprintf("use_managed_disks must be set to true when storage_type is %s.", util.AzureEphemeralOSDisk),
)
}

if data.ProvisioningScheme.AzureMachineConfig.UseAzureComputeGallery == nil {
resp.Diagnostics.AddAttributeError(
path.Root("use_azure_compute_gallery"),
"Missing Attribute Configuration",
fmt.Sprintf("use_azure_compute_gallery must be set when storage_type is %s.", util.AzureEphemeralOSDisk),
)
}
}
}
}

Expand Down Expand Up @@ -461,45 +505,6 @@ func (r *machineCatalogResource) ValidateConfig(ctx context.Context, req resourc
)
}

if data.ProvisioningScheme.IdentityType.ValueString() != azureAD &&
data.ProvisioningScheme.AzureMachineConfig != nil &&
!data.ProvisioningScheme.AzureMachineConfig.EnrollInIntune.IsNull() {
resp.Diagnostics.AddAttributeError(
path.Root("enroll_in_intune"),
"Incorrect Attribute Configuration",
"enroll_in_intune can only be configured when identity_type is Azure AD.",
)
}

if data.AllocationType.ValueString() != allocationTypeStatic &&
data.ProvisioningScheme.AzureMachineConfig != nil &&
data.ProvisioningScheme.AzureMachineConfig.EnrollInIntune.ValueBool() {
resp.Diagnostics.AddAttributeError(
path.Root("enroll_in_intune"),
"Incorrect Attribute Configuration",
fmt.Sprintf("Azure Intune auto enrollment is only supported when `allocation_type` is %s.", allocationTypeStatic),
)
}

if data.ProvisioningScheme.AzureMachineConfig != nil &&
data.ProvisioningScheme.AzureMachineConfig.StorageType.ValueString() == util.AzureEphemeralOSDisk {
if !data.ProvisioningScheme.AzureMachineConfig.UseManagedDisks.ValueBool() {
resp.Diagnostics.AddAttributeError(
path.Root("use_managed_disks"),
"Incorrect Attribute Configuration",
fmt.Sprintf("use_managed_disks must be set to true when storage_type is %s.", util.AzureEphemeralOSDisk),
)
}

if data.ProvisioningScheme.AzureMachineConfig.UseAzureComputeGallery == nil {
resp.Diagnostics.AddAttributeError(
path.Root("use_azure_compute_gallery"),
"Missing Attribute Configuration",
fmt.Sprintf("use_azure_compute_gallery must be set when storage_type is %s.", util.AzureEphemeralOSDisk),
)
}
}

data.IsPowerManaged = types.BoolValue(true) // set power managed to true for MCS catalog
} else {
// Manual provisioning type
Expand Down

0 comments on commit c5dbc4f

Please sign in to comment.