File: responses.py

package info (click to toggle)
python-moto 5.1.18-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 116,520 kB
  • sloc: python: 636,725; javascript: 181; makefile: 39; sh: 3
file content (265 lines) | stat: -rw-r--r-- 9,160 bytes parent folder | download
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
"""Handles incoming servicecatalog requests, invokes methods, returns responses."""

import json

from moto.core.responses import BaseResponse

from .models import ServiceCatalogBackend, servicecatalog_backends


class ServiceCatalogResponse(BaseResponse):
    """Handler for ServiceCatalog requests and responses."""

    def __init__(self) -> None:
        super().__init__(service_name="servicecatalog")

    @property
    def servicecatalog_backend(self) -> ServiceCatalogBackend:
        """Return backend instance specific for this region."""
        # TODO
        # servicecatalog_backends is not yet typed
        # Please modify moto/backends.py to add the appropriate type annotations for this service
        return servicecatalog_backends[self.current_account][self.region]

    def list_portfolio_access(self) -> str:
        params = json.loads(self.body)
        accept_language = params.get("AcceptLanguage")
        portfolio_id = params.get("PortfolioId")
        organization_parent_id = params.get("OrganizationParentId")
        page_token = params.get("PageToken")
        page_size = params.get("PageSize")

        account_id_objects, next_page_token = (
            self.servicecatalog_backend.list_portfolio_access(
                accept_language=accept_language,
                portfolio_id=portfolio_id,
                organization_parent_id=organization_parent_id,
                page_token=page_token,
                page_size=page_size,
            )
        )

        account_ids = [obj["account_id"] for obj in account_id_objects]

        return json.dumps({"AccountIds": account_ids, "NextPageToken": next_page_token})

    def delete_portfolio(self) -> str:
        params = json.loads(self.body)
        accept_language = params.get("AcceptLanguage")
        id = params.get("Id")

        self.servicecatalog_backend.delete_portfolio(
            accept_language=accept_language,
            id=id,
        )

        return json.dumps({})

    def delete_portfolio_share(self) -> str:
        params = json.loads(self.body)
        accept_language = params.get("AcceptLanguage")
        portfolio_id = params.get("PortfolioId")
        account_id = params.get("AccountId")
        organization_node = params.get("OrganizationNode")

        portfolio_share_token = self.servicecatalog_backend.delete_portfolio_share(
            accept_language=accept_language,
            portfolio_id=portfolio_id,
            account_id=account_id,
            organization_node=organization_node,
        )

        response = {}
        if portfolio_share_token:
            response["PortfolioShareToken"] = portfolio_share_token

        return json.dumps(response)

    def create_portfolio(self) -> str:
        params = json.loads(self.body)

        accept_language = params.get("AcceptLanguage")
        display_name = params.get("DisplayName")
        description = params.get("Description")
        provider_name = params.get("ProviderName")
        tags = params.get("Tags")
        idempotency_token = params.get("IdempotencyToken")

        portfolio_detail, tags = self.servicecatalog_backend.create_portfolio(
            accept_language=accept_language,
            display_name=display_name,
            description=description,
            provider_name=provider_name,
            tags=tags,
            idempotency_token=idempotency_token,
        )

        return json.dumps({"PortfolioDetail": portfolio_detail, "Tags": tags})

    def create_portfolio_share(self) -> str:
        params = json.loads(self.body)
        accept_language = params.get("AcceptLanguage")
        portfolio_id = params.get("PortfolioId")
        account_id = params.get("AccountId")
        organization_node = params.get("OrganizationNode")
        share_tag_options = params.get("ShareTagOptions", False)
        share_principals = params.get("SharePrincipals", False)

        portfolio_share_token = self.servicecatalog_backend.create_portfolio_share(
            accept_language=accept_language,
            portfolio_id=portfolio_id,
            account_id=account_id,
            organization_node=organization_node,
            share_tag_options=share_tag_options,
            share_principals=share_principals,
        )

        response = {}
        if portfolio_share_token:
            response["PortfolioShareToken"] = portfolio_share_token

        return json.dumps(response)

    def list_portfolios(self) -> str:
        params = self._get_params()
        accept_language = params.get("AcceptLanguage")
        page_token = params.get("PageToken")
        page_size = params.get("PageSize")

        portfolio_details, next_page_token = (
            self.servicecatalog_backend.list_portfolios(
                accept_language=accept_language,
                page_token=page_token,
                page_size=page_size,
            )
        )

        response = {
            "PortfolioDetails": portfolio_details,
            "NextPageToken": next_page_token,
        }
        return json.dumps(response)

    def describe_portfolio_shares(self) -> str:
        params = json.loads(self.body)
        portfolio_id = params.get("PortfolioId")
        type = params.get("Type")
        page_token = params.get("PageToken")
        page_size = params.get("PageSize")

        next_page_token, portfolio_share_details = (
            self.servicecatalog_backend.describe_portfolio_shares(
                portfolio_id=portfolio_id,
                type=type,
                page_token=page_token,
                page_size=page_size,
            )
        )

        response = {
            "NextPageToken": next_page_token,
            "PortfolioShareDetails": portfolio_share_details,
        }

        return json.dumps(response)

    def describe_portfolio(self) -> str:
        """Handle describe_portfolio request."""
        params = json.loads(self.body)
        accept_language = params.get("AcceptLanguage")
        id = params.get("Id")

        portfolio_detail, tags, tag_options, budgets = (
            self.servicecatalog_backend.describe_portfolio(
                accept_language=accept_language,
                id=id,
            )
        )

        response = {
            "PortfolioDetail": portfolio_detail,
            "Tags": tags,
            "TagOptions": tag_options,
            "Budgets": budgets,
        }

        return json.dumps(response)

    def create_product(self) -> str:
        params = json.loads(self.body)
        name = params.get("Name")
        owner = params.get("Owner")
        description = params.get("Description")
        distributor = params.get("Distributor")
        support_description = params.get("SupportDescription")
        support_email = params.get("SupportEmail")
        support_url = params.get("SupportUrl")
        product_type = params.get("ProductType")
        tags = params.get("Tags")
        provisioning_artifact_parameters = params.get("ProvisioningArtifactParameters")
        idempotency_token = params.get("IdempotencyToken")
        source_connection = params.get("SourceConnection")
        accept_language = params.get("AcceptLanguage")

        response = self.servicecatalog_backend.create_product(
            name=name,
            owner=owner,
            description=description,
            distributor=distributor,
            support_description=support_description,
            support_email=support_email,
            support_url=support_url,
            product_type=product_type,
            tags=tags,
            provisioning_artifact_parameters=provisioning_artifact_parameters,
            idempotency_token=idempotency_token,
            source_connection=source_connection,
            accept_language=accept_language,
        )

        product_view = {
            "ProductViewSummary": response.to_dict(),
            "Status": "AVAILABLE",
            "ProductARN": response.arn,
            "CreatedTime": response.created_time.isoformat(),
            "SourceConnection": response.source_connection,
        }

        return json.dumps(
            {
                "ProductViewDetail": product_view,
                "ProvisioningArtifactDetail": {},
                "Tags": response.tags,
            }
        )

    def describe_product(self) -> str:
        params = json.loads(self.body)
        accept_language = params.get("AcceptLanguage")
        id = params.get("Id")
        name = params.get("Name")

        response = self.servicecatalog_backend.describe_product(
            accept_language=accept_language, id=id, name=name
        )

        result = {
            "ProductViewSummary": response.to_dict(),
            "ProvisioningArtifacts": [],
            "Budgets": [],
            "LaunchPaths": [],
        }

        return json.dumps(result)

    def delete_product(self) -> str:
        params = json.loads(self.body)
        accept_language = params.get("AcceptLanguage")
        id = params.get("Id")

        self.servicecatalog_backend.delete_product(
            accept_language=accept_language,
            id=id,
        )

        return json.dumps({})