adding devtestlabs (#50961)

pull/51432/head
Zim Kalinowski 5 years ago committed by GitHub
parent a6bbf21282
commit 4041f02389
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,284 @@
#!/usr/bin/python
#
# Copyright (c) 2019 Zim Kalinowski, <zikalino@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_devtestlab
version_added: "2.8"
short_description: Manage Azure DevTest Lab instance.
description:
- Create, update and delete instance of Azure DevTest Lab.
options:
resource_group:
description:
- The name of the resource group.
required: True
name:
description:
- The name of the lab.
required: True
location:
description:
- The location of the resource.
storage_type:
description:
- Type of storage used by the lab. It can be either C(premium) or C(standard). Default is C(premium).
choices:
- 'standard'
- 'premium'
premium_data_disks:
description:
- "Allow creation of C(premium) data disks."
type: bool
state:
description:
- Assert the state of the DevTest Lab.
- Use C(present) to create or update an DevTest Lab and C(absent) to delete it.
default: present
choices:
- absent
- present
extends_documentation_fragment:
- azure
- azure_tags
author:
- "Zim Kalinowski (@zikalino)"
'''
EXAMPLES = '''
- name: Create (or update) DevTest Lab
azure_rm_devtestlab:
resource_group: testrg
name: mylab
storage_type: standard
'''
RETURN = '''
id:
description:
- The identifier of the resource.
returned: always
type: str
sample: /subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourcegroups/testrg/providers/microsoft.devtestlab/labs/mylab
'''
import time
from ansible.module_utils.azure_rm_common import AzureRMModuleBase
from ansible.module_utils.common.dict_transformations import _snake_to_camel
try:
from msrestazure.azure_exceptions import CloudError
from msrest.polling import LROPoller
from msrestazure.azure_operation import AzureOperationPoller
from azure.mgmt.devtestlabs import DevTestLabsClient
from msrest.serialization import Model
except ImportError:
# This is handled in azure_rm_common
pass
class Actions:
NoAction, Create, Update, Delete = range(4)
class AzureRMDevTestLab(AzureRMModuleBase):
"""Configuration class for an Azure RM DevTest Lab resource"""
def __init__(self):
self.module_arg_spec = dict(
resource_group=dict(
type='str',
required=True
),
name=dict(
type='str',
required=True
),
location=dict(
type='str'
),
storage_type=dict(
type='str',
choices=['standard',
'premium']
),
premium_data_disks=dict(
type='bool'
),
state=dict(
type='str',
default='present',
choices=['present', 'absent']
)
)
self.resource_group = None
self.name = None
self.lab = {}
self.results = dict(changed=False)
self.mgmt_client = None
self.state = None
self.to_do = Actions.NoAction
super(AzureRMDevTestLab, self).__init__(derived_arg_spec=self.module_arg_spec,
supports_check_mode=True,
supports_tags=True)
def exec_module(self, **kwargs):
"""Main module execution method"""
for key in list(self.module_arg_spec.keys()) + ['tags']:
if hasattr(self, key):
setattr(self, key, kwargs[key])
elif kwargs[key] is not None:
self.lab[key] = kwargs[key]
if self.lab.get('storage_type'):
self.lab['lab_storage_type'] = _snake_to_camel(self.lab['storage_type'], True)
self.lab.pop('storage_type', None)
if self.lab.get('premium_data_disks') is not None:
self.lab['premium_data_disks'] = 'Enabled' if self.lab['premium_data_disks'] else 'Disabled'
response = None
self.mgmt_client = self.get_mgmt_svc_client(DevTestLabsClient,
base_url=self._cloud_environment.endpoints.resource_manager,
api_version='2018-10-15')
resource_group = self.get_resource_group(self.resource_group)
if self.lab.get('location') is None:
self.lab['location'] = resource_group.location
old_response = self.get_devtestlab()
if not old_response:
self.log("DevTest Lab instance doesn't exist")
if self.state == 'absent':
self.log("Old instance didn't exist")
else:
self.to_do = Actions.Create
else:
self.log("DevTest Lab instance already exists")
if self.state == 'absent':
self.to_do = Actions.Delete
elif self.state == 'present':
if self.lab.get('lab_storage_type') is not None and \
self.lab.get('lab_storage_type').lower() != old_response.get('lab_storage_type', '').lower():
self.to_do = Actions.Update
if (self.lab.get('premium_data_disks') is not None and
self.lab.get('premium_data_disks').lower() != old_response.get('premium_data_disks').lower()):
self.to_do = Actions.Update
if (self.to_do == Actions.Create) or (self.to_do == Actions.Update):
self.log("Need to Create / Update the DevTest Lab instance")
self.results['changed'] = True
if self.check_mode:
return self.results
response = self.create_update_devtestlab()
self.log("Creation / Update done")
elif self.to_do == Actions.Delete:
self.log("DevTest Lab instance deleted")
self.results['changed'] = True
if self.check_mode:
return self.results
self.delete_devtestlab()
# This currently doesnt' work as there is a bug in SDK / Service
if isinstance(response, LROPoller) or isinstance(response, AzureOperationPoller):
response = self.get_poller_result(response)
else:
self.log("DevTest Lab instance unchanged")
self.results['changed'] = False
response = old_response
if self.state == 'present':
self.results.update({
'id': response.get('id', None)
})
return self.results
def create_update_devtestlab(self):
'''
Creates or updates DevTest Lab with the specified configuration.
:return: deserialized DevTest Lab instance state dictionary
'''
self.log("Creating / Updating the DevTest Lab instance {0}".format(self.name))
try:
response = self.mgmt_client.labs.create_or_update(resource_group_name=self.resource_group,
name=self.name,
lab=self.lab)
if isinstance(response, LROPoller) or isinstance(response, AzureOperationPoller):
response = self.get_poller_result(response)
except CloudError as exc:
self.log('Error attempting to create the DevTest Lab instance.')
self.fail("Error creating the DevTest Lab instance: {0}".format(str(exc)))
return response.as_dict()
def delete_devtestlab(self):
'''
Deletes specified DevTest Lab instance in the specified subscription and resource group.
:return: True
'''
self.log("Deleting the DevTest Lab instance {0}".format(self.name))
try:
response = self.mgmt_client.labs.delete(resource_group_name=self.resource_group,
name=self.name)
except CloudError as e:
self.log('Error attempting to delete the DevTest Lab instance.')
self.fail("Error deleting the DevTest Lab instance: {0}".format(str(e)))
return True
def get_devtestlab(self):
'''
Gets the properties of the specified DevTest Lab.
:return: deserialized DevTest Lab instance state dictionary
'''
self.log("Checking if the DevTest Lab instance {0} is present".format(self.name))
found = False
try:
response = self.mgmt_client.labs.get(resource_group_name=self.resource_group,
name=self.name)
found = True
self.log("Response : {0}".format(response))
self.log("DevTest Lab instance : {0} found".format(response.name))
except CloudError as e:
self.log('Did not find the DevTest Lab instance.')
if found is True:
return response.as_dict()
return False
def main():
"""Main execution"""
AzureRMDevTestLab()
if __name__ == '__main__':
main()

@ -0,0 +1,280 @@
#!/usr/bin/python
#
# Copyright (c) 2019 Zim Kalinowski, <zikalino@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_devtestlabvirtualnetwork
version_added: "2.8"
short_description: Manage Azure DevTest Lab Virtual Network instance.
description:
- Create, update and delete instance of Azure DevTest Lab Virtual Network.
options:
resource_group:
description:
- The name of the resource group.
required: True
lab_name:
description:
- The name of the lab.
required: True
name:
description:
- The name of the virtual network.
required: True
location:
description:
- The location of the resource.
description:
description:
- The description of the virtual network.
state:
description:
- Assert the state of the Virtual Network.
- Use 'present' to create or update an Virtual Network and 'absent' to delete it.
default: present
choices:
- absent
- present
extends_documentation_fragment:
- azure
- azure_tags
author:
- "Zim Kalinowski (@zikalino)"
'''
EXAMPLES = '''
- name: Create (or update) Virtual Network
azure_rm_devtestlabvirtualnetwork:
resource_group: testrg
lab_name: mylab
name: myvn
description: My Lab Virtual Network
'''
RETURN = '''
id:
description:
- The identifier of the resource.
returned: always
type: str
sample: "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourcegroups/testrg/providers/microsoft.devtestlab/
mylab/mylab/virtualnetworks/myvn"
external_provider_resource_id:
description:
- The identifier of external virtual network.
returned: always
type: str
sample: "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/testrg/providers/Microsoft.Network/vi
rtualNetworks/myvn"
'''
import time
from ansible.module_utils.azure_rm_common import AzureRMModuleBase
from ansible.module_utils.common.dict_transformations import _snake_to_camel
try:
from msrestazure.azure_exceptions import CloudError
from msrest.polling import LROPoller
from msrestazure.azure_operation import AzureOperationPoller
from azure.mgmt.devtestlabs import DevTestLabsClient
from msrest.serialization import Model
except ImportError:
# This is handled in azure_rm_common
pass
class Actions:
NoAction, Create, Update, Delete = range(4)
class AzureRMDevTestLabVirtualNetwork(AzureRMModuleBase):
"""Configuration class for an Azure RM Virtual Network resource"""
def __init__(self):
self.module_arg_spec = dict(
resource_group=dict(
type='str',
required=True
),
lab_name=dict(
type='str',
required=True
),
name=dict(
type='str',
required=True
),
location=dict(
type='str'
),
description=dict(
type='str'
),
state=dict(
type='str',
default='present',
choices=['present', 'absent']
)
)
self.resource_group = None
self.lab_name = None
self.name = None
self.virtual_network = {}
self.results = dict(changed=False)
self.mgmt_client = None
self.state = None
self.to_do = Actions.NoAction
super(AzureRMDevTestLabVirtualNetwork, self).__init__(derived_arg_spec=self.module_arg_spec,
supports_check_mode=True,
supports_tags=True)
def exec_module(self, **kwargs):
"""Main module execution method"""
for key in list(self.module_arg_spec.keys()) + ['tags']:
if hasattr(self, key):
setattr(self, key, kwargs[key])
elif kwargs[key] is not None:
self.virtual_network[key] = kwargs[key]
response = None
self.mgmt_client = self.get_mgmt_svc_client(DevTestLabsClient,
base_url=self._cloud_environment.endpoints.resource_manager,
api_version='2018-10-15')
resource_group = self.get_resource_group(self.resource_group)
if self.virtual_network.get('location') is None:
self.virtual_network['location'] = resource_group.location
old_response = self.get_virtualnetwork()
if not old_response:
self.log("Virtual Network instance doesn't exist")
if self.state == 'absent':
self.log("Old instance didn't exist")
else:
self.to_do = Actions.Create
else:
self.log("Virtual Network instance already exists")
if self.state == 'absent':
self.to_do = Actions.Delete
elif self.state == 'present':
if self.virtual_network.get('description') is not None and self.virtual_network.get('description') != old_response.get('description'):
self.to_do = Actions.Update
if (self.to_do == Actions.Create) or (self.to_do == Actions.Update):
self.log("Need to Create / Update the Virtual Network instance")
self.results['changed'] = True
if self.check_mode:
return self.results
response = self.create_update_virtualnetwork()
self.log("Creation / Update done")
elif self.to_do == Actions.Delete:
self.log("Virtual Network instance deleted")
self.results['changed'] = True
if self.check_mode:
return self.results
self.delete_virtualnetwork()
# This currently doesnt' work as there is a bug in SDK / Service
if isinstance(response, LROPoller) or isinstance(response, AzureOperationPoller):
response = self.get_poller_result(response)
else:
self.log("Virtual Network instance unchanged")
self.results['changed'] = False
response = old_response
if self.state == 'present':
self.results.update({
'id': response.get('id', None),
'external_provider_resource_id': response.get('external_provider_resource_id', None)
})
return self.results
def create_update_virtualnetwork(self):
'''
Creates or updates Virtual Network with the specified configuration.
:return: deserialized Virtual Network instance state dictionary
'''
self.log("Creating / Updating the Virtual Network instance {0}".format(self.name))
try:
response = self.mgmt_client.virtual_networks.create_or_update(resource_group_name=self.resource_group,
lab_name=self.lab_name,
name=self.name,
virtual_network=self.virtual_network)
if isinstance(response, LROPoller) or isinstance(response, AzureOperationPoller):
response = self.get_poller_result(response)
except CloudError as exc:
self.log('Error attempting to create the Virtual Network instance.')
self.fail("Error creating the Virtual Network instance: {0}".format(str(exc)))
return response.as_dict()
def delete_virtualnetwork(self):
'''
Deletes specified Virtual Network instance in the specified subscription and resource group.
:return: True
'''
self.log("Deleting the Virtual Network instance {0}".format(self.name))
try:
response = self.mgmt_client.virtual_networks.delete(resource_group_name=self.resource_group,
lab_name=self.lab_name,
name=self.name)
except CloudError as e:
self.log('Error attempting to delete the Virtual Network instance.')
self.fail("Error deleting the Virtual Network instance: {0}".format(str(e)))
return True
def get_virtualnetwork(self):
'''
Gets the properties of the specified Virtual Network.
:return: deserialized Virtual Network instance state dictionary
'''
self.log("Checking if the Virtual Network instance {0} is present".format(self.name))
found = False
try:
response = self.mgmt_client.virtual_networks.get(resource_group_name=self.resource_group,
lab_name=self.lab_name,
name=self.name)
found = True
self.log("Response : {0}".format(response))
self.log("Virtual Network instance : {0} found".format(response.name))
except CloudError as e:
self.log('Did not find the Virtual Network instance.')
if found is True:
return response.as_dict()
return False
def main():
"""Main execution"""
AzureRMDevTestLabVirtualNetwork()
if __name__ == '__main__':
main()

@ -30,3 +30,4 @@ azure-keyvault==1.0.0a1
azure-graphrbac==0.40.0
azure-mgmt-cosmosdb==0.5.1
azure-mgmt-hdinsight==0.1.0
azure-mgmt-devtestlabs==2.2.0

@ -0,0 +1,4 @@
cloud/azure
destructive
shippable/azure/group1
azure_rm_devtestlabvirtualnetwork

@ -0,0 +1,131 @@
- name: Prepare random number
set_fact:
lab_name: "lab{{ resource_group | hash('md5') | truncate(7, True, '') }}{{ 1000 | random }}"
vn_name: "vn{{ resource_group | hash('md5') | truncate(7, True, '') }}{{ 1000 | random }}"
run_once: yes
- name: Create instance of Lab -- check mode
azure_rm_devtestlab:
resource_group: "{{ resource_group }}"
name: "{{ lab_name }}"
location: eastus
storage_type: standard
premium_data_disks: no
check_mode: yes
register: output
- name: Check if the change was correctly reported
assert:
that:
- output.changed
- name: Create instance of Lab
azure_rm_devtestlab:
resource_group: "{{ resource_group }}"
name: "{{ lab_name }}"
location: eastus
storage_type: standard
premium_data_disks: no
register: output
- name: Check if the change was correctly reported
assert:
that:
- output.changed
- name: Create again instance of Lab
azure_rm_devtestlab:
resource_group: "{{ resource_group }}"
name: "{{ lab_name }}"
location: eastus
storage_type: standard
premium_data_disks: no
register: output
- name: Assert the state has not changed
assert:
that:
- output.changed == false
- name: Update lab - premium_data_disks
azure_rm_devtestlab:
resource_group: "{{ resource_group }}"
name: "{{ lab_name }}"
location: eastus
storage_type: standard
premium_data_disks: yes
register: output
- name: Assert the change was registered
assert:
that:
- output.changed
- name: Create instance of DebTest Labs virtual network
azure_rm_devtestlabvirtualnetwork:
resource_group: "{{ resource_group }}"
lab_name: "{{ lab_name }}"
name: "{{ vn_name }}"
location: eastus
description: My DevTest Lab
register: output
- name: Assert the change was registered
assert:
that:
- output.changed
- name: Update instance of DebTest Labs virtual network with same parameters
azure_rm_devtestlabvirtualnetwork:
resource_group: "{{ resource_group }}"
lab_name: "{{ lab_name }}"
name: "{{ vn_name }}"
location: eastus
description: My DevTest Lab
register: output
- name: Assert that nothing was changed
assert:
that:
- output.changed == false
- name: Update instance of DebTest Labs virtual network with changed description
azure_rm_devtestlabvirtualnetwork:
resource_group: "{{ resource_group }}"
lab_name: "{{ lab_name }}"
name: "{{ vn_name }}"
location: eastus
description: My DevTest Lab Updated
register: output
- name: Assert that nothing was changed
assert:
that:
- output.changed
- name: Delete instance of Lab -- check mode
azure_rm_devtestlab:
resource_group: "{{ resource_group }}"
state: absent
name: "{{ lab_name }}"
check_mode: yes
register: output
- name: Assert if the change was correctly reported in check mode
assert:
that:
- output.changed
- name: Delete instance of Lab
azure_rm_devtestlab:
resource_group: "{{ resource_group }}"
name: "{{ lab_name }}"
state: absent
register: output
- name: Assert the change was correctly reported
assert:
that:
- output.changed
- name: Delete unexisting instance of Lab
azure_rm_devtestlab:
resource_group: "{{ resource_group }}"
name: "{{ lab_name }}unexisting"
state: absent
register: output
- name: Assert thes state has not changed
assert:
that:
- output.changed == false

@ -30,3 +30,4 @@ azure-keyvault==1.0.0a1
azure-graphrbac==0.40.0
azure-mgmt-cosmosdb==0.5.1
azure-mgmt-hdinsight==0.1.0
azure-mgmt-devtestlabs==2.2.0

Loading…
Cancel
Save