コード例 #1
0
def main():
    argument_spec = openstack_full_argument_spec(
        **yaml.safe_load(DOCUMENTATION)['options'])
    module_kwargs = openstack_module_kwargs()
    module = AnsibleModule(argument_spec=argument_spec,
                           supports_check_mode=False,
                           **module_kwargs)

    sdk, cloud = openstack_cloud_from_module(module)
    provisioner = metalsmith.Provisioner(cloud_region=cloud.config)

    try:
        found, not_found = bd.check_existing(
            instances=module.params['instances'],
            provisioner=provisioner,
            baremetal=cloud.baremetal)
        msg = ''
        if found:
            msg += ('Found existing instances: %s. ' %
                    ', '.join([i.uuid for i in found]))
        if not_found:
            msg += ('Instance(s) %s do not exist. ' %
                    ', '.join(r['hostname'] for r in not_found))

        instances = [{
            'name': i.node.name or i.uuid,
            'hostname': i.hostname,
            'id': i.uuid,
        } for i in found]
        module.exit_json(changed=False,
                         msg=msg,
                         instances=instances,
                         not_found=not_found)
    except Exception as e:
        module.fail_json(msg=str(e))
コード例 #2
0
    def test_success(self):
        pr = mock.Mock()
        baremetal = mock.Mock()
        instances = [
            {'hostname': 'host1',
             'image': {'href': 'overcloud-full'}},
            {'hostname': 'host3',
             'image': {'href': 'overcloud-full'}},
            {'hostname': 'host2', 'resource_class': 'compute',
             'capabilities': {'answer': '42'},
             'image': {'href': 'overcloud-full'}}
        ]
        existing = mock.MagicMock(hostname='host2', allocation=None)
        existing.uuid = 'aaaa'
        pr.show_instance.side_effect = [
            sdk_exc.ResourceNotFound(""),
            metalsmith.exceptions.Error(""),
            existing,
        ]
        found, not_found = bd.check_existing(instances, pr, baremetal)

        self.assertEqual([existing], found)
        self.assertEqual([{
            'hostname': 'host1',
            'image': {'href': 'overcloud-full'},
        }, {
            'hostname': 'host3',
            'image': {'href': 'overcloud-full'},
        }], not_found)
        pr.show_instance.assert_has_calls([
            mock.call(host) for host in ['host1', 'host3', 'host2']
        ])
コード例 #3
0
    def test_existing_no_allocation(self):
        pr = mock.Mock()
        baremetal = mock.Mock()
        instances = [{
            'name': 'server2',
            'resource_class': 'compute',
            'hostname': 'host2',
            'capabilities': {
                'answer': '42'
            },
            'image': {
                'href': 'overcloud-full'
            }
        }]
        existing = mock.MagicMock(hostname='host2',
                                  allocation=None,
                                  state=metalsmith.InstanceState.ACTIVE)
        existing.uuid = 'aaaa'
        pr.show_instance.return_value = existing

        found, not_found = bd.check_existing(instances, pr, baremetal)
        baremetal.create_allocation.assert_called_once_with(
            name='host2', node='server2', resource_class='compute')

        self.assertEqual([], not_found)
        self.assertEqual([existing], found)
        pr.show_instance.assert_called_once_with('server2')