Skip to content

Commit

Permalink
do not use the client
Browse files Browse the repository at this point in the history
  • Loading branch information
pggb25 committed Sep 27, 2023
1 parent b86b9df commit 12d88ac
Showing 1 changed file with 38 additions and 15 deletions.
53 changes: 38 additions & 15 deletions cmd/token.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package cmd

import (
"context"
"bytes"
"encoding/json"
"errors"
"fmt"
"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 @@ -45,29 +47,50 @@ func generateMachineToMachineAPIToken(tokenInformation *utils.TokenInformation)
return "", err
}

req := qovery.OrganizationApiTokenCreateRequest{
Name: tokenInformation.Name,
Description: &tokenInformation.Description,
Scope: qovery.NullableOrganizationApiTokenScope{},
RoleId: qovery.NullableString{},
roleId := qovery.NullableString{}
roleId.Set(&tokenInformation.Role.ID)

requestBody, err := json.Marshal(map[string]string{
"name": tokenInformation.Name,
"description": tokenInformation.Description,
"role_id": tokenInformation.Role.ID,
})

if err != nil {
return "", err
}

// apiToken endpoint is not yet exposed in the OpenAPI spec at the moment. It's planned officially for Q3 2022
request, err := http.NewRequest(
http.MethodPost,
string("https://api.qovery.com/organization/"+tokenInformation.Organization.ID+"/apiToken"),
bytes.NewBuffer(requestBody),
)
if err != nil {
return "", err
}
req.RoleId.Set(&tokenInformation.Role.ID)

// TODO remove: for debug
tmp := fmt.Sprint(req.RoleId.IsSet())
utils.PrintlnInfo("temp IsSet " +tmp )
utils.PrintlnInfo("temp value " +*req.RoleId.Get() )
request.Header.Set("Authorization", utils.GetAuthorizationHeaderValue(tokenType, token))
request.Header.Set("Content-Type", "application/json")

client := utils.GetQoveryClient(tokenType, token)
createdToken, res, err := client.OrganizationApiTokenApi.CreateOrganizationApiToken(context.Background(), string(tokenInformation.Organization.ID)).OrganizationApiTokenCreateRequest(req).Execute()
res, err := http.DefaultClient.Do(request)
if err != nil {
return "", err
}

if res.StatusCode >= 400 {
return "", errors.New("Received " + res.Status + " response while fetching environment. ")
}

return *createdToken.Token, nil
jsonResponse, _ := io.ReadAll(res.Body)
var tokenCreationResponseDto TokenCreationResponseDto

err = json.Unmarshal(jsonResponse, &tokenCreationResponseDto)
if err != nil {
return "", err
}

return tokenCreationResponseDto.Token, nil
}

func init() {
Expand Down

0 comments on commit 12d88ac

Please sign in to comment.