Skip to content

Commit

Permalink
Updated metal-go client for sub-commands Virtual Network
Browse files Browse the repository at this point in the history
  • Loading branch information
codinja1188 committed Jul 27, 2023
1 parent 9fc1587 commit bf10e3f
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 23 deletions.
25 changes: 14 additions & 11 deletions internal/vlan/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@
package vlan

import (
"context"
"fmt"
"strconv"

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

Expand All @@ -45,28 +46,30 @@ func (c *Client) Create() *cobra.Command {

RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true

req := &packngo.VirtualNetworkCreateRequest{
ProjectID: projectID,
Metro: metro,
Facility: facility,
VXLAN: vxlan,
virtualNetworkCreateInput := metal.NewVirtualNetworkCreateInput()
if metro != "" {
virtualNetworkCreateInput.SetMetro(metro)
}
if facility != "" {
virtualNetworkCreateInput.SetFacility(facility)
}
virtualNetworkCreateInput.SetVxlan(int32(vxlan))

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

n, _, err := c.Service.Create(req)
n, _, err := c.Service.CreateVirtualNetwork(context.Background(), projectID).VirtualNetworkCreateInput(*virtualNetworkCreateInput).Include(nil).Exclude(nil).Execute()
if err != nil {
return fmt.Errorf("Could not create ProjectVirtualNetwork: %w", err)
}

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

// TODO(displague) metro is not in the response
data[0] = []string{n.ID, n.Description, strconv.Itoa(n.VXLAN), n.MetroCode, n.FacilityCode, n.CreatedAt}
data[0] = []string{n.GetId(), n.GetDescription(), strconv.Itoa(int(n.GetVxlan())), n.GetMetroCode()}

header := []string{"ID", "Description", "VXLAN", "Metro", "Facility", "Created"}
header := []string{"ID", "Description", "VXLAN", "Metro"}

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

import (
"context"
"fmt"

"github.com/manifoldco/promptui"
Expand All @@ -34,7 +35,7 @@ func (c *Client) Delete() *cobra.Command {
)

deleteVnet := func(id string) error {
_, err := c.Service.Delete(id)
_, _, err := c.Service.DeleteVirtualNetwork(context.Background(), vnetID).Execute()
if err != nil {
return err
}
Expand Down
21 changes: 15 additions & 6 deletions internal/vlan/retrieve.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,26 @@ func (c *Client) Retrieve() *cobra.Command {

RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
vnets, _, err := c.Service.List(projectID, c.Servicer.ListOptions(nil, nil))
request := c.Service.FindVirtualNetworks(cmd.Context(), projectID).Include(nil).Exclude(nil)
filters := c.Servicer.Filters()
if filters["facility"] != "" {
request = request.Facility(filters["facility"])
}

if filters["metro"] != "" {
request = request.Metro(filters["metro"])
}
VirtualNetworksList, _, err := request.Execute()
if err != nil {
return fmt.Errorf("Could not list Project Virtual Networks: %w", err)
}
VirtualNetworks := VirtualNetworksList.GetVirtualNetworks()
data := make([][]string, len(VirtualNetworks))

data := make([][]string, len(vnets.VirtualNetworks))

for i, n := range vnets.VirtualNetworks {
data[i] = []string{n.ID, n.Description, strconv.Itoa(n.VXLAN), n.FacilityCode, n.CreatedAt}
for i, n := range VirtualNetworks {
data[i] = []string{n.GetId(), n.GetDescription(), strconv.Itoa(int(n.GetVxlan()), n.GetMetroCode(), }

Check failure on line 61 in internal/vlan/retrieve.go

View workflow job for this annotation

GitHub Actions / lint

syntax error: unexpected }, expecting expression (typecheck)

Check failure on line 61 in internal/vlan/retrieve.go

View workflow job for this annotation

GitHub Actions / lint

expected operand, found '}' (typecheck)

Check failure on line 61 in internal/vlan/retrieve.go

View workflow job for this annotation

GitHub Actions / lint

syntax error: unexpected }, expecting expression) (typecheck)

Check failure on line 61 in internal/vlan/retrieve.go

View workflow job for this annotation

GitHub Actions / test

syntax error: unexpected }, expecting expression

Check failure on line 61 in internal/vlan/retrieve.go

View workflow job for this annotation

GitHub Actions / docs (1.19)

syntax error: unexpected }, expecting expression
}
header := []string{"ID", "Description", "VXLAN", "Facility", "Created"}
header := []string{"ID", "Description", "VXLAN", "Facility"}

return c.Out.Output(vnets, header, &data)

Check failure on line 65 in internal/vlan/retrieve.go

View workflow job for this annotation

GitHub Actions / lint

missing ',' in argument list (typecheck)
},

Check failure on line 66 in internal/vlan/retrieve.go

View workflow job for this annotation

GitHub Actions / lint

expected operand, found '}' (typecheck)
Expand Down
12 changes: 7 additions & 5 deletions internal/vlan/vlan.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@
package vlan

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.ProjectVirtualNetworkService
Service metal.VLANsApiService
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).ProjectVirtualNetworks
c.Service = *c.Servicer.MetalAPI(cmd).VLANsApi
},
}

Expand All @@ -58,8 +58,10 @@ 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
Filters() map[string]string
Includes(defaultIncludes []string) (incl []string)
Excludes(defaultExcludes []string) (excl []string)
}

func NewClient(s Servicer, out outputs.Outputer) *Client {
Expand Down

0 comments on commit bf10e3f

Please sign in to comment.