Debian Codegraph @

ansible_collections/community/aws/plugins/modules/codecommit_repository.py

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright: (c) 2018, Shuang Wang <[email protected]>
5 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
6
7 DOCUMENTATION = r"""
8 ---
9 module: codecommit_repository
10 version_added: 1.0.0
11 short_description: Manage repositories in AWS CodeCommit
12 description:
13 - Supports creation and deletion of CodeCommit repositories.
14 - See U(https://aws.amazon.com/codecommit/) for more information about CodeCommit.
15 - Prior to release 5.0.0 this module was called C(community.aws.aws_codecommit).
16 The usage did not change.
17 author:
18 - Shuang Wang (@ptux)
19 options:
20 name:
21 description:
22 - Name of repository.
23 required: true
24 type: str
25 description:
26 description:
27 - Description or comment of repository.
28 required: false
29 aliases:
30 - comment
31 type: str
32 default: ''
33 state:
34 description:
35 - Specifies the state of repository.
36 required: true
37 choices: [ 'present', 'absent' ]
38 type: str
39 extends_documentation_fragment:
40 - amazon.aws.common.modules
41 - amazon.aws.region.modules
42 - amazon.aws.boto3
43 """
44
45 RETURN = r"""
46 repository_metadata:
47 description: "Information about the repository."
48 returned: always
49 type: complex
50 contains:
51 account_id:
52 description: "The ID of the AWS account associated with the repository."
53 returned: when state is present
54 type: str
55 sample: "123456789012"
56 arn:
57 description: "The Amazon Resource Name (ARN) of the repository."
58 returned: when state is present
59 type: str
60 sample: "arn:aws:codecommit:ap-northeast-1:123456789012:username"
61 clone_url_http:
62 description: "The URL to use for cloning the repository over HTTPS."
63 returned: when state is present
64 type: str
65 sample: "https://git-codecommit.ap-northeast-1.amazonaws.com/v1/repos/reponame"
66 clone_url_ssh:
67 description: "The URL to use for cloning the repository over SSH."
68 returned: when state is present
69 type: str
70 sample: "ssh://git-codecommit.ap-northeast-1.amazonaws.com/v1/repos/reponame"
71 creation_date:
72 description: "The date and time the repository was created, in timestamp format."
73 returned: when state is present
74 type: str
75 sample: "2018-10-16T13:21:41.261000+09:00"
76 last_modified_date:
77 description: "The date and time the repository was last modified, in timestamp format."
78 returned: when state is present
79 type: str
80 sample: "2018-10-16T13:21:41.261000+09:00"
81 repository_description:
82 description: "A comment or description about the repository."
83 returned: when state is present
84 type: str
85 sample: "test from ptux"
86 repository_id:
87 description: "The ID of the repository that was created or deleted"
88 returned: always
89 type: str
90 sample: "e62a5c54-i879-497b-b62f-9f99e4ebfk8e"
91 repository_name:
92 description: "The repository's name."
93 returned: when state is present
94 type: str
95 sample: "reponame"
96
97 response_metadata:
98 description: "Information about the response."
99 returned: always
100 type: complex
101 contains:
102 http_headers:
103 description: "http headers of http response"
104 returned: always
105 type: dict
106 http_status_code:
107 description: "http status code of http response"
108 returned: always
109 type: str
110 sample: "200"
111 request_id:
112 description: "http request id"
113 returned: always
114 type: str
115 sample: "fb49cfca-d0fa-11e8-85cb-b3cc4b5045ef"
116 retry_attempts:
117 description: "numbers of retry attempts"
118 returned: always
119 type: str
120 sample: "0"
121 """
122
123 EXAMPLES = r"""
124 # Create a new repository
125 - community.aws.codecommit_repository:
126 name: repo
127 state: present
128
129 # Delete a repository
130 - community.aws.codecommit_repository:
131 name: repo
132 state: absent
133 """
134
135 try:
136 import botocore
137 except ImportError:
138 pass # Handled by AnsibleAWSModule
139
140 from ansible.module_utils.common.dict_transformations import camel_dict_to_snake_dict
141
142 from ansible_collections.community.aws.plugins.module_utils.modules import AnsibleCommunityAWSModule as AnsibleAWSModule
143
144
145 class CodeCommit(object):
146 def __init__(self, module=None):
147 self._module = module
148 self._client = self._module.client("codecommit")
149 self._check_mode = self._module.check_mode
150
151 def process(self):
152 result = dict(changed=False)
153
154 if self._module.params["state"] == "present":
155 if not self._repository_exists():
156 if not self._check_mode:
157 result = self._create_repository()
158 result["changed"] = True
159 else:
160 metadata = self._get_repository()["repositoryMetadata"]
161 if not metadata.get("repositoryDescription"):
162 metadata["repositoryDescription"] = ""
163 if metadata["repositoryDescription"] != self._module.params["description"]:
164 if not self._check_mode:
165 self._update_repository()
166 result["changed"] = True
167 result.update(self._get_repository())
168 if self._module.params["state"] == "absent" and self._repository_exists():
169 if not self._check_mode:
170 result = self._delete_repository()
171 result["changed"] = True
172 return result
173
174 def _repository_exists(self):
175 try:
176 paginator = self._client.get_paginator("list_repositories")
177 for page in paginator.paginate():
178 repositories = page["repositories"]
179 for item in repositories:
180 if self._module.params["name"] in item.values():
181 return True
182 except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
183 self._module.fail_json_aws(e, msg="couldn't get repository")
184 return False
185
186 def _get_repository(self):
187 try:
188 result = self._client.get_repository(
189 repositoryName=self._module.params["name"],
190 )
191 except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
192 self._module.fail_json_aws(e, msg="couldn't get repository")
193 return result
194
195 def _update_repository(self):
196 try:
197 result = self._client.update_repository_description(
198 repositoryName=self._module.params["name"],
199 repositoryDescription=self._module.params["description"],
200 )
201 except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
202 self._module.fail_json_aws(e, msg="couldn't create repository")
203 return result
204
205 def _create_repository(self):
206 try:
207 result = self._client.create_repository(
208 repositoryName=self._module.params["name"],
209 repositoryDescription=self._module.params["description"],
210 )
211 except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
212 self._module.fail_json_aws(e, msg="couldn't create repository")
213 return result
214
215 def _delete_repository(self):
216 try:
217 result = self._client.delete_repository(
218 repositoryName=self._module.params["name"],
219 )
220 except (botocore.exceptions.ClientError, botocore.exceptions.BotoCoreError) as e:
221 self._module.fail_json_aws(e, msg="couldn't delete repository")
222 return result
223
224
225 def main():
226 argument_spec = dict(
227 name=dict(required=True),
228 state=dict(choices=["present", "absent"], required=True),
229 description=dict(default="", aliases=["comment"]),
230 )
231
232 ansible_aws_module = AnsibleAWSModule(
233 argument_spec=argument_spec,
234 supports_check_mode=True,
235 )
236
237 aws_codecommit = CodeCommit(module=ansible_aws_module)
238 result = aws_codecommit.process()
239 ansible_aws_module.exit_json(**camel_dict_to_snake_dict(result))
240
241
242 if __name__ == "__main__":
243 main()