Skip to content

Commit

Permalink
Add prechecks for create and update params
Browse files Browse the repository at this point in the history
Use plan to update local record instead of state
  • Loading branch information
RayLiu7 committed Sep 5, 2023
1 parent 0314220 commit 364145d
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 4 deletions.
24 changes: 24 additions & 0 deletions powerscale/helper/ads_provider_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,27 @@ func DeleteAdsProvider(ctx context.Context, client *client.Client, adsID string)
_, err := client.PscaleOpenAPIClient.AuthApi.DeleteAuthv14ProvidersAdsById(ctx, adsID).Execute()
return err
}

// IsCreateAdsProviderParamInvalid Verify if create params contain params only for updating
func IsCreateAdsProviderParamInvalid(plan models.AdsProviderResourceModel) bool {
if !plan.DomainController.IsNull() ||
!plan.ResetSchannel.IsNull() ||
!plan.Spns.IsUnknown() {
return true
}
return false
}

// IsUpdateAdsProviderParamInvalid Verify if update params contain params only for creating
func IsUpdateAdsProviderParamInvalid(plan models.AdsProviderResourceModel, state models.AdsProviderResourceModel) bool {
if (!plan.DNSDomain.IsNull() && !state.DNSDomain.Equal(plan.DNSDomain)) ||
(!plan.Groupnet.IsUnknown() && !state.Groupnet.Equal(plan.Groupnet)) ||
(!plan.Instance.IsNull() && !state.Instance.Equal(plan.Instance)) ||
(!plan.KerberosHdfsSpn.IsNull() && !state.KerberosHdfsSpn.Equal(plan.KerberosHdfsSpn)) ||
(!plan.KerberosNfsSpn.IsNull() && !state.KerberosNfsSpn.Equal(plan.KerberosNfsSpn)) ||
(!plan.MachineAccount.IsUnknown() && !state.MachineAccount.Equal(plan.MachineAccount)) ||
(!plan.OrganizationalUnit.IsNull() && !state.OrganizationalUnit.Equal(plan.OrganizationalUnit)) {
return true
}
return false
}
2 changes: 1 addition & 1 deletion powerscale/helper/nfs_export_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ func FilterExports(paths []types.String, ids []types.Int64, exports []powerscale

// ResolvePersonaDiff implement state
// For nfs export persona info, response may only contain UID while type/username is given
// Need to manually copy plan info to state, or state would keep the type/username as null, which is inconsistent
// Need to manually copy plan info to state, or state would keep the type/username as null, which is inconsistent.
func ResolvePersonaDiff(ctx context.Context, plan models.NfsExportResource, state *models.NfsExportResource) {
state.MapAll = assignKnownObjectToUnknown(ctx, plan.MapAll, state.MapAll)
state.MapFailure = assignKnownObjectToUnknown(ctx, plan.MapAll, state.MapAll)
Expand Down
19 changes: 16 additions & 3 deletions powerscale/provider/ads_provider_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,13 @@ func (r *AdsProviderResource) Create(ctx context.Context, req resource.CreateReq
if resp.Diagnostics.HasError() {
return
}

if helper.IsCreateAdsProviderParamInvalid(plan) {
resp.Diagnostics.AddError(
"Error creating ads provider",
"Should not provide parameters for updating",
)
return
}
adsToCreate := powerscale.V14ProvidersAdsItem{}
// Get param from tf input
err := helper.ReadFromState(ctx, plan, &adsToCreate)
Expand Down Expand Up @@ -593,6 +599,13 @@ func (r *AdsProviderResource) Update(ctx context.Context, req resource.UpdateReq
"adsState": adsState,
})

if helper.IsUpdateAdsProviderParamInvalid(adsPlan, adsState) {
resp.Diagnostics.AddError(
"Error updating ads provider",
"Should not provide parameters for creating",
)
return
}
adsID := adsState.ID.ValueString()
adsPlan.ID = adsState.ID
var adsToUpdate powerscale.V14ProvidersAdsIdParams
Expand Down Expand Up @@ -640,7 +653,7 @@ func (r *AdsProviderResource) Update(ctx context.Context, req resource.UpdateReq
return
}

err = helper.CopyFieldsToNonNestedModel(ctx, updatedAds.Ads[0], &adsState)
err = helper.CopyFieldsToNonNestedModel(ctx, updatedAds.Ads[0], &adsPlan)
if err != nil {
errStr := constants.ReadAdsProviderErrorMsg + "with error: "
message := helper.GetErrorString(err, errStr)
Expand All @@ -650,7 +663,7 @@ func (r *AdsProviderResource) Update(ctx context.Context, req resource.UpdateReq
)
return
}
diags = resp.State.Set(ctx, adsState)
diags = resp.State.Set(ctx, adsPlan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
Expand Down
26 changes: 26 additions & 0 deletions powerscale/provider/ads_provider_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ func TestAccAdsProviderResourceErrorUpdate(t *testing.T) {
Config: ProviderConfig + AdsProviderInvalidResourceConfig,
ExpectError: regexp.MustCompile(".*Bad Request*."),
},
{
Config: ProviderConfig + AdsProviderUpdatePreCheckConfig,
ExpectError: regexp.MustCompile(".*Should not provide parameters for creating*."),
},
},
})
}
Expand All @@ -167,6 +171,10 @@ func TestAccAdsProviderResourceErrorCreate(t *testing.T) {
Config: ProviderConfig + AdsProviderInvalidResourceConfig,
ExpectError: regexp.MustCompile(".*Bad Request*."),
},
{
Config: ProviderConfig + AdsProviderCreatePreCheckConfig,
ExpectError: regexp.MustCompile(".*Should not provide parameters for updating*."),
},
},
})
}
Expand Down Expand Up @@ -244,6 +252,15 @@ resource "powerscale_adsprovider" "ads_test" {
}
`, adsName)

var AdsProviderCreatePreCheckConfig = fmt.Sprintf(`
resource "powerscale_adsprovider" "ads_test" {
name = "%s"
user = "administrator"
password = "Password123!"
reset_schannel = true
}
`, adsName)

var AdsProviderUpdatedResourceConfig = fmt.Sprintf(`
resource "powerscale_adsprovider" "ads_test" {
name = "%s"
Expand All @@ -262,3 +279,12 @@ resource "powerscale_adsprovider" "ads_test" {
check_online_interval = 290
}
`, adsName)

var AdsProviderUpdatePreCheckConfig = fmt.Sprintf(`
resource "powerscale_adsprovider" "ads_test" {
name = "%s"
user = "administrator"
password = "Password123!"
kerberos_hdfs_spn = true
}
`, adsName)

0 comments on commit 364145d

Please sign in to comment.