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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
|
package hcloud
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"net/url"
"strconv"
"time"
"github.com/hetznercloud/hcloud-go/v2/hcloud/schema"
)
// PlacementGroup represents a Placement Group in the Hetzner Cloud.
type PlacementGroup struct {
ID int64
Name string
Labels map[string]string
Created time.Time
Servers []int64
Type PlacementGroupType
}
// PlacementGroupType specifies the type of a Placement Group.
type PlacementGroupType string
const (
// PlacementGroupTypeSpread spreads all servers in the group on different vhosts.
PlacementGroupTypeSpread PlacementGroupType = "spread"
)
// PlacementGroupClient is a client for the Placement Groups API.
type PlacementGroupClient struct {
client *Client
}
// GetByID retrieves a PlacementGroup by its ID. If the PlacementGroup does not exist, nil is returned.
func (c *PlacementGroupClient) GetByID(ctx context.Context, id int64) (*PlacementGroup, *Response, error) {
req, err := c.client.NewRequest(ctx, "GET", fmt.Sprintf("/placement_groups/%d", id), nil)
if err != nil {
return nil, nil, err
}
var body schema.PlacementGroupGetResponse
resp, err := c.client.Do(req, &body)
if err != nil {
if IsError(err, ErrorCodeNotFound) {
return nil, resp, nil
}
return nil, nil, err
}
return PlacementGroupFromSchema(body.PlacementGroup), resp, nil
}
// GetByName retrieves a PlacementGroup by its name. If the PlacementGroup does not exist, nil is returned.
func (c *PlacementGroupClient) GetByName(ctx context.Context, name string) (*PlacementGroup, *Response, error) {
if name == "" {
return nil, nil, nil
}
placementGroups, response, err := c.List(ctx, PlacementGroupListOpts{Name: name})
if len(placementGroups) == 0 {
return nil, response, err
}
return placementGroups[0], response, err
}
// Get retrieves a PlacementGroup by its ID if the input can be parsed as an integer, otherwise it
// retrieves a PlacementGroup by its name. If the PlacementGroup does not exist, nil is returned.
func (c *PlacementGroupClient) Get(ctx context.Context, idOrName string) (*PlacementGroup, *Response, error) {
if id, err := strconv.ParseInt(idOrName, 10, 64); err == nil {
return c.GetByID(ctx, id)
}
return c.GetByName(ctx, idOrName)
}
// PlacementGroupListOpts specifies options for listing PlacementGroup.
type PlacementGroupListOpts struct {
ListOpts
Name string
Type PlacementGroupType
Sort []string
}
func (l PlacementGroupListOpts) values() url.Values {
vals := l.ListOpts.Values()
if l.Name != "" {
vals.Add("name", l.Name)
}
if l.Type != "" {
vals.Add("type", string(l.Type))
}
for _, sort := range l.Sort {
vals.Add("sort", sort)
}
return vals
}
// List returns a list of PlacementGroups for a specific page.
//
// Please note that filters specified in opts are not taken into account
// when their value corresponds to their zero value or when they are empty.
func (c *PlacementGroupClient) List(ctx context.Context, opts PlacementGroupListOpts) ([]*PlacementGroup, *Response, error) {
path := "/placement_groups?" + opts.values().Encode()
req, err := c.client.NewRequest(ctx, "GET", path, nil)
if err != nil {
return nil, nil, err
}
var body schema.PlacementGroupListResponse
resp, err := c.client.Do(req, &body)
if err != nil {
return nil, nil, err
}
placementGroups := make([]*PlacementGroup, 0, len(body.PlacementGroups))
for _, g := range body.PlacementGroups {
placementGroups = append(placementGroups, PlacementGroupFromSchema(g))
}
return placementGroups, resp, nil
}
// All returns all PlacementGroups.
func (c *PlacementGroupClient) All(ctx context.Context) ([]*PlacementGroup, error) {
opts := PlacementGroupListOpts{
ListOpts: ListOpts{
PerPage: 50,
},
}
return c.AllWithOpts(ctx, opts)
}
// AllWithOpts returns all PlacementGroups for the given options.
func (c *PlacementGroupClient) AllWithOpts(ctx context.Context, opts PlacementGroupListOpts) ([]*PlacementGroup, error) {
allPlacementGroups := []*PlacementGroup{}
err := c.client.all(func(page int) (*Response, error) {
opts.Page = page
placementGroups, resp, err := c.List(ctx, opts)
if err != nil {
return resp, err
}
allPlacementGroups = append(allPlacementGroups, placementGroups...)
return resp, nil
})
if err != nil {
return nil, err
}
return allPlacementGroups, nil
}
// PlacementGroupCreateOpts specifies options for creating a new PlacementGroup.
type PlacementGroupCreateOpts struct {
Name string
Labels map[string]string
Type PlacementGroupType
}
// Validate checks if options are valid.
func (o PlacementGroupCreateOpts) Validate() error {
if o.Name == "" {
return errors.New("missing name")
}
return nil
}
// PlacementGroupCreateResult is the result of a create PlacementGroup call.
type PlacementGroupCreateResult struct {
PlacementGroup *PlacementGroup
Action *Action
}
// Create creates a new PlacementGroup.
func (c *PlacementGroupClient) Create(ctx context.Context, opts PlacementGroupCreateOpts) (PlacementGroupCreateResult, *Response, error) {
if err := opts.Validate(); err != nil {
return PlacementGroupCreateResult{}, nil, err
}
reqBody := placementGroupCreateOptsToSchema(opts)
reqBodyData, err := json.Marshal(reqBody)
if err != nil {
return PlacementGroupCreateResult{}, nil, err
}
req, err := c.client.NewRequest(ctx, "POST", "/placement_groups", bytes.NewReader(reqBodyData))
if err != nil {
return PlacementGroupCreateResult{}, nil, err
}
respBody := schema.PlacementGroupCreateResponse{}
resp, err := c.client.Do(req, &respBody)
if err != nil {
return PlacementGroupCreateResult{}, nil, err
}
result := PlacementGroupCreateResult{
PlacementGroup: PlacementGroupFromSchema(respBody.PlacementGroup),
}
if respBody.Action != nil {
result.Action = ActionFromSchema(*respBody.Action)
}
return result, resp, nil
}
// PlacementGroupUpdateOpts specifies options for updating a PlacementGroup.
type PlacementGroupUpdateOpts struct {
Name string
Labels map[string]string
}
// Update updates a PlacementGroup.
func (c *PlacementGroupClient) Update(ctx context.Context, placementGroup *PlacementGroup, opts PlacementGroupUpdateOpts) (*PlacementGroup, *Response, error) {
reqBody := schema.PlacementGroupUpdateRequest{}
if opts.Name != "" {
reqBody.Name = &opts.Name
}
if opts.Labels != nil {
reqBody.Labels = &opts.Labels
}
reqBodyData, err := json.Marshal(reqBody)
if err != nil {
return nil, nil, err
}
path := fmt.Sprintf("/placement_groups/%d", placementGroup.ID)
req, err := c.client.NewRequest(ctx, "PUT", path, bytes.NewReader(reqBodyData))
if err != nil {
return nil, nil, err
}
respBody := schema.PlacementGroupUpdateResponse{}
resp, err := c.client.Do(req, &respBody)
if err != nil {
return nil, resp, err
}
return PlacementGroupFromSchema(respBody.PlacementGroup), resp, nil
}
// Delete deletes a PlacementGroup.
func (c *PlacementGroupClient) Delete(ctx context.Context, placementGroup *PlacementGroup) (*Response, error) {
req, err := c.client.NewRequest(ctx, "DELETE", fmt.Sprintf("/placement_groups/%d", placementGroup.ID), nil)
if err != nil {
return nil, err
}
return c.client.Do(req, nil)
}
|