Skip to content

Commit

Permalink
feat: add role in token
Browse files Browse the repository at this point in the history
  • Loading branch information
pggb25 committed Sep 28, 2023
1 parent afe08fa commit c4e673b
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 36 deletions.
47 changes: 12 additions & 35 deletions cmd/token.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
60 changes: 60 additions & 0 deletions utils/qovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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",
Expand All @@ -754,6 +813,7 @@ func SelectTokenInformation() (*TokenInformation, error) {

return &TokenInformation{
organization,
role,
name,
description,
}, nil
Expand Down

0 comments on commit c4e673b

Please sign in to comment.