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

feat: BETA loadbalancer service support #362

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
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: 2 additions & 0 deletions cmd/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
initPkg "github.com/equinix/metal-cli/internal/init"
"github.com/equinix/metal-cli/internal/interconnections"
"github.com/equinix/metal-cli/internal/ips"
"github.com/equinix/metal-cli/internal/loadbalancers"
"github.com/equinix/metal-cli/internal/metros"
"github.com/equinix/metal-cli/internal/organizations"
"github.com/equinix/metal-cli/internal/os"
Expand Down Expand Up @@ -99,5 +100,6 @@ func (cli *Cli) RegisterCommands(client *root.Client) {
interconnections.NewClient(client, cli.Outputer).NewCommand(),
vrf.NewClient(client, cli.Outputer).NewCommand(),
virtualcircuit.NewClient(client, cli.Outputer).NewCommand(),
loadbalancers.NewClient(client, cli.Outputer).NewCommand(),
)
}
1 change: 1 addition & 0 deletions docs/metal.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Command line interface for Equinix Metal
* [metal init](metal_init.md) - Create a configuration file.
* [metal interconnections](metal_interconnections.md) - interconnections operations: create, get, update, delete
* [metal ip](metal_ip.md) - IP address, reservations, and assignment operations: assign, unassign, remove, available, request and get.
* [metal loadbalancer-beta](metal_loadbalancer-beta.md) - LoadBalancer BETA operations: create, get, update, and delete.
* [metal metros](metal_metros.md) - Metro operations: get.
* [metal operating-systems](metal_operating-systems.md) - Operating system operations: get.
* [metal organization](metal_organization.md) - Organization operations: create, get, update, payment-methods, and delete.
Expand Down
36 changes: 36 additions & 0 deletions docs/metal_loadbalancer-beta.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
## metal loadbalancer-beta

LoadBalancer BETA operations: create, get, update, and delete.

### Synopsis

Information and management for LoadBalancers is on https://deploy.equinix.com/developers/docs/metal/networking/load-balancers/.

### Options

```
-h, --help help for loadbalancer-beta
```

### Options inherited from parent commands

```
--config string Path to JSON or YAML configuration file (METAL_CONFIG)
--exclude strings Comma separated Href references to collapse in results, may be dotted three levels deep
--filter stringArray Filter 'get' actions with name value pairs. Filter is not supported by all resources and is implemented as request query parameters.
--http-header strings Headers to add to requests (in format key=value)
--include strings Comma separated Href references to expand in results, may be dotted three levels deep
-o, --output string Output format (*table, json, yaml). env output formats are (*sh, terraform, capp).
--search string Search keyword for use in 'get' actions. Search is not supported by all resources.
--sort-by string Sort fields for use in 'get' actions. Sort is not supported by all resources.
--sort-dir string Sort field direction for use in 'get' actions. Sort is not supported by all resources.
--token string Metal API Token (METAL_AUTH_TOKEN)
```

### SEE ALSO

* [metal](metal.md) - Command line interface for Equinix Metal
* [metal loadbalancer-beta create](metal_loadbalancer-beta_create.md) - Creates a loadbalancer.
* [metal loadbalancer-beta delete](metal_loadbalancer-beta_delete.md) - Deletes a loadbalancer.
* [metal loadbalancer-beta get](metal_loadbalancer-beta_get.md) - Retrieves all the project loadbalancers or the details of a specified loadbalancer.

52 changes: 52 additions & 0 deletions docs/metal_loadbalancer-beta_create.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
## metal loadbalancer-beta create

Creates a loadbalancer.

### Synopsis

Creates a loadbalancer with the specified name.

```
metal loadbalancer-beta create -n <loadbalancer_name> -l <location_id_or_metro> [-p <project_UUID>] [--provider <provider_id>] [--port <port_UUID>] [flags]
```

### Examples

```
# Creates a new loadbalancer named dev-loadbal in the Dallas metro:
metal loadbalancer create --name dev-loadbal -l da

# Creates a new loadbalancer named prod-loadbal in the DC metro:
metal loadbalancer create -n prod-loadbal -l dc
```

### Options

```
-h, --help help for create
-l, --location string The location's ID. This flag is required.
-n, --name string Name of the loadbalancer
--port strings The port(s) UUID
-p, --project-id string The project's UUID. This flag is required, unless specified in the config created by metal init or set as METAL_PROJECT_ID environment variable.
-r, --provider string The provider ID. (default "loadpvd-gOB_-byp5ebFo7A3LHv2B")
```

### Options inherited from parent commands

```
--config string Path to JSON or YAML configuration file (METAL_CONFIG)
--exclude strings Comma separated Href references to collapse in results, may be dotted three levels deep
--filter stringArray Filter 'get' actions with name value pairs. Filter is not supported by all resources and is implemented as request query parameters.
--http-header strings Headers to add to requests (in format key=value)
--include strings Comma separated Href references to expand in results, may be dotted three levels deep
-o, --output string Output format (*table, json, yaml). env output formats are (*sh, terraform, capp).
--search string Search keyword for use in 'get' actions. Search is not supported by all resources.
--sort-by string Sort fields for use in 'get' actions. Sort is not supported by all resources.
--sort-dir string Sort field direction for use in 'get' actions. Sort is not supported by all resources.
--token string Metal API Token (METAL_AUTH_TOKEN)
```

### SEE ALSO

* [metal loadbalancer-beta](metal_loadbalancer-beta.md) - LoadBalancer BETA operations: create, get, update, and delete.

51 changes: 51 additions & 0 deletions docs/metal_loadbalancer-beta_delete.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
## metal loadbalancer-beta delete

Deletes a loadbalancer.

### Synopsis

Deletes the specified loadbalancer with a confirmation prompt. To skip the confirmation use --force.

```
metal loadbalancer-beta delete --id <loadbalancer_UUID> [--force] [flags]
```

### Examples

```
# Deletes loadbalancer 50693ba9-e4e4-4d8a-9eb2-4840b11e9375:
metal loadbalancer delete -i 50693ba9-e4e4-4d8a-9eb2-4840b11e9375
>
✔ Are you sure you want to delete loadbalancer 50693ba9-e4e4-4d8a-9eb2-4840b11e9375: y

# Deletes loadbalancer 50693ba9-e4e4-4d8a-9eb2-4840b11e9375, skipping confirmation:
metal loadbalancer delete -i 50693ba9-e4e4-4d8a-9eb2-4840b11e9375 -f
```

### Options

```
-f, --force Force removal of the loadbalancer
-h, --help help for delete
-i, --id string The loadbalancer's ID. This flag is required.
```

### Options inherited from parent commands

```
--config string Path to JSON or YAML configuration file (METAL_CONFIG)
--exclude strings Comma separated Href references to collapse in results, may be dotted three levels deep
--filter stringArray Filter 'get' actions with name value pairs. Filter is not supported by all resources and is implemented as request query parameters.
--http-header strings Headers to add to requests (in format key=value)
--include strings Comma separated Href references to expand in results, may be dotted three levels deep
-o, --output string Output format (*table, json, yaml). env output formats are (*sh, terraform, capp).
--search string Search keyword for use in 'get' actions. Search is not supported by all resources.
--sort-by string Sort fields for use in 'get' actions. Sort is not supported by all resources.
--sort-dir string Sort field direction for use in 'get' actions. Sort is not supported by all resources.
--token string Metal API Token (METAL_AUTH_TOKEN)
```

### SEE ALSO

* [metal loadbalancer-beta](metal_loadbalancer-beta.md) - LoadBalancer BETA operations: create, get, update, and delete.

53 changes: 53 additions & 0 deletions docs/metal_loadbalancer-beta_get.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
## metal loadbalancer-beta get

Retrieves all the project loadbalancers or the details of a specified loadbalancer.

### Synopsis

Retrieves all the project loadbalancers or the details of a specified loadbalancer. You can specify which loadbalancer by UUID or name.

```
metal loadbalancer-beta get [-i <loadbalancer_UUID> | -n <loadbalancer_name>] [flags]
```

### Examples

```
# Retrieve all loadbalancers:
metal loadbalancer get

# Retrieve a specific loadbalancer by UUID:
metal loadbalancer get -i 2008f885-1aac-406b-8d99-e6963fd21333

# Retrieve a specific loadbalancer by name:
metal loadbalancer get -n dev-cluster03
```

### Options

```
-h, --help help for get
-i, --id string The loadbalancer's UUID, which can be specified in the config created by metal init or set as METAL_PROJECT_ID environment variable.
-n, --loadbalancer string The name of the loadbalancer.
-p, --project-id string The project's UUID. This flag is required, unless specified in the config created by metal init or set as METAL_PROJECT_ID environment variable.
```

### Options inherited from parent commands

```
--config string Path to JSON or YAML configuration file (METAL_CONFIG)
--exclude strings Comma separated Href references to collapse in results, may be dotted three levels deep
--filter stringArray Filter 'get' actions with name value pairs. Filter is not supported by all resources and is implemented as request query parameters.
--http-header strings Headers to add to requests (in format key=value)
--include strings Comma separated Href references to expand in results, may be dotted three levels deep
-o, --output string Output format (*table, json, yaml). env output formats are (*sh, terraform, capp).
--search string Search keyword for use in 'get' actions. Search is not supported by all resources.
--sort-by string Sort fields for use in 'get' actions. Sort is not supported by all resources.
--sort-dir string Sort field direction for use in 'get' actions. Sort is not supported by all resources.
--token string Metal API Token (METAL_AUTH_TOKEN)
```

### SEE ALSO

* [metal loadbalancer-beta](metal_loadbalancer-beta.md) - LoadBalancer BETA operations: create, get, update, and delete.

5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/spf13/viper v1.18.2
github.com/stretchr/testify v1.8.4
golang.org/x/exp v0.0.0-20240119083558-1b970713d09a
golang.org/x/oauth2 v0.15.0
golang.org/x/term v0.16.0
sigs.k8s.io/yaml v1.3.0
)
Expand All @@ -20,6 +21,7 @@ require (
github.com/cpuguy83/go-md2man/v2 v2.0.3 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
Expand All @@ -36,8 +38,11 @@ require (
github.com/subosito/gotenv v1.6.0 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
20 changes: 18 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dnaeon/go-vcr v1.2.0 h1:zHCHvJYTMh1N7xnV7zf1m1GPBF9Ad0Jk/whtQ1663qI=
github.com/dnaeon/go-vcr v1.2.0/go.mod h1:R4UdLID7HZT3taECzJs4YgbbH6PIGXB6W/sc5OLb6RQ=
github.com/equinix/equinix-sdk-go v0.31.2 h1:7aFyKtuja2OSd7ocIHW/YugeubWIQUcVb1+g1cRbiZo=
github.com/equinix/equinix-sdk-go v0.31.2/go.mod h1:qnpdRzVftHFNaJFk1VSIrAOTLrIoeDrxzUr3l8ARyvQ=
github.com/equinix/equinix-sdk-go v0.32.0 h1:zUn0Em5FJe6f6bntftrDBpO9L+XhbpFMPuQ7RKEOgXM=
github.com/equinix/equinix-sdk-go v0.32.0/go.mod h1:qnpdRzVftHFNaJFk1VSIrAOTLrIoeDrxzUr3l8ARyvQ=
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
Expand Down Expand Up @@ -79,16 +82,29 @@ golang.org/x/crypto v0.16.0 h1:mMMrFzRSCF0GvB7Ne27XVtVAaXLrPmgPC7/v0tkwHaY=
golang.org/x/exp v0.0.0-20240119083558-1b970713d09a h1:Q8/wZp0KX97QFTc2ywcOE0YRjZPVIx+MXInMzdvQqcA=
golang.org/x/exp v0.0.0-20240119083558-1b970713d09a/go.mod h1:idGWGoKP1toJGkd5/ig9ZLuPcZBC3ewk7SzmH0uou08=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c=
golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U=
golang.org/x/oauth2 v0.15.0 h1:s8pnnxNVzjWyrvYdFUQq5llS1PX2zhPXmccZv99h7uQ=
golang.org/x/oauth2 v0.15.0/go.mod h1:q48ptWNTY5XWf+JNten23lcvHpLJ0ZSxF5ttTHKVCAM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.16.0 h1:m+B6fahuftsE9qjo0VWp2FW0mB3MTJvR0BaMQrq0pmE=
golang.org/x/term v0.16.0/go.mod h1:yn7UURbUtPyrVJPGPq404EukNFxcm/foM+bV/bfcDsY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/appengine v1.6.7 h1:FZR1q0exgwxzPzp/aF+VccGrSfxfPpkBqjIIEq3ru6c=
google.golang.org/appengine v1.6.7/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.31.0 h1:g0LDEJHgrBl9N9r17Ru3sqWhkIx2NB67okBHPwC7hs8=
google.golang.org/protobuf v1.31.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
Expand Down
52 changes: 51 additions & 1 deletion internal/cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
package cli

import (
"context"
"fmt"
"log"
"net/http"
Expand All @@ -34,7 +35,10 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"golang.org/x/oauth2"

v1 "github.com/equinix/metal-cli/internal/loadbalancers/api/v1"
"github.com/equinix/metal-cli/internal/loadbalancers/infrastructure"
outputPkg "github.com/equinix/metal-cli/internal/outputs"
)

Expand All @@ -48,6 +52,7 @@ type Client struct {
// apiClient client
apiClient *packngo.Client
metalApiClient *metal.APIClient
lbaasApiClient *v1.APIClient

includes *[]string // nolint:unused
excludes *[]string // nolint:unused
Expand Down Expand Up @@ -93,12 +98,14 @@ func checkEnvForDebug() bool {
return os.Getenv(debugVar) != ""
}

var uaFormat = "metal-cli/%s %s"

func (c *Client) apiConnect(httpClient *http.Client) error {
client, err := packngo.NewClientWithBaseURL(c.consumerToken, c.metalToken, httpClient, c.apiURL)
if err != nil {
return fmt.Errorf("could not create client: %w", err)
}
client.UserAgent = fmt.Sprintf("metal-cli/%s %s", c.Version, client.UserAgent)
client.UserAgent = fmt.Sprintf(uaFormat, c.Version, client.UserAgent)
c.apiClient = client
return nil
}
Expand All @@ -118,6 +125,35 @@ func (c *Client) metalApiConnect(httpClient *http.Client) error {
return nil
}

func (c *Client) lbaasApiConnect(header http.Header) error {
ctx := context.Background()
config := oauth2.Config{
Endpoint: oauth2.Endpoint{
TokenURL: "https://iam.metalctrl.io/token",
},
}
ts := infrastructure.NewTokenExchanger(c.Token(), nil)
token, err := ts.Token()
if err != nil {
return err
}
client := &http.Client{
Transport: &headerTransport{
header: header,
},
}
ctx = context.WithValue(ctx, oauth2.HTTPClient, client)
client = config.Client(ctx, token)

configuration := v1.NewConfiguration()
configuration.Debug = checkEnvForDebug()
configuration.HTTPClient = client
configuration.UserAgent = fmt.Sprintf(uaFormat, c.Version, configuration.UserAgent)

c.lbaasApiClient = v1.NewAPIClient(configuration)
return nil
}

func (c *Client) Config(cmd *cobra.Command) *viper.Viper {
if c.viper == nil {
v := viper.New()
Expand Down Expand Up @@ -219,6 +255,20 @@ func (c *Client) MetalAPI(cmd *cobra.Command) *metal.APIClient {
return c.metalApiClient
}

func (c *Client) LoadbalancerAPI(cmd *cobra.Command) *v1.APIClient {
if c.metalToken == "" {
log.Fatal("Equinix Metal authentication token not provided. Please set the 'METAL_AUTH_TOKEN' environment variable or create a configuration file using 'metal init'.")
}

if c.lbaasApiClient == nil {
err := c.lbaasApiConnect(getAdditionalHeaders(cmd))
if err != nil {
log.Fatal(err)
}
}
return c.lbaasApiClient
}

func (c *Client) Token() string {
return c.metalToken
}
Expand Down
Loading
Loading