Skip to content

Commit

Permalink
add --metro to ip request command
Browse files Browse the repository at this point in the history
Signed-off-by: Marques Johansson <[email protected]>
  • Loading branch information
displague committed Jun 16, 2022
1 parent dce7f70 commit 6d584e5
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 19 deletions.
11 changes: 6 additions & 5 deletions docs/metal_ip_request.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,26 @@ Request a block of IP addresses.

### Synopsis

Requests either a block of public IPv4 addresses or global IPv4 addresses for your project in a specific facility.
Requests either a block of public IPv4 addresses or global IPv4 addresses for your project in a specific metro or facility.

```
metal ip request -p <project_id> -t <ip_address_type> -q <quantity> -f <facility_code> [-f <flags>] [-c <comments>] [flags]
metal ip request -p <project_id> -t <ip_address_type> -q <quantity> (-m <metro> | -f <facility>) [-f <flags>] [-c <comments>] [flags]
```

### Examples

```
# Requests a block of 4 public IPv4 addresses in DA11:
metal ip request -p $METAL_PROJECT_ID -t public_ipv4 -q 4 -f da11
# Requests a block of 4 public IPv4 addresses in Dallas:
metal ip request -p $METAL_PROJECT_ID -t public_ipv4 -q 4 -m da
```

### Options

```
-c, --comments string General comments or description.
-f, --facility string Code of the facility.
-f, --facility string Code of the facility where the IP Reservation will be created
-h, --help help for request
-m, --metro string Code of the metro where the IP Reservation will be created
-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.
-q, --quantity int Number of IP addresses to reserve.
--tags strings Tag or Tags to add to the reservation, in a comma-separated list.
Expand Down
17 changes: 9 additions & 8 deletions internal/ips/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,30 +29,31 @@ import (
)

func (c *Client) Request() *cobra.Command {

var (
ttype string
quantity int
comments string
facility string
metro string
projectID string
tags []string
)

// requestIPCmd represents the requestIp command
var requestIPCmd = &cobra.Command{
Use: `request -p <project_id> -t <ip_address_type> -q <quantity> -f <facility_code> [-f <flags>] [-c <comments>]`,
requestIPCmd := &cobra.Command{
Use: `request -p <project_id> -t <ip_address_type> -q <quantity> (-m <metro> | -f <facility>) [-f <flags>] [-c <comments>]`,
Short: "Request a block of IP addresses.",
Long: "Requests either a block of public IPv4 addresses or global IPv4 addresses for your project in a specific facility.",
Example: ` # Requests a block of 4 public IPv4 addresses in DA11:
metal ip request -p $METAL_PROJECT_ID -t public_ipv4 -q 4 -f da11`,
Long: "Requests either a block of public IPv4 addresses or global IPv4 addresses for your project in a specific metro or facility.",
Example: ` # Requests a block of 4 public IPv4 addresses in Dallas:
metal ip request -p $METAL_PROJECT_ID -t public_ipv4 -q 4 -m da`,

RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
req := &packngo.IPReservationRequest{
Type: ttype,
Quantity: quantity,
Facility: &facility,
Metro: &metro,
Tags: tags,
}

Expand All @@ -72,14 +73,14 @@ func (c *Client) Request() *cobra.Command {

requestIPCmd.Flags().StringVarP(&projectID, "project-id", "p", "", "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.")
requestIPCmd.Flags().StringVarP(&ttype, "type", "t", "", "The type of IP Address, either public_ipv4 or global_ipv4.")
requestIPCmd.Flags().StringVarP(&facility, "facility", "f", "", "Code of the facility.")
requestIPCmd.Flags().StringVarP(&facility, "facility", "f", "", "Code of the facility where the IP Reservation will be created")
requestIPCmd.Flags().StringVarP(&metro, "metro", "m", "", "Code of the metro where the IP Reservation will be created")
requestIPCmd.Flags().IntVarP(&quantity, "quantity", "q", 0, "Number of IP addresses to reserve.")
requestIPCmd.Flags().StringSliceVar(&tags, "tags", nil, "Tag or Tags to add to the reservation, in a comma-separated list.")

_ = requestIPCmd.MarkFlagRequired("project-id")
_ = requestIPCmd.MarkFlagRequired("type")
_ = requestIPCmd.MarkFlagRequired("quantity")
_ = requestIPCmd.MarkFlagRequired("facility")

requestIPCmd.Flags().StringVarP(&comments, "comments", "c", "", "General comments or description.")
return requestIPCmd
Expand Down
23 changes: 17 additions & 6 deletions internal/ips/retrieve.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (c *Client) Retrieve() *cobra.Command {
)

// ipCmd represents the ip command
var retrieveIPCmd = &cobra.Command{
retrieveIPCmd := &cobra.Command{
Use: `get -p <project_UUID> | -a <assignment_UUID> | -r <reservation_UUID>`,
Aliases: []string{"list"},
Short: "Retrieves information about IP addresses, IP address reservations, and IP address assignments.",
Expand Down Expand Up @@ -79,9 +79,16 @@ func (c *Client) Retrieve() *cobra.Command {
}

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

data[0] = []string{ip.ID, ip.Address, ip.Facility.Code, strconv.FormatBool(ip.Public), ip.Created}
header := []string{"ID", "Address", "Facility", "Public", "Created"}
code := ""
metro := ""
if ip.Facility != nil {
code = ip.Facility.Code
}
if ip.Metro != nil {
metro = ip.Metro.Code
}
data[0] = []string{ip.ID, ip.Address, metro, code, strconv.FormatBool(ip.Public), ip.Created}
header := []string{"ID", "Address", "Metro", "Facility", "Public", "Created"}

return c.Out.Output(ip, header, &data)
}
Expand All @@ -95,12 +102,16 @@ func (c *Client) Retrieve() *cobra.Command {

for i, ip := range ips {
code := ""
metro := ""
if ip.Facility != nil {
code = ip.Facility.Code
}
data[i] = []string{ip.ID, ip.Address, code, strconv.FormatBool(ip.Public), ip.Created}
if ip.Metro != nil {
metro = ip.Metro.Code
}
data[i] = []string{ip.ID, ip.Address, metro, code, strconv.FormatBool(ip.Public), ip.Created}
}
header := []string{"ID", "Address", "Facility", "Public", "Created"}
header := []string{"ID", "Address", "Metro", "Facility", "Public", "Created"}

return c.Out.Output(ips, header, &data)
},
Expand Down

0 comments on commit 6d584e5

Please sign in to comment.