generated from ansible-collections/collection_template
-
Notifications
You must be signed in to change notification settings - Fork 35
/
models.py
229 lines (183 loc) · 6.92 KB
/
models.py
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
from typing import Any, Dict, List, Union, Optional
from dataclasses import dataclass
from ansible_collections.cloud.terraform.plugins.module_utils.types import AnyJsonType, TJsonObject
@dataclass
class TerraformWorkspaceContext:
current: str
all: List[str]
@dataclass
class TerraformOutput:
sensitive: bool
value: Any
# as string is shown "string"
# a list of strings is shown as ["list", "string"]
type: Union[str, List[str]]
@classmethod
def from_json(cls, json: TJsonObject) -> "TerraformOutput":
return cls(sensitive=json.get("sensitive"), value=json.get("value"), type=json.get("type"))
@dataclass
class TerraformModuleResource:
address: str
mode: str
type: str
name: str
provider_name: str
schema_version: int
values: Dict[str, AnyJsonType]
# potentially undefined
sensitive_values: Dict[str, Union[bool, List[bool], Dict[str, bool]]]
depends_on: List[str]
@classmethod
def from_json(cls, json: TJsonObject) -> "TerraformModuleResource":
return cls(
address=json["address"],
mode=json["mode"],
type=json["type"],
name=json["name"],
provider_name=json["provider_name"],
schema_version=json["schema_version"],
values=json["values"],
sensitive_values=json.get("sensitive_values", {}),
depends_on=json.get("depends_on", []),
)
@dataclass
class TerraformRootModuleResource(TerraformModuleResource):
pass
@dataclass
class TerraformChildModuleResource(TerraformModuleResource):
pass
@dataclass
class TerraformAnsibleProvider:
name: str
groups: List[str]
children: List[str]
variables: Dict[str, str]
@classmethod
def from_json(cls, json: TerraformModuleResource) -> "TerraformAnsibleProvider":
return cls(
name=json.values.get("name", None),
groups=json.values.get("groups", []),
children=json.values.get("children", []),
variables=json.values.get("variables", {}),
)
@dataclass
class TerraformChildModule:
resources: List[TerraformModuleResource]
@classmethod
def from_json(cls, json: TJsonObject) -> "TerraformChildModule":
return cls(resources=[TerraformChildModuleResource.from_json(r) for r in json.get("resources", [])])
@dataclass
class TerraformRootModule:
resources: List[TerraformModuleResource]
child_modules: List[TerraformChildModule]
@classmethod
def from_json(cls, json: TJsonObject) -> "TerraformRootModule":
return cls(
resources=[TerraformRootModuleResource.from_json(r) for r in json.get("resources", [])],
child_modules=[TerraformChildModule.from_json(r) for r in json.get("child_modules", [])],
)
@dataclass
class TerraformShowValues:
outputs: Dict[str, TerraformOutput]
root_module: TerraformRootModule
@classmethod
def from_json(cls, json: TJsonObject) -> "TerraformShowValues":
return cls(
outputs={
output_name: TerraformOutput.from_json(output_value)
for output_name, output_value in json.get("outputs", {}).items()
},
root_module=TerraformRootModule.from_json(json["root_module"]),
)
@dataclass
class TerraformShow:
format_version: str
terraform_version: str
values: TerraformShowValues
@classmethod
def from_json(cls, json: TJsonObject) -> "TerraformShow":
return cls(
format_version=json["format_version"],
terraform_version=json["terraform_version"],
values=TerraformShowValues.from_json(json["values"]),
)
@dataclass
class TerraformAttributeSpec:
type: Union[str, List[str]]
description_kind: str
# potentially undefined
description: Optional[str]
optional: bool
required: bool
deprecated: bool
sensitive: bool
computed: bool
@classmethod
def from_json(cls, json: TJsonObject) -> "TerraformAttributeSpec":
return cls(
type=json["type"],
description_kind=json["description_kind"],
description=json.get("description"),
optional=json.get("optional", False),
required=json.get("required", False),
deprecated=json.get("deprecated", False),
sensitive=json.get("sensitive", False),
computed=json.get("computed", False),
)
@dataclass
class TerraformBlockSensitive:
sensitive: bool
@classmethod
def create(cls, block_sensitive: bool) -> "TerraformBlockSensitive":
return cls(sensitive=block_sensitive)
@dataclass
class TerraformResourceSchema:
version: int
# this de-nests the "block" and the "block_type" subelement
# but "block_type" is simplified - contains only sensitive attribute
attributes: Dict[str, Union[TerraformAttributeSpec, TerraformBlockSensitive]]
@classmethod
def from_json(cls, json: TJsonObject) -> "TerraformResourceSchema":
attributes: Dict[str, Union[TerraformAttributeSpec, TerraformBlockSensitive]] = {
attr_name: TerraformAttributeSpec.from_json(attr_value)
for attr_name, attr_value in json.get("block", {}).get("attributes", {}).items()
}
for block_name, block_value in json.get("block", {}).get("block_types", {}).items():
block_attributes = {
block_attr_name: TerraformAttributeSpec.from_json(block_attr_value)
for block_attr_name, block_attr_value in block_value.get("block", {}).get("attributes", {}).items()
}
sensitive_list = [
block_attribute_value.sensitive
for block_attribute_name, block_attribute_value in block_attributes.items()
]
# if one attribute is sensitive, we make the whole block sensitive
attributes[block_name] = TerraformBlockSensitive.create(any(sensitive_list))
return cls(
version=json["version"],
attributes=attributes,
)
@dataclass
class TerraformProviderSchema:
resource_schemas: Dict[str, TerraformResourceSchema]
@classmethod
def from_json(cls, json: TJsonObject) -> "TerraformProviderSchema":
return cls(
resource_schemas={
schema_name: TerraformResourceSchema.from_json(schema_value)
for schema_name, schema_value in json.get("resource_schemas", {}).items()
}
)
@dataclass
class TerraformProviderSchemaCollection:
format_version: str
provider_schemas: Dict[str, TerraformProviderSchema]
@classmethod
def from_json(cls, json: TJsonObject) -> "TerraformProviderSchemaCollection":
return cls(
format_version=json["format_version"],
provider_schemas={
schema_name: TerraformProviderSchema.from_json(schema_value)
for schema_name, schema_value in json.get("provider_schemas", {}).items()
},
)