Debian Codegraph @

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

raw
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 # Copyright (c) 2017 Ansible Project
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: s3_cors
10 version_added: 1.0.0
11 short_description: Manage CORS for S3 buckets in AWS
12 description:
13 - Manage CORS for S3 buckets in AWS.
14 - Prior to release 5.0.0 this module was called C(community.aws.aws_s3_cors).
15 The usage did not change.
16 author:
17 - "Oyvind Saltvik (@fivethreeo)"
18 options:
19 name:
20 description:
21 - Name of the S3 bucket.
22 required: true
23 type: str
24 rules:
25 description:
26 - Cors rules to put on the S3 bucket.
27 type: list
28 elements: dict
29 state:
30 description:
31 - Create or remove cors on the S3 bucket.
32 required: true
33 choices: [ 'present', 'absent' ]
34 type: str
35 extends_documentation_fragment:
36 - amazon.aws.common.modules
37 - amazon.aws.region.modules
38 - amazon.aws.boto3
39 """
40
41 EXAMPLES = r"""
42 # Note: These examples do not set authentication details, see the AWS Guide for details.
43
44 # Create a simple cors for s3 bucket
45 - community.aws.s3_cors:
46 name: mys3bucket
47 state: present
48 rules:
49 - allowed_origins:
50 - http://www.example.com/
51 allowed_methods:
52 - GET
53 - POST
54 allowed_headers:
55 - Authorization
56 expose_headers:
57 - x-amz-server-side-encryption
58 - x-amz-request-id
59 max_age_seconds: 30000
60
61 # Remove cors for s3 bucket
62 - community.aws.s3_cors:
63 name: mys3bucket
64 state: absent
65 """
66
67 RETURN = r"""
68 changed:
69 description: check to see if a change was made to the rules
70 returned: always
71 type: bool
72 sample: true
73 name:
74 description: name of bucket
75 returned: always
76 type: str
77 sample: 'bucket-name'
78 rules:
79 description: list of current rules
80 returned: always
81 type: list
82 sample: [
83 {
84 "allowed_headers": [
85 "Authorization"
86 ],
87 "allowed_methods": [
88 "GET"
89 ],
90 "allowed_origins": [
91 "*"
92 ],
93 "max_age_seconds": 30000
94 }
95 ]
96 """
97
98 try:
99 from botocore.exceptions import BotoCoreError
100 from botocore.exceptions import ClientError
101 except ImportError:
102 pass # Handled by AnsibleAWSModule
103
104 from ansible.module_utils.common.dict_transformations import snake_dict_to_camel_dict
105
106 from ansible_collections.amazon.aws.plugins.module_utils.policy import compare_policies
107
108 from ansible_collections.community.aws.plugins.module_utils.modules import AnsibleCommunityAWSModule as AnsibleAWSModule
109
110
111 def create_or_update_bucket_cors(connection, module):
112 name = module.params.get("name")
113 rules = module.params.get("rules", [])
114 changed = False
115
116 try:
117 current_camel_rules = connection.get_bucket_cors(Bucket=name)["CORSRules"]
118 except ClientError:
119 current_camel_rules = []
120
121 new_camel_rules = snake_dict_to_camel_dict(rules, capitalize_first=True)
122 # compare_policies() takes two dicts and makes them hashable for comparison
123 if compare_policies(new_camel_rules, current_camel_rules):
124 changed = True
125
126 if changed:
127 try:
128 connection.put_bucket_cors(Bucket=name, CORSConfiguration={"CORSRules": new_camel_rules})
129 except (BotoCoreError, ClientError) as e:
130 module.fail_json_aws(e, msg=f"Unable to update CORS for bucket {name}")
131
132 module.exit_json(changed=changed, name=name, rules=rules)
133
134
135 def destroy_bucket_cors(connection, module):
136 name = module.params.get("name")
137 changed = False
138
139 try:
140 connection.delete_bucket_cors(Bucket=name)
141 changed = True
142 except (BotoCoreError, ClientError) as e:
143 module.fail_json_aws(e, msg=f"Unable to delete CORS for bucket {name}")
144
145 module.exit_json(changed=changed)
146
147
148 def main():
149 argument_spec = dict(
150 name=dict(required=True, type="str"),
151 rules=dict(type="list", elements="dict"),
152 state=dict(type="str", choices=["present", "absent"], required=True),
153 )
154
155 module = AnsibleAWSModule(argument_spec=argument_spec)
156
157 client = module.client("s3")
158
159 state = module.params.get("state")
160
161 if state == "present":
162 create_or_update_bucket_cors(client, module)
163 elif state == "absent":
164 destroy_bucket_cors(client, module)
165
166
167 if __name__ == "__main__":
168 main()