From 3b60fedfb5d57945862db1eec6e5f7d70dd49ffd Mon Sep 17 00:00:00 2001 From: Daniel Hougaard Date: Tue, 24 Sep 2024 14:20:15 +0400 Subject: [PATCH 1/3] Fix: error typos --- internal/client/project_user.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/client/project_user.go b/internal/client/project_user.go index 931653b..94c9725 100644 --- a/internal/client/project_user.go +++ b/internal/client/project_user.go @@ -72,11 +72,11 @@ func (client Client) GetProjectUserByUsername(request GetProjectUserByUserNameRe Post(fmt.Sprintf("api/v1/workspace/%s/memberships/details", request.ProjectID)) if err != nil { - return GetProjectUserByUserNameResponse{}, fmt.Errorf("CallCreateProject: Unable to complete api request [err=%s]", err) + return GetProjectUserByUserNameResponse{}, fmt.Errorf("CallGetProjectUserByUsername: Unable to complete api request [err=%s]", err) } if response.IsError() { - return GetProjectUserByUserNameResponse{}, fmt.Errorf("CallCreateProject: Unsuccessful response. [response=%s]", response) + return GetProjectUserByUserNameResponse{}, fmt.Errorf("CallGetProjectUserByUsername: Unsuccessful response. [response=%s]", response) } return projectUserResponse, nil From ca8f5732dbd645bc945b49843905ca6353dda3b3 Mon Sep 17 00:00:00 2001 From: Daniel Hougaard Date: Tue, 24 Sep 2024 14:20:39 +0400 Subject: [PATCH 2/3] Fix: Project membership removals causing TF to error out due to bad state --- internal/provider/resource/project_user_resource.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/internal/provider/resource/project_user_resource.go b/internal/provider/resource/project_user_resource.go index 6851001..0388c1c 100644 --- a/internal/provider/resource/project_user_resource.go +++ b/internal/provider/resource/project_user_resource.go @@ -3,6 +3,7 @@ package resource import ( "context" "fmt" + "strings" infisical "terraform-provider-infisical/internal/client" "time" @@ -345,11 +346,19 @@ func (r *ProjectUserResource) Read(ctx context.Context, req resource.ReadRequest ProjectID: state.ProjectID.ValueString(), Username: state.Username.ValueString(), }) + if err != nil { + // If the membership isn't found for whatever reason (user left the project, etc), remove the resource so we can re-create it, and fix the state. + if strings.Contains(err.Error(), "Project membership not found") { + resp.State.RemoveResource(ctx) + return + } + resp.Diagnostics.AddError( "Error fetching user", "Couldn't find user in project, unexpected error: "+err.Error(), ) + return } From 3626183a13b5b3eb987c849339454604c6e3b2d3 Mon Sep 17 00:00:00 2001 From: Daniel Hougaard Date: Tue, 24 Sep 2024 21:04:52 +0400 Subject: [PATCH 3/3] fix: requested changes --- internal/client/project_user.go | 10 +++++++++- internal/provider/resource/project_user_resource.go | 4 +--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/internal/client/project_user.go b/internal/client/project_user.go index 94c9725..bc37c79 100644 --- a/internal/client/project_user.go +++ b/internal/client/project_user.go @@ -1,6 +1,9 @@ package infisicalclient -import "fmt" +import ( + "fmt" + "net/http" +) func (client Client) InviteUsersToProject(request InviteUsersToProjectRequest) ([]ProjectMemberships, error) { var inviteUsersToProjectResponse InviteUsersToProjectResponse @@ -76,6 +79,11 @@ func (client Client) GetProjectUserByUsername(request GetProjectUserByUserNameRe } if response.IsError() { + + if response.StatusCode() == http.StatusNotFound { + return GetProjectUserByUserNameResponse{}, ErrNotFound + } + return GetProjectUserByUserNameResponse{}, fmt.Errorf("CallGetProjectUserByUsername: Unsuccessful response. [response=%s]", response) } diff --git a/internal/provider/resource/project_user_resource.go b/internal/provider/resource/project_user_resource.go index 0388c1c..f6de92f 100644 --- a/internal/provider/resource/project_user_resource.go +++ b/internal/provider/resource/project_user_resource.go @@ -3,7 +3,6 @@ package resource import ( "context" "fmt" - "strings" infisical "terraform-provider-infisical/internal/client" "time" @@ -348,8 +347,7 @@ func (r *ProjectUserResource) Read(ctx context.Context, req resource.ReadRequest }) if err != nil { - // If the membership isn't found for whatever reason (user left the project, etc), remove the resource so we can re-create it, and fix the state. - if strings.Contains(err.Error(), "Project membership not found") { + if err == infisical.ErrNotFound { resp.State.RemoveResource(ctx) return }