From f8ba54972adf74df8ba8a5b60c8b3204eecbc84f Mon Sep 17 00:00:00 2001 From: jwcolbert Date: Mon, 19 Jun 2017 14:11:12 -0500 Subject: [PATCH] Made os_type for bareMetal more dynamic --- clc_ansible_module/clc_server.py | 46 +++++++++++++++++++++++++------- tests/test_clc_server.py | 8 ++++++ 2 files changed, 45 insertions(+), 9 deletions(-) diff --git a/clc_ansible_module/clc_server.py b/clc_ansible_module/clc_server.py index 55014da..684d9b6 100644 --- a/clc_ansible_module/clc_server.py +++ b/clc_ansible_module/clc_server.py @@ -233,10 +233,9 @@ os_type: description: - Only required for bare metal servers. - Specifies the OS to provision with the bare metal server. + Specifies the OS to provision with the bare metal server. Will search for a os_type if a partial string is provided. default: None required: False - choices: ['redHat6_64Bit', 'centOS6_64Bit', 'windows2012R2Standard_64Bit', 'ubuntu14_64Bit'] wait: description: - Whether to wait for the provisioning tasks to finish before returning. @@ -703,13 +702,7 @@ def _define_module_argument_spec(): 'ICMP']), public_ip_ports=dict(type='list', default=[]), configuration_id=dict(default=None), - os_type=dict(default=None, - choices=[ - 'redHat6_64Bit', - 'centOS6_64Bit', - 'windows2012R2Standard_64Bit', - 'ubuntu14_64Bit' - ]), + os_type=dict(), wait=dict(type='bool', default=True)) mutually_exclusive = [ @@ -778,6 +771,7 @@ def _validate_module_params(clc, module): params['description'] = ClcServer._find_description(module) params['ttl'] = ClcServer._find_ttl(clc, module) params['template'] = ClcServer._find_template_id(module, datacenter) + params['os_type'] = ClcServer._find_os_type_id(clc, module, alias, datacenter) if params['state'] == 'present': params['group'] = ClcServer._find_group(module, datacenter).id params['network_id'] = ClcServer._find_network_id(module, datacenter) @@ -984,6 +978,40 @@ def _find_template_id(module, datacenter): datacenter.id)) return result + + @staticmethod + def _find_os_type_id(clc, module, alias, datacenter): + """ + Find the template id by calling the CLC API. + :param module: the module to validate + :param datacenter: the datacenter to search for the template + :return: a valid clc template id + """ + lookup_os = module.params.get('os_type') + state = module.params.get('state') + type = module.params.get('type') + os_types = [] + + if state == 'present' and type == 'bareMetal': + try: + baremetal = clc.v2.API.Call(method='GET', url='datacenters/%s/%s/bareMetalCapabilities' % (alias, datacenter.id)) + for os in baremetal["operatingSystems"]: + os_types.append(os["type"]) + if os["type"].lower().find(lookup_os.lower()) != -1: + return os["type"] + raise CLCException() + except CLCException: + module.fail_json( + msg=str( + "Unable to find a os_type: " + + lookup_os + + " in location: " + + datacenter.id)) + + + + + @staticmethod def _find_network_id(module, datacenter): """ diff --git a/tests/test_clc_server.py b/tests/test_clc_server.py index 730b6fe..36be013 100644 --- a/tests/test_clc_server.py +++ b/tests/test_clc_server.py @@ -405,6 +405,8 @@ def test_process_request_count_1_bare_metal_server(self, 'os_type': 'ubuntu14_64Bit' } + + # Define Mock Objects mock_server = mock.MagicMock() mock_requests = mock.MagicMock() @@ -438,13 +440,19 @@ def test_process_request_count_1_bare_metal_server(self, mock_result_group.data = { "id":"1111111" } mock_result_group.Servers().Servers.return_value = [ mock_existing_server, mock_server ] + # Set Mock bareMetalCapabilities REturn Values + mock_baremetal = mock.MagicMock() + # Setup Mock API Responses def _api_call_return_values(*args, **kwargs): + if kwargs.get('method') == 'GET' and 'bareMetalCapabilities' in kwargs.get('url'): + return {"operatingSystems": [{'type': 'ubuntu14_64Bit'}]} if kwargs.get('method') == 'GET': return {'id': '12345','name': 'test'} if kwargs.get('method') == 'POST': return {'links': [{'rel': 'self', 'id': '12345'}]} + mock_clc_sdk.v2.Datacenter().Groups().Get.side_effect = [ mock_group, mock_result_group ] mock_clc_sdk.v2.Group.return_value = mock_group mock_clc_sdk.v2.Server.return_value = mock_server