-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathaccount.py
More file actions
512 lines (388 loc) · 16.8 KB
/
account.py
File metadata and controls
512 lines (388 loc) · 16.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
from typing import Union
from linode_api4.errors import UnexpectedResponseError
from linode_api4.groups import Group
from linode_api4.objects import (
Account,
AccountAvailability,
AccountBetaProgram,
AccountSettings,
BetaProgram,
ChildAccount,
Event,
Invoice,
Login,
MappedObject,
OAuthClient,
Payment,
PaymentMethod,
ServiceTransfer,
User,
)
class AccountGroup(Group):
"""
Collections related to your account.
"""
def __call__(self):
"""
Retrieves information about the acting user's account, such as billing
information. This is intended to be called off of the :any:`LinodeClient`
class, like this::
account = client.account()
API Documentation: https://techdocs.akamai.com/linode-api/reference/get-account
:returns: Returns the acting user's account information.
:rtype: Account
"""
result = self.client.get("/account")
if not "email" in result:
raise UnexpectedResponseError(
"Unexpected response when getting account!", json=result
)
return Account(self.client, result["email"], result)
def events(self, *filters):
"""
Lists events on the current account matching the given filters.
API Documentation: https://techdocs.akamai.com/linode-api/reference/get-events
:param filters: Any number of filters to apply to this query.
See :doc:`Filtering Collections</linode_api4/objects/filtering>`
for more details on filtering.
:returns: A list of events on the current account matching the given filters.
:rtype: PaginatedList of Event
"""
return self.client._get_and_filter(Event, *filters)
def events_mark_seen(self, event):
"""
Marks event as the last event we have seen. If event is an int, it is treated
as an event_id, otherwise it should be an event object whose id will be used.
API Documentation: https://techdocs.akamai.com/linode-api/reference/post-event-seen
:param event: The Linode event to mark as seen.
:type event: Event or int
"""
last_seen = event if isinstance(event, int) else event.id
self.client.post(
"{}/seen".format(Event.api_endpoint),
model=Event(self.client, last_seen),
)
def settings(self):
"""
Returns the account settings data for this acocunt. This is not a
listing endpoint.
API Documentation: https://techdocs.akamai.com/linode-api/reference/get-account-settings
:returns: The account settings data for this account.
:rtype: AccountSettings
"""
result = self.client.get("/account/settings")
if not "managed" in result:
raise UnexpectedResponseError(
"Unexpected response when getting account settings!",
json=result,
)
s = AccountSettings(self.client, result["managed"], result)
return s
def invoices(self, *filters):
"""
Returns Invoices issued to this account.
API Documentation: https://techdocs.akamai.com/linode-api/reference/get-invoices
:param filters: Any number of filters to apply to this query.
:returns: Invoices issued to this account.
:rtype: PaginatedList of Invoice
"""
return self.client._get_and_filter(Invoice, *filters)
def payments(self, *filters):
"""
Returns a list of Payments made on this account.
API Documentation: https://techdocs.akamai.com/linode-api/reference/get-payments
:returns: A list of payments made on this account.
:rtype: PaginatedList of Payment
"""
return self.client._get_and_filter(Payment, *filters)
def oauth_clients(self, *filters):
"""
Returns the OAuth Clients associated with this account.
API Documentation: https://techdocs.akamai.com/linode-api/reference/get-clients
:param filters: Any number of filters to apply to this query.
See :doc:`Filtering Collections</linode_api4/objects/filtering>`
for more details on filtering.
:returns: A list of OAuth Clients associated with this account.
:rtype: PaginatedList of OAuthClient
"""
return self.client._get_and_filter(OAuthClient, *filters)
def oauth_client_create(self, name, redirect_uri, **kwargs):
"""
Creates a new OAuth client.
API Documentation: https://techdocs.akamai.com/linode-api/reference/post-client
:param name: The name of this application.
:type name: str
:param redirect_uri: The location a successful log in from https://login.linode.com should be redirected to for this client.
:type redirect_uri: str
:returns: The created OAuth Client.
:rtype: OAuthClient
"""
params = {
"label": name,
"redirect_uri": redirect_uri,
}
params.update(kwargs)
result = self.client.post("/account/oauth-clients", data=params)
if not "id" in result:
raise UnexpectedResponseError(
"Unexpected response when creating OAuth Client!", json=result
)
c = OAuthClient(self.client, result["id"], result)
return c
def users(self, *filters):
"""
Returns a list of users on this account.
API Documentation: https://techdocs.akamai.com/linode-api/reference/get-users
:param filters: Any number of filters to apply to this query.
See :doc:`Filtering Collections</linode_api4/objects/filtering>`
for more details on filtering.
:returns: A list of users on this account.
:rtype: PaginatedList of User
"""
return self.client._get_and_filter(User, *filters)
def logins(self):
"""
Returns a collection of successful logins for all users on the account during the last 90 days.
API Documentation: https://techdocs.akamai.com/linode-api/reference/get-account-logins
:returns: A list of Logins on this account.
:rtype: PaginatedList of Login
"""
return self.client._get_and_filter(Login)
def maintenance(self):
"""
Returns a collection of Maintenance objects for any entity a user has permissions to view. Cancelled Maintenance objects are not returned.
API Documentation: https://techdocs.akamai.com/linode-api/reference/get-account-logins
:returns: A list of Maintenance objects on this account.
:rtype: List of Maintenance objects as MappedObjects
"""
result = self.client.get(
"{}/maintenance".format(Account.api_endpoint), model=self
)
return [MappedObject(**r) for r in result["data"]]
def payment_methods(self):
"""
Returns a list of Payment Methods for this Account.
API Documentation: https://techdocs.akamai.com/linode-api/reference/get-payment-methods
:returns: A list of Payment Methods on this account.
:rtype: PaginatedList of PaymentMethod
"""
return self.client._get_and_filter(PaymentMethod)
def add_payment_method(self, data, is_default, type):
"""
Adds a Payment Method to your Account with the option to set it as the default method.
API Documentation: https://techdocs.akamai.com/linode-api/reference/post-payment-method
:param data: An object representing the credit card information you have on file with
Linode to make Payments against your Account.
:type data: dict
Example usage::
data = {
"card_number": "4111111111111111",
"expiry_month": 11,
"expiry_year": 2020,
"cvv": "111"
}
:param is_default: Whether this Payment Method is the default method for
automatically processing service charges.
:type is_default: bool
:param type: The type of Payment Method. Enum: ["credit_card]
:type type: str
"""
if type != "credit_card":
raise ValueError("Unknown Payment Method type: {}".format(type))
if (
"card_number" not in data
or "expiry_month" not in data
or "expiry_year" not in data
or "cvv" not in data
or not data
):
raise ValueError("Invalid credit card info provided")
params = {"data": data, "type": type, "is_default": is_default}
resp = self.client.post(
"{}/payment-methods".format(Account.api_endpoint),
model=self,
data=params,
)
if "error" in resp:
raise UnexpectedResponseError(
"Unexpected response when adding payment method!",
json=resp,
)
def notifications(self):
"""
Returns a collection of Notification objects representing important, often time-sensitive items related to your Account.
API Documentation: https://techdocs.akamai.com/linode-api/reference/get-notifications
:returns: A list of Notifications on this account.
:rtype: List of Notification objects as MappedObjects
"""
result = self.client.get(
"{}/notifications".format(Account.api_endpoint), model=self
)
return [MappedObject(**r) for r in result["data"]]
def linode_managed_enable(self):
"""
Enables Linode Managed for the entire account and sends a welcome email to the account’s associated email address.
API Documentation: https://techdocs.akamai.com/linode-api/reference/post-enable-account-managed
"""
resp = self.client.post(
"{}/settings/managed-enable".format(Account.api_endpoint),
model=self,
)
if "error" in resp:
raise UnexpectedResponseError(
"Unexpected response when enabling Linode Managed!",
json=resp,
)
def add_promo_code(self, promo_code):
"""
Adds an expiring Promo Credit to your account.
API Documentation: https://techdocs.akamai.com/linode-api/reference/post-promo-credit
:param promo_code: The Promo Code.
:type promo_code: str
"""
params = {
"promo_code": promo_code,
}
resp = self.client.post(
"{}/promo-codes".format(Account.api_endpoint),
model=self,
data=params,
)
if "error" in resp:
raise UnexpectedResponseError(
"Unexpected response when adding Promo Code!",
json=resp,
)
def service_transfers(self, *filters):
"""
Returns a collection of all created and accepted Service Transfers for this account, regardless of the user that created or accepted the transfer.
API Documentation: https://techdocs.akamai.com/linode-api/reference/get-service-transfers
:returns: A list of Service Transfers on this account.
:rtype: PaginatedList of ServiceTransfer
"""
return self.client._get_and_filter(ServiceTransfer, *filters)
def service_transfer_create(self, entities):
"""
Creates a transfer request for the specified services.
API Documentation: https://techdocs.akamai.com/linode-api/reference/post-service-transfer
:param entities: A collection of the services to include in this transfer request, separated by type.
:type entities: dict
Example usage::
entities = {
"linodes": [
111,
222
]
}
"""
if not entities:
raise ValueError("Entities must be provided!")
bad_entries = [
k for k, v in entities.items() if not isinstance(v, list)
]
if len(bad_entries) > 0:
raise ValueError(
f"Got unexpected type for entity lists: {', '.join(bad_entries)}"
)
params = {"entities": entities}
resp = self.client.post(
"{}/service-transfers".format(Account.api_endpoint),
model=self,
data=params,
)
if "error" in resp:
raise UnexpectedResponseError(
"Unexpected response when creating Service Transfer!",
json=resp,
)
def transfer(self):
"""
Returns a MappedObject containing the account's transfer pool data.
API Documentation: https://techdocs.akamai.com/linode-api/reference/get-transfer
:returns: Information about this account's transfer pool data.
:rtype: MappedObject
"""
result = self.client.get("/account/transfer")
if not "used" in result:
raise UnexpectedResponseError(
"Unexpected response when getting Transfer Pool!"
)
return MappedObject(**result)
def user_create(self, email, username, restricted=True):
"""
Creates a new user on your account. If you create an unrestricted user,
they will immediately be able to access everything on your account. If
you create a restricted user, you must grant them access to parts of your
account that you want to allow them to manage (see :any:`User.grants` for
details).
The new user will receive an email inviting them to set up their password.
This must be completed before they can log in.
API Documentation: https://techdocs.akamai.com/linode-api/reference/post-user
:param email: The new user's email address. This is used to finish setting
up their user account.
:type email: str
:param username: The new user's unique username. They will use this username
to log in.
:type username: str
:param restricted: If True, the new user must be granted access to parts of
the account before they can do anything. If False, the
new user will immediately be able to manage the entire
account. Defaults to True.
:type restricted: True
:returns The new User.
:rtype: User
"""
params = {
"email": email,
"username": username,
"restricted": restricted,
}
result = self.client.post("/account/users", data=params)
if not all(
[c in result for c in ("email", "restricted", "username")]
): # pylint: disable=use-a-generator
raise UnexpectedResponseError(
"Unexpected response when creating user!", json=result
)
u = User(self.client, result["username"], result)
return u
def enrolled_betas(self, *filters):
"""
Returns a list of all Beta Programs an account is enrolled in.
API doc: https://techdocs.akamai.com/linode-api/reference/get-enrolled-beta-programs
:returns: a list of Beta Programs.
:rtype: PaginatedList of AccountBetaProgram
"""
return self.client._get_and_filter(AccountBetaProgram, *filters)
def join_beta_program(self, beta: Union[str, BetaProgram]):
"""
Enrolls an account into a beta program.
API doc: https://techdocs.akamai.com/linode-api/reference/post-beta-program
:param beta: The object or id of a beta program to join.
:type beta: BetaProgram or str
:returns: A boolean indicating whether the account joined a beta program successfully.
:rtype: bool
"""
self.client.post(
"/account/betas",
data={"id": beta.id if isinstance(beta, BetaProgram) else beta},
)
return True
def availabilities(self, *filters):
"""
Returns a list of all available regions and the resource types which are available
to the account.
API doc: https://techdocs.akamai.com/linode-api/reference/get-account-availability
:returns: a list of region availability information.
:rtype: PaginatedList of AccountAvailability
"""
return self.client._get_and_filter(AccountAvailability, *filters)
def child_accounts(self, *filters):
"""
Returns a list of all child accounts under the this parent account.
NOTE: Parent/Child related features may not be generally available.
API Documentation: https://techdocs.akamai.com/linode-api/reference/get-child-accounts
:returns: a list of all child accounts.
:rtype: PaginatedList of ChildAccount
"""
return self.client._get_and_filter(ChildAccount, *filters)