Skip to content

Commit

Permalink
misc: moved rest of resource methods to use IDs
Browse files Browse the repository at this point in the history
  • Loading branch information
sheensantoscapadngan committed Sep 23, 2024
1 parent 53e376a commit 0518fd3
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 82 deletions.
1 change: 0 additions & 1 deletion docs/resources/project_group.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ resource "infisical_project_group" "group" {
### Read-Only

- `membership_id` (String) The membership Id of the project group
- `project_slug` (String) The slug of the project.

<a id="nestedatt--roles"></a>
### Nested Schema for `roles`
Expand Down
16 changes: 8 additions & 8 deletions internal/client/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -817,9 +817,9 @@ type CreateProjectGroupRequestRoles struct {
}

type CreateProjectGroupRequest struct {
ProjectSlug string `json:"projectSlug"`
GroupSlug string `json:"groupSlug"`
Roles []CreateProjectGroupRequestRoles `json:"roles"`
ProjectId string `json:"projectId"`
GroupId string `json:"groupId"`
Roles []CreateProjectGroupRequestRoles `json:"roles"`
}

type CreateProjectGroupResponseMembers struct {
Expand Down Expand Up @@ -849,9 +849,9 @@ type UpdateProjectGroupRequestRoles struct {
}

type UpdateProjectGroupRequest struct {
ProjectSlug string `json:"projectSlug"`
GroupSlug string `json:"groupSlug"`
Roles []UpdateProjectGroupRequestRoles `json:"roles"`
ProjectId string `json:"projectId"`
GroupId string `json:"groupId"`
Roles []UpdateProjectGroupRequestRoles `json:"roles"`
}

type UpdateProjectGroupResponse struct {
Expand All @@ -870,8 +870,8 @@ type UpdateProjectGroupResponse struct {
}

type DeleteProjectGroupRequest struct {
ProjectSlug string `json:"projectSlug"`
GroupSlug string `json:"groupSlug"`
ProjectId string `json:"projectId"`
GroupId string `json:"groupId"`
}

type DeleteProjectGroupResponseMembers struct {
Expand Down
8 changes: 4 additions & 4 deletions internal/client/project_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func (client Client) CreateProjectGroup(request CreateProjectGroupRequest) (Crea
SetResult(&responseData).
SetHeader("User-Agent", USER_AGENT).
SetBody(request).
Post(fmt.Sprintf("api/v2/workspace/%s/groups/%s", request.ProjectSlug, request.GroupSlug))
Post(fmt.Sprintf("api/v2/workspace/%s/groups/%s", request.ProjectId, request.GroupId))

if err != nil {
return CreateProjectGroupResponse{}, fmt.Errorf("CallCreateProjectGroup: Unable to complete api request [err=%s]", err)
Expand All @@ -32,7 +32,7 @@ func (client Client) GetProjectGroupMembership(request GetProjectGroupMembership
SetResult(&responseData).
SetHeader("User-Agent", USER_AGENT).
SetBody(request).
Get(fmt.Sprintf("api/v3/workspaces/%s/groups/%s", request.ProjectId, request.GroupId))
Get(fmt.Sprintf("api/v2/workspace/%s/groups/%s", request.ProjectId, request.GroupId))

if response.StatusCode() == http.StatusNotFound {
return GetProjectGroupMembershipResponse{}, ErrNotFound
Expand All @@ -56,7 +56,7 @@ func (client Client) UpdateProjectGroup(request UpdateProjectGroupRequest) (Upda
SetResult(&responseData).
SetHeader("User-Agent", USER_AGENT).
SetBody(request).
Patch(fmt.Sprintf("api/v2/workspace/%s/groups/%s", request.ProjectSlug, request.GroupSlug))
Patch(fmt.Sprintf("api/v2/workspace/%s/groups/%s", request.ProjectId, request.GroupId))

if err != nil {
return UpdateProjectGroupResponse{}, fmt.Errorf("CallUpdateProjectGroup: Unable to complete api request [err=%s]", err)
Expand All @@ -76,7 +76,7 @@ func (client Client) DeleteProjectGroup(request DeleteProjectGroupRequest) (Dele
SetResult(&responseData).
SetHeader("User-Agent", USER_AGENT).
SetBody(request).
Delete(fmt.Sprintf("/api/v2/workspace/%s/groups/%s", request.ProjectSlug, request.GroupSlug))
Delete(fmt.Sprintf("/api/v2/workspace/%s/groups/%s", request.ProjectId, request.GroupId))

if err != nil {
return DeleteProjectGroupResponse{}, fmt.Errorf("CallDeleteProjectGroup: Unable to complete api request [err=%s]", err)
Expand Down
83 changes: 14 additions & 69 deletions internal/provider/resource/project_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ type ProjectGroupResource struct {
// projectResourceSourceModel describes the data source data model.
type ProjectGroupResourceModel struct {
ProjectID types.String `tfsdk:"project_id"`
ProjectSlug types.String `tfsdk:"project_slug"`
GroupId types.String `tfsdk:"group_id"`
GroupID types.String `tfsdk:"group_id"`
Roles []ProjectGroupRole `tfsdk:"roles"`
MembershipID types.String `tfsdk:"membership_id"`
}
Expand All @@ -59,11 +58,6 @@ func (r *ProjectGroupResource) Schema(_ context.Context, _ resource.SchemaReques
Description: "The id of the project.",
Required: true,
},
"project_slug": schema.StringAttribute{
Description: "The slug of the project.",
Computed: true,
PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()},
},
"group_id": schema.StringAttribute{
Description: "The id of the group.",
Required: true,
Expand Down Expand Up @@ -193,34 +187,10 @@ func (r *ProjectGroupResource) Create(ctx context.Context, req resource.CreateRe
return
}

projectDetail, err := r.client.GetProjectById(infisical.GetProjectByIdRequest{
ID: plan.ProjectID.ValueString(),
})

if err != nil {
resp.Diagnostics.AddError(
"Error attaching group to project",
"Couldn't fetch project details, unexpected error: "+err.Error(),
)
return
}

groupResponse, err := r.client.GetGroupById(infisical.GetGroupByIdRequest{
ID: plan.GroupId.ValueString(),
})

if err != nil {
resp.Diagnostics.AddError(
"Error attaching group to project",
"Couldn't fetch group, unexpected error: "+err.Error(),
)
return
}

projectGroupResponse, err := r.client.CreateProjectGroup(infisical.CreateProjectGroupRequest{
ProjectSlug: projectDetail.Slug,
GroupSlug: groupResponse.Slug,
Roles: roles,
ProjectId: plan.ProjectID.ValueString(),
GroupId: plan.GroupID.ValueString(),
Roles: roles,
})

if err != nil {
Expand All @@ -231,7 +201,6 @@ func (r *ProjectGroupResource) Create(ctx context.Context, req resource.CreateRe
return
}

plan.ProjectSlug = types.StringValue(projectDetail.Slug)
plan.MembershipID = types.StringValue(projectGroupResponse.Membership.ID)

if err != nil {
Expand Down Expand Up @@ -266,7 +235,7 @@ func (r *ProjectGroupResource) Read(ctx context.Context, req resource.ReadReques

projectGroupMembership, err := r.client.GetProjectGroupMembership(infisical.GetProjectGroupMembershipRequest{
ProjectId: state.ProjectID.ValueString(),
GroupId: state.GroupId.ValueString(),
GroupId: state.GroupID.ValueString(),
})

if err != nil {
Expand Down Expand Up @@ -363,10 +332,10 @@ func (r *ProjectGroupResource) Update(ctx context.Context, req resource.UpdateRe
return
}

if plan.GroupId != state.GroupId {
if plan.GroupID != state.GroupID {
resp.Diagnostics.AddError(
"Unable to update project group",
fmt.Sprintf("Cannot change group ID, previous group: %s, new group: %s", state.GroupId, plan.GroupId),
fmt.Sprintf("Cannot change group ID, previous group: %s, new group: %s", state.GroupID, plan.GroupID),
)
return
}
Expand Down Expand Up @@ -424,22 +393,10 @@ func (r *ProjectGroupResource) Update(ctx context.Context, req resource.UpdateRe
return
}

groupResponse, err := r.client.GetGroupById(infisical.GetGroupByIdRequest{
ID: plan.GroupId.ValueString(),
})

if err != nil {
resp.Diagnostics.AddError(
"Error assigning roles to group",
"Couldn't fetch group, unexpected error: "+err.Error(),
)
return
}

_, err = r.client.UpdateProjectGroup(infisical.UpdateProjectGroupRequest{
ProjectSlug: state.ProjectSlug.ValueString(),
GroupSlug: groupResponse.Slug,
Roles: roles,
_, err := r.client.UpdateProjectGroup(infisical.UpdateProjectGroupRequest{
ProjectId: state.ProjectID.ValueString(),
GroupId: state.GroupID.ValueString(),
Roles: roles,
})

if err != nil {
Expand Down Expand Up @@ -471,21 +428,9 @@ func (r *ProjectGroupResource) Delete(ctx context.Context, req resource.DeleteRe
return
}

groupResponse, err := r.client.GetGroupById(infisical.GetGroupByIdRequest{
ID: state.GroupId.ValueString(),
})

if err != nil {
resp.Diagnostics.AddError(
"Error deleting project group",
"Couldn't fetch group, unexpected error: "+err.Error(),
)
return
}

_, err = r.client.DeleteProjectGroup(infisical.DeleteProjectGroupRequest{
ProjectSlug: state.ProjectSlug.ValueString(),
GroupSlug: groupResponse.Slug,
_, err := r.client.DeleteProjectGroup(infisical.DeleteProjectGroupRequest{
ProjectId: state.ProjectID.ValueString(),
GroupId: state.GroupID.ValueString(),
})

if err != nil {
Expand Down

0 comments on commit 0518fd3

Please sign in to comment.