-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated metal-go client for sub-commands hardware
- Loading branch information
1 parent
6ab4251
commit b15ef18
Showing
7 changed files
with
178 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,14 @@ | ||
// Copyright © 2018 Jasmin Gacic <[email protected]> | ||
// | ||
// 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 hardware | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/equinix/metal-cli/internal/outputs" | ||
"github.com/packethost/packngo" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
metal "github.com/equinix/equinix-sdk-go/services/metalv1" | ||
) | ||
|
||
func (c *Client) Retrieve() *cobra.Command { | ||
|
@@ -46,7 +29,8 @@ func (c *Client) Retrieve() *cobra.Command { | |
|
||
header := []string{"ID", "Facility", "Metro", "Plan", "Created"} | ||
|
||
inc := []string{} | ||
inc := c.Servicer.Includes(nil) | ||
exc := c.Servicer.Excludes(nil) | ||
|
||
// only fetch extra details when rendered | ||
switch c.Servicer.Format() { | ||
|
@@ -56,44 +40,57 @@ func (c *Client) Retrieve() *cobra.Command { | |
inc = []string{"facility.metro"} | ||
} | ||
|
||
listOpt := c.Servicer.ListOptions(inc, nil) | ||
|
||
if hardwareReservationID == "" && projectID == "" { | ||
return fmt.Errorf("either id or project-id should be set") | ||
} | ||
|
||
cmd.SilenceUsage = true | ||
if hardwareReservationID != "" { | ||
getOpts := &packngo.GetOptions{Includes: listOpt.Includes, Excludes: listOpt.Excludes} | ||
r, _, err := c.Service.Get(hardwareReservationID, getOpts) | ||
r, _, err := c.Service.FindHardwareReservationById(context.Background(), hardwareReservationID).Include(inc).Exclude(exc).Execute() | ||
if err != nil { | ||
return fmt.Errorf("Could not get Hardware Reservation: %w", err) | ||
return fmt.Errorf("could not get Hardware Reservation: %w", err) | ||
} | ||
|
||
data := make([][]string, 1) | ||
metro := "" | ||
if r.Facility.Metro != nil { | ||
metro = r.Facility.Metro.Code | ||
metro = *r.Facility.Metro.Code | ||
} | ||
|
||
data[0] = []string{r.ID, r.Facility.Code, metro, r.Plan.Name, r.CreatedAt.String()} | ||
data[0] = []string{r.GetId(), r.Facility.GetCode(), metro, r.Plan.GetName(), r.CreatedAt.String()} | ||
|
||
return c.Out.Output(r, header, &data) | ||
} | ||
request := c.Service.FindProjectHardwareReservations(context.Background(), projectID).Include(inc).Exclude(exc) | ||
filters := c.Servicer.Filters() | ||
|
||
reservations, _, err := c.Service.List(projectID, listOpt) | ||
if err != nil { | ||
return fmt.Errorf("Could not list Hardware Reservations: %w", err) | ||
if filters["query"] != "" { | ||
request = request.Query(filters["query"]) | ||
} | ||
|
||
if filters["state"] != "" { | ||
state, _ := metal.NewFindProjectHardwareReservationsStateParameterFromValue(filters["state"]) | ||
request = request.State(*state) | ||
} | ||
|
||
if filters["provisionable"] != "" { | ||
provisionable, _ := metal.NewFindProjectHardwareReservationsProvisionableParameterFromValue(filters["provisionable"]) | ||
request = request.Provisionable(*provisionable) | ||
} | ||
|
||
reservationsList, err := request.ExecuteWithPagination() | ||
if err != nil { | ||
return fmt.Errorf("could not list Hardware Reservations: %w", err) | ||
} | ||
reservations := reservationsList.GetHardwareReservations() | ||
data := make([][]string, len(reservations)) | ||
|
||
for i, r := range reservations { | ||
metro := "" | ||
if r.Facility.Metro != nil { | ||
metro = r.Facility.Metro.Code | ||
metro = r.Facility.Metro.GetCode() | ||
} | ||
data[i] = []string{r.ID, r.Facility.Code, metro, r.Plan.Name, r.CreatedAt.String()} | ||
data[i] = []string{r.GetId(), r.Facility.GetCode(), metro, r.Plan.GetName(), r.CreatedAt.String()} | ||
} | ||
|
||
return c.Out.Output(reservations, header, &data) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,9 @@ | ||
// Copyright © 2018 Jasmin Gacic <[email protected]> | ||
// | ||
// 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 organizations | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
pager "github.com/equinix/metal-cli/internal/pagination" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
|
@@ -53,11 +32,11 @@ func (c *Client) Retrieve() *cobra.Command { | |
exclude := c.Servicer.Excludes(nil) | ||
|
||
if organizationID == "" { | ||
orgs, err := pager.GetAllOrganizations(c.Service, include, exclude) | ||
orgsList, err := c.Service.FindOrganizations(context.Background()).Include(c.Servicer.Includes(include)).Exclude(c.Servicer.Excludes(exclude)).ExecuteWithPagination() | ||
if err != nil { | ||
return fmt.Errorf("Could not list Organizations: %w", err) | ||
return fmt.Errorf("could not list Organizations: %w", err) | ||
} | ||
|
||
orgs := orgsList.GetOrganizations() | ||
data := make([][]string, len(orgs)) | ||
|
||
for i, p := range orgs { | ||
|
@@ -69,7 +48,7 @@ func (c *Client) Retrieve() *cobra.Command { | |
} else { | ||
org, _, err := c.Service.FindOrganizationById(context.Background(), organizationID).Include(include).Exclude(exclude).Execute() | ||
if err != nil { | ||
return fmt.Errorf("Could not get Organization: %w", err) | ||
return fmt.Errorf("could not get Organization: %w", err) | ||
} | ||
|
||
data := make([][]string, 1) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.