Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): using the UIs version endpoint #3147

Merged
merged 2 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion api/openapi.yaml
Expand Up @@ -1185,10 +1185,12 @@ paths:
500:
description: "problem deleting an variable set"

/version:
/version.{fileExtension}:
get:
tags:
- api
parameters:
- $ref: "./parameters.yaml#/components/parameters/fileExtension"
summary: "Get the version of the API"
description: "Get the version of the API"
operationId: getVersion
Expand Down
20 changes: 13 additions & 7 deletions api/parameters.yaml
@@ -1,7 +1,6 @@
version: 3.0.0
components:
parameters:

# Test parameters
testId:
in: path
Expand All @@ -13,12 +12,12 @@ components:

# versioning and runs
runId:
in: path
name: runId
required: true
description: id of the run
schema:
type: integer
in: path
name: runId
required: true
description: id of the run
schema:
type: integer

version:
in: path
Expand Down Expand Up @@ -141,3 +140,10 @@ components:
description: "ID of an Linter"
schema:
type: string

fileExtension:
in: path
name: fileExtension
required: true
schema:
type: string
2 changes: 2 additions & 0 deletions api/version.yaml
Expand Up @@ -15,3 +15,5 @@ components:
type: string
agentEndpoint:
type: string
apiEndpoint:
type: string
9 changes: 8 additions & 1 deletion cli/cmd/configure_cmd.go
Expand Up @@ -22,7 +22,9 @@ var configureCmd = &cobra.Command{
PreRun: setupLogger,
Run: WithResultHandler(WithParamsHandler(configParams)(func(cmd *cobra.Command, _ []string) (string, error) {
ctx := context.Background()
flags := config.ConfigFlags{}
flags := config.ConfigFlags{
CI: configParams.CI,
}
config, err := config.LoadConfig("")
if err != nil {
return "", err
Expand All @@ -45,12 +47,17 @@ func flagProvided(cmd *cobra.Command, name string) bool {
func init() {
configureCmd.PersistentFlags().BoolVarP(&configParams.Global, "global", "g", false, "configuration will be saved in your home dir")
configureCmd.PersistentFlags().StringVarP(&configParams.Endpoint, "endpoint", "e", "", "set the value for the endpoint, so the CLI won't ask for this value")

if isCloudEnabled {
configureCmd.PersistentFlags().BoolVarP(&configParams.CI, "ci", "", false, "if cloud is used, don't ask for authentication")
}
rootCmd.AddCommand(configureCmd)
}

type configureParameters struct {
Endpoint string
Global bool
CI bool
}

func (p configureParameters) Validate(cmd *cobra.Command, args []string) []error {
Expand Down
39 changes: 16 additions & 23 deletions cli/cmd/resource_select_cmd.go
Expand Up @@ -2,24 +2,22 @@ package cmd

import (
"context"
"errors"
"fmt"
"strings"

"github.com/kubeshop/tracetest/cli/config"
"github.com/kubeshop/tracetest/cli/pkg/resourcemanager"
"github.com/spf13/cobra"
)

type selectableFn func(ctx context.Context, cfg config.Config)
type selectableFn func(ctx context.Context, cfg config.Config, flags config.ConfigFlags)

var (
selectParams = &resourceIDParameters{}
selectParams = &selectParameters{}
selectCmd *cobra.Command
selectable = strings.Join([]string{"organization"}, "|")
selectableMap = map[string]selectableFn{
"organization": func(ctx context.Context, cfg config.Config) {
configurator.ShowOrganizationSelector(ctx, cfg)
"organization": func(ctx context.Context, cfg config.Config, flags config.ConfigFlags) {
configurator.ShowOrganizationSelector(ctx, cfg, flags)
}}
)

Expand All @@ -39,31 +37,26 @@ func init() {
return "", fmt.Errorf("resource type %s not selectable. Selectable resources are %s", resourceType, selectable)
}

resourceClient, err := resources.Get(resourceType)
if err != nil {
return "", err
flags := config.ConfigFlags{
OrganizationID: selectParams.organizationID,
EnvironmentID: selectParams.environmentID,
}

resultFormat, err := resourcemanager.Formats.GetWithFallback(output, "yaml")
if err != nil {
return "", err
}

result, err := resourceClient.Get(ctx, selectParams.ResourceID, resultFormat)
if errors.Is(err, resourcemanager.ErrNotFound) {
return result, nil
}
if err != nil {
return "", err
}

selectableFn(ctx, cliConfig)
selectableFn(ctx, cliConfig, flags)
return "", nil
}),
PostRun: teardownCommand,
}

if isCloudEnabled {
selectCmd.Flags().StringVarP(&selectParams.organizationID, "organization", "", "", "organization id")
selectCmd.Flags().StringVarP(&selectParams.environmentID, "environment", "", "", "environment id")
rootCmd.AddCommand(selectCmd)
}
}

type selectParameters struct {
organizationID string
environmentID string
endpoint string
}
12 changes: 11 additions & 1 deletion cli/cmd/start_cmd.go
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"context"

"github.com/kubeshop/tracetest/cli/config"
"github.com/kubeshop/tracetest/cli/pkg/starter"
"github.com/spf13/cobra"
)
Expand All @@ -20,14 +21,23 @@ var startCmd = &cobra.Command{
Run: WithResultHandler((func(_ *cobra.Command, _ []string) (string, error) {
ctx := context.Background()

err := start.Run(ctx, cliConfig)
flags := config.ConfigFlags{
OrganizationID: selectParams.organizationID,
EnvironmentID: selectParams.environmentID,
Endpoint: selectParams.endpoint,
}

err := start.Run(ctx, cliConfig, flags)
return "", err
})),
PostRun: teardownCommand,
}

func init() {
if isCloudEnabled {
startCmd.Flags().StringVarP(&selectParams.organizationID, "organization", "", "", "organization id")
startCmd.Flags().StringVarP(&selectParams.environmentID, "environment", "", "", "environment id")
startCmd.Flags().StringVarP(&selectParams.endpoint, "endpoint", "e", "", "set the value for the endpoint, so the CLI won't ask for this value")
rootCmd.AddCommand(startCmd)
}
}
5 changes: 4 additions & 1 deletion cli/config/config.go
Expand Up @@ -20,7 +20,10 @@ var (
)

type ConfigFlags struct {
Endpoint string
Endpoint string
OrganizationID string
EnvironmentID string
CI bool
}

type Config struct {
Expand Down
63 changes: 43 additions & 20 deletions cli/config/configurator.go
Expand Up @@ -11,22 +11,24 @@ import (
cliUI "github.com/kubeshop/tracetest/cli/ui"
)

type onFinishFn func(context.Context, Config, Entry, Entry)
type onFinishFn func(context.Context, Config)

type Configurator struct {
resources *resourcemanager.Registry
ui cliUI.UI
onFinish onFinishFn
flags ConfigFlags
}

func NewConfigurator(resources *resourcemanager.Registry) Configurator {
ui := cliUI.DefaultUI
onFinish := func(_ context.Context, _ Config, _ Entry, _ Entry) {
onFinish := func(_ context.Context, _ Config) {
ui.Success("Successfully configured Tracetest CLI")
ui.Finish()
}
flags := ConfigFlags{}

return Configurator{resources, ui, onFinish}
return Configurator{resources, ui, onFinish, flags}
}

func (c Configurator) WithOnFinish(onFinish onFinishFn) Configurator {
Expand All @@ -35,8 +37,11 @@ func (c Configurator) WithOnFinish(onFinish onFinishFn) Configurator {
}

func (c Configurator) Start(ctx context.Context, prev Config, flags ConfigFlags) error {
c.flags = flags
var serverURL string
if flags.Endpoint != "" {
if prev.UIEndpoint != "" {
serverURL = prev.UIEndpoint
} else if flags.Endpoint != "" {
serverURL = flags.Endpoint
} else {
path := ""
Expand Down Expand Up @@ -67,9 +72,6 @@ func (c Configurator) Start(ctx context.Context, prev Config, flags ConfigFlags)
return fmt.Errorf("cannot get version metadata: %w", err)
}

cfg.AgentEndpoint = version.GetAgentEndpoint()
cfg.UIEndpoint = version.GetUiEndpoint()

serverType := version.GetType()
if serverType == "oss" {
err := Save(cfg)
Expand All @@ -81,11 +83,22 @@ func (c Configurator) Start(ctx context.Context, prev Config, flags ConfigFlags)
return nil
}

cfg.AgentEndpoint = version.GetAgentEndpoint()
cfg.UIEndpoint = version.GetUiEndpoint()
cfg.Scheme, cfg.Endpoint, cfg.ServerPath, err = ParseServerURL(version.GetApiEndpoint())
if err != nil {
return fmt.Errorf("cannot parse server url: %w", err)
}

if flags.CI {
return Save(cfg)
}

if prev.Jwt != "" {
cfg.Jwt = prev.Jwt
cfg.Token = prev.Token

c.ShowOrganizationSelector(ctx, cfg)
c.ShowOrganizationSelector(ctx, cfg, flags)
return nil
}

Expand All @@ -102,34 +115,44 @@ func (c Configurator) onOAuthSuccess(ctx context.Context, cfg Config) func(token
cfg.Jwt = jwt
cfg.Token = token

c.ShowOrganizationSelector(ctx, cfg)
c.ShowOrganizationSelector(ctx, cfg, c.flags)
}
}

func (c Configurator) onOAuthFailure(err error) {
c.ui.Exit(err.Error())
}

func (c Configurator) ShowOrganizationSelector(ctx context.Context, cfg Config) {
cfg, org, err := c.organizationSelector(ctx, cfg)
if err != nil {
c.ui.Exit(err.Error())
return
func (c Configurator) ShowOrganizationSelector(ctx context.Context, cfg Config, flags ConfigFlags) {
cfg.OrganizationID = flags.OrganizationID
if cfg.OrganizationID == "" {
orgID, err := c.organizationSelector(ctx, cfg)
if err != nil {
c.ui.Exit(err.Error())
return
}

cfg.OrganizationID = orgID
}

cfg, env, err := c.environmentSelector(ctx, cfg)
if err != nil {
c.ui.Exit(err.Error())
return
cfg.EnvironmentID = flags.EnvironmentID
if cfg.EnvironmentID == "" {
envID, err := c.environmentSelector(ctx, cfg)
if err != nil {
c.ui.Exit(err.Error())
return
}

cfg.EnvironmentID = envID
}

err = Save(cfg)
err := Save(cfg)
if err != nil {
c.ui.Exit(err.Error())
return
}

c.onFinish(ctx, cfg, org, env)
c.onFinish(ctx, cfg)
}

func SetupHttpClient(cfg Config) *resourcemanager.HTTPClient {
Expand Down