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 govultr
import (
"context"
"fmt"
"net/http"
"github.com/google/go-querystring/query"
)
const bmPath = "/v2/bare-metals"
// BareMetalServerService is the interface to interact with the Bare Metal endpoints on the Vultr API
// Link : https://www.vultr.com/api/#tag/baremetal
type BareMetalServerService interface {
Create(ctx context.Context, bmCreate *BareMetalCreate) (*BareMetalServer, error)
Get(ctx context.Context, serverID string) (*BareMetalServer, error)
Update(ctx context.Context, serverID string, bmReq *BareMetalUpdate) (*BareMetalServer, error)
Delete(ctx context.Context, serverID string) error
List(ctx context.Context, options *ListOptions) ([]BareMetalServer, *Meta, error)
GetBandwidth(ctx context.Context, serverID string) (*Bandwidth, error)
GetUserData(ctx context.Context, serverID string) (*UserData, error)
GetVNCUrl(ctx context.Context, serverID string) (*VNCUrl, error)
ListIPv4s(ctx context.Context, serverID string, options *ListOptions) ([]IPv4, *Meta, error)
ListIPv6s(ctx context.Context, serverID string, options *ListOptions) ([]IPv6, *Meta, error)
Halt(ctx context.Context, serverID string) error
Reboot(ctx context.Context, serverID string) error
Start(ctx context.Context, serverID string) error
Reinstall(ctx context.Context, serverID string) (*BareMetalServer, error)
MassStart(ctx context.Context, serverList []string) error
MassHalt(ctx context.Context, serverList []string) error
MassReboot(ctx context.Context, serverList []string) error
GetUpgrades(ctx context.Context, serverID string) (*Upgrades, error)
}
// BareMetalServerServiceHandler handles interaction with the Bare Metal methods for the Vultr API
type BareMetalServerServiceHandler struct {
client *Client
}
// BareMetalServer represents a Bare Metal server on Vultr
type BareMetalServer struct {
ID string `json:"id"`
Os string `json:"os"`
RAM string `json:"ram"`
Disk string `json:"disk"`
MainIP string `json:"main_ip"`
CPUCount int `json:"cpu_count"`
Region string `json:"region"`
DefaultPassword string `json:"default_password"`
DateCreated string `json:"date_created"`
Status string `json:"status"`
NetmaskV4 string `json:"netmask_v4"`
GatewayV4 string `json:"gateway_v4"`
Plan string `json:"plan"`
V6Network string `json:"v6_network"`
V6MainIP string `json:"v6_main_ip"`
V6NetworkSize int `json:"v6_network_size"`
MacAddress int `json:"mac_address"`
Label string `json:"label"`
// Deprecated: Tag should no longer be used. Instead, use Tags.
Tag string `json:"tag"`
OsID int `json:"os_id"`
AppID int `json:"app_id"`
ImageID string `json:"image_id"`
Features []string `json:"features"`
Tags []string `json:"tags"`
}
// BareMetalCreate represents the optional parameters that can be set when creating a Bare Metal server
type BareMetalCreate struct {
Region string `json:"region,omitempty"`
Plan string `json:"plan,omitempty"`
OsID int `json:"os_id,omitempty"`
StartupScriptID string `json:"script_id,omitempty"`
SnapshotID string `json:"snapshot_id,omitempty"`
EnableIPv6 *bool `json:"enable_ipv6,omitempty"`
Label string `json:"label,omitempty"`
SSHKeyIDs []string `json:"sshkey_id,omitempty"`
AppID int `json:"app_id,omitempty"`
ImageID string `json:"image_id,omitempty"`
UserData string `json:"user_data,omitempty"`
ActivationEmail *bool `json:"activation_email,omitempty"`
Hostname string `json:"hostname,omitempty"`
// Deprecated: Tag should no longer be used. Instead, use Tags.
Tag string `json:"tag,omitempty"`
ReservedIPv4 string `json:"reserved_ipv4,omitempty"`
PersistentPxe *bool `json:"persistent_pxe,omitempty"`
Tags []string `json:"tags"`
}
// BareMetalUpdate represents the optional parameters that can be set when updating a Bare Metal server
type BareMetalUpdate struct {
OsID int `json:"os_id,omitempty"`
EnableIPv6 *bool `json:"enable_ipv6,omitempty"`
Label string `json:"label,omitempty"`
AppID int `json:"app_id,omitempty"`
ImageID string `json:"image_id,omitempty"`
UserData string `json:"user_data,omitempty"`
// Deprecated: Tag should no longer be used. Instead, use Tags.
Tag *string `json:"tag,omitempty"`
Tags []string `json:"tags"`
}
// BareMetalServerBandwidth represents bandwidth information for a Bare Metal server
type BareMetalServerBandwidth struct {
IncomingBytes int `json:"incoming_bytes"`
OutgoingBytes int `json:"outgoing_bytes"`
}
type bareMetalsBase struct {
BareMetals []BareMetalServer `json:"bare_metals"`
Meta *Meta `json:"meta"`
}
type bareMetalBase struct {
BareMetal *BareMetalServer `json:"bare_metal"`
}
// BMBareMetalBase ...
type BMBareMetalBase struct {
BareMetalBandwidth map[string]BareMetalServerBandwidth `json:"bandwidth"`
}
type vncBase struct {
VNCUrl *VNCUrl `json:"vnc"`
}
// VNCUrl contains the URL for a given Bare Metals VNC
type VNCUrl struct {
URL string `json:"url"`
}
// Create a new Bare Metal server.
func (b *BareMetalServerServiceHandler) Create(ctx context.Context, bmCreate *BareMetalCreate) (*BareMetalServer, error) {
req, err := b.client.NewRequest(ctx, http.MethodPost, bmPath, bmCreate)
if err != nil {
return nil, err
}
bm := new(bareMetalBase)
if err = b.client.DoWithContext(ctx, req, bm); err != nil {
return nil, err
}
return bm.BareMetal, nil
}
// Get information for a Bare Metal instance.
func (b *BareMetalServerServiceHandler) Get(ctx context.Context, serverID string) (*BareMetalServer, error) {
uri := fmt.Sprintf("%s/%s", bmPath, serverID)
req, err := b.client.NewRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, err
}
bms := new(bareMetalBase)
if err = b.client.DoWithContext(ctx, req, bms); err != nil {
return nil, err
}
return bms.BareMetal, nil
}
// Update a Bare Metal server
func (b *BareMetalServerServiceHandler) Update(ctx context.Context, serverID string, bmReq *BareMetalUpdate) (*BareMetalServer, error) {
uri := fmt.Sprintf("%s/%s", bmPath, serverID)
req, err := b.client.NewRequest(ctx, http.MethodPatch, uri, bmReq)
if err != nil {
return nil, err
}
bms := new(bareMetalBase)
if err = b.client.DoWithContext(ctx, req, bms); err != nil {
return nil, err
}
return bms.BareMetal, nil
}
// Delete a Bare Metal server.
func (b *BareMetalServerServiceHandler) Delete(ctx context.Context, serverID string) error {
uri := fmt.Sprintf("%s/%s", bmPath, serverID)
req, err := b.client.NewRequest(ctx, http.MethodDelete, uri, nil)
if err != nil {
return err
}
return b.client.DoWithContext(ctx, req, nil)
}
// List all Bare Metal instances in your account.
func (b *BareMetalServerServiceHandler) List(ctx context.Context, options *ListOptions) ([]BareMetalServer, *Meta, error) {
req, err := b.client.NewRequest(ctx, http.MethodGet, bmPath, nil)
if err != nil {
return nil, nil, err
}
newValues, err := query.Values(options)
if err != nil {
return nil, nil, err
}
req.URL.RawQuery = newValues.Encode()
bms := new(bareMetalsBase)
if err = b.client.DoWithContext(ctx, req, bms); err != nil {
return nil, nil, err
}
return bms.BareMetals, bms.Meta, nil
}
// GetBandwidth used by a Bare Metal server.
func (b *BareMetalServerServiceHandler) GetBandwidth(ctx context.Context, serverID string) (*Bandwidth, error) {
uri := fmt.Sprintf("%s/%s/bandwidth", bmPath, serverID)
req, err := b.client.NewRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, err
}
bms := new(Bandwidth)
if err = b.client.DoWithContext(ctx, req, &bms); err != nil {
return nil, err
}
return bms, nil
}
// GetUserData for a Bare Metal server. The userdata returned will be in base64 encoding.
func (b *BareMetalServerServiceHandler) GetUserData(ctx context.Context, serverID string) (*UserData, error) {
uri := fmt.Sprintf("%s/%s/user-data", bmPath, serverID)
req, err := b.client.NewRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, err
}
userData := new(userDataBase)
if err = b.client.DoWithContext(ctx, req, userData); err != nil {
return nil, err
}
return userData.UserData, nil
}
// GetVNCUrl gets the vnc url for a given Bare Metal server.
func (b *BareMetalServerServiceHandler) GetVNCUrl(ctx context.Context, serverID string) (*VNCUrl, error) {
uri := fmt.Sprintf("%s/%s/vnc", bmPath, serverID)
req, err := b.client.NewRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, err
}
vnc := new(vncBase)
if err = b.client.DoWithContext(ctx, req, vnc); err != nil {
return nil, err
}
return vnc.VNCUrl, nil
}
// ListIPv4s information of a Bare Metal server.
// IP information is only available for Bare Metal servers in the "active" state.
func (b *BareMetalServerServiceHandler) ListIPv4s(ctx context.Context, serverID string, options *ListOptions) ([]IPv4, *Meta, error) {
uri := fmt.Sprintf("%s/%s/ipv4", bmPath, serverID)
req, err := b.client.NewRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, nil, err
}
newValues, err := query.Values(options)
if err != nil {
return nil, nil, err
}
req.URL.RawQuery = newValues.Encode()
ipv4 := new(ipBase)
if err = b.client.DoWithContext(ctx, req, ipv4); err != nil {
return nil, nil, err
}
return ipv4.IPv4s, ipv4.Meta, nil
}
// ListIPv6s information of a Bare Metal server.
// IP information is only available for Bare Metal servers in the "active" state.
// If the Bare Metal server does not have IPv6 enabled, then an empty array is returned.
func (b *BareMetalServerServiceHandler) ListIPv6s(ctx context.Context, serverID string, options *ListOptions) ([]IPv6, *Meta, error) {
uri := fmt.Sprintf("%s/%s/ipv6", bmPath, serverID)
req, err := b.client.NewRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, nil, err
}
newValues, err := query.Values(options)
if err != nil {
return nil, nil, err
}
req.URL.RawQuery = newValues.Encode()
ipv6 := new(ipBase)
if err = b.client.DoWithContext(ctx, req, ipv6); err != nil {
return nil, nil, err
}
return ipv6.IPv6s, ipv6.Meta, nil
}
// Halt a Bare Metal server.
// This is a hard power off, meaning that the power to the machine is severed.
// The data on the machine will not be modified, and you will still be billed for the machine.
func (b *BareMetalServerServiceHandler) Halt(ctx context.Context, serverID string) error {
uri := fmt.Sprintf("%s/%s/halt", bmPath, serverID)
req, err := b.client.NewRequest(ctx, http.MethodPost, uri, nil)
if err != nil {
return err
}
return b.client.DoWithContext(ctx, req, nil)
}
// Reboot a Bare Metal server. This is a hard reboot, which means that the server is powered off, then back on.
func (b *BareMetalServerServiceHandler) Reboot(ctx context.Context, serverID string) error {
uri := fmt.Sprintf("%s/%s/reboot", bmPath, serverID)
req, err := b.client.NewRequest(ctx, http.MethodPost, uri, nil)
if err != nil {
return err
}
return b.client.DoWithContext(ctx, req, nil)
}
// Start a Bare Metal server.
func (b *BareMetalServerServiceHandler) Start(ctx context.Context, serverID string) error {
uri := fmt.Sprintf("%s/%s/start", bmPath, serverID)
req, err := b.client.NewRequest(ctx, http.MethodPost, uri, nil)
if err != nil {
return err
}
return b.client.DoWithContext(ctx, req, nil)
}
// Reinstall the operating system on a Bare Metal server.
// All data will be permanently lost, but the IP address will remain the same.
func (b *BareMetalServerServiceHandler) Reinstall(ctx context.Context, serverID string) (*BareMetalServer, error) {
uri := fmt.Sprintf("%s/%s/reinstall", bmPath, serverID)
req, err := b.client.NewRequest(ctx, http.MethodPost, uri, nil)
if err != nil {
return nil, err
}
bms := new(bareMetalBase)
if err = b.client.DoWithContext(ctx, req, bms); err != nil {
return nil, err
}
return bms.BareMetal, nil
}
// MassStart will start a list of Bare Metal servers the machine is already running, it will be restarted.
func (b *BareMetalServerServiceHandler) MassStart(ctx context.Context, serverList []string) error {
uri := fmt.Sprintf("%s/start", bmPath)
reqBody := RequestBody{"baremetal_ids": serverList}
req, err := b.client.NewRequest(ctx, http.MethodPost, uri, reqBody)
if err != nil {
return err
}
return b.client.DoWithContext(ctx, req, nil)
}
// MassHalt a list of Bare Metal servers.
func (b *BareMetalServerServiceHandler) MassHalt(ctx context.Context, serverList []string) error {
uri := fmt.Sprintf("%s/halt", bmPath)
reqBody := RequestBody{"baremetal_ids": serverList}
req, err := b.client.NewRequest(ctx, http.MethodPost, uri, reqBody)
if err != nil {
return err
}
return b.client.DoWithContext(ctx, req, nil)
}
// MassReboot a list of Bare Metal servers.
func (b *BareMetalServerServiceHandler) MassReboot(ctx context.Context, serverList []string) error {
uri := fmt.Sprintf("%s/reboot", bmPath)
reqBody := RequestBody{"baremetal_ids": serverList}
req, err := b.client.NewRequest(ctx, http.MethodPost, uri, reqBody)
if err != nil {
return err
}
return b.client.DoWithContext(ctx, req, nil)
}
// GetUpgrades that are available for a Bare Metal server.
func (b *BareMetalServerServiceHandler) GetUpgrades(ctx context.Context, serverID string) (*Upgrades, error) {
uri := fmt.Sprintf("%s/%s/upgrades", bmPath, serverID)
req, err := b.client.NewRequest(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, err
}
upgrades := new(upgradeBase)
if err = b.client.DoWithContext(ctx, req, upgrades); err != nil {
return nil, err
}
return upgrades.Upgrades, nil
}
|