Skip to content

Commit

Permalink
Made os_type for bareMetal more dynamic
Browse files Browse the repository at this point in the history
  • Loading branch information
jwcolbert committed Jun 19, 2017
1 parent 339e640 commit f8ba549
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 9 deletions.
46 changes: 37 additions & 9 deletions clc_ansible_module/clc_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 = [
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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):
"""
Expand Down
8 changes: 8 additions & 0 deletions tests/test_clc_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit f8ba549

Please sign in to comment.