def test_get_request_result_raw_content(self, mock_config): mock_config.return_value.verify = False mock_config.return_value.timeout = 12 mock_config.return_value.vcac_server = 'fake_server' mock_config.return_value.session.get.return_value.text = '{"content": ["fake_data"]}' vra_request = VraRequest('') vra_request.status_url = "fake_status_url" result = vra_request.get_request_result_raw() self.assertEqual(result, 'fake_data') mock_config.return_value.session.get.assert_called_once_with( "fake_status_url/resources", verify=False, timeout=12)
def test_get_object_vm_full_key_id(self, mock_format, mock_config): mock_config.return_value.config_file = {"virtual_machine_type": "vm"} mock_config.return_value.verify = False mock_config.return_value.timeout = 12 mock_config.return_value.vcac_server = 'fake_server' mock_format.return_value = "$filter=(((resourceType/name+eq+'FAKE_RESOURCE_TYPE'))+and+(fake_key+eq+'fake_value'))" mock_config.return_value.session.get.return_value.json.side_effect = [{ "content": [{ "id": "id1" }] }, { "id": "id1", "fake_res1": "fake_value1" }] result = VraRequest('').get_object_raw('vm', 'id', 'id1', 1, 1, True) mock_config.return_value.session.get.assert_any_call( "https://fake_server/catalog-service/api/consumer/resources/?limit=1&page=1&$filter=(((resourceType/name+eq+'FAKE_RESOURCE_TYPE'))+and+(fake_key+eq+'fake_value'))", verify=False, timeout=12) mock_config.return_value.session.get.assert_any_call( 'https://fake_server/catalog-service/api/consumer/resources/id1', verify=False, timeout=12) self.assertEqual(result, [{'id': 'id1', 'fake_res1': 'fake_value1'}])
def test_get_request_result_raw_no_content(self, mock_session, mock_config): mock_config.return_value.verify = False mock_config.return_value.timeout = 12 mock_config.return_value.vcac_server = 'fake_server' mock_config.return_value.session.get.return_value.text = '{"fake_data":""}' vra_request = VraRequest('') vra_request.status_url = "fake_status_url" result = vra_request.get_request_result_raw() self.assertEqual(result, {'fake_data': ''}) mock_config.return_value.session.get.assert_any_call( 'fake_status_url/resources', verify=False, timeout=12) mock_config.return_value.session.get.assert_any_call( 'fake_status_url/forms/details', verify=False, timeout=12)
def test_format_filters_raises_get_module_class(self, mock_module, mock_config): mock_module.side_effect = Exception() mock_config.return_value.config = {'business_models': {'vm': ''}} with self.assertRaises(VraSdkConfigException): VraRequest('').format_filters('vm', '', '')
def test_execute_async_(self, mock_config): mock_payload = MagicMock() mock_payload.execute_request.return_value.headers = {'NoLocation': ''} vra_req = VraRequest(mock_payload).execute_async() self.assertEqual('NoLocationFound', vra_req.status_url) mock_payload.execute_request.assert_called_once
def get_raw_definition(self, key, value, resource_type): """Return dict used to create object inside the VraFactory This methods is a helper. You should use only at the beginning to help you build the object definitions needed for the factrory Args: key (string): key to search data from value (string): value of the key resource_type (string): vRa resource type Returns: dict: dict of user friendly vRa formatted data """ return VraRequest({}).get_object(None, key, value, 1, 1, True, resource_type)[0]
def test_format_filters(self, mock_module, mock_attr, mock_config): mock_module.return_value = 'fake_module', 'fake_class' mock_config.return_value.config_file = { 'business_models': { 'vm': { 'path': 'fake_module_class' } } } mock_attr.return_value = ['FAKE_RESOURCE_TYPE'] result = VraRequest('').format_filters('vm', 'fake_key', 'fake_value') mock_module.assert_called_once_with('fake_module_class') mock_attr.assert_called_once_with('fake_module', 'RESOURCE_TYPE') self.assertEqual( result, "$filter=(((resourceType/name+eq+'FAKE_RESOURCE_TYPE'))+and+(fake_key+eq+'fake_value'))" )
def get_data(self, object_type, key, value): """Get data about one catalog resource in vRa. Get detailed info about your object Args: object_type (string): object type as described in the 'business_models' section of the configuration fiel key (string): field to filter on value (string): value of the field Returns: object: business models object type as described in you configuration file """ data = VraRequest({}).get_object(object_type, key, value, 1, 1, True) if not data: raise VraSdkMainException( f'No {object_type} exist with {key}={value}') return data[0]
def request_catalog_item(self, item_name, customization_func=None, **kwargs): """create a ready to use Request object to request a catalog item Args: item_name (string): catalog item name customization_func (function, optional): Defaults to None. payload customization function Returns: VraRequest: object with the payload attribute well customized """ if not kwargs.get('payload_version'): kwargs['payload_version'] = self.config.config_file[ 'payload_default_version'] payload = self.format_payload('catalog_item', item_name, customization_func, **kwargs) return VraRequest(payload)
def test_get_object_raw_not_vm(self, mock_format, mock_config): mock_config.return_value.verify = False mock_config.return_value.timeout = 12 mock_config.return_value.vcac_server = 'fake_server' mock_format.return_value = "$filter=(((resourceType/name+eq+'FAKE_RESOURCE_TYPE'))+and+(fake_key+eq+'fake_value'))" mock_config.return_value.session.get.return_value.json.return_value = { "content": [{ "id1": "fake_id1" }, { "id2": "fake_id2" }] } result = VraRequest('').get_object_raw('notvm', 'key1', 'value1', 1, 1) self.assertEqual(result, [{'id1': 'fake_id1'}, {'id2': 'fake_id2'}]) mock_config.return_value.session.get.assert_called_once_with( "https://fake_server/catalog-service/api/consumer/resources/?limit=1&page=1&$filter=(((resourceType/name+eq+'FAKE_RESOURCE_TYPE'))+and+(fake_key+eq+'fake_value'))", verify=False, timeout=12)
def list_data(self, object_type, key, value, limit=None, page=1, full=False, recursive=False): """Get info about a list of object. Get less details than get_data(), but you still get the id Args: object_type (string): object typre key (string): field to filter on value (string): value of the field to search on limit (int, optional): Defaults to None. maximum result page (int, optional): Defaults to 1. vRa result page to get data from recursive (bool, optional): Defaults to False. If True, will call itself recursively until there's no more page to get from Raises: VraSdkMainException: [description] Returns: list: list of business models object type as described in you configuration file """ if not limit: limit = self.config.config_file['max_vra_result_per_page'] data = VraRequest({}).get_object(object_type, key, value, limit, page, full) if not data: raise VraSdkMainException( f'No {object_type} exist with {key}={value}') if recursive and (len(data) == limit): page = page + 1 data_to_add = self.list_data(object_type, key, value, limit, page, full, recursive) data = data + data_to_add return data
def request_resource_action(self, action_name, resource_id, customization_func=None, **kwargs): """create a ready to use Request object to request a resource action Args: action_name (string): resource action name resource_id (string): resource id to perform action on customization_func (function, optional): Defaults to None. payload customization function Returns: VraRequest: object with the payload attribute well customized """ if not kwargs.get('payload_version'): kwargs['payload_version'] = self.config.config_file[ 'payload_default_version'] payload = self.format_payload('resource_action', action_name, resource_id, customization_func, **kwargs) return VraRequest(payload)
def test_get_status(self, mock_config): mock_config.return_value.session.get.return_value.text = '{"state":"OK"}' result = VraRequest('').get_status() self.assertEqual(result, 'OK')
def test_get_status_raises_unmanaged(self, mock_json, mock_config): mock_json.side_effect = Exception() with self.assertRaises(VraSdkMainRequestException): VraRequest('').get_status()
def test_get_status_raises(self, mock_config): with self.assertRaises(VraSdkRequestException): mock_config.return_value.session.get.side_effect = RequestException( ) VraRequest('').get_status()
def test_get_request_result_raw_raises(self, mock_json, mock_config): mock_config.return_value.session.get.side_effect = RequestException() with self.assertRaises(VraSdkRequestException): VraRequest('').get_request_result_raw()
def test_execute_sync_raises(self, mock_status, mock_exec, mock_config): mock_status.return_value = 'PROVIDER_FAILED' with self.assertRaises(VraSdkMainRequestException): VraRequest("").execute_sync() mock_exec.assert_called_once mock_status.assert_called_once
def test_format_filters_raises_not_object_type(self, mock_config): mock_config.return_value.config = {'business_models': {'vm': ''}} with self.assertRaises(VraSdkConfigException): VraRequest('').format_filters('fake_object_type', '', '')
def test_init(self, mock_config): result = VraRequest('fake_payload') self.assertEqual(result.payload, 'fake_payload') self.assertEqual(result.response, None)
def test_execute_sync(self, mock_status, mock_exec, mock_config): mock_status.return_value = 'SUCCESSFUL' VraRequest("").execute_sync() mock_exec.assert_called_once mock_status.assert_called_once
def test_get_request_result_raw_raises(self, mock_json, mock_config): mock_json.loads.side_effect = Exception() with self.assertRaises(VraSdkMainRequestException): VraRequest('').get_request_result_raw()