-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cloud): adding support for cloud resources (#3101)
* feature(cli): adding CLI cloud login and auth logic * feature(cli): adding CLI cloud login and auth logic * feat(cloud): adding support for cloud resources * feat(cloud): adding select command for cloud resources
- Loading branch information
Showing
8 changed files
with
157 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
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(config config.Config, ID string) config.Config | ||
|
||
var ( | ||
selectParams = &resourceIDParameters{} | ||
selectCmd *cobra.Command | ||
selectable = strings.Join([]string{"environment", "organization"}, "|") | ||
selectableMap = map[string]selectableFn{ | ||
"environment": func(config config.Config, ID string) config.Config { | ||
config.EnvironmentID = ID | ||
return config | ||
}, | ||
"organization": func(config config.Config, ID string) config.Config { | ||
config.OrganizationID = ID | ||
return config | ||
}} | ||
) | ||
|
||
func init() { | ||
selectCmd = &cobra.Command{ | ||
GroupID: cmdGroupCloud.ID, | ||
Use: "select " + selectable, | ||
Short: "select resources", | ||
Long: "Select resources to your Tracetest CLI config", | ||
PreRun: setupCommand(), | ||
Run: WithResourceMiddleware(func(_ *cobra.Command, args []string) (string, error) { | ||
resourceType := resourceParams.ResourceName | ||
ctx := context.Background() | ||
|
||
selectableFn, ok := selectableMap[resourceType] | ||
if !ok { | ||
return "", fmt.Errorf("resource type %s not selectable. Selectable resources are %s", resourceType, selectable) | ||
} | ||
|
||
resourceClient, err := resources.Get(resourceType) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
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 | ||
} | ||
|
||
cliConfig = selectableFn(cliConfig, selectParams.ResourceID) | ||
err = config.Save(ctx, cliConfig, config.ConfigureConfig{}) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return fmt.Sprintf("✔ Resource %s of type %s has been stored to your configuration file", selectParams.ResourceID, resourceType), nil | ||
}, selectParams), | ||
PostRun: teardownCommand, | ||
} | ||
|
||
selectCmd.Flags().StringVar(&selectParams.ResourceID, "id", "", "id of the resource to select") | ||
rootCmd.AddCommand(selectCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters