|
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 |
| - |
21 | 1 | package projects
|
22 | 2 |
|
23 | 3 | import (
|
| 4 | + "context" |
24 | 5 | "fmt"
|
25 | 6 | "strconv"
|
26 | 7 |
|
27 |
| - "github.com/packethost/packngo" |
| 8 | + metal "github.com/equinix-labs/metal-go/metal/v1" |
28 | 9 | "github.com/spf13/cobra"
|
29 | 10 | )
|
30 | 11 |
|
| 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 | + |
31 | 21 | 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 | + |
37 | 24 | 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`, |
43 | 29 | 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) |
62 | 31 | },
|
63 | 32 | }
|
64 | 33 |
|
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") |
70 | 39 |
|
71 | 40 | _ = bgpEnableProjectCmd.MarkFlagRequired("project-id")
|
72 | 41 | _ = bgpEnableProjectCmd.MarkFlagRequired("asn")
|
73 | 42 | _ = bgpEnableProjectCmd.MarkFlagRequired("deployment-type")
|
| 43 | + |
74 | 44 | return bgpEnableProjectCmd
|
75 | 45 | }
|
| 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