ACME DNS challenges

This commit is contained in:
Ludovic Fernandez 2018-10-10 16:28:04 +02:00 committed by Traefiker Bot
parent 7a2592b2fa
commit 5bdf8a5ea3
127 changed files with 24386 additions and 739 deletions

21
vendor/github.com/linode/linodego/LICENSE generated vendored Normal file
View file

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2017 Christopher "Chief" Najewicz
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.

44
vendor/github.com/linode/linodego/account.go generated vendored Normal file
View file

@ -0,0 +1,44 @@
package linodego
import "context"
// Account associated with the token in use
type Account struct {
FirstName string `json:"first_name"`
LastName string `json:"last_name"`
Email string `json:"email"`
Company string `json:"company"`
Address1 string `json:"address1"`
Address2 string `json:"address2"`
Balance float32 `json:"balance"`
City string `json:"city"`
State string `json:"state"`
Zip string `json:"zip"`
Country string `json:"country"`
TaxID string `json:"tax_id"`
CreditCard *CreditCard `json:"credit_card"`
}
// CreditCard information associated with the Account.
type CreditCard struct {
LastFour string `json:"last_four"`
Expiry string `json:"expiry"`
}
// fixDates converts JSON timestamps to Go time.Time values
func (v *Account) fixDates() *Account {
return v
}
// GetAccount gets the contact and billing information related to the Account
func (c *Client) GetAccount(ctx context.Context) (*Account, error) {
e, err := c.Account.Endpoint()
if err != nil {
return nil, err
}
r, err := coupleAPIErrors(c.R(ctx).SetResult(&Account{}).Get(e))
if err != nil {
return nil, err
}
return r.Result().(*Account).fixDates(), nil
}

277
vendor/github.com/linode/linodego/account_events.go generated vendored Normal file
View file

@ -0,0 +1,277 @@
package linodego
import (
"context"
"encoding/json"
"fmt"
"log"
"strconv"
"strings"
"time"
)
// Event represents an action taken on the Account.
type Event struct {
CreatedStr string `json:"created"`
// The unique ID of this Event.
ID int `json:"id"`
// Current status of the Event, Enum: "failed" "finished" "notification" "scheduled" "started"
Status EventStatus `json:"status"`
// The action that caused this Event. New actions may be added in the future.
Action EventAction `json:"action"`
// A percentage estimating the amount of time remaining for an Event. Returns null for notification events.
PercentComplete int `json:"percent_complete"`
// The rate of completion of the Event. Only some Events will return rate; for example, migration and resize Events.
Rate *string `json:"rate"`
// If this Event has been read.
Read bool `json:"read"`
// If this Event has been seen.
Seen bool `json:"seen"`
// The estimated time remaining until the completion of this Event. This value is only returned for in-progress events.
TimeRemainingMsg json.RawMessage `json:"time_remaining"`
TimeRemaining *int `json:"-"`
// The username of the User who caused the Event.
Username string `json:"username"`
// Detailed information about the Event's entity, including ID, type, label, and URL used to access it.
Entity *EventEntity `json:"entity"`
// When this Event was created.
Created *time.Time `json:"-"`
}
// EventAction constants start with Action and include all known Linode API Event Actions.
type EventAction string
// EventAction constants represent the actions that cause an Event. New actions may be added in the future.
const (
ActionBackupsEnable EventAction = "backups_enable"
ActionBackupsCancel EventAction = "backups_cancel"
ActionBackupsRestore EventAction = "backups_restore"
ActionCommunityQuestionReply EventAction = "community_question_reply"
ActionCreateCardUpdated EventAction = "credit_card_updated"
ActionDiskCreate EventAction = "disk_create"
ActionDiskDelete EventAction = "disk_delete"
ActionDiskDuplicate EventAction = "disk_duplicate"
ActionDiskImagize EventAction = "disk_imagize"
ActionDiskResize EventAction = "disk_resize"
ActionDNSRecordCreate EventAction = "dns_record_create"
ActionDNSRecordDelete EventAction = "dns_record_delete"
ActionDNSZoneCreate EventAction = "dns_zone_create"
ActionDNSZoneDelete EventAction = "dns_zone_delete"
ActionImageDelete EventAction = "image_delete"
ActionLinodeAddIP EventAction = "linode_addip"
ActionLinodeBoot EventAction = "linode_boot"
ActionLinodeClone EventAction = "linode_clone"
ActionLinodeCreate EventAction = "linode_create"
ActionLinodeDelete EventAction = "linode_delete"
ActionLinodeDeleteIP EventAction = "linode_deleteip"
ActionLinodeMigrate EventAction = "linode_migrate"
ActionLinodeMutate EventAction = "linode_mutate"
ActionLinodeReboot EventAction = "linode_reboot"
ActionLinodeRebuild EventAction = "linode_rebuild"
ActionLinodeResize EventAction = "linode_resize"
ActionLinodeShutdown EventAction = "linode_shutdown"
ActionLinodeSnapshot EventAction = "linode_snapshot"
ActionLongviewClientCreate EventAction = "longviewclient_create"
ActionLongviewClientDelete EventAction = "longviewclient_delete"
ActionManagedDisabled EventAction = "managed_disabled"
ActionManagedEnabled EventAction = "managed_enabled"
ActionManagedServiceCreate EventAction = "managed_service_create"
ActionManagedServiceDelete EventAction = "managed_service_delete"
ActionNodebalancerCreate EventAction = "nodebalancer_create"
ActionNodebalancerDelete EventAction = "nodebalancer_delete"
ActionNodebalancerConfigCreate EventAction = "nodebalancer_config_create"
ActionNodebalancerConfigDelete EventAction = "nodebalancer_config_delete"
ActionPasswordReset EventAction = "password_reset"
ActionPaymentSubmitted EventAction = "payment_submitted"
ActionStackScriptCreate EventAction = "stackscript_create"
ActionStackScriptDelete EventAction = "stackscript_delete"
ActionStackScriptPublicize EventAction = "stackscript_publicize"
ActionStackScriptRevise EventAction = "stackscript_revise"
ActionTFADisabled EventAction = "tfa_disabled"
ActionTFAEnabled EventAction = "tfa_enabled"
ActionTicketAttachmentUpload EventAction = "ticket_attachment_upload"
ActionTicketCreate EventAction = "ticket_create"
ActionTicketReply EventAction = "ticket_reply"
ActionVolumeAttach EventAction = "volume_attach"
ActionVolumeClone EventAction = "volume_clone"
ActionVolumeCreate EventAction = "volume_create"
ActionVolumeDelte EventAction = "volume_delete"
ActionVolumeDetach EventAction = "volume_detach"
ActionVolumeResize EventAction = "volume_resize"
)
// EntityType constants start with Entity and include Linode API Event Entity Types
type EntityType string
// EntityType contants are the entities an Event can be related to
const (
EntityLinode EntityType = "linode"
EntityDisk EntityType = "disk"
)
// EventStatus constants start with Event and include Linode API Event Status values
type EventStatus string
// EventStatus constants reflect the current status of an Event
const (
EventFailed EventStatus = "failed"
EventFinished EventStatus = "finished"
EventNotification EventStatus = "notification"
EventScheduled EventStatus = "scheduled"
EventStarted EventStatus = "started"
)
// EventEntity provides detailed information about the Event's
// associated entity, including ID, Type, Label, and a URL that
// can be used to access it.
type EventEntity struct {
// ID may be a string or int, it depends on the EntityType
ID interface{} `json:"id"`
Label string `json:"label"`
Type EntityType `json:"type"`
URL string `json:"url"`
}
// EventsPagedResponse represents a paginated Events API response
type EventsPagedResponse struct {
*PageOptions
Data []Event `json:"data"`
}
// endpoint gets the endpoint URL for Event
func (EventsPagedResponse) endpoint(c *Client) string {
endpoint, err := c.Events.Endpoint()
if err != nil {
panic(err)
}
return endpoint
}
// endpointWithID gets the endpoint URL for a specific Event
func (e Event) endpointWithID(c *Client) string {
endpoint, err := c.Events.Endpoint()
if err != nil {
panic(err)
}
endpoint = fmt.Sprintf("%s/%d", endpoint, e.ID)
return endpoint
}
// appendData appends Events when processing paginated Event responses
func (resp *EventsPagedResponse) appendData(r *EventsPagedResponse) {
resp.Data = append(resp.Data, r.Data...)
}
// ListEvents gets a collection of Event objects representing actions taken
// on the Account. The Events returned depend on the token grants and the grants
// of the associated user.
func (c *Client) ListEvents(ctx context.Context, opts *ListOptions) ([]Event, error) {
response := EventsPagedResponse{}
err := c.listHelper(ctx, &response, opts)
for i := range response.Data {
response.Data[i].fixDates()
}
if err != nil {
return nil, err
}
return response.Data, nil
}
// GetEvent gets the Event with the Event ID
func (c *Client) GetEvent(ctx context.Context, id int) (*Event, error) {
e, err := c.Events.Endpoint()
if err != nil {
return nil, err
}
e = fmt.Sprintf("%s/%d", e, id)
r, err := c.R(ctx).SetResult(&Event{}).Get(e)
if err != nil {
return nil, err
}
return r.Result().(*Event).fixDates(), nil
}
// fixDates converts JSON timestamps to Go time.Time values
func (e *Event) fixDates() *Event {
e.Created, _ = parseDates(e.CreatedStr)
e.TimeRemaining = unmarshalTimeRemaining(e.TimeRemainingMsg)
return e
}
// MarkEventRead marks a single Event as read.
func (c *Client) MarkEventRead(ctx context.Context, event *Event) error {
e := event.endpointWithID(c)
e = fmt.Sprintf("%s/read", e)
_, err := coupleAPIErrors(c.R(ctx).Post(e))
return err
}
// MarkEventsSeen marks all Events up to and including this Event by ID as seen.
func (c *Client) MarkEventsSeen(ctx context.Context, event *Event) error {
e := event.endpointWithID(c)
e = fmt.Sprintf("%s/seen", e)
_, err := coupleAPIErrors(c.R(ctx).Post(e))
return err
}
func unmarshalTimeRemaining(m json.RawMessage) *int {
jsonBytes, err := m.MarshalJSON()
if err != nil {
panic(jsonBytes)
}
if len(jsonBytes) == 4 && string(jsonBytes) == "null" {
return nil
}
var timeStr string
if err := json.Unmarshal(jsonBytes, &timeStr); err == nil && len(timeStr) > 0 {
if dur, err := durationToSeconds(timeStr); err != nil {
panic(err)
} else {
return &dur
}
} else {
var intPtr int
if err := json.Unmarshal(jsonBytes, &intPtr); err == nil {
return &intPtr
}
}
log.Println("[WARN] Unexpected unmarshalTimeRemaining value: ", jsonBytes)
return nil
}
// durationToSeconds takes a hh:mm:ss string and returns the number of seconds
func durationToSeconds(s string) (int, error) {
multipliers := [3]int{60 * 60, 60, 1}
segs := strings.Split(s, ":")
if len(segs) > len(multipliers) {
return 0, fmt.Errorf("too many ':' separators in time duration: %s", s)
}
var d int
l := len(segs)
for i := 0; i < l; i++ {
m, err := strconv.Atoi(segs[i])
if err != nil {
return 0, err
}
d += m * multipliers[i+len(multipliers)-l]
}
return d, nil
}

125
vendor/github.com/linode/linodego/account_invoices.go generated vendored Normal file
View file

@ -0,0 +1,125 @@
package linodego
import (
"context"
"fmt"
"time"
)
// Invoice structs reflect an invoice for billable activity on the account.
type Invoice struct {
DateStr string `json:"date"`
ID int `json:"id"`
Label string `json:"label"`
Total float32 `json:"total"`
Date *time.Time `json:"-"`
}
// InvoiceItem structs reflect an single billable activity associate with an Invoice
type InvoiceItem struct {
FromStr string `json:"from"`
ToStr string `json:"to"`
Label string `json:"label"`
Type string `json:"type"`
UnitPrice int `json:"unitprice"`
Quantity int `json:"quantity"`
Amount float32 `json:"amount"`
From *time.Time `json:"-"`
To *time.Time `json:"-"`
}
// InvoicesPagedResponse represents a paginated Invoice API response
type InvoicesPagedResponse struct {
*PageOptions
Data []Invoice `json:"data"`
}
// endpoint gets the endpoint URL for Invoice
func (InvoicesPagedResponse) endpoint(c *Client) string {
endpoint, err := c.Invoices.Endpoint()
if err != nil {
panic(err)
}
return endpoint
}
// appendData appends Invoices when processing paginated Invoice responses
func (resp *InvoicesPagedResponse) appendData(r *InvoicesPagedResponse) {
resp.Data = append(resp.Data, r.Data...)
}
// ListInvoices gets a paginated list of Invoices against the Account
func (c *Client) ListInvoices(ctx context.Context, opts *ListOptions) ([]Invoice, error) {
response := InvoicesPagedResponse{}
err := c.listHelper(ctx, &response, opts)
for i := range response.Data {
response.Data[i].fixDates()
}
if err != nil {
return nil, err
}
return response.Data, nil
}
// fixDates converts JSON timestamps to Go time.Time values
func (v *Invoice) fixDates() *Invoice {
v.Date, _ = parseDates(v.DateStr)
return v
}
// fixDates converts JSON timestamps to Go time.Time values
func (v *InvoiceItem) fixDates() *InvoiceItem {
v.From, _ = parseDates(v.FromStr)
v.To, _ = parseDates(v.ToStr)
return v
}
// GetInvoice gets the a single Invoice matching the provided ID
func (c *Client) GetInvoice(ctx context.Context, id int) (*Invoice, error) {
e, err := c.Invoices.Endpoint()
if err != nil {
return nil, err
}
e = fmt.Sprintf("%s/%d", e, id)
r, err := coupleAPIErrors(c.R(ctx).SetResult(&Invoice{}).Get(e))
if err != nil {
return nil, err
}
return r.Result().(*Invoice).fixDates(), nil
}
// InvoiceItemsPagedResponse represents a paginated Invoice Item API response
type InvoiceItemsPagedResponse struct {
*PageOptions
Data []InvoiceItem `json:"data"`
}
// endpointWithID gets the endpoint URL for InvoiceItems associated with a specific Invoice
func (InvoiceItemsPagedResponse) endpointWithID(c *Client, id int) string {
endpoint, err := c.InvoiceItems.endpointWithID(id)
if err != nil {
panic(err)
}
return endpoint
}
// appendData appends InvoiceItems when processing paginated Invoice Item responses
func (resp *InvoiceItemsPagedResponse) appendData(r *InvoiceItemsPagedResponse) {
resp.Data = append(resp.Data, r.Data...)
}
// ListInvoiceItems gets the invoice items associated with a specific Invoice
func (c *Client) ListInvoiceItems(ctx context.Context, id int, opts *ListOptions) ([]InvoiceItem, error) {
response := InvoiceItemsPagedResponse{}
err := c.listHelperWithID(ctx, &response, id, opts)
for i := range response.Data {
response.Data[i].fixDates()
}
if err != nil {
return nil, err
}
return response.Data, nil
}

View file

@ -0,0 +1,73 @@
package linodego
import (
"context"
"time"
)
// Notification represents a notification on an Account
type Notification struct {
UntilStr string `json:"until"`
WhenStr string `json:"when"`
Label string `json:"label"`
Message string `json:"message"`
Type string `json:"type"`
Severity string `json:"severity"`
Entity *NotificationEntity `json:"entity"`
Until *time.Time `json:"-"`
When *time.Time `json:"-"`
}
// NotificationEntity adds detailed information about the Notification.
// This could refer to the ticket that triggered the notification, for example.
type NotificationEntity struct {
ID int `json:"id"`
Label string `json:"label"`
Type string `json:"type"`
URL string `json:"url"`
}
// NotificationsPagedResponse represents a paginated Notifications API response
type NotificationsPagedResponse struct {
*PageOptions
Data []Notification `json:"data"`
}
// endpoint gets the endpoint URL for Notification
func (NotificationsPagedResponse) endpoint(c *Client) string {
endpoint, err := c.Notifications.Endpoint()
if err != nil {
panic(err)
}
return endpoint
}
// appendData appends Notifications when processing paginated Notification responses
func (resp *NotificationsPagedResponse) appendData(r *NotificationsPagedResponse) {
resp.Data = append(resp.Data, r.Data...)
}
// ListNotifications gets a collection of Notification objects representing important,
// often time-sensitive items related to the Account. An account cannot interact directly with
// Notifications, and a Notification will disappear when the circumstances causing it
// have been resolved. For example, if the account has an important Ticket open, a response
// to the Ticket will dismiss the Notification.
func (c *Client) ListNotifications(ctx context.Context, opts *ListOptions) ([]Notification, error) {
response := NotificationsPagedResponse{}
err := c.listHelper(ctx, &response, opts)
for i := range response.Data {
response.Data[i].fixDates()
}
if err != nil {
return nil, err
}
return response.Data, nil
}
// fixDates converts JSON timestamps to Go time.Time values
func (v *Notification) fixDates() *Notification {
v.Until, _ = parseDates(v.UntilStr)
v.When, _ = parseDates(v.WhenStr)
return v
}

201
vendor/github.com/linode/linodego/client.go generated vendored Normal file
View file

@ -0,0 +1,201 @@
package linodego
import (
"context"
"fmt"
"log"
"net/http"
"os"
"strconv"
"github.com/go-resty/resty"
)
const (
// APIHost Linode API hostname
APIHost = "api.linode.com"
// APIVersion Linode API version
APIVersion = "v4"
// APIProto connect to API with http(s)
APIProto = "https"
// Version of linodego
Version = "0.5.1"
// APIEnvVar environment var to check for API token
APIEnvVar = "LINODE_TOKEN"
// APISecondsPerPoll how frequently to poll for new Events
APISecondsPerPoll = 10
)
// DefaultUserAgent is the default User-Agent sent in HTTP request headers
const DefaultUserAgent = "linodego " + Version + " https://github.com/linode/linodego"
var envDebug = false
// Client is a wrapper around the Resty client
type Client struct {
resty *resty.Client
userAgent string
resources map[string]*Resource
debug bool
Images *Resource
InstanceDisks *Resource
InstanceConfigs *Resource
InstanceSnapshots *Resource
InstanceIPs *Resource
InstanceVolumes *Resource
Instances *Resource
IPAddresses *Resource
IPv6Pools *Resource
IPv6Ranges *Resource
Regions *Resource
StackScripts *Resource
Volumes *Resource
Kernels *Resource
Types *Resource
Domains *Resource
DomainRecords *Resource
Longview *Resource
LongviewClients *Resource
LongviewSubscriptions *Resource
NodeBalancers *Resource
NodeBalancerConfigs *Resource
NodeBalancerNodes *Resource
SSHKeys *Resource
Tickets *Resource
Account *Resource
Invoices *Resource
InvoiceItems *Resource
Events *Resource
Notifications *Resource
Profile *Resource
Managed *Resource
}
func init() {
// Wether or not we will enable Resty debugging output
if apiDebug, ok := os.LookupEnv("LINODE_DEBUG"); ok {
if parsed, err := strconv.ParseBool(apiDebug); err == nil {
envDebug = parsed
log.Println("[INFO] LINODE_DEBUG being set to", envDebug)
} else {
log.Println("[WARN] LINODE_DEBUG should be an integer, 0 or 1")
}
}
}
// SetUserAgent sets a custom user-agent for HTTP requests
func (c *Client) SetUserAgent(ua string) *Client {
c.userAgent = ua
c.resty.SetHeader("User-Agent", c.userAgent)
return c
}
// R wraps resty's R method
func (c *Client) R(ctx context.Context) *resty.Request {
return c.resty.R().
ExpectContentType("application/json").
SetHeader("Content-Type", "application/json").
SetContext(ctx).
SetError(APIError{})
}
// SetDebug sets the debug on resty's client
func (c *Client) SetDebug(debug bool) *Client {
c.debug = debug
c.resty.SetDebug(debug)
return c
}
// SetBaseURL sets the base URL of the Linode v4 API (https://api.linode.com/v4)
func (c *Client) SetBaseURL(url string) *Client {
c.resty.SetHostURL(url)
return c
}
// Resource looks up a resource by name
func (c Client) Resource(resourceName string) *Resource {
selectedResource, ok := c.resources[resourceName]
if !ok {
log.Fatalf("Could not find resource named '%s', exiting.", resourceName)
}
return selectedResource
}
// NewClient factory to create new Client struct
func NewClient(hc *http.Client) (client Client) {
restyClient := resty.NewWithClient(hc)
client.resty = restyClient
client.SetUserAgent(DefaultUserAgent)
client.SetBaseURL(fmt.Sprintf("%s://%s/%s", APIProto, APIHost, APIVersion))
resources := map[string]*Resource{
stackscriptsName: NewResource(&client, stackscriptsName, stackscriptsEndpoint, false, Stackscript{}, StackscriptsPagedResponse{}),
imagesName: NewResource(&client, imagesName, imagesEndpoint, false, Image{}, ImagesPagedResponse{}),
instancesName: NewResource(&client, instancesName, instancesEndpoint, false, Instance{}, InstancesPagedResponse{}),
instanceDisksName: NewResource(&client, instanceDisksName, instanceDisksEndpoint, true, InstanceDisk{}, InstanceDisksPagedResponse{}),
instanceConfigsName: NewResource(&client, instanceConfigsName, instanceConfigsEndpoint, true, InstanceConfig{}, InstanceConfigsPagedResponse{}),
instanceSnapshotsName: NewResource(&client, instanceSnapshotsName, instanceSnapshotsEndpoint, true, InstanceSnapshot{}, nil),
instanceIPsName: NewResource(&client, instanceIPsName, instanceIPsEndpoint, true, InstanceIP{}, nil), // really?
instanceVolumesName: NewResource(&client, instanceVolumesName, instanceVolumesEndpoint, true, nil, InstanceVolumesPagedResponse{}), // really?
ipaddressesName: NewResource(&client, ipaddressesName, ipaddressesEndpoint, false, nil, IPAddressesPagedResponse{}), // really?
ipv6poolsName: NewResource(&client, ipv6poolsName, ipv6poolsEndpoint, false, nil, IPv6PoolsPagedResponse{}), // really?
ipv6rangesName: NewResource(&client, ipv6rangesName, ipv6rangesEndpoint, false, IPv6Range{}, IPv6RangesPagedResponse{}),
regionsName: NewResource(&client, regionsName, regionsEndpoint, false, Region{}, RegionsPagedResponse{}),
volumesName: NewResource(&client, volumesName, volumesEndpoint, false, Volume{}, VolumesPagedResponse{}),
kernelsName: NewResource(&client, kernelsName, kernelsEndpoint, false, LinodeKernel{}, LinodeKernelsPagedResponse{}),
typesName: NewResource(&client, typesName, typesEndpoint, false, LinodeType{}, LinodeTypesPagedResponse{}),
domainsName: NewResource(&client, domainsName, domainsEndpoint, false, Domain{}, DomainsPagedResponse{}),
domainRecordsName: NewResource(&client, domainRecordsName, domainRecordsEndpoint, true, DomainRecord{}, DomainRecordsPagedResponse{}),
longviewName: NewResource(&client, longviewName, longviewEndpoint, false, nil, nil), // really?
longviewclientsName: NewResource(&client, longviewclientsName, longviewclientsEndpoint, false, LongviewClient{}, LongviewClientsPagedResponse{}),
longviewsubscriptionsName: NewResource(&client, longviewsubscriptionsName, longviewsubscriptionsEndpoint, false, LongviewSubscription{}, LongviewSubscriptionsPagedResponse{}),
nodebalancersName: NewResource(&client, nodebalancersName, nodebalancersEndpoint, false, NodeBalancer{}, NodeBalancerConfigsPagedResponse{}),
nodebalancerconfigsName: NewResource(&client, nodebalancerconfigsName, nodebalancerconfigsEndpoint, true, NodeBalancerConfig{}, NodeBalancerConfigsPagedResponse{}),
nodebalancernodesName: NewResource(&client, nodebalancernodesName, nodebalancernodesEndpoint, true, NodeBalancerNode{}, NodeBalancerNodesPagedResponse{}),
sshkeysName: NewResource(&client, sshkeysName, sshkeysEndpoint, false, SSHKey{}, SSHKeysPagedResponse{}),
ticketsName: NewResource(&client, ticketsName, ticketsEndpoint, false, Ticket{}, TicketsPagedResponse{}),
accountName: NewResource(&client, accountName, accountEndpoint, false, Account{}, nil), // really?
eventsName: NewResource(&client, eventsName, eventsEndpoint, false, Event{}, EventsPagedResponse{}),
invoicesName: NewResource(&client, invoicesName, invoicesEndpoint, false, Invoice{}, InvoicesPagedResponse{}),
invoiceItemsName: NewResource(&client, invoiceItemsName, invoiceItemsEndpoint, true, InvoiceItem{}, InvoiceItemsPagedResponse{}),
profileName: NewResource(&client, profileName, profileEndpoint, false, nil, nil), // really?
managedName: NewResource(&client, managedName, managedEndpoint, false, nil, nil), // really?
}
client.resources = resources
client.SetDebug(envDebug)
client.Images = resources[imagesName]
client.StackScripts = resources[stackscriptsName]
client.Instances = resources[instancesName]
client.Regions = resources[regionsName]
client.InstanceDisks = resources[instanceDisksName]
client.InstanceConfigs = resources[instanceConfigsName]
client.InstanceSnapshots = resources[instanceSnapshotsName]
client.InstanceIPs = resources[instanceIPsName]
client.InstanceVolumes = resources[instanceVolumesName]
client.IPAddresses = resources[ipaddressesName]
client.IPv6Pools = resources[ipv6poolsName]
client.IPv6Ranges = resources[ipv6rangesName]
client.Volumes = resources[volumesName]
client.Kernels = resources[kernelsName]
client.Types = resources[typesName]
client.Domains = resources[domainsName]
client.DomainRecords = resources[domainRecordsName]
client.Longview = resources[longviewName]
client.LongviewSubscriptions = resources[longviewsubscriptionsName]
client.NodeBalancers = resources[nodebalancersName]
client.NodeBalancerConfigs = resources[nodebalancerconfigsName]
client.NodeBalancerNodes = resources[nodebalancernodesName]
client.SSHKeys = resources[sshkeysName]
client.Tickets = resources[ticketsName]
client.Account = resources[accountName]
client.Events = resources[eventsName]
client.Invoices = resources[invoicesName]
client.Profile = resources[profileName]
client.Managed = resources[managedName]
return
}

211
vendor/github.com/linode/linodego/domain_records.go generated vendored Normal file
View file

@ -0,0 +1,211 @@
package linodego
import (
"context"
"encoding/json"
"fmt"
)
// DomainRecord represents a DomainRecord object
type DomainRecord struct {
ID int `json:"id"`
Type DomainRecordType `json:"type"`
Name string `json:"name"`
Target string `json:"target"`
Priority int `json:"priority"`
Weight int `json:"weight"`
Port int `json:"port"`
Service *string `json:"service"`
Protocol *string `json:"protocol"`
TTLSec int `json:"ttl_sec"`
Tag *string `json:"tag"`
}
// DomainRecordCreateOptions fields are those accepted by CreateDomainRecord
type DomainRecordCreateOptions struct {
Type DomainRecordType `json:"type"`
Name string `json:"name"`
Target string `json:"target"`
Priority *int `json:"priority,omitempty"`
Weight *int `json:"weight,omitempty"`
Port *int `json:"port,omitempty"`
Service *string `json:"service,omitempty"`
Protocol *string `json:"protocol,omitempty"`
TTLSec int `json:"ttl_sec,omitempty"` // 0 is not accepted by Linode, so can be omitted
Tag *string `json:"tag,omitempty"`
}
// DomainRecordUpdateOptions fields are those accepted by UpdateDomainRecord
type DomainRecordUpdateOptions struct {
Type DomainRecordType `json:"type,omitempty"`
Name string `json:"name,omitempty"`
Target string `json:"target,omitempty"`
Priority *int `json:"priority,omitempty"` // 0 is valid, so omit only nil values
Weight *int `json:"weight,omitempty"` // 0 is valid, so omit only nil values
Port *int `json:"port,omitempty"` // 0 is valid to spec, so omit only nil values
Service *string `json:"service,omitempty"`
Protocol *string `json:"protocol,omitempty"`
TTLSec int `json:"ttl_sec,omitempty"` // 0 is not accepted by Linode, so can be omitted
Tag *string `json:"tag,omitempty"`
}
// DomainRecordType constants start with RecordType and include Linode API Domain Record Types
type DomainRecordType string
// DomainRecordType contants are the DNS record types a DomainRecord can assign
const (
RecordTypeA DomainRecordType = "A"
RecordTypeAAAA DomainRecordType = "AAAA"
RecordTypeNS DomainRecordType = "NS"
RecordTypeMX DomainRecordType = "MX"
RecordTypeCNAME DomainRecordType = "CNAME"
RecordTypeTXT DomainRecordType = "TXT"
RecordTypeSRV DomainRecordType = "SRV"
RecordTypePTR DomainRecordType = "PTR"
RecordTypeCAA DomainRecordType = "CAA"
)
// GetUpdateOptions converts a DomainRecord to DomainRecordUpdateOptions for use in UpdateDomainRecord
func (d DomainRecord) GetUpdateOptions() (du DomainRecordUpdateOptions) {
du.Type = d.Type
du.Name = d.Name
du.Target = d.Target
du.Priority = copyInt(&d.Priority)
du.Weight = copyInt(&d.Weight)
du.Port = copyInt(&d.Port)
du.Service = copyString(d.Service)
du.Protocol = copyString(d.Protocol)
du.TTLSec = d.TTLSec
du.Tag = copyString(d.Tag)
return
}
func copyInt(iPtr *int) *int {
if iPtr == nil {
return nil
}
var t = *iPtr
return &t
}
func copyString(sPtr *string) *string {
if sPtr == nil {
return nil
}
var t = *sPtr
return &t
}
// DomainRecordsPagedResponse represents a paginated DomainRecord API response
type DomainRecordsPagedResponse struct {
*PageOptions
Data []DomainRecord `json:"data"`
}
// endpoint gets the endpoint URL for InstanceConfig
func (DomainRecordsPagedResponse) endpointWithID(c *Client, id int) string {
endpoint, err := c.DomainRecords.endpointWithID(id)
if err != nil {
panic(err)
}
return endpoint
}
// appendData appends DomainRecords when processing paginated DomainRecord responses
func (resp *DomainRecordsPagedResponse) appendData(r *DomainRecordsPagedResponse) {
resp.Data = append(resp.Data, r.Data...)
}
// ListDomainRecords lists DomainRecords
func (c *Client) ListDomainRecords(ctx context.Context, domainID int, opts *ListOptions) ([]DomainRecord, error) {
response := DomainRecordsPagedResponse{}
err := c.listHelperWithID(ctx, &response, domainID, opts)
if err != nil {
return nil, err
}
return response.Data, nil
}
// fixDates converts JSON timestamps to Go time.Time values
func (d *DomainRecord) fixDates() *DomainRecord {
return d
}
// GetDomainRecord gets the domainrecord with the provided ID
func (c *Client) GetDomainRecord(ctx context.Context, domainID int, id int) (*DomainRecord, error) {
e, err := c.DomainRecords.endpointWithID(domainID)
if err != nil {
return nil, err
}
e = fmt.Sprintf("%s/%d", e, id)
r, err := coupleAPIErrors(c.R(ctx).SetResult(&DomainRecord{}).Get(e))
if err != nil {
return nil, err
}
return r.Result().(*DomainRecord), nil
}
// CreateDomainRecord creates a DomainRecord
func (c *Client) CreateDomainRecord(ctx context.Context, domainID int, domainrecord DomainRecordCreateOptions) (*DomainRecord, error) {
var body string
e, err := c.DomainRecords.endpointWithID(domainID)
if err != nil {
return nil, err
}
req := c.R(ctx).SetResult(&DomainRecord{})
bodyData, err := json.Marshal(domainrecord)
if err != nil {
return nil, NewError(err)
}
body = string(bodyData)
r, err := coupleAPIErrors(req.
SetBody(body).
Post(e))
if err != nil {
return nil, err
}
return r.Result().(*DomainRecord).fixDates(), nil
}
// UpdateDomainRecord updates the DomainRecord with the specified id
func (c *Client) UpdateDomainRecord(ctx context.Context, domainID int, id int, domainrecord DomainRecordUpdateOptions) (*DomainRecord, error) {
var body string
e, err := c.DomainRecords.endpointWithID(domainID)
if err != nil {
return nil, err
}
e = fmt.Sprintf("%s/%d", e, id)
req := c.R(ctx).SetResult(&DomainRecord{})
if bodyData, err := json.Marshal(domainrecord); err == nil {
body = string(bodyData)
} else {
return nil, NewError(err)
}
r, err := coupleAPIErrors(req.
SetBody(body).
Put(e))
if err != nil {
return nil, err
}
return r.Result().(*DomainRecord).fixDates(), nil
}
// DeleteDomainRecord deletes the DomainRecord with the specified id
func (c *Client) DeleteDomainRecord(ctx context.Context, domainID int, id int) error {
e, err := c.DomainRecords.endpointWithID(domainID)
if err != nil {
return err
}
e = fmt.Sprintf("%s/%d", e, id)
_, err = coupleAPIErrors(c.R(ctx).Delete(e))
return err
}

285
vendor/github.com/linode/linodego/domains.go generated vendored Normal file
View file

@ -0,0 +1,285 @@
package linodego
import (
"context"
"encoding/json"
"fmt"
)
// Domain represents a Domain object
type Domain struct {
// This Domain's unique ID
ID int `json:"id"`
// The domain this Domain represents. These must be unique in our system; you cannot have two Domains representing the same domain.
Domain string `json:"domain"`
// If this Domain represents the authoritative source of information for the domain it describes, or if it is a read-only copy of a master (also called a slave).
Type DomainType `json:"type"` // Enum:"master" "slave"
// Deprecated: The group this Domain belongs to. This is for display purposes only.
Group string `json:"group"`
// Used to control whether this Domain is currently being rendered.
Status DomainStatus `json:"status"` // Enum:"disabled" "active" "edit_mode" "has_errors"
// A description for this Domain. This is for display purposes only.
Description string `json:"description"`
// Start of Authority email address. This is required for master Domains.
SOAEmail string `json:"soa_email"`
// The interval, in seconds, at which a failed refresh should be retried.
// Valid values are 300, 3600, 7200, 14400, 28800, 57600, 86400, 172800, 345600, 604800, 1209600, and 2419200 - any other value will be rounded to the nearest valid value.
RetrySec int `json:"retry_sec"`
// The IP addresses representing the master DNS for this Domain.
MasterIPs []string `json:"master_ips"`
// The list of IPs that may perform a zone transfer for this Domain. This is potentially dangerous, and should be set to an empty list unless you intend to use it.
AXfrIPs []string `json:"axfr_ips"`
// The amount of time in seconds that may pass before this Domain is no longer authoritative. Valid values are 300, 3600, 7200, 14400, 28800, 57600, 86400, 172800, 345600, 604800, 1209600, and 2419200 - any other value will be rounded to the nearest valid value.
ExpireSec int `json:"expire_sec"`
// The amount of time in seconds before this Domain should be refreshed. Valid values are 300, 3600, 7200, 14400, 28800, 57600, 86400, 172800, 345600, 604800, 1209600, and 2419200 - any other value will be rounded to the nearest valid value.
RefreshSec int `json:"refresh_sec"`
// "Time to Live" - the amount of time in seconds that this Domain's records may be cached by resolvers or other domain servers. Valid values are 300, 3600, 7200, 14400, 28800, 57600, 86400, 172800, 345600, 604800, 1209600, and 2419200 - any other value will be rounded to the nearest valid value.
TTLSec int `json:"ttl_sec"`
}
// DomainCreateOptions fields are those accepted by CreateDomain
type DomainCreateOptions struct {
// The domain this Domain represents. These must be unique in our system; you cannot have two Domains representing the same domain.
Domain string `json:"domain"`
// If this Domain represents the authoritative source of information for the domain it describes, or if it is a read-only copy of a master (also called a slave).
// Enum:"master" "slave"
Type DomainType `json:"type"`
// Deprecated: The group this Domain belongs to. This is for display purposes only.
Group string `json:"group,omitempty"`
// Used to control whether this Domain is currently being rendered.
// Enum:"disabled" "active" "edit_mode" "has_errors"
Status DomainStatus `json:"status,omitempty"`
// A description for this Domain. This is for display purposes only.
Description string `json:"description,omitempty"`
// Start of Authority email address. This is required for master Domains.
SOAEmail string `json:"soa_email,omitempty"`
// The interval, in seconds, at which a failed refresh should be retried.
// Valid values are 300, 3600, 7200, 14400, 28800, 57600, 86400, 172800, 345600, 604800, 1209600, and 2419200 - any other value will be rounded to the nearest valid value.
RetrySec int `json:"retry_sec,omitempty"`
// The IP addresses representing the master DNS for this Domain.
MasterIPs []string `json:"master_ips,omitempty"`
// The list of IPs that may perform a zone transfer for this Domain. This is potentially dangerous, and should be set to an empty list unless you intend to use it.
AXfrIPs []string `json:"axfr_ips,omitempty"`
// The amount of time in seconds that may pass before this Domain is no longer authoritative. Valid values are 300, 3600, 7200, 14400, 28800, 57600, 86400, 172800, 345600, 604800, 1209600, and 2419200 - any other value will be rounded to the nearest valid value.
ExpireSec int `json:"expire_sec,omitempty"`
// The amount of time in seconds before this Domain should be refreshed. Valid values are 300, 3600, 7200, 14400, 28800, 57600, 86400, 172800, 345600, 604800, 1209600, and 2419200 - any other value will be rounded to the nearest valid value.
RefreshSec int `json:"refresh_sec,omitempty"`
// "Time to Live" - the amount of time in seconds that this Domain's records may be cached by resolvers or other domain servers. Valid values are 300, 3600, 7200, 14400, 28800, 57600, 86400, 172800, 345600, 604800, 1209600, and 2419200 - any other value will be rounded to the nearest valid value.
TTLSec int `json:"ttl_sec,omitempty"`
}
// DomainUpdateOptions converts a Domain to DomainUpdateOptions for use in UpdateDomain
type DomainUpdateOptions struct {
// The domain this Domain represents. These must be unique in our system; you cannot have two Domains representing the same domain.
Domain string `json:"domain,omitempty"`
// If this Domain represents the authoritative source of information for the domain it describes, or if it is a read-only copy of a master (also called a slave).
// Enum:"master" "slave"
Type DomainType `json:"type,omitempty"`
// Deprecated: The group this Domain belongs to. This is for display purposes only.
Group string `json:"group,omitempty"`
// Used to control whether this Domain is currently being rendered.
// Enum:"disabled" "active" "edit_mode" "has_errors"
Status DomainStatus `json:"status,omitempty"`
// A description for this Domain. This is for display purposes only.
Description string `json:"description,omitempty"`
// Start of Authority email address. This is required for master Domains.
SOAEmail string `json:"soa_email,omitempty"`
// The interval, in seconds, at which a failed refresh should be retried.
// Valid values are 300, 3600, 7200, 14400, 28800, 57600, 86400, 172800, 345600, 604800, 1209600, and 2419200 - any other value will be rounded to the nearest valid value.
RetrySec int `json:"retry_sec,omitempty"`
// The IP addresses representing the master DNS for this Domain.
MasterIPs []string `json:"master_ips,omitempty"`
// The list of IPs that may perform a zone transfer for this Domain. This is potentially dangerous, and should be set to an empty list unless you intend to use it.
AXfrIPs []string `json:"axfr_ips,omitempty"`
// The amount of time in seconds that may pass before this Domain is no longer authoritative. Valid values are 300, 3600, 7200, 14400, 28800, 57600, 86400, 172800, 345600, 604800, 1209600, and 2419200 - any other value will be rounded to the nearest valid value.
ExpireSec int `json:"expire_sec,omitempty"`
// The amount of time in seconds before this Domain should be refreshed. Valid values are 300, 3600, 7200, 14400, 28800, 57600, 86400, 172800, 345600, 604800, 1209600, and 2419200 - any other value will be rounded to the nearest valid value.
RefreshSec int `json:"refresh_sec,omitempty"`
// "Time to Live" - the amount of time in seconds that this Domain's records may be cached by resolvers or other domain servers. Valid values are 300, 3600, 7200, 14400, 28800, 57600, 86400, 172800, 345600, 604800, 1209600, and 2419200 - any other value will be rounded to the nearest valid value.
TTLSec int `json:"ttl_sec,omitempty"`
}
// DomainType constants start with DomainType and include Linode API Domain Type values
type DomainType string
// DomainType constants reflect the DNS zone type of a Domain
const (
DomainTypeMaster DomainType = "master"
DomainTypeSlave DomainType = "slave"
)
// DomainStatus constants start with DomainStatus and include Linode API Domain Status values
type DomainStatus string
// DomainStatus constants reflect the current status of a Domain
const (
DomainStatusDisabled DomainStatus = "disabled"
DomainStatusActive DomainStatus = "active"
DomainStatusEditMode DomainStatus = "edit_mode"
DomainStatusHasErrors DomainStatus = "has_errors"
)
// GetUpdateOptions converts a Domain to DomainUpdateOptions for use in UpdateDomain
func (d Domain) GetUpdateOptions() (du DomainUpdateOptions) {
du.Domain = d.Domain
du.Type = d.Type
du.Group = d.Group
du.Status = d.Status
du.Description = d.Description
du.SOAEmail = d.SOAEmail
du.RetrySec = d.RetrySec
du.MasterIPs = d.MasterIPs
du.AXfrIPs = d.AXfrIPs
du.ExpireSec = d.ExpireSec
du.RefreshSec = d.RefreshSec
du.TTLSec = d.TTLSec
return
}
// DomainsPagedResponse represents a paginated Domain API response
type DomainsPagedResponse struct {
*PageOptions
Data []Domain `json:"data"`
}
// endpoint gets the endpoint URL for Domain
func (DomainsPagedResponse) endpoint(c *Client) string {
endpoint, err := c.Domains.Endpoint()
if err != nil {
panic(err)
}
return endpoint
}
// appendData appends Domains when processing paginated Domain responses
func (resp *DomainsPagedResponse) appendData(r *DomainsPagedResponse) {
resp.Data = append(resp.Data, r.Data...)
}
// ListDomains lists Domains
func (c *Client) ListDomains(ctx context.Context, opts *ListOptions) ([]Domain, error) {
response := DomainsPagedResponse{}
err := c.listHelper(ctx, &response, opts)
if err != nil {
return nil, err
}
return response.Data, nil
}
// fixDates converts JSON timestamps to Go time.Time values
func (d *Domain) fixDates() *Domain {
return d
}
// GetDomain gets the domain with the provided ID
func (c *Client) GetDomain(ctx context.Context, id int) (*Domain, error) {
e, err := c.Domains.Endpoint()
if err != nil {
return nil, err
}
e = fmt.Sprintf("%s/%d", e, id)
r, err := coupleAPIErrors(c.R(ctx).SetResult(&Domain{}).Get(e))
if err != nil {
return nil, err
}
return r.Result().(*Domain).fixDates(), nil
}
// CreateDomain creates a Domain
func (c *Client) CreateDomain(ctx context.Context, domain DomainCreateOptions) (*Domain, error) {
var body string
e, err := c.Domains.Endpoint()
if err != nil {
return nil, err
}
req := c.R(ctx).SetResult(&Domain{})
bodyData, err := json.Marshal(domain)
if err != nil {
return nil, NewError(err)
}
body = string(bodyData)
r, err := coupleAPIErrors(req.
SetBody(body).
Post(e))
if err != nil {
return nil, err
}
return r.Result().(*Domain).fixDates(), nil
}
// UpdateDomain updates the Domain with the specified id
func (c *Client) UpdateDomain(ctx context.Context, id int, domain DomainUpdateOptions) (*Domain, error) {
var body string
e, err := c.Domains.Endpoint()
if err != nil {
return nil, err
}
e = fmt.Sprintf("%s/%d", e, id)
req := c.R(ctx).SetResult(&Domain{})
if bodyData, err := json.Marshal(domain); err == nil {
body = string(bodyData)
} else {
return nil, NewError(err)
}
r, err := coupleAPIErrors(req.
SetBody(body).
Put(e))
if err != nil {
return nil, err
}
return r.Result().(*Domain).fixDates(), nil
}
// DeleteDomain deletes the Domain with the specified id
func (c *Client) DeleteDomain(ctx context.Context, id int) error {
e, err := c.Domains.Endpoint()
if err != nil {
return err
}
e = fmt.Sprintf("%s/%d", e, id)
_, err = coupleAPIErrors(c.R(ctx).Delete(e))
return err
}

109
vendor/github.com/linode/linodego/errors.go generated vendored Normal file
View file

@ -0,0 +1,109 @@
package linodego
import (
"fmt"
"log"
"net/http"
"strings"
"github.com/go-resty/resty"
)
const (
// ErrorFromString is the Code identifying Errors created by string types
ErrorFromString = 1
// ErrorFromError is the Code identifying Errors created by error types
ErrorFromError = 2
// ErrorFromStringer is the Code identifying Errors created by fmt.Stringer types
ErrorFromStringer = 3
)
// Error wraps the LinodeGo error with the relevant http.Response
type Error struct {
Response *http.Response
Code int
Message string
}
// APIErrorReason is an individual invalid request message returned by the Linode API
type APIErrorReason struct {
Reason string `json:"reason"`
Field string `json:"field"`
}
func (r APIErrorReason) Error() string {
if len(r.Field) == 0 {
return r.Reason
}
return fmt.Sprintf("[%s] %s", r.Field, r.Reason)
}
// APIError is the error-set returned by the Linode API when presented with an invalid request
type APIError struct {
Errors []APIErrorReason `json:"errors"`
}
func coupleAPIErrors(r *resty.Response, err error) (*resty.Response, error) {
if err != nil {
return nil, NewError(err)
}
if r.Error() != nil {
apiError, ok := r.Error().(*APIError)
if !ok || (ok && len(apiError.Errors) == 0) {
return r, nil
}
return nil, NewError(r)
}
return r, nil
}
func (e APIError) Error() string {
var x []string
for _, msg := range e.Errors {
x = append(x, msg.Error())
}
return strings.Join(x, "; ")
}
func (g Error) Error() string {
return fmt.Sprintf("[%03d] %s", g.Code, g.Message)
}
// NewError creates a linodego.Error with a Code identifying the source err type,
// - ErrorFromString (1) from a string
// - ErrorFromError (2) for an error
// - ErrorFromStringer (3) for a Stringer
// - HTTP Status Codes (100-600) for a resty.Response object
func NewError(err interface{}) *Error {
if err == nil {
return nil
}
switch e := err.(type) {
case *Error:
return e
case *resty.Response:
apiError, ok := e.Error().(*APIError)
if !ok {
log.Fatalln("Unexpected Resty Error Response")
}
return &Error{
Code: e.RawResponse.StatusCode,
Message: apiError.Error(),
Response: e.RawResponse,
}
case error:
return &Error{Code: ErrorFromError, Message: e.Error()}
case string:
return &Error{Code: ErrorFromString, Message: e}
case fmt.Stringer:
return &Error{Code: ErrorFromStringer, Message: e.String()}
default:
log.Fatalln("Unsupported type to linodego.NewError")
panic(err)
}
}

163
vendor/github.com/linode/linodego/images.go generated vendored Normal file
View file

@ -0,0 +1,163 @@
package linodego
import (
"context"
"encoding/json"
"fmt"
"time"
)
// Image represents a deployable Image object for use with Linode Instances
type Image struct {
CreatedStr string `json:"created"`
UpdatedStr string `json:"updated"`
ID string `json:"id"`
CreatedBy string `json:"created_by"`
Label string `json:"label"`
Description string `json:"description"`
Type string `json:"type"`
Vendor string `json:"vendor"`
Size int `json:"size"`
IsPublic bool `json:"is_public"`
Deprecated bool `json:"deprecated"`
Created *time.Time `json:"-"`
Updated *time.Time `json:"-"`
}
// ImageCreateOptions fields are those accepted by CreateImage
type ImageCreateOptions struct {
DiskID int `json:"disk_id"`
Label string `json:"label"`
Description string `json:"description,omitempty"`
}
// ImageUpdateOptions fields are those accepted by UpdateImage
type ImageUpdateOptions struct {
Label string `json:"label,omitempty"`
Description *string `json:"description,omitempty"`
}
func (i *Image) fixDates() *Image {
i.Created, _ = parseDates(i.CreatedStr)
i.Updated, _ = parseDates(i.UpdatedStr)
return i
}
// GetUpdateOptions converts an Image to ImageUpdateOptions for use in UpdateImage
func (i Image) GetUpdateOptions() (iu ImageUpdateOptions) {
iu.Label = i.Label
iu.Description = copyString(&i.Description)
return
}
// ImagesPagedResponse represents a linode API response for listing of images
type ImagesPagedResponse struct {
*PageOptions
Data []Image `json:"data"`
}
func (ImagesPagedResponse) endpoint(c *Client) string {
endpoint, err := c.Images.Endpoint()
if err != nil {
panic(err)
}
return endpoint
}
func (resp *ImagesPagedResponse) appendData(r *ImagesPagedResponse) {
resp.Data = append(resp.Data, r.Data...)
}
// ListImages lists Images
func (c *Client) ListImages(ctx context.Context, opts *ListOptions) ([]Image, error) {
response := ImagesPagedResponse{}
err := c.listHelper(ctx, &response, opts)
for i := range response.Data {
response.Data[i].fixDates()
}
if err != nil {
return nil, err
}
return response.Data, nil
}
// GetImage gets the Image with the provided ID
func (c *Client) GetImage(ctx context.Context, id string) (*Image, error) {
e, err := c.Images.Endpoint()
if err != nil {
return nil, err
}
e = fmt.Sprintf("%s/%s", e, id)
r, err := coupleAPIErrors(c.Images.R(ctx).Get(e))
if err != nil {
return nil, err
}
return r.Result().(*Image), nil
}
// CreateImage creates a Image
func (c *Client) CreateImage(ctx context.Context, createOpts ImageCreateOptions) (*Image, error) {
var body string
e, err := c.Images.Endpoint()
if err != nil {
return nil, err
}
req := c.R(ctx).SetResult(&Image{})
if bodyData, err := json.Marshal(createOpts); err == nil {
body = string(bodyData)
} else {
return nil, NewError(err)
}
r, err := coupleAPIErrors(req.
SetBody(body).
Post(e))
if err != nil {
return nil, err
}
return r.Result().(*Image).fixDates(), nil
}
// UpdateImage updates the Image with the specified id
func (c *Client) UpdateImage(ctx context.Context, id string, updateOpts ImageUpdateOptions) (*Image, error) {
var body string
e, err := c.Images.Endpoint()
if err != nil {
return nil, err
}
e = fmt.Sprintf("%s/%s", e, id)
req := c.R(ctx).SetResult(&Image{})
if bodyData, err := json.Marshal(updateOpts); err == nil {
body = string(bodyData)
} else {
return nil, NewError(err)
}
r, err := coupleAPIErrors(req.
SetBody(body).
Put(e))
if err != nil {
return nil, err
}
return r.Result().(*Image).fixDates(), nil
}
// DeleteImage deletes the Image with the specified id
func (c *Client) DeleteImage(ctx context.Context, id string) error {
e, err := c.Images.Endpoint()
if err != nil {
return err
}
e = fmt.Sprintf("%s/%s", e, id)
_, err = coupleAPIErrors(c.R(ctx).Delete(e))
return err
}

246
vendor/github.com/linode/linodego/instance_configs.go generated vendored Normal file
View file

@ -0,0 +1,246 @@
package linodego
import (
"context"
"encoding/json"
"fmt"
"time"
)
// InstanceConfig represents all of the settings that control the boot and run configuration of a Linode Instance
type InstanceConfig struct {
CreatedStr string `json:"created"`
UpdatedStr string `json:"updated"`
ID int `json:"id"`
Label string `json:"label"`
Comments string `json:"comments"`
Devices *InstanceConfigDeviceMap `json:"devices"`
Helpers *InstanceConfigHelpers `json:"helpers"`
MemoryLimit int `json:"memory_limit"`
Kernel string `json:"kernel"`
InitRD *int `json:"init_rd"`
RootDevice string `json:"root_device"`
RunLevel string `json:"run_level"`
VirtMode string `json:"virt_mode"`
Created *time.Time `json:"-"`
Updated *time.Time `json:"-"`
}
// InstanceConfigDevice contains either the DiskID or VolumeID assigned to a Config Device
type InstanceConfigDevice struct {
DiskID int `json:"disk_id,omitempty"`
VolumeID int `json:"volume_id,omitempty"`
}
// InstanceConfigDeviceMap contains SDA-SDH InstanceConfigDevice settings
type InstanceConfigDeviceMap struct {
SDA *InstanceConfigDevice `json:"sda,omitempty"`
SDB *InstanceConfigDevice `json:"sdb,omitempty"`
SDC *InstanceConfigDevice `json:"sdc,omitempty"`
SDD *InstanceConfigDevice `json:"sdd,omitempty"`
SDE *InstanceConfigDevice `json:"sde,omitempty"`
SDF *InstanceConfigDevice `json:"sdf,omitempty"`
SDG *InstanceConfigDevice `json:"sdg,omitempty"`
SDH *InstanceConfigDevice `json:"sdh,omitempty"`
}
// InstanceConfigHelpers are Instance Config options that control Linux distribution specific tweaks
type InstanceConfigHelpers struct {
UpdateDBDisabled bool `json:"updatedb_disabled"`
Distro bool `json:"distro"`
ModulesDep bool `json:"modules_dep"`
Network bool `json:"network"`
DevTmpFsAutomount bool `json:"devtmpfs_automount"`
}
// InstanceConfigsPagedResponse represents a paginated InstanceConfig API response
type InstanceConfigsPagedResponse struct {
*PageOptions
Data []InstanceConfig `json:"data"`
}
// InstanceConfigCreateOptions are InstanceConfig settings that can be used at creation
type InstanceConfigCreateOptions struct {
Label string `json:"label,omitempty"`
Comments string `json:"comments,omitempty"`
Devices InstanceConfigDeviceMap `json:"devices"`
Helpers *InstanceConfigHelpers `json:"helpers,omitempty"`
MemoryLimit int `json:"memory_limit,omitempty"`
Kernel string `json:"kernel,omitempty"`
InitRD int `json:"init_rd,omitempty"`
RootDevice *string `json:"root_device,omitempty"`
RunLevel string `json:"run_level,omitempty"`
VirtMode string `json:"virt_mode,omitempty"`
}
// InstanceConfigUpdateOptions are InstanceConfig settings that can be used in updates
type InstanceConfigUpdateOptions struct {
Label string `json:"label,omitempty"`
Comments string `json:"comments"`
Devices *InstanceConfigDeviceMap `json:"devices,omitempty"`
Helpers *InstanceConfigHelpers `json:"helpers,omitempty"`
// MemoryLimit 0 means unlimitted, this is not omitted
MemoryLimit int `json:"memory_limit"`
Kernel string `json:"kernel,omitempty"`
// InitRD is nullable, permit the sending of null
InitRD *int `json:"init_rd"`
RootDevice string `json:"root_device,omitempty"`
RunLevel string `json:"run_level,omitempty"`
VirtMode string `json:"virt_mode,omitempty"`
}
// GetCreateOptions converts a InstanceConfig to InstanceConfigCreateOptions for use in CreateInstanceConfig
func (i InstanceConfig) GetCreateOptions() InstanceConfigCreateOptions {
initrd := 0
if i.InitRD != nil {
initrd = *i.InitRD
}
return InstanceConfigCreateOptions{
Label: i.Label,
Comments: i.Comments,
Devices: *i.Devices,
Helpers: i.Helpers,
MemoryLimit: i.MemoryLimit,
Kernel: i.Kernel,
InitRD: initrd,
RootDevice: copyString(&i.RootDevice),
RunLevel: i.RunLevel,
VirtMode: i.VirtMode,
}
}
// GetUpdateOptions converts a InstanceConfig to InstanceConfigUpdateOptions for use in UpdateInstanceConfig
func (i InstanceConfig) GetUpdateOptions() InstanceConfigUpdateOptions {
return InstanceConfigUpdateOptions{
Label: i.Label,
Comments: i.Comments,
Devices: i.Devices,
Helpers: i.Helpers,
MemoryLimit: i.MemoryLimit,
Kernel: i.Kernel,
InitRD: copyInt(i.InitRD),
RootDevice: i.RootDevice,
RunLevel: i.RunLevel,
VirtMode: i.VirtMode,
}
}
// endpointWithID gets the endpoint URL for InstanceConfigs of a given Instance
func (InstanceConfigsPagedResponse) endpointWithID(c *Client, id int) string {
endpoint, err := c.InstanceConfigs.endpointWithID(id)
if err != nil {
panic(err)
}
return endpoint
}
// appendData appends InstanceConfigs when processing paginated InstanceConfig responses
func (resp *InstanceConfigsPagedResponse) appendData(r *InstanceConfigsPagedResponse) {
resp.Data = append(resp.Data, r.Data...)
}
// ListInstanceConfigs lists InstanceConfigs
func (c *Client) ListInstanceConfigs(ctx context.Context, linodeID int, opts *ListOptions) ([]InstanceConfig, error) {
response := InstanceConfigsPagedResponse{}
err := c.listHelperWithID(ctx, &response, linodeID, opts)
for i := range response.Data {
response.Data[i].fixDates()
}
if err != nil {
return nil, err
}
return response.Data, nil
}
// fixDates converts JSON timestamps to Go time.Time values
func (i *InstanceConfig) fixDates() *InstanceConfig {
i.Created, _ = parseDates(i.CreatedStr)
i.Updated, _ = parseDates(i.UpdatedStr)
return i
}
// GetInstanceConfig gets the template with the provided ID
func (c *Client) GetInstanceConfig(ctx context.Context, linodeID int, configID int) (*InstanceConfig, error) {
e, err := c.InstanceConfigs.endpointWithID(linodeID)
if err != nil {
return nil, err
}
e = fmt.Sprintf("%s/%d", e, configID)
r, err := coupleAPIErrors(c.R(ctx).SetResult(&InstanceConfig{}).Get(e))
if err != nil {
return nil, err
}
return r.Result().(*InstanceConfig).fixDates(), nil
}
// CreateInstanceConfig creates a new InstanceConfig for the given Instance
func (c *Client) CreateInstanceConfig(ctx context.Context, linodeID int, createOpts InstanceConfigCreateOptions) (*InstanceConfig, error) {
var body string
e, err := c.InstanceConfigs.endpointWithID(linodeID)
if err != nil {
return nil, err
}
req := c.R(ctx).SetResult(&InstanceConfig{})
if bodyData, err := json.Marshal(createOpts); err == nil {
body = string(bodyData)
} else {
return nil, err
}
r, err := coupleAPIErrors(req.
SetBody(body).
Post(e))
if err != nil {
return nil, err
}
return r.Result().(*InstanceConfig).fixDates(), nil
}
// UpdateInstanceConfig update an InstanceConfig for the given Instance
func (c *Client) UpdateInstanceConfig(ctx context.Context, linodeID int, configID int, updateOpts InstanceConfigUpdateOptions) (*InstanceConfig, error) {
var body string
e, err := c.InstanceConfigs.endpointWithID(linodeID)
if err != nil {
return nil, err
}
e = fmt.Sprintf("%s/%d", e, configID)
req := c.R(ctx).SetResult(&InstanceConfig{})
if bodyData, err := json.Marshal(updateOpts); err == nil {
body = string(bodyData)
} else {
return nil, err
}
r, err := coupleAPIErrors(req.
SetBody(body).
Put(e))
if err != nil {
return nil, err
}
return r.Result().(*InstanceConfig).fixDates(), nil
}
// RenameInstanceConfig renames an InstanceConfig
func (c *Client) RenameInstanceConfig(ctx context.Context, linodeID int, configID int, label string) (*InstanceConfig, error) {
return c.UpdateInstanceConfig(ctx, linodeID, configID, InstanceConfigUpdateOptions{Label: label})
}
// DeleteInstanceConfig deletes a Linode InstanceConfig
func (c *Client) DeleteInstanceConfig(ctx context.Context, linodeID int, configID int) error {
e, err := c.InstanceConfigs.endpointWithID(linodeID)
if err != nil {
return err
}
e = fmt.Sprintf("%s/%d", e, configID)
_, err = coupleAPIErrors(c.R(ctx).Delete(e))
return err
}

217
vendor/github.com/linode/linodego/instance_disks.go generated vendored Normal file
View file

@ -0,0 +1,217 @@
package linodego
import (
"context"
"encoding/json"
"fmt"
"time"
)
// InstanceDisk represents an Instance Disk object
type InstanceDisk struct {
CreatedStr string `json:"created"`
UpdatedStr string `json:"updated"`
ID int `json:"id"`
Label string `json:"label"`
Status string `json:"status"`
Size int `json:"size"`
Filesystem DiskFilesystem `json:"filesystem"`
Created time.Time `json:"-"`
Updated time.Time `json:"-"`
}
// DiskFilesystem constants start with Filesystem and include Linode API Filesystems
type DiskFilesystem string
// DiskFilesystem constants represent the filesystems types an Instance Disk may use
const (
FilesystemRaw DiskFilesystem = "raw"
FilesystemSwap DiskFilesystem = "swap"
FilesystemExt3 DiskFilesystem = "ext3"
FilesystemExt4 DiskFilesystem = "ext4"
FilesystemInitrd DiskFilesystem = "initrd"
)
// InstanceDisksPagedResponse represents a paginated InstanceDisk API response
type InstanceDisksPagedResponse struct {
*PageOptions
Data []InstanceDisk `json:"data"`
}
// InstanceDiskCreateOptions are InstanceDisk settings that can be used at creation
type InstanceDiskCreateOptions struct {
Label string `json:"label"`
Size int `json:"size"`
// Image is optional, but requires RootPass if provided
Image string `json:"image,omitempty"`
RootPass string `json:"root_pass,omitempty"`
Filesystem string `json:"filesystem,omitempty"`
AuthorizedKeys []string `json:"authorized_keys,omitempty"`
AuthorizedUsers []string `json:"authorized_users,omitempty"`
ReadOnly bool `json:"read_only,omitempty"`
StackscriptID int `json:"stackscript_id,omitempty"`
StackscriptData map[string]string `json:"stackscript_data,omitempty"`
}
// InstanceDiskUpdateOptions are InstanceDisk settings that can be used in updates
type InstanceDiskUpdateOptions struct {
Label string `json:"label"`
ReadOnly bool `json:"read_only"`
}
// endpointWithID gets the endpoint URL for InstanceDisks of a given Instance
func (InstanceDisksPagedResponse) endpointWithID(c *Client, id int) string {
endpoint, err := c.InstanceDisks.endpointWithID(id)
if err != nil {
panic(err)
}
return endpoint
}
// appendData appends InstanceDisks when processing paginated InstanceDisk responses
func (resp *InstanceDisksPagedResponse) appendData(r *InstanceDisksPagedResponse) {
resp.Data = append(resp.Data, r.Data...)
}
// ListInstanceDisks lists InstanceDisks
func (c *Client) ListInstanceDisks(ctx context.Context, linodeID int, opts *ListOptions) ([]InstanceDisk, error) {
response := InstanceDisksPagedResponse{}
err := c.listHelperWithID(ctx, &response, linodeID, opts)
for i := range response.Data {
response.Data[i].fixDates()
}
if err != nil {
return nil, err
}
return response.Data, nil
}
// fixDates converts JSON timestamps to Go time.Time values
func (v *InstanceDisk) fixDates() *InstanceDisk {
if created, err := parseDates(v.CreatedStr); err == nil {
v.Created = *created
}
if updated, err := parseDates(v.UpdatedStr); err == nil {
v.Updated = *updated
}
return v
}
// GetInstanceDisk gets the template with the provided ID
func (c *Client) GetInstanceDisk(ctx context.Context, linodeID int, configID int) (*InstanceDisk, error) {
e, err := c.InstanceDisks.endpointWithID(linodeID)
if err != nil {
return nil, err
}
e = fmt.Sprintf("%s/%d", e, configID)
r, err := coupleAPIErrors(c.R(ctx).SetResult(&InstanceDisk{}).Get(e))
if err != nil {
return nil, err
}
return r.Result().(*InstanceDisk).fixDates(), nil
}
// CreateInstanceDisk creates a new InstanceDisk for the given Instance
func (c *Client) CreateInstanceDisk(ctx context.Context, linodeID int, createOpts InstanceDiskCreateOptions) (*InstanceDisk, error) {
var body string
e, err := c.InstanceDisks.endpointWithID(linodeID)
if err != nil {
return nil, err
}
req := c.R(ctx).SetResult(&InstanceDisk{})
if bodyData, err := json.Marshal(createOpts); err == nil {
body = string(bodyData)
} else {
return nil, NewError(err)
}
r, err := coupleAPIErrors(req.
SetBody(body).
Post(e))
if err != nil {
return nil, err
}
return r.Result().(*InstanceDisk).fixDates(), nil
}
// UpdateInstanceDisk creates a new InstanceDisk for the given Instance
func (c *Client) UpdateInstanceDisk(ctx context.Context, linodeID int, diskID int, updateOpts InstanceDiskUpdateOptions) (*InstanceDisk, error) {
var body string
e, err := c.InstanceDisks.endpointWithID(linodeID)
if err != nil {
return nil, err
}
e = fmt.Sprintf("%s/%d", e, diskID)
req := c.R(ctx).SetResult(&InstanceDisk{})
if bodyData, err := json.Marshal(updateOpts); err == nil {
body = string(bodyData)
} else {
return nil, NewError(err)
}
r, err := coupleAPIErrors(req.
SetBody(body).
Put(e))
if err != nil {
return nil, err
}
return r.Result().(*InstanceDisk).fixDates(), nil
}
// RenameInstanceDisk renames an InstanceDisk
func (c *Client) RenameInstanceDisk(ctx context.Context, linodeID int, diskID int, label string) (*InstanceDisk, error) {
return c.UpdateInstanceDisk(ctx, linodeID, diskID, InstanceDiskUpdateOptions{Label: label})
}
// ResizeInstanceDisk resizes the size of the Instance disk
func (c *Client) ResizeInstanceDisk(ctx context.Context, linodeID int, diskID int, size int) (*InstanceDisk, error) {
var body string
e, err := c.InstanceDisks.endpointWithID(linodeID)
if err != nil {
return nil, err
}
e = fmt.Sprintf("%s/%d", e, diskID)
req := c.R(ctx).SetResult(&InstanceDisk{})
updateOpts := map[string]interface{}{
"size": size,
}
if bodyData, err := json.Marshal(updateOpts); err == nil {
body = string(bodyData)
} else {
return nil, NewError(err)
}
r, err := coupleAPIErrors(req.
SetBody(body).
Post(e))
if err != nil {
return nil, err
}
return r.Result().(*InstanceDisk).fixDates(), nil
}
// DeleteInstanceDisk deletes a Linode Instance Disk
func (c *Client) DeleteInstanceDisk(ctx context.Context, linodeID int, diskID int) error {
e, err := c.InstanceDisks.endpointWithID(linodeID)
if err != nil {
return err
}
e = fmt.Sprintf("%s/%d", e, diskID)
_, err = coupleAPIErrors(c.R(ctx).Delete(e))
return err
}

106
vendor/github.com/linode/linodego/instance_ips.go generated vendored Normal file
View file

@ -0,0 +1,106 @@
package linodego
import (
"context"
"encoding/json"
"fmt"
)
// InstanceIPAddressResponse contains the IPv4 and IPv6 details for an Instance
type InstanceIPAddressResponse struct {
IPv4 *InstanceIPv4Response `json:"ipv4"`
IPv6 *InstanceIPv6Response `json:"ipv6"`
}
// InstanceIPv4Response contains the details of all IPv4 addresses associated with an Instance
type InstanceIPv4Response struct {
Public []*InstanceIP `json:"public"`
Private []*InstanceIP `json:"private"`
Shared []*InstanceIP `json:"shared"`
}
// InstanceIP represents an Instance IP with additional DNS and networking details
type InstanceIP struct {
Address string `json:"address"`
Gateway string `json:"gateway"`
SubnetMask string `json:"subnet_mask"`
Prefix int `json:"prefix"`
Type string `json:"type"`
Public bool `json:"public"`
RDNS string `json:"rdns"`
LinodeID int `json:"linode_id"`
Region string `json:"region"`
}
// InstanceIPv6Response contains the IPv6 addresses and ranges for an Instance
type InstanceIPv6Response struct {
LinkLocal *InstanceIP `json:"link_local"`
SLAAC *InstanceIP `json:"slaac"`
Global []*IPv6Range `json:"global"`
}
// IPv6Range represents a range of IPv6 addresses routed to a single Linode in a given Region
type IPv6Range struct {
Range string `json:"range"`
Region string `json:"region"`
}
// GetInstanceIPAddresses gets the IPAddresses for a Linode instance
func (c *Client) GetInstanceIPAddresses(ctx context.Context, linodeID int) (*InstanceIPAddressResponse, error) {
e, err := c.InstanceIPs.endpointWithID(linodeID)
if err != nil {
return nil, err
}
r, err := coupleAPIErrors(c.R(ctx).SetResult(&InstanceIPAddressResponse{}).Get(e))
if err != nil {
return nil, err
}
return r.Result().(*InstanceIPAddressResponse), nil
}
// GetInstanceIPAddress gets the IPAddress for a Linode instance matching a supplied IP address
func (c *Client) GetInstanceIPAddress(ctx context.Context, linodeID int, ipaddress string) (*InstanceIP, error) {
e, err := c.InstanceIPs.endpointWithID(linodeID)
if err != nil {
return nil, err
}
e = fmt.Sprintf("%s/%s", e, ipaddress)
r, err := coupleAPIErrors(c.R(ctx).SetResult(&InstanceIP{}).Get(e))
if err != nil {
return nil, err
}
return r.Result().(*InstanceIP), nil
}
// AddInstanceIPAddress adds a public or private IP to a Linode instance
func (c *Client) AddInstanceIPAddress(ctx context.Context, linodeID int, public bool) (*InstanceIP, error) {
var body string
e, err := c.InstanceIPs.endpointWithID(linodeID)
if err != nil {
return nil, err
}
req := c.R(ctx).SetResult(&InstanceIP{})
instanceipRequest := struct {
Type string `json:"type"`
Public bool `json:"public"`
}{"ipv4", public}
if bodyData, err := json.Marshal(instanceipRequest); err == nil {
body = string(bodyData)
} else {
return nil, NewError(err)
}
r, err := coupleAPIErrors(req.
SetHeader("Content-Type", "application/json").
SetBody(body).
Post(e))
if err != nil {
return nil, err
}
return r.Result().(*InstanceIP), nil
}

188
vendor/github.com/linode/linodego/instance_snapshots.go generated vendored Normal file
View file

@ -0,0 +1,188 @@
package linodego
import (
"context"
"encoding/json"
"fmt"
"time"
)
// InstanceBackupsResponse response struct for backup snapshot
type InstanceBackupsResponse struct {
Automatic []*InstanceSnapshot `json:"automatic"`
Snapshot *InstanceBackupSnapshotResponse `json:"snapshot"`
}
// InstanceBackupSnapshotResponse fields are those representing Instance Backup Snapshots
type InstanceBackupSnapshotResponse struct {
Current *InstanceSnapshot `json:"current"`
InProgress *InstanceSnapshot `json:"in_progress"`
}
// RestoreInstanceOptions fields are those accepted by InstanceRestore
type RestoreInstanceOptions struct {
LinodeID int `json:"linode_id"`
Overwrite bool `json:"overwrite"`
}
// InstanceSnapshot represents a linode backup snapshot
type InstanceSnapshot struct {
CreatedStr string `json:"created"`
UpdatedStr string `json:"updated"`
FinishedStr string `json:"finished"`
ID int `json:"id"`
Label string `json:"label"`
Status InstanceSnapshotStatus `json:"status"`
Type string `json:"type"`
Created *time.Time `json:"-"`
Updated *time.Time `json:"-"`
Finished *time.Time `json:"-"`
Configs []string `json:"configs"`
Disks []*InstanceSnapshotDisk `json:"disks"`
}
// InstanceSnapshotDisk fields represent the source disk of a Snapshot
type InstanceSnapshotDisk struct {
Label string `json:"label"`
Size int `json:"size"`
Filesystem string `json:"filesystem"`
}
// InstanceSnapshotStatus constants start with Snapshot and include Linode API Instance Backup Snapshot status values
type InstanceSnapshotStatus string
// InstanceSnapshotStatus constants reflect the current status of an Instance Snapshot
var (
SnapshotPaused InstanceSnapshotStatus = "paused"
SnapshotPending InstanceSnapshotStatus = "pending"
SnapshotRunning InstanceSnapshotStatus = "running"
SnapshotNeedsPostProcessing InstanceSnapshotStatus = "needsPostProcessing"
SnapshotSuccessful InstanceSnapshotStatus = "successful"
SnapshotFailed InstanceSnapshotStatus = "failed"
SnapshotUserAborted InstanceSnapshotStatus = "userAborted"
)
func (l *InstanceSnapshot) fixDates() *InstanceSnapshot {
l.Created, _ = parseDates(l.CreatedStr)
l.Updated, _ = parseDates(l.UpdatedStr)
l.Finished, _ = parseDates(l.FinishedStr)
return l
}
// GetInstanceSnapshot gets the snapshot with the provided ID
func (c *Client) GetInstanceSnapshot(ctx context.Context, linodeID int, snapshotID int) (*InstanceSnapshot, error) {
e, err := c.InstanceSnapshots.endpointWithID(linodeID)
if err != nil {
return nil, err
}
e = fmt.Sprintf("%s/%d", e, snapshotID)
r, err := coupleAPIErrors(c.R(ctx).SetResult(&InstanceSnapshot{}).Get(e))
if err != nil {
return nil, err
}
return r.Result().(*InstanceSnapshot).fixDates(), nil
}
// CreateInstanceSnapshot Creates or Replaces the snapshot Backup of a Linode. If a previous snapshot exists for this Linode, it will be deleted.
func (c *Client) CreateInstanceSnapshot(ctx context.Context, linodeID int, label string) (*InstanceSnapshot, error) {
o, err := json.Marshal(map[string]string{"label": label})
if err != nil {
return nil, err
}
body := string(o)
e, err := c.InstanceSnapshots.endpointWithID(linodeID)
if err != nil {
return nil, err
}
r, err := coupleAPIErrors(c.R(ctx).
SetBody(body).
SetResult(&InstanceSnapshot{}).
Post(e))
if err != nil {
return nil, err
}
return r.Result().(*InstanceSnapshot).fixDates(), nil
}
// GetInstanceBackups gets the Instance's available Backups.
// This is not called ListInstanceBackups because a single object is returned, matching the API response.
func (c *Client) GetInstanceBackups(ctx context.Context, linodeID int) (*InstanceBackupsResponse, error) {
e, err := c.InstanceSnapshots.endpointWithID(linodeID)
if err != nil {
return nil, err
}
r, err := coupleAPIErrors(c.R(ctx).
SetResult(&InstanceBackupsResponse{}).
Get(e))
if err != nil {
return nil, err
}
return r.Result().(*InstanceBackupsResponse).fixDates(), nil
}
// EnableInstanceBackups Enables backups for the specified Linode.
func (c *Client) EnableInstanceBackups(ctx context.Context, linodeID int) error {
e, err := c.InstanceSnapshots.endpointWithID(linodeID)
if err != nil {
return err
}
e = fmt.Sprintf("%s/enable", e)
_, err = coupleAPIErrors(c.R(ctx).Post(e))
return err
}
// CancelInstanceBackups Cancels backups for the specified Linode.
func (c *Client) CancelInstanceBackups(ctx context.Context, linodeID int) error {
e, err := c.InstanceSnapshots.endpointWithID(linodeID)
if err != nil {
return err
}
e = fmt.Sprintf("%s/cancel", e)
_, err = coupleAPIErrors(c.R(ctx).Post(e))
return err
}
// RestoreInstanceBackup Restores a Linode's Backup to the specified Linode.
func (c *Client) RestoreInstanceBackup(ctx context.Context, linodeID int, backupID int, opts RestoreInstanceOptions) error {
o, err := json.Marshal(opts)
if err != nil {
return NewError(err)
}
body := string(o)
e, err := c.InstanceSnapshots.endpointWithID(linodeID)
if err != nil {
return err
}
e = fmt.Sprintf("%s/%d/restore", e, backupID)
_, err = coupleAPIErrors(c.R(ctx).SetBody(body).Post(e))
return err
}
func (l *InstanceBackupSnapshotResponse) fixDates() *InstanceBackupSnapshotResponse {
if l.Current != nil {
l.Current.fixDates()
}
if l.InProgress != nil {
l.InProgress.fixDates()
}
return l
}
func (l *InstanceBackupsResponse) fixDates() *InstanceBackupsResponse {
for i := range l.Automatic {
l.Automatic[i].fixDates()
}
if l.Snapshot != nil {
l.Snapshot.fixDates()
}
return l
}

38
vendor/github.com/linode/linodego/instance_volumes.go generated vendored Normal file
View file

@ -0,0 +1,38 @@
package linodego
import (
"context"
)
// InstanceVolumesPagedResponse represents a paginated InstanceVolume API response
type InstanceVolumesPagedResponse struct {
*PageOptions
Data []Volume `json:"data"`
}
// endpoint gets the endpoint URL for InstanceVolume
func (InstanceVolumesPagedResponse) endpointWithID(c *Client, id int) string {
endpoint, err := c.InstanceVolumes.endpointWithID(id)
if err != nil {
panic(err)
}
return endpoint
}
// appendData appends InstanceVolumes when processing paginated InstanceVolume responses
func (resp *InstanceVolumesPagedResponse) appendData(r *InstanceVolumesPagedResponse) {
resp.Data = append(resp.Data, r.Data...)
}
// ListInstanceVolumes lists InstanceVolumes
func (c *Client) ListInstanceVolumes(ctx context.Context, linodeID int, opts *ListOptions) ([]Volume, error) {
response := InstanceVolumesPagedResponse{}
err := c.listHelperWithID(ctx, &response, linodeID, opts)
for i := range response.Data {
response.Data[i].fixDates()
}
if err != nil {
return nil, err
}
return response.Data, nil
}

438
vendor/github.com/linode/linodego/instances.go generated vendored Normal file
View file

@ -0,0 +1,438 @@
package linodego
import (
"context"
"encoding/json"
"fmt"
"net"
"time"
)
/*
* https://developers.linode.com/v4/reference/endpoints/linode/instances
*/
// InstanceStatus constants start with Instance and include Linode API Instance Status values
type InstanceStatus string
// InstanceStatus constants reflect the current status of an Instance
const (
InstanceBooting InstanceStatus = "booting"
InstanceRunning InstanceStatus = "running"
InstanceOffline InstanceStatus = "offline"
InstanceShuttingDown InstanceStatus = "shutting_down"
InstanceRebooting InstanceStatus = "rebooting"
InstanceProvisioning InstanceStatus = "provisioning"
InstanceDeleting InstanceStatus = "deleting"
InstanceMigrating InstanceStatus = "migrating"
InstanceRebuilding InstanceStatus = "rebuilding"
InstanceCloning InstanceStatus = "cloning"
InstanceRestoring InstanceStatus = "restoring"
InstanceResizing InstanceStatus = "resizing"
)
// Instance represents a linode object
type Instance struct {
CreatedStr string `json:"created"`
UpdatedStr string `json:"updated"`
ID int `json:"id"`
Created *time.Time `json:"-"`
Updated *time.Time `json:"-"`
Region string `json:"region"`
Alerts *InstanceAlert `json:"alerts"`
Backups *InstanceBackup `json:"backups"`
Image string `json:"image"`
Group string `json:"group"`
IPv4 []*net.IP `json:"ipv4"`
IPv6 string `json:"ipv6"`
Label string `json:"label"`
Type string `json:"type"`
Status InstanceStatus `json:"status"`
Hypervisor string `json:"hypervisor"`
Specs *InstanceSpec `json:"specs"`
}
// InstanceSpec represents a linode spec
type InstanceSpec struct {
Disk int `json:"disk"`
Memory int `json:"memory"`
VCPUs int `json:"vcpus"`
Transfer int `json:"transfer"`
}
// InstanceAlert represents a metric alert
type InstanceAlert struct {
CPU int `json:"cpu"`
IO int `json:"io"`
NetworkIn int `json:"network_in"`
NetworkOut int `json:"network_out"`
TransferQuota int `json:"transfer_quota"`
}
// InstanceBackup represents backup settings for an instance
type InstanceBackup struct {
Enabled bool `json:"enabled"`
Schedule struct {
Day string `json:"day,omitempty"`
Window string `json:"window,omitempty"`
}
}
// InstanceCreateOptions require only Region and Type
type InstanceCreateOptions struct {
Region string `json:"region"`
Type string `json:"type"`
Label string `json:"label,omitempty"`
Group string `json:"group,omitempty"`
RootPass string `json:"root_pass,omitempty"`
AuthorizedKeys []string `json:"authorized_keys,omitempty"`
AuthorizedUsers []string `json:"authorized_users,omitempty"`
StackScriptID int `json:"stackscript_id,omitempty"`
StackScriptData map[string]string `json:"stackscript_data,omitempty"`
BackupID int `json:"backup_id,omitempty"`
Image string `json:"image,omitempty"`
BackupsEnabled bool `json:"backups_enabled,omitempty"`
PrivateIP bool `json:"private_ip,omitempty"`
// Creation fields that need to be set explicitly false, "", or 0 use pointers
SwapSize *int `json:"swap_size,omitempty"`
Booted *bool `json:"booted,omitempty"`
}
// InstanceUpdateOptions is an options struct used when Updating an Instance
type InstanceUpdateOptions struct {
Label string `json:"label,omitempty"`
Group string `json:"group,omitempty"`
Backups *InstanceBackup `json:"backups,omitempty"`
Alerts *InstanceAlert `json:"alerts,omitempty"`
WatchdogEnabled *bool `json:"watchdog_enabled,omitempty"`
}
// InstanceCloneOptions is an options struct sent when Cloning an Instance
type InstanceCloneOptions struct {
Region string `json:"region,omitempty"`
Type string `json:"type,omitempty"`
// LinodeID is an optional existing instance to use as the target of the clone
LinodeID int `json:"linode_id,omitempty"`
Label string `json:"label,omitempty"`
Group string `json:"group,omitempty"`
BackupsEnabled bool `json:"backups_enabled"`
Disks []int `json:"disks,omitempty"`
Configs []int `json:"configs,omitempty"`
}
func (l *Instance) fixDates() *Instance {
l.Created, _ = parseDates(l.CreatedStr)
l.Updated, _ = parseDates(l.UpdatedStr)
return l
}
// InstancesPagedResponse represents a linode API response for listing
type InstancesPagedResponse struct {
*PageOptions
Data []Instance `json:"data"`
}
// endpoint gets the endpoint URL for Instance
func (InstancesPagedResponse) endpoint(c *Client) string {
endpoint, err := c.Instances.Endpoint()
if err != nil {
panic(err)
}
return endpoint
}
// appendData appends Instances when processing paginated Instance responses
func (resp *InstancesPagedResponse) appendData(r *InstancesPagedResponse) {
resp.Data = append(resp.Data, r.Data...)
}
// ListInstances lists linode instances
func (c *Client) ListInstances(ctx context.Context, opts *ListOptions) ([]Instance, error) {
response := InstancesPagedResponse{}
err := c.listHelper(ctx, &response, opts)
for i := range response.Data {
response.Data[i].fixDates()
}
if err != nil {
return nil, err
}
return response.Data, nil
}
// GetInstance gets the instance with the provided ID
func (c *Client) GetInstance(ctx context.Context, linodeID int) (*Instance, error) {
e, err := c.Instances.Endpoint()
if err != nil {
return nil, err
}
e = fmt.Sprintf("%s/%d", e, linodeID)
r, err := coupleAPIErrors(c.R(ctx).
SetResult(Instance{}).
Get(e))
if err != nil {
return nil, err
}
return r.Result().(*Instance).fixDates(), nil
}
// CreateInstance creates a Linode instance
func (c *Client) CreateInstance(ctx context.Context, instance InstanceCreateOptions) (*Instance, error) {
var body string
e, err := c.Instances.Endpoint()
if err != nil {
return nil, err
}
req := c.R(ctx).SetResult(&Instance{})
if bodyData, err := json.Marshal(instance); err == nil {
body = string(bodyData)
} else {
return nil, NewError(err)
}
r, err := coupleAPIErrors(req.
SetBody(body).
Post(e))
if err != nil {
return nil, err
}
return r.Result().(*Instance).fixDates(), nil
}
// UpdateInstance creates a Linode instance
func (c *Client) UpdateInstance(ctx context.Context, id int, instance InstanceUpdateOptions) (*Instance, error) {
var body string
e, err := c.Instances.Endpoint()
if err != nil {
return nil, err
}
e = fmt.Sprintf("%s/%d", e, id)
req := c.R(ctx).SetResult(&Instance{})
if bodyData, err := json.Marshal(instance); err == nil {
body = string(bodyData)
} else {
return nil, NewError(err)
}
r, err := coupleAPIErrors(req.
SetBody(body).
Put(e))
if err != nil {
return nil, err
}
return r.Result().(*Instance).fixDates(), nil
}
// RenameInstance renames an Instance
func (c *Client) RenameInstance(ctx context.Context, linodeID int, label string) (*Instance, error) {
return c.UpdateInstance(ctx, linodeID, InstanceUpdateOptions{Label: label})
}
// DeleteInstance deletes a Linode instance
func (c *Client) DeleteInstance(ctx context.Context, id int) error {
e, err := c.Instances.Endpoint()
if err != nil {
return err
}
e = fmt.Sprintf("%s/%d", e, id)
_, err = coupleAPIErrors(c.R(ctx).Delete(e))
return err
}
// BootInstance will boot a Linode instance
// A configID of 0 will cause Linode to choose the last/best config
func (c *Client) BootInstance(ctx context.Context, id int, configID int) error {
bodyStr := ""
if configID != 0 {
bodyMap := map[string]int{"config_id": configID}
bodyJSON, err := json.Marshal(bodyMap)
if err != nil {
return NewError(err)
}
bodyStr = string(bodyJSON)
}
e, err := c.Instances.Endpoint()
if err != nil {
return err
}
e = fmt.Sprintf("%s/%d/boot", e, id)
_, err = coupleAPIErrors(c.R(ctx).
SetBody(bodyStr).
Post(e))
return err
}
// CloneInstance clone an existing Instances Disks and Configuration profiles to another Linode Instance
func (c *Client) CloneInstance(ctx context.Context, id int, options InstanceCloneOptions) (*Instance, error) {
var body string
e, err := c.Instances.Endpoint()
if err != nil {
return nil, err
}
e = fmt.Sprintf("%s/%d/clone", e, id)
req := c.R(ctx).SetResult(&Instance{})
if bodyData, err := json.Marshal(options); err == nil {
body = string(bodyData)
} else {
return nil, NewError(err)
}
r, err := coupleAPIErrors(req.
SetBody(body).
Post(e))
if err != nil {
return nil, err
}
return r.Result().(*Instance).fixDates(), nil
}
// RebootInstance reboots a Linode instance
// A configID of 0 will cause Linode to choose the last/best config
func (c *Client) RebootInstance(ctx context.Context, id int, configID int) error {
bodyStr := "{}"
if configID != 0 {
bodyMap := map[string]int{"config_id": configID}
bodyJSON, err := json.Marshal(bodyMap)
if err != nil {
return NewError(err)
}
bodyStr = string(bodyJSON)
}
e, err := c.Instances.Endpoint()
if err != nil {
return err
}
e = fmt.Sprintf("%s/%d/reboot", e, id)
_, err = coupleAPIErrors(c.R(ctx).
SetBody(bodyStr).
Post(e))
return err
}
// RebuildInstanceOptions is a struct representing the options to send to the rebuild linode endpoint
type RebuildInstanceOptions struct {
Image string `json:"image"`
RootPass string `json:"root_pass"`
AuthorizedKeys []string `json:"authorized_keys"`
AuthorizedUsers []string `json:"authorized_users"`
StackscriptID int `json:"stackscript_id"`
StackscriptData map[string]string `json:"stackscript_data"`
Booted bool `json:"booted"`
}
// RebuildInstance Deletes all Disks and Configs on this Linode,
// then deploys a new Image to this Linode with the given attributes.
func (c *Client) RebuildInstance(ctx context.Context, id int, opts RebuildInstanceOptions) (*Instance, error) {
o, err := json.Marshal(opts)
if err != nil {
return nil, NewError(err)
}
b := string(o)
e, err := c.Instances.Endpoint()
if err != nil {
return nil, err
}
e = fmt.Sprintf("%s/%d/rebuild", e, id)
r, err := coupleAPIErrors(c.R(ctx).
SetBody(b).
SetResult(&Instance{}).
Post(e))
if err != nil {
return nil, err
}
return r.Result().(*Instance).fixDates(), nil
}
// RescueInstanceOptions fields are those accepted by RescueInstance
type RescueInstanceOptions struct {
Devices InstanceConfigDeviceMap `json:"devices"`
}
// RescueInstance reboots an instance into a safe environment for performing many system recovery and disk management tasks.
// Rescue Mode is based on the Finnix recovery distribution, a self-contained and bootable Linux distribution.
// You can also use Rescue Mode for tasks other than disaster recovery, such as formatting disks to use different filesystems,
// copying data between disks, and downloading files from a disk via SSH and SFTP.
func (c *Client) RescueInstance(ctx context.Context, id int, opts RescueInstanceOptions) error {
o, err := json.Marshal(opts)
if err != nil {
return NewError(err)
}
b := string(o)
e, err := c.Instances.Endpoint()
if err != nil {
return err
}
e = fmt.Sprintf("%s/%d/rescue", e, id)
_, err = coupleAPIErrors(c.R(ctx).
SetBody(b).
Post(e))
return err
}
// ResizeInstance resizes an instance to new Linode type
func (c *Client) ResizeInstance(ctx context.Context, id int, linodeType string) error {
body := fmt.Sprintf("{\"type\":\"%s\"}", linodeType)
e, err := c.Instances.Endpoint()
if err != nil {
return err
}
e = fmt.Sprintf("%s/%d/resize", e, id)
_, err = coupleAPIErrors(c.R(ctx).
SetBody(body).
Post(e))
return err
}
// ShutdownInstance - Shutdown an instance
func (c *Client) ShutdownInstance(ctx context.Context, id int) error {
return c.simpleInstanceAction(ctx, "shutdown", id)
}
// MutateInstance Upgrades a Linode to its next generation.
func (c *Client) MutateInstance(ctx context.Context, id int) error {
return c.simpleInstanceAction(ctx, "mutate", id)
}
// MigrateInstance - Migrate an instance
func (c *Client) MigrateInstance(ctx context.Context, id int) error {
return c.simpleInstanceAction(ctx, "migrate", id)
}
// simpleInstanceAction is a helper for Instance actions that take no parameters
// and return empty responses `{}` unless they return a standard error
func (c *Client) simpleInstanceAction(ctx context.Context, action string, id int) error {
e, err := c.Instances.Endpoint()
if err != nil {
return err
}
e = fmt.Sprintf("%s/%d/%s", e, id, action)
_, err = coupleAPIErrors(c.R(ctx).Post(e))
return err
}

61
vendor/github.com/linode/linodego/kernels.go generated vendored Normal file
View file

@ -0,0 +1,61 @@
package linodego
import (
"context"
"fmt"
)
// LinodeKernel represents a Linode Instance kernel object
type LinodeKernel struct {
ID string `json:"id"`
Label string `json:"label"`
Version string `json:"version"`
Architecture string `json:"architecture"`
KVM bool `json:"kvm"`
XEN bool `json:"xen"`
PVOPS bool `json:"pvops"`
}
// LinodeKernelsPagedResponse represents a Linode kernels API response for listing
type LinodeKernelsPagedResponse struct {
*PageOptions
Data []LinodeKernel `json:"data"`
}
// ListKernels lists linode kernels
func (c *Client) ListKernels(ctx context.Context, opts *ListOptions) ([]LinodeKernel, error) {
response := LinodeKernelsPagedResponse{}
err := c.listHelper(ctx, &response, opts)
if err != nil {
return nil, err
}
return response.Data, nil
}
func (LinodeKernelsPagedResponse) endpoint(c *Client) string {
endpoint, err := c.Kernels.Endpoint()
if err != nil {
panic(err)
}
return endpoint
}
func (resp *LinodeKernelsPagedResponse) appendData(r *LinodeKernelsPagedResponse) {
resp.Data = append(resp.Data, r.Data...)
}
// GetKernel gets the kernel with the provided ID
func (c *Client) GetKernel(ctx context.Context, kernelID string) (*LinodeKernel, error) {
e, err := c.Kernels.Endpoint()
if err != nil {
return nil, err
}
e = fmt.Sprintf("%s/%s", e, kernelID)
r, err := c.R(ctx).
SetResult(&LinodeKernel{}).
Get(e)
if err != nil {
return nil, err
}
return r.Result().(*LinodeKernel), nil
}

67
vendor/github.com/linode/linodego/longview.go generated vendored Normal file
View file

@ -0,0 +1,67 @@
package linodego
import (
"context"
"fmt"
)
// LongviewClient represents a LongviewClient object
type LongviewClient struct {
ID int `json:"id"`
// UpdatedStr string `json:"updated"`
// Updated *time.Time `json:"-"`
}
// LongviewClientsPagedResponse represents a paginated LongviewClient API response
type LongviewClientsPagedResponse struct {
*PageOptions
Data []LongviewClient `json:"data"`
}
// endpoint gets the endpoint URL for LongviewClient
func (LongviewClientsPagedResponse) endpoint(c *Client) string {
endpoint, err := c.LongviewClients.Endpoint()
if err != nil {
panic(err)
}
return endpoint
}
// appendData appends LongviewClients when processing paginated LongviewClient responses
func (resp *LongviewClientsPagedResponse) appendData(r *LongviewClientsPagedResponse) {
resp.Data = append(resp.Data, r.Data...)
}
// ListLongviewClients lists LongviewClients
func (c *Client) ListLongviewClients(ctx context.Context, opts *ListOptions) ([]LongviewClient, error) {
response := LongviewClientsPagedResponse{}
err := c.listHelper(ctx, &response, opts)
for i := range response.Data {
response.Data[i].fixDates()
}
if err != nil {
return nil, err
}
return response.Data, nil
}
// fixDates converts JSON timestamps to Go time.Time values
func (v *LongviewClient) fixDates() *LongviewClient {
// v.Created, _ = parseDates(v.CreatedStr)
// v.Updated, _ = parseDates(v.UpdatedStr)
return v
}
// GetLongviewClient gets the template with the provided ID
func (c *Client) GetLongviewClient(ctx context.Context, id string) (*LongviewClient, error) {
e, err := c.LongviewClients.Endpoint()
if err != nil {
return nil, err
}
e = fmt.Sprintf("%s/%s", e, id)
r, err := c.R(ctx).SetResult(&LongviewClient{}).Get(e)
if err != nil {
return nil, err
}
return r.Result().(*LongviewClient).fixDates(), nil
}

View file

@ -0,0 +1,70 @@
package linodego
import (
"context"
"fmt"
)
// LongviewSubscription represents a LongviewSubscription object
type LongviewSubscription struct {
ID string `json:"id"`
Label string `json:"label"`
ClientsIncluded int `json:"clients_included"`
Price *LinodePrice `json:"price"`
// UpdatedStr string `json:"updated"`
// Updated *time.Time `json:"-"`
}
// LongviewSubscriptionsPagedResponse represents a paginated LongviewSubscription API response
type LongviewSubscriptionsPagedResponse struct {
*PageOptions
Data []LongviewSubscription `json:"data"`
}
// endpoint gets the endpoint URL for LongviewSubscription
func (LongviewSubscriptionsPagedResponse) endpoint(c *Client) string {
endpoint, err := c.LongviewSubscriptions.Endpoint()
if err != nil {
panic(err)
}
return endpoint
}
// appendData appends LongviewSubscriptions when processing paginated LongviewSubscription responses
func (resp *LongviewSubscriptionsPagedResponse) appendData(r *LongviewSubscriptionsPagedResponse) {
resp.Data = append(resp.Data, r.Data...)
}
// ListLongviewSubscriptions lists LongviewSubscriptions
func (c *Client) ListLongviewSubscriptions(ctx context.Context, opts *ListOptions) ([]LongviewSubscription, error) {
response := LongviewSubscriptionsPagedResponse{}
err := c.listHelper(ctx, &response, opts)
for i := range response.Data {
response.Data[i].fixDates()
}
if err != nil {
return nil, err
}
return response.Data, nil
}
// fixDates converts JSON timestamps to Go time.Time values
func (v *LongviewSubscription) fixDates() *LongviewSubscription {
// v.Created, _ = parseDates(v.CreatedStr)
// v.Updated, _ = parseDates(v.UpdatedStr)
return v
}
// GetLongviewSubscription gets the template with the provided ID
func (c *Client) GetLongviewSubscription(ctx context.Context, id string) (*LongviewSubscription, error) {
e, err := c.LongviewSubscriptions.Endpoint()
if err != nil {
return nil, err
}
e = fmt.Sprintf("%s/%s", e, id)
r, err := c.R(ctx).SetResult(&LongviewSubscription{}).Get(e)
if err != nil {
return nil, err
}
return r.Result().(*LongviewSubscription).fixDates(), nil
}

1
vendor/github.com/linode/linodego/managed.go generated vendored Normal file
View file

@ -0,0 +1 @@
package linodego

50
vendor/github.com/linode/linodego/network_ips.go generated vendored Normal file
View file

@ -0,0 +1,50 @@
package linodego
import (
"context"
"fmt"
)
// IPAddressesPagedResponse represents a paginated IPAddress API response
type IPAddressesPagedResponse struct {
*PageOptions
Data []InstanceIP `json:"data"`
}
// endpoint gets the endpoint URL for IPAddress
func (IPAddressesPagedResponse) endpoint(c *Client) string {
endpoint, err := c.IPAddresses.Endpoint()
if err != nil {
panic(err)
}
return endpoint
}
// appendData appends IPAddresses when processing paginated InstanceIPAddress responses
func (resp *IPAddressesPagedResponse) appendData(r *IPAddressesPagedResponse) {
resp.Data = append(resp.Data, r.Data...)
}
// ListIPAddresses lists IPAddresses
func (c *Client) ListIPAddresses(ctx context.Context, opts *ListOptions) ([]InstanceIP, error) {
response := IPAddressesPagedResponse{}
err := c.listHelper(ctx, &response, opts)
if err != nil {
return nil, err
}
return response.Data, nil
}
// GetIPAddress gets the template with the provided ID
func (c *Client) GetIPAddress(ctx context.Context, id string) (*InstanceIP, error) {
e, err := c.IPAddresses.Endpoint()
if err != nil {
return nil, err
}
e = fmt.Sprintf("%s/%s", e, id)
r, err := coupleAPIErrors(c.R(ctx).SetResult(&InstanceIP{}).Get(e))
if err != nil {
return nil, err
}
return r.Result().(*InstanceIP), nil
}

50
vendor/github.com/linode/linodego/network_pools.go generated vendored Normal file
View file

@ -0,0 +1,50 @@
package linodego
import (
"context"
"fmt"
)
// IPv6PoolsPagedResponse represents a paginated IPv6Pool API response
type IPv6PoolsPagedResponse struct {
*PageOptions
Data []IPv6Range `json:"data"`
}
// endpoint gets the endpoint URL for IPv6Pool
func (IPv6PoolsPagedResponse) endpoint(c *Client) string {
endpoint, err := c.IPv6Pools.Endpoint()
if err != nil {
panic(err)
}
return endpoint
}
// appendData appends IPv6Pools when processing paginated IPv6Pool responses
func (resp *IPv6PoolsPagedResponse) appendData(r *IPv6PoolsPagedResponse) {
resp.Data = append(resp.Data, r.Data...)
}
// ListIPv6Pools lists IPv6Pools
func (c *Client) ListIPv6Pools(ctx context.Context, opts *ListOptions) ([]IPv6Range, error) {
response := IPv6PoolsPagedResponse{}
err := c.listHelper(ctx, &response, opts)
if err != nil {
return nil, err
}
return response.Data, nil
}
// GetIPv6Pool gets the template with the provided ID
func (c *Client) GetIPv6Pool(ctx context.Context, id string) (*IPv6Range, error) {
e, err := c.IPv6Pools.Endpoint()
if err != nil {
return nil, err
}
e = fmt.Sprintf("%s/%s", e, id)
r, err := coupleAPIErrors(c.R(ctx).SetResult(&IPv6Range{}).Get(e))
if err != nil {
return nil, err
}
return r.Result().(*IPv6Range), nil
}

50
vendor/github.com/linode/linodego/network_ranges.go generated vendored Normal file
View file

@ -0,0 +1,50 @@
package linodego
import (
"context"
"fmt"
)
// IPv6RangesPagedResponse represents a paginated IPv6Range API response
type IPv6RangesPagedResponse struct {
*PageOptions
Data []IPv6Range `json:"data"`
}
// endpoint gets the endpoint URL for IPv6Range
func (IPv6RangesPagedResponse) endpoint(c *Client) string {
endpoint, err := c.IPv6Ranges.Endpoint()
if err != nil {
panic(err)
}
return endpoint
}
// appendData appends IPv6Ranges when processing paginated IPv6Range responses
func (resp *IPv6RangesPagedResponse) appendData(r *IPv6RangesPagedResponse) {
resp.Data = append(resp.Data, r.Data...)
}
// ListIPv6Ranges lists IPv6Ranges
func (c *Client) ListIPv6Ranges(ctx context.Context, opts *ListOptions) ([]IPv6Range, error) {
response := IPv6RangesPagedResponse{}
err := c.listHelper(ctx, &response, opts)
if err != nil {
return nil, err
}
return response.Data, nil
}
// GetIPv6Range gets the template with the provided ID
func (c *Client) GetIPv6Range(ctx context.Context, id string) (*IPv6Range, error) {
e, err := c.IPv6Ranges.Endpoint()
if err != nil {
return nil, err
}
e = fmt.Sprintf("%s/%s", e, id)
r, err := coupleAPIErrors(c.R(ctx).SetResult(&IPv6Range{}).Get(e))
if err != nil {
return nil, err
}
return r.Result().(*IPv6Range), nil
}

192
vendor/github.com/linode/linodego/nodebalancer.go generated vendored Normal file
View file

@ -0,0 +1,192 @@
package linodego
import (
"context"
"encoding/json"
"fmt"
"time"
)
// NodeBalancer represents a NodeBalancer object
type NodeBalancer struct {
CreatedStr string `json:"created"`
UpdatedStr string `json:"updated"`
// This NodeBalancer's unique ID.
ID int `json:"id"`
// This NodeBalancer's label. These must be unique on your Account.
Label *string `json:"label"`
// The Region where this NodeBalancer is located. NodeBalancers only support backends in the same Region.
Region string `json:"region"`
// This NodeBalancer's hostname, ending with .nodebalancer.linode.com
Hostname *string `json:"hostname"`
// This NodeBalancer's public IPv4 address.
IPv4 *string `json:"ipv4"`
// This NodeBalancer's public IPv6 address.
IPv6 *string `json:"ipv6"`
// Throttle connections per second (0-20). Set to 0 (zero) to disable throttling.
ClientConnThrottle int `json:"client_conn_throttle"`
// Information about the amount of transfer this NodeBalancer has had so far this month.
Transfer NodeBalancerTransfer `json:"transfer"`
Created *time.Time `json:"-"`
Updated *time.Time `json:"-"`
}
// NodeBalancerTransfer contains information about the amount of transfer a NodeBalancer has had in the current month
type NodeBalancerTransfer struct {
// The total transfer, in MB, used by this NodeBalancer this month.
Total *float64 `json:"total"`
// The total inbound transfer, in MB, used for this NodeBalancer this month.
Out *float64 `json:"out"`
// The total outbound transfer, in MB, used for this NodeBalancer this month.
In *float64 `json:"in"`
}
// NodeBalancerCreateOptions are the options permitted for CreateNodeBalancer
type NodeBalancerCreateOptions struct {
Label *string `json:"label,omitempty"`
Region string `json:"region,omitempty"`
ClientConnThrottle *int `json:"client_conn_throttle,omitempty"`
Configs []*NodeBalancerConfigCreateOptions `json:"configs,omitempty"`
}
// NodeBalancerUpdateOptions are the options permitted for UpdateNodeBalancer
type NodeBalancerUpdateOptions struct {
Label *string `json:"label,omitempty"`
ClientConnThrottle *int `json:"client_conn_throttle,omitempty"`
}
// GetCreateOptions converts a NodeBalancer to NodeBalancerCreateOptions for use in CreateNodeBalancer
func (i NodeBalancer) GetCreateOptions() NodeBalancerCreateOptions {
return NodeBalancerCreateOptions{
Label: i.Label,
Region: i.Region,
ClientConnThrottle: &i.ClientConnThrottle,
}
}
// GetUpdateOptions converts a NodeBalancer to NodeBalancerUpdateOptions for use in UpdateNodeBalancer
func (i NodeBalancer) GetUpdateOptions() NodeBalancerUpdateOptions {
return NodeBalancerUpdateOptions{
Label: i.Label,
ClientConnThrottle: &i.ClientConnThrottle,
}
}
// NodeBalancersPagedResponse represents a paginated NodeBalancer API response
type NodeBalancersPagedResponse struct {
*PageOptions
Data []NodeBalancer `json:"data"`
}
func (NodeBalancersPagedResponse) endpoint(c *Client) string {
endpoint, err := c.NodeBalancers.Endpoint()
if err != nil {
panic(err)
}
return endpoint
}
func (resp *NodeBalancersPagedResponse) appendData(r *NodeBalancersPagedResponse) {
resp.Data = append(resp.Data, r.Data...)
}
// ListNodeBalancers lists NodeBalancers
func (c *Client) ListNodeBalancers(ctx context.Context, opts *ListOptions) ([]NodeBalancer, error) {
response := NodeBalancersPagedResponse{}
err := c.listHelper(ctx, &response, opts)
if err != nil {
return nil, err
}
return response.Data, nil
}
// fixDates converts JSON timestamps to Go time.Time values
func (i *NodeBalancer) fixDates() *NodeBalancer {
i.Created, _ = parseDates(i.CreatedStr)
i.Updated, _ = parseDates(i.UpdatedStr)
return i
}
// GetNodeBalancer gets the NodeBalancer with the provided ID
func (c *Client) GetNodeBalancer(ctx context.Context, id int) (*NodeBalancer, error) {
e, err := c.NodeBalancers.Endpoint()
if err != nil {
return nil, err
}
e = fmt.Sprintf("%s/%d", e, id)
r, err := coupleAPIErrors(c.R(ctx).
SetResult(&NodeBalancer{}).
Get(e))
if err != nil {
return nil, err
}
return r.Result().(*NodeBalancer).fixDates(), nil
}
// CreateNodeBalancer creates a NodeBalancer
func (c *Client) CreateNodeBalancer(ctx context.Context, nodebalancer NodeBalancerCreateOptions) (*NodeBalancer, error) {
var body string
e, err := c.NodeBalancers.Endpoint()
if err != nil {
return nil, err
}
req := c.R(ctx).SetResult(&NodeBalancer{})
if bodyData, err := json.Marshal(nodebalancer); err == nil {
body = string(bodyData)
} else {
return nil, NewError(err)
}
r, err := coupleAPIErrors(req.
SetHeader("Content-Type", "application/json").
SetBody(body).
Post(e))
if err != nil {
return nil, err
}
return r.Result().(*NodeBalancer).fixDates(), nil
}
// UpdateNodeBalancer updates the NodeBalancer with the specified id
func (c *Client) UpdateNodeBalancer(ctx context.Context, id int, updateOpts NodeBalancerUpdateOptions) (*NodeBalancer, error) {
var body string
e, err := c.NodeBalancers.Endpoint()
if err != nil {
return nil, err
}
e = fmt.Sprintf("%s/%d", e, id)
req := c.R(ctx).SetResult(&NodeBalancer{})
if bodyData, err := json.Marshal(updateOpts); err == nil {
body = string(bodyData)
} else {
return nil, NewError(err)
}
r, err := coupleAPIErrors(req.
SetBody(body).
Put(e))
if err != nil {
return nil, err
}
return r.Result().(*NodeBalancer).fixDates(), nil
}
// DeleteNodeBalancer deletes the NodeBalancer with the specified id
func (c *Client) DeleteNodeBalancer(ctx context.Context, id int) error {
e, err := c.NodeBalancers.Endpoint()
if err != nil {
return err
}
e = fmt.Sprintf("%s/%d", e, id)
_, err = coupleAPIErrors(c.R(ctx).Delete(e))
return err
}

View file

@ -0,0 +1,186 @@
package linodego
import (
"context"
"encoding/json"
"fmt"
)
// NodeBalancerNode objects represent a backend that can accept traffic for a NodeBalancer Config
type NodeBalancerNode struct {
ID int `json:"id"`
Address string `json:"address"`
Label string `json:"label"`
Status string `json:"status"`
Weight int `json:"weight"`
Mode NodeMode `json:"mode"`
ConfigID int `json:"config_id"`
NodeBalancerID int `json:"nodebalancer_id"`
}
// NodeMode is the mode a NodeBalancer should use when sending traffic to a NodeBalancer Node
type NodeMode string
var (
// ModeAccept is the NodeMode indicating a NodeBalancer Node is accepting traffic
ModeAccept NodeMode = "accept"
// ModeReject is the NodeMode indicating a NodeBalancer Node is not receiving traffic
ModeReject NodeMode = "reject"
// ModeDrain is the NodeMode indicating a NodeBalancer Node is not receiving new traffic, but may continue receiving traffic from pinned connections
ModeDrain NodeMode = "drain"
)
// NodeBalancerNodeCreateOptions fields are those accepted by CreateNodeBalancerNode
type NodeBalancerNodeCreateOptions struct {
Address string `json:"address"`
Label string `json:"label"`
Weight int `json:"weight,omitempty"`
Mode NodeMode `json:"mode,omitempty"`
}
// NodeBalancerNodeUpdateOptions fields are those accepted by UpdateNodeBalancerNode
type NodeBalancerNodeUpdateOptions struct {
Address string `json:"address,omitempty"`
Label string `json:"label,omitempty"`
Weight int `json:"weight,omitempty"`
Mode NodeMode `json:"mode,omitempty"`
}
// GetCreateOptions converts a NodeBalancerNode to NodeBalancerNodeCreateOptions for use in CreateNodeBalancerNode
func (i NodeBalancerNode) GetCreateOptions() NodeBalancerNodeCreateOptions {
return NodeBalancerNodeCreateOptions{
Address: i.Address,
Label: i.Label,
Weight: i.Weight,
Mode: i.Mode,
}
}
// GetUpdateOptions converts a NodeBalancerNode to NodeBalancerNodeUpdateOptions for use in UpdateNodeBalancerNode
func (i NodeBalancerNode) GetUpdateOptions() NodeBalancerNodeUpdateOptions {
return NodeBalancerNodeUpdateOptions{
Address: i.Address,
Label: i.Label,
Weight: i.Weight,
Mode: i.Mode,
}
}
// NodeBalancerNodesPagedResponse represents a paginated NodeBalancerNode API response
type NodeBalancerNodesPagedResponse struct {
*PageOptions
Data []NodeBalancerNode `json:"data"`
}
// endpoint gets the endpoint URL for NodeBalancerNode
func (NodeBalancerNodesPagedResponse) endpointWithTwoIDs(c *Client, nodebalancerID int, configID int) string {
endpoint, err := c.NodeBalancerNodes.endpointWithID(nodebalancerID, configID)
if err != nil {
panic(err)
}
return endpoint
}
// appendData appends NodeBalancerNodes when processing paginated NodeBalancerNode responses
func (resp *NodeBalancerNodesPagedResponse) appendData(r *NodeBalancerNodesPagedResponse) {
resp.Data = append(resp.Data, r.Data...)
}
// ListNodeBalancerNodes lists NodeBalancerNodes
func (c *Client) ListNodeBalancerNodes(ctx context.Context, nodebalancerID int, configID int, opts *ListOptions) ([]NodeBalancerNode, error) {
response := NodeBalancerNodesPagedResponse{}
err := c.listHelperWithTwoIDs(ctx, &response, nodebalancerID, configID, opts)
for i := range response.Data {
response.Data[i].fixDates()
}
if err != nil {
return nil, err
}
return response.Data, nil
}
// fixDates converts JSON timestamps to Go time.Time values
func (i *NodeBalancerNode) fixDates() *NodeBalancerNode {
return i
}
// GetNodeBalancerNode gets the template with the provided ID
func (c *Client) GetNodeBalancerNode(ctx context.Context, nodebalancerID int, configID int, nodeID int) (*NodeBalancerNode, error) {
e, err := c.NodeBalancerNodes.endpointWithID(nodebalancerID, configID)
if err != nil {
return nil, err
}
e = fmt.Sprintf("%s/%d", e, nodeID)
r, err := coupleAPIErrors(c.R(ctx).SetResult(&NodeBalancerNode{}).Get(e))
if err != nil {
return nil, err
}
return r.Result().(*NodeBalancerNode).fixDates(), nil
}
// CreateNodeBalancerNode creates a NodeBalancerNode
func (c *Client) CreateNodeBalancerNode(ctx context.Context, nodebalancerID int, configID int, createOpts NodeBalancerNodeCreateOptions) (*NodeBalancerNode, error) {
var body string
e, err := c.NodeBalancerNodes.endpointWithID(nodebalancerID, configID)
if err != nil {
return nil, err
}
req := c.R(ctx).SetResult(&NodeBalancerNode{})
if bodyData, err := json.Marshal(createOpts); err == nil {
body = string(bodyData)
} else {
return nil, NewError(err)
}
r, err := coupleAPIErrors(req.
SetBody(body).
Post(e))
if err != nil {
return nil, err
}
return r.Result().(*NodeBalancerNode).fixDates(), nil
}
// UpdateNodeBalancerNode updates the NodeBalancerNode with the specified id
func (c *Client) UpdateNodeBalancerNode(ctx context.Context, nodebalancerID int, configID int, nodeID int, updateOpts NodeBalancerNodeUpdateOptions) (*NodeBalancerNode, error) {
var body string
e, err := c.NodeBalancerNodes.endpointWithID(nodebalancerID, configID)
if err != nil {
return nil, err
}
e = fmt.Sprintf("%s/%d", e, nodeID)
req := c.R(ctx).SetResult(&NodeBalancerNode{})
if bodyData, err := json.Marshal(updateOpts); err == nil {
body = string(bodyData)
} else {
return nil, NewError(err)
}
r, err := coupleAPIErrors(req.
SetBody(body).
Put(e))
if err != nil {
return nil, err
}
return r.Result().(*NodeBalancerNode).fixDates(), nil
}
// DeleteNodeBalancerNode deletes the NodeBalancerNode with the specified id
func (c *Client) DeleteNodeBalancerNode(ctx context.Context, nodebalancerID int, configID int, nodeID int) error {
e, err := c.NodeBalancerNodes.endpointWithID(nodebalancerID, configID)
if err != nil {
return err
}
e = fmt.Sprintf("%s/%d", e, nodeID)
_, err = coupleAPIErrors(c.R(ctx).Delete(e))
return err
}

View file

@ -0,0 +1,334 @@
package linodego
import (
"context"
"encoding/json"
"fmt"
)
// NodeBalancerConfig objects allow a NodeBalancer to accept traffic on a new port
type NodeBalancerConfig struct {
ID int `json:"id"`
Port int `json:"port"`
Protocol ConfigProtocol `json:"protocol"`
Algorithm ConfigAlgorithm `json:"algorithm"`
Stickiness ConfigStickiness `json:"stickiness"`
Check ConfigCheck `json:"check"`
CheckInterval int `json:"check_interval"`
CheckAttempts int `json:"check_attempts"`
CheckPath string `json:"check_path"`
CheckBody string `json:"check_body"`
CheckPassive bool `json:"check_passive"`
CheckTimeout int `json:"check_timeout"`
CipherSuite ConfigCipher `json:"cipher_suite"`
NodeBalancerID int `json:"nodebalancer_id"`
SSLCommonName string `json:"ssl_commonname"`
SSLFingerprint string `json:"ssl_fingerprint"`
SSLCert string `json:"ssl_cert"`
SSLKey string `json:"ssl_key"`
NodesStatus *NodeBalancerNodeStatus `json:"nodes_status"`
}
// ConfigAlgorithm constants start with Algorithm and include Linode API NodeBalancer Config Algorithms
type ConfigAlgorithm string
// ConfigAlgorithm constants reflect the NodeBalancer Config Algorithm
const (
AlgorithmRoundRobin ConfigAlgorithm = "roundrobin"
AlgorithmLeastConn ConfigAlgorithm = "leastconn"
AlgorithmSource ConfigAlgorithm = "source"
)
// ConfigStickiness constants start with Stickiness and include Linode API NodeBalancer Config Stickiness
type ConfigStickiness string
// ConfigStickiness constants reflect the node stickiness method for a NodeBalancer Config
const (
StickinessNone ConfigStickiness = "none"
StickinessTable ConfigStickiness = "table"
StickinessHTTPCookie ConfigStickiness = "http_cookie"
)
// ConfigCheck constants start with Check and include Linode API NodeBalancer Config Check methods
type ConfigCheck string
// ConfigCheck constants reflect the node health status checking method for a NodeBalancer Config
const (
CheckNone ConfigCheck = "none"
CheckConnection ConfigCheck = "connection"
CheckHTTP ConfigCheck = "http"
CheckHTTPBody ConfigCheck = "http_body"
)
// ConfigProtocol constants start with Protocol and include Linode API Nodebalancer Config protocols
type ConfigProtocol string
// ConfigProtocol constants reflect the protocol used by a NodeBalancer Config
const (
ProtocolHTTP ConfigProtocol = "http"
ProtocolHTTPS ConfigProtocol = "https"
ProtocolTCP ConfigProtocol = "tcp"
)
// ConfigCipher constants start with Cipher and include Linode API NodeBalancer Config Cipher values
type ConfigCipher string
// ConfigCipher constants reflect the preferred cipher set for a NodeBalancer Config
const (
CipherRecommended ConfigCipher = "recommended"
CipherLegacy ConfigCipher = "legacy"
)
// NodeBalancerNodeStatus represents the total number of nodes whose status is Up or Down
type NodeBalancerNodeStatus struct {
Up int `json:"up"`
Down int `json:"down"`
}
// NodeBalancerConfigCreateOptions are permitted by CreateNodeBalancerConfig
type NodeBalancerConfigCreateOptions struct {
Port int `json:"port"`
Protocol ConfigProtocol `json:"protocol,omitempty"`
Algorithm ConfigAlgorithm `json:"algorithm,omitempty"`
Stickiness ConfigStickiness `json:"stickiness,omitempty"`
Check ConfigCheck `json:"check,omitempty"`
CheckInterval int `json:"check_interval,omitempty"`
CheckAttempts int `json:"check_attempts,omitempty"`
CheckPath string `json:"check_path,omitempty"`
CheckBody string `json:"check_body,omitempty"`
CheckPassive *bool `json:"check_passive,omitempty"`
CheckTimeout int `json:"check_timeout,omitempty"`
CipherSuite ConfigCipher `json:"cipher_suite,omitempty"`
SSLCert string `json:"ssl_cert,omitempty"`
SSLKey string `json:"ssl_key,omitempty"`
Nodes []NodeBalancerNodeCreateOptions `json:"nodes,omitempty"`
}
// NodeBalancerConfigRebuildOptions used by RebuildNodeBalancerConfig
type NodeBalancerConfigRebuildOptions struct {
Port int `json:"port"`
Protocol ConfigProtocol `json:"protocol,omitempty"`
Algorithm ConfigAlgorithm `json:"algorithm,omitempty"`
Stickiness ConfigStickiness `json:"stickiness,omitempty"`
Check ConfigCheck `json:"check,omitempty"`
CheckInterval int `json:"check_interval,omitempty"`
CheckAttempts int `json:"check_attempts,omitempty"`
CheckPath string `json:"check_path,omitempty"`
CheckBody string `json:"check_body,omitempty"`
CheckPassive *bool `json:"check_passive,omitempty"`
CheckTimeout int `json:"check_timeout,omitempty"`
CipherSuite ConfigCipher `json:"cipher_suite,omitempty"`
SSLCert string `json:"ssl_cert,omitempty"`
SSLKey string `json:"ssl_key,omitempty"`
Nodes []NodeBalancerNodeCreateOptions `json:"nodes"`
}
// NodeBalancerConfigUpdateOptions are permitted by UpdateNodeBalancerConfig
type NodeBalancerConfigUpdateOptions NodeBalancerConfigCreateOptions
// GetCreateOptions converts a NodeBalancerConfig to NodeBalancerConfigCreateOptions for use in CreateNodeBalancerConfig
func (i NodeBalancerConfig) GetCreateOptions() NodeBalancerConfigCreateOptions {
return NodeBalancerConfigCreateOptions{
Port: i.Port,
Protocol: i.Protocol,
Algorithm: i.Algorithm,
Stickiness: i.Stickiness,
Check: i.Check,
CheckInterval: i.CheckInterval,
CheckAttempts: i.CheckAttempts,
CheckTimeout: i.CheckTimeout,
CheckPath: i.CheckPath,
CheckBody: i.CheckBody,
CheckPassive: &i.CheckPassive,
CipherSuite: i.CipherSuite,
SSLCert: i.SSLCert,
SSLKey: i.SSLKey,
}
}
// GetUpdateOptions converts a NodeBalancerConfig to NodeBalancerConfigUpdateOptions for use in UpdateNodeBalancerConfig
func (i NodeBalancerConfig) GetUpdateOptions() NodeBalancerConfigUpdateOptions {
return NodeBalancerConfigUpdateOptions{
Port: i.Port,
Protocol: i.Protocol,
Algorithm: i.Algorithm,
Stickiness: i.Stickiness,
Check: i.Check,
CheckInterval: i.CheckInterval,
CheckAttempts: i.CheckAttempts,
CheckPath: i.CheckPath,
CheckBody: i.CheckBody,
CheckPassive: &i.CheckPassive,
CheckTimeout: i.CheckTimeout,
CipherSuite: i.CipherSuite,
SSLCert: i.SSLCert,
SSLKey: i.SSLKey,
}
}
// GetRebuildOptions converts a NodeBalancerConfig to NodeBalancerConfigRebuildOptions for use in RebuildNodeBalancerConfig
func (i NodeBalancerConfig) GetRebuildOptions() NodeBalancerConfigRebuildOptions {
return NodeBalancerConfigRebuildOptions{
Port: i.Port,
Protocol: i.Protocol,
Algorithm: i.Algorithm,
Stickiness: i.Stickiness,
Check: i.Check,
CheckInterval: i.CheckInterval,
CheckAttempts: i.CheckAttempts,
CheckTimeout: i.CheckTimeout,
CheckPath: i.CheckPath,
CheckBody: i.CheckBody,
CheckPassive: &i.CheckPassive,
CipherSuite: i.CipherSuite,
SSLCert: i.SSLCert,
SSLKey: i.SSLKey,
Nodes: make([]NodeBalancerNodeCreateOptions, 0),
}
}
// NodeBalancerConfigsPagedResponse represents a paginated NodeBalancerConfig API response
type NodeBalancerConfigsPagedResponse struct {
*PageOptions
Data []NodeBalancerConfig `json:"data"`
}
// endpointWithID gets the endpoint URL for NodeBalancerConfig
func (NodeBalancerConfigsPagedResponse) endpointWithID(c *Client, id int) string {
endpoint, err := c.NodeBalancerConfigs.endpointWithID(id)
if err != nil {
panic(err)
}
return endpoint
}
// appendData appends NodeBalancerConfigs when processing paginated NodeBalancerConfig responses
func (resp *NodeBalancerConfigsPagedResponse) appendData(r *NodeBalancerConfigsPagedResponse) {
resp.Data = append(resp.Data, r.Data...)
}
// ListNodeBalancerConfigs lists NodeBalancerConfigs
func (c *Client) ListNodeBalancerConfigs(ctx context.Context, nodebalancerID int, opts *ListOptions) ([]NodeBalancerConfig, error) {
response := NodeBalancerConfigsPagedResponse{}
err := c.listHelperWithID(ctx, &response, nodebalancerID, opts)
for i := range response.Data {
response.Data[i].fixDates()
}
if err != nil {
return nil, err
}
return response.Data, nil
}
// fixDates converts JSON timestamps to Go time.Time values
func (i *NodeBalancerConfig) fixDates() *NodeBalancerConfig {
return i
}
// GetNodeBalancerConfig gets the template with the provided ID
func (c *Client) GetNodeBalancerConfig(ctx context.Context, nodebalancerID int, configID int) (*NodeBalancerConfig, error) {
e, err := c.NodeBalancerConfigs.endpointWithID(nodebalancerID)
if err != nil {
return nil, err
}
e = fmt.Sprintf("%s/%d", e, configID)
r, err := coupleAPIErrors(c.R(ctx).SetResult(&NodeBalancerConfig{}).Get(e))
if err != nil {
return nil, err
}
return r.Result().(*NodeBalancerConfig).fixDates(), nil
}
// CreateNodeBalancerConfig creates a NodeBalancerConfig
func (c *Client) CreateNodeBalancerConfig(ctx context.Context, nodebalancerID int, nodebalancerConfig NodeBalancerConfigCreateOptions) (*NodeBalancerConfig, error) {
var body string
e, err := c.NodeBalancerConfigs.endpointWithID(nodebalancerID)
if err != nil {
return nil, err
}
req := c.R(ctx).SetResult(&NodeBalancerConfig{})
if bodyData, err := json.Marshal(nodebalancerConfig); err == nil {
body = string(bodyData)
} else {
return nil, NewError(err)
}
r, err := coupleAPIErrors(req.
SetHeader("Content-Type", "application/json").
SetBody(body).
Post(e))
if err != nil {
return nil, err
}
return r.Result().(*NodeBalancerConfig).fixDates(), nil
}
// UpdateNodeBalancerConfig updates the NodeBalancerConfig with the specified id
func (c *Client) UpdateNodeBalancerConfig(ctx context.Context, nodebalancerID int, configID int, updateOpts NodeBalancerConfigUpdateOptions) (*NodeBalancerConfig, error) {
var body string
e, err := c.NodeBalancerConfigs.endpointWithID(nodebalancerID)
if err != nil {
return nil, err
}
e = fmt.Sprintf("%s/%d", e, configID)
req := c.R(ctx).SetResult(&NodeBalancerConfig{})
if bodyData, err := json.Marshal(updateOpts); err == nil {
body = string(bodyData)
} else {
return nil, NewError(err)
}
r, err := coupleAPIErrors(req.
SetBody(body).
Put(e))
if err != nil {
return nil, err
}
return r.Result().(*NodeBalancerConfig).fixDates(), nil
}
// DeleteNodeBalancerConfig deletes the NodeBalancerConfig with the specified id
func (c *Client) DeleteNodeBalancerConfig(ctx context.Context, nodebalancerID int, configID int) error {
e, err := c.NodeBalancerConfigs.endpointWithID(nodebalancerID)
if err != nil {
return err
}
e = fmt.Sprintf("%s/%d", e, configID)
_, err = coupleAPIErrors(c.R(ctx).Delete(e))
return err
}
// RebuildNodeBalancerConfig updates the NodeBalancer with the specified id
func (c *Client) RebuildNodeBalancerConfig(ctx context.Context, nodeBalancerID int, configID int, rebuildOpts NodeBalancerConfigRebuildOptions) (*NodeBalancerConfig, error) {
var body string
e, err := c.NodeBalancerConfigs.endpointWithID(nodeBalancerID)
if err != nil {
return nil, err
}
e = fmt.Sprintf("%s/%d/rebuild", e, configID)
req := c.R(ctx).SetResult(&NodeBalancerConfig{})
if bodyData, err := json.Marshal(rebuildOpts); err == nil {
body = string(bodyData)
} else {
return nil, NewError(err)
}
r, err := coupleAPIErrors(req.
SetBody(body).
Post(e))
if err != nil {
return nil, err
}
return r.Result().(*NodeBalancerConfig).fixDates(), nil
}

481
vendor/github.com/linode/linodego/pagination.go generated vendored Normal file
View file

@ -0,0 +1,481 @@
package linodego
/**
* Pagination and Filtering types and helpers
*/
import (
"context"
"fmt"
"log"
"strconv"
"github.com/go-resty/resty"
)
// PageOptions are the pagination parameters for List endpoints
type PageOptions struct {
Page int `url:"page,omitempty" json:"page"`
Pages int `url:"pages,omitempty" json:"pages"`
Results int `url:"results,omitempty" json:"results"`
}
// ListOptions are the pagination and filtering (TODO) parameters for endpoints
type ListOptions struct {
*PageOptions
Filter string
}
// NewListOptions simplified construction of ListOptions using only
// the two writable properties, Page and Filter
func NewListOptions(Page int, Filter string) *ListOptions {
return &ListOptions{PageOptions: &PageOptions{Page: Page}, Filter: Filter}
}
// listHelper abstracts fetching and pagination for GET endpoints that
// do not require any Ids (top level endpoints).
// When opts (or opts.Page) is nil, all pages will be fetched and
// returned in a single (endpoint-specific)PagedResponse
// opts.results and opts.pages will be updated from the API response
func (c *Client) listHelper(ctx context.Context, i interface{}, opts *ListOptions) error {
req := c.R(ctx)
if opts != nil && opts.PageOptions != nil && opts.Page > 0 {
req.SetQueryParam("page", strconv.Itoa(opts.Page))
}
var (
err error
pages int
results int
r *resty.Response
)
if opts != nil && len(opts.Filter) > 0 {
req.SetHeader("X-Filter", opts.Filter)
}
switch v := i.(type) {
case *LinodeKernelsPagedResponse:
if r, err = coupleAPIErrors(req.SetResult(LinodeKernelsPagedResponse{}).Get(v.endpoint(c))); err == nil {
pages = r.Result().(*LinodeKernelsPagedResponse).Pages
results = r.Result().(*LinodeKernelsPagedResponse).Results
v.appendData(r.Result().(*LinodeKernelsPagedResponse))
}
case *LinodeTypesPagedResponse:
if r, err = coupleAPIErrors(req.SetResult(LinodeTypesPagedResponse{}).Get(v.endpoint(c))); err == nil {
pages = r.Result().(*LinodeTypesPagedResponse).Pages
results = r.Result().(*LinodeTypesPagedResponse).Results
v.appendData(r.Result().(*LinodeTypesPagedResponse))
}
case *ImagesPagedResponse:
if r, err = coupleAPIErrors(req.SetResult(ImagesPagedResponse{}).Get(v.endpoint(c))); err == nil {
pages = r.Result().(*ImagesPagedResponse).Pages
results = r.Result().(*ImagesPagedResponse).Results
v.appendData(r.Result().(*ImagesPagedResponse))
}
case *StackscriptsPagedResponse:
if r, err = coupleAPIErrors(req.SetResult(StackscriptsPagedResponse{}).Get(v.endpoint(c))); err == nil {
pages = r.Result().(*StackscriptsPagedResponse).Pages
results = r.Result().(*StackscriptsPagedResponse).Results
v.appendData(r.Result().(*StackscriptsPagedResponse))
}
case *InstancesPagedResponse:
if r, err = coupleAPIErrors(req.SetResult(InstancesPagedResponse{}).Get(v.endpoint(c))); err == nil {
pages = r.Result().(*InstancesPagedResponse).Pages
results = r.Result().(*InstancesPagedResponse).Results
v.appendData(r.Result().(*InstancesPagedResponse))
}
case *RegionsPagedResponse:
if r, err = coupleAPIErrors(req.SetResult(RegionsPagedResponse{}).Get(v.endpoint(c))); err == nil {
pages = r.Result().(*RegionsPagedResponse).Pages
results = r.Result().(*RegionsPagedResponse).Results
v.appendData(r.Result().(*RegionsPagedResponse))
}
case *VolumesPagedResponse:
if r, err = coupleAPIErrors(req.SetResult(VolumesPagedResponse{}).Get(v.endpoint(c))); err == nil {
pages = r.Result().(*VolumesPagedResponse).Pages
results = r.Result().(*VolumesPagedResponse).Results
v.appendData(r.Result().(*VolumesPagedResponse))
}
case *DomainsPagedResponse:
if r, err = coupleAPIErrors(req.SetResult(DomainsPagedResponse{}).Get(v.endpoint(c))); err == nil {
response, ok := r.Result().(*DomainsPagedResponse)
if !ok {
return fmt.Errorf("Response is not a *DomainsPagedResponse")
}
pages = response.Pages
results = response.Results
v.appendData(response)
}
case *EventsPagedResponse:
if r, err = coupleAPIErrors(req.SetResult(EventsPagedResponse{}).Get(v.endpoint(c))); err == nil {
pages = r.Result().(*EventsPagedResponse).Pages
results = r.Result().(*EventsPagedResponse).Results
v.appendData(r.Result().(*EventsPagedResponse))
}
case *LongviewSubscriptionsPagedResponse:
if r, err = coupleAPIErrors(req.SetResult(LongviewSubscriptionsPagedResponse{}).Get(v.endpoint(c))); err == nil {
pages = r.Result().(*LongviewSubscriptionsPagedResponse).Pages
results = r.Result().(*LongviewSubscriptionsPagedResponse).Results
v.appendData(r.Result().(*LongviewSubscriptionsPagedResponse))
}
case *LongviewClientsPagedResponse:
if r, err = coupleAPIErrors(req.SetResult(LongviewClientsPagedResponse{}).Get(v.endpoint(c))); err == nil {
pages = r.Result().(*LongviewClientsPagedResponse).Pages
results = r.Result().(*LongviewClientsPagedResponse).Results
v.appendData(r.Result().(*LongviewClientsPagedResponse))
}
case *IPAddressesPagedResponse:
if r, err = coupleAPIErrors(req.SetResult(IPAddressesPagedResponse{}).Get(v.endpoint(c))); err == nil {
pages = r.Result().(*IPAddressesPagedResponse).Pages
results = r.Result().(*IPAddressesPagedResponse).Results
v.appendData(r.Result().(*IPAddressesPagedResponse))
}
case *IPv6PoolsPagedResponse:
if r, err = coupleAPIErrors(req.SetResult(IPv6PoolsPagedResponse{}).Get(v.endpoint(c))); err == nil {
pages = r.Result().(*IPv6PoolsPagedResponse).Pages
results = r.Result().(*IPv6PoolsPagedResponse).Results
v.appendData(r.Result().(*IPv6PoolsPagedResponse))
}
case *IPv6RangesPagedResponse:
if r, err = coupleAPIErrors(req.SetResult(IPv6RangesPagedResponse{}).Get(v.endpoint(c))); err == nil {
pages = r.Result().(*IPv6RangesPagedResponse).Pages
results = r.Result().(*IPv6RangesPagedResponse).Results
v.appendData(r.Result().(*IPv6RangesPagedResponse))
// @TODO consolidate this type with IPv6PoolsPagedResponse?
}
case *SSHKeysPagedResponse:
if r, err = coupleAPIErrors(req.SetResult(SSHKeysPagedResponse{}).Get(v.endpoint(c))); err == nil {
response, ok := r.Result().(*SSHKeysPagedResponse)
if !ok {
return fmt.Errorf("Response is not a *SSHKeysPagedResponse")
}
pages = response.Pages
results = response.Results
v.appendData(response)
}
case *TicketsPagedResponse:
if r, err = coupleAPIErrors(req.SetResult(TicketsPagedResponse{}).Get(v.endpoint(c))); err == nil {
pages = r.Result().(*TicketsPagedResponse).Pages
results = r.Result().(*TicketsPagedResponse).Results
v.appendData(r.Result().(*TicketsPagedResponse))
}
case *InvoicesPagedResponse:
if r, err = coupleAPIErrors(req.SetResult(InvoicesPagedResponse{}).Get(v.endpoint(c))); err == nil {
pages = r.Result().(*InvoicesPagedResponse).Pages
results = r.Result().(*InvoicesPagedResponse).Results
v.appendData(r.Result().(*InvoicesPagedResponse))
}
case *NotificationsPagedResponse:
if r, err = coupleAPIErrors(req.SetResult(NotificationsPagedResponse{}).Get(v.endpoint(c))); err == nil {
pages = r.Result().(*NotificationsPagedResponse).Pages
results = r.Result().(*NotificationsPagedResponse).Results
v.appendData(r.Result().(*NotificationsPagedResponse))
}
case *NodeBalancersPagedResponse:
if r, err = coupleAPIErrors(req.SetResult(NodeBalancersPagedResponse{}).Get(v.endpoint(c))); err == nil {
pages = r.Result().(*NodeBalancersPagedResponse).Pages
results = r.Result().(*NodeBalancersPagedResponse).Results
v.appendData(r.Result().(*NodeBalancersPagedResponse))
}
/**
case AccountOauthClientsPagedResponse:
if r, err = req.SetResult(v).Get(v.endpoint(c)); r.Error() != nil {
return NewError(r)
} else if err == nil {
pages = r.Result().(*AccountOauthClientsPagedResponse).Pages
results = r.Result().(*AccountOauthClientsPagedResponse).Results
v.appendData(r.Result().(*AccountOauthClientsPagedResponse))
}
case AccountPaymentsPagedResponse:
if r, err = req.SetResult(v).Get(v.endpoint(c)); r.Error() != nil {
return NewError(r)
} else if err == nil {
pages = r.Result().(*AccountPaymentsPagedResponse).Pages
results = r.Result().(*AccountPaymentsPagedResponse).Results
v.appendData(r.Result().(*AccountPaymentsPagedResponse))
}
case AccountUsersPagedResponse:
if r, err = req.SetResult(v).Get(v.endpoint(c)); r.Error() != nil {
return NewError(r)
} else if err == nil {
pages = r.Result().(*AccountUsersPagedResponse).Pages
results = r.Result().(*AccountUsersPagedResponse).Results
v.appendData(r.Result().(*AccountUsersPagedResponse))
}
case ProfileAppsPagedResponse:
if r, err = req.SetResult(v).Get(v.endpoint(c)); r.Error() != nil {
return NewError(r)
} else if err == nil {
pages = r.Result().(*ProfileAppsPagedResponse).Pages
results = r.Result().(*ProfileAppsPagedResponse).Results
v.appendData(r.Result().(*ProfileAppsPagedResponse))
}
case ProfileTokensPagedResponse:
if r, err = req.SetResult(v).Get(v.endpoint(c)); r.Error() != nil {
return NewError(r)
} else if err == nil {
pages = r.Result().(*ProfileTokensPagedResponse).Pages
results = r.Result().(*ProfileTokensPagedResponse).Results
v.appendData(r.Result().(*ProfileTokensPagedResponse))
}
case ProfileWhitelistPagedResponse:
if r, err = req.SetResult(v).Get(v.endpoint(c)); r.Error() != nil {
return NewError(r)
} else if err == nil {
pages = r.Result().(*ProfileWhitelistPagedResponse).Pages
results = r.Result().(*ProfileWhitelistPagedResponse).Results
v.appendData(r.Result().(*ProfileWhitelistPagedResponse))
}
case ManagedContactsPagedResponse:
if r, err = req.SetResult(v).Get(v.endpoint(c)); r.Error() != nil {
return NewError(r)
} else if err == nil {
pages = r.Result().(*ManagedContactsPagedResponse).Pages
results = r.Result().(*ManagedContactsPagedResponse).Results
v.appendData(r.Result().(*ManagedContactsPagedResponse))
}
case ManagedCredentialsPagedResponse:
if r, err = req.SetResult(v).Get(v.endpoint(c)); r.Error() != nil {
return NewError(r)
} else if err == nil {
pages = r.Result().(*ManagedCredentialsPagedResponse).Pages
results = r.Result().(*ManagedCredentialsPagedResponse).Results
v.appendData(r.Result().(*ManagedCredentialsPagedResponse))
}
case ManagedIssuesPagedResponse:
if r, err = req.SetResult(v).Get(v.endpoint(c)); r.Error() != nil {
return NewError(r)
} else if err == nil {
pages = r.Result().(*ManagedIssuesPagedResponse).Pages
results = r.Result().(*ManagedIssuesPagedResponse).Results
v.appendData(r.Result().(*ManagedIssuesPagedResponse))
}
case ManagedLinodeSettingsPagedResponse:
if r, err = req.SetResult(v).Get(v.endpoint(c)); r.Error() != nil {
return NewError(r)
} else if err == nil {
pages = r.Result().(*ManagedLinodeSettingsPagedResponse).Pages
results = r.Result().(*ManagedLinodeSettingsPagedResponse).Results
v.appendData(r.Result().(*ManagedLinodeSettingsPagedResponse))
}
case ManagedServicesPagedResponse:
if r, err = req.SetResult(v).Get(v.endpoint(c)); r.Error() != nil {
return NewError(r)
} else if err == nil {
pages = r.Result().(*ManagedServicesPagedResponse).Pages
results = r.Result().(*ManagedServicesPagedResponse).Results
v.appendData(r.Result().(*ManagedServicesPagedResponse))
}
**/
default:
log.Fatalf("listHelper interface{} %+v used", i)
}
if err != nil {
return err
}
if opts == nil {
for page := 2; page <= pages; page = page + 1 {
if err := c.listHelper(ctx, i, &ListOptions{PageOptions: &PageOptions{Page: page}}); err != nil {
return err
}
}
} else {
if opts.PageOptions == nil {
opts.PageOptions = &PageOptions{}
}
if opts.Page == 0 {
for page := 2; page <= pages; page = page + 1 {
opts.Page = page
if err := c.listHelper(ctx, i, opts); err != nil {
return err
}
}
}
opts.Results = results
opts.Pages = pages
}
return nil
}
// listHelperWithID abstracts fetching and pagination for GET endpoints that
// require an Id (second level endpoints).
// When opts (or opts.Page) is nil, all pages will be fetched and
// returned in a single (endpoint-specific)PagedResponse
// opts.results and opts.pages will be updated from the API response
func (c *Client) listHelperWithID(ctx context.Context, i interface{}, id int, opts *ListOptions) error {
req := c.R(ctx)
if opts != nil && opts.Page > 0 {
req.SetQueryParam("page", strconv.Itoa(opts.Page))
}
var (
err error
pages int
results int
r *resty.Response
)
if opts != nil && len(opts.Filter) > 0 {
req.SetHeader("X-Filter", opts.Filter)
}
switch v := i.(type) {
case *InvoiceItemsPagedResponse:
if r, err = coupleAPIErrors(req.SetResult(InvoiceItemsPagedResponse{}).Get(v.endpointWithID(c, id))); err == nil {
pages = r.Result().(*InvoiceItemsPagedResponse).Pages
results = r.Result().(*InvoiceItemsPagedResponse).Results
v.appendData(r.Result().(*InvoiceItemsPagedResponse))
}
case *DomainRecordsPagedResponse:
if r, err = coupleAPIErrors(req.SetResult(DomainRecordsPagedResponse{}).Get(v.endpointWithID(c, id))); err == nil {
response, ok := r.Result().(*DomainRecordsPagedResponse)
if !ok {
return fmt.Errorf("Response is not a *DomainRecordsPagedResponse")
}
pages = response.Pages
results = response.Results
v.appendData(response)
}
case *InstanceConfigsPagedResponse:
if r, err = coupleAPIErrors(req.SetResult(InstanceConfigsPagedResponse{}).Get(v.endpointWithID(c, id))); err == nil {
pages = r.Result().(*InstanceConfigsPagedResponse).Pages
results = r.Result().(*InstanceConfigsPagedResponse).Results
v.appendData(r.Result().(*InstanceConfigsPagedResponse))
}
case *InstanceDisksPagedResponse:
if r, err = coupleAPIErrors(req.SetResult(InstanceDisksPagedResponse{}).Get(v.endpointWithID(c, id))); err == nil {
pages = r.Result().(*InstanceDisksPagedResponse).Pages
results = r.Result().(*InstanceDisksPagedResponse).Results
v.appendData(r.Result().(*InstanceDisksPagedResponse))
}
case *NodeBalancerConfigsPagedResponse:
if r, err = coupleAPIErrors(req.SetResult(NodeBalancerConfigsPagedResponse{}).Get(v.endpointWithID(c, id))); err == nil {
pages = r.Result().(*NodeBalancerConfigsPagedResponse).Pages
results = r.Result().(*NodeBalancerConfigsPagedResponse).Results
v.appendData(r.Result().(*NodeBalancerConfigsPagedResponse))
}
case *InstanceVolumesPagedResponse:
if r, err = coupleAPIErrors(req.SetResult(InstanceVolumesPagedResponse{}).Get(v.endpointWithID(c, id))); err == nil {
pages = r.Result().(*InstanceVolumesPagedResponse).Pages
results = r.Result().(*InstanceVolumesPagedResponse).Results
v.appendData(r.Result().(*InstanceVolumesPagedResponse))
}
/**
case TicketAttachmentsPagedResponse:
if r, err = req.SetResult(v).Get(v.endpoint(c)); r.Error() != nil {
return NewError(r)
} else if err == nil {
pages = r.Result().(*TicketAttachmentsPagedResponse).Pages
results = r.Result().(*TicketAttachmentsPagedResponse).Results
v.appendData(r.Result().(*TicketAttachmentsPagedResponse))
}
case TicketRepliesPagedResponse:
if r, err = req.SetResult(v).Get(v.endpoint(c)); r.Error() != nil {
return NewError(r)
} else if err == nil {
pages = r.Result().(*TicketRepliesPagedResponse).Pages
results = r.Result().(*TicketRepliesPagedResponse).Results
v.appendData(r.Result().(*TicketRepliesPagedResponse))
}
**/
default:
log.Fatalf("Unknown listHelperWithID interface{} %T used", i)
}
if err != nil {
return err
}
if opts == nil {
for page := 2; page <= pages; page = page + 1 {
if err := c.listHelperWithID(ctx, i, id, &ListOptions{PageOptions: &PageOptions{Page: page}}); err != nil {
return err
}
}
} else {
if opts.PageOptions == nil {
opts.PageOptions = &PageOptions{}
}
if opts.Page == 0 {
for page := 2; page <= pages; page = page + 1 {
opts.Page = page
if err := c.listHelperWithID(ctx, i, id, opts); err != nil {
return err
}
}
}
opts.Results = results
opts.Pages = pages
}
return nil
}
// listHelperWithTwoIDs abstracts fetching and pagination for GET endpoints that
// require twos IDs (third level endpoints).
// When opts (or opts.Page) is nil, all pages will be fetched and
// returned in a single (endpoint-specific)PagedResponse
// opts.results and opts.pages will be updated from the API response
func (c *Client) listHelperWithTwoIDs(ctx context.Context, i interface{}, firstID, secondID int, opts *ListOptions) error {
req := c.R(ctx)
if opts != nil && opts.Page > 0 {
req.SetQueryParam("page", strconv.Itoa(opts.Page))
}
var (
err error
pages int
results int
r *resty.Response
)
if opts != nil && len(opts.Filter) > 0 {
req.SetHeader("X-Filter", opts.Filter)
}
switch v := i.(type) {
case *NodeBalancerNodesPagedResponse:
if r, err = coupleAPIErrors(req.SetResult(NodeBalancerNodesPagedResponse{}).Get(v.endpointWithTwoIDs(c, firstID, secondID))); err == nil {
pages = r.Result().(*NodeBalancerNodesPagedResponse).Pages
results = r.Result().(*NodeBalancerNodesPagedResponse).Results
v.appendData(r.Result().(*NodeBalancerNodesPagedResponse))
}
default:
log.Fatalf("Unknown listHelperWithTwoIDs interface{} %T used", i)
}
if err != nil {
return err
}
if opts == nil {
for page := 2; page <= pages; page = page + 1 {
if err := c.listHelper(ctx, i, &ListOptions{PageOptions: &PageOptions{Page: page}}); err != nil {
return err
}
}
} else {
if opts.PageOptions == nil {
opts.PageOptions = &PageOptions{}
}
if opts.Page == 0 {
for page := 2; page <= pages; page = page + 1 {
opts.Page = page
if err := c.listHelperWithTwoIDs(ctx, i, firstID, secondID, opts); err != nil {
return err
}
}
}
opts.Results = results
opts.Pages = pages
}
return nil
}

1
vendor/github.com/linode/linodego/profile.go generated vendored Normal file
View file

@ -0,0 +1 @@
package linodego

160
vendor/github.com/linode/linodego/profile_sshkeys.go generated vendored Normal file
View file

@ -0,0 +1,160 @@
package linodego
import (
"context"
"encoding/json"
"fmt"
"time"
)
// SSHKey represents a SSHKey object
type SSHKey struct {
ID int `json:"id"`
Label string `json:"label"`
SSHKey string `json:"ssh_key"`
CreatedStr string `json:"created"`
Created *time.Time `json:"-"`
}
// SSHKeyCreateOptions fields are those accepted by CreateSSHKey
type SSHKeyCreateOptions struct {
Label string `json:"label"`
SSHKey string `json:"ssh_key"`
}
// SSHKeyUpdateOptions fields are those accepted by UpdateSSHKey
type SSHKeyUpdateOptions struct {
Label string `json:"label"`
}
// GetCreateOptions converts a SSHKey to SSHKeyCreateOptions for use in CreateSSHKey
func (i SSHKey) GetCreateOptions() (o SSHKeyCreateOptions) {
o.Label = i.Label
o.SSHKey = i.SSHKey
return
}
// GetUpdateOptions converts a SSHKey to SSHKeyCreateOptions for use in UpdateSSHKey
func (i SSHKey) GetUpdateOptions() (o SSHKeyUpdateOptions) {
o.Label = i.Label
return
}
// SSHKeysPagedResponse represents a paginated SSHKey API response
type SSHKeysPagedResponse struct {
*PageOptions
Data []SSHKey `json:"data"`
}
// endpoint gets the endpoint URL for SSHKey
func (SSHKeysPagedResponse) endpoint(c *Client) string {
endpoint, err := c.SSHKeys.Endpoint()
if err != nil {
panic(err)
}
return endpoint
}
// appendData appends SSHKeys when processing paginated SSHKey responses
func (resp *SSHKeysPagedResponse) appendData(r *SSHKeysPagedResponse) {
resp.Data = append(resp.Data, r.Data...)
}
// ListSSHKeys lists SSHKeys
func (c *Client) ListSSHKeys(ctx context.Context, opts *ListOptions) ([]SSHKey, error) {
response := SSHKeysPagedResponse{}
err := c.listHelper(ctx, &response, opts)
for i := range response.Data {
response.Data[i].fixDates()
}
if err != nil {
return nil, err
}
return response.Data, nil
}
// fixDates converts JSON timestamps to Go time.Time values
func (i *SSHKey) fixDates() *SSHKey {
i.Created, _ = parseDates(i.CreatedStr)
return i
}
// GetSSHKey gets the sshkey with the provided ID
func (c *Client) GetSSHKey(ctx context.Context, id int) (*SSHKey, error) {
e, err := c.SSHKeys.Endpoint()
if err != nil {
return nil, err
}
e = fmt.Sprintf("%s/%d", e, id)
r, err := coupleAPIErrors(c.R(ctx).SetResult(&SSHKey{}).Get(e))
if err != nil {
return nil, err
}
return r.Result().(*SSHKey).fixDates(), nil
}
// CreateSSHKey creates a SSHKey
func (c *Client) CreateSSHKey(ctx context.Context, createOpts SSHKeyCreateOptions) (*SSHKey, error) {
var body string
e, err := c.SSHKeys.Endpoint()
if err != nil {
return nil, err
}
req := c.R(ctx).SetResult(&SSHKey{})
if bodyData, err := json.Marshal(createOpts); err == nil {
body = string(bodyData)
} else {
return nil, NewError(err)
}
r, err := coupleAPIErrors(req.
SetBody(body).
Post(e))
if err != nil {
return nil, err
}
return r.Result().(*SSHKey).fixDates(), nil
}
// UpdateSSHKey updates the SSHKey with the specified id
func (c *Client) UpdateSSHKey(ctx context.Context, id int, updateOpts SSHKeyUpdateOptions) (*SSHKey, error) {
var body string
e, err := c.SSHKeys.Endpoint()
if err != nil {
return nil, err
}
e = fmt.Sprintf("%s/%d", e, id)
req := c.R(ctx).SetResult(&SSHKey{})
if bodyData, err := json.Marshal(updateOpts); err == nil {
body = string(bodyData)
} else {
return nil, NewError(err)
}
r, err := coupleAPIErrors(req.
SetBody(body).
Put(e))
if err != nil {
return nil, err
}
return r.Result().(*SSHKey).fixDates(), nil
}
// DeleteSSHKey deletes the SSHKey with the specified id
func (c *Client) DeleteSSHKey(ctx context.Context, id int) error {
e, err := c.SSHKeys.Endpoint()
if err != nil {
return err
}
e = fmt.Sprintf("%s/%d", e, id)
_, err = coupleAPIErrors(c.R(ctx).Delete(e))
return err
}

64
vendor/github.com/linode/linodego/regions.go generated vendored Normal file
View file

@ -0,0 +1,64 @@
package linodego
import (
"context"
"fmt"
)
// Region represents a linode region object
type Region struct {
ID string `json:"id"`
Country string `json:"country"`
}
// RegionsPagedResponse represents a linode API response for listing
type RegionsPagedResponse struct {
*PageOptions
Data []Region `json:"data"`
}
// endpoint gets the endpoint URL for Region
func (RegionsPagedResponse) endpoint(c *Client) string {
endpoint, err := c.Regions.Endpoint()
if err != nil {
panic(err)
}
return endpoint
}
// appendData appends Regions when processing paginated Region responses
func (resp *RegionsPagedResponse) appendData(r *RegionsPagedResponse) {
resp.Data = append(resp.Data, r.Data...)
}
// ListRegions lists Regions
func (c *Client) ListRegions(ctx context.Context, opts *ListOptions) ([]Region, error) {
response := RegionsPagedResponse{}
err := c.listHelper(ctx, &response, opts)
for i := range response.Data {
response.Data[i].fixDates()
}
if err != nil {
return nil, err
}
return response.Data, nil
}
// fixDates converts JSON timestamps to Go time.Time values
func (v *Region) fixDates() *Region {
return v
}
// GetRegion gets the template with the provided ID
func (c *Client) GetRegion(ctx context.Context, id string) (*Region, error) {
e, err := c.Regions.Endpoint()
if err != nil {
return nil, err
}
e = fmt.Sprintf("%s/%s", e, id)
r, err := coupleAPIErrors(c.R(ctx).SetResult(&Region{}).Get(e))
if err != nil {
return nil, err
}
return r.Result().(*Region).fixDates(), nil
}

155
vendor/github.com/linode/linodego/resources.go generated vendored Normal file
View file

@ -0,0 +1,155 @@
package linodego
import (
"bytes"
"context"
"fmt"
"text/template"
"github.com/go-resty/resty"
)
const (
stackscriptsName = "stackscripts"
imagesName = "images"
instancesName = "instances"
instanceDisksName = "disks"
instanceConfigsName = "configs"
instanceIPsName = "ips"
instanceSnapshotsName = "snapshots"
instanceVolumesName = "instancevolumes"
ipaddressesName = "ipaddresses"
ipv6poolsName = "ipv6pools"
ipv6rangesName = "ipv6ranges"
regionsName = "regions"
volumesName = "volumes"
kernelsName = "kernels"
typesName = "types"
domainsName = "domains"
domainRecordsName = "records"
longviewName = "longview"
longviewclientsName = "longviewclients"
longviewsubscriptionsName = "longviewsubscriptions"
nodebalancersName = "nodebalancers"
nodebalancerconfigsName = "nodebalancerconfigs"
nodebalancernodesName = "nodebalancernodes"
sshkeysName = "sshkeys"
ticketsName = "tickets"
accountName = "account"
eventsName = "events"
invoicesName = "invoices"
invoiceItemsName = "invoiceitems"
profileName = "profile"
managedName = "managed"
// notificationsName = "notifications"
stackscriptsEndpoint = "linode/stackscripts"
imagesEndpoint = "images"
instancesEndpoint = "linode/instances"
instanceConfigsEndpoint = "linode/instances/{{ .ID }}/configs"
instanceDisksEndpoint = "linode/instances/{{ .ID }}/disks"
instanceSnapshotsEndpoint = "linode/instances/{{ .ID }}/backups"
instanceIPsEndpoint = "linode/instances/{{ .ID }}/ips"
instanceVolumesEndpoint = "linode/instances/{{ .ID }}/volumes"
ipaddressesEndpoint = "network/ips"
ipv6poolsEndpoint = "network/ipv6/pools"
ipv6rangesEndpoint = "network/ipv6/ranges"
regionsEndpoint = "regions"
volumesEndpoint = "volumes"
kernelsEndpoint = "linode/kernels"
typesEndpoint = "linode/types"
domainsEndpoint = "domains"
domainRecordsEndpoint = "domains/{{ .ID }}/records"
longviewEndpoint = "longview"
longviewclientsEndpoint = "longview/clients"
longviewsubscriptionsEndpoint = "longview/subscriptions"
nodebalancersEndpoint = "nodebalancers"
// @TODO we can't use these nodebalancer endpoints unless we include these templated fields
// The API seems inconsistent about including parent IDs in objects, (compare instance configs to nb configs)
// Parent IDs would be immutable for updates and are ignored in create requests ..
// Should we include these fields in CreateOpts and UpdateOpts?
nodebalancerconfigsEndpoint = "nodebalancers/{{ .ID }}/configs"
nodebalancernodesEndpoint = "nodebalancers/{{ .ID }}/configs/{{ .SecondID }}/nodes"
sshkeysEndpoint = "profile/sshkeys"
ticketsEndpoint = "support/tickets"
accountEndpoint = "account"
eventsEndpoint = "account/events"
invoicesEndpoint = "account/invoices"
invoiceItemsEndpoint = "account/invoices/{{ .ID }}/items"
profileEndpoint = "profile"
managedEndpoint = "managed"
// notificationsEndpoint = "account/notifications"
)
// Resource represents a linode API resource
type Resource struct {
name string
endpoint string
isTemplate bool
endpointTemplate *template.Template
R func(ctx context.Context) *resty.Request
PR func(ctx context.Context) *resty.Request
}
// NewResource is the factory to create a new Resource struct. If it has a template string the useTemplate bool must be set.
func NewResource(client *Client, name string, endpoint string, useTemplate bool, singleType interface{}, pagedType interface{}) *Resource {
var tmpl *template.Template
if useTemplate {
tmpl = template.Must(template.New(name).Parse(endpoint))
}
r := func(ctx context.Context) *resty.Request {
return client.R(ctx).SetResult(singleType)
}
pr := func(ctx context.Context) *resty.Request {
return client.R(ctx).SetResult(pagedType)
}
return &Resource{name, endpoint, useTemplate, tmpl, r, pr}
}
func (r Resource) render(data ...interface{}) (string, error) {
if data == nil {
return "", NewError("Cannot template endpoint with <nil> data")
}
out := ""
buf := bytes.NewBufferString(out)
var substitutions interface{}
if len(data) == 1 {
substitutions = struct{ ID interface{} }{data[0]}
} else if len(data) == 2 {
substitutions = struct {
ID interface{}
SecondID interface{}
}{data[0], data[1]}
} else {
return "", NewError("Too many arguments to render template (expected 1 or 2)")
}
if err := r.endpointTemplate.Execute(buf, substitutions); err != nil {
return "", NewError(err)
}
return buf.String(), nil
}
// endpointWithID will return the rendered endpoint string for the resource with provided id
func (r Resource) endpointWithID(id ...int) (string, error) {
if !r.isTemplate {
return r.endpoint, nil
}
data := make([]interface{}, len(id))
for i, v := range id {
data[i] = v
}
return r.render(data...)
}
// Endpoint will return the non-templated endpoint string for resource
func (r Resource) Endpoint() (string, error) {
if r.isTemplate {
return "", NewError(fmt.Sprintf("Tried to get endpoint for %s without providing data for template", r.name))
}
return r.endpoint, nil
}

208
vendor/github.com/linode/linodego/stackscripts.go generated vendored Normal file
View file

@ -0,0 +1,208 @@
package linodego
import (
"context"
"encoding/json"
"fmt"
"time"
)
// Stackscript represents a Linode StackScript
type Stackscript struct {
CreatedStr string `json:"created"`
UpdatedStr string `json:"updated"`
ID int `json:"id"`
Username string `json:"username"`
Label string `json:"label"`
Description string `json:"description"`
Images []string `json:"images"`
DeploymentsTotal int `json:"deployments_total"`
DeploymentsActive int `json:"deployments_active"`
IsPublic bool `json:"is_public"`
Created *time.Time `json:"-"`
Updated *time.Time `json:"-"`
RevNote string `json:"rev_note"`
Script string `json:"script"`
UserDefinedFields *[]StackscriptUDF `json:"user_defined_fields"`
UserGravatarID string `json:"user_gravatar_id"`
}
// StackscriptUDF define a single variable that is accepted by a Stackscript
type StackscriptUDF struct {
// A human-readable label for the field that will serve as the input prompt for entering the value during deployment.
Label string `json:"label"`
// The name of the field.
Name string `json:"name"`
// An example value for the field.
Example string `json:"example"`
// A list of acceptable single values for the field.
OneOf string `json:"oneOf,omitempty"`
// A list of acceptable values for the field in any quantity, combination or order.
ManyOf string `json:"manyOf,omitempty"`
// The default value. If not specified, this value will be used.
Default string `json:"default,omitempty"`
}
// StackscriptCreateOptions fields are those accepted by CreateStackscript
type StackscriptCreateOptions struct {
Label string `json:"label"`
Description string `json:"description"`
Images []string `json:"images"`
IsPublic bool `json:"is_public"`
RevNote string `json:"rev_note"`
Script string `json:"script"`
}
// StackscriptUpdateOptions fields are those accepted by UpdateStackscript
type StackscriptUpdateOptions StackscriptCreateOptions
// GetCreateOptions converts a Stackscript to StackscriptCreateOptions for use in CreateStackscript
func (i Stackscript) GetCreateOptions() StackscriptCreateOptions {
return StackscriptCreateOptions{
Label: i.Label,
Description: i.Description,
Images: i.Images,
IsPublic: i.IsPublic,
RevNote: i.RevNote,
Script: i.Script,
}
}
// GetUpdateOptions converts a Stackscript to StackscriptUpdateOptions for use in UpdateStackscript
func (i Stackscript) GetUpdateOptions() StackscriptUpdateOptions {
return StackscriptUpdateOptions{
Label: i.Label,
Description: i.Description,
Images: i.Images,
IsPublic: i.IsPublic,
RevNote: i.RevNote,
Script: i.Script,
}
}
// StackscriptsPagedResponse represents a paginated Stackscript API response
type StackscriptsPagedResponse struct {
*PageOptions
Data []Stackscript `json:"data"`
}
// endpoint gets the endpoint URL for Stackscript
func (StackscriptsPagedResponse) endpoint(c *Client) string {
endpoint, err := c.StackScripts.Endpoint()
if err != nil {
panic(err)
}
return endpoint
}
// appendData appends Stackscripts when processing paginated Stackscript responses
func (resp *StackscriptsPagedResponse) appendData(r *StackscriptsPagedResponse) {
resp.Data = append(resp.Data, r.Data...)
}
// ListStackscripts lists Stackscripts
func (c *Client) ListStackscripts(ctx context.Context, opts *ListOptions) ([]Stackscript, error) {
response := StackscriptsPagedResponse{}
err := c.listHelper(ctx, &response, opts)
for i := range response.Data {
response.Data[i].fixDates()
}
if err != nil {
return nil, err
}
return response.Data, nil
}
// fixDates converts JSON timestamps to Go time.Time values
func (i *Stackscript) fixDates() *Stackscript {
i.Created, _ = parseDates(i.CreatedStr)
i.Updated, _ = parseDates(i.UpdatedStr)
return i
}
// GetStackscript gets the Stackscript with the provided ID
func (c *Client) GetStackscript(ctx context.Context, id int) (*Stackscript, error) {
e, err := c.StackScripts.Endpoint()
if err != nil {
return nil, err
}
e = fmt.Sprintf("%s/%d", e, id)
r, err := coupleAPIErrors(c.R(ctx).
SetResult(&Stackscript{}).
Get(e))
if err != nil {
return nil, err
}
return r.Result().(*Stackscript).fixDates(), nil
}
// CreateStackscript creates a StackScript
func (c *Client) CreateStackscript(ctx context.Context, createOpts StackscriptCreateOptions) (*Stackscript, error) {
var body string
e, err := c.StackScripts.Endpoint()
if err != nil {
return nil, err
}
req := c.R(ctx).SetResult(&Stackscript{})
if bodyData, err := json.Marshal(createOpts); err == nil {
body = string(bodyData)
} else {
return nil, NewError(err)
}
r, err := coupleAPIErrors(req.
SetBody(body).
Post(e))
if err != nil {
return nil, err
}
return r.Result().(*Stackscript).fixDates(), nil
}
// UpdateStackscript updates the StackScript with the specified id
func (c *Client) UpdateStackscript(ctx context.Context, id int, updateOpts StackscriptUpdateOptions) (*Stackscript, error) {
var body string
e, err := c.StackScripts.Endpoint()
if err != nil {
return nil, err
}
e = fmt.Sprintf("%s/%d", e, id)
req := c.R(ctx).SetResult(&Stackscript{})
if bodyData, err := json.Marshal(updateOpts); err == nil {
body = string(bodyData)
} else {
return nil, NewError(err)
}
r, err := coupleAPIErrors(req.
SetBody(body).
Put(e))
if err != nil {
return nil, err
}
return r.Result().(*Stackscript).fixDates(), nil
}
// DeleteStackscript deletes the StackScript with the specified id
func (c *Client) DeleteStackscript(ctx context.Context, id int) error {
e, err := c.StackScripts.Endpoint()
if err != nil {
return err
}
e = fmt.Sprintf("%s/%d", e, id)
_, err = coupleAPIErrors(c.R(ctx).Delete(e))
return err
}

88
vendor/github.com/linode/linodego/support.go generated vendored Normal file
View file

@ -0,0 +1,88 @@
package linodego
import (
"context"
"fmt"
"time"
)
// Ticket represents a support ticket object
type Ticket struct {
ID int `json:"id"`
Attachments []string `json:"attachments"`
Closed *time.Time `json:"-"`
Description string `json:"description"`
Entity *TicketEntity `json:"entity"`
GravatarID string `json:"gravatar_id"`
Opened *time.Time `json:"-"`
OpenedBy string `json:"opened_by"`
Status TicketStatus `json:"status"`
Summary string `json:"summary"`
Updated *time.Time `json:"-"`
UpdatedBy string `json:"updated_by"`
}
// TicketEntity refers a ticket to a specific entity
type TicketEntity struct {
ID int `json:"id"`
Label string `json:"label"`
Type string `json:"type"`
URL string `json:"url"`
}
// TicketStatus constants start with Ticket and include Linode API Ticket Status values
type TicketStatus string
// TicketStatus constants reflect the current status of a Ticket
const (
TicketNew TicketStatus = "new"
TicketClosed TicketStatus = "closed"
TicketOpen TicketStatus = "open"
)
// TicketsPagedResponse represents a paginated ticket API response
type TicketsPagedResponse struct {
*PageOptions
Data []Ticket `json:"data"`
}
func (TicketsPagedResponse) endpoint(c *Client) string {
endpoint, err := c.Tickets.Endpoint()
if err != nil {
panic(err)
}
return endpoint
}
func (resp *TicketsPagedResponse) appendData(r *TicketsPagedResponse) {
resp.Data = append(resp.Data, r.Data...)
}
// ListTickets returns a collection of Support Tickets on the Account. Support Tickets
// can be both tickets opened with Linode for support, as well as tickets generated by
// Linode regarding the Account. This collection includes all Support Tickets generated
// on the Account, with open tickets returned first.
func (c *Client) ListTickets(ctx context.Context, opts *ListOptions) ([]Ticket, error) {
response := TicketsPagedResponse{}
err := c.listHelper(ctx, &response, opts)
if err != nil {
return nil, err
}
return response.Data, nil
}
// GetTicket gets a Support Ticket on the Account with the specified ID
func (c *Client) GetTicket(ctx context.Context, id int) (*Ticket, error) {
e, err := c.Tickets.Endpoint()
if err != nil {
return nil, err
}
e = fmt.Sprintf("%s/%d", e, id)
r, err := coupleAPIErrors(c.R(ctx).
SetResult(&Ticket{}).
Get(e))
if err != nil {
return nil, err
}
return r.Result().(*Ticket), nil
}

167
vendor/github.com/linode/linodego/template.go generated vendored Normal file
View file

@ -0,0 +1,167 @@
// +build ignore
package linodego
/*
- replace "Template" with "NameOfResource"
- replace "template" with "nameOfResource"
- copy template_test.go and do the same
- When updating Template structs,
- use pointers where ever null'able would have a different meaning if the wrapper
supplied "" or 0 instead
- Add "NameOfResource" to client.go, resources.go, pagination.go
*/
import (
"context"
"encoding/json"
"fmt"
)
// Template represents a Template object
type Template struct {
ID int `json:"id"`
// UpdatedStr string `json:"updated"`
// Updated *time.Time `json:"-"`
}
// TemplateCreateOptions fields are those accepted by CreateTemplate
type TemplateCreateOptions struct {
}
// TemplateUpdateOptions fields are those accepted by UpdateTemplate
type TemplateUpdateOptions struct {
}
// GetCreateOptions converts a Template to TemplateCreateOptions for use in CreateTemplate
func (i Template) GetCreateOptions() (o TemplateCreateOptions) {
// o.Label = i.Label
// o.Description = copyString(o.Description)
return
}
// GetUpdateOptions converts a Template to TemplateUpdateOptions for use in UpdateTemplate
func (i Template) GetUpdateOptions() (o TemplateUpdateOptions) {
// o.Label = i.Label
// o.Description = copyString(o.Description)
return
}
// TemplatesPagedResponse represents a paginated Template API response
type TemplatesPagedResponse struct {
*PageOptions
Data []Template `json:"data"`
}
// endpoint gets the endpoint URL for Template
func (TemplatesPagedResponse) endpoint(c *Client) string {
endpoint, err := c.Templates.Endpoint()
if err != nil {
panic(err)
}
return endpoint
}
// appendData appends Templates when processing paginated Template responses
func (resp *TemplatesPagedResponse) appendData(r *TemplatesPagedResponse) {
resp.Data = append(resp.Data, r.Data...)
}
// ListTemplates lists Templates
func (c *Client) ListTemplates(ctx context.Context, opts *ListOptions) ([]Template, error) {
response := TemplatesPagedResponse{}
err := c.listHelper(ctx, &response, opts)
for i := range response.Data {
response.Data[i].fixDates()
}
if err != nil {
return nil, err
}
return response.Data, nil
}
// fixDates converts JSON timestamps to Go time.Time values
func (i *Template) fixDates() *Template {
// i.Created, _ = parseDates(i.CreatedStr)
// i.Updated, _ = parseDates(i.UpdatedStr)
return i
}
// GetTemplate gets the template with the provided ID
func (c *Client) GetTemplate(ctx context.Context, id int) (*Template, error) {
e, err := c.Templates.Endpoint()
if err != nil {
return nil, err
}
e = fmt.Sprintf("%s/%d", e, id)
r, err := coupleAPIErrors(c.R(ctx).SetResult(&Template{}).Get(e))
if err != nil {
return nil, err
}
return r.Result().(*Template).fixDates(), nil
}
// CreateTemplate creates a Template
func (c *Client) CreateTemplate(ctx context.Context, createOpts TemplateCreateOptions) (*Template, error) {
var body string
e, err := c.Templates.Endpoint()
if err != nil {
return nil, err
}
req := c.R(ctx).SetResult(&Template{})
if bodyData, err := json.Marshal(createOpts); err == nil {
body = string(bodyData)
} else {
return nil, NewError(err)
}
r, err := coupleAPIErrors(req.
SetBody(body).
Post(e))
if err != nil {
return nil, err
}
return r.Result().(*Template).fixDates(), nil
}
// UpdateTemplate updates the Template with the specified id
func (c *Client) UpdateTemplate(ctx context.Context, id int, updateOpts TemplateUpdateOptions) (*Template, error) {
var body string
e, err := c.Templates.Endpoint()
if err != nil {
return nil, err
}
e = fmt.Sprintf("%s/%d", e, id)
req := c.R(ctx).SetResult(&Template{})
if bodyData, err := json.Marshal(updateOpts); err == nil {
body = string(bodyData)
} else {
return nil, NewError(err)
}
r, err := coupleAPIErrors(req.
SetBody(body).
Put(e))
if err != nil {
return nil, err
}
return r.Result().(*Template).fixDates(), nil
}
// DeleteTemplate deletes the Template with the specified id
func (c *Client) DeleteTemplate(ctx context.Context, id int) error {
e, err := c.Templates.Endpoint()
if err != nil {
return err
}
e = fmt.Sprintf("%s/%d", e, id)
_, err = coupleAPIErrors(c.R(ctx).Delete(e))
return err
}

89
vendor/github.com/linode/linodego/types.go generated vendored Normal file
View file

@ -0,0 +1,89 @@
package linodego
import (
"context"
"fmt"
)
// LinodeType represents a linode type object
type LinodeType struct {
ID string `json:"id"`
Disk int `json:"disk"`
Class LinodeTypeClass `json:"class"` // enum: nanode, standard, highmem
Price *LinodePrice `json:"price"`
Label string `json:"label"`
Addons *LinodeAddons `json:"addons"`
NetworkOut int `json:"network_out"`
Memory int `json:"memory"`
Transfer int `json:"transfer"`
VCPUs int `json:"vcpus"`
}
// LinodePrice represents a linode type price object
type LinodePrice struct {
Hourly float32 `json:"hourly"`
Monthly float32 `json:"monthly"`
}
// LinodeBackupsAddon represents a linode backups addon object
type LinodeBackupsAddon struct {
Price *LinodePrice `json:"price"`
}
// LinodeAddons represent the linode addons object
type LinodeAddons struct {
Backups *LinodeBackupsAddon `json:"backups"`
}
// LinodeTypeClass constants start with Class and include Linode API Instance Type Classes
type LinodeTypeClass string
// LinodeTypeClass contants are the Instance Type Classes that an Instance Type can be assigned
const (
ClassNanode LinodeTypeClass = "nanode"
ClassStandard LinodeTypeClass = "standard"
ClassHighmem LinodeTypeClass = "highmem"
)
// LinodeTypesPagedResponse represents a linode types API response for listing
type LinodeTypesPagedResponse struct {
*PageOptions
Data []LinodeType `json:"data"`
}
func (LinodeTypesPagedResponse) endpoint(c *Client) string {
endpoint, err := c.Types.Endpoint()
if err != nil {
panic(err)
}
return endpoint
}
func (resp *LinodeTypesPagedResponse) appendData(r *LinodeTypesPagedResponse) {
resp.Data = append(resp.Data, r.Data...)
}
// ListTypes lists linode types
func (c *Client) ListTypes(ctx context.Context, opts *ListOptions) ([]LinodeType, error) {
response := LinodeTypesPagedResponse{}
err := c.listHelper(ctx, &response, opts)
if err != nil {
return nil, err
}
return response.Data, nil
}
// GetType gets the type with the provided ID
func (c *Client) GetType(ctx context.Context, typeID string) (*LinodeType, error) {
e, err := c.Types.Endpoint()
if err != nil {
return nil, err
}
e = fmt.Sprintf("%s/%s", e, typeID)
r, err := coupleAPIErrors(c.Types.R(ctx).Get(e))
if err != nil {
return nil, err
}
return r.Result().(*LinodeType), nil
}

15
vendor/github.com/linode/linodego/util.go generated vendored Normal file
View file

@ -0,0 +1,15 @@
package linodego
import "time"
const (
dateLayout = "2006-01-02T15:04:05"
)
func parseDates(dateStr string) (*time.Time, error) {
d, err := time.Parse(dateLayout, dateStr)
if err != nil {
return nil, err
}
return &d, nil
}

260
vendor/github.com/linode/linodego/volumes.go generated vendored Normal file
View file

@ -0,0 +1,260 @@
package linodego
import (
"context"
"encoding/json"
"fmt"
"time"
)
// VolumeStatus indicates the status of the Volume
type VolumeStatus string
const (
// VolumeCreating indicates the Volume is being created and is not yet available for use
VolumeCreating VolumeStatus = "creating"
// VolumeActive indicates the Volume is online and available for use
VolumeActive VolumeStatus = "active"
// VolumeResizing indicates the Volume is in the process of upgrading its current capacity
VolumeResizing VolumeStatus = "resizing"
// VolumeContactSupport indicates there is a problem with the Volume. A support ticket must be opened to resolve the issue
VolumeContactSupport VolumeStatus = "contact_support"
)
// Volume represents a linode volume object
type Volume struct {
CreatedStr string `json:"created"`
UpdatedStr string `json:"updated"`
ID int `json:"id"`
Label string `json:"label"`
Status VolumeStatus `json:"status"`
Region string `json:"region"`
Size int `json:"size"`
LinodeID *int `json:"linode_id"`
FilesystemPath string `json:"filesystem_path"`
Created time.Time `json:"-"`
Updated time.Time `json:"-"`
}
// VolumeCreateOptions fields are those accepted by CreateVolume
type VolumeCreateOptions struct {
Label string `json:"label,omitempty"`
Region string `json:"region,omitempty"`
LinodeID int `json:"linode_id,omitempty"`
ConfigID int `json:"config_id,omitempty"`
// The Volume's size, in GiB. Minimum size is 10GiB, maximum size is 10240GiB. A "0" value will result in the default size.
Size int `json:"size,omitempty"`
}
// VolumeAttachOptions fields are those accepted by AttachVolume
type VolumeAttachOptions struct {
LinodeID int `json:"linode_id"`
ConfigID int `json:"config_id,omitempty"`
}
// VolumesPagedResponse represents a linode API response for listing of volumes
type VolumesPagedResponse struct {
*PageOptions
Data []Volume `json:"data"`
}
// endpoint gets the endpoint URL for Volume
func (VolumesPagedResponse) endpoint(c *Client) string {
endpoint, err := c.Volumes.Endpoint()
if err != nil {
panic(err)
}
return endpoint
}
// appendData appends Volumes when processing paginated Volume responses
func (resp *VolumesPagedResponse) appendData(r *VolumesPagedResponse) {
resp.Data = append(resp.Data, r.Data...)
}
// ListVolumes lists Volumes
func (c *Client) ListVolumes(ctx context.Context, opts *ListOptions) ([]Volume, error) {
response := VolumesPagedResponse{}
err := c.listHelper(ctx, &response, opts)
for i := range response.Data {
response.Data[i].fixDates()
}
if err != nil {
return nil, err
}
return response.Data, nil
}
// fixDates converts JSON timestamps to Go time.Time values
func (v *Volume) fixDates() *Volume {
if parsed, err := parseDates(v.CreatedStr); err != nil {
v.Created = *parsed
}
if parsed, err := parseDates(v.UpdatedStr); err != nil {
v.Updated = *parsed
}
return v
}
// GetVolume gets the template with the provided ID
func (c *Client) GetVolume(ctx context.Context, id int) (*Volume, error) {
e, err := c.Volumes.Endpoint()
if err != nil {
return nil, err
}
e = fmt.Sprintf("%s/%d", e, id)
r, err := coupleAPIErrors(c.R(ctx).SetResult(&Volume{}).Get(e))
if err != nil {
return nil, err
}
return r.Result().(*Volume).fixDates(), nil
}
// AttachVolume attaches a volume to a Linode instance
func (c *Client) AttachVolume(ctx context.Context, id int, options *VolumeAttachOptions) (*Volume, error) {
body := ""
if bodyData, err := json.Marshal(options); err == nil {
body = string(bodyData)
} else {
return nil, NewError(err)
}
e, err := c.Volumes.Endpoint()
if err != nil {
return nil, NewError(err)
}
e = fmt.Sprintf("%s/%d/attach", e, id)
resp, err := coupleAPIErrors(c.R(ctx).
SetResult(&Volume{}).
SetBody(body).
Post(e))
if err != nil {
return nil, err
}
return resp.Result().(*Volume).fixDates(), nil
}
// CreateVolume creates a Linode Volume
func (c *Client) CreateVolume(ctx context.Context, createOpts VolumeCreateOptions) (*Volume, error) {
body := ""
if bodyData, err := json.Marshal(createOpts); err == nil {
body = string(bodyData)
} else {
return nil, NewError(err)
}
e, err := c.Volumes.Endpoint()
if err != nil {
return nil, NewError(err)
}
resp, err := coupleAPIErrors(c.R(ctx).
SetResult(&Volume{}).
SetBody(body).
Post(e))
if err != nil {
return nil, err
}
return resp.Result().(*Volume).fixDates(), nil
}
// RenameVolume renames the label of a Linode volume
// There is no UpdateVolume because the label is the only alterable field.
func (c *Client) RenameVolume(ctx context.Context, id int, label string) (*Volume, error) {
body, _ := json.Marshal(map[string]string{"label": label})
e, err := c.Volumes.Endpoint()
if err != nil {
return nil, NewError(err)
}
e = fmt.Sprintf("%s/%d", e, id)
resp, err := coupleAPIErrors(c.R(ctx).
SetResult(&Volume{}).
SetBody(body).
Put(e))
if err != nil {
return nil, err
}
return resp.Result().(*Volume).fixDates(), nil
}
// CloneVolume clones a Linode volume
func (c *Client) CloneVolume(ctx context.Context, id int, label string) (*Volume, error) {
body := fmt.Sprintf("{\"label\":\"%s\"}", label)
e, err := c.Volumes.Endpoint()
if err != nil {
return nil, NewError(err)
}
e = fmt.Sprintf("%s/%d/clone", e, id)
resp, err := coupleAPIErrors(c.R(ctx).
SetResult(&Volume{}).
SetBody(body).
Post(e))
if err != nil {
return nil, err
}
return resp.Result().(*Volume).fixDates(), nil
}
// DetachVolume detaches a Linode volume
func (c *Client) DetachVolume(ctx context.Context, id int) error {
body := ""
e, err := c.Volumes.Endpoint()
if err != nil {
return NewError(err)
}
e = fmt.Sprintf("%s/%d/detach", e, id)
_, err = coupleAPIErrors(c.R(ctx).
SetBody(body).
Post(e))
return err
}
// ResizeVolume resizes an instance to new Linode type
func (c *Client) ResizeVolume(ctx context.Context, id int, size int) error {
body := fmt.Sprintf("{\"size\": %d}", size)
e, err := c.Volumes.Endpoint()
if err != nil {
return NewError(err)
}
e = fmt.Sprintf("%s/%d/resize", e, id)
_, err = coupleAPIErrors(c.R(ctx).
SetBody(body).
Post(e))
return err
}
// DeleteVolume deletes the Volume with the specified id
func (c *Client) DeleteVolume(ctx context.Context, id int) error {
e, err := c.Volumes.Endpoint()
if err != nil {
return err
}
e = fmt.Sprintf("%s/%d", e, id)
_, err = coupleAPIErrors(c.R(ctx).Delete(e))
return err
}

240
vendor/github.com/linode/linodego/waitfor.go generated vendored Normal file
View file

@ -0,0 +1,240 @@
package linodego
import (
"context"
"encoding/json"
"fmt"
"log"
"strconv"
"strings"
"time"
)
// WaitForInstanceStatus waits for the Linode instance to reach the desired state
// before returning. It will timeout with an error after timeoutSeconds.
func (client Client) WaitForInstanceStatus(ctx context.Context, instanceID int, status InstanceStatus, timeoutSeconds int) (*Instance, error) {
ctx, cancel := context.WithTimeout(ctx, time.Duration(timeoutSeconds)*time.Second)
defer cancel()
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
for {
select {
case <-ticker.C:
instance, err := client.GetInstance(ctx, instanceID)
if err != nil {
return instance, err
}
complete := (instance.Status == status)
if complete {
return instance, nil
}
case <-ctx.Done():
return nil, fmt.Errorf("Error waiting for Instance %d status %s: %s", instanceID, status, ctx.Err())
}
}
}
// WaitForVolumeStatus waits for the Volume to reach the desired state
// before returning. It will timeout with an error after timeoutSeconds.
func (client Client) WaitForVolumeStatus(ctx context.Context, volumeID int, status VolumeStatus, timeoutSeconds int) (*Volume, error) {
ctx, cancel := context.WithTimeout(ctx, time.Duration(timeoutSeconds)*time.Second)
defer cancel()
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
for {
select {
case <-ticker.C:
volume, err := client.GetVolume(ctx, volumeID)
if err != nil {
return volume, err
}
complete := (volume.Status == status)
if complete {
return volume, nil
}
case <-ctx.Done():
return nil, fmt.Errorf("Error waiting for Volume %d status %s: %s", volumeID, status, ctx.Err())
}
}
}
// WaitForSnapshotStatus waits for the Snapshot to reach the desired state
// before returning. It will timeout with an error after timeoutSeconds.
func (client Client) WaitForSnapshotStatus(ctx context.Context, instanceID int, snapshotID int, status InstanceSnapshotStatus, timeoutSeconds int) (*InstanceSnapshot, error) {
ctx, cancel := context.WithTimeout(ctx, time.Duration(timeoutSeconds)*time.Second)
defer cancel()
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
for {
select {
case <-ticker.C:
snapshot, err := client.GetInstanceSnapshot(ctx, instanceID, snapshotID)
if err != nil {
return snapshot, err
}
complete := (snapshot.Status == status)
if complete {
return snapshot, nil
}
case <-ctx.Done():
return nil, fmt.Errorf("Error waiting for Instance %d Snapshot %d status %s: %s", instanceID, snapshotID, status, ctx.Err())
}
}
}
// WaitForVolumeLinodeID waits for the Volume to match the desired LinodeID
// before returning. An active Instance will not immediately attach or detach a volume, so the
// the LinodeID must be polled to determine volume readiness from the API.
// WaitForVolumeLinodeID will timeout with an error after timeoutSeconds.
func (client Client) WaitForVolumeLinodeID(ctx context.Context, volumeID int, linodeID *int, timeoutSeconds int) (*Volume, error) {
ctx, cancel := context.WithTimeout(ctx, time.Duration(timeoutSeconds)*time.Second)
defer cancel()
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
for {
select {
case <-ticker.C:
volume, err := client.GetVolume(ctx, volumeID)
if err != nil {
return volume, err
}
if linodeID == nil && volume.LinodeID == nil {
return volume, nil
} else if linodeID == nil || volume.LinodeID == nil {
// continue waiting
} else if *volume.LinodeID == *linodeID {
return volume, nil
}
case <-ctx.Done():
return nil, fmt.Errorf("Error waiting for Volume %d to have Instance %v: %s", volumeID, linodeID, ctx.Err())
}
}
}
// WaitForEventFinished waits for an entity action to reach the 'finished' state
// before returning. It will timeout with an error after timeoutSeconds.
// If the event indicates a failure both the failed event and the error will be returned.
func (client Client) WaitForEventFinished(ctx context.Context, id interface{}, entityType EntityType, action EventAction, minStart time.Time, timeoutSeconds int) (*Event, error) {
titledEntityType := strings.Title(string(entityType))
filter, _ := json.Marshal(map[string]interface{}{
// Entity is not filtered by the API
// Perhaps one day they will permit Entity ID/Type filtering.
// We'll have to verify these values manually, for now.
//"entity": map[string]interface{}{
// "id": fmt.Sprintf("%v", id),
// "type": entityType,
//},
// Nor is action
//"action": action,
// Created is not correctly filtered by the API
// We'll have to verify these values manually, for now.
//"created": map[string]interface{}{
// "+gte": minStart.Format(time.RFC3339),
//},
// With potentially 1000+ events coming back, we should filter on something
"seen": false,
// Float the latest events to page 1
"+order_by": "created",
"+order": "desc",
})
// Optimistically restrict results to page 1. We should remove this when more
// precise filtering options exist.
listOptions := NewListOptions(1, string(filter))
ctx, cancel := context.WithTimeout(ctx, time.Duration(timeoutSeconds)*time.Second)
defer cancel()
if deadline, ok := ctx.Deadline(); ok {
duration := time.Until(deadline)
log.Printf("[INFO] Waiting %d seconds for %s events since %v for %s %v", int(duration.Seconds()), action, minStart, titledEntityType, id)
}
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
for {
select {
case <-ticker.C:
events, err := client.ListEvents(ctx, listOptions)
if err != nil {
return nil, err
}
// If there are events for this instance + action, inspect them
for _, event := range events {
if event.Action != action {
// log.Println("action mismatch", event.Action, action)
continue
}
if event.Entity.Type != entityType {
// log.Println("type mismatch", event.Entity.Type, entityType)
continue
}
var entID string
switch event.Entity.ID.(type) {
case float64, float32:
entID = fmt.Sprintf("%.f", event.Entity.ID)
case int:
entID = strconv.Itoa(event.Entity.ID.(int))
default:
entID = fmt.Sprintf("%v", event.Entity.ID)
}
var findID string
switch id.(type) {
case float64, float32:
findID = fmt.Sprintf("%.f", id)
case int:
findID = strconv.Itoa(id.(int))
default:
findID = fmt.Sprintf("%v", id)
}
if entID != findID {
// log.Println("id mismatch", entID, findID)
continue
}
// @TODO(displague) This event.Created check shouldn't be needed, but it appears
// that the ListEvents method is not populating it correctly
if event.Created == nil {
log.Printf("[WARN] event.Created is nil when API returned: %#+v", event.CreatedStr)
} else if *event.Created != minStart && !event.Created.After(minStart) {
// Not the event we were looking for
// log.Println(event.Created, "is not >=", minStart)
continue
}
if event.Status == EventFailed {
return &event, fmt.Errorf("%s %v action %s failed", titledEntityType, id, action)
} else if event.Status == EventScheduled {
log.Printf("[INFO] %s %v action %s is scheduled", titledEntityType, id, action)
} else if event.Status == EventFinished {
log.Printf("[INFO] %s %v action %s is finished", titledEntityType, id, action)
return &event, nil
}
// TODO(displague) can we bump the ticker to TimeRemaining/2 (>=1) when non-nil?
log.Printf("[INFO] %s %v action %s is %s", titledEntityType, id, action, event.Status)
}
case <-ctx.Done():
return nil, fmt.Errorf("Error waiting for Event Status '%s' of %s %v action '%s': %s", EventFinished, titledEntityType, id, action, ctx.Err())
}
}
}