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 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
|
package hcloud
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net"
"net/url"
"strconv"
"time"
"github.com/hetznercloud/hcloud-go/v2/hcloud/schema"
)
// PrimaryIP defines a Primary IP.
type PrimaryIP struct {
ID int64
IP net.IP
Network *net.IPNet
Labels map[string]string
Name string
Type PrimaryIPType
Protection PrimaryIPProtection
DNSPtr map[string]string
AssigneeID int64
AssigneeType string
AutoDelete bool
Blocked bool
Created time.Time
Datacenter *Datacenter
}
// PrimaryIPProtection represents the protection level of a Primary IP.
type PrimaryIPProtection struct {
Delete bool
}
// PrimaryIPDNSPTR contains reverse DNS information for a
// IPv4 or IPv6 Primary IP.
type PrimaryIPDNSPTR struct {
DNSPtr string
IP string
}
// changeDNSPtr changes or resets the reverse DNS pointer for a IP address.
// Pass a nil ptr to reset the reverse DNS pointer to its default value.
func (p *PrimaryIP) changeDNSPtr(ctx context.Context, client *Client, ip net.IP, ptr *string) (*Action, *Response, error) {
reqBody := schema.PrimaryIPActionChangeDNSPtrRequest{
IP: ip.String(),
DNSPtr: ptr,
}
reqBodyData, err := json.Marshal(reqBody)
if err != nil {
return nil, nil, err
}
path := fmt.Sprintf("/primary_ips/%d/actions/change_dns_ptr", p.ID)
req, err := client.NewRequest(ctx, "POST", path, bytes.NewReader(reqBodyData))
if err != nil {
return nil, nil, err
}
var respBody PrimaryIPChangeDNSPtrResult
resp, err := client.Do(req, &respBody)
if err != nil {
return nil, resp, err
}
return ActionFromSchema(respBody.Action), resp, nil
}
// GetDNSPtrForIP searches for the dns assigned to the given IP address.
// It returns an error if there is no dns set for the given IP address.
func (p *PrimaryIP) GetDNSPtrForIP(ip net.IP) (string, error) {
dns, ok := p.DNSPtr[ip.String()]
if !ok {
return "", DNSNotFoundError{ip}
}
return dns, nil
}
// PrimaryIPType represents the type of Primary IP.
type PrimaryIPType string
// PrimaryIPType Primary IP types.
const (
PrimaryIPTypeIPv4 PrimaryIPType = "ipv4"
PrimaryIPTypeIPv6 PrimaryIPType = "ipv6"
)
// PrimaryIPCreateOpts defines the request to
// create a Primary IP.
type PrimaryIPCreateOpts struct {
AssigneeID *int64 `json:"assignee_id,omitempty"`
AssigneeType string `json:"assignee_type"`
AutoDelete *bool `json:"auto_delete,omitempty"`
Datacenter string `json:"datacenter,omitempty"`
Labels map[string]string `json:"labels,omitempty"`
Name string `json:"name"`
Type PrimaryIPType `json:"type"`
}
// PrimaryIPCreateResult defines the response
// when creating a Primary IP.
type PrimaryIPCreateResult struct {
PrimaryIP *PrimaryIP
Action *Action
}
// PrimaryIPUpdateOpts defines the request to
// update a Primary IP.
type PrimaryIPUpdateOpts struct {
AutoDelete *bool `json:"auto_delete,omitempty"`
Labels *map[string]string `json:"labels,omitempty"`
Name string `json:"name,omitempty"`
}
// PrimaryIPAssignOpts defines the request to
// assign a Primary IP to an assignee (usually a server).
type PrimaryIPAssignOpts struct {
ID int64
AssigneeID int64 `json:"assignee_id"`
AssigneeType string `json:"assignee_type"`
}
// PrimaryIPAssignResult defines the response
// when assigning a Primary IP to a assignee.
type PrimaryIPAssignResult struct {
Action schema.Action `json:"action"`
}
// PrimaryIPChangeDNSPtrOpts defines the request to
// change a DNS PTR entry from a Primary IP.
type PrimaryIPChangeDNSPtrOpts struct {
ID int64
DNSPtr string `json:"dns_ptr"`
IP string `json:"ip"`
}
// PrimaryIPChangeDNSPtrResult defines the response
// when assigning a Primary IP to a assignee.
type PrimaryIPChangeDNSPtrResult struct {
Action schema.Action `json:"action"`
}
// PrimaryIPChangeProtectionOpts defines the request to
// change protection configuration of a Primary IP.
type PrimaryIPChangeProtectionOpts struct {
ID int64
Delete bool `json:"delete"`
}
// PrimaryIPChangeProtectionResult defines the response
// when changing a protection of a PrimaryIP.
type PrimaryIPChangeProtectionResult struct {
Action schema.Action `json:"action"`
}
// PrimaryIPClient is a client for the Primary IP API.
type PrimaryIPClient struct {
client *Client
Action *ResourceActionClient
}
// GetByID retrieves a Primary IP by its ID. If the Primary IP does not exist, nil is returned.
func (c *PrimaryIPClient) GetByID(ctx context.Context, id int64) (*PrimaryIP, *Response, error) {
req, err := c.client.NewRequest(ctx, "GET", fmt.Sprintf("/primary_ips/%d", id), nil)
if err != nil {
return nil, nil, err
}
var body schema.PrimaryIPGetResult
resp, err := c.client.Do(req, &body)
if err != nil {
if IsError(err, ErrorCodeNotFound) {
return nil, resp, nil
}
return nil, nil, err
}
return PrimaryIPFromSchema(body.PrimaryIP), resp, nil
}
// GetByIP retrieves a Primary IP by its IP Address. If the Primary IP does not exist, nil is returned.
func (c *PrimaryIPClient) GetByIP(ctx context.Context, ip string) (*PrimaryIP, *Response, error) {
if ip == "" {
return nil, nil, nil
}
primaryIPs, response, err := c.List(ctx, PrimaryIPListOpts{IP: ip})
if len(primaryIPs) == 0 {
return nil, response, err
}
return primaryIPs[0], response, err
}
// GetByName retrieves a Primary IP by its name. If the Primary IP does not exist, nil is returned.
func (c *PrimaryIPClient) GetByName(ctx context.Context, name string) (*PrimaryIP, *Response, error) {
if name == "" {
return nil, nil, nil
}
primaryIPs, response, err := c.List(ctx, PrimaryIPListOpts{Name: name})
if len(primaryIPs) == 0 {
return nil, response, err
}
return primaryIPs[0], response, err
}
// Get retrieves a Primary IP by its ID if the input can be parsed as an integer, otherwise it
// retrieves a Primary IP by its name. If the Primary IP does not exist, nil is returned.
func (c *PrimaryIPClient) Get(ctx context.Context, idOrName string) (*PrimaryIP, *Response, error) {
if id, err := strconv.ParseInt(idOrName, 10, 64); err == nil {
return c.GetByID(ctx, id)
}
return c.GetByName(ctx, idOrName)
}
// PrimaryIPListOpts specifies options for listing Primary IPs.
type PrimaryIPListOpts struct {
ListOpts
Name string
IP string
Sort []string
}
func (l PrimaryIPListOpts) values() url.Values {
vals := l.ListOpts.Values()
if l.Name != "" {
vals.Add("name", l.Name)
}
if l.IP != "" {
vals.Add("ip", l.IP)
}
for _, sort := range l.Sort {
vals.Add("sort", sort)
}
return vals
}
// List returns a list of Primary IPs 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 *PrimaryIPClient) List(ctx context.Context, opts PrimaryIPListOpts) ([]*PrimaryIP, *Response, error) {
path := "/primary_ips?" + opts.values().Encode()
req, err := c.client.NewRequest(ctx, "GET", path, nil)
if err != nil {
return nil, nil, err
}
var body schema.PrimaryIPListResult
resp, err := c.client.Do(req, &body)
if err != nil {
return nil, nil, err
}
primaryIPs := make([]*PrimaryIP, 0, len(body.PrimaryIPs))
for _, s := range body.PrimaryIPs {
primaryIPs = append(primaryIPs, PrimaryIPFromSchema(s))
}
return primaryIPs, resp, nil
}
// All returns all Primary IPs.
func (c *PrimaryIPClient) All(ctx context.Context) ([]*PrimaryIP, error) {
return c.AllWithOpts(ctx, PrimaryIPListOpts{ListOpts: ListOpts{PerPage: 50}})
}
// AllWithOpts returns all Primary IPs for the given options.
func (c *PrimaryIPClient) AllWithOpts(ctx context.Context, opts PrimaryIPListOpts) ([]*PrimaryIP, error) {
allPrimaryIPs := []*PrimaryIP{}
err := c.client.all(func(page int) (*Response, error) {
opts.Page = page
primaryIPs, resp, err := c.List(ctx, opts)
if err != nil {
return resp, err
}
allPrimaryIPs = append(allPrimaryIPs, primaryIPs...)
return resp, nil
})
if err != nil {
return nil, err
}
return allPrimaryIPs, nil
}
// Create creates a Primary IP.
func (c *PrimaryIPClient) Create(ctx context.Context, reqBody PrimaryIPCreateOpts) (*PrimaryIPCreateResult, *Response, error) {
reqBodyData, err := json.Marshal(reqBody)
if err != nil {
return &PrimaryIPCreateResult{}, nil, err
}
req, err := c.client.NewRequest(ctx, "POST", "/primary_ips", bytes.NewReader(reqBodyData))
if err != nil {
return &PrimaryIPCreateResult{}, nil, err
}
var respBody schema.PrimaryIPCreateResponse
resp, err := c.client.Do(req, &respBody)
if err != nil {
return &PrimaryIPCreateResult{}, resp, err
}
var action *Action
if respBody.Action != nil {
action = ActionFromSchema(*respBody.Action)
}
primaryIP := PrimaryIPFromSchema(respBody.PrimaryIP)
return &PrimaryIPCreateResult{
PrimaryIP: primaryIP,
Action: action,
}, resp, nil
}
// Delete deletes a Primary IP.
func (c *PrimaryIPClient) Delete(ctx context.Context, primaryIP *PrimaryIP) (*Response, error) {
req, err := c.client.NewRequest(ctx, "DELETE", fmt.Sprintf("/primary_ips/%d", primaryIP.ID), nil)
if err != nil {
return nil, err
}
return c.client.Do(req, nil)
}
// Update updates a Primary IP.
func (c *PrimaryIPClient) Update(ctx context.Context, primaryIP *PrimaryIP, reqBody PrimaryIPUpdateOpts) (*PrimaryIP, *Response, error) {
reqBodyData, err := json.Marshal(reqBody)
if err != nil {
return nil, nil, err
}
path := fmt.Sprintf("/primary_ips/%d", primaryIP.ID)
req, err := c.client.NewRequest(ctx, "PUT", path, bytes.NewReader(reqBodyData))
if err != nil {
return nil, nil, err
}
var respBody schema.PrimaryIPUpdateResult
resp, err := c.client.Do(req, &respBody)
if err != nil {
return nil, resp, err
}
return PrimaryIPFromSchema(respBody.PrimaryIP), resp, nil
}
// Assign a Primary IP to a resource.
func (c *PrimaryIPClient) Assign(ctx context.Context, opts PrimaryIPAssignOpts) (*Action, *Response, error) {
reqBodyData, err := json.Marshal(opts)
if err != nil {
return nil, nil, err
}
path := fmt.Sprintf("/primary_ips/%d/actions/assign", opts.ID)
req, err := c.client.NewRequest(ctx, "POST", path, bytes.NewReader(reqBodyData))
if err != nil {
return nil, nil, err
}
var respBody PrimaryIPAssignResult
resp, err := c.client.Do(req, &respBody)
if err != nil {
return nil, resp, err
}
return ActionFromSchema(respBody.Action), resp, nil
}
// Unassign a Primary IP from a resource.
func (c *PrimaryIPClient) Unassign(ctx context.Context, id int64) (*Action, *Response, error) {
path := fmt.Sprintf("/primary_ips/%d/actions/unassign", id)
req, err := c.client.NewRequest(ctx, "POST", path, bytes.NewReader([]byte{}))
if err != nil {
return nil, nil, err
}
var respBody PrimaryIPAssignResult
resp, err := c.client.Do(req, &respBody)
if err != nil {
return nil, resp, err
}
return ActionFromSchema(respBody.Action), resp, nil
}
// ChangeDNSPtr Change the reverse DNS from a Primary IP.
func (c *PrimaryIPClient) ChangeDNSPtr(ctx context.Context, opts PrimaryIPChangeDNSPtrOpts) (*Action, *Response, error) {
reqBodyData, err := json.Marshal(opts)
if err != nil {
return nil, nil, err
}
path := fmt.Sprintf("/primary_ips/%d/actions/change_dns_ptr", opts.ID)
req, err := c.client.NewRequest(ctx, "POST", path, bytes.NewReader(reqBodyData))
if err != nil {
return nil, nil, err
}
var respBody PrimaryIPChangeDNSPtrResult
resp, err := c.client.Do(req, &respBody)
if err != nil {
return nil, resp, err
}
return ActionFromSchema(respBody.Action), resp, nil
}
// ChangeProtection Changes the protection configuration of a Primary IP.
func (c *PrimaryIPClient) ChangeProtection(ctx context.Context, opts PrimaryIPChangeProtectionOpts) (*Action, *Response, error) {
reqBodyData, err := json.Marshal(opts)
if err != nil {
return nil, nil, err
}
path := fmt.Sprintf("/primary_ips/%d/actions/change_protection", opts.ID)
req, err := c.client.NewRequest(ctx, "POST", path, bytes.NewReader(reqBodyData))
if err != nil {
return nil, nil, err
}
var respBody PrimaryIPChangeProtectionResult
resp, err := c.client.Do(req, &respBody)
if err != nil {
return nil, resp, err
}
return ActionFromSchema(respBody.Action), resp, nil
}
|