Repository navigation
Expand file tree
/
Copy pathiface.py
More file actions
230 lines (163 loc) · 7.5 KB
/
Copy pathiface.py
File metadata and controls
230 lines (163 loc) · 7.5 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
from abc import abstractmethod
from dataclasses import dataclass
from typing import TYPE_CHECKING, Optional, Protocol, Sequence, Type, TypeAlias
from tf.schema import Attribute, NestedBlock, Schema
from tf.utils import Diagnostics
if TYPE_CHECKING: # pragma: no cover
from tf.function import Function
State: TypeAlias = dict
"""
State is the current state of a resource.
It is a dictionary where field names are mapped to Python values (or None, or Unknown).
Resource operations are mostly just pushing around, mutating, and returning State.
"""
Config: TypeAlias = dict
"""
Config is like State, except its used in configuration validation and the values are null when
they are not bound to a value.
This is because the configuration is not yet bound to a resource.
This is merely for validating that set of input parameters or values are correct.
"""
class AbstractResource(Protocol):
@classmethod
@abstractmethod
def get_name(cls) -> str:
"""Get the type name for this resource type"""
@classmethod
@abstractmethod
def get_schema(cls) -> Schema:
"""Get the schema for this resource"""
@dataclass
class _Context:
diagnostics: Diagnostics
type_name: str
class ReadDataContext(_Context): ...
def _validate_config(
diags: Diagnostics, type_name: str, config: Config, attributes: Sequence[Attribute], blocks: Sequence[NestedBlock]
):
"""Validate the configuration"""
# Verify each supplied field is valid
attr_names = {a.name for a in attributes}
block_names = {b.type_name for b in blocks}
known_keys = attr_names | block_names
unknown_keys = set(config.keys()) - known_keys
# Kind of lazy about this right now... might need schema version promotion logic later
for k in unknown_keys:
diags.add_error(
f"Unknown field {type_name}.{k}",
f"The field '{k}' was supplied, but is not a valid field for {type_name}."
f" This is likely a bug in your state file."
f" If you did not manually edit state, please report this to your provider.",
)
if len(unknown_keys) > 0:
return
amap = {a.name: a for a in attributes}
for k in attr_names:
if k not in config:
# If the field is not set, we don't need to validate it -- makes unit tests easy
continue
v = config[k]
a = amap[k]
if v is not None and a.computed and not a.optional and not a.required:
diags.add_error(
f"Field {type_name}.{k} is read-only and should not be set",
path=[k],
)
class DataSource(AbstractResource, Protocol):
def validate(self, diags: Diagnostics, type_name: str, config: Config):
"""Validate the data source configuration"""
schema = self.get_schema()
_validate_config(diags, type_name, config, schema.attributes, schema.block_types)
@abstractmethod
def read(self, ctx: ReadDataContext, config: Config) -> Optional[State]:
"""Read the data source"""
# Right now all the same but UpdateContext should have a planning/delta fields method
class CreateContext(_Context): ...
class ReadContext(_Context): ...
class UpdateContext(_Context): ...
class DeleteContext(_Context): ...
class UpgradeContext(_Context): ...
class ImportContext(_Context): ...
@dataclass
class PlanContext(_Context):
changed_fields: set[str]
class Resource(AbstractResource, Protocol):
def validate(self, diags: Diagnostics, type_name: str, config: Config):
"""
Validate the resource configuration
This is called before any other operation to validate the configuration of the resource.
You should run parameter validation here.
Generate errors and warnings through the `diags` object.
:param diags: Diagnostics
:param type_name: The type name of the resource
:param config: The configuration to validate
"""
schema = self.get_schema()
_validate_config(diags, type_name, config, schema.attributes, schema.block_types)
@abstractmethod
def create(self, ctx: CreateContext, planned: State) -> Optional[State]:
"""
Create the resource, returning the actual state after creation.
This is called when a user runs `opentofu apply` and the resource needs to be initially created.
:param ctx: CreateContext
:param planned: The planned state of the resource
"""
@abstractmethod
def read(self, ctx: ReadContext, current: State) -> Optional[State]:
"""Read the current state of the resource"""
@abstractmethod
def update(self, ctx: UpdateContext, current: State, planned: State) -> Optional[State]:
"""Update the resource to the planned state, returning the actual state after the update"""
@abstractmethod
def delete(self, ctx: DeleteContext, current: State):
"""Delete the resource, returning None generally"""
def plan(self, ctx: PlanContext, current: Optional[State], planned: State) -> Optional[State]:
"""Modify the resource change plan"""
return planned
def import_(self, ctx: ImportContext, id: str) -> Optional[State]:
"""
Import a resource
This is called when a user runs `opentofu import` and provides a resource ID to import.
:param ctx: ImportContext
:param id: The resource ID to import
"""
# TODO(Hunter): move
def upgrade(self, ctx: UpgradeContext, version: int, old: State) -> Optional[State]:
"""Upgrade an old resource state to the newest schema version"""
ctx.diagnostics.add_warning(f"Using default upgrade for {ctx.type_name}.")
# TODO(Hunter): This should probably iterate all attrs, find ones not in the old state, and set defaults
return old
def is_importable(klass: Type[Resource]) -> bool:
"""Has the resource implemented the import_ method"""
return hasattr(klass, "import_") and klass.import_ is not Resource.import_
class Provider(Protocol):
@abstractmethod
def get_model_prefix(self) -> str:
"""Get the model prefix for all loaded resources"""
@abstractmethod
def get_provider_schema(self, diags: Diagnostics) -> Schema:
"""Get the schema for the provider"""
@abstractmethod
def full_name(self) -> str:
"""Get the full provider name eg `terraform.example.com/ex/ex`"""
@abstractmethod
def validate_config(self, diags: Diagnostics, config: Config):
"""Validate the provider configuration"""
@abstractmethod
def configure_provider(self, diags: Diagnostics, config: Config):
"""Called when the provider is configured. This is a good place to set up any global state"""
@abstractmethod
def get_data_sources(self) -> list[Type[DataSource]]:
"""Get all the data source types that this provider supports"""
@abstractmethod
def get_resources(self) -> list[Type[Resource]]:
"""Get all the resource types that this provider supports"""
def get_functions(self) -> list[Type["Function"]]:
"""Get all the function types that this provider supports"""
return []
def new_resource(self, klass: Type[Resource]) -> Resource:
return klass(self) # pyre-ignore[19]: noqa: Don't care about __init__
def new_data_source(self, klass: Type[DataSource]) -> DataSource:
return klass(self) # pyre-ignore[19]: noqa: Don't care about __init__
def new_function(self, klass: Type["Function"]) -> "Function":
return klass(self) # pyre-ignore[19]: noqa: Don't care about __init__