summaryrefslogtreecommitdiffstats
path: root/ansible_collections/openstack/cloud/plugins/modules/object.py
blob: e5b930f819c3f915f79217a63625f40d002b3761 (plain)
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
#!/usr/bin/python
# -*- coding: utf-8 -*-

# Copyright (c) 2015 Hewlett-Packard Development Company, L.P.
# Copyright (c) 2013, Benno Joy <benno@ansible.com>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

DOCUMENTATION = r'''
---
module: object
short_description: Create or delete Swift objects in OpenStack clouds
author: OpenStack Ansible SIG
description:
   - Create or delete Swift objects in OpenStack clouds
options:
  container:
    description:
      - The name (and ID) of the container in which to create the object in.
      - This container will not be created if it does not exist already.
    required: true
    type: str
  data:
    description:
      - The content to upload to the object.
      - Mutually exclusive with I(filename).
      - This attribute cannot be updated.
    type: str
  filename:
    description:
      - The path to the local file whose contents will be uploaded.
      - Mutually exclusive with I(data).
    type: str
  name:
    description:
      - Name (and ID) of the object.
    required: true
    type: str
  state:
    description:
      - Whether the object should be C(present) or C(absent).
    choices: ['present', 'absent']
    default: present
    type: str
extends_documentation_fragment:
  - openstack.cloud.openstack
'''

RETURN = r'''
object:
  description: Dictionary describing the object.
  returned: On success when I(state) is C(present).
  type: dict
  contains:
    accept_ranges:
      description: The type of ranges that the object accepts.
      type: str
    access_control_allow_origin:
      description: CORS for RAX (deviating from standard)
      type: str
    content_disposition:
      description: If set, specifies the override behavior for the browser.
                   For example, this header might specify that the browser use
                   a download program to save this file rather than show the
                   file, which is the default. If not set, this header is not
                   returned by this operation.
      type: str
    content_encoding:
      description: If set, the value of the Content-Encoding metadata.
                   If not set, this header is not returned by this operation.
      type: str
    content_length:
      description: HEAD operations do not return content. However, in this
                   operation the value in the Content-Length header is not the
                   size of the response body. Instead it contains the size of
                   the object, in bytes.
      type: str
    content_type:
      description: The MIME type of the object.
      type: int
    copy_from:
      description: If set, this is the name of an object used to create the new
                   object by copying the X-Copy-From object. The value is in
                   form {container}/{object}. You must UTF-8-encode and then
                   URL-encode the names of the container and object before you
                   include them in the header. Using PUT with X-Copy-From has
                   the same effect as using the COPY operation to copy an
                   object.
      type: str
    delete_after:
      description: Specifies the number of seconds after which the object is
                   removed. Internally, the Object Storage system stores this
                   value in the X-Delete-At metadata item.
      type: int
    delete_at:
      description: If set, the time when the object will be deleted by the
                   system in the format of a UNIX Epoch timestamp. If not set,
                   this header is not returned by this operation.
      type: str
    etag:
      description: For objects smaller than 5 GB, this value is the MD5
                   checksum of the object content. The value is not quoted.
                   For manifest objects, this value is the MD5 checksum of the
                   concatenated string of MD5 checksums and ETags for each of
                   the segments in the manifest, and not the MD5 checksum of
                   the content that was downloaded. Also the value is enclosed
                   in double-quote characters.
                   You are strongly recommended to compute the MD5 checksum of
                   the response body as it is received and compare this value
                   with the one in the ETag header. If they differ, the content
                   was corrupted, so retry the operation.
      type: str
    expires_at:
      description: Used with temporary URLs to specify the expiry time of the
                   signature. For more information about temporary URLs, see
                   OpenStack Object Storage API v1 Reference.
      type: str
    id:
      description: ID of the object. Equal to C(name).
      type: str
    if_match:
      description: See U(http://www.ietf.org/rfc/rfc2616.txt).
      type: list
    if_modified_since:
      description: See U(http://www.ietf.org/rfc/rfc2616.txt).
      type: str
    if_none_match:
      description: "In combination with C(Expect: 100-Continue), specify an
                    C(If-None-Match: *) header to query whether the server
                    already has a copy of the object before any data is sent."
      type: list
    if_unmodified_since:
      description: See U(http://www.ietf.org/rfc/rfc2616.txt).
      type: str
    is_content_type_detected:
      description: If set to true, Object Storage guesses the content type
                   based on the file extension and ignores the value sent in
                   the Content-Type header, if present.
      type: bool
    is_newest:
      description: If set to True, Object Storage queries all replicas to
                   return the most recent one. If you omit this header, Object
                   Storage responds faster after it finds one valid replica.
                   Because setting this header to True is more expensive for
                   the back end, use it only when it is absolutely needed.
      type: bool
    is_static_large_object:
      description: Set to True if this object is a static large object manifest
                   object.
      type: bool
    last_modified_at:
      description: The date and time that the object was created or the last
                   time that the metadata was changed.
      type: str
    manifest:
      description: If present, this is a dynamic large object manifest object.
                   The value is the container and object name prefix of the
                   segment objects in the form container/prefix.
      type: str
    multipart_manifest:
      description: If you include the multipart-manifest=get query parameter
                   and the object is a large object, the object contents are
                   not returned. Instead, the manifest is returned in the
                   X-Object-Manifest response header for dynamic large objects
                   or in the response body for static large objects.
      type: str
    name:
      description: Name of the object.
      returned: success
      type: str
    object_manifest:
      description: If set, to this is a dynamic large object manifest object.
                   The value is the container and object name prefix of the
                   segment objects in the form container/prefix.
      type: str
    range:
      description: TODO.
      type: dict
    signature:
      description: Used with temporary URLs to sign the request. For more
                   information about temporary URLs, see OpenStack Object
                   Storage API v1 Reference.
      type: str
    symlink_target:
      description: If present, this is a symlink object. The value is the
                   relative path of the target object in the format
                   <container>/<object>.
      type: str
    symlink_target_account:
      description: If present, and X-Symlink-Target is present, then this is a
                   cross-account symlink to an object in the account specified
                   in the value.
      type: str
    timestamp:
      description: The timestamp of the transaction.
      type: str
    transfer_encoding:
      description: Set to chunked to enable chunked transfer encoding. If used,
                   do not set the Content-Length header to a non-zero value.
      type: str
'''

EXAMPLES = r'''
- name: Create a object named 'fstab' in the 'config' container
  openstack.cloud.object:
    cloud: mordred
    container: config
    filename: /etc/fstab
    name: fstab
    state: present

- name: Delete a container called config and all of its contents
  openstack.cloud.object:
    cloud: rax-iad
    container: config
    state: absent
'''

from ansible_collections.openstack.cloud.plugins.module_utils.openstack import OpenStackModule


class ObjectModule(OpenStackModule):
    argument_spec = dict(
        container=dict(required=True),
        data=dict(),
        filename=dict(),
        name=dict(required=True),
        state=dict(default='present', choices=['absent', 'present']),
    )

    module_kwargs = dict(
        mutually_exclusive=[
            ('data', 'filename'),
        ],
        required_if=[
            ('state', 'present', ('data', 'filename'), True),
        ],
        supports_check_mode=True
    )

    def run(self):
        state = self.params['state']
        object = self._find()

        if self.ansible.check_mode:
            self.exit_json(changed=self._will_change(state, object))

        if state == 'present' and not object:
            # Create object
            object = self._create()
            self.exit_json(changed=True,
                           # metadata is not returned by
                           # to_dict(computed=False) so return it explicitly
                           object=dict(metadata=object.metadata,
                                       **object.to_dict(computed=False)))

        elif state == 'present' and object:
            # Update object
            update = self._build_update(object)
            if update:
                object = self._update(object, update)

            self.exit_json(changed=bool(update),
                           # metadata is not returned by
                           # to_dict(computed=False) so return it explicitly
                           object=dict(metadata=object.metadata,
                                       **object.to_dict(computed=False)))

        elif state == 'absent' and object:
            # Delete object
            self._delete(object)
            self.exit_json(changed=True)

        elif state == 'absent' and not object:
            # Do nothing
            self.exit_json(changed=False)

    def _build_update(self, object):
        update = {}

        container_name = self.params['container']

        filename = self.params['filename']
        if filename is not None:
            if self.conn.object_store.is_object_stale(container_name,
                                                      object.id, filename):
                update['filename'] = filename

        return update

    def _create(self):
        name = self.params['name']
        container_name = self.params['container']

        kwargs = dict((k, self.params[k])
                      for k in ['data', 'filename']
                      if self.params[k] is not None)

        return self.conn.object_store.create_object(container_name, name,
                                                    **kwargs)

    def _delete(self, object):
        container_name = self.params['container']
        self.conn.object_store.delete_object(object.id,
                                             container=container_name)

    def _find(self):
        name_or_id = self.params['name']
        container_name = self.params['container']
        # openstacksdk has no object_store.find_object() function
        try:
            return self.conn.object_store.get_object(name_or_id,
                                                     container=container_name)
        except self.sdk.exceptions.ResourceNotFound:
            return None

    def _update(self, object, update):
        filename = update.get('filename')
        if filename:
            container_name = self.params['container']
            object = self.conn.object_store.create_object(container_name,
                                                          object.id,
                                                          filename=filename)

        return object

    def _will_change(self, state, object):
        if state == 'present' and not object:
            return True
        elif state == 'present' and object:
            return bool(self._build_update(object))
        elif state == 'absent' and object:
            return True
        else:
            # state == 'absent' and not object:
            return False


def main():
    module = ObjectModule()
    module()


if __name__ == "__main__":
    main()