Skip to content

Commit

Permalink
Handle import for users with no invitation email (#534)
Browse files Browse the repository at this point in the history
  • Loading branch information
michieldewilde authored Apr 5, 2024
1 parent 787a6a9 commit c48c864
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 9 deletions.
5 changes: 4 additions & 1 deletion docs/resources/user.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ description: |-

### Required

- `invitation_email` (String) Email of the user. Used for sending an invitation.
- `policy` (Block List, Min: 1) (see [below for nested schema](#nestedblock--policy))
- `username` (String) Username of the user

### Optional

- `invitation_email` (String) `invitation_email` will be used to send an invitation to the specified email address. This property is required when creating a new user. This property is optional when importing an existing user.

### Read-Only

- `id` (String) The ID of this resource.
Expand Down
2 changes: 1 addition & 1 deletion spacelift/internal/structs/user_input.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package structs
import "github.com/shurcooL/graphql"

type ManagedUserInviteInput struct {
InvitationEmail graphql.String `json:"invitationEmail"`
InvitationEmail *graphql.String `json:"invitationEmail"`
Username graphql.String `json:"username"`
AccessRules []SpaceAccessRuleInput `json:"accessRules"`
}
Expand Down
25 changes: 18 additions & 7 deletions spacelift/resource_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/shurcooL/graphql"

"github.com/spacelift-io/terraform-provider-spacelift/spacelift/internal"
"github.com/spacelift-io/terraform-provider-spacelift/spacelift/internal/structs"
Expand All @@ -28,15 +29,10 @@ func resourceUser() *schema.Resource {
},

Schema: map[string]*schema.Schema{
"invitation_email": {
Type: schema.TypeString,
Description: "Email of the user. Used for sending an invitation.",
Required: true,
},
"username": {
Type: schema.TypeString,
Description: "Username of the user",
Required: true,
Description: "Username of the user",
},
"policy": {
Type: schema.TypeList,
Expand All @@ -60,6 +56,11 @@ func resourceUser() *schema.Resource {
},
},
},
"invitation_email": {
Type: schema.TypeString,
Optional: true,
Description: "`invitation_email` will be used to send an invitation to the specified email address. This property is required when creating a new user. This property is optional when importing an existing user.",
},
},
}
}
Expand All @@ -69,9 +70,19 @@ func resourceUserCreate(ctx context.Context, d *schema.ResourceData, i interface
var mutation struct {
User *structs.User `graphql:"managedUserInvite(input: $input)"`
}

var email *graphql.String
if d.Get("invitation_email") != "" {
email = toOptionalString(d.Get("invitation_email"))
}

if email == nil || *email == "" {
return diag.Errorf("invitation_email is required for new users")
}

variables := map[string]interface{}{
"input": structs.ManagedUserInviteInput{
InvitationEmail: toString(d.Get("invitation_email")),
InvitationEmail: email,
Username: toString(d.Get("username")),
AccessRules: getAccessRules(d),
},
Expand Down
18 changes: 18 additions & 0 deletions spacelift/resource_user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,24 @@ func TestUserResource(t *testing.T) {
})
})

t.Run("creates a user without invitation email returns an error", func(t *testing.T) {
randomUsername := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)

testSteps(t, []resource.TestStep{
{
Config: fmt.Sprintf(`
resource "spacelift_user" "test" {
username = "%s"
policy {
space_id = "root"
role = "ADMIN"
}
}`, randomUsername),
ExpectError: regexp.MustCompile(`invitation_email is required for new users`),
},
})
})

t.Run("can edit access list", func(t *testing.T) {
randomUsername := acctest.RandStringFromCharSet(5, acctest.CharSetAlphaNum)
exampleEmail := fmt.Sprintf("%[email protected]", randomUsername)
Expand Down

0 comments on commit c48c864

Please sign in to comment.