diff --git a/cmd/token.go b/cmd/token.go index f08f8cec..60facf5c 100644 --- a/cmd/token.go +++ b/cmd/token.go @@ -1,13 +1,11 @@ package cmd import ( - "bytes" - "encoding/json" + "context" "errors" "github.com/qovery/qovery-cli/utils" + "github.com/qovery/qovery-client-go" "github.com/spf13/cobra" - "io" - "net/http" ) type TokenCreationResponseDto struct { @@ -46,47 +44,26 @@ func generateMachineToMachineAPIToken(tokenInformation *utils.TokenInformation) return "", err } - requestBody, err := json.Marshal(map[string]string{ - "name": tokenInformation.Name, - "description": tokenInformation.Description, - "scope": "ADMIN", - }) + roleId := qovery.NullableString{} + roleId.Set(&tokenInformation.Role.ID) - if err != nil { - return "", err + req := qovery.OrganizationApiTokenCreateRequest{ + Name: tokenInformation.Name, + Description: &tokenInformation.Description, + Scope: qovery.NullableOrganizationApiTokenScope{}, + RoleId: roleId, } - // apiToken endpoint is not yet exposed in the OpenAPI spec at the moment. It's planned officially for Q3 2022 - req, err := http.NewRequest( - http.MethodPost, - string("https://api.qovery.com/organization/"+tokenInformation.Organization.ID+"/apiToken"), - bytes.NewBuffer(requestBody), - ) - if err != nil { - return "", err - } - - req.Header.Set("Authorization", utils.GetAuthorizationHeaderValue(tokenType, token)) - req.Header.Set("Content-Type", "application/json") - - res, err := http.DefaultClient.Do(req) + client := utils.GetQoveryClient(tokenType, token) + createdToken, res, err := client.OrganizationApiTokenApi.CreateOrganizationApiToken(context.Background(), string(tokenInformation.Organization.ID)).OrganizationApiTokenCreateRequest(req).Execute() if err != nil { return "", err } - if res.StatusCode >= 400 { return "", errors.New("Received " + res.Status + " response while fetching environment. ") } - jsonResponse, _ := io.ReadAll(res.Body) - var tokenCreationResponseDto TokenCreationResponseDto - - err = json.Unmarshal(jsonResponse, &tokenCreationResponseDto) - if err != nil { - return "", err - } - - return tokenCreationResponseDto.Token, nil + return *createdToken.Token, nil } func init() { diff --git a/go.mod b/go.mod index a8f626e5..76dd1b5d 100644 --- a/go.mod +++ b/go.mod @@ -19,7 +19,7 @@ require ( github.com/pkg/errors v0.9.1 github.com/posthog/posthog-go v0.0.0-20221221115252-24dfed35d71a github.com/pterm/pterm v0.12.55 - github.com/qovery/qovery-client-go v0.0.0-20230915164401-2cd06785e576 + github.com/qovery/qovery-client-go v0.0.0-20230928091140-1cb795f1cb29 github.com/sirupsen/logrus v1.9.0 github.com/spf13/cobra v1.6.1 github.com/spf13/pflag v1.0.5 diff --git a/go.sum b/go.sum index 1502f5b7..d94f8361 100644 --- a/go.sum +++ b/go.sum @@ -281,6 +281,12 @@ github.com/qovery/qovery-client-go v0.0.0-20230915162603-0cabf77a242f h1:SYuj4Z5 github.com/qovery/qovery-client-go v0.0.0-20230915162603-0cabf77a242f/go.mod h1:7su0Zq+YniKNRSXNJsdrbR2/dGn7UHz3QJ2WpcxyP8k= github.com/qovery/qovery-client-go v0.0.0-20230915164401-2cd06785e576 h1:+KbVGJNUA+b/IdD87wo/F9/fTF+uhM8RKufEzjWEQTI= github.com/qovery/qovery-client-go v0.0.0-20230915164401-2cd06785e576/go.mod h1:7su0Zq+YniKNRSXNJsdrbR2/dGn7UHz3QJ2WpcxyP8k= +github.com/qovery/qovery-client-go v0.0.0-20230925112315-1af5467e045f h1:liuGhzcIL5KbLYaZvGVbQpt16Of+qfLQNGA2bq4YcK8= +github.com/qovery/qovery-client-go v0.0.0-20230925112315-1af5467e045f/go.mod h1:7su0Zq+YniKNRSXNJsdrbR2/dGn7UHz3QJ2WpcxyP8k= +github.com/qovery/qovery-client-go v0.0.0-20230927121209-d25a62c3cc92 h1:Idy6l2rolcB6QLX+woM/jnLx+QVZwM0xdCj55AkC6vA= +github.com/qovery/qovery-client-go v0.0.0-20230927121209-d25a62c3cc92/go.mod h1:7su0Zq+YniKNRSXNJsdrbR2/dGn7UHz3QJ2WpcxyP8k= +github.com/qovery/qovery-client-go v0.0.0-20230928091140-1cb795f1cb29 h1:ZZ+VZYXfDG5ATLDirlxjO6mfCBvEkmFJdjWQl+mdXT4= +github.com/qovery/qovery-client-go v0.0.0-20230928091140-1cb795f1cb29/go.mod h1:7su0Zq+YniKNRSXNJsdrbR2/dGn7UHz3QJ2WpcxyP8k= github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis= github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= diff --git a/utils/qovery.go b/utils/qovery.go index 69176c69..c8c95abe 100644 --- a/utils/qovery.go +++ b/utils/qovery.go @@ -30,10 +30,16 @@ type Organization struct { type TokenInformation struct { Organization *Organization + Role *Role Name string Description string } +type Role struct { + ID string + Name Name +} + const AdminUrl = "https://api-admin.qovery.com" func GetQoveryClient(tokenType AccessTokenType, token AccessToken) *qovery.APIClient { @@ -44,6 +50,53 @@ func GetQoveryClient(tokenType AccessTokenType, token AccessToken) *qovery.APICl return qovery.NewAPIClient(conf) } +func SelectRole(organization *Organization) (*Role, error) { + tokenType, token, err := GetAccessToken() + if err != nil { + return nil, err + } + + client := GetQoveryClient(tokenType, token) + + roles, res, err := client.OrganizationMainCallsApi.ListOrganizationAvailableRoles(context.Background(), string(organization.ID)).Execute() + if err != nil { + return nil, err + } + if res.StatusCode >= 400 { + return nil, errors.New("Received " + res.Status + " response while listing organizations. ") + } + + var roleNames []string + var rolesIds = make(map[string]string) + + for _, role := range roles.GetResults() { + roleNames = append(roleNames, *role.Name) + rolesIds[*role.Name] = *role.Id + } + + if len(roleNames) < 1 { + return nil, errors.New("No role found.") + } + + fmt.Println("Roles:") + prompt := promptui.Select{ + Items: roleNames, + Searcher: func(input string, index int) bool { + return strings.Contains(strings.ToLower(roleNames[index]), strings.ToLower(input)) + }, + } + _, selectedRole, err := prompt.Run() + if err != nil { + return nil, err + } + + return &Role{ + ID: rolesIds[selectedRole], + Name: Name(selectedRole), + }, nil + +} + func SelectOrganization() (*Organization, error) { tokenType, token, err := GetAccessToken() if err != nil { @@ -728,6 +781,12 @@ func SelectTokenInformation() (*TokenInformation, error) { return nil, err } + PrintlnInfo("Select Role") + role, err := SelectRole(organization) + if err != nil { + return nil, err + } + fmt.Println("Choose a token name") promptName := promptui.Prompt{ Label: "Token name", @@ -754,6 +813,7 @@ func SelectTokenInformation() (*TokenInformation, error) { return &TokenInformation{ organization, + role, name, description, }, nil