diff --git a/docs/metal_vrf_get.md b/docs/metal_vrf_get.md index e5ddcc29..96688177 100644 --- a/docs/metal_vrf_get.md +++ b/docs/metal_vrf_get.md @@ -26,7 +26,7 @@ metal vrf get -p [flags] -h, --help help for get -m, --metro string Filter by Metro ID (uuid) or Metro Code -p, --project-id string 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. - -v, --vrfID string VRF UUID + -v, --vrfID string Specify the VRF UUID. ``` ### Options inherited from parent commands diff --git a/internal/vrf/retrieve.go b/internal/vrf/retrieve.go index cabce9ac..64112880 100644 --- a/internal/vrf/retrieve.go +++ b/internal/vrf/retrieve.go @@ -6,7 +6,6 @@ import ( "strconv" "strings" - metal "github.com/equinix/equinix-sdk-go/services/metalv1" "github.com/spf13/cobra" ) @@ -33,26 +32,27 @@ func (c *Client) Retrieve() *cobra.Command { cmd.SilenceUsage = true inc := []string{} exc := []string{} - var ( - vrfsList *metal.VrfList - vrf *metal.Vrf - vrfs []metal.Vrf - err error - ) + vrfIdFlag, _ := cmd.Flags().GetString("vrfID") + // It's a required flag in case of get VRF by ID. if vrfIdFlag != "" { - vrf, _, err = c.Service.FindVrfById(context.Background(), vrfID).Include(c.Servicer.Includes(inc)).Exclude(c.Servicer.Excludes(exc)).Execute() + vrf, _, err := c.Service.FindVrfById(context.Background(), vrfID).Include(c.Servicer.Includes(inc)).Exclude(c.Servicer.Excludes(exc)).Execute() if err != nil { return fmt.Errorf("error when calling `VRFsApi.FindVrfById``: %w", err) } - vrfs[0] = *vrf - } else { - vrfsList, _, err = c.Service.FindVrfs(context.Background(), projectID).Metro(metro).Include(c.Servicer.Includes(inc)).Exclude(c.Servicer.Excludes(exc)).Execute() - if err != nil { - return fmt.Errorf("error when calling `VRFsApi.FindVrfs``: %w", err) - } - vrfs = vrfsList.GetVrfs() + data := make([][]string, 1) + + data[0] = []string{vrf.GetId(), vrf.GetName(), vrf.GetDescription(), strings.Join(vrf.GetIpRanges(), ","), strconv.FormatInt(int64(vrf.GetLocalAsn()), 10), vrf.CreatedAt.String()} + header := []string{"ID", "Name", "Description", "IPRanges", "LocalASN", "Created"} + + return c.Out.Output(vrf, header, &data) + + } + vrfsList, _, err := c.Service.FindVrfs(context.Background(), projectID).Metro(metro).Include(c.Servicer.Includes(inc)).Exclude(c.Servicer.Excludes(exc)).Execute() + if err != nil { + return fmt.Errorf("error when calling `VRFsApi.FindVrfs``: %w", err) } + vrfs := vrfsList.GetVrfs() data := make([][]string, len(vrfs)) @@ -67,7 +67,7 @@ func (c *Client) Retrieve() *cobra.Command { } retrieveVrfsCmd.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.") retrieveVrfsCmd.Flags().StringVarP(&metro, "metro", "m", "", "Filter by Metro ID (uuid) or Metro Code") - retrieveVrfsCmd.Flags().StringVarP(&vrfID, "vrfID", "v", "", "VRF UUID") + retrieveVrfsCmd.Flags().StringVarP(&vrfID, "vrfID", "v", "", "Specify the VRF UUID.") _ = retrieveVrfsCmd.MarkFlagRequired("project-id") diff --git a/test/e2e/vrfstest/vrf_test.go b/test/e2e/vrfstest/vrf_test.go new file mode 100644 index 00000000..394df3c7 --- /dev/null +++ b/test/e2e/vrfstest/vrf_test.go @@ -0,0 +1,145 @@ +package vrfstest + +import ( + "strings" + "testing" + + root "github.com/equinix/metal-cli/internal/cli" + outputPkg "github.com/equinix/metal-cli/internal/outputs" + "github.com/equinix/metal-cli/internal/vrf" + "github.com/equinix/metal-cli/test/helper" + "github.com/spf13/cobra" +) + +func TestCli_Vrf_Create(t *testing.T) { + subCommand := "vrf" + consumerToken := "" + apiURL := "" + Version := "metal" + rootClient := root.NewClient(consumerToken, apiURL, Version) + randName := helper.GenerateRandomString(5) + + type fields struct { + MainCmd *cobra.Command + Outputer outputPkg.Outputer + } + + tests := []struct { + name string + fields fields + want *cobra.Command + cmdFunc func(*testing.T, *cobra.Command) + }{ + { + name: "vrf-create-test", + fields: fields{ + MainCmd: vrf.NewClient(rootClient, outputPkg.Outputer(&outputPkg.Standard{})).NewCommand(), + Outputer: outputPkg.Outputer(&outputPkg.Standard{}), + }, + want: &cobra.Command{}, + cmdFunc: func(t *testing.T, c *cobra.Command) { + root := c.Root() + projName := "metal-cli-" + randName + "-vrf-create-test" + projectId := helper.CreateTestProject(t, projName) + if projectId.GetId() != "" { + + root.SetArgs([]string{subCommand, "create", "-p", projectId.GetId(), "-m", "da", "-n", projName, "-a", "3456", "-r", "10.0.1.0/24"}) + + out := helper.ExecuteAndCaptureOutput(t, root) + + if !strings.Contains(string(out[:]), projName) { + t.Error("expected output should include " + projName + ", in the out string ") + } + } + }, + }, + { + name: "vrf-delete-test", + fields: fields{ + MainCmd: vrf.NewClient(rootClient, outputPkg.Outputer(&outputPkg.Standard{})).NewCommand(), + Outputer: outputPkg.Outputer(&outputPkg.Standard{}), + }, + want: &cobra.Command{}, + cmdFunc: func(t *testing.T, c *cobra.Command) { + root := c.Root() + + projName := "metal-cli-" + randName + "-vrf-delete-test" + projectId := helper.CreateTestProject(t, projName) + + if projectId.GetId() != "" { + vrf := helper.CreateTestVrfs(t, projectId.GetId(), projName) + if vrf.GetId() != "" { + root.SetArgs([]string{subCommand, "delete", "-i", vrf.GetId(), "-f"}) + out := helper.ExecuteAndCaptureOutput(t, root) + + if !strings.Contains(string(out[:]), "VRF deletion initiated. Please check 'metal vrf get -i "+vrf.GetId()+" ' for status") { + t.Error("expected output should include VRF deletion initiated. Please check 'metal vrf get -i " + vrf.GetId() + " ' for status, in the out string") + } + } + } + }, + }, + { + name: "vrf-list-test", + fields: fields{ + MainCmd: vrf.NewClient(rootClient, outputPkg.Outputer(&outputPkg.Standard{})).NewCommand(), + Outputer: outputPkg.Outputer(&outputPkg.Standard{}), + }, + want: &cobra.Command{}, + cmdFunc: func(t *testing.T, c *cobra.Command) { + root := c.Root() + + projName := "metal-cli-" + randName + "-vrf-list-test" + projectId := helper.CreateTestProject(t, projName) + + if projectId.GetId() != "" { + vrf := helper.CreateTestVrfs(t, projectId.GetId(), projName) + + root.SetArgs([]string{subCommand, "get", "-p", projectId.GetId()}) + out := helper.ExecuteAndCaptureOutput(t, root) + + if !strings.Contains(string(out[:]), vrf.GetId()) && + !strings.Contains(string(out[:]), projName) { + t.Error("expected output should include " + vrf.GetId() + ", in the out string ") + } + } + }, + }, + { + name: "vrf-get-test", + fields: fields{ + MainCmd: vrf.NewClient(rootClient, outputPkg.Outputer(&outputPkg.Standard{})).NewCommand(), + Outputer: outputPkg.Outputer(&outputPkg.Standard{}), + }, + want: &cobra.Command{}, + cmdFunc: func(t *testing.T, c *cobra.Command) { + root := c.Root() + + projName := "metal-cli-" + randName + "-vrf-get-test" + projectId := helper.CreateTestProject(t, projName) + + if projectId.GetId() != "" { + vrf := helper.CreateTestVrfs(t, projectId.GetId(), projName) + + if vrf.GetId() != "" { + root.SetArgs([]string{subCommand, "get", "-p", projectId.GetId(), "-v", vrf.GetId()}) + out := helper.ExecuteAndCaptureOutput(t, root) + + if !strings.Contains(string(out[:]), vrf.GetId()) && + !strings.Contains(string(out[:]), projName) { + t.Error("expected output should include " + vrf.GetId() + ", in the out string ") + } + } + } + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + rootCmd := rootClient.NewCommand() + rootCmd.AddCommand(tt.fields.MainCmd) + tt.cmdFunc(t, tt.fields.MainCmd) + }) + } +} diff --git a/test/helper/helper.go b/test/helper/helper.go index 30465074..1091c263 100644 --- a/test/helper/helper.go +++ b/test/helper/helper.go @@ -452,3 +452,38 @@ func ExecuteAndCaptureOutput(t *testing.T, root *cobra.Command) []byte { return out } + +//nolint:staticcheck +func CreateTestVrfs(t *testing.T, projectId, name string) *metalv1.Vrf { + t.Helper() + TestApiClient := TestClient() + + var IpRanges []string + + vrfCreateInput := *metalv1.NewVrfCreateInput("da", name) + vrfCreateInput.SetLocalAsn(5678) + IpRanges = append(IpRanges, "10.10.1.0/24") + vrfCreateInput.SetIpRanges(IpRanges) + + resp, _, err := TestApiClient.VRFsApi.CreateVrf(context.Background(), projectId).VrfCreateInput(vrfCreateInput).Execute() + if err != nil { + t.Fatalf("Error when calling `VRFsApi.CreateVrf``: %v\n", err) + } + + t.Cleanup(func() { + CleanTestVrfs(t, resp.GetId()) + }) + + return resp +} + +//nolint:staticcheck +func CleanTestVrfs(t *testing.T, vrfId string) { + t.Helper() + TestApiClient := TestClient() + + resp, err := TestApiClient.VRFsApi.DeleteVrf(context.Background(), vrfId).Execute() + if err != nil && resp.StatusCode != http.StatusNotFound { + t.Fatalf("Error when calling `VRFsApi.DeleteVrf`` for %v: %v\n", vrfId, err) + } +}