forked from linode/linode_api4-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstackscript.py
More file actions
64 lines (55 loc) · 2.14 KB
/
Copy pathstackscript.py
File metadata and controls
64 lines (55 loc) · 2.14 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
from .base import Base, Property
from .distribution import Distribution
from enum import Enum
class UserDefinedFieldType(Enum):
text = 1
select_one = 2
select_many = 3
class UserDefinedField():
def __init__(self, name, label, example, field_type, choices=None):
self.name = name
self.label = label
self.example = example
self.field_type = field_type
self.choices = choices
def __repr__(self):
return "{}({}): {}".format(self.label, self.field_type.name, self.example)
class StackScript(Base):
api_name = 'stackscripts'
api_endpoint = '/stackscripts/{id}'
properties = {
"user_defined_fields": Property(),
"label": Property(mutable=True, filterable=True),
"customer_id": Property(),
"rev_note": Property(mutable=True),
"user_id": Property(),
"is_public": Property(mutable=True, filterable=True),
"created": Property(is_datetime=True),
"deployments_active": Property(),
"script": Property(mutable=True),
"distributions": Property(relationship=Distribution, mutable=True, filterable=True),
"deployments_total": Property(),
"description": Property(mutable=True, filterable=True),
"updated": Property(is_datetime=True),
}
def _populate(self, json):
"""
Override the populate method to map user_defined_fields to
fancy values
"""
Base._populate(self, json)
mapped_udfs = []
for udf in self.user_defined_fields:
t = UserDefinedFieldType.text
choices = None
if hasattr(udf, 'oneof'):
t = UserDefinedFieldType.select_one
choices = udf.oneof.split(',')
elif hasattr(udf, 'manyof'):
t = UserDefinedFieldType.select_many
choices = udf.manyof.split(',')
mapped_udfs.append(UserDefinedField(udf.name, udf.label, udf.example, t, \
choices=choices))
self._set('user_defined_fields', mapped_udfs)
for d in self.distributions:
d._set("_populated", False) # these come in as partials