Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions cmd/admin/v2/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strings"
"time"

"github.com/metal-stack/api/go/errorutil"
adminv2 "github.com/metal-stack/api/go/metalstack/admin/v2"
apiv2 "github.com/metal-stack/api/go/metalstack/api/v2"
"github.com/metal-stack/cli/cmd/config"
Expand Down Expand Up @@ -86,6 +87,10 @@ func (c *image) Create(rq *adminv2.ImageServiceCreateRequest) (*apiv2.Image, err

resp, err := c.c.Client.Adminv2().Image().Create(ctx, rq)
if err != nil {
if errorutil.IsConflict(err) {
return nil, genericcli.AlreadyExistsError()
}

return nil, fmt.Errorf("failed to create image: %w", err)
}

Expand Down
19 changes: 10 additions & 9 deletions cmd/admin/v2/size.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package v2
import (
"fmt"

"github.com/metal-stack/api/go/errorutil"
adminv2 "github.com/metal-stack/api/go/metalstack/admin/v2"
apiv2 "github.com/metal-stack/api/go/metalstack/api/v2"
"github.com/metal-stack/cli/cmd/config"
"github.com/metal-stack/cli/cmd/sorters"
"github.com/metal-stack/cli/pkg/helpers"
"github.com/metal-stack/metal-lib/pkg/genericcli"
"github.com/metal-stack/metal-lib/pkg/genericcli/printers"
"github.com/metal-stack/metal-lib/pkg/pointer"
Expand Down Expand Up @@ -43,10 +45,6 @@ func newSizeCmd(c *config.Config) *cobra.Command {
cmd.Flags().StringP("name", "", "", "size name to update")
cmd.Flags().StringP("description", "", "", "size description to update")
},
CreateCmdMutateFn: func(cmd *cobra.Command) {

},
// TODO: create from CLI might be nice to have?
}

return genericcli.NewCmds(cmdsConfig)
Expand All @@ -72,6 +70,10 @@ func (c *size) Create(rq *adminv2.SizeServiceCreateRequest) (*apiv2.Size, error)

resp, err := c.c.Client.Adminv2().Size().Create(ctx, rq)
if err != nil {
if errorutil.IsConflict(err) {
return nil, genericcli.AlreadyExistsError()
}

return nil, fmt.Errorf("failed to create size: %w", err)
}

Expand Down Expand Up @@ -121,7 +123,9 @@ func (c *size) Update(rq *adminv2.SizeServiceUpdateRequest) (*apiv2.Size, error)
}

func (c *size) Convert(r *apiv2.Size) (string, *adminv2.SizeServiceCreateRequest, *adminv2.SizeServiceUpdateRequest, error) {
return r.Id, &adminv2.SizeServiceCreateRequest{

return r.Id,
&adminv2.SizeServiceCreateRequest{
Size: &apiv2.Size{
Id: r.Id,
Name: r.Name,
Expand All @@ -134,10 +138,7 @@ func (c *size) Convert(r *apiv2.Size) (string, *adminv2.SizeServiceCreateRequest
Name: r.Name,
Description: r.Description,
Constraints: r.Constraints,
UpdateMeta: &apiv2.UpdateMeta{
LockingStrategy: apiv2.OptimisticLockingStrategy_OPTIMISTIC_LOCKING_STRATEGY_CLIENT,
UpdatedAt: r.Meta.UpdatedAt,
},
UpdateMeta: helpers.UpdateMetaFromMeta(r.Meta),
Labels: &apiv2.UpdateLabels{
Strategy: &apiv2.UpdateLabels_Replace{
Replace: &apiv2.Labels{
Expand Down
53 changes: 52 additions & 1 deletion cmd/admin/v2/tenant.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package v2
import (
"fmt"

"github.com/metal-stack/api/go/errorutil"
adminv2 "github.com/metal-stack/api/go/metalstack/admin/v2"
apiv2 "github.com/metal-stack/api/go/metalstack/api/v2"
"github.com/metal-stack/cli/cmd/config"
Expand Down Expand Up @@ -55,7 +56,7 @@ func newTenantCmd(c *config.Config) *cobra.Command {
ValidArgsFn: w.c.Completion.AdminTenantListCompletion,
}

return genericcli.NewCmds(cmdsConfig)
return genericcli.NewCmds(cmdsConfig, newAddMemberCmd(c))
}

func (c *tenant) Get(id string) (*apiv2.Tenant, error) {
Expand Down Expand Up @@ -87,6 +88,10 @@ func (c *tenant) Create(rq *adminv2.TenantServiceCreateRequest) (*apiv2.Tenant,

resp, err := c.c.Client.Adminv2().Tenant().Create(ctx, rq)
if err != nil {
if errorutil.IsConflict(err) {
return nil, genericcli.AlreadyExistsError()
}

return nil, fmt.Errorf("failed to create tenant: %w", err)
}

Expand All @@ -104,3 +109,49 @@ func (c *tenant) Convert(r *apiv2.Tenant) (string, *adminv2.TenantServiceCreateR
func (c *tenant) Update(rq any) (*apiv2.Tenant, error) {
panic("unimplemented")
}

func newAddMemberCmd(c *config.Config) *cobra.Command {
cmd := &cobra.Command{
Use: "add-member",
Short: "Add a new member to a tenant",
Long: `Add a new member to an existing tenant by specifying the tenant ID, member's ID, and role.`,
RunE: func(cmd *cobra.Command, args []string) error {
ctx, cancel := c.NewRequestContext()
defer cancel()

var (
tenantId = viper.GetString("tenant-id")
memberId = viper.GetString("member-id")
memberRole = viper.GetString("role")
)

if tenantId == "" || memberId == "" || memberRole == "" {
return fmt.Errorf("tenant ID, member ID, and role must all be specified")
}

_, err := c.Client.Adminv2().Tenant().AddMember(ctx, &adminv2.TenantServiceAddMemberRequest{
Role: apiv2.TenantRole(apiv2.TenantRole_value[memberRole]),
Tenant: tenantId,
Member: memberId,
})
if err != nil {
return fmt.Errorf("failed to add member to tenant: %w", err)
}

return nil
},
}

cmd.Flags().String("tenant-id", "", "ID of the tenant where the member is added")
cmd.Flags().String("member-id", "", "ID of the member to be added")
cmd.Flags().String("role", "", "Role of the member within the tenant")
genericcli.Must(cmd.MarkFlagRequired("tenant-id"))
genericcli.Must(cmd.MarkFlagRequired("member-id"))
genericcli.Must(cmd.MarkFlagRequired("role"))

genericcli.Must(cmd.RegisterFlagCompletionFunc("tenant-id", c.Completion.AdminTenantListCompletion))
genericcli.Must(cmd.RegisterFlagCompletionFunc("member-id", c.Completion.AdminTenantListCompletion))
genericcli.Must(cmd.RegisterFlagCompletionFunc("role", c.Completion.TenantRoleCompletion))

return cmd
}
3 changes: 2 additions & 1 deletion cmd/api/v2/ip.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package v2

import (
"github.com/metal-stack/api/go/errorutil"
apiv2 "github.com/metal-stack/api/go/metalstack/api/v2"
"github.com/metal-stack/cli/cmd/config"
"github.com/metal-stack/cli/cmd/sorters"
Expand Down Expand Up @@ -151,7 +152,7 @@ func (c *ip) Create(rq *apiv2.IPServiceCreateRequest) (*apiv2.IP, error) {

resp, err := c.c.Client.Apiv2().IP().Create(ctx, rq)
if err != nil {
if helpers.IsAlreadyExists(err) {
if errorutil.IsConflict(err) {
return nil, genericcli.AlreadyExistsError()
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/api/v2/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (

"github.com/dustin/go-humanize"
"github.com/fatih/color"
"github.com/metal-stack/api/go/errorutil"
apiv2 "github.com/metal-stack/api/go/metalstack/api/v2"
"github.com/metal-stack/cli/cmd/config"
"github.com/metal-stack/cli/cmd/sorters"
"github.com/metal-stack/cli/pkg/helpers"
"github.com/metal-stack/metal-lib/pkg/genericcli"
"github.com/metal-stack/metal-lib/pkg/genericcli/printers"
"github.com/metal-stack/metal-lib/pkg/pointer"
Expand Down Expand Up @@ -206,7 +206,7 @@ func (c *project) Create(rq *apiv2.ProjectServiceCreateRequest) (*apiv2.Project,

resp, err := c.c.Client.Apiv2().Project().Create(ctx, rq)
if err != nil {
if helpers.IsAlreadyExists(err) {
if errorutil.IsConflict(err) {
return nil, genericcli.AlreadyExistsError()
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/api/v2/tenant.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (

"github.com/dustin/go-humanize"
"github.com/fatih/color"
"github.com/metal-stack/api/go/errorutil"
apiv2 "github.com/metal-stack/api/go/metalstack/api/v2"
"github.com/metal-stack/cli/cmd/config"
"github.com/metal-stack/cli/cmd/sorters"
"github.com/metal-stack/cli/pkg/helpers"
"github.com/metal-stack/metal-lib/pkg/genericcli"
"github.com/metal-stack/metal-lib/pkg/genericcli/printers"
"github.com/metal-stack/metal-lib/pkg/pointer"
Expand Down Expand Up @@ -204,7 +204,7 @@ func (c *tenant) Create(rq *apiv2.TenantServiceCreateRequest) (*apiv2.Tenant, er

resp, err := c.c.Client.Apiv2().Tenant().Create(ctx, rq)
if err != nil {
if helpers.IsAlreadyExists(err) {
if errorutil.IsConflict(err) {
return nil, genericcli.AlreadyExistsError()
}

Expand Down
6 changes: 3 additions & 3 deletions cmd/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,14 @@ func (c *Config) GetTenant() (string, error) {
}

func (c *Config) GetToken() string {
if viper.IsSet("api-token") {
if viper.GetString("api-token") != "" {
return viper.GetString("api-token")
}
return c.Context.Token
}

func (c *Config) GetApiURL() string {
if viper.IsSet("api-url") {
if viper.GetString("api-url") != "" {
return viper.GetString("api-url")
}
if c.Context.ApiURL != nil {
Expand All @@ -120,7 +120,7 @@ func (c *Config) GetApiURL() string {
}

func (c *Config) GetProvider() string {
if viper.IsSet("provider") {
if viper.GetString("provider") != "" {
return viper.GetString("provider")
}
return c.Context.Provider
Expand Down
8 changes: 2 additions & 6 deletions cmd/printers.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,9 @@ func newPrinterFromCLI(out io.Writer) (printers.Printer, error) {

switch format := viper.GetString("output-format"); format {
case "yaml":
printer = printers.NewProtoYAMLPrinter().WithFallback(true).WithOut(out)
printer = printers.NewProtoYAMLPrinter().WithFallback(false).WithOut(out)
case "json":
printer = printers.NewProtoJSONPrinter().WithFallback(true).WithOut(out)
case "yamlraw":
printer = printers.NewYAMLPrinter().WithOut(out)
case "jsonraw":
printer = printers.NewJSONPrinter().WithOut(out)
printer = printers.NewProtoJSONPrinter().WithFallback(false).WithOut(out)
case "table", "wide", "markdown":
tp := tableprinters.New()
cfg := &printers.TablePrinterConfig{
Expand Down
3 changes: 2 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func NewRootCmd(c *config.Config) *cobra.Command {
},
}
rootCmd.PersistentFlags().StringP("config", "c", "", "alternative config file path, (default is ~/.metal-stack/config.yaml)")
rootCmd.PersistentFlags().StringP("output-format", "o", "table", "output format (table|wide|markdown|json|yaml|template|jsonraw|yamlraw), wide is a table with more columns, jsonraw and yamlraw do not translate proto enums into string types but leave the original int32 values intact (for apply, create, update, delete commands from file the raw output formatters must be used).")
rootCmd.PersistentFlags().StringP("output-format", "o", "table", "output format (table|wide|markdown|json|yaml|template), wide is a table with more columns.")

genericcli.Must(rootCmd.RegisterFlagCompletionFunc("output-format", cobra.FixedCompletions([]string{"table", "wide", "markdown", "json", "yaml", "template"}, cobra.ShellCompDirectiveNoFileComp)))

Expand Down Expand Up @@ -127,6 +127,7 @@ func newApiClient(apiURL, token string) (client.Client, error) {
if viper.GetBool("debug") {
logLevel = slog.LevelDebug
}

dialConfig := &client.DialConfig{
BaseURL: apiURL,
Token: token,
Expand Down
2 changes: 1 addition & 1 deletion docs/metalctlv2.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ cli for managing entities in metal-stack
--debug debug output
--force-color force colored output even without tty
-h, --help help for metalctlv2
-o, --output-format string output format (table|wide|markdown|json|yaml|template|jsonraw|yamlraw), wide is a table with more columns, jsonraw and yamlraw do not translate proto enums into string types but leave the original int32 values intact (for apply, create, update, delete commands from file the raw output formatters must be used). (default "table")
-o, --output-format string output format (table|wide|markdown|json|yaml|template), wide is a table with more columns. (default "table")
--template string output template for template output-format, go template format. For property names inspect the output of -o json or -o yaml for reference.
--timeout duration request timeout used for api requests
```
Expand Down
2 changes: 1 addition & 1 deletion docs/metalctlv2_api-methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ metalctlv2 api-methods [flags]
-c, --config string alternative config file path, (default is ~/.metal-stack/config.yaml)
--debug debug output
--force-color force colored output even without tty
-o, --output-format string output format (table|wide|markdown|json|yaml|template|jsonraw|yamlraw), wide is a table with more columns, jsonraw and yamlraw do not translate proto enums into string types but leave the original int32 values intact (for apply, create, update, delete commands from file the raw output formatters must be used). (default "table")
-o, --output-format string output format (table|wide|markdown|json|yaml|template), wide is a table with more columns. (default "table")
--template string output template for template output-format, go template format. For property names inspect the output of -o json or -o yaml for reference.
--timeout duration request timeout used for api requests
```
Expand Down
2 changes: 1 addition & 1 deletion docs/metalctlv2_audit.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ read api audit traces of a tenant
-c, --config string alternative config file path, (default is ~/.metal-stack/config.yaml)
--debug debug output
--force-color force colored output even without tty
-o, --output-format string output format (table|wide|markdown|json|yaml|template|jsonraw|yamlraw), wide is a table with more columns, jsonraw and yamlraw do not translate proto enums into string types but leave the original int32 values intact (for apply, create, update, delete commands from file the raw output formatters must be used). (default "table")
-o, --output-format string output format (table|wide|markdown|json|yaml|template), wide is a table with more columns. (default "table")
--template string output template for template output-format, go template format. For property names inspect the output of -o json or -o yaml for reference.
--timeout duration request timeout used for api requests
```
Expand Down
2 changes: 1 addition & 1 deletion docs/metalctlv2_audit_describe.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ metalctlv2 audit describe <id> [flags]
-c, --config string alternative config file path, (default is ~/.metal-stack/config.yaml)
--debug debug output
--force-color force colored output even without tty
-o, --output-format string output format (table|wide|markdown|json|yaml|template|jsonraw|yamlraw), wide is a table with more columns, jsonraw and yamlraw do not translate proto enums into string types but leave the original int32 values intact (for apply, create, update, delete commands from file the raw output formatters must be used). (default "table")
-o, --output-format string output format (table|wide|markdown|json|yaml|template), wide is a table with more columns. (default "table")
--template string output template for template output-format, go template format. For property names inspect the output of -o json or -o yaml for reference.
--timeout duration request timeout used for api requests
```
Expand Down
2 changes: 1 addition & 1 deletion docs/metalctlv2_audit_list.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ metalctlv2 audit list [flags]
-c, --config string alternative config file path, (default is ~/.metal-stack/config.yaml)
--debug debug output
--force-color force colored output even without tty
-o, --output-format string output format (table|wide|markdown|json|yaml|template|jsonraw|yamlraw), wide is a table with more columns, jsonraw and yamlraw do not translate proto enums into string types but leave the original int32 values intact (for apply, create, update, delete commands from file the raw output formatters must be used). (default "table")
-o, --output-format string output format (table|wide|markdown|json|yaml|template), wide is a table with more columns. (default "table")
--template string output template for template output-format, go template format. For property names inspect the output of -o json or -o yaml for reference.
--timeout duration request timeout used for api requests
```
Expand Down
2 changes: 1 addition & 1 deletion docs/metalctlv2_completion.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ See each sub-command's help for details on how to use the generated script.
-c, --config string alternative config file path, (default is ~/.metal-stack/config.yaml)
--debug debug output
--force-color force colored output even without tty
-o, --output-format string output format (table|wide|markdown|json|yaml|template|jsonraw|yamlraw), wide is a table with more columns, jsonraw and yamlraw do not translate proto enums into string types but leave the original int32 values intact (for apply, create, update, delete commands from file the raw output formatters must be used). (default "table")
-o, --output-format string output format (table|wide|markdown|json|yaml|template), wide is a table with more columns. (default "table")
--template string output template for template output-format, go template format. For property names inspect the output of -o json or -o yaml for reference.
--timeout duration request timeout used for api requests
```
Expand Down
2 changes: 1 addition & 1 deletion docs/metalctlv2_completion_bash.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ metalctlv2 completion bash
-c, --config string alternative config file path, (default is ~/.metal-stack/config.yaml)
--debug debug output
--force-color force colored output even without tty
-o, --output-format string output format (table|wide|markdown|json|yaml|template|jsonraw|yamlraw), wide is a table with more columns, jsonraw and yamlraw do not translate proto enums into string types but leave the original int32 values intact (for apply, create, update, delete commands from file the raw output formatters must be used). (default "table")
-o, --output-format string output format (table|wide|markdown|json|yaml|template), wide is a table with more columns. (default "table")
--template string output template for template output-format, go template format. For property names inspect the output of -o json or -o yaml for reference.
--timeout duration request timeout used for api requests
```
Expand Down
2 changes: 1 addition & 1 deletion docs/metalctlv2_completion_fish.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ metalctlv2 completion fish [flags]
-c, --config string alternative config file path, (default is ~/.metal-stack/config.yaml)
--debug debug output
--force-color force colored output even without tty
-o, --output-format string output format (table|wide|markdown|json|yaml|template|jsonraw|yamlraw), wide is a table with more columns, jsonraw and yamlraw do not translate proto enums into string types but leave the original int32 values intact (for apply, create, update, delete commands from file the raw output formatters must be used). (default "table")
-o, --output-format string output format (table|wide|markdown|json|yaml|template), wide is a table with more columns. (default "table")
--template string output template for template output-format, go template format. For property names inspect the output of -o json or -o yaml for reference.
--timeout duration request timeout used for api requests
```
Expand Down
2 changes: 1 addition & 1 deletion docs/metalctlv2_completion_powershell.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ metalctlv2 completion powershell [flags]
-c, --config string alternative config file path, (default is ~/.metal-stack/config.yaml)
--debug debug output
--force-color force colored output even without tty
-o, --output-format string output format (table|wide|markdown|json|yaml|template|jsonraw|yamlraw), wide is a table with more columns, jsonraw and yamlraw do not translate proto enums into string types but leave the original int32 values intact (for apply, create, update, delete commands from file the raw output formatters must be used). (default "table")
-o, --output-format string output format (table|wide|markdown|json|yaml|template), wide is a table with more columns. (default "table")
--template string output template for template output-format, go template format. For property names inspect the output of -o json or -o yaml for reference.
--timeout duration request timeout used for api requests
```
Expand Down
2 changes: 1 addition & 1 deletion docs/metalctlv2_completion_zsh.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ metalctlv2 completion zsh [flags]
-c, --config string alternative config file path, (default is ~/.metal-stack/config.yaml)
--debug debug output
--force-color force colored output even without tty
-o, --output-format string output format (table|wide|markdown|json|yaml|template|jsonraw|yamlraw), wide is a table with more columns, jsonraw and yamlraw do not translate proto enums into string types but leave the original int32 values intact (for apply, create, update, delete commands from file the raw output formatters must be used). (default "table")
-o, --output-format string output format (table|wide|markdown|json|yaml|template), wide is a table with more columns. (default "table")
--template string output template for template output-format, go template format. For property names inspect the output of -o json or -o yaml for reference.
--timeout duration request timeout used for api requests
```
Expand Down
Loading
Loading