From 9542075f16ea3d488a0508a84b47fec54dd52614 Mon Sep 17 00:00:00 2001 From: Daniel Hougaard Date: Tue, 12 Nov 2024 01:31:06 +0400 Subject: [PATCH] feat: move folders --- internal/client/model.go | 6 +++++ internal/client/secret_folder.go | 20 ++++++++++++++ .../resource/project_secret_folder.go | 26 +++++++++++++++---- 3 files changed, 47 insertions(+), 5 deletions(-) diff --git a/internal/client/model.go b/internal/client/model.go index 2454733..4ed80e8 100644 --- a/internal/client/model.go +++ b/internal/client/model.go @@ -1107,6 +1107,12 @@ type UpdateSecretFolderResponse struct { Folder SecretFolder `json:"folder"` } +type MoveSecretFolderRequest struct { + ID string + ProjectID string `json:"projectId"` + NewPath string `json:"newPath"` +} + type DeleteSecretFolderRequest struct { ID string `json:"id"` Environment string `json:"environment"` diff --git a/internal/client/secret_folder.go b/internal/client/secret_folder.go index 7d283c0..cfe08a4 100644 --- a/internal/client/secret_folder.go +++ b/internal/client/secret_folder.go @@ -91,6 +91,26 @@ func (client Client) UpdateSecretFolder(request UpdateSecretFolderRequest) (Upda return body, nil } +func (client Client) MoveSecretFolder(request MoveSecretFolderRequest) (UpdateSecretFolderResponse, error) { + var body UpdateSecretFolderResponse + response, err := client.Config.HttpClient. + R(). + SetResult(&body). + SetHeader("User-Agent", USER_AGENT). + SetBody(request). + Patch(fmt.Sprintf("api/v1/folders/%s/move", request.ID)) + + if err != nil { + return UpdateSecretFolderResponse{}, fmt.Errorf("CallMoveSecretFolder: Unable to complete api request [err=%s]", err) + } + + if response.IsError() { + return UpdateSecretFolderResponse{}, fmt.Errorf("CallMoveSecretFolder: Unsuccessful response. [response=%s]", string(response.Body())) + } + + return body, nil +} + func (client Client) DeleteSecretFolder(request DeleteSecretFolderRequest) (DeleteSecretFolderResponse, error) { var body DeleteSecretFolderResponse response, err := client.Config.HttpClient. diff --git a/internal/provider/resource/project_secret_folder.go b/internal/provider/resource/project_secret_folder.go index 75b5dd4..de88f7b 100644 --- a/internal/provider/resource/project_secret_folder.go +++ b/internal/provider/resource/project_secret_folder.go @@ -51,9 +51,8 @@ func (r *projectSecretFolderResource) Schema(_ context.Context, _ resource.Schem Required: true, }, "folder_path": schema.StringAttribute{ - Description: "The path where the folder should be created/updated", - Required: true, - PlanModifiers: []planmodifier.String{stringplanmodifier.RequiresReplace()}, + Description: "The path where the folder should be created/updated", + Required: true, }, "environment_slug": schema.StringAttribute{ Description: "The environment slug of the folder to modify/create", @@ -71,8 +70,9 @@ func (r *projectSecretFolderResource) Schema(_ context.Context, _ resource.Schem PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()}, }, "environment_id": schema.StringAttribute{ - Description: "The ID of the environment", - Computed: true, + Description: "The ID of the environment", + Computed: true, + PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()}, }, }, } @@ -211,6 +211,22 @@ func (r *projectSecretFolderResource) Update(ctx context.Context, req resource.U return } + if state.SecretPath.ValueString() != plan.SecretPath.ValueString() { + _, err := r.client.MoveSecretFolder(infisical.MoveSecretFolderRequest{ + ID: state.ID.ValueString(), + ProjectID: state.ProjectID.ValueString(), + NewPath: plan.SecretPath.ValueString(), + }) + + if err != nil { + resp.Diagnostics.AddError( + "Error moving secret folder", + "Couldn't move secret folder from Infiscial, unexpected error: "+err.Error(), + ) + return + } + } + updatedFolder, err := r.client.UpdateSecretFolder(infisical.UpdateSecretFolderRequest{ ProjectID: plan.ProjectID.ValueString(), Name: plan.Name.ValueString(),