Skip to content

Commit

Permalink
feat(cli): output normalization (#2475)
Browse files Browse the repository at this point in the history
* feat(cli): output normalization

* Updates based on feedback

* fixing unit tests

* fixing unit tests

* feature(cli): updating output to match better with specs
  • Loading branch information
xoscar authored and schoren committed May 9, 2023
1 parent ca0a3f9 commit 6b56a45
Show file tree
Hide file tree
Showing 37 changed files with 1,326 additions and 176 deletions.
9 changes: 9 additions & 0 deletions api/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,12 @@ components:
$ref: "#/components/schemas/DemoOpenTelemetryStore"
required:
- enabled
DemoList:
type: object
properties:
count:
type: integer
items:
type: array
items:
$ref: "./config.yaml#/components/schemas/Demo"
10 changes: 10 additions & 0 deletions api/environments.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@ openapi: 3.0.0

components:
schemas:
EnvironmentResourceList:
type: object
properties:
count:
type: integer
items:
type: array
items:
$ref: "./environments.yaml#/components/schemas/EnvironmentResource"

EnvironmentResource:
type: object
description: "Represents an environment structured into the Resources format."
Expand Down
18 changes: 2 additions & 16 deletions api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -882,14 +882,7 @@ paths:
content:
application/json:
schema:
type: object
properties:
count:
type: integer
items:
type: array
items:
$ref: "./config.yaml#/components/schemas/Demo"
$ref: "./config.yaml#/components/schemas/DemoList"
text/yaml:
schema:
type: object
Expand Down Expand Up @@ -1075,14 +1068,7 @@ paths:
content:
application/json:
schema:
type: object
properties:
count:
type: integer
items:
type: array
items:
$ref: "./environments.yaml#/components/schemas/EnvironmentResource"
$ref: "./environments.yaml#/components/schemas/EnvironmentResourceList"
text/yaml:
schema:
type: object
Expand Down
22 changes: 17 additions & 5 deletions cli/actions/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package actions

import (
"context"
"fmt"

"github.com/kubeshop/tracetest/cli/file"
"github.com/kubeshop/tracetest/cli/openapi"
"github.com/kubeshop/tracetest/cli/utils"
"github.com/kubeshop/tracetest/server/model/yaml"
)
Expand Down Expand Up @@ -31,18 +33,28 @@ func (configActions) Name() string {
return "config"
}

func (config configActions) Apply(ctx context.Context, fileContent file.File) (*file.File, error) {
return config.resourceClient.Update(ctx, fileContent, currentConfigID)
func (config configActions) GetID(file *file.File) (string, error) {
resource, err := config.formatter.ToStruct(file)
if err != nil {
return "", err
}

return *resource.(openapi.ConfigurationResource).Spec.Id, nil
}

func (config configActions) Apply(ctx context.Context, fileContent file.File) (result *file.File, err error) {
result, err = config.resourceClient.Update(ctx, fileContent, currentConfigID)
return result, err
}

func (config configActions) Get(ctx context.Context, ID string) (*file.File, error) {
return config.resourceClient.Get(ctx, currentConfigID)
}

func (config configActions) List(ctx context.Context, listArgs utils.ListArgs) (*file.File, error) {
return nil, ErrNotSupportedResourceAction
return nil, fmt.Errorf("Config does not support listing. Try `tracetest get config` instead")
}

func (config configActions) Delete(ctx context.Context, ID string) error {
return ErrNotSupportedResourceAction
func (config configActions) Delete(ctx context.Context, ID string) (string, error) {
return "Config successfully reset to default", ErrNotSupportedResourceAction
}
22 changes: 17 additions & 5 deletions cli/actions/datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package actions

import (
"context"
"fmt"

"github.com/kubeshop/tracetest/cli/file"
"github.com/kubeshop/tracetest/cli/openapi"
Expand Down Expand Up @@ -32,21 +33,32 @@ func (d *dataStoreActions) Name() string {
return "datastore"
}

func (d *dataStoreActions) Apply(ctx context.Context, fileContent file.File) (*file.File, error) {
func (d dataStoreActions) GetID(file *file.File) (string, error) {
resource, err := d.formatter.ToStruct(file)
if err != nil {
return "", err
}

return *resource.(openapi.DataStoreResource).Spec.Id, nil
}

func (d *dataStoreActions) Apply(ctx context.Context, fileContent file.File) (result *file.File, err error) {
var dataStore openapi.DataStore
mapstructure.Decode(fileContent.Definition().Spec, &dataStore)

return d.resourceClient.Update(ctx, fileContent, currentConfigID)
result, err = d.resourceClient.Update(ctx, fileContent, currentConfigID)
return result, err
}

func (d *dataStoreActions) List(ctx context.Context, args utils.ListArgs) (*file.File, error) {
return nil, ErrNotSupportedResourceAction
return nil, fmt.Errorf("DataStore does not support listing. Try `tracetest get datastore`")
}

func (d *dataStoreActions) Get(ctx context.Context, id string) (*file.File, error) {
return d.resourceClient.Get(ctx, currentConfigID)
}

func (d *dataStoreActions) Delete(ctx context.Context, id string) error {
return d.resourceClient.Delete(ctx, currentConfigID)
func (d *dataStoreActions) Delete(ctx context.Context, id string) (string, error) {
err := d.resourceClient.Delete(ctx, currentConfigID)
return "DataStore removed. Defaulting back to no-tracing mode", err
}
22 changes: 17 additions & 5 deletions cli/actions/demo.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,35 @@ func (demoActions) Name() string {
return "demo"
}

func (demo demoActions) Apply(ctx context.Context, fileContent file.File) (*file.File, error) {
func (demo demoActions) GetID(file *file.File) (string, error) {
resource, err := demo.formatter.ToStruct(file)
if err != nil {
return "", err
}

return *resource.(openapi.Demo).Spec.Id, nil
}

func (demo demoActions) Apply(ctx context.Context, fileContent file.File) (result *file.File, err error) {
var demoResource openapi.Demo
mapstructure.Decode(fileContent.Definition().Spec, &demoResource.Spec)

if demoResource.Spec.Id == nil || *demoResource.Spec.Id == "" {
return demo.resourceClient.Create(ctx, fileContent)
result, err = demo.resourceClient.Create(ctx, fileContent)
return result, err
}

return demo.resourceClient.Update(ctx, fileContent, *demoResource.Spec.Id)
result, err = demo.resourceClient.Update(ctx, fileContent, *demoResource.Spec.Id)
return result, err
}

func (demo demoActions) List(ctx context.Context, listArgs utils.ListArgs) (*file.File, error) {
return demo.resourceClient.List(ctx, listArgs)
}

func (demo demoActions) Delete(ctx context.Context, ID string) error {
return demo.resourceClient.Delete(ctx, ID)
func (demo demoActions) Delete(ctx context.Context, ID string) (string, error) {
err := demo.resourceClient.Delete(ctx, ID)
return "Demo successfully deleted", err
}

func (demo demoActions) Get(ctx context.Context, ID string) (*file.File, error) {
Expand Down
21 changes: 16 additions & 5 deletions cli/actions/environments.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,37 @@ func (environmentsActions) Name() string {
return "environment"
}

func (environment environmentsActions) Apply(ctx context.Context, fileContent file.File) (*file.File, error) {
func (environment environmentsActions) GetID(file *file.File) (string, error) {
resource, err := environment.formatter.ToStruct(file)
if err != nil {
return "", err
}

return *resource.(openapi.EnvironmentResource).Spec.Id, nil
}

func (environment environmentsActions) Apply(ctx context.Context, fileContent file.File) (result *file.File, err error) {
envResource := openapi.EnvironmentResource{
Spec: &openapi.Environment{},
}

mapstructure.Decode(fileContent.Definition().Spec, &envResource.Spec)

if envResource.Spec.Id == nil || *envResource.Spec.Id == "" {
return environment.resourceClient.Create(ctx, fileContent)
result, err := environment.resourceClient.Create(ctx, fileContent)
return result, err
}

return environment.resourceClient.Update(ctx, fileContent, *envResource.Spec.Id)
result, err = environment.resourceClient.Update(ctx, fileContent, *envResource.Spec.Id)
return result, err
}

func (environment environmentsActions) List(ctx context.Context, listArgs utils.ListArgs) (*file.File, error) {
return environment.resourceClient.List(ctx, listArgs)
}

func (environment environmentsActions) Delete(ctx context.Context, ID string) error {
return environment.resourceClient.Delete(ctx, ID)
func (environment environmentsActions) Delete(ctx context.Context, ID string) (string, error) {
return "Environment successfully deleted", environment.resourceClient.Delete(ctx, ID)
}

func (environment environmentsActions) Get(ctx context.Context, ID string) (*file.File, error) {
Expand Down
18 changes: 14 additions & 4 deletions cli/actions/polling.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,29 @@ func (pollingActions) Name() string {
return "pollingprofile"
}

func (polling pollingActions) Apply(ctx context.Context, fileContent file.File) (*file.File, error) {
func (polling pollingActions) GetID(file *file.File) (string, error) {
resource, err := polling.formatter.ToStruct(file)
if err != nil {
return "", err
}

return resource.(openapi.PollingProfile).Spec.Id, nil
}

func (polling pollingActions) Apply(ctx context.Context, fileContent file.File) (result *file.File, err error) {
var pollingProfile openapi.PollingProfile
mapstructure.Decode(fileContent.Definition().Spec, &pollingProfile.Spec)

return polling.resourceClient.Update(ctx, fileContent, currentConfigID)
result, err = polling.resourceClient.Update(ctx, fileContent, currentConfigID)
return result, err
}

func (polling pollingActions) List(ctx context.Context, listArgs utils.ListArgs) (*file.File, error) {
return nil, ErrNotSupportedResourceAction
}

func (polling pollingActions) Delete(ctx context.Context, ID string) error {
return ErrNotSupportedResourceAction
func (polling pollingActions) Delete(ctx context.Context, ID string) (string, error) {
return "PollingProfile successfully reset to default", ErrNotSupportedResourceAction
}

func (polling pollingActions) Get(ctx context.Context, ID string) (*file.File, error) {
Expand Down
12 changes: 12 additions & 0 deletions cli/actions/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"

"github.com/kubeshop/tracetest/cli/config"
"github.com/kubeshop/tracetest/cli/formatters"
"github.com/kubeshop/tracetest/cli/utils"
"go.uber.org/zap"
)
Expand All @@ -16,12 +17,17 @@ type resourceArgs struct {
logger *zap.Logger
resourceClient utils.ResourceClient
config config.Config
formatter formatters.ResourceFormatter
}

func (r resourceArgs) Logger() *zap.Logger {
return r.logger
}

func (r resourceArgs) Formatter() formatters.ResourceFormatter {
return r.formatter
}

type ResourceArgsOption = func(args *resourceArgs)
type ResourceRegistry map[string]resourceActions

Expand Down Expand Up @@ -66,6 +72,12 @@ func WithConfig(config config.Config) ResourceArgsOption {
}
}

func WithFormatter(formatter formatters.ResourceFormatter) ResourceArgsOption {
return func(args *resourceArgs) {
args.formatter = formatter
}
}

func NewResourceArgs(options ...ResourceArgsOption) resourceArgs {
args := resourceArgs{}

Expand Down
Loading

0 comments on commit 6b56a45

Please sign in to comment.