summaryrefslogtreecommitdiff
path: root/lib/ansible/modules/cloud/azure/azure_rm_cdnprofile_info.py
blob: 3de696727bec7391b1d7287d1e3ec16cec03bb76 (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
#!/usr/bin/python
#
# Copyright (c) 2018 Hai Cao, <t-haicao@microsoft.com>, Yunge Zhu <yungez@microsoft.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: azure_rm_cdnprofile_info

version_added: "2.9"

short_description: Get Azure CDN profile facts

description:
    - Get facts for a specific Azure CDN profile or all CDN profiles.

options:
    name:
        description:
            - Limit results to a specific CDN profile.
    resource_group:
        description:
            - The resource group to search for the desired CDN profile.
    tags:
        description:
            - Limit results by providing a list of tags. Format tags as 'key' or 'key:value'.

extends_documentation_fragment:
    - azure

author:
    - Hai Cao (@caohai)
    - Yunge Zhu (@yungezz)
'''

EXAMPLES = '''
    - name: Get facts for one CDN profile
      azure_rm_cdnprofile_info:
        name: Testing
        resource_group: myResourceGroup

    - name: Get facts for all CDN profiles
      azure_rm_cdnprofile_info:

    - name: Get facts by tags
      azure_rm_cdnprofile_info:
        tags:
          - Environment:Test
'''

RETURN = '''
cdnprofiles:
    description: List of CDN profiles.
    returned: always
    type: complex
    contains:
        resource_group:
            description:
                - Name of a resource group where the CDN profile exists.
            returned: always
            type: str
            sample: myResourceGroup
        name:
            description:
                - Name of the CDN profile.
            returned: always
            type: str
            sample: Testing
        location:
            description:
                - Location of the CDN profile.
            type: str
            sample: WestUS
        id:
            description:
                - ID of the CDN profile.
            type: str
            sample: /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourcegroups/myResourceGroup/providers/Microsoft.Cdn/profiles/myCDN
        provisioning_state:
            description:
                - Provisioning status of the profile.
            type: str
            sample: Succeeded
        resource_state:
            description:
                - Resource status of the profile.
            type: str
            sample: Active
        sku:
            description:
                - The pricing tier, defines a CDN provider, feature list and rate of the CDN profile.
            type: str
            sample: standard_verizon
        type:
            description:
                - The type of the CDN profile.
            type: str
            sample: Microsoft.Cdn/profiles
        tags:
            description:
                - The tags of the CDN profile.
            type: list
            sample: [
                {"foo": "bar"}
            ]
'''

from ansible.module_utils.azure_rm_common import AzureRMModuleBase

try:
    from azure.mgmt.cdn.models import ErrorResponseException
    from azure.common import AzureHttpError
    from azure.mgmt.cdn import CdnManagementClient
except Exception:
    # handled in azure_rm_common
    pass

import re

AZURE_OBJECT_CLASS = 'profiles'


class AzureRMCdnprofileInfo(AzureRMModuleBase):
    """Utility class to get Azure CDN profile facts"""

    def __init__(self):

        self.module_args = dict(
            name=dict(type='str'),
            resource_group=dict(type='str'),
            tags=dict(type='list')
        )

        self.results = dict(
            changed=False,
            cdnprofiles=[]
        )

        self.name = None
        self.resource_group = None
        self.tags = None
        self.cdn_client = None

        super(AzureRMCdnprofileInfo, self).__init__(
            derived_arg_spec=self.module_args,
            supports_tags=False,
            facts_module=True
        )

    def exec_module(self, **kwargs):

        is_old_facts = self.module._name == 'azure_rm_cdnprofile_facts'
        if is_old_facts:
            self.module.deprecate("The 'azure_rm_cdnprofile_facts' module has been renamed to 'azure_rm_cdnprofile_info'", version='2.13')

        for key in self.module_args:
            setattr(self, key, kwargs[key])

        self.cdn_client = self.get_cdn_client()

        if self.name and not self.resource_group:
            self.fail("Parameter error: resource group required when filtering by name.")

        if self.name:
            self.results['cdnprofiles'] = self.get_item()
        elif self.resource_group:
            self.results['cdnprofiles'] = self.list_resource_group()
        else:
            self.results['cdnprofiles'] = self.list_all()

        return self.results

    def get_item(self):
        """Get a single Azure CDN profile"""

        self.log('Get properties for {0}'.format(self.name))

        item = None
        result = []

        try:
            item = self.cdn_client.profiles.get(
                self.resource_group, self.name)
        except ErrorResponseException:
            pass

        if item and self.has_tags(item.tags, self.tags):
            result = [self.serialize_cdnprofile(item)]

        return result

    def list_resource_group(self):
        """Get all Azure CDN profiles within a resource group"""

        self.log('List all Azure CDNs within a resource group')

        try:
            response = self.cdn_client.profiles.list_by_resource_group(
                self.resource_group)
        except AzureHttpError as exc:
            self.fail('Failed to list all items - {0}'.format(str(exc)))

        results = []
        for item in response:
            if self.has_tags(item.tags, self.tags):
                results.append(self.serialize_cdnprofile(item))

        return results

    def list_all(self):
        """Get all Azure CDN profiles within a subscription"""
        self.log('List all CDN profiles within a subscription')
        try:
            response = self.cdn_client.profiles.list()
        except Exception as exc:
            self.fail("Error listing all items - {0}".format(str(exc)))

        results = []
        for item in response:
            if self.has_tags(item.tags, self.tags):
                results.append(self.serialize_cdnprofile(item))
        return results

    def serialize_cdnprofile(self, cdnprofile):
        '''
        Convert a CDN profile object to dict.
        :param cdn: CDN profile object
        :return: dict
        '''
        result = self.serialize_obj(cdnprofile, AZURE_OBJECT_CLASS)

        new_result = {}
        new_result['id'] = cdnprofile.id
        new_result['resource_group'] = re.sub('\\/.*', '', re.sub('.*resourcegroups\\/', '', result['id']))
        new_result['name'] = cdnprofile.name
        new_result['type'] = cdnprofile.type
        new_result['location'] = cdnprofile.location
        new_result['resource_state'] = cdnprofile.resource_state
        new_result['sku'] = cdnprofile.sku.name
        new_result['provisioning_state'] = cdnprofile.provisioning_state
        new_result['tags'] = cdnprofile.tags
        return new_result

    def get_cdn_client(self):
        if not self.cdn_client:
            self.cdn_client = self.get_mgmt_svc_client(CdnManagementClient,
                                                       base_url=self._cloud_environment.endpoints.resource_manager,
                                                       api_version='2017-04-02')
        return self.cdn_client


def main():
    """Main module execution code path"""

    AzureRMCdnprofileInfo()


if __name__ == '__main__':
    main()