Skip to content

Commit

Permalink
Re-implment user create for 0.3.0 changes
Browse files Browse the repository at this point in the history
  • Loading branch information
andscoop committed Jul 6, 2018
1 parent 55a1bde commit 531647c
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 42 deletions.
33 changes: 17 additions & 16 deletions cmd/user.go
Expand Up @@ -9,8 +9,7 @@ import (
)

var (
skipVerify bool
userEmail string
userEmail string

userRootCmd = &cobra.Command{
Use: "user",
Expand All @@ -19,24 +18,27 @@ var (
}

userListCmd = &cobra.Command{
Use: "list",
Short: "List astronomer users",
Long: "List astronomer users",
Run: userList,
Use: "list",
Aliases: []string{"ls"},
Short: "List astronomer users",
Long: "List astronomer users",
Run: userList,
}

userCreateCmd = &cobra.Command{
Use: "create",
Short: "Create a user in the astronomer platform",
Long: "Create a user in the astronomer platform, user will receive an invite at the email address provided",
RunE: userCreate,
Use: "create",
Aliases: []string{"cr"},
Short: "Create a user in the astronomer platform",
Long: "Create a user in the astronomer platform, user will receive an invite at the email address provided",
RunE: userCreate,
}

userDeleteCmd = &cobra.Command{
Use: "delete",
Short: "Delete an astronomer user",
Long: "Delete an astronomer user",
Run: userDelete,
Use: "delete",
Aliases: []string{"de"},
Short: "Delete an astronomer user",
Long: "Delete an astronomer user",
Run: userDelete,
}
)

Expand All @@ -49,7 +51,6 @@ func init() {

// User create
userRootCmd.AddCommand(userCreateCmd)
userCreateCmd.Flags().BoolVar(&skipVerify, "skip-verify", false, "Skips password verification on create")
userCreateCmd.Flags().StringVar(&userEmail, "email", "", "Supply user email at runtime")

// User delete
Expand All @@ -60,7 +61,7 @@ func userList(cmd *cobra.Command, args []string) {
}

func userCreate(cmd *cobra.Command, args []string) error {
err := user.CreateUser(skipVerify, userEmail)
err := user.CreateUser(userEmail)
if err != nil {
fmt.Println(err)
os.Exit(1)
Expand Down
15 changes: 11 additions & 4 deletions houston/houston.go
Expand Up @@ -115,9 +115,16 @@ var (
email: "%s",
password: "%s"
) {
success,
message,
token
user {
uuid
username
status
createdAt
updatedAt
}
token {
value
}
}
}`

Expand Down Expand Up @@ -312,7 +319,7 @@ func (c *Client) CreateOAuthToken(authCode string) (*AuthUser, error) {

// CreateUser will send a request to houston to create a new user
// Returns a Status object with a new token
func (c *Client) CreateUser(email string, password string) (*Token, error) {
func (c *Client) CreateUser(email string, password string) (*AuthUser, error) {
request := fmt.Sprintf(userCreateRequest, email, password)

response, err := c.QueryHouston(request)
Expand Down
16 changes: 8 additions & 8 deletions houston/types.go
Expand Up @@ -7,7 +7,7 @@ type HoustonResponse struct {
RemoveWorkspaceUser *Workspace `json:"workspaceRemoveUser,omitempty"`
CreateDeployment *Deployment `json:"createDeployment,omitempty"`
CreateToken *AuthUser `json:"createToken,omitempty"`
CreateUser *Token `json:"createUser,omitempty"`
CreateUser *AuthUser `json:"createUser,omitempty"`
CreateWorkspace *Workspace `json:"createWorkspace,omitempty"`
DeleteDeployment *Deployment `json:"deleteDeployment,omitempty"`
DeleteWorkspace *Workspace `json:"deleteWorkspace,omitempty"`
Expand All @@ -18,6 +18,13 @@ type HoustonResponse struct {
Errors []Error `json:"errors,omitempty"`
}

// AuthConfig holds data related to oAuth and basic authentication
type AuthConfig struct {
LocalEnabled bool `json:"localEnabled"`
GoogleEnabled bool `json:"googleEnabled"`
OauthUrl string `json:"googleOAuthUrl"`
}

type AuthUser struct {
User User `json:"user"`
Token Token `json:"token"`
Expand Down Expand Up @@ -57,13 +64,6 @@ type Error struct {
Message string `json:"message"`
}

// AuthConfig holds data related to oAuth and basic authentication
type AuthConfig struct {
LocalEnabled bool `json:"localEnabled"`
GoogleEnabled bool `json:"googleEnabled"`
OauthUrl string `json:"googleOAuthUrl"`
}

// Status defines structure of a houston response StatusResponse object
type Status struct {
Success bool `json:"success"`
Expand Down
1 change: 1 addition & 0 deletions messages/messages.go
Expand Up @@ -59,6 +59,7 @@ var (
HOUSTON_WORKSPACE_DELETE_SUCCESS = "Succesfully deleted %s (%s)\n"
HOUSTON_WORKSPACE_USER_ADD_SUCCESS = "Successfully added user %s from workspace (%s)\n"
HOUSTON_WORKSPACE_USER_REMOVE_SUCCESS = "Successfully removed user %s from workspace (%s)\n"
HOUSTON_USER_CREATE_SUCCESS = "Successfully created user (%s) with email %s\n"

INPUT_PASSWORD = "Password: "
INPUT_USERNAME = "Username (leave blank for oAuth): "
Expand Down
26 changes: 12 additions & 14 deletions user/user.go
Expand Up @@ -2,40 +2,38 @@ package user

import (
"errors"
"fmt"

"github.com/astronomerio/astro-cli/config"
"github.com/astronomerio/astro-cli/houston"
"github.com/astronomerio/astro-cli/messages"
"github.com/astronomerio/astro-cli/pkg/httputil"
"github.com/astronomerio/astro-cli/pkg/input"
)

var (
HTTP = httputil.NewHTTPClient()
http = httputil.NewHTTPClient()
api = houston.NewHoustonClient(http)
)

// CreateUser verifies input before sending a CreateUser API call to houston
func CreateUser(skipVerify bool, emailIn string) error {
API := houston.NewHoustonClient(HTTP)
email := emailIn

if len(emailIn) == 0 {
func CreateUser(email string) error {
if len(email) == 0 {
email = input.InputText("Email: ")
}

password, _ := input.InputPassword("Password: ")

if !skipVerify {
passwordVerify, _ := input.InputPassword("Re-enter Password: ")
if password != passwordVerify {
return errors.New("Passwords do not match, try again")
}
passwordVerify, _ := input.InputPassword("Re-enter Password: ")
if password != passwordVerify {
return errors.New("Passwords do not match")
}

status, err := API.CreateUser(email, password)
r, err := api.CreateUser(email, password)
if err != nil {
return err
}

config.CFG.CloudAPIToken.SetProjectString(status.Value)
fmt.Printf(messages.HOUSTON_USER_CREATE_SUCCESS, r.User.Uuid, email)

return nil
}

0 comments on commit 531647c

Please sign in to comment.