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

Updated metal-go client for sub-command orgs #284

Merged
merged 1 commit into from
Jul 24, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/equinix/metal-cli
go 1.19

require (
github.com/equinix-labs/metal-go v0.11.0
github.com/equinix-labs/metal-go v0.12.0
github.com/manifoldco/promptui v0.9.0
github.com/olekukonko/tablewriter v0.0.5
github.com/packethost/packngo v0.29.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1m
github.com/envoyproxy/go-control-plane v0.9.7/go.mod h1:cwu0lG7PUMfa9snN8LXBig5ynNVH9qI8YYLbd1fK2po=
github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.mod h1:cXg6YxExXjJnVBQHBLXeUAgxn2UodCpnH306RInaBQk=
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/equinix-labs/metal-go v0.11.0 h1:56gFeGZr4baJ0zRJspYWT+TY6Jjfxag4kQ1S3m6dIo8=
github.com/equinix-labs/metal-go v0.11.0/go.mod h1:SmxCklxW+KjmBLVMdEXgtFO5gD5/b4N0VxcNgUYbOH4=
github.com/equinix-labs/metal-go v0.12.0 h1:zT+Sjham0mDY0y0B26+5i0xVaY707WmshWCzdTLF74Q=
github.com/equinix-labs/metal-go v0.12.0/go.mod h1:SmxCklxW+KjmBLVMdEXgtFO5gD5/b4N0VxcNgUYbOH4=
github.com/frankban/quicktest v1.14.3 h1:FJKSZTDHjyhriyC81FLQ0LY93eSai0ZyR/ZIkd3ZUKE=
github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY=
github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw=
Expand Down
23 changes: 14 additions & 9 deletions internal/organizations/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@
package organizations

import (
"context"
"fmt"

"github.com/packethost/packngo"
metal "github.com/equinix-labs/metal-go/metal/v1"
"github.com/spf13/cobra"
)

Expand All @@ -49,30 +50,34 @@ func (c *Client) Create() *cobra.Command {

RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
req := &packngo.OrganizationCreateRequest{
Name: name,
}

req := metal.NewOrganizationInput()
req.Name = &name

if description != "" {
req.Description = description
req.Description = &description
}

if twitter != "" {
req.Twitter = twitter
req.Twitter = &twitter
}

if website != "" {
req.Website = &website
}

if logo != "" {
req.Logo = logo
req.Logo = &logo
}

org, _, err := c.Service.Create(req)
org, _, err := c.Service.CreateOrganization(context.Background()).OrganizationInput(*req).Include(c.Servicer.Includes(nil)).Exclude(c.Servicer.Excludes(nil)).Execute()
if err != nil {
return fmt.Errorf("Could not create Organization: %w", err)
}

data := make([][]string, 1)

data[0] = []string{org.ID, org.Name, org.Created}
data[0] = []string{org.GetId(), org.GetName(), org.GetCreatedAt().String()}
header := []string{"ID", "Name", "Created"}

return c.Out.Output(org, header, &data)
Expand Down
3 changes: 2 additions & 1 deletion internal/organizations/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package organizations

import (
"context"
"fmt"

"github.com/manifoldco/promptui"
Expand All @@ -33,7 +34,7 @@ func (c *Client) Delete() *cobra.Command {
force bool
)
deleteOrganization := func(id string) error {
_, err := c.Service.Delete(id)
_, err := c.Service.DeleteOrganization(context.Background(), id).Execute()
if err != nil {
return err
}
Expand Down
12 changes: 7 additions & 5 deletions internal/organizations/organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@
package organizations

import (
metal "github.com/equinix-labs/metal-go/metal/v1"
"github.com/equinix/metal-cli/internal/outputs"
"github.com/packethost/packngo"
"github.com/spf13/cobra"
)

type Client struct {
Servicer Servicer
Service packngo.OrganizationService
Service metal.OrganizationsApiService
Out outputs.Outputer
}

Expand All @@ -45,7 +45,7 @@ func (c *Client) NewCommand() *cobra.Command {
root.PersistentPreRun(cmd, args)
}
}
c.Service = c.Servicer.API(cmd).Organizations
c.Service = *c.Servicer.MetalAPI(cmd).OrganizationsApi
},
}

Expand All @@ -60,9 +60,11 @@ func (c *Client) NewCommand() *cobra.Command {
}

type Servicer interface {
API(*cobra.Command) *packngo.Client
ListOptions(defaultIncludes, defaultExcludes []string) *packngo.ListOptions
MetalAPI(*cobra.Command) *metal.APIClient
Format() outputs.Format
Filters() map[string]string
Includes(defaultIncludes []string) (incl []string)
Excludes(defaultExcludes []string) (excl []string)
}

func NewClient(s Servicer, out outputs.Outputer) *Client {
Expand Down
10 changes: 6 additions & 4 deletions internal/organizations/payment.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package organizations

import (
"context"
"fmt"

"github.com/spf13/cobra"
Expand All @@ -39,15 +40,16 @@ func (c *Client) PaymentMethods() *cobra.Command {

RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
paymentMethods, _, err := c.Service.ListPaymentMethods(organizationID)

paymentMethodsList, _, err := c.Service.FindOrganizationPaymentMethods(context.Background(), organizationID).Include(c.Servicer.Includes(nil)).Exclude(c.Servicer.Excludes(nil)).Execute()
if err != nil {
return fmt.Errorf("Could not list Payment Methods: %w", err)
return fmt.Errorf("could not list Payment Methods: %w", err)
}

paymentMethods := paymentMethodsList.GetPaymentMethods()
data := make([][]string, len(paymentMethods))

for i, p := range paymentMethods {
data[i] = []string{p.ID, p.CardholderName, p.ExpMonth, p.ExpYear, p.Created}
data[i] = []string{p.GetId(), p.GetCardholderName(), p.GetExpirationMonth(), p.GetExpirationYear(), p.GetCreatedAt().String()}
}
header := []string{"ID", "Cardholder", "Exp. Month", "Exp. Year", "Created"}

Expand Down
17 changes: 10 additions & 7 deletions internal/organizations/retrieve.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@
package organizations

import (
"context"
"fmt"

"github.com/packethost/packngo"
pager "github.com/equinix/metal-cli/internal/pagination"
"github.com/spf13/cobra"
)

Expand All @@ -43,31 +44,33 @@ func (c *Client) Retrieve() *cobra.Command {

RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
listOpts := c.Servicer.ListOptions(nil, nil)
include := []string{"Inner_example"}
exclude := []string{"Inner_example"}
withoutProjects := "withoutProjects_example"

if organizationID == "" {
orgs, _, err := c.Service.List(listOpts)
orgs, err := pager.GetAllOrganizations(c.Service, include, exclude, withoutProjects)
if err != nil {
return fmt.Errorf("Could not list Organizations: %w", err)
}

data := make([][]string, len(orgs))

for i, p := range orgs {
data[i] = []string{p.ID, p.Name, p.Created}
data[i] = []string{p.GetId(), p.GetName(), p.CreatedAt.String()}
}
header := []string{"ID", "Name", "Created"}

return c.Out.Output(orgs, header, &data)
} else {
getOpts := &packngo.GetOptions{Includes: listOpts.Includes, Excludes: listOpts.Excludes}
org, _, err := c.Service.Get(organizationID, getOpts)
org, _, err := c.Service.FindOrganizationById(context.Background(), organizationID).Include(include).Exclude(exclude).Execute()
if err != nil {
return fmt.Errorf("Could not get Organization: %w", err)
}

data := make([][]string, 1)

data[0] = []string{org.ID, org.Name, org.Created}
data[0] = []string{org.GetId(), org.GetName(), org.GetCreatedAt().String()}
header := []string{"ID", "Name", "Created"}

return c.Out.Output(org, header, &data)
Expand Down
13 changes: 9 additions & 4 deletions internal/organizations/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@
package organizations

import (
"context"
"fmt"

"github.com/packethost/packngo"
metal "github.com/equinix-labs/metal-go/metal/v1"
"github.com/spf13/cobra"
)

Expand All @@ -39,7 +40,7 @@ func (c *Client) Update() *cobra.Command {

RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
req := &packngo.OrganizationUpdateRequest{}
req := metal.NewOrganizationInput()

if name != "" {
req.Name = &name
Expand All @@ -57,14 +58,18 @@ func (c *Client) Update() *cobra.Command {
req.Logo = &logo
}

org, _, err := c.Service.Update(organizationID, req)
if website != "" {
req.Website = &website
}

org, _, err := c.Service.UpdateOrganization(context.Background(), organizationID).OrganizationInput(*req).Execute()
if err != nil {
return fmt.Errorf("Could not update Organization: %w", err)
}

data := make([][]string, 1)

data[0] = []string{org.ID, org.Name, org.Created}
data[0] = []string{org.GetId(), org.GetName(), org.GetCreatedAt().String()}
header := []string{"ID", "Name", "Created"}

return c.Out.Output(org, header, &data)
Expand Down
19 changes: 19 additions & 0 deletions internal/pagination/pager.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,22 @@ func GetAllEvents(s metal.EventsApiService, include []string, exclude []string)
return events, nil
}
}

func GetAllOrganizations(s metal.OrganizationsApiService, include, exclude []string, withOutProjects string) ([]metal.Organization, error) {
var orgs []metal.Organization
page := int32(1) // int32 | Page to return (optional) (default to 1)
perPage := int32(56) // int32 | Items returned per page (optional) (default to 10)

for {
orgPage, _, err := s.FindOrganizations(context.Background()).Include(include).Exclude(exclude).WithoutProjects(withOutProjects).Page(page).PerPage(perPage).Execute()
if err != nil {
return nil, err
}
orgs = append(orgs, orgPage.GetOrganizations()...)
if orgPage.Meta.GetLastPage() > orgPage.Meta.GetCurrentPage() {
page = page + 1
continue
}
return orgs, nil
}
}
51 changes: 51 additions & 0 deletions test/e2e/organization_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package e2e

import (
"testing"

root "github.com/equinix/metal-cli/internal/cli"
"github.com/equinix/metal-cli/internal/organizations"
outputPkg "github.com/equinix/metal-cli/internal/outputs"
"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
)

func TestCli_Organization(t *testing.T) {
subCommand := "organization"
consumerToken := ""
apiURL := ""
Version := "metal"
rootClient := root.NewClient(consumerToken, apiURL, Version)
type fields struct {
MainCmd *cobra.Command
Outputer outputPkg.Outputer
}
tests := []struct {
name string
fields fields
want *cobra.Command
cmdFunc func(*testing.T, *cobra.Command)
}{
{
name: "get",
fields: fields{
MainCmd: organizations.NewClient(rootClient, outputPkg.Outputer(&outputPkg.Standard{})).NewCommand(),
Outputer: outputPkg.Outputer(&outputPkg.Standard{}),
},
want: &cobra.Command{},
cmdFunc: func(t *testing.T, c *cobra.Command) {
root := c.Root()
root.SetArgs([]string{subCommand, "get"})
err := root.Execute()
assert.NoError(t, err)
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
rootCmd := rootClient.NewCommand()
rootCmd.AddCommand(tt.fields.MainCmd)
tt.cmdFunc(t, tt.fields.MainCmd)
})
}
}
Loading