Skip to content

Commit

Permalink
feat(infisical-project-group): link group by name
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielHougaard committed Jan 16, 2025
1 parent 342fb1e commit 4c3b9f3
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 17 deletions.
3 changes: 3 additions & 0 deletions examples/resources/infisical_project_group/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ resource "infisical_project" "example" {

resource "infisical_project_group" "group" {
project_id = infisical_project.example.id

# Either group_id or group_name is required.
group_id = "<>"
group_name = "<>"
roles = [
{
role_slug = "admin",
Expand Down
6 changes: 3 additions & 3 deletions internal/client/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -954,9 +954,9 @@ type CreateProjectGroupRequestRoles struct {
}

type CreateProjectGroupRequest struct {
ProjectId string `json:"projectId"`
GroupId string `json:"groupId"`
Roles []CreateProjectGroupRequestRoles `json:"roles"`
GroupIdOrName string
ProjectId string `json:"projectId"`
Roles []CreateProjectGroupRequestRoles `json:"roles"`
}

type CreateProjectGroupResponseMembers struct {
Expand Down
2 changes: 1 addition & 1 deletion 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.ProjectId, request.GroupId))
Post(fmt.Sprintf("api/v2/workspace/%s/groups/%s", request.ProjectId, request.GroupIdOrName))

if err != nil {
return CreateProjectGroupResponse{}, fmt.Errorf("CallCreateProjectGroup: Unable to complete api request [err=%s]", err)
Expand Down
49 changes: 36 additions & 13 deletions internal/provider/resource/project_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type ProjectGroupResource struct {
type ProjectGroupResourceModel struct {
ProjectID types.String `tfsdk:"project_id"`
GroupID types.String `tfsdk:"group_id"`
GroupName types.String `tfsdk:"group_name"`
Roles []ProjectGroupRole `tfsdk:"roles"`
MembershipID types.String `tfsdk:"membership_id"`
}
Expand All @@ -59,8 +60,14 @@ func (r *ProjectGroupResource) Schema(_ context.Context, _ resource.SchemaReques
Required: true,
},
"group_id": schema.StringAttribute{
Description: "The id of the group.",
Required: true,
Description: "The id of the group.",
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()},
},
"group_name": schema.StringAttribute{
Description: "The name of the group.",
Optional: true,
},
"membership_id": schema.StringAttribute{
Description: "The membership Id of the project group",
Expand Down Expand Up @@ -133,6 +140,22 @@ func (r *ProjectGroupResource) Create(ctx context.Context, req resource.CreateRe
return
}

if plan.GroupID.ValueString() == "" && plan.GroupName.ValueString() == "" {
resp.Diagnostics.AddError(
"Unable to create project group",
"Must provide either group_id or group_name",
)
return
}

if plan.GroupID.ValueString() != "" && plan.GroupName.ValueString() != "" {
resp.Diagnostics.AddError(
"Unable to create project group",
"Must provide either group_id or group_name, not both",
)
return
}

var roles []infisical.CreateProjectGroupRequestRoles
var hasAtleastOnePermanentRole bool
for _, el := range plan.Roles {
Expand Down Expand Up @@ -187,11 +210,18 @@ func (r *ProjectGroupResource) Create(ctx context.Context, req resource.CreateRe
return
}

projectGroupResponse, err := r.client.CreateProjectGroup(infisical.CreateProjectGroupRequest{
request := infisical.CreateProjectGroupRequest{
ProjectId: plan.ProjectID.ValueString(),
GroupId: plan.GroupID.ValueString(),
Roles: roles,
})
}

if plan.GroupID.ValueString() != "" {
request.GroupIdOrName = plan.GroupID.ValueString()
} else {
request.GroupIdOrName = plan.GroupName.ValueString()
}

projectGroupResponse, err := r.client.CreateProjectGroup(request)

if err != nil {
resp.Diagnostics.AddError(
Expand All @@ -202,14 +232,7 @@ func (r *ProjectGroupResource) Create(ctx context.Context, req resource.CreateRe
}

plan.MembershipID = types.StringValue(projectGroupResponse.Membership.ID)

if err != nil {
resp.Diagnostics.AddError(
"Error fetching group details",
"Couldn't find group in project, unexpected error: "+err.Error(),
)
return
}
plan.GroupID = types.StringValue(projectGroupResponse.Membership.GroupID)

diags = resp.State.Set(ctx, plan)
resp.Diagnostics.Append(diags...)
Expand Down

0 comments on commit 4c3b9f3

Please sign in to comment.