Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions cmd/admin/v2/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ func newFilesystemCmd(c *config.Config) *cobra.Command {
cmdsConfig := &genericcli.CmdsConfig[*adminv2.FilesystemServiceCreateRequest, *adminv2.FilesystemServiceUpdateRequest, *apiv2.FilesystemLayout]{
BinaryName: config.BinaryName,
GenericCLI: genericcli.NewGenericCLI(w).WithFS(c.Fs),
Singular: "filesystem",
Plural: "filesystems",
Singular: "filesystemlayout",
Plural: "filesystemlayouts",
Aliases: []string{"fsl"},
Description: "manage filesystem layouts which define how disks, raid arrays and volumes are set up on machines during installation",
Sorter: sorters.FilesystemSorter(),
DescribePrinter: func() printers.Printer { return c.DescribePrinter },
Expand Down
7 changes: 4 additions & 3 deletions cmd/admin/v2/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ func (c *networkCmd) Create(rq *adminv2.NetworkServiceCreateRequest) (*apiv2.Net
if errorutil.IsConflict(err) {
return nil, genericcli.AlreadyExistsError()
}

return nil, err
}

Expand All @@ -223,13 +224,13 @@ func (c *networkCmd) Update(rq *adminv2.NetworkServiceUpdateRequest) (*apiv2.Net
return resp.Network, nil
}

func (c *networkCmd) Convert(r *apiv2.Network) (string, *adminv2.NetworkServiceCreateRequest, *adminv2.NetworkServiceUpdateRequest, error) {
addressFamily, err := helpers.AddressFamilyFromPrefixes(r.Prefixes...)
func (c *networkCmd) Convert(n *apiv2.Network) (string, *adminv2.NetworkServiceCreateRequest, *adminv2.NetworkServiceUpdateRequest, error) {
addressFamily, err := helpers.AddressFamilyFromPrefixes(n)
if err != nil {
return "", nil, nil, err
}

return r.Id, networkResponseToCreate(r, addressFamily), networkResponseToUpdate(r), nil
return n.Id, networkResponseToCreate(n, addressFamily), networkResponseToUpdate(n), nil
}

func networkResponseToCreate(r *apiv2.Network, addressFamily *apiv2.NetworkAddressFamily) *adminv2.NetworkServiceCreateRequest {
Expand Down
15 changes: 6 additions & 9 deletions cmd/admin/v2/partition.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"sort"
"strings"

"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 @@ -48,14 +49,6 @@ func newPartitionCmd(c *config.Config) *cobra.Command {
ListPrinter: func() printers.Printer { return c.ListPrinter },
ValidArgsFn: c.Completion.Partition,
Sorter: sorters.PartitionSorter(),
OnlyCmds: genericcli.OnlyCmds(
genericcli.DescribeCmd,
genericcli.ListCmd,
genericcli.CreateCmd,
genericcli.UpdateCmd,
genericcli.DeleteCmd,
genericcli.EditCmd,
),
CreateCmdMutateFn: func(cmd *cobra.Command) {
cmd.Flags().String("id", "", "the id of the partition to create")
addMutableFlags(cmd)
Expand Down Expand Up @@ -173,7 +166,11 @@ func (c *partition) Create(rq *adminv2.PartitionServiceCreateRequest) (*apiv2.Pa

resp, err := c.c.Client.Adminv2().Partition().Create(ctx, rq)
if err != nil {
return nil, fmt.Errorf("failed to create partition: %w", err)
if errorutil.IsConflict(err) {
return nil, genericcli.AlreadyExistsError()
}

return nil, err
}

return resp.Partition, nil
Expand Down
75 changes: 67 additions & 8 deletions cmd/admin/v2/project.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 All @@ -24,7 +25,7 @@ func newProjectCmd(c *config.Config) *cobra.Command {
c: c,
}

cmdsConfig := &genericcli.CmdsConfig[*apiv2.ProjectServiceCreateRequest, *apiv2.ProjectServiceUpdateRequest, *apiv2.Project]{
cmdsConfig := &genericcli.CmdsConfig[*adminv2.ProjectServiceCreateRequest, *apiv2.ProjectServiceUpdateRequest, *apiv2.Project]{
BinaryName: config.BinaryName,
GenericCLI: genericcli.NewGenericCLI(w).WithFS(c.Fs),
Singular: "project",
Expand All @@ -43,7 +44,19 @@ func newProjectCmd(c *config.Config) *cobra.Command {
}

func (c *project) Get(id string) (*apiv2.Project, error) {
panic("unimplemented")
ctx, cancel := c.c.NewRequestContext()
defer cancel()

req := &apiv2.ProjectServiceGetRequest{
Project: id,
}

resp, err := c.c.Client.Apiv2().Project().Get(ctx, req)
if err != nil {
return nil, fmt.Errorf("failed to get project: %w", err)
}

return resp.GetProject(), nil
}

func (c *project) List() ([]*apiv2.Project, error) {
Expand Down Expand Up @@ -73,18 +86,64 @@ func (c *project) List() ([]*apiv2.Project, error) {
return resp.GetProjects(), nil
}

func (c *project) Create(rq *apiv2.ProjectServiceCreateRequest) (*apiv2.Project, error) {
panic("unimplemented")
func (c *project) Create(rq *adminv2.ProjectServiceCreateRequest) (*apiv2.Project, error) {
ctx, cancel := c.c.NewRequestContext()
defer cancel()

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

return nil, err
}

return resp.Project, nil
}

func (c *project) Delete(id string) (*apiv2.Project, error) {
panic("unimplemented")
ctx, cancel := c.c.NewRequestContext()
defer cancel()

resp, err := c.c.Client.Apiv2().Project().Delete(ctx, &apiv2.ProjectServiceDeleteRequest{
Project: id,
})
if err != nil {
return nil, fmt.Errorf("failed to delete project: %w", err)
}

return resp.Project, nil
}

func (c *project) Convert(r *apiv2.Project) (string, *apiv2.ProjectServiceCreateRequest, *apiv2.ProjectServiceUpdateRequest, error) {
panic("unimplemented")
func (c *project) Convert(r *apiv2.Project) (string, *adminv2.ProjectServiceCreateRequest, *apiv2.ProjectServiceUpdateRequest, error) {
return r.Uuid, &adminv2.ProjectServiceCreateRequest{
Id: pointer.PointerOrNil(r.Uuid),
Project: &apiv2.ProjectServiceCreateRequest{
Login: r.Tenant,
Name: r.Name,
Description: r.Description,
AvatarUrl: r.AvatarUrl,
Labels: pointer.SafeDeref(r.Meta).Labels,
},
}, &apiv2.ProjectServiceUpdateRequest{
Project: r.Uuid,
Name: pointer.PointerOrNil(r.Name),
Description: pointer.PointerOrNil(r.Description),
AvatarUrl: r.AvatarUrl,
UpdateMeta: helpers.UpdateMetaFromMeta(r.Meta),
Labels: helpers.UpdateLabelsFromMeta(r.Meta),
}, nil
}

func (c *project) Update(rq *apiv2.ProjectServiceUpdateRequest) (*apiv2.Project, error) {
panic("unimplemented")
ctx, cancel := c.c.NewRequestContext()
defer cancel()

resp, err := c.c.Client.Apiv2().Project().Update(ctx, rq)
if err != nil {
return nil, fmt.Errorf("failed to update project: %w", err)
}

return resp.Project, nil
}
116 changes: 61 additions & 55 deletions cmd/admin/v2/tenant.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func newTenantCmd(c *config.Config) *cobra.Command {
c: c,
}

cmdsConfig := &genericcli.CmdsConfig[*adminv2.TenantServiceCreateRequest, any, *apiv2.Tenant]{
cmdsConfig := &genericcli.CmdsConfig[*adminv2.TenantServiceCreateRequest, *apiv2.TenantServiceUpdateRequest, *apiv2.Tenant]{
BinaryName: config.BinaryName,
GenericCLI: genericcli.NewGenericCLI(w).WithFS(c.Fs),
Singular: "tenant",
Expand All @@ -41,28 +41,43 @@ func newTenantCmd(c *config.Config) *cobra.Command {
cmd.Flags().StringSlice("labels", nil, "lists only tenant with the given labels")
},
CreateCmdMutateFn: func(cmd *cobra.Command) {
cmd.Flags().String("login", "", "the login id of the tenant to create")
cmd.Flags().String("name", "", "the name of the tenant to create")
cmd.Flags().String("description", "", "the description of the tenant to create")
cmd.Flags().String("email", "", "the email of the tenant to create")
cmd.Flags().String("avatar-url", "", "the avatar url of the tenant to create")
},
CreateRequestFromCLI: func() (*adminv2.TenantServiceCreateRequest, error) {
return &adminv2.TenantServiceCreateRequest{
Name: viper.GetString("name"),
Description: pointer.PointerOrNil(viper.GetString("description")),
Email: pointer.PointerOrNil(viper.GetString("email")),
AvatarUrl: pointer.PointerOrNil(viper.GetString("avatar-url")),
Login: pointer.PointerOrNil(viper.GetString("login")),
Tenant: &apiv2.TenantServiceCreateRequest{
Name: viper.GetString("name"),
Description: pointer.PointerOrNil(viper.GetString("description")),
Email: pointer.PointerOrNil(viper.GetString("email")),
AvatarUrl: pointer.PointerOrNil(viper.GetString("avatar-url")),
},
}, nil
},
OnlyCmds: genericcli.OnlyCmds(genericcli.ListCmd, genericcli.CreateCmd),
ValidArgsFn: w.c.Completion.AdminTenant,
}

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

func (c *tenant) Get(id string) (*apiv2.Tenant, error) {
panic("unimplemented")
ctx, cancel := c.c.NewRequestContext()
defer cancel()

req := &apiv2.TenantServiceGetRequest{
Login: id,
}

resp, err := c.c.Client.Apiv2().Tenant().Get(ctx, req)
if err != nil {
return nil, fmt.Errorf("failed to get tenant: %w", err)
}

return resp.GetTenant(), nil
}

func (c *tenant) List() ([]*apiv2.Tenant, error) {
Expand Down Expand Up @@ -110,59 +125,50 @@ func (c *tenant) Create(rq *adminv2.TenantServiceCreateRequest) (*apiv2.Tenant,
}

func (c *tenant) Delete(id string) (*apiv2.Tenant, error) {
panic("unimplemented")
}
ctx, cancel := c.c.NewRequestContext()
defer cancel()

func (c *tenant) Convert(r *apiv2.Tenant) (string, *adminv2.TenantServiceCreateRequest, any, error) {
panic("unimplemented")
}
resp, err := c.c.Client.Apiv2().Tenant().Delete(ctx, &apiv2.TenantServiceDeleteRequest{
Login: id,
})
if err != nil {
return nil, fmt.Errorf("failed to delete tenant: %w", err)
}

func (c *tenant) Update(rq any) (*apiv2.Tenant, error) {
panic("unimplemented")
return resp.Tenant, nil
}

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
func (c *tenant) Convert(r *apiv2.Tenant) (string, *adminv2.TenantServiceCreateRequest, *apiv2.TenantServiceUpdateRequest, error) {
return r.Login, &adminv2.TenantServiceCreateRequest{
Login: pointer.PointerOrNil(r.Login),
Tenant: &apiv2.TenantServiceCreateRequest{
Name: r.Name,
Description: pointer.PointerOrNil(r.Description),
Email: pointer.PointerOrNil(r.Email),
AvatarUrl: pointer.PointerOrNil(r.AvatarUrl),
Labels: pointer.SafeDeref(r.Meta).Labels,
},
},
}
&apiv2.TenantServiceUpdateRequest{
Login: r.Login,
Name: pointer.PointerOrNil(r.Name),
Email: pointer.PointerOrNil(r.Email),
Description: pointer.PointerOrNil(r.Description),
AvatarUrl: pointer.PointerOrNil(r.AvatarUrl),
UpdateMeta: helpers.UpdateMetaFromMeta(r.Meta),
Labels: helpers.UpdateLabelsFromMeta(r.Meta),
},
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"))
func (c *tenant) Update(rq *apiv2.TenantServiceUpdateRequest) (*apiv2.Tenant, error) {
ctx, cancel := c.c.NewRequestContext()
defer cancel()

genericcli.Must(cmd.RegisterFlagCompletionFunc("tenant-id", c.Completion.AdminTenant))
genericcli.Must(cmd.RegisterFlagCompletionFunc("member-id", c.Completion.AdminTenant))
genericcli.Must(cmd.RegisterFlagCompletionFunc("role", c.Completion.TenantRole))
resp, err := c.c.Client.Apiv2().Tenant().Update(ctx, rq)
if err != nil {
return nil, fmt.Errorf("failed to update tenant: %w", err)
}

return cmd
return resp.Tenant, nil
}
5 changes: 3 additions & 2 deletions cmd/api/v2/filesystem.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ func newFilesystemCmd(c *config.Config) *cobra.Command {
cmdsConfig := &genericcli.CmdsConfig[any, any, *apiv2.FilesystemLayout]{
BinaryName: config.BinaryName,
GenericCLI: genericcli.NewGenericCLI(w).WithFS(c.Fs),
Singular: "filesystem",
Plural: "filesystems",
Singular: "filesystemlayout",
Plural: "filesystemlayouts",
Aliases: []string{"fsl"},
Description: "a filesystem layout defines how disks, raid arrays and volumes are set up on a machine during installation",
Sorter: sorters.FilesystemSorter(),
OnlyCmds: genericcli.OnlyCmds(genericcli.ListCmd, genericcli.DescribeCmd),
Expand Down
6 changes: 3 additions & 3 deletions cmd/api/v2/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,13 +218,13 @@ func (c *networkCmd) Update(rq *apiv2.NetworkServiceUpdateRequest) (*apiv2.Netwo
return resp.Network, nil
}

func (c *networkCmd) Convert(r *apiv2.Network) (string, *apiv2.NetworkServiceCreateRequest, *apiv2.NetworkServiceUpdateRequest, error) {
addressFamily, err := helpers.AddressFamilyFromPrefixes(r.Prefixes...)
func (c *networkCmd) Convert(n *apiv2.Network) (string, *apiv2.NetworkServiceCreateRequest, *apiv2.NetworkServiceUpdateRequest, error) {
addressFamily, err := helpers.AddressFamilyFromPrefixes(n)
if err != nil {
return "", nil, nil, err
}

return helpers.EncodeProject(r.Id, pointer.SafeDeref(r.Project)), networkResponseToCreate(r, addressFamily), networkResponseToUpdate(r), nil
return helpers.EncodeProject(n.Id, pointer.SafeDeref(n.Project)), networkResponseToCreate(n, addressFamily), networkResponseToUpdate(n), nil
}

func networkResponseToCreate(r *apiv2.Network, addressFamily *apiv2.NetworkAddressFamily) *apiv2.NetworkServiceCreateRequest {
Expand Down
Loading
Loading