Bläddra i källkod

Tutorial 7: Delete.

Frédéric G. MARAND 5 månader sedan
förälder
incheckning
83b095acc7
1 ändrade filer med 19 tillägg och 1 borttagningar
  1. 19 1
      internal/provider/order_resource.go

+ 19 - 1
internal/provider/order_resource.go

@@ -346,5 +346,23 @@ func (r *orderResource) Update(ctx context.Context, req resource.UpdateRequest,
 
 // Delete deletes the resource and removes the Terraform state on success.
 func (r *orderResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
-	return
+	// 1. Retrieves values from the state.
+	// 1.a The method will attempt to retrieve values from the state and convert it to an orderResourceModel struct
+	var state orderResourceModel
+	diags := req.State.Get(ctx, &state)
+	resp.Diagnostics.Append(diags...)
+	if resp.Diagnostics.HasError() {
+		return
+	}
+
+	// 2. Deletes an existing order.
+	// The method invokes the API client's DeleteOrder method.
+	err := r.client.DeleteOrder(state.ID.ValueString())
+	if err != nil {
+		resp.Diagnostics.AddError(
+			"Error Deleting HashiCups Order",
+			"Could not delete order, unexpected error: "+err.Error(),
+		)
+		return
+	}
 }