Skip to content

Commit

Permalink
fix: Updated metal-go client for sub-command project bgp sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
codinja1188 committed Nov 30, 2023
1 parent 1994e1e commit 7e68154
Show file tree
Hide file tree
Showing 15 changed files with 536 additions and 165 deletions.
2 changes: 1 addition & 1 deletion docs/metal.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ Command line interface for Equinix Metal
* [metal organization](metal_organization.md) - Organization operations: create, get, update, payment-methods, and delete.
* [metal plan](metal_plan.md) - Plan operations: get.
* [metal port](metal_port.md) - Port operations: get, convert, vlans.
* [metal project](metal_project.md) - Project operations: create, get, update, delete, and bgpenable, bgpconfig, bgpsessions.
* [metal project](metal_project.md) - Project operations: create, get, update, delete, and bgp-enable, bgp-config, bgp-sessions.
* [metal ssh-key](metal_ssh-key.md) - SSH key operations: create, get, update, and delete.
* [metal user](metal_user.md) - User operations: get and add.
* [metal virtual-network](metal_virtual-network.md) - Virtual network (VLAN) operations : create, get, delete.
Expand Down
2 changes: 1 addition & 1 deletion docs/metal_project.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## metal project

Project operations: create, get, update, delete, and bgpenable, bgpconfig, bgpsessions.
Project operations: create, get, update, delete, and bgp-enable, bgp-config, bgp-sessions.

### Synopsis

Expand Down
5 changes: 2 additions & 3 deletions docs/metal_project_bgp-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ metal project bgp-config --project-id <project_UUID> [flags]
### Examples

```
# Get BGP config for project 50693ba9-e4e4-4d8a-9eb2-4840b11e9375:
metal project bgp-config --project-id 50693ba9-e4e4-4d8a-9eb2-4840b11e9375
metal project bgp-config --project-id 50693ba9-e4e4-4d8a-9eb2-4840b11e9375 -d
```

### Options
Expand All @@ -41,5 +40,5 @@ metal project bgp-config --project-id <project_UUID> [flags]

### SEE ALSO

* [metal project](metal_project.md) - Project operations: create, get, update, delete, and bgpenable, bgpconfig, bgpsessions.
* [metal project](metal_project.md) - Project operations: create, get, update, delete, and bgp-enable, bgp-config, bgp-sessions.

3 changes: 1 addition & 2 deletions docs/metal_project_bgp-enable.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ metal project bgp-enable --project-id <project_UUID> --deployment-type <deployme
### Examples

```
# Enable BGP on project 50693ba9-e4e4-4d8a-9eb2-4840b11e9375:
metal project bgp-enable --project-id 50693ba9-e4e4-4d8a-9eb2-4840b11e9375 --deployment-type local --asn 65000
```

Expand Down Expand Up @@ -45,5 +44,5 @@ metal project bgp-enable --project-id <project_UUID> --deployment-type <deployme

### SEE ALSO

* [metal project](metal_project.md) - Project operations: create, get, update, delete, and bgpenable, bgpconfig, bgpsessions.
* [metal project](metal_project.md) - Project operations: create, get, update, delete, and bgp-enable, bgp-config, bgp-sessions.

3 changes: 1 addition & 2 deletions docs/metal_project_bgp-sessions.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ metal project bgp-sessions --project-id <project_UUID> [flags]
### Examples

```
# Get BGP Sessions for project 50693ba9-e4e4-4d8a-9eb2-4840b11e9375:
metal project bgp-sessions --project-id 50693ba9-e4e4-4d8a-9eb2-4840b11e9375
```

Expand Down Expand Up @@ -41,5 +40,5 @@ metal project bgp-sessions --project-id <project_UUID> [flags]

### SEE ALSO

* [metal project](metal_project.md) - Project operations: create, get, update, delete, and bgpenable, bgpconfig, bgpsessions.
* [metal project](metal_project.md) - Project operations: create, get, update, delete, and bgp-enable, bgp-config, bgp-sessions.

2 changes: 1 addition & 1 deletion docs/metal_project_create.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,5 @@ metal project create -n <project_name> [-O <organization_UUID>] [-m <payment_met

### SEE ALSO

* [metal project](metal_project.md) - Project operations: create, get, update, delete, and bgpenable, bgpconfig, bgpsessions.
* [metal project](metal_project.md) - Project operations: create, get, update, delete, and bgp-enable, bgp-config, bgp-sessions.

2 changes: 1 addition & 1 deletion docs/metal_project_delete.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,5 @@ metal project delete --id <project_UUID> [--force] [flags]

### SEE ALSO

* [metal project](metal_project.md) - Project operations: create, get, update, delete, and bgpenable, bgpconfig, bgpsessions.
* [metal project](metal_project.md) - Project operations: create, get, update, delete, and bgp-enable, bgp-config, bgp-sessions.

2 changes: 1 addition & 1 deletion docs/metal_project_get.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,5 @@ metal project get [-i <project_UUID> | -n <project_name>] [flags]

### SEE ALSO

* [metal project](metal_project.md) - Project operations: create, get, update, delete, and bgpenable, bgpconfig, bgpsessions.
* [metal project](metal_project.md) - Project operations: create, get, update, delete, and bgp-enable, bgp-config, bgp-sessions.

2 changes: 1 addition & 1 deletion docs/metal_project_update.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,5 @@ metal project update -i <project_UUID> [-n <name>] [-m <payment_method_UUID>] [f

### SEE ALSO

* [metal project](metal_project.md) - Project operations: create, get, update, delete, and bgpenable, bgpconfig, bgpsessions.
* [metal project](metal_project.md) - Project operations: create, get, update, delete, and bgp-enable, bgp-config, bgp-sessions.

76 changes: 34 additions & 42 deletions internal/projects/bgpconfig.go
Original file line number Diff line number Diff line change
@@ -1,62 +1,54 @@
// Copyright © 2022 Equinix Metal Developers <[email protected]>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package projects

import (
"context"
"fmt"
"strconv"

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

// BGPConfigFlags holds the flags for the BGPConfig command
type BGPConfigFlags struct {
projectID string
}

func (c *Client) BGPConfig() *cobra.Command {
var projectID string
flags := BGPConfigFlags{}

// bgpConfigProjectCmd represents the updateProject command
bgpConfigProjectCmd := &cobra.Command{
Use: `bgp-config --project-id <project_UUID>`,
Short: "Gets BGP Config for a project.",
Long: `Gets BGP Config for a project.`,
Example: ` # Get BGP config for project 50693ba9-e4e4-4d8a-9eb2-4840b11e9375:
metal project bgp-config --project-id 50693ba9-e4e4-4d8a-9eb2-4840b11e9375`,
Use: `bgp-config --project-id <project_UUID>`,
Short: "Gets BGP Config for a project.",
Long: `Gets BGP Config for a project.`,
Example: ` metal project bgp-config --project-id 50693ba9-e4e4-4d8a-9eb2-4840b11e9375 -d `,
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
listOpt := c.Servicer.ListOptions(nil, nil)
getOpts := &packngo.GetOptions{Includes: listOpt.Includes, Excludes: listOpt.Excludes}
p, _, err := c.BGPConfigService.Get(projectID, getOpts)
if err != nil {
return fmt.Errorf("Could not get Project BGP Config: %w", err)
}

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

data[0] = []string{projectID, p.Status, strconv.Itoa(p.Asn), p.DeploymentType, strconv.Itoa(p.MaxPrefix)}
header := []string{"ID", "Status", "Sessions", "ASN", "DeploymentType", "MaxPrefix"}
return c.Out.Output(p, header, &data)
return getBGPConfig(c, &flags)
},
}

bgpConfigProjectCmd.Flags().StringVarP(&projectID, "project-id", "p", "", "Project ID (METAL_PROJECT_ID)")
bgpConfigProjectCmd.Flags().StringVarP(&flags.projectID, "project-id", "p", "", "Project ID (METAL_PROJECT_ID)")

_ = bgpConfigProjectCmd.MarkFlagRequired("project-id")

return bgpConfigProjectCmd
}

func getBGPConfig(c *Client, flags *BGPConfigFlags) error {
p, _, err := c.BGPConfigService.FindBgpConfigByProject(context.Background(), flags.projectID).Execute()
if err != nil {
return fmt.Errorf("error getting BGP Config for project %s: %w", flags.projectID, err)
}

data := [][]string{
{
flags.projectID,
string(p.GetStatus()),
strconv.Itoa(int(p.GetAsn())),
string(p.GetDeploymentType()),
strconv.Itoa(int(p.GetMaxPrefix())),
},
}
header := []string{"ID", "Status", "ASN", "DeploymentType", "MaxPrefix"}

return c.Out.Output(p, header, &data)
}
96 changes: 42 additions & 54 deletions internal/projects/bgpenable.go
Original file line number Diff line number Diff line change
@@ -1,75 +1,63 @@
// Copyright © 2022 Equinix Metal Developers <[email protected]>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package projects

import (
"context"
"fmt"
"strconv"

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

// BgpConfig holds the configuration for the BGP enable command
type BgpConfig struct {
ProjectID string
UseCase string
MD5 string
DeploymentType string
ASN int
}

func (c *Client) BGPEnable() *cobra.Command {
var (
projectID, useCase, md5, deploymentType string
asn int
)
// bgpEnableProjectCmd represents the updateProject command
config := BgpConfig{}

bgpEnableProjectCmd := &cobra.Command{
Use: `bgp-enable --project-id <project_UUID> --deployment-type <deployment_type> [--asn <asn>] [--md5 <md5_secret>] [--use-case <use_case>]`,
Short: "Enables BGP on a project.",
Long: `Enables BGP on a project.`,
Example: ` # Enable BGP on project 50693ba9-e4e4-4d8a-9eb2-4840b11e9375:
metal project bgp-enable --project-id 50693ba9-e4e4-4d8a-9eb2-4840b11e9375 --deployment-type local --asn 65000`,
Use: `bgp-enable --project-id <project_UUID> --deployment-type <deployment_type> [--asn <asn>] [--md5 <md5_secret>] [--use-case <use_case>]`,
Short: "Enables BGP on a project.",
Long: `Enables BGP on a project.`,
Example: ` metal project bgp-enable --project-id 50693ba9-e4e4-4d8a-9eb2-4840b11e9375 --deployment-type local --asn 65000`,
RunE: func(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true
req := packngo.CreateBGPConfigRequest{
UseCase: useCase,
Asn: asn,
DeploymentType: deploymentType,
Md5: md5,
}

p, err := c.BGPConfigService.Create(projectID, req)
if err != nil {
return fmt.Errorf("Could not update Project: %w", err)
}

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

data[0] = []string{projectID, useCase, strconv.Itoa(asn), deploymentType}
header := []string{"ID", "UseCase", "ASN", "DeploymentType"}
return c.Out.Output(p, header, &data)
return enableBGP(c, &config)
},
}

bgpEnableProjectCmd.Flags().StringVarP(&projectID, "project-id", "p", "", "Project ID (METAL_PROJECT_ID)")
bgpEnableProjectCmd.Flags().StringVar(&useCase, "use-case", "", "Use case for BGP")
bgpEnableProjectCmd.Flags().IntVar(&asn, "asn", 65000, "Local ASN")
bgpEnableProjectCmd.Flags().StringVar(&deploymentType, "deployment-type", "", "Deployment type (local, global)")
bgpEnableProjectCmd.Flags().StringVar(&md5, "md5", "", "BGP Password")
bgpEnableProjectCmd.Flags().StringVarP(&config.ProjectID, "project-id", "p", "", "Project ID (METAL_PROJECT_ID)")
bgpEnableProjectCmd.Flags().StringVar(&config.UseCase, "use-case", "", "Use case for BGP")
bgpEnableProjectCmd.Flags().IntVar(&config.ASN, "asn", 65000, "Local ASN")
bgpEnableProjectCmd.Flags().StringVar(&config.DeploymentType, "deployment-type", "", "Deployment type (local, global)")
bgpEnableProjectCmd.Flags().StringVar(&config.MD5, "md5", "", "BGP Password")

_ = bgpEnableProjectCmd.MarkFlagRequired("project-id")
_ = bgpEnableProjectCmd.MarkFlagRequired("asn")
_ = bgpEnableProjectCmd.MarkFlagRequired("deployment-type")

return bgpEnableProjectCmd
}

func enableBGP(c *Client, config *BgpConfig) error {
req := metal.BgpConfigRequestInput{
UseCase: &config.UseCase,
Asn: int32(config.ASN),
DeploymentType: metal.BgpConfigRequestInputDeploymentType(config.DeploymentType),
Md5: &config.MD5,
}

p, err := c.BGPConfigService.RequestBgpConfig(context.Background(), config.ProjectID).BgpConfigRequestInput(req).Execute()
if err != nil {
return fmt.Errorf("error enabling BGP for project %s: %w", config.ProjectID, err)
}

data := [][]string{{config.ProjectID, config.UseCase, strconv.Itoa(config.ASN), config.DeploymentType}}
header := []string{"ID", "UseCase", "ASN", "DeploymentType"}
return c.Out.Output(p, header, &data)
}
Loading

0 comments on commit 7e68154

Please sign in to comment.