-
Notifications
You must be signed in to change notification settings - Fork 180
Expand file tree
/
Copy path_resources.py
More file actions
337 lines (275 loc) · 11.2 KB
/
Copy path_resources.py
File metadata and controls
337 lines (275 loc) · 11.2 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
from typing import TYPE_CHECKING, Optional
from databricks.bundles.core._diagnostics import Diagnostics
from databricks.bundles.core._location import Location
from databricks.bundles.core._resource import Resource
from databricks.bundles.core._transform import _transform
if TYPE_CHECKING:
from databricks.bundles.jobs._models.job import Job, JobParam
from databricks.bundles.pipelines._models.pipeline import Pipeline, PipelineParam
from databricks.bundles.schemas._models.schema import Schema, SchemaParam
from databricks.bundles.volumes._models.volume import Volume, VolumeParam
__all__ = ["Resources"]
class Resources:
"""
Resources is a collection of resources in a bundle.
Resources class is returned by 'load_resources' function specified in databricks.yml. Each element in
'python/resources' list is a fully qualified function name that returns an instance of Resources class.
If there are multiple functions specified in 'python/resources' list, the resources from all functions
are combined into a single Resources object.
Example:
.. code-block:: yaml
python:
resources:
- "resources:load_resources"
`load_resources` function can be implemented using built-in functions:
- :meth:`load_resources_from_current_package_module`
- :meth:`load_resources_from_package_module`
- :meth:`load_resources_from_modules`
- :meth:`load_resources_from_module`
Programmatic construction of resources is supported using :meth:`add_resource` and :meth:`add_job` methods.
Example:
.. code-block:: python
def load_resources(bundle: Bundle) -> Resources:
resources = Resources()
for resource_name, config in get_configs():
job = create_job(config)
resources.add_job(resource_name, job)
return resources
"""
def __init__(self):
self._jobs = dict[str, "Job"]()
self._pipelines = dict[str, "Pipeline"]()
self._schemas = dict[str, "Schema"]()
self._volumes = dict[str, "Volume"]()
self._locations = dict[tuple[str, ...], Location]()
self._diagnostics = Diagnostics()
@property
def jobs(self) -> dict[str, "Job"]:
return self._jobs
@property
def pipelines(self) -> dict[str, "Pipeline"]:
return self._pipelines
@property
def schemas(self) -> dict[str, "Schema"]:
return self._schemas
@property
def volumes(self) -> dict[str, "Volume"]:
return self._volumes
@property
def diagnostics(self) -> Diagnostics:
"""
Returns diagnostics. If there are any diagnostic errors, bundle validation fails.
"""
return self._diagnostics
def add_resource(
self,
resource_name: str,
resource: Resource,
*,
location: Optional[Location] = None,
) -> None:
"""
Adds a resource to the collection of resources. Resource name must be unique across all
resources of the same type.
:param resource_name: unique identifier for the resource
:param resource: the resource to add
:param location: optional location of the resource in the source code
"""
from databricks.bundles.jobs import Job
from databricks.bundles.pipelines import Pipeline
from databricks.bundles.schemas import Schema
from databricks.bundles.volumes import Volume
location = location or Location.from_stack_frame(depth=1)
match resource:
case Job():
self.add_job(resource_name, resource, location=location)
case Pipeline():
self.add_pipeline(resource_name, resource, location=location)
case Schema():
self.add_schema(resource_name, resource, location=location)
case Volume():
self.add_volume(resource_name, resource, location=location)
case _:
raise ValueError(f"Unsupported resource type: {type(resource)}")
def add_job(
self,
resource_name: str,
job: "JobParam",
*,
location: Optional[Location] = None,
) -> None:
"""
Adds a job to the collection of resources. Resource name must be unique across all jobs.
:param resource_name: unique identifier for the job
:param job: the job to add, can be Job or dict
:param location: optional location of the job in the source code
"""
from databricks.bundles.jobs import Job
job = _transform(Job, job)
path = ("resources", "jobs", resource_name)
location = location or Location.from_stack_frame(depth=1)
if self._jobs.get(resource_name):
self.add_diagnostic_error(
msg=f"Duplicate resource name '{resource_name}' for a job. Resource names must be unique.",
location=location,
path=path,
)
else:
if location:
self.add_location(path, location)
self._jobs[resource_name] = job
def add_pipeline(
self,
resource_name: str,
pipeline: "PipelineParam",
*,
location: Optional[Location] = None,
) -> None:
"""
Adds a pipeline to the collection of resources. Resource name must be unique across all pipelines.
:param resource_name: unique identifier for the pipeline
:param pipeline: the pipeline to add, can be Pipeline or dict
:param location: optional location of the pipeline in the source code
"""
from databricks.bundles.pipelines import Pipeline
pipeline = _transform(Pipeline, pipeline)
path = ("resources", "pipelines", resource_name)
location = location or Location.from_stack_frame(depth=1)
if self._pipelines.get(resource_name):
self.add_diagnostic_error(
msg=f"Duplicate resource name '{resource_name}' for a pipeline. Resource names must be unique.",
location=location,
path=path,
)
else:
if location:
self.add_location(path, location)
self._pipelines[resource_name] = pipeline
def add_schema(
self,
resource_name: str,
schema: "SchemaParam",
*,
location: Optional[Location] = None,
) -> None:
"""
Adds a schema to the collection of resources. Resource name must be unique across all schemas.
:param resource_name: unique identifier for the schema
:param schema: the schema to add, can be Schema or dict
:param location: optional location of the schema in the source code
"""
from databricks.bundles.schemas import Schema
schema = _transform(Schema, schema)
path = ("resources", "schemas", resource_name)
location = location or Location.from_stack_frame(depth=1)
if self._schemas.get(resource_name):
self.add_diagnostic_error(
msg=f"Duplicate resource name '{resource_name}' for a schema. Resource names must be unique.",
location=location,
path=path,
)
else:
if location:
self.add_location(path, location)
self._schemas[resource_name] = schema
def add_volume(
self,
resource_name: str,
volume: "VolumeParam",
*,
location: Optional[Location] = None,
) -> None:
"""
Adds a volume to the collection of resources. Resource name must be unique across all volumes.
:param resource_name: unique identifier for the volume
:param volume: the volume to add, can be Volume or dict
:param location: optional location of the volume in the source code
"""
from databricks.bundles.volumes import Volume
volume = _transform(Volume, volume)
path = ("resources", "volumes", resource_name)
location = location or Location.from_stack_frame(depth=1)
if self._volumes.get(resource_name):
self.add_diagnostic_error(
msg=f"Duplicate resource name '{resource_name}' for a volume. Resource names must be unique.",
location=location,
path=path,
)
else:
if location:
self.add_location(path, location)
self._volumes[resource_name] = volume
def add_location(self, path: tuple[str, ...], location: Location) -> None:
"""
Associate source code location with a path in the bundle configuration.
"""
self._locations[path] = location
def add_diagnostics(self, other: Diagnostics) -> None:
"""
Add diagnostics from another Diagnostics object.
:param other:
:return:
"""
self._diagnostics = self._diagnostics.extend(other)
def add_diagnostic_error(
self,
msg: str,
*,
detail: Optional[str] = None,
path: Optional[tuple[str, ...]] = None,
location: Optional[Location] = None,
) -> None:
"""
Report a diagnostic error. If there are any diagnostic errors, bundle validation fails.
:param msg: short summary of the error
:param detail: optional detailed description of the error
:param path: optional path in bundle configuration where the error occurred
:param location: optional location in the source code where the error occurred
"""
self.add_diagnostics(
Diagnostics.create_error(
msg=msg,
location=location,
detail=detail,
path=path,
)
)
def add_diagnostic_warning(
self,
msg: str,
*,
detail: Optional[str] = None,
path: Optional[tuple[str, ...]] = None,
location: Optional[Location] = None,
) -> None:
"""
Report a diagnostic warning. Warnings are informational and do not cause bundle validation to fail.
:param msg: short summary of the warning
:param detail: optional detailed description of the warning
:param path: optional path in bundle configuration where the warning occurred
:param location: optional location in the source code where the warning occurred
"""
self.add_diagnostics(
Diagnostics.create_warning(
msg=msg,
location=location,
detail=detail,
path=path,
)
)
def add_resources(self, other: "Resources") -> None:
"""
Add resources from another Resources object.
Adds error to diagnostics if there are duplicate resource names.
"""
for name, job in other.jobs.items():
self.add_job(name, job)
for name, pipeline in other.pipelines.items():
self.add_pipeline(name, pipeline)
for name, schema in other.schemas.items():
self.add_schema(name, schema)
for name, volume in other.volumes.items():
self.add_volume(name, volume)
for path, location in other._locations.items():
self.add_location(path, location)
self._diagnostics = self._diagnostics.extend(other._diagnostics)