@@ -3,13 +3,15 @@ package provider
3
3
import (
4
4
"bytes"
5
5
"context"
6
+ "crypto/rand"
6
7
"crypto/tls"
7
8
"fmt"
8
9
"github.com/google/jsonapi"
9
10
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
10
11
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
11
12
"io"
12
13
"net/http"
14
+ "strconv"
13
15
"strings"
14
16
"terraform-provider-terrakube/internal/client"
15
17
@@ -311,31 +313,57 @@ func (r *OrganizationResource) Delete(ctx context.Context, req resource.DeleteRe
311
313
return
312
314
}
313
315
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
+
314
333
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" ,
317
339
}
318
340
319
341
var out = new (bytes.Buffer )
320
342
err := jsonapi .MarshalPayload (out , bodyRequest )
321
343
344
+ tflog .Info (ctx , "Request Body Delete Organization..." )
345
+ tflog .Info (ctx , out .String ())
346
+
322
347
if err != nil {
323
348
resp .Diagnostics .AddError ("Unable to marshal payload" , fmt .Sprintf ("Unable to marshal payload: %s" , err ))
324
349
return
325
350
}
326
351
327
352
reqOrg , err := http .NewRequest (http .MethodPatch , fmt .Sprintf ("%s/api/v1/organization/%s" , r .endpoint , data .ID .ValueString ()), strings .NewReader (out .String ()))
328
353
reqOrg .Header .Add ("Authorization" , fmt .Sprintf ("Bearer %s" , r .token ))
354
+ reqOrg .Header .Add ("Content-Type" , "application/vnd.api+json" )
329
355
if err != nil {
330
356
resp .Diagnostics .AddError ("Error creating organization resource request" , fmt .Sprintf ("Error creating organization resource request: %s" , err ))
331
357
return
332
358
}
333
359
334
- _ , err = r .client .Do (reqOrg )
360
+ organizationResponse , err : = r .client .Do (reqOrg )
335
361
if err != nil {
336
362
resp .Diagnostics .AddError ("Error executing organization resource request" , fmt .Sprintf ("Error executing organization resource request: %s" , err ))
337
363
return
338
364
}
365
+
366
+ tflog .Info (ctx , "Delete Organization response code: " + strconv .Itoa (organizationResponse .StatusCode ))
339
367
}
340
368
341
369
func (r * OrganizationResource ) ImportState (ctx context.Context , req resource.ImportStateRequest , resp * resource.ImportStateResponse ) {
0 commit comments