Skip to content

Commit

Permalink
cleanup on login
Browse files Browse the repository at this point in the history
  • Loading branch information
anna-cross committed Apr 23, 2024
1 parent 79d12a3 commit 448b607
Show file tree
Hide file tree
Showing 128 changed files with 959 additions and 13,102 deletions.
14 changes: 2 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,19 +105,9 @@ func appendCell(cells []*simpletable.Cell, text string) []*simpletable.Cell {
^
```

## Setting Local Environment Variables
## Using Meroxa CLI

To start using the Meroxa CLI locally, you'll need to set the following environment variables:

```
export MEROXA_API_URL=""
export MEROXA_TENANT_SUBDOMAIN=""
export MEROXA_TENANT_EMAIL_ADDRESS=""
export MEROXA_TENANT_PASSWORD=""
```

The tenant email and password should come from an existing platform user.
To start using the Meroxa CLI, you'll need to have an existing platform user from either local or deployed mdpx instance. Meroxa CLI will prompt you for tenant url ,which should be a full url (e.g http://localhost:8090), user email and password.

## Tests

Expand Down
5 changes: 5 additions & 0 deletions cmd/meroxa/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,11 @@ func buildCommandWithBasicClient(cmd *cobra.Command, c Command) {
return err
}
}
err := global.PersistentPreRunE(cmd)
if err != nil {
return err
}

cl, err := global.NewBasicClient()
if err != nil {
return err
Expand Down
29 changes: 16 additions & 13 deletions cmd/meroxa/global/basic_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ const (
type BasicClient interface {
CollectionRequestMultipart(context.Context, string, string, string, interface{}, url.Values, map[string]string) (*http.Response, error)
CollectionRequest(context.Context, string, string, string, interface{}, url.Values) (*http.Response, error)
URLRequest(context.Context, string, string, interface{}, url.Values, http.Header, interface{}) (*http.Response, error)
URLRequest(context.Context, string, string, interface{}, url.Values, http.Header) (*http.Response, error)
AddHeader(string, string)
SetTimeout(time.Duration)
ResetBaseURL() error
}

type client struct {
Expand All @@ -37,7 +38,7 @@ type client struct {

func NewBasicClient() (BasicClient, error) {
// @TODO incorporate tenant subdomain
baseURL := GetMeroxaAPIURL()
baseURL := GetMeroxaTenantURL()
if flagAPIURL != "" {
baseURL = flagAPIURL
}
Expand Down Expand Up @@ -70,6 +71,19 @@ func NewBasicClient() (BasicClient, error) {
return r, nil
}

func (c *client) ResetBaseURL() error {
baseURL := GetMeroxaTenantURL()
if flagAPIURL != "" {
baseURL = flagAPIURL
}
u, err := url.Parse(baseURL)
if err != nil {
return err
}
c.baseURL = u
return nil
}

func (c *client) AddHeader(key, value string) {
if len(c.headers) == 0 {
c.headers = make(http.Header)
Expand Down Expand Up @@ -148,7 +162,6 @@ func (c *client) URLRequest(
body interface{},
params url.Values,
headers http.Header,
output interface{},
) (*http.Response, error) {
req, err := c.newRequest(ctx, method, path, body, params, headers)
if err != nil {
Expand All @@ -161,12 +174,6 @@ func (c *client) URLRequest(
return nil, err
}

if output != nil {
err = json.NewDecoder(resp.Body).Decode(&output)
if err != nil {
return nil, err
}
}
return resp, nil
}

Expand Down Expand Up @@ -244,9 +251,7 @@ func (c *client) newRequestMultiPart(
return nil, err
}
if _, exists := req.Header["Authorization"]; !exists {

req.Header.Set("Authorization", accessToken)

}
}

Expand Down Expand Up @@ -308,9 +313,7 @@ func (c *client) newRequest(
return nil, err
}
if _, exists := req.Header["Authorization"]; !exists {

req.Header.Set("Authorization", accessToken)

}
}
req.Header.Add("Content-Type", jsonContentType)
Expand Down
45 changes: 5 additions & 40 deletions cmd/meroxa/global/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package global

import (
"fmt"
"net/url"
"os"
"path/filepath"
"strings"
Expand All @@ -33,52 +32,18 @@ const (
envType = "env"
)

func GetMeroxaAPIURL() string {
return getEnvVal([]string{"MEROXA_API_URL"}, "https://api.meroxa.io")
func GetMeroxaTenantURL() string {
return getEnvVal([]string{"TENANT_URL"}, "https://api.meroxa.io")
}

func GetMeroxaAuthAudience() string {
return getEnvVal([]string{"MEROXA_AUTH_AUDIENCE", "MEROXA_AUDIENCE"}, "https://api.meroxa.io/v1")
}

func GetMeroxaAuthDomain() string {
return getEnvVal([]string{"MEROXA_AUTH_DOMAIN", "MEROXA_DOMAIN"}, "auth.meroxa.io")
}

func GetMeroxaAuthClientID() string {
return getEnvVal([]string{"MEROXA_AUTH_CLIENT_ID", "MEROXA_CLIENT_ID"}, "2VC9z0ZxtzTcQLDNygeEELV3lYFRZwpb")
}

func getMeroxaAuthCallbackPort() string {
return getEnvVal([]string{MeroxaAuthCallbackPort}, "21900")
}

// getMeroxaAuthCallbackHost will return the callback host.
// Note: If port is desired, it'll need to be included here.
func getMeroxaAuthCallbackHost() string {
defaultHost := fmt.Sprintf("localhost:%s", getMeroxaAuthCallbackPort())
// check if port was included or not
return getEnvVal([]string{MeroxaAuthCallbackHost}, defaultHost)
}

func getMeroxaAuthCallbackProtocol() string {
return getEnvVal([]string{MeroxaAuthCallbackProtocol}, "http")
}

// GetMeroxaAuthCallbackURL will return either the user configured oauth callback url
// or a default one: "http://localhost:21900/oauth/callback".
func GetMeroxaAuthCallbackURL() string {
callback := url.URL{
Scheme: getMeroxaAuthCallbackProtocol(),
Host: getMeroxaAuthCallbackHost(),
Path: "/oauth/callback",
}
return getEnvVal([]string{MeroxaAuthCallbackURL}, callback.String())
func GetMeroxaTenantUser() string {
return getEnvVal([]string{"TENANT_EMAIL_ADDRESS"}, "")
}

// getEnvVal returns the value of either the first existing key specified in keys, or defaultVal if none were present.
func getEnvVal(keys []string, defaultVal string) string {
for _, key := range keys {

if Config != nil {
// First tries to return the value from the meroxa configuration file
if val := Config.GetString(key); val != "" {
Expand Down
86 changes: 0 additions & 86 deletions cmd/meroxa/global/config_test.go
Original file line number Diff line number Diff line change
@@ -1,87 +1 @@
package global

import (
"testing"

"github.com/spf13/viper"
)

func TestGetMeroxaMeroxaAuthCallbackURL(t *testing.T) {
oldConfig := Config

testCases := []struct {
name string
config func() *viper.Viper
want string
}{
{
name: "when MEROXA_AUTH_CALLBACK_URL is not set",
config: func() *viper.Viper {
cfg := viper.New()
return cfg
},
want: "http://localhost:21900/oauth/callback",
},
{
name: "when MEROXA_AUTH_CALLBACK_URL is set",
config: func() *viper.Viper {
cfg := viper.New()
cfg.Set(MeroxaAuthCallbackURL, "https://nimbus.meroxa.io:3000/oauth/callback")
return cfg
},
want: "https://nimbus.meroxa.io:3000/oauth/callback",
},
{
name: "when MEROXA_AUTH_CALLBACK_PROTOCOL is set",
config: func() *viper.Viper {
cfg := viper.New()
cfg.Set(MeroxaAuthCallbackProtocol, "https")
return cfg
},
want: "https://localhost:21900/oauth/callback",
},
{
name: "when MEROXA_AUTH_CALLBACK_HOST is set",
config: func() *viper.Viper {
cfg := viper.New()
cfg.Set(MeroxaAuthCallbackHost, "nimbus.meroxa.io:3000")
return cfg
},
want: "http://nimbus.meroxa.io:3000/oauth/callback",
},
{
name: "when MEROXA_AUTH_CALLBACK_PORT is set",
config: func() *viper.Viper {
cfg := viper.New()
cfg.Set(MeroxaAuthCallbackPort, "3000")
return cfg
},
want: "http://localhost:3000/oauth/callback",
},
{
name: "when MEROXA_AUTH_CALLBACK_URL, MEROXA_AUTH_CALLBACK_PROTOCOL, and MEROXA_AUTH_CALLBACK_HOST are set",
config: func() *viper.Viper {
cfg := viper.New()
cfg.Set(MeroxaAuthCallbackURL, "https://nimbus.meroxa.io:3000/oauth/callback")
cfg.Set(MeroxaAuthCallbackPort, "https")
cfg.Set(MeroxaAuthCallbackHost, "nimbus.meroxa.io")

return cfg
},
want: "https://nimbus.meroxa.io:3000/oauth/callback",
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
Config = tc.config()
got := GetMeroxaAuthCallbackURL()

if got != tc.want {
t.Errorf("expected MEROXA_AUTH_CALLBACK_URL to be %q, got %q", tc.want, got)
}
})
}

Config = oldConfig
}
2 changes: 0 additions & 2 deletions cmd/meroxa/global/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ func handleAPIErrors(resp *http.Response) error {
if err != nil {
return err
}

// API error returned by Meroxa Platform API
return apiError
}
return nil
Expand Down
12 changes: 4 additions & 8 deletions cmd/meroxa/global/global.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,14 @@ const (
CasedPublishKeyEnv = "CASED_PUBLISH_KEY"
LatestCLIVersionUpdatedAtEnv = "LATEST_CLI_VERSION_UPDATED_AT"
DisableNotificationsUpdate = "DISABLE_NOTIFICATIONS_UPDATE"
MeroxaAuthCallbackHost = "MEROXA_AUTH_CALLBACK_HOST"
MeroxaAuthCallbackURL = "MEROXA_AUTH_CALLBACK_URL"
MeroxaAuthCallbackPort = "MEROXA_AUTH_CALLBACK_PORT"
MeroxaAuthCallbackProtocol = "MEROXA_AUTH_CALLBACK_PROTOCOL"
PublishMetricsEnv = "PUBLISH_METRICS"
RefreshTokenEnv = "REFRESH_TOKEN"
UserFeatureFlagsEnv = "USER_FEATURE_FLAGS"
UserInfoUpdatedAtEnv = "USER_INFO_UPDATED_AT"
TenantSubdomainEnv = "TENANT_SUBDOMAIN"
TenantEmailAddress = "TENANT_EMAIL_ADDRESS"
TenantPassword = "TENANT_PASSWORD"
defaultClientTimeout = time.Second * 10
// TenantPassword = "TENANT_PASSWORD"

Check failure on line 50 in cmd/meroxa/global/global.go

View workflow job for this annotation

GitHub Actions / lint

Comment should end in a period (godot)
TenantURL = "TENANT_URL"

defaultClientTimeout = time.Second * 10
)

func RegisterGlobalFlags(cmd *cobra.Command) {
Expand Down
2 changes: 1 addition & 1 deletion cmd/meroxa/global/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func NewPublisher() cased.Publisher {
if casedAPIKey != "" {
options = append(options, cased.WithPublishKey(casedAPIKey))
} else {
options = append(options, cased.WithPublishURL(fmt.Sprintf("%s/telemetry", GetMeroxaAPIURL())))
options = append(options, cased.WithPublishURL(fmt.Sprintf("%s/telemetry", GetMeroxaTenantURL())))
}

if v := Config.GetString(PublishMetricsEnv); v == "false" {
Expand Down
2 changes: 1 addition & 1 deletion cmd/meroxa/global/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ func TestNewPublisherWithoutCasedAPIKey(t *testing.T) {
Config = viper.New()
defer clearConfiguration()

apiURL := fmt.Sprintf("%s/telemetry", GetMeroxaAPIURL())
apiURL := fmt.Sprintf("%s/telemetry", GetMeroxaTenantURL())
got := NewPublisher()

if got.Options().PublishKey != "" {
Expand Down
22 changes: 18 additions & 4 deletions cmd/meroxa/global/mock/basic_client_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion cmd/meroxa/root/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (a *API) ParseArgs(args []string) error {
}

func (a *API) Execute(ctx context.Context) error {
resp, err := a.client.URLRequest(ctx, a.args.Method, a.args.Path, a.args.Body, nil, nil, nil)
resp, err := a.client.URLRequest(ctx, a.args.Method, a.args.Path, a.args.Body, nil, nil)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/meroxa/root/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func TestAPIExecution(t *testing.T) {
Body: io.NopCloser(bytes.NewReader([]byte(bodyResponse))),
}

client.EXPECT().URLRequest(ctx, "GET", "/api/collections/apps/records", "somebody", nil, nil, nil).Return(
client.EXPECT().URLRequest(ctx, "GET", "/api/collections/apps/records", "somebody", nil, nil).Return(
httpResponse,
nil,
)
Expand Down
Loading

0 comments on commit 448b607

Please sign in to comment.