| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
from __future__ import absolute_import, division, print_function |
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
DOCUMENTATION = ''' |
| 11 |
--- |
| 12 |
module: azure_rm_hostgroup |
| 13 |
|
| 14 |
version_added: "1.10.0" |
| 15 |
|
| 16 |
short_description: Create, delete and update a dedicated host group |
| 17 |
|
| 18 |
description: |
| 19 |
- Creates, deletes, and updates a dedicated host group. |
| 20 |
|
| 21 |
options: |
| 22 |
resource_group: |
| 23 |
description: |
| 24 |
- Name of resource group. |
| 25 |
required: true |
| 26 |
type: str |
| 27 |
name: |
| 28 |
description: |
| 29 |
- The name of the dedicated host group. |
| 30 |
required: true |
| 31 |
type: str |
| 32 |
location: |
| 33 |
description: |
| 34 |
- Valid Azure location for host group. Defaults to location of resource group. |
| 35 |
type: str |
| 36 |
platform_fault_domain_count: |
| 37 |
description: |
| 38 |
- Number of fault domains that the host group can span. |
| 39 |
type: int |
| 40 |
zones: |
| 41 |
description: |
| 42 |
- Availability Zone to use for this host group. Only single zone is supported. The zone can be assigned only |
| 43 |
during creation. If not provided, the group supports all zones in the region. |
| 44 |
type: list |
| 45 |
elements: str |
| 46 |
state: |
| 47 |
description: |
| 48 |
- Assert the state of the host group. Use C(present) to create or update and C(absent) to delete. |
| 49 |
default: present |
| 50 |
type: str |
| 51 |
choices: |
| 52 |
- absent |
| 53 |
- present |
| 54 |
|
| 55 |
extends_documentation_fragment: |
| 56 |
- azure.azcollection.azure |
| 57 |
- azure.azcollection.azure_tags |
| 58 |
|
| 59 |
author: |
| 60 |
- Aparna Patil (@aparna-patil) |
| 61 |
''' |
| 62 |
|
| 63 |
EXAMPLES = ''' |
| 64 |
- name: Create a host group |
| 65 |
azure_rm_hostgroup: |
| 66 |
resource_group: myAzureResourceGroup |
| 67 |
name: myhostgroup |
| 68 |
location: eastus |
| 69 |
zones: |
| 70 |
- "1" |
| 71 |
platform_fault_domain_count: 1 |
| 72 |
state: present |
| 73 |
|
| 74 |
- name: Update a host group |
| 75 |
azure_rm_hostgroup: |
| 76 |
resource_group: myAzureResourceGroup |
| 77 |
name: myhostgroup |
| 78 |
location: eastus |
| 79 |
zones: |
| 80 |
- "1" |
| 81 |
platform_fault_domain_count: 1 |
| 82 |
state: present |
| 83 |
tags: |
| 84 |
key1: "value1" |
| 85 |
|
| 86 |
- name: Delete a host group |
| 87 |
azure_rm_hostgroup: |
| 88 |
resource_group: myAzureResourceGroup |
| 89 |
name: myhostgroup |
| 90 |
state: absent |
| 91 |
''' |
| 92 |
|
| 93 |
RETURN = ''' |
| 94 |
state: |
| 95 |
description: |
| 96 |
- Current state of the host group. |
| 97 |
returned: always |
| 98 |
type: complex |
| 99 |
contains: |
| 100 |
id: |
| 101 |
description: |
| 102 |
- The host group ID. |
| 103 |
returned: always |
| 104 |
type: str |
| 105 |
sample: "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myAzureResourceGroup/providers/ |
| 106 |
Microsoft.Compute/hostGroups/myhostgroup" |
| 107 |
name: |
| 108 |
description: |
| 109 |
- The host group name. |
| 110 |
returned: always |
| 111 |
type: str |
| 112 |
sample: 'myhostgroup' |
| 113 |
location: |
| 114 |
description: |
| 115 |
- The Azure Region where the resource lives. |
| 116 |
returned: always |
| 117 |
type: str |
| 118 |
sample: eastus |
| 119 |
platform_fault_domain_count: |
| 120 |
description: |
| 121 |
- Number of fault domains. |
| 122 |
returned: always |
| 123 |
type: int |
| 124 |
sample: 1 |
| 125 |
zones: |
| 126 |
description: |
| 127 |
- Availability zones configured for this host group. |
| 128 |
returned: always |
| 129 |
type: list |
| 130 |
sample: ["1"] |
| 131 |
tags: |
| 132 |
description: |
| 133 |
- Resource tags. |
| 134 |
returned: always |
| 135 |
type: list |
| 136 |
sample: [{"key1": "value1"}] |
| 137 |
type: |
| 138 |
description: |
| 139 |
- The type of resource. |
| 140 |
returned: always |
| 141 |
type: str |
| 142 |
sample: Microsoft.Compute/hostGroups |
| 143 |
''' |
| 144 |
|
| 145 |
from ansible.module_utils.basic import _load_params |
| 146 |
from ansible_collections.azure.azcollection.plugins.module_utils.azure_rm_common import AzureRMModuleBase, \ |
| 147 |
normalize_location_name |
| 148 |
|
| 149 |
try: |
| 150 |
from azure.core.exceptions import ResourceNotFoundError |
| 151 |
except ImportError: |
| 152 |
|
| 153 |
pass |
| 154 |
|
| 155 |
|
| 156 |
class AzureRMHostGroup(AzureRMModuleBase): |
| 157 |
|
| 158 |
def __init__(self): |
| 159 |
|
| 160 |
_load_params() |
| 161 |
|
| 162 |
self.module_arg_spec = dict( |
| 163 |
resource_group=dict(type='str', required=True), |
| 164 |
name=dict(type='str', required=True), |
| 165 |
location=dict(type='str'), |
| 166 |
platform_fault_domain_count=dict(type='int'), |
| 167 |
zones=dict(type='list', elements='str'), |
| 168 |
state=dict(choices=['present', 'absent'], default='present', type='str') |
| 169 |
) |
| 170 |
|
| 171 |
required_if = [ |
| 172 |
('state', 'present', ['platform_fault_domain_count']) |
| 173 |
] |
| 174 |
|
| 175 |
self.results = dict( |
| 176 |
changed=False, |
| 177 |
state=dict() |
| 178 |
) |
| 179 |
|
| 180 |
self.resource_group = None |
| 181 |
self.name = None |
| 182 |
self.state = None |
| 183 |
self.location = None |
| 184 |
self.tags = None |
| 185 |
self.platform_fault_domain_count = None |
| 186 |
self.zones = None |
| 187 |
|
| 188 |
super(AzureRMHostGroup, self).__init__(self.module_arg_spec, |
| 189 |
required_if=required_if, |
| 190 |
supports_check_mode=True) |
| 191 |
|
| 192 |
def exec_module(self, **kwargs): |
| 193 |
for key in list(self.module_arg_spec.keys()) + ['tags']: |
| 194 |
setattr(self, key, kwargs[key]) |
| 195 |
|
| 196 |
changed = False |
| 197 |
results = dict() |
| 198 |
host_group = None |
| 199 |
|
| 200 |
|
| 201 |
resource_group = self.get_resource_group(self.resource_group) |
| 202 |
if not self.location: |
| 203 |
|
| 204 |
self.location = resource_group.location |
| 205 |
|
| 206 |
self.location = normalize_location_name(self.location) |
| 207 |
|
| 208 |
try: |
| 209 |
self.log('Fetching host group {0}'.format(self.name)) |
| 210 |
host_group = self.compute_client.dedicated_host_groups.get(self.resource_group, self.name) |
| 211 |
|
| 212 |
results = self.hostgroup_to_dict(host_group) |
| 213 |
if self.state == 'present': |
| 214 |
changed = False |
| 215 |
update_tags, results['tags'] = self.update_tags(results['tags']) |
| 216 |
if update_tags: |
| 217 |
changed = True |
| 218 |
self.tags = results['tags'] |
| 219 |
if self.platform_fault_domain_count != results['platform_fault_domain_count']: |
| 220 |
self.fail("Error updating host group : {0}. Changing platform_fault_domain_count is not allowed." |
| 221 |
.format(self.name)) |
| 222 |
if self.zones: |
| 223 |
if ('zones' in results and self.zones[0] != results['zones'][0]) or 'zones' not in results: |
| 224 |
self.fail("Error updating host group : {0}. Changing property zones is not allowed." |
| 225 |
.format(self.name)) |
| 226 |
elif self.state == 'absent': |
| 227 |
changed = True |
| 228 |
|
| 229 |
except ResourceNotFoundError: |
| 230 |
if self.state == 'present': |
| 231 |
changed = True |
| 232 |
else: |
| 233 |
changed = False |
| 234 |
|
| 235 |
self.results['changed'] = changed |
| 236 |
self.results['state'] = results |
| 237 |
|
| 238 |
if self.check_mode: |
| 239 |
return self.results |
| 240 |
|
| 241 |
if changed: |
| 242 |
if self.state == 'present': |
| 243 |
|
| 244 |
host_group_new = \ |
| 245 |
self.compute_models.DedicatedHostGroup(location=self.location, |
| 246 |
platform_fault_domain_count=self.platform_fault_domain_count, |
| 247 |
zones=self.zones) |
| 248 |
if self.tags: |
| 249 |
host_group_new.tags = self.tags |
| 250 |
self.results['state'] = self.create_or_update_hostgroup(host_group_new) |
| 251 |
|
| 252 |
elif self.state == 'absent': |
| 253 |
|
| 254 |
self.delete_hostgroup() |
| 255 |
self.results['state'] = 'Deleted' |
| 256 |
|
| 257 |
return self.results |
| 258 |
|
| 259 |
def create_or_update_hostgroup(self, host_group): |
| 260 |
try: |
| 261 |
|
| 262 |
response = self.compute_client.dedicated_host_groups.create_or_update( |
| 263 |
resource_group_name=self.resource_group, |
| 264 |
host_group_name=self.name, |
| 265 |
parameters=host_group) |
| 266 |
except Exception as exc: |
| 267 |
self.fail("Error creating or updating host group {0} - {1}".format(self.name, str(exc))) |
| 268 |
return self.hostgroup_to_dict(response) |
| 269 |
|
| 270 |
def delete_hostgroup(self): |
| 271 |
try: |
| 272 |
|
| 273 |
response = self.compute_client.dedicated_host_groups.delete(resource_group_name=self.resource_group, |
| 274 |
host_group_name=self.name) |
| 275 |
except Exception as exc: |
| 276 |
self.fail("Error deleting host group {0} - {1}".format(self.name, str(exc))) |
| 277 |
return response |
| 278 |
|
| 279 |
def hostgroup_to_dict(self, hostgroup): |
| 280 |
result = hostgroup.as_dict() |
| 281 |
result['tags'] = hostgroup.tags |
| 282 |
return result |
| 283 |
|
| 284 |
|
| 285 |
def main(): |
| 286 |
AzureRMHostGroup() |
| 287 |
|
| 288 |
|
| 289 |
if __name__ == '__main__': |
| 290 |
main() |