in internal/provider/template_resource.go [388:572]
func (r *templateResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
// Retrieve values from plan
var plan templateResourceModel
diags := req.Plan.Get(ctx, &plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
// Get current state
var state templateResourceModel
diags = req.State.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
// update SLT template
reqSlt := backupdr.SltRest{
Name: plan.Name.ValueString(),
Description: plan.Description.ValueString(),
Override: plan.Override.ValueString(),
Href: plan.Href.ValueString(),
PolicyHref: plan.PolicyHref.ValueString(),
}
// Generate API request body from plan
reqBody := backupdr.SLATemplateApiUpdateSltOpts{
Body: optional.NewInterface(reqSlt),
}
respObject, res, err := r.client.SLATemplateApi.UpdateSlt(r.authCtx, plan.ID.ValueString(), &reqBody)
if err != nil {
resp.Diagnostics.AddError(
"Error Updating SLA Template",
"Could not update template, unexpected error: "+err.Error(),
)
return
}
if res.StatusCode != 200 {
resp.Diagnostics.AddError(
"Unable to Update SLA Template",
"An unexpected error occurred when creating the BackupDR API client. "+
"If the error is not clear, please contact the provider developers.\n\n"+
"BackupDR Client Error: "+res.Status,
)
}
// Update resource state with updated items and timestamp
// Map response body to schema and populate Computed attribute values
plan.Href = types.StringValue(respObject.Href)
plan.OptionHref = types.StringValue(respObject.OptionHref)
plan.PolicyHref = types.StringValue(respObject.PolicyHref)
// create SLT template policy
if len(plan.Policies) > len(state.Policies) {
for i, pol := range plan.Policies {
tflog.Info(ctx, "------------------ Update Method: Check Create policy : "+pol.ID.ValueString())
if pol.ID.ValueString() == "" {
tflog.Info(ctx, "------------------ Update Method: Created policy : "+pol.ID.ValueString())
reqPol := backupdr.PolicyRest{
Id: pol.ID.ValueString(),
Name: pol.Name.ValueString(),
Description: pol.Description.ValueString(),
Priority: pol.Priority.ValueString(),
Exclusiontype: pol.Exclusiontype.ValueString(),
Iscontinuous: pol.Iscontinuous.ValueBool(),
Rpo: pol.Rpo.ValueString(),
Rpom: pol.Rpom.ValueString(),
Starttime: pol.Starttime.ValueString(),
Endtime: pol.Endtime.ValueString(),
Targetvault: int32(pol.Targetvault.ValueInt64()),
Sourcevault: int32(pol.Targetvault.ValueInt64()),
Scheduletype: pol.Scheduletype.ValueString(),
Selection: pol.Selection.ValueString(),
Exclusion: pol.Exclusion.ValueString(),
Exclusioninterval: pol.Exclusioninterval.ValueString(),
Retention: pol.Retention.ValueString(),
Retentionm: pol.Retentionm.ValueString(),
Remoteretention: int32(pol.Remoteretention.ValueInt64()),
PolicyType: pol.PolicyType.ValueString(),
Op: pol.Op.ValueString(),
Verification: pol.Verification.ValueBool(),
Repeatinterval: pol.Repeatinterval.ValueString(),
Encrypt: pol.Encrypt.ValueString(),
Reptype: pol.Reptype.ValueString(),
Verifychoice: pol.Verifychoice.ValueString(),
}
// Generate API request body from plan
reqPolBody := backupdr.SLATemplateApiCreatePolicyOpts{
Body: optional.NewInterface(reqPol),
}
respPol, _, err := r.client.SLATemplateApi.CreatePolicy(r.authCtx, respObject.Id, &reqPolBody)
if err != nil {
resp.Diagnostics.AddError(
"Error Creating SLT Policy",
"Could not Create SLT policies ID: "+respObject.Id+": "+err.Error(),
)
return
}
plan.Policies[i].ID = types.StringValue(respPol.Id)
plan.Policies[i].Href = types.StringValue(respPol.Href)
}
}
}
// delete SLT template policy
if len(plan.Policies) < len(state.Policies) {
missingPolicies := findMissingPolicies(plan.Policies, state.Policies)
tflog.Info(ctx, "------------------ Update Method: Check Delete policy "+fmt.Sprint(len(missingPolicies)))
for _, pol := range missingPolicies {
tflog.Info(ctx, "------------------ Update Method: Deleted policy : "+pol.ID.ValueString())
_, err := r.client.SLATemplateApi.DeletePolicy(r.authCtx, respObject.Id, pol.ID.ValueString())
if err != nil {
resp.Diagnostics.AddError(
"Error Creating SLT Policy",
"Could not Create SLT policies ID: "+respObject.Id+": "+err.Error(),
)
return
}
}
}
// update SLT template policy
for i, pol := range plan.Policies {
reqPol := backupdr.PolicyRest{
Id: pol.ID.ValueString(),
Name: pol.Name.ValueString(),
Description: pol.Description.ValueString(),
Priority: pol.Priority.ValueString(),
Exclusiontype: pol.Exclusiontype.ValueString(),
Iscontinuous: pol.Iscontinuous.ValueBool(),
Rpo: pol.Rpo.ValueString(),
Rpom: pol.Rpom.ValueString(),
Starttime: pol.Starttime.ValueString(),
Endtime: pol.Endtime.ValueString(),
Targetvault: int32(pol.Targetvault.ValueInt64()),
Sourcevault: int32(pol.Targetvault.ValueInt64()),
Scheduletype: pol.Scheduletype.ValueString(),
Selection: pol.Selection.ValueString(),
Exclusion: pol.Exclusion.ValueString(),
Exclusioninterval: pol.Exclusioninterval.ValueString(),
Retention: pol.Retention.ValueString(),
Retentionm: pol.Retentionm.ValueString(),
Remoteretention: int32(pol.Remoteretention.ValueInt64()),
PolicyType: pol.PolicyType.ValueString(),
Op: pol.Op.ValueString(),
Verification: pol.Verification.ValueBool(),
Repeatinterval: pol.Repeatinterval.ValueString(),
Encrypt: pol.Encrypt.ValueString(),
Reptype: pol.Reptype.ValueString(),
Verifychoice: pol.Verifychoice.ValueString(),
}
// Generate API request body from plan
if pol.ID.ValueString() != "" {
tflog.Info(ctx, "------------------ Update Method: Update policy : "+pol.ID.ValueString())
reqPolBody := backupdr.SLATemplateApiUpdatePolicyOpts{
Body: optional.NewInterface(reqPol),
}
// ignore if there is no diff
respPol, _, err := r.client.SLATemplateApi.UpdatePolicy(r.authCtx, respObject.Id, pol.ID.ValueString(), &reqPolBody)
if err != nil {
resp.Diagnostics.AddError(
"Error Updating SLT Policy",
"Could not Update SLT policies ID: "+pol.ID.ValueString()+": "+err.Error(),
)
return
}
plan.Policies[i].Href = types.StringValue(respPol.Href)
}
}
diags = resp.State.Set(ctx, plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}