summaryrefslogtreecommitdiff
path: root/lib/ansible/modules/cloud/digital_ocean/digital_ocean_image_facts.py
blob: cec527d3e7a72a496c6011780cf031ecb3136edf (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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Copyright: (c) 2018, Ansible Project
# Copyright: (c) 2018, Abhijeet Kasurde <akasurde@redhat.com>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

from __future__ import absolute_import, division, print_function
__metaclass__ = type


ANSIBLE_METADATA = {
    'metadata_version': '1.1',
    'status': ['preview'],
    'supported_by': 'community'
}


DOCUMENTATION = '''
---
module: digital_ocean_image_facts
short_description: Gather facts about DigitalOcean images
description:
    - This module can be used to gather facts about DigitalOcean provided images.
    - These images can be either of type C(distribution), C(application) and C(private).
author: "Abhijeet Kasurde (@Akasurde)"
version_added: "2.6"
options:
  image_type:
    description:
     - Specifies the type of image facts to be retrived.
     - If set to C(application), then facts are gathered related to all application images.
     - If set to C(distribution), then facts are gathered related to all distribution images.
     - If set to C(private), then facts are gathered related to all private images.
     - If not set to any of above, then facts are gathered related to all images.
    default: 'all'
    choices: [ 'all', 'application', 'distribution', 'private' ]
    required: false
requirements:
  - "python >= 2.6"
extends_documentation_fragment: digital_ocean.documentation
'''


EXAMPLES = '''
- name: Gather facts about all images
  digital_ocean_image_facts:
    image_type: all
    oauth_token: "{{ oauth_token }}"

- name: Gather facts about application images
  digital_ocean_image_facts:
    image_type: application
    oauth_token: "{{ oauth_token }}"

- name: Gather facts about distribution images
  digital_ocean_image_facts:
    image_type: distribution
    oauth_token: "{{ oauth_token }}"

- name: Get distribution about image with slug coreos-beta
  digital_ocean_image_facts:
  register: resp_out
- set_fact:
    distribution_name: "{{ item.distribution }}"
  loop: "{{ resp_out.data|json_query(name) }}"
  vars:
    name: "[?slug=='coreos-beta']"
- debug: var=distribution_name

'''


RETURN = '''
data:
    description: DigitalOcean image facts
    returned: success
    type: list
    sample: [
        {
            "created_at": "2018-02-02T07:11:43Z",
            "distribution": "CoreOS",
            "id": 31434061,
            "min_disk_size": 20,
            "name": "1662.1.0 (beta)",
            "public": true,
            "regions": [
                "nyc1",
                "sfo1",
                "nyc2",
                "ams2",
                "sgp1",
                "lon1",
                "nyc3",
                "ams3",
                "fra1",
                "tor1",
                "sfo2",
                "blr1"
            ],
            "size_gigabytes": 0.42,
            "slug": "coreos-beta",
            "type": "snapshot"
        },
    ]
'''

from traceback import format_exc
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.digital_ocean import DigitalOceanHelper
from ansible.module_utils._text import to_native


def core(module):
    image_type = module.params['image_type']

    rest = DigitalOceanHelper(module)

    base_url = 'images?'
    if image_type == 'distribution':
        base_url += "type=distribution&"
    elif image_type == 'application':
        base_url += "type=application&"
    elif image_type == 'private':
        base_url += "private=true&"

    images = rest.get_paginated_data(base_url=base_url, data_key_name='images')

    module.exit_json(changed=False, data=images)


def main():
    argument_spec = DigitalOceanHelper.digital_ocean_argument_spec()
    argument_spec.update(
        image_type=dict(type='str',
                        required=False,
                        choices=['all', 'application', 'distribution', 'private'],
                        default='all'
                        )
    )

    module = AnsibleModule(argument_spec=argument_spec)

    try:
        core(module)
    except Exception as e:
        module.fail_json(msg=to_native(e), exception=format_exc())


if __name__ == '__main__':
    main()