Skip to content

Commit bb40a73

Browse files
authored
fix: Updated metal-go client for sub-command facilities (#442)
Co-authored-by: codinja1188 <[email protected]>
1 parent 10d7143 commit bb40a73

File tree

3 files changed

+92
-50
lines changed

3 files changed

+92
-50
lines changed

internal/facilities/facility.go

+7-25
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,14 @@
1-
// Copyright © 2018 Jasmin Gacic <[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-
211
package facilities
222

233
import (
4+
metal "github.com/equinix/equinix-sdk-go/services/metalv1"
245
"github.com/equinix/metal-cli/internal/outputs"
25-
"github.com/packethost/packngo"
266
"github.com/spf13/cobra"
277
)
288

299
type Client struct {
3010
Servicer Servicer
31-
Service packngo.FacilityService
11+
Service *metal.FacilitiesApiService
3212
Out outputs.Outputer
3313
}
3414

@@ -44,7 +24,7 @@ func (c *Client) NewCommand() *cobra.Command {
4424
root.PersistentPreRun(cmd, args)
4525
}
4626
}
47-
c.Service = c.Servicer.API(cmd).Facilities
27+
c.Service = c.Servicer.MetalAPI(cmd).FacilitiesApi
4828
},
4929
}
5030

@@ -55,8 +35,10 @@ func (c *Client) NewCommand() *cobra.Command {
5535
}
5636

5737
type Servicer interface {
58-
API(*cobra.Command) *packngo.Client
59-
ListOptions(defaultIncludes, defaultExcludes []string) *packngo.ListOptions
38+
MetalAPI(*cobra.Command) *metal.APIClient
39+
Filters() map[string]string
40+
Includes(defaultIncludes []string) (incl []string)
41+
Excludes(defaultExcludes []string) (excl []string)
6042
}
6143

6244
func NewClient(s Servicer, out outputs.Outputer) *Client {

internal/facilities/retrieve.go

+24-25
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,17 @@
1-
// Copyright © 2018 Jasmin Gacic <[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-
211
package facilities
222

233
import (
4+
"context"
245
"fmt"
256
"strings"
267

8+
"github.com/equinix/equinix-sdk-go/services/metalv1"
279
"github.com/spf13/cobra"
2810
)
2911

3012
func (c *Client) Retrieve() *cobra.Command {
31-
return &cobra.Command{
13+
14+
retrieveFacilitesCmd := &cobra.Command{
3215
Use: `get`,
3316
Aliases: []string{"list"},
3417
Short: "Retrieves a list of facilities.",
@@ -37,23 +20,39 @@ func (c *Client) Retrieve() *cobra.Command {
3720
metal facilities get`,
3821

3922
RunE: func(cmd *cobra.Command, args []string) error {
23+
var (
24+
facilityList *metalv1.FacilityList
25+
err error
26+
)
4027
cmd.SilenceUsage = true
41-
facilities, _, err := c.Service.List(c.Servicer.ListOptions(nil, nil))
28+
29+
inc := []metalv1.FindFacilitiesIncludeParameterInner{}
30+
exc := []metalv1.FindFacilitiesIncludeParameterInner{}
31+
facilityList, _, err = c.Service.FindFacilities(context.Background()).Include(inc).Exclude(exc).Execute()
4232
if err != nil {
4333
return fmt.Errorf("Could not list Facilities: %w", err)
4434
}
35+
36+
facilities := facilityList.GetFacilities()
4537
data := make([][]string, len(facilities))
4638

4739
for i, facility := range facilities {
4840
var metro string
4941
if facility.Metro != nil {
50-
metro = facility.Metro.Code
42+
metro = facility.Metro.GetCode()
5143
}
52-
data[i] = []string{facility.Name, facility.Code, metro, strings.Join(facility.Features, ",")}
44+
45+
facilityFeatures := facility.GetFeatures()
46+
var stringFeatures []string
47+
for _, feature := range facilityFeatures {
48+
stringFeatures = append(stringFeatures, string(feature))
49+
}
50+
data[i] = []string{facility.GetName(), facility.GetCode(), metro, strings.Join(([]string)(stringFeatures), ",")}
5351
}
5452
header := []string{"Name", "Code", "Metro", "Features"}
55-
5653
return c.Out.Output(facilities, header, &data)
5754
},
5855
}
56+
57+
return retrieveFacilitesCmd
5958
}
+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package facilitiestest
2+
3+
import (
4+
"strings"
5+
"testing"
6+
7+
root "github.com/equinix/metal-cli/internal/cli"
8+
"github.com/equinix/metal-cli/internal/facilities"
9+
outputPkg "github.com/equinix/metal-cli/internal/outputs"
10+
"github.com/spf13/cobra"
11+
12+
"github.com/equinix/metal-cli/test/helper"
13+
)
14+
15+
func TestFacilities_Retrieve(t *testing.T) {
16+
subCommand := "facilities"
17+
rootClient := root.NewClient(helper.ConsumerToken, helper.URL, helper.Version)
18+
randName := helper.GenerateRandomString(5)
19+
20+
tests := []struct {
21+
name string
22+
cmd *cobra.Command
23+
want *cobra.Command
24+
cmdFunc func(*testing.T, *cobra.Command)
25+
}{
26+
{
27+
name: "retrieve All facilities",
28+
cmd: facilities.NewClient(rootClient, outputPkg.Outputer(&outputPkg.Standard{})).NewCommand(),
29+
want: &cobra.Command{},
30+
cmdFunc: func(t *testing.T, c *cobra.Command) {
31+
root := c.Root()
32+
33+
projName := "metal-cli-" + randName + "-facilities-get-all-test"
34+
projectId := helper.CreateTestProject(t, projName)
35+
36+
if projectId.GetId() != "" {
37+
38+
root.SetArgs([]string{subCommand, "get"})
39+
40+
out := helper.ExecuteAndCaptureOutput(t, root)
41+
42+
if !strings.Contains(string(out[:]), "NAME") &&
43+
!strings.Contains(string(out[:]), "CODE") &&
44+
!strings.Contains(string(out[:]), "METRO") &&
45+
!strings.Contains(string(out[:]), "baremetal,backend_transfer,layer_2,global_ipv4,ibx") &&
46+
!strings.Contains(string(out[:]), "Singapore") {
47+
t.Error("expected output should include NAME CODE METRO Singapore and baremetal,backend_transfer,layer_2,global_ipv4,ibx, in the out string ")
48+
}
49+
}
50+
},
51+
},
52+
}
53+
54+
for _, tt := range tests {
55+
t.Run(tt.name, func(t *testing.T) {
56+
rootCmd := rootClient.NewCommand()
57+
rootCmd.AddCommand(tt.cmd)
58+
tt.cmdFunc(t, tt.cmd)
59+
})
60+
}
61+
}

0 commit comments

Comments
 (0)