Skip to content

Commit

Permalink
Fix - User invite flow
Browse files Browse the repository at this point in the history
  • Loading branch information
GtheSheep committed Jun 15, 2022
1 parent b83326a commit 6e09040
Show file tree
Hide file tree
Showing 11 changed files with 281 additions and 114 deletions.
Binary file added .assets/lightdash.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
vendor/
terraform-provider*
env.vars
.idea
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ description: |-
# lightdash Provider


<a href="https://www.lightdash.com/" target="_blank"><img src="https://github.com/gthesheep/terraform-provider-lightdash/.assets/lightdash.jpg"/></a>



Expand Down
7 changes: 3 additions & 4 deletions docs/resources/user.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ description: |-

# lightdash_user (Resource)


Adding a user will generate an email invite link in order for them to set their name and password.



Expand All @@ -17,9 +17,7 @@ description: |-

### Required

- `email` (String) User email address
- `first_name` (String) First name
- `last_name` (String) Last name
- `email` (String) User email address to send invite link to

### Optional

Expand All @@ -28,5 +26,6 @@ description: |-
### Read-Only

- `id` (String) The ID of this resource.
- `invite_code` (String) Original invite code for the user


33 changes: 24 additions & 9 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,19 @@ go 1.17
require github.com/hashicorp/terraform-plugin-sdk/v2 v2.17.0

require (
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver/v3 v3.1.1 // indirect
github.com/Masterminds/sprig/v3 v3.2.2 // indirect
github.com/agext/levenshtein v1.2.2 // indirect
github.com/apparentlymart/go-cidr v1.1.0 // indirect
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
github.com/armon/go-radix v1.0.0 // indirect
github.com/bgentry/speakeasy v0.1.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fatih/color v1.7.0 // indirect
github.com/fatih/color v1.13.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-cmp v0.5.8 // indirect
github.com/hashicorp/errwrap v1.0.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-checkpoint v0.5.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320 // indirect
Expand All @@ -26,29 +31,39 @@ require (
github.com/hashicorp/logutils v1.0.0 // indirect
github.com/hashicorp/terraform-exec v0.16.1 // indirect
github.com/hashicorp/terraform-json v0.14.0 // indirect
github.com/hashicorp/terraform-plugin-docs v0.10.1 // indirect
github.com/hashicorp/terraform-plugin-go v0.9.1 // indirect
github.com/hashicorp/terraform-plugin-log v0.4.0 // indirect
github.com/hashicorp/terraform-registry-address v0.0.0-20210412075316-9b2996cce896 // indirect
github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734 // indirect
github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d // indirect
github.com/mattn/go-colorable v0.1.4 // indirect
github.com/mattn/go-isatty v0.0.10 // indirect
github.com/huandu/xstrings v1.3.2 // indirect
github.com/imdario/mergo v0.3.13 // indirect
github.com/kr/pretty v0.3.0 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mitchellh/cli v1.1.4 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
github.com/mitchellh/go-wordwrap v1.0.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/oklog/run v1.0.0 // indirect
github.com/posener/complete v1.2.3 // indirect
github.com/russross/blackfriday v1.6.0 // indirect
github.com/shopspring/decimal v1.3.1 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/vmihailenco/msgpack v4.0.4+incompatible // indirect
github.com/vmihailenco/msgpack/v4 v4.3.12 // indirect
github.com/vmihailenco/tagparser v0.1.1 // indirect
github.com/zclconf/go-cty v1.10.0 // indirect
golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e // indirect
golang.org/x/net v0.0.0-20210326060303-6b1517762897 // indirect
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 // indirect
golang.org/x/text v0.3.5 // indirect
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e // indirect
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2 // indirect
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/appengine v1.6.6 // indirect
google.golang.org/genproto v0.0.0-20200711021454-869866162049 // indirect
google.golang.org/grpc v1.46.0 // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/yaml.v3 v3.0.0 // indirect
)
89 changes: 76 additions & 13 deletions go.sum

Large diffs are not rendered by default.

41 changes: 40 additions & 1 deletion pkg/lightdash/invite_links.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type InviteLink struct {
ExpiresAt string `json:"expiresAt"`
InviteUrl string `json:"inviteUrl"`
OrganizationUUID string `json:"organizationUuid"`
UserUuid string `json:"userUuid"`
UserUUID string `json:"userUuid"`
Email string `json:"email"`
}

Expand All @@ -26,6 +26,25 @@ type InviteLinkResponse struct {
Status string `json:"status"`
}

func (c *Client) GetInviteLink(inviteCode string) (*InviteLink, error) {
req, err := http.NewRequest("GET", fmt.Sprintf("%s/invite-links/%s", c.ApiURL, inviteCode), nil)
if err != nil {
return nil, err
}

body, err, _ := c.doRequest(req)
if err != nil {
return nil, err
}

inviteLinkResponse := InviteLinkResponse{}
err = json.Unmarshal(body, &inviteLinkResponse)
if err != nil {
return nil, err
}
return &inviteLinkResponse.Results, nil
}

func (c *Client) CreateInviteLink(email string) (*InviteLink, error) {
newInviteLinkRequest := InviteLinkRequest{
ExpiresAt: "2099-01-01T23:59:59Z",
Expand Down Expand Up @@ -54,6 +73,26 @@ func (c *Client) CreateInviteLink(email string) (*InviteLink, error) {
return &inviteLinkResponse.Results, nil
}

func (c *Client) DeleteInviteLink(inviteCode string) (string, error) {
req, err := http.NewRequest("DELETE", fmt.Sprintf("%s/invite-links/%s", c.ApiURL, inviteCode), nil)
if err != nil {
return "", err
}

body, err, _ := c.doRequest(req)
if err != nil {
return "", err
}

inviteLinkResponse := InviteLinkResponse{}
err = json.Unmarshal(body, &inviteLinkResponse)
if err != nil {
return "", err
}

return inviteLinkResponse.Status, nil
}

func (c *Client) DeleteAllInviteLinks() (string, error) {
req, err := http.NewRequest("DELETE", fmt.Sprintf("%s/invite-links", c.ApiURL), nil)
if err != nil {
Expand Down
78 changes: 78 additions & 0 deletions pkg/lightdash/project.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package lightdash

import (
"encoding/json"
"fmt"
"net/http"
)

type Project struct {
ProjectUUID string `json:"projectUuid,omitempty"`
Name string `json:"name,omitempty"`
}

type ProjectResponse struct {
Results Project `json:"results"`
Status string `json:"status"`
}

type ProjectsResponse struct {
Results []Project `json:"results"`
Status string `json:"status"`
}

func (c *Client) GetProject(projectUUID string) (*Project, error) {
req, err := http.NewRequest("GET", fmt.Sprintf("%s/org/projects", c.ApiURL), nil)
if err != nil {
return nil, err
}

body, err, _ := c.doRequest(req)
if err != nil {
return nil, err
}

projectsResponse := ProjectsResponse{}
err = json.Unmarshal(body, &projectsResponse)
if err != nil {
return nil, err
}

for i, project := range projectsResponse.Results {
if project.ProjectUUID == projectUUID {
return &projectsResponse.Results[i], nil
}
}

return nil, fmt.Errorf("Project not found UUID %s", projectUUID)
}

// func (c *Client) CreateProject(name string) (*Project, error) {
//
// return &newProject, nil
// }
//
// func (c *Client) UpdateProject(projectUUID string) (*Project, error) {
//
// return &projectResponse.Results, nil
// }

func (c *Client) DeleteProject(projectUUID string) (string, error) {
req, err := http.NewRequest("DELETE", fmt.Sprintf("%s/org/projects/%s", c.ApiURL, projectUUID), nil)
if err != nil {
return "", err
}

body, err, _ := c.doRequest(req)
if err != nil {
return "", err
}

projectResponse := ProjectResponse{}
err = json.Unmarshal(body, &projectResponse)
if err != nil {
return "", err
}

return projectResponse.Status, nil
}
60 changes: 15 additions & 45 deletions pkg/lightdash/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"strings"
)

const DEFAULT_PASSWORD = "password"

type User struct {
UserUUID string `json:"userUuid,omitempty"`
FirstName string `json:"firstName,omitempty"`
Expand Down Expand Up @@ -55,51 +53,23 @@ func (c *Client) GetUser(userUUID string) (*User, error) {
}
}

return nil, fmt.Errorf("Did not find user UUID %s", userUUID)
return nil, fmt.Errorf("User not found UUID %s", userUUID)
}

func (c *Client) CreateUser(email string, firstName string, lastName string) (*User, error) {
inviteLink, err := c.CreateInviteLink(email)
if err != nil {
return nil, err
}

newUser := User{
FirstName: firstName,
LastName: lastName,
Email: email,
InviteCode: &inviteLink.InviteCode,
Password: DEFAULT_PASSWORD,
}

newUserData, err := json.Marshal(newUser)
if err != nil {
return nil, err
}

req, err := http.NewRequest("POST", fmt.Sprintf("%s/user", c.ApiURL), strings.NewReader(string(newUserData)))
if err != nil {
return nil, err
}

body, err, _ := c.doRequest(req)
if err != nil {
return nil, err
}

userResponse := UserResponse{}
err = json.Unmarshal(body, &userResponse)
if err != nil {
return nil, err
}

status, err := c.DeleteAllInviteLinks()
if (status != "ok") || (err != nil) {
return nil, err
}

return &userResponse.Results, nil
}
// func (c *Client) CreateUser(email string) (*User, error) {
// inviteLink, err := c.CreateInviteLink(email)
// if err != nil {
// return nil, err
// }
//
// newUser := User{
// Email: email,
// InviteCode: &inviteLink.InviteCode,
// UserUUID: inviteLink.UserUUID,
// }
//
// return &newUser, nil
// }

func (c *Client) UpdateUser(userID string, role string) (*User, error) {
updatedUser := User{
Expand Down
Loading

0 comments on commit 6e09040

Please sign in to comment.