File size: 12,422 Bytes
4021124
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
#     http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file 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.
"""This module contains code to create and manage SageMaker ``Actions``."""
from __future__ import absolute_import

from typing import Optional, Iterator, List
from datetime import datetime

from sagemaker.session import Session
from sagemaker.apiutils import _base_types
from sagemaker.lineage import _api_types, _utils
from sagemaker.lineage._api_types import ActionSource, ActionSummary
from sagemaker.lineage.artifact import Artifact

from sagemaker.lineage.query import (
    LineageQuery,
    LineageFilter,
    LineageSourceEnum,
    LineageEntityEnum,
    LineageQueryDirectionEnum,
)


class Action(_base_types.Record):
    """An Amazon SageMaker action, which is part of a SageMaker lineage.

    Examples:
        .. code-block:: python

            from sagemaker.lineage import action

            my_action = action.Action.create(
                action_name='MyAction',
                action_type='EndpointDeployment',
                source_uri='s3://...')

            my_action.properties["added"] = "property"
            my_action.save()

            for actn in action.Action.list():
                print(actn)

            my_action.delete()

    Attributes:
        action_arn (str): The ARN of the action.
        action_name (str): The name of the action.
        action_type (str): The type of the action.
        description (str): A description of the action.
        status (str): The status of the action.
        source (obj): The source of the action with a URI and type.
        properties (dict): Dictionary of properties.
        tags (List[dict[str, str]]): A list of tags to associate with the action.
        creation_time (datetime): When the action was created.
        created_by (obj): Contextual info on which account created the action.
        last_modified_time (datetime): When the action was last modified.
        last_modified_by (obj): Contextual info on which account created the action.
    """

    action_arn: str = None
    action_name: str = None
    action_type: str = None
    description: str = None
    status: str = None
    source: ActionSource = None
    properties: dict = None
    properties_to_remove: list = None
    tags: list = None
    creation_time: datetime = None
    created_by: str = None
    last_modified_time: datetime = None
    last_modified_by: str = None

    _boto_create_method: str = "create_action"
    _boto_load_method: str = "describe_action"
    _boto_update_method: str = "update_action"
    _boto_delete_method: str = "delete_action"

    _boto_update_members = [
        "action_name",
        "description",
        "status",
        "properties",
        "properties_to_remove",
    ]

    _boto_delete_members = ["action_name"]

    _custom_boto_types = {"source": (_api_types.ActionSource, False)}

    def save(self) -> "Action":
        """Save the state of this Action to SageMaker.

        Returns:
            Action: A SageMaker ``Action``object.
        """
        return self._invoke_api(self._boto_update_method, self._boto_update_members)

    def delete(self, disassociate: bool = False):
        """Delete the action.

        Args:
            disassociate (bool): When set to true, disassociate incoming and outgoing association.

        """
        if disassociate:
            _utils._disassociate(
                source_arn=self.action_arn, sagemaker_session=self.sagemaker_session
            )
            _utils._disassociate(
                destination_arn=self.action_arn,
                sagemaker_session=self.sagemaker_session,
            )

        self._invoke_api(self._boto_delete_method, self._boto_delete_members)

    @classmethod
    def load(cls, action_name: str, sagemaker_session=None) -> "Action":
        """Load an existing action and return an ``Action`` object representing it.

        Args:
            action_name (str): Name of the action
            sagemaker_session (sagemaker.session.Session): Session object which
                manages interactions with Amazon SageMaker APIs and any other
                AWS services needed. If not specified, one is created using the
                default AWS configuration chain.

        Returns:
            Action: A SageMaker ``Action`` object
        """
        result = cls._construct(
            cls._boto_load_method,
            action_name=action_name,
            sagemaker_session=sagemaker_session,
        )
        return result

    def set_tag(self, tag=None):
        """Add a tag to the object.

        Args:

        Returns:
            list({str:str}): a list of key value pairs
        """
        return self._set_tags(resource_arn=self.action_arn, tags=[tag])

    def set_tags(self, tags=None):
        """Add tags to the object.

        Args:
            tags ([{key:value}]): list of key value pairs.

        Returns:
            list({str:str}): a list of key value pairs
        """
        return self._set_tags(resource_arn=self.action_arn, tags=tags)

    @classmethod
    def create(
        cls,
        action_name: str = None,
        source_uri: str = None,
        source_type: str = None,
        action_type: str = None,
        description: str = None,
        status: str = None,
        properties: dict = None,
        tags: dict = None,
        sagemaker_session: Session = None,
    ) -> "Action":
        """Create an action and return an ``Action`` object representing it.

        Args:
            action_name (str): Name of the action
            source_uri (str): Source URI of the action
            source_type (str): Source type of the action
            action_type (str): The type of the action
            description (str): Description of the action
            status (str): Status of the action.
            properties (dict): key/value properties
            tags (dict): AWS tags for the action
            sagemaker_session (sagemaker.session.Session): Session object which
                manages interactions with Amazon SageMaker APIs and any other
                AWS services needed. If not specified, one is created using the
                default AWS configuration chain.

        Returns:
            Action: A SageMaker ``Action`` object.
        """
        return super(Action, cls)._construct(
            cls._boto_create_method,
            action_name=action_name,
            source=_api_types.ActionSource(source_uri=source_uri, source_type=source_type),
            action_type=action_type,
            description=description,
            status=status,
            properties=properties,
            tags=tags,
            sagemaker_session=sagemaker_session,
        )

    @classmethod
    def list(
        cls,
        source_uri: Optional[str] = None,
        action_type: Optional[str] = None,
        created_after: Optional[datetime] = None,
        created_before: Optional[datetime] = None,
        sort_by: Optional[str] = None,
        sort_order: Optional[str] = None,
        sagemaker_session: Session = None,
        max_results: Optional[int] = None,
        next_token: Optional[str] = None,
    ) -> Iterator[ActionSummary]:
        """Return a list of action summaries.

        Args:
            source_uri (str, optional): A source URI.
            action_type (str, optional): An action type.
            created_before (datetime.datetime, optional): Return actions created before this
                instant.
            created_after (datetime.datetime, optional): Return actions created after this instant.
            sort_by (str, optional): Which property to sort results by.
                One of 'SourceArn', 'CreatedBefore', 'CreatedAfter'
            sort_order (str, optional): One of 'Ascending', or 'Descending'.
            max_results (int, optional): maximum number of actions to retrieve
            next_token (str, optional): token for next page of results
            sagemaker_session (sagemaker.session.Session): Session object which
                manages interactions with Amazon SageMaker APIs and any other
                AWS services needed. If not specified, one is created using the
                default AWS configuration chain.

        Returns:
            collections.Iterator[ActionSummary]: An iterator
                over ``ActionSummary`` objects.
        """
        return super(Action, cls)._list(
            "list_actions",
            _api_types.ActionSummary.from_boto,
            "ActionSummaries",
            source_uri=source_uri,
            action_type=action_type,
            created_before=created_before,
            created_after=created_after,
            sort_by=sort_by,
            sort_order=sort_order,
            sagemaker_session=sagemaker_session,
            max_results=max_results,
            next_token=next_token,
        )

    def artifacts(
        self, direction: LineageQueryDirectionEnum = LineageQueryDirectionEnum.BOTH
    ) -> List[Artifact]:
        """Use a lineage query to retrieve all artifacts that use this action.

        Args:
            direction (LineageQueryDirectionEnum, optional): The query direction.

        Returns:
            list of Artifacts: Artifacts.
        """
        query_filter = LineageFilter(entities=[LineageEntityEnum.ARTIFACT])
        query_result = LineageQuery(self.sagemaker_session).query(
            start_arns=[self.action_arn],
            query_filter=query_filter,
            direction=direction,
            include_edges=False,
        )
        return [vertex.to_lineage_object() for vertex in query_result.vertices]


class ModelPackageApprovalAction(Action):
    """An Amazon SageMaker model package approval action, which is part of a SageMaker lineage."""

    def datasets(
        self, direction: LineageQueryDirectionEnum = LineageQueryDirectionEnum.ASCENDANTS
    ) -> List[Artifact]:
        """Use a lineage query to retrieve all upstream datasets that use this action.

        Args:
            direction (LineageQueryDirectionEnum, optional): The query direction.

        Returns:
            list of Artifacts: Artifacts representing a dataset.
        """
        query_filter = LineageFilter(
            entities=[LineageEntityEnum.ARTIFACT], sources=[LineageSourceEnum.DATASET]
        )
        query_result = LineageQuery(self.sagemaker_session).query(
            start_arns=[self.action_arn],
            query_filter=query_filter,
            direction=direction,
            include_edges=False,
        )
        return [vertex.to_lineage_object() for vertex in query_result.vertices]

    def model_package(self):
        """Get model package from model package approval action.

        Returns:
            Model package.
        """
        source_uri = self.source.source_uri
        if source_uri is None:
            return None

        model_package_name = source_uri.split("/")[1]
        return self.sagemaker_session.sagemaker_client.describe_model_package(
            ModelPackageName=model_package_name
        )

    def endpoints(
        self, direction: LineageQueryDirectionEnum = LineageQueryDirectionEnum.DESCENDANTS
    ) -> List:
        """Use a lineage query to retrieve downstream endpoint contexts that use this action.

        Args:
            direction (LineageQueryDirectionEnum, optional): The query direction.

        Returns:
            list of Contexts: Contexts representing an endpoint.
        """
        query_filter = LineageFilter(
            entities=[LineageEntityEnum.CONTEXT], sources=[LineageSourceEnum.ENDPOINT]
        )
        query_result = LineageQuery(self.sagemaker_session).query(
            start_arns=[self.action_arn],
            query_filter=query_filter,
            direction=direction,
            include_edges=False,
        )
        return [vertex.to_lineage_object() for vertex in query_result.vertices]