Skip to content

Commit 43729d1

Browse files
authored
Fix missing content type and logic to delete org (#35)
1 parent ec45715 commit 43729d1

File tree

1 file changed

+31
-3
lines changed

1 file changed

+31
-3
lines changed

internal/provider/organization_resource.go

+31-3
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ package provider
33
import (
44
"bytes"
55
"context"
6+
"crypto/rand"
67
"crypto/tls"
78
"fmt"
89
"github.com/google/jsonapi"
910
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
1011
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
1112
"io"
1213
"net/http"
14+
"strconv"
1315
"strings"
1416
"terraform-provider-terrakube/internal/client"
1517

@@ -311,31 +313,57 @@ func (r *OrganizationResource) Delete(ctx context.Context, req resource.DeleteRe
311313
return
312314
}
313315

316+
var chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
317+
318+
ll := len(chars)
319+
b := make([]byte, 4)
320+
321+
if _, err := rand.Read(b); err != nil {
322+
resp.Diagnostics.AddError("Error generating random string to delete workspace", fmt.Sprintf("Error generating random string to delete workspace: %s", err))
323+
return
324+
}
325+
326+
for i := 0; i < 4; i++ {
327+
b[i] = chars[int(b[i])%ll]
328+
}
329+
330+
tflog.Info(ctx, "Send patch request to mark organization as deleted...")
331+
tflog.Info(ctx, fmt.Sprintf("%s_DEL_%s", data.Name.ValueString(), string(b)))
332+
314333
bodyRequest := &client.OrganizationEntity{
315-
Disabled: true,
316-
ID: data.ID.ValueString(),
334+
Disabled: true,
335+
Name: fmt.Sprintf("%s_DEL_%s", data.Name.ValueString(), string(b)),
336+
ID: data.ID.ValueString(),
337+
ExecutionMode: data.ExecutionMode.ValueString(),
338+
Description: "Deleted from terraform provider",
317339
}
318340

319341
var out = new(bytes.Buffer)
320342
err := jsonapi.MarshalPayload(out, bodyRequest)
321343

344+
tflog.Info(ctx, "Request Body Delete Organization...")
345+
tflog.Info(ctx, out.String())
346+
322347
if err != nil {
323348
resp.Diagnostics.AddError("Unable to marshal payload", fmt.Sprintf("Unable to marshal payload: %s", err))
324349
return
325350
}
326351

327352
reqOrg, err := http.NewRequest(http.MethodPatch, fmt.Sprintf("%s/api/v1/organization/%s", r.endpoint, data.ID.ValueString()), strings.NewReader(out.String()))
328353
reqOrg.Header.Add("Authorization", fmt.Sprintf("Bearer %s", r.token))
354+
reqOrg.Header.Add("Content-Type", "application/vnd.api+json")
329355
if err != nil {
330356
resp.Diagnostics.AddError("Error creating organization resource request", fmt.Sprintf("Error creating organization resource request: %s", err))
331357
return
332358
}
333359

334-
_, err = r.client.Do(reqOrg)
360+
organizationResponse, err := r.client.Do(reqOrg)
335361
if err != nil {
336362
resp.Diagnostics.AddError("Error executing organization resource request", fmt.Sprintf("Error executing organization resource request: %s", err))
337363
return
338364
}
365+
366+
tflog.Info(ctx, "Delete Organization response code: "+strconv.Itoa(organizationResponse.StatusCode))
339367
}
340368

341369
func (r *OrganizationResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {

0 commit comments

Comments
 (0)