Skip to content

Commit

Permalink
Merge pull request #62 from Infisical/daniel/fix-project-env-destroy
Browse files Browse the repository at this point in the history
feat: add position support to project envs
  • Loading branch information
DanielHougaard authored Oct 7, 2024
2 parents 8803714 + e1fd4b7 commit 980fe51
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 16 deletions.
10 changes: 8 additions & 2 deletions docs/resources/project_environment.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,16 @@ provider "infisical" {
}
resource "infisical_project" "example" {
name = "example"
slug = "example"
name = "example"
slug = "example"
position = 1 # Optional
}
resource "infisical_project_environment" "pre-prod" {
name = "pre-prod"
project_id = infisical_project.example.id
slug = "preprod"
position = 2 # Optional
}
```

Expand All @@ -49,6 +51,10 @@ resource "infisical_project_environment" "pre-prod" {
- `project_id` (String) The Infisical project ID (Required for Machine Identity auth, and service tokens with multiple scopes)
- `slug` (String) The slug of the environment

### Optional

- `position` (Number) The position of the environment

### Read-Only

- `id` (String) The ID of the environment
6 changes: 4 additions & 2 deletions examples/resources/infisical_project_environment/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ provider "infisical" {
}

resource "infisical_project" "example" {
name = "example"
slug = "example"
name = "example"
slug = "example"
position = 1 # Optional
}

resource "infisical_project_environment" "pre-prod" {
name = "pre-prod"
project_id = infisical_project.example.id
slug = "preprod"
position = 2 # Optional
}
18 changes: 10 additions & 8 deletions internal/client/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,11 +281,11 @@ type ProjectEnvironment struct {
ID string `json:"id"`
}

type ProjectEnvironmentByID struct {
type ProjectEnvironmentWithPosition struct {
ID string `json:"id"`
Name string `json:"name"`
Slug string `json:"slug"`
Position int `json:"position"`
Position int64 `json:"position"`
ProjectID string `json:"projectId"`
}

Expand Down Expand Up @@ -1071,10 +1071,11 @@ type CreateProjectEnvironmentRequest struct {
Name string `json:"name"`
Slug string `json:"slug"`
ProjectID string `json:"workspaceId"`
Position int64 `json:"position"`
}

type CreateProjectEnvironmentResponse struct {
Environment ProjectEnvironment `json:"environment"`
Environment ProjectEnvironmentWithPosition `json:"environment"`
}

type DeleteProjectEnvironmentRequest struct {
Expand All @@ -1083,26 +1084,27 @@ type DeleteProjectEnvironmentRequest struct {
}

type DeleteProjectEnvironmentResponse struct {
Environment ProjectEnvironment `json:"environment"`
Environment ProjectEnvironmentWithPosition `json:"environment"`
}

type GetProjectEnvironmentByIDRequest struct {
ID string
}

type GetProjectEnvironmentByIDResponse struct {
Environment ProjectEnvironmentByID `json:"environment"`
Environment ProjectEnvironmentWithPosition `json:"environment"`
}

type UpdateProjectEnvironmentRequest struct {
ID string `json:"id"`
ID string
ProjectID string
Name string `json:"name"`
Slug string `json:"slug"`
ProjectID string `json:"workspaceId"`
Position int64 `json:"position"`
}

type UpdateProjectEnvironmentResponse struct {
Environment ProjectEnvironment `json:"environment"`
Environment ProjectEnvironmentWithPosition `json:"environment"`
}

type CreateIdentityRequest struct {
Expand Down
29 changes: 25 additions & 4 deletions internal/provider/resource/project_environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type projectEnvironmentResource struct {

type projectEnvironmentResourceModel struct {
ID types.String `tfsdk:"id"`
Position types.Int64 `tfsdk:"position"`
Slug types.String `tfsdk:"slug"`
Name types.String `tfsdk:"name"`
ProjectID types.String `tfsdk:"project_id"`
Expand Down Expand Up @@ -58,6 +59,11 @@ func (r *projectEnvironmentResource) Schema(_ context.Context, _ resource.Schema
Required: true,
PlanModifiers: []planmodifier.String{stringplanmodifier.RequiresReplace()},
},
"position": schema.Int64Attribute{
Description: "The position of the environment",
Optional: true,
Computed: true,
},
},
}
}
Expand Down Expand Up @@ -100,11 +106,25 @@ func (r *projectEnvironmentResource) Create(ctx context.Context, req resource.Cr
return
}

newProjectEnvironment, err := r.client.CreateProjectEnvironment(infisical.CreateProjectEnvironmentRequest{
request := infisical.CreateProjectEnvironmentRequest{
Name: plan.Name.ValueString(),
ProjectID: plan.ProjectID.ValueString(),
Slug: plan.Slug.ValueString(),
})
}

if !plan.Position.IsNull() {
if plan.Position.ValueInt64() < 0 {
resp.Diagnostics.AddError(
"Invalid position",
"The position must be a positive integer",
)
return
}

request.Position = plan.Position.ValueInt64()
}

newProjectEnvironment, err := r.client.CreateProjectEnvironment(request)

if err != nil {
resp.Diagnostics.AddError(
Expand All @@ -115,8 +135,7 @@ func (r *projectEnvironmentResource) Create(ctx context.Context, req resource.Cr
}

plan.ID = types.StringValue(newProjectEnvironment.Environment.ID)
plan.Name = types.StringValue(newProjectEnvironment.Environment.Name)
plan.Slug = types.StringValue(newProjectEnvironment.Environment.Slug)
plan.Position = types.Int64Value(newProjectEnvironment.Environment.Position)

diags = resp.State.Set(ctx, plan)
resp.Diagnostics.Append(diags...)
Expand Down Expand Up @@ -195,6 +214,7 @@ func (r *projectEnvironmentResource) Read(ctx context.Context, req resource.Read

state.Name = types.StringValue(projectEnvironment.Environment.Name)
state.Slug = types.StringValue(projectEnvironment.Environment.Slug)
state.Position = types.Int64Value(projectEnvironment.Environment.Position)

diags = resp.State.Set(ctx, &state)
resp.Diagnostics.Append(diags...)
Expand Down Expand Up @@ -233,6 +253,7 @@ func (r *projectEnvironmentResource) Update(ctx context.Context, req resource.Up
Name: plan.Name.ValueString(),
ID: plan.ID.ValueString(),
Slug: plan.Slug.ValueString(),
Position: plan.Position.ValueInt64(),
})

if err != nil {
Expand Down

0 comments on commit 980fe51

Please sign in to comment.