-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Updated metal-go client for sub-command project bgp sessions
- Loading branch information
1 parent
1994e1e
commit 7e68154
Showing
15 changed files
with
536 additions
and
165 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.