-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathclient.go
176 lines (159 loc) · 8.16 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package linodego
import (
"fmt"
"log"
"net/http"
"os"
"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 = "1.0.0"
// APIEnvVar environment var to check for API token
APIEnvVar = "LINODE_TOKEN"
)
var userAgent = fmt.Sprintf("linodego %s https://github.com/chiefy/linodego", Version)
// Client is a wrapper around the Resty client
type Client struct {
apiToken string
resty *resty.Client
resources map[string]*Resource
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
Tickets *Resource
Account *Resource
Invoices *Resource
InvoiceItems *Resource
Events *Resource
Notifications *Resource
Profile *Resource
Managed *Resource
}
// SetUserAgent sets a custom user-agent for HTTP requests
func (c *Client) SetUserAgent(ua string) *Client {
userAgent = ua
return c
}
// R wraps resty's R method
func (c *Client) R() *resty.Request {
return c.resty.R().
SetError(APIError{})
}
// SetDebug sets the debug on resty's client
func (c *Client) SetDebug(debug bool) *Client {
c.resty.SetDebug(debug)
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(codeAPIToken *string, transport http.RoundTripper) (client Client) {
linodeAPIToken := ""
if codeAPIToken != nil {
linodeAPIToken = *codeAPIToken
} else if envAPIToken, ok := os.LookupEnv(APIEnvVar); ok {
linodeAPIToken = envAPIToken
}
if len(linodeAPIToken) == 0 || linodeAPIToken == "" {
log.Print("Could not find LINODE_TOKEN, authenticated endpoints will fail.")
}
restyClient := resty.New().
SetHostURL(fmt.Sprintf("%s://%s/%s", APIProto, APIHost, APIVersion)).
SetAuthToken(linodeAPIToken).
SetTransport(transport).
SetHeader("User-Agent", userAgent)
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{}, InstanceSnapshotsPagedResponse{}),
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{}),
ticketsName: NewResource(&client, ticketsName, ticketsEndpoint, false, Ticket{}, TicketsPagedResponse{}),
accountName: NewResource(&client, accountName, accountEndpoint, false, Account{}, nil), // really?
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.apiToken = linodeAPIToken
client.resty = restyClient
client.resources = resources
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.Longview = resources[longviewName]
client.LongviewSubscriptions = resources[longviewsubscriptionsName]
client.NodeBalancers = resources[nodebalancersName]
client.NodeBalancerConfigs = resources[nodebalancerconfigsName]
client.NodeBalancerNodes = resources[nodebalancernodesName]
client.Tickets = resources[ticketsName]
client.Account = resources[accountName]
client.Invoices = resources[invoicesName]
client.Profile = resources[profileName]
client.Managed = resources[managedName]
return
}