aboutsummaryrefslogtreecommitdiffstats
path: root/ms/py-executor/resource_resolution/resource_resolution.py
blob: 3b8c19b744116a138ef0470ddb214321e40ea385 (plain)
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
"""Copyright 2020 Deutsche Telekom.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""

import json
from dataclasses import dataclass, field
from enum import Enum, unique
from logging import Logger, getLogger
from os import getenv
from types import TracebackType
from typing import Any, Dict, Generator, Optional, Type

from google.protobuf import json_format

from proto.BluePrintProcessing_pb2 import ExecutionServiceInput, ExecutionServiceOutput

from .grpc import Client as GrpcClient
from .http import Client as HttpClient


@unique
class WorkflowMode(Enum):
    """Workflow mode enumerator.

    Workflow can be executed in two modes: synchronously and asynchronously.
    This enumerator stores valid values to set the mode: SYNC for synchronously mode and ASYNC for asynchronously.
    """

    SYNC = "sync"
    ASYNC = "async"


class WorkflowExecution:
    """Wokflow execution class.

    Describes workflow to call. Set blueprint name and version and workflow name to execute.
    Workflow inputs are optional, by default set to empty directory.
    Workflow mode is also optional. It is set by default to call workflow synchronously.
    """

    def __init__(
        self,
        blueprint_name: str,
        blueprint_version: str,
        workflow_name: str,
        workflow_inputs: Dict[str, Any] = None,
        workflow_mode: WorkflowMode = WorkflowMode.SYNC,
    ) -> None:
        """Initialize workflow execution.

        Get all needed information to execute workflow.

        Args:
            blueprint_name (str): Blueprint name to execute workflow from.
            blueprint_version (str): Blueprint version.
            workflow_name (str): Name of the workflow to execute
            workflow_inputs (Dict[str, Any], optional): Key-value workflow inputs. Defaults to None.
            workflow_mode (WorkflowMode, optional): Workflow execution mode. It can be run synchronously or
                asynchronously. Defaults to WorkflowMode.SYNC.
        """
        self.blueprint_name: str = blueprint_name
        self.blueprint_version: str = blueprint_version
        self.workflow_name: str = workflow_name
        if workflow_inputs is None:
            workflow_inputs = {}
        self.workflow_inputs: Dict[str, Any] = workflow_inputs
        self.workflow_mode: WorkflowMode = workflow_mode

    @property
    def message(self) -> ExecutionServiceInput:
        """Workflow execution protobuf message.

        This message is going to be sent to gRPC server to execute workflow.

        Returns:
            ExecutionServiceInput: Properly filled protobuf message.
        """
        execution_msg: ExecutionServiceInput = ExecutionServiceInput()
        execution_msg.actionIdentifiers.mode = self.workflow_mode.value
        execution_msg.actionIdentifiers.blueprintName = self.blueprint_name
        execution_msg.actionIdentifiers.blueprintVersion = self.blueprint_version
        execution_msg.actionIdentifiers.actionName = self.workflow_name
        execution_msg.payload.update({f"{self.workflow_name}-request": self.workflow_inputs})
        return execution_msg


class WorkflowExecutionResult:
    """Result of workflow execution.

    Store both workflow data and the result returns by server.
    """

    def __init__(self, workflow_execution: WorkflowExecution, execution_output: ExecutionServiceOutput) -> None:
        """Initialize workflow execution result object.

        Stores workflow execution data and execution result.

        Args:
            workflow_execution (WorkflowExecution): WorkflowExecution object which was used to call request.
            execution_output (ExecutionServiceOutput): gRPC server response.
        """
        self.workflow_execution: WorkflowExecution = workflow_execution
        self.execution_output: ExecutionServiceOutput = execution_output

    @property
    def blueprint_name(self) -> str:
        """Name of blueprint used to call workflow.

        This value is taken from server response not request (should be the same).

        Returns:
            str: Blueprint name
        """
        return self.execution_output.actionIdentifiers.blueprintName

    @property
    def blueprint_version(self) -> str:
        """Blueprint version.

        This value is taken from server response not request (should be the same).

        Returns:
            str: Blueprint version
        """
        return self.execution_output.actionIdentifiers.blueprintVersion

    @property
    def workflow_name(self) -> str:
        """Workflow name.

        This value is taken from server response not request (should be the same).

        Returns:
            str: Workflow name
        """
        return self.execution_output.actionIdentifiers.actionName

    @property
    def has_error(self) -> bool:
        """Returns bool if request returns error or not.

        Returns:
            bool: True if response has status code different than 200
        """
        return self.execution_output.status.code != 200

    @property
    def error_message(self) -> str:
        """Error message.

        This property is available only if response has error. Otherwise AttributeError will be raised.

        Raises:
            AttributeError: Response has 200 response code and hasn't error message.

        Returns:
            str: Error message returned by server
        """
        if self.has_error:
            return self.execution_output.status.errorMessage
        raise AttributeError("Execution does not finish with error")

    @property
    def payload(self) -> dict:
        """Response payload.

        Payload retured by the server is migrated to Python dict.

        Returns:
            dict: Response's payload.
        """
        return json_format.MessageToDict(self.execution_output.payload)


@dataclass
class Template:
    """Template dataclass.

    Store resolved template data.
    It keeps also ResourceResolution object to call `store_template` method.
    """

    resource_resolution: "ResourceResolution" = field(repr=False)
    blueprint_name: str
    blueprint_version: str
    artifact_name: str = None
    result: str = None
    resolution_key: str = None
    resource_type: str = None
    resource_id: str = None

    def store(self) -> None:
        """Store template using blueprintprocessor HTTP API.

        It uses ResourceResolution `store_template` method.
        """
        self.resource_resolution.store_template(
            blueprint_name=self.blueprint_name,
            blueprint_version=self.blueprint_version,
            artifact_name=self.artifact_name,
            result=self.result,
            resolution_key=self.resolution_key,
            resource_type=self.resource_type,
            resource_id=self.resource_id,
        )


class ResourceResolution:
    """Resource resolution class.

    Helper class to connect to blueprintprocessor's gRPC server, send request to execute workflow and parse responses.
    Blueprint with workflow must be deployed before workflow request.
    It's possible to create both secre or unsecure connection (without SSL/TLS).
    """

    def __init__(
        self,
        *,
        server_address: str = "127.0.0.1",
        # GRPC client configuration
        grpc_server_port: int = 9111,
        use_ssl: bool = False,
        root_certificates: bytes = None,
        private_key: bytes = None,
        certificate_chain: bytes = None,
        # Authentication header configuration for GRPC client
        use_header_auth: bool = False,
        header_auth_token: str = None,
        # HTTP client configuration
        http_server_port: int = 8080,
        http_auth_user: str = None,
        http_auth_pass: str = None,
        http_use_ssl: bool = True,
    ) -> None:
        """Resource resolution object initialization.

        Args:
            server_address (str, optional): gRPC server address. Defaults to "127.0.0.1".
            grpc_server_port (int, optional): gRPC server address port. Defaults to 9111.
            use_ssl (bool, optional): Boolean flag to determine if secure channel should be created or not.
                Defaults to False.
            root_certificates (bytes, optional): The PEM-encoded root certificates. None if it shouldn't be used.
                Defaults to None.
            private_key (bytes, optional): The PEM-encoded private key as a byte string, or None if no private key
                should be used. Defaults to None.
            certificate_chain (bytes, optional): The PEM-encoded certificate chain as a byte string to use or or None if
                no certificate chain should be used. Defaults to None.
            use_header_auth (bool, optional): Boolean flag to determine if authorization headed shoud be added for
                every call or not. Defaults to False.
            header_auth_token (str, optional): Authorization token value. Defaults to None.
                If no value is provided "AUTH_TOKEN" environment variable will be used.
            http_server_port (int, optional): HTTP server address port. Defaults to 8080.
            http_auth_user (str, optional): Username used for HTTP requests authorization. Defaults to None.
                If no value is provided "API_USERNAME" environment variable will be used.
            http_auth_pass (str, optional): Password used for HTTP requests authorization. Defaults to None.
                If no value is provided "API_PASSWORD" environment variable will be used.
            http_use_ssl (bool, optional): Determines if secure connection should be used for HTTP requests.
                Defaults to False.
        """
        # Logger
        self.logger: Logger = getLogger(__name__)
        # GrpcClient settings
        self.grpc_client_server_address: str = server_address
        self.grpc_client_server_port: str = grpc_server_port
        self.grpc_client_use_ssl: bool = use_ssl
        self.grpc_client_root_certificates: bytes = root_certificates
        self.grpc_client_private_key: bytes = private_key
        self.grpc_client_certificate_chain: bytes = certificate_chain
        self.grpc_client_use_header_auth: bool = use_header_auth
        self.grpc_client_header_auth_token: str = header_auth_token or getenv("AUTH_TOKEN")
        self.grpc_client: GrpcClient = None
        # HttpClient settings
        self.http_client: HttpClient = HttpClient(
            server_address,
            server_port=http_server_port,
            auth_user=http_auth_user or getenv("API_USERNAME"),
            auth_pass=http_auth_pass or getenv("API_PASSWORD"),
            use_ssl=http_use_ssl,
        )

    def __enter__(self) -> "ResourceResolution":
        """Enter ResourceResolution instance context.

        GrpcClient connection is created.
        """
        self.grpc_client = GrpcClient(
            server_address=f"{self.grpc_client_server_address}:{self.grpc_client_server_port}",
            use_ssl=self.grpc_client_use_ssl,
            root_certificates=self.grpc_client_root_certificates,
            private_key=self.grpc_client_private_key,
            certificate_chain=self.grpc_client_certificate_chain,
            use_header_auth=self.grpc_client_use_header_auth,
            header_auth_token=self.grpc_client_header_auth_token,
        )
        return self

    def __exit__(
        self,
        unused_exc_type: Optional[Type[BaseException]],
        unused_exc_value: Optional[BaseException],
        unused_traceback: Optional[TracebackType],
    ) -> None:
        """Exit ResourceResolution instance context.

        GrpcClient connection is closed.
        """
        self.grpc_client.close()

    def execute_workflows(self, *workflows: WorkflowExecution) -> Generator[WorkflowExecutionResult, None, None]:
        """Execute provided workflows.

        Workflows are going to be execured using one gRPC API call. Depends of implementation that may has
        some consequences. In some cases if any request fails all requests after that won't be called.

        Responses and zipped with workflows and WorkflowExecutionResult object is initialized and yielded.

        Raises:
            AttributeError: Raises if client object is not created. It occurs only if you not uses context manager.
                Then user have to create client instance for ResourceResolution object by himself calling:
                ```
                resource_resoulution.client = GrpcClient(
                    server_address=f"{resource_resoulution.client_server_address}:{resource_resoulution.client_server_port}",
                    use_ssl=resource_resoulution.client_use_ssl,
                    root_certificates=resource_resoulution.client_root_certificates,
                    private_key=resource_resoulution.client_private_key,
                    certificate_chain=resource_resoulution.client_certificate_chain,
                    use_header_auth=resource_resoulution.client_use_header_auth,
                    header_auth_token=resource_resoulution.client_header_auth_token,
                )
                ```
                Remeber also to close client connection.

        Returns:
            Generator[WorkflowExecutionResult, None, None]: WorkflowExecutionResult object
                with both WorkflowExection object and server response for it's request.
        """
        self.logger.debug("Execute workflows")
        if not self.grpc_client:
            raise AttributeError("gRPC client not connected")

        for response, workflow in zip(
            self.grpc_client.process((workflow.message for workflow in workflows)), workflows
        ):
            yield WorkflowExecutionResult(workflow, response)

    def _check_template_resolve_params(
        self, resolution_key: str = None, resource_type: str = None, resource_id: str = None
    ):
        """Check template API request parameters.

        It's possible to store/retrieve templates using pair of artifact name and resolution key OR
        resource type and resource id. This method checks if valid combination of parameters were used.
        
        Args:
            resolution_key (str, optional): resolutionKey HTTP request parameter value. Defaults to None.
            resource_type (str, optional): resourceType HTTP request parameter value. Defaults to None.
            resource_id (str, optional): resourceId HTTP request parameter value. Defaults to None.
        
        Raises:
            AttributeError: Invalid combination of parametes used
        """
        if not any([resolution_key, all([resource_type, resource_id])]):
            raise AttributeError(
                "To store/retrieve template resolution_key and artifact_name or both resource_type and resource_id have to be provided"
            )

    def store_template(
        self,
        blueprint_name: str,
        blueprint_version: str,
        result: str,
        artifact_name: str,
        resolution_key: str = None,
        resource_type: str = None,
        resource_id: str = None,
    ) -> None:
        """Store template using blueprintprocessor HTTP API.

        Prepare and send a request to store resolved template using blueprint name, blueprint version
        and pair of artifact name and resolution key OR resource type and resource id.

        Method returns Template dataclass, which stores all template data and can be used to update
        it's result.

        Args:
            blueprint_name (str): Blueprint name
            blueprint_version (str): Blueprint version
            result (str): Template result
            artifact_name (str): Artifact name
            resolution_key (str, optional): Resolution key. Defaults to None.
            resource_type (str, optional): Resource type. Defaults to None.
            resource_id (str, optional): Resource ID. Defaults to None.
        """
        self.logger.debug("Store template")
        self._check_template_resolve_params(resolution_key, resource_type, resource_id)
        base_endpoint: str = f"template/{blueprint_name}/{blueprint_version}"
        if resolution_key and artifact_name:
            endpoint: str = f"{base_endpoint}/{artifact_name}/{resolution_key}"
        else:
            endpoint: str = f"{base_endpoint}/{resource_type}/{resource_id}"
        response = self.http_client.send_request(
            "POST", endpoint, headers={"Content-Type": "application/json"}, data=json.dumps({"result": result})
        )

    def retrieve_template(
        self,
        blueprint_name: str,
        blueprint_version: str,
        artifact_name: str,
        resolution_key: str = None,
        resource_type: str = None,
        resource_id: str = None,
    ) -> Template:
        """Get stored template using blueprintprocessor's HTTP API.

        Prepare and send a request to retrieve resolved template using blueprint name, blueprint version
        and pair of artifact name and resolution key OR resource type and resource id.

        Args:
            blueprint_name (str): Blueprint name
            blueprint_version (str): Blueprint version
            artifact_name (str): Artifact name
            resolution_key (str, optional): Resolution key. Defaults to None.
            resource_type (str, optional): Resource type. Defaults to None.
            resource_id (str, optional): Resource ID. Defaults to None.
        """
        self.logger.debug("Retrieve template")
        self._check_template_resolve_params(resolution_key, resource_type, resource_id)
        params: dict = {"bpName": blueprint_name, "bpVersion": blueprint_version, "artifactName": artifact_name}
        if resolution_key:
            params.update({"resolutionKey": resolution_key})
        else:
            params.update({"resourceType": resource_type, "resourceId": resource_id})
        response = self.http_client.send_request(
            "GET", "template", headers={"Accept": "application/json"}, params=params
        )
        return Template(
            resource_resolution=self,
            blueprint_name=blueprint_name,
            blueprint_version=blueprint_version,
            artifact_name=artifact_name,
            resolution_key=resolution_key,
            resource_type=resource_type,
            resource_id=resource_id,
            result=response.json()["result"],
        )