Skip to content

Commit

Permalink
Updated metal-go client for sub-command orgs
Browse files Browse the repository at this point in the history
  • Loading branch information
codinja1188 committed Jun 21, 2023
1 parent b5f4ee5 commit 52b123e
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 30 deletions.
33 changes: 24 additions & 9 deletions internal/organizations/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@
package organizations

import (
"context"
"fmt"
"log"
"os"

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

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

RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
req := &packngo.OrganizationCreateRequest{
Name: name,
}
// inc := []string{}
// exc := []string{}
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
logoData, fileErr := os.OpenFile(logo, os.O_RDWR, 0755)
if fileErr != nil {
log.Fatal(fileErr)
}
if fileErr := logoData.Close(); fileErr != nil {
log.Fatal(fileErr)
}
req.Logo = &logoData
}

org, _, err := c.Service.Create(req)
org, _, err := c.Service.CreateOrganization(context.Background()).OrganizationInput(*req).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
9 changes: 4 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,8 +60,7 @@ 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
}

Expand Down
9 changes: 6 additions & 3 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,17 @@ func (c *Client) PaymentMethods() *cobra.Command {

RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
paymentMethods, _, err := c.Service.ListPaymentMethods(organizationID)
include := []string{}
exclude := []string{}
paymentMethodsList, _, err := c.Service.FindOrganizationPaymentMethods(context.Background(), organizationID).Include(include).Exclude(exclude).Execute()
if err != nil {
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
16 changes: 9 additions & 7 deletions internal/organizations/retrieve.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
package organizations

import (
"context"
"fmt"

"github.com/packethost/packngo"
"github.com/spf13/cobra"
)

Expand All @@ -43,31 +43,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 := utils.GetAllOrganizations(c.Service, include, exclude, withoutProjects)

Check failure on line 51 in internal/organizations/retrieve.go

View workflow job for this annotation

GitHub Actions / test

undefined: utils

Check failure on line 51 in internal/organizations/retrieve.go

View workflow job for this annotation

GitHub Actions / lint

undeclared name: `utils` (typecheck)

Check failure on line 51 in internal/organizations/retrieve.go

View workflow job for this annotation

GitHub Actions / docs (1.19)

undefined: utils
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
24 changes: 19 additions & 5 deletions internal/organizations/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@
package organizations

import (
"context"
"fmt"
"log"
"os"

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

Expand All @@ -39,7 +42,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 @@ -54,17 +57,28 @@ func (c *Client) Update() *cobra.Command {
}

if logo != "" {
req.Logo = &logo
logoData, err := os.OpenFile(logo, os.O_RDWR, 0755)
if err != nil {
log.Fatal(err)
}
if err := logoData.Close(); err != nil {
log.Fatal(err)
}
req.Logo = &logoData
}

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 @@ -24,3 +24,22 @@ func GetAllProjects(s metal.ProjectsApiService, include []string, exclude []stri
return projects, 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
}
}

0 comments on commit 52b123e

Please sign in to comment.