Skip to content

Commit fe37db4

Browse files
authored
fix: Updated metal-go client for sub-command project bgp sessions (#394)
1 parent 099001c commit fe37db4

15 files changed

+536
-165
lines changed

docs/metal.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Command line interface for Equinix Metal
4343
* [metal organization](metal_organization.md) - Organization operations: create, get, update, payment-methods, and delete.
4444
* [metal plan](metal_plan.md) - Plan operations: get.
4545
* [metal port](metal_port.md) - Port operations: get, convert, vlans.
46-
* [metal project](metal_project.md) - Project operations: create, get, update, delete, and bgpenable, bgpconfig, bgpsessions.
46+
* [metal project](metal_project.md) - Project operations: create, get, update, delete, and bgp-enable, bgp-config, bgp-sessions.
4747
* [metal ssh-key](metal_ssh-key.md) - SSH key operations: create, get, update, and delete.
4848
* [metal user](metal_user.md) - User operations: get and add.
4949
* [metal virtual-network](metal_virtual-network.md) - Virtual network (VLAN) operations : create, get, delete.

docs/metal_project.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## metal project
22

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

55
### Synopsis
66

docs/metal_project_bgp-config.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ metal project bgp-config --project-id <project_UUID> [flags]
1313
### Examples
1414

1515
```
16-
# Get BGP config for project 50693ba9-e4e4-4d8a-9eb2-4840b11e9375:
17-
metal project bgp-config --project-id 50693ba9-e4e4-4d8a-9eb2-4840b11e9375
16+
metal project bgp-config --project-id 50693ba9-e4e4-4d8a-9eb2-4840b11e9375 -d
1817
```
1918

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

4241
### SEE ALSO
4342

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

docs/metal_project_bgp-enable.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ metal project bgp-enable --project-id <project_UUID> --deployment-type <deployme
1313
### Examples
1414

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

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

4645
### SEE ALSO
4746

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

docs/metal_project_bgp-sessions.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ metal project bgp-sessions --project-id <project_UUID> [flags]
1313
### Examples
1414

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

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

4241
### SEE ALSO
4342

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

docs/metal_project_create.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,5 @@ metal project create -n <project_name> [-O <organization_UUID>] [-m <payment_met
4646

4747
### SEE ALSO
4848

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

docs/metal_project_delete.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,5 @@ metal project delete --id <project_UUID> [--force] [flags]
4747

4848
### SEE ALSO
4949

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

docs/metal_project_get.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,5 @@ metal project get [-i <project_UUID> | -n <project_name>] [flags]
4848

4949
### SEE ALSO
5050

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

docs/metal_project_update.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,5 @@ metal project update -i <project_UUID> [-n <name>] [-m <payment_method_UUID>] [f
4646

4747
### SEE ALSO
4848

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

internal/projects/bgpconfig.go

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

233
import (
4+
"context"
245
"fmt"
256
"strconv"
267

27-
"github.com/packethost/packngo"
288
"github.com/spf13/cobra"
299
)
3010

11+
// BGPConfigFlags holds the flags for the BGPConfig command
12+
type BGPConfigFlags struct {
13+
projectID string
14+
}
15+
3116
func (c *Client) BGPConfig() *cobra.Command {
32-
var projectID string
17+
flags := BGPConfigFlags{}
3318

34-
// bgpConfigProjectCmd represents the updateProject command
3519
bgpConfigProjectCmd := &cobra.Command{
36-
Use: `bgp-config --project-id <project_UUID>`,
37-
Short: "Gets BGP Config for a project.",
38-
Long: `Gets BGP Config for a project.`,
39-
Example: ` # Get BGP config for project 50693ba9-e4e4-4d8a-9eb2-4840b11e9375:
40-
metal project bgp-config --project-id 50693ba9-e4e4-4d8a-9eb2-4840b11e9375`,
20+
Use: `bgp-config --project-id <project_UUID>`,
21+
Short: "Gets BGP Config for a project.",
22+
Long: `Gets BGP Config for a project.`,
23+
Example: ` metal project bgp-config --project-id 50693ba9-e4e4-4d8a-9eb2-4840b11e9375 -d `,
4124
RunE: func(cmd *cobra.Command, args []string) error {
42-
cmd.SilenceUsage = true
43-
listOpt := c.Servicer.ListOptions(nil, nil)
44-
getOpts := &packngo.GetOptions{Includes: listOpt.Includes, Excludes: listOpt.Excludes}
45-
p, _, err := c.BGPConfigService.Get(projectID, getOpts)
46-
if err != nil {
47-
return fmt.Errorf("Could not get Project BGP Config: %w", err)
48-
}
49-
50-
data := make([][]string, 1)
51-
52-
data[0] = []string{projectID, p.Status, strconv.Itoa(p.Asn), p.DeploymentType, strconv.Itoa(p.MaxPrefix)}
53-
header := []string{"ID", "Status", "Sessions", "ASN", "DeploymentType", "MaxPrefix"}
54-
return c.Out.Output(p, header, &data)
25+
return getBGPConfig(c, &flags)
5526
},
5627
}
5728

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

6031
_ = bgpConfigProjectCmd.MarkFlagRequired("project-id")
32+
6133
return bgpConfigProjectCmd
6234
}
35+
36+
func getBGPConfig(c *Client, flags *BGPConfigFlags) error {
37+
p, _, err := c.BGPConfigService.FindBgpConfigByProject(context.Background(), flags.projectID).Execute()
38+
if err != nil {
39+
return fmt.Errorf("error getting BGP Config for project %s: %w", flags.projectID, err)
40+
}
41+
42+
data := [][]string{
43+
{
44+
flags.projectID,
45+
string(p.GetStatus()),
46+
strconv.Itoa(int(p.GetAsn())),
47+
string(p.GetDeploymentType()),
48+
strconv.Itoa(int(p.GetMaxPrefix())),
49+
},
50+
}
51+
header := []string{"ID", "Status", "ASN", "DeploymentType", "MaxPrefix"}
52+
53+
return c.Out.Output(p, header, &data)
54+
}

internal/projects/bgpenable.go

+42-54
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,63 @@
1-
// Copyright © 2022 Equinix Metal Developers <[email protected]>
2-
//
3-
// Permission is hereby granted, free of charge, to any person obtaining a copy
4-
// of this software and associated documentation files (the "Software"), to deal
5-
// in the Software without restriction, including without limitation the rights
6-
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7-
// copies of the Software, and to permit persons to whom the Software is
8-
// furnished to do so, subject to the following conditions:
9-
//
10-
// The above copyright notice and this permission notice shall be included in
11-
// all copies or substantial portions of the Software.
12-
//
13-
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14-
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15-
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16-
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17-
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18-
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19-
// THE SOFTWARE.
20-
211
package projects
222

233
import (
4+
"context"
245
"fmt"
256
"strconv"
267

27-
"github.com/packethost/packngo"
8+
metal "github.com/equinix-labs/metal-go/metal/v1"
289
"github.com/spf13/cobra"
2910
)
3011

12+
// BgpConfig holds the configuration for the BGP enable command
13+
type BgpConfig struct {
14+
ProjectID string
15+
UseCase string
16+
MD5 string
17+
DeploymentType string
18+
ASN int
19+
}
20+
3121
func (c *Client) BGPEnable() *cobra.Command {
32-
var (
33-
projectID, useCase, md5, deploymentType string
34-
asn int
35-
)
36-
// bgpEnableProjectCmd represents the updateProject command
22+
config := BgpConfig{}
23+
3724
bgpEnableProjectCmd := &cobra.Command{
38-
Use: `bgp-enable --project-id <project_UUID> --deployment-type <deployment_type> [--asn <asn>] [--md5 <md5_secret>] [--use-case <use_case>]`,
39-
Short: "Enables BGP on a project.",
40-
Long: `Enables BGP on a project.`,
41-
Example: ` # Enable BGP on project 50693ba9-e4e4-4d8a-9eb2-4840b11e9375:
42-
metal project bgp-enable --project-id 50693ba9-e4e4-4d8a-9eb2-4840b11e9375 --deployment-type local --asn 65000`,
25+
Use: `bgp-enable --project-id <project_UUID> --deployment-type <deployment_type> [--asn <asn>] [--md5 <md5_secret>] [--use-case <use_case>]`,
26+
Short: "Enables BGP on a project.",
27+
Long: `Enables BGP on a project.`,
28+
Example: ` metal project bgp-enable --project-id 50693ba9-e4e4-4d8a-9eb2-4840b11e9375 --deployment-type local --asn 65000`,
4329
RunE: func(cmd *cobra.Command, args []string) error {
44-
cmd.SilenceUsage = true
45-
req := packngo.CreateBGPConfigRequest{
46-
UseCase: useCase,
47-
Asn: asn,
48-
DeploymentType: deploymentType,
49-
Md5: md5,
50-
}
51-
52-
p, err := c.BGPConfigService.Create(projectID, req)
53-
if err != nil {
54-
return fmt.Errorf("Could not update Project: %w", err)
55-
}
56-
57-
data := make([][]string, 1)
58-
59-
data[0] = []string{projectID, useCase, strconv.Itoa(asn), deploymentType}
60-
header := []string{"ID", "UseCase", "ASN", "DeploymentType"}
61-
return c.Out.Output(p, header, &data)
30+
return enableBGP(c, &config)
6231
},
6332
}
6433

65-
bgpEnableProjectCmd.Flags().StringVarP(&projectID, "project-id", "p", "", "Project ID (METAL_PROJECT_ID)")
66-
bgpEnableProjectCmd.Flags().StringVar(&useCase, "use-case", "", "Use case for BGP")
67-
bgpEnableProjectCmd.Flags().IntVar(&asn, "asn", 65000, "Local ASN")
68-
bgpEnableProjectCmd.Flags().StringVar(&deploymentType, "deployment-type", "", "Deployment type (local, global)")
69-
bgpEnableProjectCmd.Flags().StringVar(&md5, "md5", "", "BGP Password")
34+
bgpEnableProjectCmd.Flags().StringVarP(&config.ProjectID, "project-id", "p", "", "Project ID (METAL_PROJECT_ID)")
35+
bgpEnableProjectCmd.Flags().StringVar(&config.UseCase, "use-case", "", "Use case for BGP")
36+
bgpEnableProjectCmd.Flags().IntVar(&config.ASN, "asn", 65000, "Local ASN")
37+
bgpEnableProjectCmd.Flags().StringVar(&config.DeploymentType, "deployment-type", "", "Deployment type (local, global)")
38+
bgpEnableProjectCmd.Flags().StringVar(&config.MD5, "md5", "", "BGP Password")
7039

7140
_ = bgpEnableProjectCmd.MarkFlagRequired("project-id")
7241
_ = bgpEnableProjectCmd.MarkFlagRequired("asn")
7342
_ = bgpEnableProjectCmd.MarkFlagRequired("deployment-type")
43+
7444
return bgpEnableProjectCmd
7545
}
46+
47+
func enableBGP(c *Client, config *BgpConfig) error {
48+
req := metal.BgpConfigRequestInput{
49+
UseCase: &config.UseCase,
50+
Asn: int32(config.ASN),
51+
DeploymentType: metal.BgpConfigRequestInputDeploymentType(config.DeploymentType),
52+
Md5: &config.MD5,
53+
}
54+
55+
p, err := c.BGPConfigService.RequestBgpConfig(context.Background(), config.ProjectID).BgpConfigRequestInput(req).Execute()
56+
if err != nil {
57+
return fmt.Errorf("error enabling BGP for project %s: %w", config.ProjectID, err)
58+
}
59+
60+
data := [][]string{{config.ProjectID, config.UseCase, strconv.Itoa(config.ASN), config.DeploymentType}}
61+
header := []string{"ID", "UseCase", "ASN", "DeploymentType"}
62+
return c.Out.Output(p, header, &data)
63+
}

0 commit comments

Comments
 (0)