func()

in internal/provider/plan_resource.go [200:291]


func (r *planResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
	// Retrieve values from plan
	var plan planResourceModel
	diags := req.Plan.Get(ctx, &plan)
	resp.Diagnostics.Append(diags...)
	if resp.Diagnostics.HasError() {
		return
	}

	reqSla := backupdr.SlaRest{
		Description:      plan.Description.ValueString(),
		Logexpirationoff: plan.Logexpirationoff.ValueBool(),
		Dedupasyncoff:    plan.Dedupasyncoff.ValueString(),
		Expirationoff:    plan.Expirationoff.ValueString(),
		Scheduleoff:      plan.Scheduleoff.ValueString(),
	}

	if plan.Application != nil {
		reqSla.Application = &backupdr.ApplicationRest{
			Id: plan.Application.ID.ValueString(),
		}
	}

	if plan.Slp != nil {
		reqSla.Slp = &backupdr.SlpRest{
			Id: plan.Slp.ID.ValueString(),
		}
	}

	if plan.Slt != nil {
		reqSla.Slt = &backupdr.SltRest{
			Id: plan.Slt.ID.ValueString(),
		}
	}

	// Generate API request body from plan
	reqBody := backupdr.SLAApiCreateSlaOpts{
		Body: optional.NewInterface(reqSla),
	}

	// Create new sla
	respObject, _, err := r.client.SLAApi.CreateSla(r.authCtx, &reqBody)
	if err != nil {
		resp.Diagnostics.AddError(
			"Error creating SLA",
			"Could not create SLA, unexpected error: "+err.Error(),
		)
		return
	}

	// Map response body to schema and populate Computed attribute values
	plan.ID = types.StringValue(respObject.Id)
	plan.Href = types.StringValue(respObject.Href)
	plan.Stale = types.BoolValue(respObject.Stale)
	plan.Modifydate = types.Int64Value(respObject.Modifydate)
	plan.Syncdate = types.Int64Value(respObject.Syncdate)
	// plan.Immutable = types.BoolValue(respObject.Immutable)

	plan.Expirationoff = types.StringValue(respObject.Expirationoff)
	plan.Dedupasyncoff = types.StringValue(respObject.Dedupasyncoff)
	plan.Logexpirationoff = types.BoolValue(respObject.Logexpirationoff)
	plan.Scheduleoff = types.StringValue(respObject.Scheduleoff)

	plan.Application.Appname = types.StringValue(respObject.Application.Appname)
	plan.Application.Apptype = types.StringValue(respObject.Application.Apptype)
	plan.Application.Description = types.StringValue(respObject.Application.Description)
	plan.Application.Href = types.StringValue(respObject.Application.Href)
	plan.Application.Name = types.StringValue(respObject.Application.Name)
	plan.Application.Stale = types.BoolValue(respObject.Application.Stale)
	plan.Application.Syncdate = types.Int64Value(respObject.Application.Syncdate)
	plan.Application.Href = types.StringValue(respObject.Application.Href)
	plan.Application.Href = types.StringValue(respObject.Application.Href)

	plan.Slp.Href = types.StringValue(respObject.Slp.Href)
	plan.Slp.Cid = types.StringValue(respObject.Slp.Cid)
	plan.Slp.Name = types.StringValue(respObject.Slp.Name)
	plan.Slp.Stale = types.BoolValue(respObject.Slp.Stale)
	plan.Slp.Syncdate = types.Int64Value(respObject.Slp.Syncdate)

	plan.Slt.Href = types.StringValue(respObject.Slt.Href)
	plan.Slt.Name = types.StringValue(respObject.Slt.Name)
	plan.Slt.Override = types.StringValue(respObject.Slt.Override)
	plan.Slt.Sourcename = types.StringValue(respObject.Slt.Sourcename)
	plan.Slt.Stale = types.BoolValue(respObject.Slt.Stale)

	// Set state to fully populated data
	diags = resp.State.Set(ctx, plan)
	resp.Diagnostics.Append(diags...)
	if resp.Diagnostics.HasError() {
		return
	}
}