File: models.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 (286 lines) | stat: -rw-r--r-- 9,526 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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
from collections.abc import Iterable
from typing import Any, Optional

from moto.core.base_backend import BackendDict, BaseBackend
from moto.core.common_models import BaseModel
from moto.moto_api._internal import mock_random
from moto.utilities.tagging_service import TaggingService
from moto.utilities.utils import get_partition

from .exceptions import (
    AppNotFoundException,
    ConfigurationProfileNotFound,
    ConfigurationVersionNotFound,
)


class HostedConfigurationVersion(BaseModel):
    def __init__(
        self,
        app_id: str,
        config_id: str,
        version: int,
        description: str,
        content: str,
        content_type: str,
        version_label: str,
    ):
        self.app_id = app_id
        self.config_id = config_id
        self.version = version
        self.description = description
        self.content = content
        self.content_type = content_type
        self.version_label = version_label

    def get_headers(self) -> dict[str, Any]:
        return {
            "application-id": self.app_id,
            "configuration-profile-id": self.config_id,
            "version-number": self.version,
            "description": self.description,
            "content-type": self.content_type,
            "VersionLabel": self.version_label,
        }


class ConfigurationProfile(BaseModel):
    def __init__(
        self,
        application_id: str,
        name: str,
        region: str,
        account_id: str,
        description: str,
        location_uri: str,
        retrieval_role_arn: str,
        validators: list[dict[str, str]],
        _type: str,
    ):
        self.id = mock_random.get_random_hex(7)
        self.arn = f"arn:{get_partition(region)}:appconfig:{region}:{account_id}:application/{application_id}/configurationprofile/{self.id}"
        self.application_id = application_id
        self.name = name
        self.description = description
        self.location_uri = location_uri
        self.retrieval_role_arn = retrieval_role_arn
        self.validators = validators
        self._type = _type
        self.config_versions: dict[int, HostedConfigurationVersion] = {}

    def create_version(
        self,
        app_id: str,
        config_id: str,
        description: str,
        content: str,
        content_type: str,
        version_label: str,
    ) -> HostedConfigurationVersion:
        if self.config_versions:
            version = sorted(self.config_versions.keys())[-1] + 1
        else:
            version = 1
        self.config_versions[version] = HostedConfigurationVersion(
            app_id=app_id,
            config_id=config_id,
            version=version,
            description=description,
            content=content,
            content_type=content_type,
            version_label=version_label,
        )
        return self.config_versions[version]

    def get_version(self, version: int) -> HostedConfigurationVersion:
        if version not in self.config_versions:
            raise ConfigurationVersionNotFound
        return self.config_versions[version]

    def delete_version(self, version: int) -> None:
        self.config_versions.pop(version)

    def to_json(self) -> dict[str, Any]:
        return {
            "Id": self.id,
            "Name": self.name,
            "ApplicationId": self.application_id,
            "Description": self.description,
            "LocationUri": self.location_uri,
            "RetrievalRoleArn": self.retrieval_role_arn,
            "Validators": self.validators,
            "Type": self._type,
        }


class Application(BaseModel):
    def __init__(
        self, name: str, description: Optional[str], region: str, account_id: str
    ):
        self.id = mock_random.get_random_hex(7)
        self.arn = f"arn:{get_partition(region)}:appconfig:{region}:{account_id}:application/{self.id}"
        self.name = name
        self.description = description

        self.config_profiles: dict[str, ConfigurationProfile] = {}

    def to_json(self) -> dict[str, Any]:
        return {
            "Id": self.id,
            "Name": self.name,
            "Description": self.description,
        }


class AppConfigBackend(BaseBackend):
    """Implementation of AppConfig APIs."""

    def __init__(self, region_name: str, account_id: str):
        super().__init__(region_name, account_id)
        self.applications: dict[str, Application] = {}
        self.tagger = TaggingService()

    def create_application(
        self, name: str, description: Optional[str], tags: dict[str, str]
    ) -> Application:
        app = Application(
            name, description, region=self.region_name, account_id=self.account_id
        )
        self.applications[app.id] = app
        self.tag_resource(app.arn, tags)
        return app

    def delete_application(self, app_id: str) -> None:
        self.applications.pop(app_id, None)

    def get_application(self, app_id: str) -> Application:
        if app_id not in self.applications:
            raise AppNotFoundException
        return self.applications[app_id]

    def update_application(
        self, application_id: str, name: str, description: str
    ) -> Application:
        app = self.get_application(application_id)
        if name is not None:
            app.name = name
        if description is not None:
            app.description = description
        return app

    def create_configuration_profile(
        self,
        application_id: str,
        name: str,
        description: str,
        location_uri: str,
        retrieval_role_arn: str,
        validators: list[dict[str, str]],
        _type: str,
        tags: dict[str, str],
    ) -> ConfigurationProfile:
        config_profile = ConfigurationProfile(
            application_id=application_id,
            name=name,
            region=self.region_name,
            account_id=self.account_id,
            description=description,
            location_uri=location_uri,
            retrieval_role_arn=retrieval_role_arn,
            validators=validators,
            _type=_type,
        )
        self.tag_resource(config_profile.arn, tags)
        self.get_application(application_id).config_profiles[config_profile.id] = (
            config_profile
        )
        return config_profile

    def delete_configuration_profile(self, app_id: str, config_profile_id: str) -> None:
        self.get_application(app_id).config_profiles.pop(config_profile_id)

    def get_configuration_profile(
        self, app_id: str, config_profile_id: str
    ) -> ConfigurationProfile:
        app = self.get_application(app_id)
        if config_profile_id not in app.config_profiles:
            raise ConfigurationProfileNotFound
        return app.config_profiles[config_profile_id]

    def update_configuration_profile(
        self,
        application_id: str,
        config_profile_id: str,
        name: str,
        description: str,
        retrieval_role_arn: str,
        validators: list[dict[str, str]],
    ) -> ConfigurationProfile:
        config_profile = self.get_configuration_profile(
            application_id, config_profile_id
        )
        if name is not None:
            config_profile.name = name
        if description is not None:
            config_profile.description = description
        if retrieval_role_arn is not None:
            config_profile.retrieval_role_arn = retrieval_role_arn
        if validators is not None:
            config_profile.validators = validators
        return config_profile

    def list_configuration_profiles(
        self, app_id: str
    ) -> Iterable[ConfigurationProfile]:
        app = self.get_application(app_id)
        return app.config_profiles.values()

    def create_hosted_configuration_version(
        self,
        app_id: str,
        config_profile_id: str,
        description: str,
        content: str,
        content_type: str,
        version_label: str,
    ) -> HostedConfigurationVersion:
        """
        The LatestVersionNumber-parameter is not yet implemented
        """
        profile = self.get_configuration_profile(app_id, config_profile_id)
        return profile.create_version(
            app_id=app_id,
            config_id=config_profile_id,
            description=description,
            content=content,
            content_type=content_type,
            version_label=version_label,
        )

    def get_hosted_configuration_version(
        self, app_id: str, config_profile_id: str, version: int
    ) -> HostedConfigurationVersion:
        profile = self.get_configuration_profile(
            app_id=app_id, config_profile_id=config_profile_id
        )
        return profile.get_version(version)

    def delete_hosted_configuration_version(
        self, app_id: str, config_profile_id: str, version: int
    ) -> None:
        profile = self.get_configuration_profile(
            app_id=app_id, config_profile_id=config_profile_id
        )
        profile.delete_version(version=version)

    def list_tags_for_resource(self, arn: str) -> dict[str, str]:
        return self.tagger.get_tag_dict_for_resource(arn)

    def tag_resource(self, arn: str, tags: dict[str, str]) -> None:
        self.tagger.tag_resource(arn, TaggingService.convert_dict_to_tags_input(tags))

    def untag_resource(self, arn: str, tag_keys: list[str]) -> None:
        self.tagger.untag_resource_using_names(arn, tag_keys)


appconfig_backends = BackendDict(AppConfigBackend, "appconfig")