Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Updated metal-go client for sub-commands hardware reservations #288

Merged
merged 1 commit into from
Jan 16, 2024

Conversation

codinja1188
Copy link
Contributor

@codinja1188 codinja1188 commented Jun 20, 2023

Breakout from #270

What this PR does / why we need it:

This PR replaces packngo with metal-go for all interactions with the Equinix Metal API specifically for hardware sub commands

DISCUSSION POINTS:

Fixes #46

@cprivitere
Copy link
Member

cprivitere commented Jun 30, 2023

This has an extra commit in it and needs to be rebased.

internal/hardware/retrieve.go Outdated Show resolved Hide resolved
internal/hardware/retrieve.go Outdated Show resolved Hide resolved
@cprivitere
Copy link
Member

This has a conflict and needs a rebase.

@displague displague changed the title Updated metal-go client for sub-commands hardware Updated metal-go client for sub-commands hardware reservations Oct 16, 2023
@displague displague changed the title Updated metal-go client for sub-commands hardware reservations chore: Updated metal-go client for sub-commands hardware reservations Nov 14, 2023
want: &cobra.Command{},
cmdFunc: func(t *testing.T, c *cobra.Command) {
root := c.Root()
root.SetArgs([]string{subCommand, "get"})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like we need -i or -p and an ID here. Since we won't have any hardware reservations available to this E2E account, we should use a mocked client that will always return a stub reservation.

=== RUN   TestCli_Hardware
=== RUN   TestCli_Hardware/get
Error: either id or project-id should be set
Usage:
  metal hardware-reservation get [-p <project_id>] | [-i <hardware_reservation_id>] [flags]

Aliases:
  get, list

Examples:
  # Retrieve all hardware reservations of a project:
  metal hardware-reservations get -p $METAL_PROJECT_ID
  
  # Retrieve the details of a specific hardware reservation:
  metal hardware-reservations get -i 8404b73c-d18f-4190-8c49-20bb17501f88

Flags:
  -h, --help                help for get
  -i, --id string           The UUID of a hardware reservation.
  -p, --project-id string   A project's UUID.

Global Flags:
      --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)

    hardware_test.go:45: either id or project-id should be set
    hardware_test.go:56: expected output should include n3.xlarge.x86, m3.large.x86, s3.xlarge.x86, dc10.
--- FAIL: TestCli_Hardware (0.00s)
    --- FAIL: TestCli_Hardware/get (0.00s)
FAIL

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The terraform provider has an example of setting up a mock API with httptest: https://github.com/equinix/terraform-provider-equinix/blob/main/equinix/resource_metal_device_acc_test.go#L1082-L1116

In this case, you would want to have your mock return a 200 with a JSON string that has the necessary fields. You could also consider the other mocking approaches documented here: https://medium.com/zus-health/mocking-outbound-http-requests-in-go-youre-probably-doing-it-wrong-60373a38d2aa

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated mock client, and response

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test mentioned earlier is still failing with the same error as before:

=== RUN   TestCli_Hardware/get
Error: either id or project-id should be set

@@ -108,6 +108,11 @@ func (c *Client) metalApiConnect(httpClient *http.Client) error {
configuration.Debug = checkEnvForDebug()
configuration.AddDefaultHeader("X-Auth-Token", c.Token())
configuration.UserAgent = fmt.Sprintf("metal-cli/%s %s", c.Version, configuration.UserAgent)
configuration.Servers = metal.ServerConfigurations{
metal.ServerConfiguration{
URL: c.apiURL,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It appears that this value is empty because the E2E tests are failing with an invalid API URL (it does not contain https://api.equinix.com/metal/v1 so requests are being made directly to /path/...)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed this in #432

@codinja1188
Copy link
Contributor Author

Vasubabus-MacBook-Pro:metal-cli vasubabu$ make test
go test -v ./... -timeout 1000s
=== RUN   TestCli_RegisterCommands
=== RUN   TestCli_RegisterCommands/test
--- PASS: TestCli_RegisterCommands (0.00s)
    --- PASS: TestCli_RegisterCommands/test (0.00s)
PASS
ok  	github.com/equinix/metal-cli/cmd	0.513s
?   	github.com/equinix/metal-cli/cmd/metal	[no test files]
?   	github.com/equinix/metal-cli/internal/capacity	[no test files]
?   	github.com/equinix/metal-cli/internal/cli	[no test files]
?   	github.com/equinix/metal-cli/internal/completion	[no test files]
?   	github.com/equinix/metal-cli/internal/devices	[no test files]
?   	github.com/equinix/metal-cli/internal/docs	[no test files]
?   	github.com/equinix/metal-cli/internal/emdocs	[no test files]
?   	github.com/equinix/metal-cli/internal/env	[no test files]
?   	github.com/equinix/metal-cli/internal/events	[no test files]
?   	github.com/equinix/metal-cli/internal/facilities	[no test files]
?   	github.com/equinix/metal-cli/internal/gateway	[no test files]
?   	github.com/equinix/metal-cli/internal/hardware	[no test files]
?   	github.com/equinix/metal-cli/internal/init	[no test files]
?   	github.com/equinix/metal-cli/internal/interconnections	[no test files]
?   	github.com/equinix/metal-cli/internal/ips	[no test files]
?   	github.com/equinix/metal-cli/internal/metros	[no test files]
?   	github.com/equinix/metal-cli/internal/organizations	[no test files]
?   	github.com/equinix/metal-cli/internal/os	[no test files]
?   	github.com/equinix/metal-cli/internal/outputs	[no test files]
?   	github.com/equinix/metal-cli/internal/pagination	[no test files]
?   	github.com/equinix/metal-cli/internal/plans	[no test files]
?   	github.com/equinix/metal-cli/internal/ports	[no test files]
?   	github.com/equinix/metal-cli/internal/projects	[no test files]
?   	github.com/equinix/metal-cli/internal/ssh	[no test files]
?   	github.com/equinix/metal-cli/internal/twofa	[no test files]
?   	github.com/equinix/metal-cli/internal/users	[no test files]
?   	github.com/equinix/metal-cli/internal/vlan	[no test files]
?   	github.com/equinix/metal-cli/internal/vrf	[no test files]
=== RUN   TestCli_Capacity
=== RUN   TestCli_Capacity/get
=== RUN   TestCli_Capacity/get_by_plan_1
=== RUN   TestCli_Capacity/get_by_plan_2
=== RUN   TestCli_Capacity/check_by_multi_metro
=== RUN   TestCli_Capacity/check_by_multi_plan
=== RUN   TestCli_Capacity/check_by_multi_metro_and_plan
--- PASS: TestCli_Capacity (7.67s)
    --- PASS: TestCli_Capacity/get (1.31s)
    --- PASS: TestCli_Capacity/get_by_plan_1 (3.76s)
    --- PASS: TestCli_Capacity/get_by_plan_2 (0.84s)
    --- PASS: TestCli_Capacity/check_by_multi_metro (0.54s)
    --- PASS: TestCli_Capacity/check_by_multi_plan (0.53s)
    --- PASS: TestCli_Capacity/check_by_multi_metro_and_plan (0.69s)
PASS
ok  	github.com/equinix/metal-cli/test/e2e/capacitytest	8.544s
=== RUN   TestCli_Devices_Create_Flags
=== RUN   TestCli_Devices_Create_Flags/create_device
--- PASS: TestCli_Devices_Create_Flags (111.48s)
    --- PASS: TestCli_Devices_Create_Flags/create_device (111.48s)
PASS
ok  	github.com/equinix/metal-cli/test/e2e/devices/devicecreateflagstest	113.107s
=== RUN   TestCli_Devices_Create
=== RUN   TestCli_Devices_Create/create_device
--- PASS: TestCli_Devices_Create (163.77s)
    --- PASS: TestCli_Devices_Create/create_device (163.77s)
PASS
ok  	github.com/equinix/metal-cli/test/e2e/devices/devicecreatetest	165.776s
=== RUN   TestCli_Devices_Get
=== RUN   TestCli_Devices_Get/get_by_device_id
--- PASS: TestCli_Devices_Get (187.03s)
    --- PASS: TestCli_Devices_Get/get_by_device_id (187.03s)
PASS
ok  	github.com/equinix/metal-cli/test/e2e/devices/devicegettest	188.193s
=== RUN   TestCli_Devices_Update
=== RUN   TestCli_Devices_Update/reinstall_device
--- PASS: TestCli_Devices_Update (683.15s)
    --- PASS: TestCli_Devices_Update/reinstall_device (683.15s)
PASS
ok  	github.com/equinix/metal-cli/test/e2e/devices/devicereinstalltest	685.904s
=== RUN   TestCli_Devices_Update
=== RUN   TestCli_Devices_Update/start_device
--- PASS: TestCli_Devices_Update (186.22s)
    --- PASS: TestCli_Devices_Update/start_device (186.22s)
PASS
ok  	github.com/equinix/metal-cli/test/e2e/devices/devicestarttest	188.578s
=== RUN   TestCli_Devices_Update
=== RUN   TestCli_Devices_Update/stop_device
--- PASS: TestCli_Devices_Update (207.60s)
    --- PASS: TestCli_Devices_Update/stop_device (207.60s)
PASS
ok  	github.com/equinix/metal-cli/test/e2e/devices/devicestoptest	211.357s
=== RUN   TestCli_Devices_Update
=== RUN   TestCli_Devices_Update/update_device
--- PASS: TestCli_Devices_Update (179.33s)
    --- PASS: TestCli_Devices_Update/update_device (179.33s)
PASS
ok  	github.com/equinix/metal-cli/test/e2e/devices/deviceupdatetest	182.413s
=== RUN   TestCli_Events_Get
=== RUN   TestCli_Events_Get/get_events_by_dev_id
--- PASS: TestCli_Events_Get (187.18s)
    --- PASS: TestCli_Events_Get/get_events_by_dev_id (187.18s)
PASS
ok  	github.com/equinix/metal-cli/test/e2e/events/deviceeventstest	190.576s
=== RUN   TestCli_Events_Get
=== RUN   TestCli_Events_Get/get_events_by_proj_id
--- PASS: TestCli_Events_Get (2.21s)
    --- PASS: TestCli_Events_Get/get_events_by_proj_id (2.21s)
PASS
ok  	github.com/equinix/metal-cli/test/e2e/events/projecteventstest	5.724s
=== RUN   TestGateways_Create
=== RUN   TestGateways_Create/create_gateways
--- PASS: TestGateways_Create (3.88s)
    --- PASS: TestGateways_Create/create_gateways (1.56s)
=== RUN   TestGateways_Delete
=== RUN   TestGateways_Delete/delete_gateways
--- PASS: TestGateways_Delete (5.57s)
    --- PASS: TestGateways_Delete/delete_gateways (1.95s)
=== RUN   TestGateways_Retrieve
=== RUN   TestGateways_Retrieve/retrieve_gateways_by_projectId
--- PASS: TestGateways_Retrieve (3.98s)
    --- PASS: TestGateways_Retrieve/retrieve_gateways_by_projectId (0.41s)
PASS
ok  	github.com/equinix/metal-cli/test/e2e/gateways	13.778s
=== RUN   TestCli_Hardware
=== RUN   TestCli_Hardware/get
--- PASS: TestCli_Hardware (1.18s)
    --- PASS: TestCli_Hardware/get (1.18s)
PASS
ok  	github.com/equinix/metal-cli/test/e2e/hardwaretest	1.329s
=== RUN   TestCli_Ips_Get
=== RUN   TestCli_Ips_Get/get_ip_reservations
    ips_get_test.go:38: Skipping this test because someCondition is true
--- PASS: TestCli_Ips_Get (0.00s)
    --- SKIP: TestCli_Ips_Get/get_ip_reservations (0.00s)
=== RUN   TestCli_Vlan_Create
=== RUN   TestCli_Vlan_Create/Request_NewIP
    ips_request_test.go:36: Skipping temporarily for now
--- PASS: TestCli_Vlan_Create (0.00s)
    --- SKIP: TestCli_Vlan_Create/Request_NewIP (0.00s)
PASS
ok  	github.com/equinix/metal-cli/test/e2e/ipstest	0.389s
=== RUN   TestCli_Metros
=== RUN   TestCli_Metros/get
--- PASS: TestCli_Metros (0.51s)
    --- PASS: TestCli_Metros/get (0.51s)
PASS
ok  	github.com/equinix/metal-cli/test/e2e/metrotest	0.932s
=== RUN   TestCli_Organization
=== RUN   TestCli_Organization/get
+--------------------------------------+---------------------------------+-------------------------------+
|                  ID                  |              NAME               |            CREATED            |
+--------------------------------------+---------------------------------+-------------------------------+
| 4d12f460-8c5e-43ea-986d-529d328815ee | DevRel Engineering (Playground) | 2023-08-03 20:50:33 +0000 UTC |
+--------------------------------------+---------------------------------+-------------------------------+
--- PASS: TestCli_Organization (2.39s)
    --- PASS: TestCli_Organization/get (2.39s)
PASS
ok  	github.com/equinix/metal-cli/test/e2e/organizationtest	2.820s
=== RUN   TestCli_OperatingSystem
=== RUN   TestCli_OperatingSystem/get
--- PASS: TestCli_OperatingSystem (0.85s)
    --- PASS: TestCli_OperatingSystem/get (0.85s)
PASS
ok  	github.com/equinix/metal-cli/test/e2e/ostest	1.865s
=== RUN   TestCli_Plans
=== RUN   TestCli_Plans/get
=== RUN   TestCli_Plans/get_by_slug
=== RUN   TestCli_Plans/get_by_type
--- PASS: TestCli_Plans (3.64s)
    --- PASS: TestCli_Plans/get (2.60s)
    --- PASS: TestCli_Plans/get_by_slug (0.42s)
    --- PASS: TestCli_Plans/get_by_type (0.61s)
PASS
ok  	github.com/equinix/metal-cli/test/e2e/plantest	4.065s
=== RUN   TestPorts_Convert
=== RUN   TestPorts_Convert/convert_port_layer-2_bonded_false
=== RUN   TestPorts_Convert/convert_port_layer-2_bonded_true
=== RUN   TestPorts_Convert/convert_port_layer-3_bonded_true
2024/01/12 16:37:40 Converting port d6a934d2-d802-4333-ad0b-395f19ff2baa to layer-3
--- PASS: TestPorts_Convert (197.72s)
    --- PASS: TestPorts_Convert/convert_port_layer-2_bonded_false (24.38s)
    --- PASS: TestPorts_Convert/convert_port_layer-2_bonded_true (8.95s)
    --- PASS: TestPorts_Convert/convert_port_layer-3_bonded_true (9.28s)
=== RUN   TestPorts_Retrieve
=== RUN   TestPorts_Retrieve/retrieve_port
--- PASS: TestPorts_Retrieve (177.32s)
    --- PASS: TestPorts_Retrieve/retrieve_port (0.45s)
=== RUN   TestPorts_VLANs
=== RUN   TestPorts_VLANs/vlan_assignment_port
--- PASS: TestPorts_VLANs (193.74s)
    --- PASS: TestPorts_VLANs/vlan_assignment_port (15.95s)
PASS
ok  	github.com/equinix/metal-cli/test/e2e/ports	569.196s
=== RUN   TestCli_Project_Tests
=== RUN   TestCli_Project_Tests/project-create-test
=== RUN   TestCli_Project_Tests/project-update-test
=== RUN   TestCli_Project_Tests/project-get-test
=== RUN   TestCli_Project_Tests/project-get-id-test
=== RUN   TestCli_Project_Tests/project-delete-test
=== RUN   TestCli_Project_Tests/project-bgpenbale-test
=== RUN   TestCli_Project_Tests/project-bgpconfig-test
--- PASS: TestCli_Project_Tests (16.77s)
    --- PASS: TestCli_Project_Tests/project-create-test (5.72s)
    --- PASS: TestCli_Project_Tests/project-update-test (1.63s)
    --- PASS: TestCli_Project_Tests/project-get-test (2.58s)
    --- PASS: TestCli_Project_Tests/project-get-id-test (1.51s)
    --- PASS: TestCli_Project_Tests/project-delete-test (1.54s)
    --- PASS: TestCli_Project_Tests/project-bgpenbale-test (1.48s)
    --- PASS: TestCli_Project_Tests/project-bgpconfig-test (2.31s)
PASS
ok  	github.com/equinix/metal-cli/test/e2e/projecttest	17.205s
=== RUN   TestCli_SshKey
=== RUN   TestCli_SshKey/get
--- PASS: TestCli_SshKey (1.52s)
    --- PASS: TestCli_SshKey/get (1.52s)
PASS
ok  	github.com/equinix/metal-cli/test/e2e/sshtest	1.881s
=== RUN   TestCli_Users
=== RUN   TestCli_Users/get
--- PASS: TestCli_Users (0.53s)
    --- PASS: TestCli_Users/get (0.53s)
PASS
ok  	github.com/equinix/metal-cli/test/e2e/usertest	0.932s
=== RUN   TestCli_Vlan_Create
=== RUN   TestCli_Vlan_Create/create_virtual_network
--- PASS: TestCli_Vlan_Create (1.76s)
    --- PASS: TestCli_Vlan_Create/create_virtual_network (1.76s)
=== RUN   TestCli_Vlan_Clean
=== RUN   TestCli_Vlan_Clean/delete_virtual_network
--- PASS: TestCli_Vlan_Clean (2.11s)
    --- PASS: TestCli_Vlan_Clean/delete_virtual_network (2.11s)
=== RUN   TestCli_Vlan_Get
=== RUN   TestCli_Vlan_Get/get_virtual_network
--- PASS: TestCli_Vlan_Get (2.52s)
    --- PASS: TestCli_Vlan_Get/get_virtual_network (2.52s)
PASS
ok  	github.com/equinix/metal-cli/test/e2e/vlan	6.779s
?   	github.com/equinix/metal-cli/test/helper	[no test files]

@@ -46,7 +27,8 @@ func (c *Client) Retrieve() *cobra.Command {

header := []string{"ID", "Facility", "Metro", "Plan", "Created"}

inc := []string{}
inc := c.Servicer.Includes(nil)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be moved to after the switch block a couple lines down so that we keep the user-specified includes in addition to the ones that are set automatically by the CLI.


return c.Out.Output(r, header, &data)
}
request := c.Service.FindProjectHardwareReservations(context.Background(), projectID).Include(inc).Exclude(exc)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This request isn't wired up to the CLI flags for include and exclude

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated

@codinja1188 codinja1188 requested a deployment to external January 16, 2024 18:17 — with GitHub Actions Abandoned
@codinja1188 codinja1188 requested a deployment to external January 16, 2024 19:29 — with GitHub Actions Abandoned
Copy link
Contributor

@ctreatma ctreatma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM (the only test failure in CI is due to a platform failure while reinstalling a device)

@ctreatma ctreatma merged commit a2319b7 into equinix:main Jan 16, 2024
7 of 8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support pagination in hardware-reservation
4 participants