From 8c7d5a939322a2873500d43c1d38eae2c5817304 Mon Sep 17 00:00:00 2001 From: Daniel Hougaard Date: Mon, 7 Oct 2024 13:00:20 +0400 Subject: [PATCH 1/2] feat: add position support to project envs --- internal/client/model.go | 18 +++++++----- .../provider/resource/project_environment.go | 29 ++++++++++++++++--- 2 files changed, 35 insertions(+), 12 deletions(-) diff --git a/internal/client/model.go b/internal/client/model.go index aa3188c..9fe9039 100644 --- a/internal/client/model.go +++ b/internal/client/model.go @@ -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"` } @@ -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 { @@ -1083,7 +1084,7 @@ type DeleteProjectEnvironmentRequest struct { } type DeleteProjectEnvironmentResponse struct { - Environment ProjectEnvironment `json:"environment"` + Environment ProjectEnvironmentWithPosition `json:"environment"` } type GetProjectEnvironmentByIDRequest struct { @@ -1091,18 +1092,19 @@ type GetProjectEnvironmentByIDRequest struct { } 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 { diff --git a/internal/provider/resource/project_environment.go b/internal/provider/resource/project_environment.go index baae977..5d3668e 100644 --- a/internal/provider/resource/project_environment.go +++ b/internal/provider/resource/project_environment.go @@ -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"` @@ -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, + }, }, } } @@ -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( @@ -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...) @@ -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...) @@ -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 { From e1fd4b728a6f33685a115453e719eb50312c568b Mon Sep 17 00:00:00 2001 From: Daniel Hougaard Date: Mon, 7 Oct 2024 13:02:49 +0400 Subject: [PATCH 2/2] docs --- docs/resources/project_environment.md | 10 ++++++++-- .../infisical_project_environment/resource.tf | 6 ++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/docs/resources/project_environment.md b/docs/resources/project_environment.md index 0da111c..3c5e0fe 100644 --- a/docs/resources/project_environment.md +++ b/docs/resources/project_environment.md @@ -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 } ``` @@ -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 diff --git a/examples/resources/infisical_project_environment/resource.tf b/examples/resources/infisical_project_environment/resource.tf index 4efa2f1..3b67070 100644 --- a/examples/resources/infisical_project_environment/resource.tf +++ b/examples/resources/infisical_project_environment/resource.tf @@ -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 }