File: hardware_reservations.go

package info (click to toggle)
golang-github-packethost-packngo 0.2.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 440 kB
  • sloc: makefile: 2
file content (99 lines) | stat: -rw-r--r-- 3,429 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package packngo

import "fmt"

const hardwareReservationBasePath = "/hardware-reservations"

// HardwareReservationService interface defines available hardware reservation functions
type HardwareReservationService interface {
	Get(hardwareReservationID string, getOpt *GetOptions) (*HardwareReservation, *Response, error)
	List(projectID string, listOpt *ListOptions) ([]HardwareReservation, *Response, error)
	Move(string, string) (*HardwareReservation, *Response, error)
}

// HardwareReservationServiceOp implements HardwareReservationService
type HardwareReservationServiceOp struct {
	client *Client
}

// HardwareReservation struct
type HardwareReservation struct {
	ID            string    `json:"id,omitempty"`
	ShortID       string    `json:"short_id,omitempty"`
	Facility      Facility  `json:"facility,omitempty"`
	Plan          Plan      `json:"plan,omitempty"`
	Provisionable bool      `json:"provisionable,omitempty"`
	Spare         bool      `json:"spare,omitempty"`
	SwitchUUID    string    `json:"switch_uuid,omitempty"`
	Intervals     int       `json:"intervals,omitempty"`
	CurrentPeriod int       `json:"current_period,omitempty"`
	Href          string    `json:"href,omitempty"`
	Project       Project   `json:"project,omitempty"`
	Device        *Device   `json:"device,omitempty"`
	CreatedAt     Timestamp `json:"created_at,omitempty"`
}

type hardwareReservationRoot struct {
	HardwareReservations []HardwareReservation `json:"hardware_reservations"`
	Meta                 meta                  `json:"meta"`
}

// List returns all hardware reservations for a given project
func (s *HardwareReservationServiceOp) List(projectID string, listOpt *ListOptions) (reservations []HardwareReservation, resp *Response, err error) {
	root := new(hardwareReservationRoot)
	params := createListOptionsURL(listOpt)

	path := fmt.Sprintf("%s/%s%s?%s", projectBasePath, projectID, hardwareReservationBasePath, params)

	for {
		subset := new(hardwareReservationRoot)

		resp, err = s.client.DoRequest("GET", path, nil, root)
		if err != nil {
			return nil, resp, err
		}

		reservations = append(reservations, root.HardwareReservations...)

		if subset.Meta.Next != nil && (listOpt == nil || listOpt.Page == 0) {
			path = subset.Meta.Next.Href
			if params != "" {
				path = fmt.Sprintf("%s&%s", path, params)
			}
			continue
		}

		return
	}
}

// Get returns a single hardware reservation
func (s *HardwareReservationServiceOp) Get(hardwareReservationdID string, getOpt *GetOptions) (*HardwareReservation, *Response, error) {
	params := createGetOptionsURL(getOpt)

	hardwareReservation := new(HardwareReservation)

	path := fmt.Sprintf("%s/%s?%s", hardwareReservationBasePath, hardwareReservationdID, params)

	resp, err := s.client.DoRequest("GET", path, nil, hardwareReservation)
	if err != nil {
		return nil, resp, err
	}

	return hardwareReservation, resp, err
}

// Move a hardware reservation to another project
func (s *HardwareReservationServiceOp) Move(hardwareReservationdID, projectID string) (*HardwareReservation, *Response, error) {
	hardwareReservation := new(HardwareReservation)
	path := fmt.Sprintf("%s/%s/%s", hardwareReservationBasePath, hardwareReservationdID, "move")
	body := map[string]string{}
	body["project_id"] = projectID

	resp, err := s.client.DoRequest("POST", path, body, hardwareReservation)
	if err != nil {
		return nil, resp, err
	}

	return hardwareReservation, resp, err
}