From 97fa5818454bcbf1954777f12820008a0bd4be51 Mon Sep 17 00:00:00 2001 From: Vasubabu <3358152+vasubabu@users.noreply.github.com> Date: Thu, 3 Aug 2023 20:44:38 +0530 Subject: [PATCH] Added support for Metal Interconnections --- cmd/cli.go | 2 + docs/metal.md | 1 + docs/metal_interconnections.md | 35 +++++++++ docs/metal_interconnections_create.md | 50 ++++++++++++ docs/metal_interconnections_get.md | 44 +++++++++++ internal/interconnections/create.go | 78 +++++++++++++++++++ internal/interconnections/interconnections.go | 71 +++++++++++++++++ internal/interconnections/retrieve.go | 68 ++++++++++++++++ 8 files changed, 349 insertions(+) create mode 100644 docs/metal_interconnections.md create mode 100644 docs/metal_interconnections_create.md create mode 100644 docs/metal_interconnections_get.md create mode 100644 internal/interconnections/create.go create mode 100644 internal/interconnections/interconnections.go create mode 100644 internal/interconnections/retrieve.go diff --git a/cmd/cli.go b/cmd/cli.go index 41ee8629..a245d30e 100644 --- a/cmd/cli.go +++ b/cmd/cli.go @@ -15,6 +15,7 @@ import ( "github.com/equinix/metal-cli/internal/gateway" "github.com/equinix/metal-cli/internal/hardware" initPkg "github.com/equinix/metal-cli/internal/init" + "github.com/equinix/metal-cli/internal/interconnections" "github.com/equinix/metal-cli/internal/ips" "github.com/equinix/metal-cli/internal/metros" "github.com/equinix/metal-cli/internal/organizations" @@ -93,5 +94,6 @@ func (cli *Cli) RegisterCommands(client *root.Client) { twofa.NewClient(client, cli.Outputer).NewCommand(), gateway.NewClient(client, cli.Outputer).NewCommand(), ports.NewClient(client, cli.Outputer).NewCommand(), + interconnections.NewClient(client, cli.Outputer).NewCommand(), ) } diff --git a/docs/metal.md b/docs/metal.md index b20f223a..21e013ee 100644 --- a/docs/metal.md +++ b/docs/metal.md @@ -36,6 +36,7 @@ Command line interface for Equinix Metal * [metal gateway](metal_gateway.md) - Metal Gateway operations: create, delete, and retrieve. * [metal hardware-reservation](metal_hardware-reservation.md) - Hardware reservation operations: get, move. * [metal init](metal_init.md) - Create a configuration file. +* [metal interconnections](metal_interconnections.md) - interconnections operations: get. * [metal ip](metal_ip.md) - IP address, reservations, and assignment operations: assign, unassign, remove, available, request and get. * [metal metros](metal_metros.md) - Metro operations: get. * [metal operating-systems](metal_operating-systems.md) - Operating system operations: get. diff --git a/docs/metal_interconnections.md b/docs/metal_interconnections.md new file mode 100644 index 00000000..31326263 --- /dev/null +++ b/docs/metal_interconnections.md @@ -0,0 +1,35 @@ +## metal interconnections + +interconnections operations: get. + +### Synopsis + +Get information on Metro locations. For more information on https://deploy.equinix.com/developers/docs/metal/interconnections. + +### Options + +``` + -h, --help help for interconnections +``` + +### Options inherited from parent commands + +``` + --config string Path to JSON or YAML configuration file + --exclude strings Comma separated Href references to collapse in results, may be dotted three levels deep + --filter stringArray Filter 'get' actions with name value pairs. Filter is not supported by all resources and is implemented as request query parameters. + --http-header strings Headers to add to requests (in format key=value) + --include strings Comma separated Href references to expand in results, may be dotted three levels deep + -o, --output string Output format (*table, json, yaml). env output formats are (*sh, terraform, capp). + --search string Search keyword for use in 'get' actions. Search is not supported by all resources. + --sort-by string Sort fields for use in 'get' actions. Sort is not supported by all resources. + --sort-dir string Sort field direction for use in 'get' actions. Sort is not supported by all resources. + --token string Metal API Token (METAL_AUTH_TOKEN) +``` + +### SEE ALSO + +* [metal](metal.md) - Command line interface for Equinix Metal +* [metal interconnections create](metal_interconnections_create.md) - Creates an interconnection. +* [metal interconnections get](metal_interconnections_get.md) - Retrieves a list of interconnections. + diff --git a/docs/metal_interconnections_create.md b/docs/metal_interconnections_create.md new file mode 100644 index 00000000..146ad835 --- /dev/null +++ b/docs/metal_interconnections_create.md @@ -0,0 +1,50 @@ +## metal interconnections create + +Creates an interconnection. + +### Synopsis + +Creates a new interconnection as per the organization ID or project ID + +``` +metal interconnections create -n [-m ] [-r ] [-t ] [-p ] | [-O ] [flags] +``` + +### Examples + +``` + # Creates a new interconnection named "it-interconnection": + metal interconnections create -n [-m ] [-r ] [-t type ] [-p ] | [-O ] +``` + +### Options + +``` + -h, --help help for create + -m, --metro string metro in the interconnection + -n, --name string Name of the interconnection + -O, --organizationID string Org ID + -p, --projectID string project ID + -r, --redundancy string Website URL of the organization. + -t, --type string type of of interconnection. +``` + +### Options inherited from parent commands + +``` + --config string Path to JSON or YAML configuration file + --exclude strings Comma separated Href references to collapse in results, may be dotted three levels deep + --filter stringArray Filter 'get' actions with name value pairs. Filter is not supported by all resources and is implemented as request query parameters. + --http-header strings Headers to add to requests (in format key=value) + --include strings Comma separated Href references to expand in results, may be dotted three levels deep + -o, --output string Output format (*table, json, yaml). env output formats are (*sh, terraform, capp). + --search string Search keyword for use in 'get' actions. Search is not supported by all resources. + --sort-by string Sort fields for use in 'get' actions. Sort is not supported by all resources. + --sort-dir string Sort field direction for use in 'get' actions. Sort is not supported by all resources. + --token string Metal API Token (METAL_AUTH_TOKEN) +``` + +### SEE ALSO + +* [metal interconnections](metal_interconnections.md) - interconnections operations: get. + diff --git a/docs/metal_interconnections_get.md b/docs/metal_interconnections_get.md new file mode 100644 index 00000000..4c913b2c --- /dev/null +++ b/docs/metal_interconnections_get.md @@ -0,0 +1,44 @@ +## metal interconnections get + +Retrieves a list of interconnections. + +### Synopsis + +Retrieves a list of interconnections available to the default Organizations and by project ID + +``` +metal interconnections get [flags] +``` + +### Examples + +``` + # Lists interconnections available to the default OrganizationID OR by project ID: + metal interconnections get +``` + +### Options + +``` + -h, --help help for get +``` + +### Options inherited from parent commands + +``` + --config string Path to JSON or YAML configuration file + --exclude strings Comma separated Href references to collapse in results, may be dotted three levels deep + --filter stringArray Filter 'get' actions with name value pairs. Filter is not supported by all resources and is implemented as request query parameters. + --http-header strings Headers to add to requests (in format key=value) + --include strings Comma separated Href references to expand in results, may be dotted three levels deep + -o, --output string Output format (*table, json, yaml). env output formats are (*sh, terraform, capp). + --search string Search keyword for use in 'get' actions. Search is not supported by all resources. + --sort-by string Sort fields for use in 'get' actions. Sort is not supported by all resources. + --sort-dir string Sort field direction for use in 'get' actions. Sort is not supported by all resources. + --token string Metal API Token (METAL_AUTH_TOKEN) +``` + +### SEE ALSO + +* [metal interconnections](metal_interconnections.md) - interconnections operations: get. + diff --git a/internal/interconnections/create.go b/internal/interconnections/create.go new file mode 100644 index 00000000..cc9c02a7 --- /dev/null +++ b/internal/interconnections/create.go @@ -0,0 +1,78 @@ +// Copyright © 2018 Jasmin Gacic +// +// 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 interconnections + +import ( + "context" + "fmt" + + metal "github.com/equinix-labs/metal-go/metal/v1" + "github.com/spf13/cobra" +) + +func (c *Client) Create() *cobra.Command { + var name, metro, redundancy, connType, projectID, organizationID string + + createInterconnectionsCmd := &cobra.Command{ + Use: `create -n [-m ] [-r ] [-t ] [-p ] | [-O ]`, + Short: "Creates an interconnection.", + Long: "Creates a new interconnection as per the organization ID or project ID ", + Example: ` # Creates a new interconnection named "it-interconnection": + metal interconnections create -n [-m ] [-r ] [-t type ] [-p ] | [-O ]`, + + RunE: func(cmd *cobra.Command, args []string) error { + cmd.SilenceUsage = true + var request metal.ApiCreateOrganizationInterconnectionRequest + interconnectionCreateInput := metal.NewInterconnectionCreateInput(metro, name, redundancy, connType) + + if projectID != "" { + request = c.Service.CreateOrganizationInterconnection(context.Background(), projectID).InterconnectionCreateInput(*interconnectionCreateInput) + } else { + request = c.Service.CreateOrganizationInterconnection(context.Background(), organizationID).InterconnectionCreateInput(*interconnectionCreateInput) + } + + interconn, _, err := request.Execute() + if err != nil { + return fmt.Errorf("could not create interconnections: %w", err) + } + + data := make([][]string, 1) + + data[0] = []string{interconn.GetId(), interconn.GetName(), interconn.GetType(), interconn.GetCreatedAt().String()} + header := []string{"ID", "Name", "Type", "Created"} + + return c.Out.Output(interconn, header, &data) + }, + } + + createInterconnectionsCmd.Flags().StringVarP(&name, "name", "n", "", "Name of the interconnection") + createInterconnectionsCmd.Flags().StringVarP(&metro, "metro", "m", "", "metro in the interconnection") + createInterconnectionsCmd.Flags().StringVarP(&redundancy, "redundancy", "r", "", "Website URL of the organization.") + createInterconnectionsCmd.Flags().StringVarP(&connType, "type", "t", "", "type of of interconnection.") + createInterconnectionsCmd.Flags().StringVarP(&connType, "projectID", "p", "", "project ID") + createInterconnectionsCmd.Flags().StringVarP(&connType, "organizationID", "O", "", "Org ID") + + _ = createInterconnectionsCmd.MarkFlagRequired("name") + _ = createInterconnectionsCmd.MarkFlagRequired("metro") + _ = createInterconnectionsCmd.MarkFlagRequired("redundancy") + _ = createInterconnectionsCmd.MarkFlagRequired("type") + return createInterconnectionsCmd +} diff --git a/internal/interconnections/interconnections.go b/internal/interconnections/interconnections.go new file mode 100644 index 00000000..145ed9c6 --- /dev/null +++ b/internal/interconnections/interconnections.go @@ -0,0 +1,71 @@ +// Copyright © 2018 Jasmin Gacic +// +// 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 interconnections + +import ( + metal "github.com/equinix-labs/metal-go/metal/v1" + "github.com/equinix/metal-cli/internal/outputs" + "github.com/spf13/cobra" +) + +type Client struct { + Servicer Servicer + Service metal.InterconnectionsApiService + Out outputs.Outputer +} + +func (c *Client) NewCommand() *cobra.Command { + cmd := &cobra.Command{ + Use: `interconnections`, + Aliases: []string{"conn"}, + Short: "interconnections operations: get.", + Long: "Get information on Metro locations. For more information on https://deploy.equinix.com/developers/docs/metal/interconnections.", + PersistentPreRun: func(cmd *cobra.Command, args []string) { + if root := cmd.Root(); root != nil { + if root.PersistentPreRun != nil { + root.PersistentPreRun(cmd, args) + } + } + c.Service = *c.Servicer.MetalAPI(cmd).InterconnectionsApi + }, + } + + cmd.AddCommand( + c.Retrieve(), + c.Create(), + ) + + return cmd +} + +type Servicer interface { + MetalAPI(*cobra.Command) *metal.APIClient + Filters() map[string]string + Includes(defaultIncludes []string) (incl []string) + Excludes(defaultExcludes []string) (excl []string) +} + +func NewClient(s Servicer, out outputs.Outputer) *Client { + return &Client{ + Servicer: s, + Out: out, + } +} diff --git a/internal/interconnections/retrieve.go b/internal/interconnections/retrieve.go new file mode 100644 index 00000000..a5f221ed --- /dev/null +++ b/internal/interconnections/retrieve.go @@ -0,0 +1,68 @@ +// Copyright © 2018 Jasmin Gacic +// +// 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 interconnections + +import ( + "context" + "fmt" + + metal "github.com/equinix-labs/metal-go/metal/v1" + "github.com/spf13/cobra" +) + +func (c *Client) Retrieve() *cobra.Command { + // metrosCmd represents the metros command + var projectID, organizationID string + retrieveInterconnectionsCmd := &cobra.Command{ + Use: "get", + Aliases: []string{"list"}, + Short: "Retrieves a list of interconnections.", + Long: "Retrieves a list of interconnections available to the default Organizations and by project ID", + Example: ` # Lists interconnections available to the default OrganizationID OR by project ID: + metal interconnections get`, + + RunE: func(cmd *cobra.Command, args []string) error { + cmd.SilenceUsage = true + var request metal.ApiOrganizationListInterconnectionsRequest + + if projectID != "" { + request = c.Service.OrganizationListInterconnections(context.Background(), projectID) + } else { + request = c.Service.OrganizationListInterconnections(context.Background(), organizationID) + } + + interConnectionsList, _, err := request.Execute() + if err != nil { + return fmt.Errorf("could not list metros: %w", err) + } + interConnections := interConnectionsList.GetInterconnections() + data := make([][]string, len(interConnections)) + + for i, interConn := range interConnections { + data[i] = []string{interConn.GetId(), interConn.GetName(), interConn.GetType(), interConn.GetCreatedAt().String()} + } + header := []string{"ID", "Name", "Code", "Type", "Created"} + + return c.Out.Output(interConnections, header, &data) + }, + } + return retrieveInterconnectionsCmd +}