def test_should_not_update_pool_type_when_no_changes(self):
        self.resource.get.return_value = DEFAULT_ID_POOLS

        self.resource.update_pool_type.return_value = UPDATE_TEMPLATE

        self.mock_ansible_module.params = PARAMS_FOR_UPDATE

        IdPoolsModule().run()

        self.mock_ansible_module.exit_json.assert_called_once_with(
            changed=False,
            msg=IdPoolsModule.MSG_ALREADY_PRESENT,
            ansible_facts=dict(id_pool=UPDATE_TEMPLATE))
    def test_should_fail_when_ids_not_available_for_allocation(self):
        self.resource.get.return_value = None
        self.resource.data['uri'] = URI + "/allocator"

        self.resource.allocate.return_value = None
        self.resource.allocate.side_effect = OneViewModuleValueError(
            IdPoolsModule.MSG_IDS_NOT_AVAILABLE)

        self.mock_ansible_module.params = PARAMS_WITH_ALLOCATE

        IdPoolsModule().run()

        self.mock_ansible_module.fail_json.assert_called_once_with(
            exception=mock.ANY, msg=IdPoolsModule.MSG_IDS_NOT_AVAILABLE)
    def test_should_validate(self):
        self.resource.get.return_value = DEFAULT_ID_POOLS
        self.resource.data['uri'] = DEFAULT_ID_POOLS[
            'uri'] + "/validate?idList=VCGYOAA023&idList=VCGYOAA024"

        self.resource.validate.return_value = VALIDATE_TEMPLATE

        self.mock_ansible_module.params = PARAMS_WITH_VALIDATE

        IdPoolsModule().run()

        self.mock_ansible_module.exit_json.assert_called_once_with(
            changed=True,
            msg=IdPoolsModule.MSG_VALIDATED,
            ansible_facts=dict(id_pool=VALIDATE_TEMPLATE))
    def test_should_collect_when_ids_allocated(self):
        self.resource.get.return_value = DEFAULT_ID_POOLS
        self.resource.data['uri'] = URI + "/collector"
        self.resource.data['idList'] = ['10.1.0.1', '10.1.0.5']

        self.resource.collect.return_value = COLLECTOR_TEMPLATE

        self.mock_ansible_module.params = PARAMS_WITH_COLLECTOR

        IdPoolsModule().run()

        self.mock_ansible_module.exit_json.assert_called_once_with(
            changed=True,
            msg=IdPoolsModule.MSG_COLLECTED,
            ansible_facts=dict(id_pool=COLLECTOR_TEMPLATE))
    def test_should_allocate_ids_from_pool(self):
        self.resource.get.return_value = DEFAULT_ID_POOLS
        self.resource.data['uri'] = URI + "/allocator"
        self.resource.data['count'] = 2

        self.resource.allocate.return_value = ALLOCATE_TEMPLATE

        self.mock_ansible_module.params = PARAMS_WITH_ALLOCATE

        IdPoolsModule().run()

        self.mock_ansible_module.exit_json.assert_called_once_with(
            changed=True,
            msg=IdPoolsModule.MSG_ALLOCATED,
            ansible_facts=dict(id_pool=ALLOCATE_TEMPLATE))
    def test_should_update_pool_type(self):
        self.resource.get.return_value = DEFAULT_ID_POOLS
        update_data = UPDATE_TEMPLATE.copy()
        update_data['enabled'] = False

        self.resource.update_pool_type.return_value = update_data

        self.mock_ansible_module.params = PARAMS_FOR_UPDATE

        IdPoolsModule().run()

        self.mock_ansible_module.exit_json.assert_called_once_with(
            changed=True,
            msg=IdPoolsModule.MSG_UPDATED,
            ansible_facts=dict(id_pool=update_data))
    def test_should_not_collect_when_ids_not_allocated(self):
        self.resource.get.return_value = DEFAULT_ID_POOLS
        self.resource.data['uri'] = DEFAULT_ID_POOLS['uri'] + "/collector"

        invalid_data = COLLECTOR_TEMPLATE.copy()
        invalid_data['idList'] = []
        self.resource.collect.return_value = invalid_data

        self.mock_ansible_module.params = PARAMS_WITH_COLLECTOR

        IdPoolsModule().run()

        self.mock_ansible_module.exit_json.assert_called_once_with(
            changed=False,
            msg=IdPoolsModule.MSG_IDS_NOT_AVAILABLE,
            ansible_facts=dict(id_pool=invalid_data))
    def test_validate_should_fail_when_ids_not_valid(self):
        self.resource.get.return_value = DEFAULT_ID_POOLS
        self.resource.data['uri'] = DEFAULT_ID_POOLS[
            'uri'] + "/validate?idList=VCGYOAA023&idList=VCGYOAA024"

        invalid_data = VALIDATE_TEMPLATE.copy()
        invalid_data['idList'] = []
        self.resource.validate.return_value = invalid_data

        self.mock_ansible_module.params = PARAMS_WITH_VALIDATE

        IdPoolsModule().run()

        self.mock_ansible_module.exit_json.assert_called_once_with(
            changed=False,
            msg=IdPoolsModule.MSG_IDS_NOT_AVAILABLE,
            ansible_facts=dict(id_pool=invalid_data))