Skip to content

Commit 97792fc

Browse files
authored
Added resolving for organization ID from flags first and then config file (#160)
1 parent 5f00947 commit 97792fc

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

src/pkg/cloudclient/restapi/client.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package restapi
33
import (
44
"context"
55
"encoding/json"
6+
"errors"
67
"fmt"
78
"github.com/deepmap/oapi-codegen/pkg/securityprovider"
89
"github.com/deepmap/oapi-codegen/pkg/util"
@@ -15,6 +16,8 @@ import (
1516
"net/http"
1617
)
1718

19+
var NoOrganizationError = errors.New("no organization exists in config or as parameter")
20+
1821
type Client struct {
1922
*cloudapi.ClientWithResponses
2023
restApiURL string
@@ -25,7 +28,11 @@ type Doer interface {
2528
}
2629

2730
func NewClient(ctx context.Context) (*Client, error) {
28-
return NewClientFromToken(viper.GetString(config.OtterizeAPIAddressKey), auth.GetAPIToken(ctx), viper.GetString(config.ApiSelectedOrganizationId))
31+
orgID, found := ResolveOrgID()
32+
if !found { // Shouldn't happen after login
33+
return nil, NoOrganizationError
34+
}
35+
return NewClientFromToken(viper.GetString(config.OtterizeAPIAddressKey), auth.GetAPIToken(ctx), orgID)
2936
}
3037

3138
func NewClientFromToken(apiRoot string, token string, orgId string) (*Client, error) {

src/pkg/cloudclient/restapi/utils.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ package restapi
22

33
import (
44
"github.com/otterize/otterize-cli/src/pkg/cloudclient/restapi/cloudapi"
5+
"github.com/otterize/otterize-cli/src/pkg/config"
6+
"github.com/otterize/otterize-cli/src/pkg/utils/must"
57
"github.com/samber/lo"
8+
"github.com/spf13/viper"
69
)
710

811
func LabelsToLabelInput(labels map[string]string) []cloudapi.LabelInput {
@@ -20,3 +23,24 @@ func LabelToLabelInput(key string, value string) cloudapi.LabelInput {
2023
Value: lo.Ternary(value == "", nil, &value),
2124
}
2225
}
26+
27+
func ResolveOrgID() (string, bool) {
28+
if viper.GetString(config.ApiSelectedOrganizationId) != "" {
29+
return viper.GetString(config.ApiSelectedOrganizationId), true
30+
}
31+
32+
var Config config.Config
33+
loaded, err := config.LoadConfigFile(&Config, config.ApiCredentialsFilename)
34+
must.Must(err)
35+
36+
if !loaded {
37+
return "", false
38+
}
39+
40+
if Config.OrganizationId != "" {
41+
return Config.OrganizationId, true
42+
}
43+
44+
return "", false
45+
46+
}

src/pkg/config/persistent_config.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,7 @@ func LoadApiCredentialsFile() {
7777
if !Config.Expiry.IsZero() {
7878
viper.Set(ApiUserTokenExpiryKey, Config.Expiry)
7979
}
80-
if Config.OrganizationId != "" {
81-
viper.Set(ApiSelectedOrganizationId, Config.OrganizationId)
82-
}
80+
8381
if Config.ClientId != "" || Config.ClientSecret != "" {
8482
viper.Set(ApiClientIdKey, Config.ClientId)
8583
viper.Set(ApiClientSecretKey, Config.ClientSecret)

0 commit comments

Comments
 (0)