Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow changing folder_path on secret folder resoruces #70

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions internal/client/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
20 changes: 20 additions & 0 deletions internal/client/secret_folder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
26 changes: 21 additions & 5 deletions internal/provider/resource/project_secret_folder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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()},
},
},
}
Expand Down Expand Up @@ -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(),
Expand Down
Loading