|
| 1 | +package vrf |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "strconv" |
| 7 | + "strings" |
| 8 | + |
| 9 | + metal "github.com/equinix-labs/metal-go/metal/v1" |
| 10 | + "github.com/spf13/cobra" |
| 11 | +) |
| 12 | + |
| 13 | +func (c *Client) Create() *cobra.Command { |
| 14 | + var ( |
| 15 | + projectID string |
| 16 | + metro string |
| 17 | + name string |
| 18 | + description string |
| 19 | + ipRanges []string |
| 20 | + localASN int32 |
| 21 | + tags []string |
| 22 | + ) |
| 23 | + |
| 24 | + // createVRFCmd represents the creatVRF command |
| 25 | + createVRFCmd := &cobra.Command{ |
| 26 | + Use: "create vrf <my_vrf> [-m <metro_code>] [-AS int] [-I str] [-d <description>]", |
| 27 | + Short: "Creates a Virtual Routing and Forwarding(VRF) for a specified project.", |
| 28 | + Long: "Creates a Creates a Virtual Routing and Forwarding(VRF) for a specified project.", |
| 29 | + Example: ` # Creates an Creates a Virtual Routing and Forwarding(VRF) for a specified project. |
| 30 | + |
| 31 | + metal vrf create [-p <project_id] [-d <description>] [-m <metro>] [-n <name>] [-a <localASN>] [-r <IPranges>] [-t <tags> ]`, |
| 32 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 33 | + cmd.SilenceUsage = true |
| 34 | + |
| 35 | + req := metal.VrfCreateInput{ |
| 36 | + Metro: metro, |
| 37 | + Name: name, |
| 38 | + IpRanges: ipRanges, |
| 39 | + LocalAsn: &localASN, |
| 40 | + Tags: tags, |
| 41 | + Description: &description, |
| 42 | + } |
| 43 | + |
| 44 | + inc := []string{} |
| 45 | + exc := []string{} |
| 46 | + vrfRequest, _, err := c.Service.CreateVrf(context.Background(), projectID).VrfCreateInput(req).Exclude(c.Servicer.Excludes(exc)).Include(c.Servicer.Includes(inc)).Execute() |
| 47 | + if err != nil { |
| 48 | + return fmt.Errorf("Could not create VRF: %w", err) |
| 49 | + } |
| 50 | + |
| 51 | + data := make([][]string, 1) |
| 52 | + |
| 53 | + // This output block below is probably incorrect but leaving it for now for testing later. |
| 54 | + data[0] = []string{vrfRequest.GetName(), vrfRequest.Metro.GetCode(), vrfRequest.GetDescription(), strconv.Itoa(int(vrfRequest.GetLocalAsn())), strings.Join(vrfRequest.GetIpRanges(), ","), vrfRequest.GetCreatedAt().String()} |
| 55 | + header := []string{"Name", "Metro", "Description", "LocalASN", "IPrange", "Created"} |
| 56 | + |
| 57 | + return c.Out.Output(vrfRequest, header, &data) |
| 58 | + }, |
| 59 | + } |
| 60 | + |
| 61 | + createVRFCmd.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.") |
| 62 | + createVRFCmd.Flags().StringSliceVarP(&tags, "tags", "t", []string{}, `Adds or updates the tags for the connection --tags="tag1,tag2".`) |
| 63 | + createVRFCmd.Flags().StringVarP(&name, "name", "n", "", "Name of the VRF") |
| 64 | + createVRFCmd.Flags().StringVarP(&description, "description", "d", "", "Description of the VRF.") |
| 65 | + |
| 66 | + createVRFCmd.Flags().StringVarP(&metro, "metro", "m", "", "The UUID (or metro code) for the Metro in which to create this VRF.") |
| 67 | + createVRFCmd.Flags().Int32VarP(&localASN, "local-asn", "a", 0, "Local ASN for the VRF") |
| 68 | + createVRFCmd.Flags().StringSliceVarP(&ipRanges, "ip-ranges", "r", []string{}, "A list of CIDR network addresses. Like [10.0.0.0/16, 2001:d78::/56]. IPv4 blocks must be between /8 and /29 in size. IPv6 blocks must be between /56 and /64.") |
| 69 | + |
| 70 | + // making them all required here |
| 71 | + _ = createVRFCmd.MarkFlagRequired("name") |
| 72 | + _ = createVRFCmd.MarkFlagRequired("metro") |
| 73 | + _ = createVRFCmd.MarkFlagRequired("local-asn") |
| 74 | + _ = createVRFCmd.MarkFlagRequired("IPrange") |
| 75 | + |
| 76 | + return createVRFCmd |
| 77 | +} |
0 commit comments