Skip to content

Commit

Permalink
enabled organization access key to work with CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
filippetroviccc committed Dec 5, 2020
1 parent d51dc35 commit 734c70e
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 21 deletions.
20 changes: 16 additions & 4 deletions commands/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"github.com/sirupsen/logrus"
"github.com/tenderly/tenderly-cli/model"
"github.com/tenderly/tenderly-cli/rest"
"github.com/tenderly/tenderly-cli/rest/payloads"
"github.com/tenderly/tenderly-cli/userError"
Expand Down Expand Up @@ -43,6 +44,9 @@ var loginCmd = &cobra.Command{
if len(alreadyLoggedIn) == 0 {
alreadyLoggedIn = config.GetString(config.Email)
}
if len(alreadyLoggedIn) == 0 {
alreadyLoggedIn = config.GetString(config.OrganizationName)
}
if len(alreadyLoggedIn) == 0 {
alreadyLoggedIn = config.GetString(config.AccountID)
}
Expand Down Expand Up @@ -87,7 +91,7 @@ var loginCmd = &cobra.Command{
config.SetGlobalConfig(config.AccessKey, key)
config.SetGlobalConfig(config.AccessKeyId, keyId)

user, err := rest.User.User()
principal, err := rest.User.Principal()
if err != nil {
if providedAuthenticationMethod == "access-key" {
userError.LogErrorf("cannot fetch user info: %s", userError.NewUserError(
Expand All @@ -107,9 +111,17 @@ var loginCmd = &cobra.Command{
os.Exit(1)
}

config.SetGlobalConfig(config.AccountID, user.ID)
config.SetGlobalConfig(config.Email, user.Email)
config.SetGlobalConfig(config.Username, user.Username)
config.SetGlobalConfig(config.AccountID, principal.ID)

if principal.Type == model.UserPrincipalType {
config.SetGlobalConfig(config.Email, principal.User.Email)
}

if principal.Type == model.OrganizationPrincipalType {
config.SetGlobalConfig(config.OrganizationName, principal.Organization.Name)
}

config.SetGlobalConfig(config.Username, principal.Username)

WriteGlobalConfig()

Expand Down
1 change: 1 addition & 0 deletions commands/logout.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ var logoutCmd = &cobra.Command{
config.SetGlobalConfig(config.AccessKey, "")
config.SetGlobalConfig(config.AccessKeyId, "")
config.SetGlobalConfig(config.Email, "")
config.SetGlobalConfig(config.OrganizationName, "")
config.SetGlobalConfig(config.Username, "")
config.SetGlobalConfig(config.AccountID, "")
WriteGlobalConfig()
Expand Down
17 changes: 12 additions & 5 deletions commands/whoami.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package commands

import (
"github.com/tenderly/tenderly-cli/model"
"github.com/tenderly/tenderly-cli/userError"
"os"

Expand All @@ -19,7 +20,7 @@ var whoamiCmd = &cobra.Command{
CheckLogin()
rest := newRest()

user, err := rest.User.User()
principal, err := rest.User.Principal()
if err != nil {
userError.LogErrorf("failed whoami: %s", userError.NewUserError(
err,
Expand All @@ -31,10 +32,16 @@ var whoamiCmd = &cobra.Command{
os.Exit(0)
}

logrus.Infof("ID: %s", colorizer.Bold(colorizer.Green(user.ID)))
logrus.Infof("Email: %s", colorizer.Bold(colorizer.Green(user.Email)))
if len(user.Username) != 0 {
logrus.Infof("Username: %s", colorizer.Bold(colorizer.Green(user.Username)))
logrus.Infof("ID: %s", colorizer.Bold(colorizer.Green(principal.ID)))
if principal.Type == model.UserPrincipalType {
logrus.Infof("Email: %s", colorizer.Bold(colorizer.Green(principal.User.Email)))
}
if principal.Type == model.OrganizationPrincipalType {
logrus.Infof("Organization name: %s", colorizer.Bold(colorizer.Green(principal.Organization.Name)))
}

if len(principal.Username) != 0 {
logrus.Infof("Username: %s", colorizer.Bold(colorizer.Green(principal.Username)))
}
},
}
2 changes: 2 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ const (
ProjectSlug = "project_slug"
Provider = "provider"

OrganizationName = "org_name"

Exports = "exports"

Projects = "projects"
Expand Down
24 changes: 24 additions & 0 deletions model/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ package model

import "time"

type PrincipalType string

const (
UserPrincipalType PrincipalType = "user"
OrganizationPrincipalType PrincipalType = "organization"
)

type User struct {
ID AccountID `json:"id"`

Expand All @@ -12,3 +19,20 @@ type User struct {

CreatedAt time.Time `json:"-"`
}

type Organization struct {
PrincipalID AccountID `json:"id"`

Name string `json:"name"`
Logo string `json:"logo,omitempty"`
}

type Principal struct {
ID AccountID `json:"id" sql:"type:varchar(36),pk"`

Username string `json:"username" sql:"type:varchar(255)"`
Organization *Organization `json:"organization,omitempty" pg:"fk:id"`
User *User `json:"user,omitempty" pg:"fk:id"`

Type PrincipalType `json:"type" sql:",notnull"`
}
16 changes: 8 additions & 8 deletions rest/call/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,23 @@ func NewUserCalls() *UserCalls {
return &UserCalls{}
}

func (rest *UserCalls) User() (*model.User, error) {
var userResponse payloads.UserResponse
response := client.Request("GET", "api/v1/user", nil)
func (rest *UserCalls) Principal() (*model.Principal, error) {
var principalResponse payloads.PrincipalResponse
response := client.Request("GET", "api/v1/principal", nil)

err := json.NewDecoder(response).Decode(&userResponse)
err := json.NewDecoder(response).Decode(&principalResponse)
if err != nil {
logrus.Debug("failed parsing user response")
return nil, err
}

if userResponse.Error != nil {
if principalResponse.Error != nil {
logrus.Debug("failed fetching user data ", logrus.Fields{
"error_message": userResponse.Error.Message,
"error_message": principalResponse.Error.Message,
})

return nil, userResponse.Error
return nil, principalResponse.Error
}

return userResponse.User, err
return principalResponse.Principal, err
}
6 changes: 3 additions & 3 deletions rest/payloads/userPayloads.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package payloads

import "github.com/tenderly/tenderly-cli/model"

type UserResponse struct {
User *model.User `json:"user"`
Error *ApiError `json:"error"`
type PrincipalResponse struct {
Principal *model.Principal `json:"principal"`
Error *ApiError `json:"error"`
}
2 changes: 1 addition & 1 deletion rest/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type AuthRoutes interface {
}

type UserRoutes interface {
User() (*model.User, error)
Principal() (*model.Principal, error)
}

type ProjectRoutes interface {
Expand Down

0 comments on commit 734c70e

Please sign in to comment.