def test_validate_payload_v6(self, mock_session): mock_session.return_value.get.side_effect = get_side_effect mock_session.return_value.post.side_effect = post_side_effect VraConfig(get_test_config_file_path()) auth = VraAuthenticate('PRD').auth_login_password( 'fake_login', 'fake_password', 'foo.fuu.com') sdk = VraSdk(auth, 'fake_bg') data = { "payload_version": 6, "provider-fake_data1": "fake_value1", "fake_data2": 122 } fake_item_request = sdk.request_catalog_item("fake_catalog_item", **data) self.assertTrue( self.validator.catalog_item(fake_item_request.payload.customized)) fake_action_request = sdk.request_resource_action( "fake_action_name", "fake_resource_id", **data) self.assertTrue( self.validator.resource_action( fake_action_request.payload.customized))
def test_get_data(self, mock_request, mock_config, mock_catalog, mock_get_bg_id): mock_request.return_value.get_object.return_value = ['fake_data'] vra_sdk = VraSdk(MagicMock(), '') vra_sdk.get_data('vm', 'key', 'value') mock_request.return_value.get_object.assert_called_once_with( 'vm', 'key', 'value', 1, 1, True)
def test_get_catalog_raises_get(self, mock_config, mock_get_bg_id): mock_config.return_value.session.get.side_effect = RequestException() with self.assertRaises(VraSdkRequestException): VraSdk(MagicMock(), "").get_catalog() mock_config.return_value.session.get.assert_called_once()
def test_get_catalog_raises_json(self, mock_resolve, mock_json, mock_os, mock_config, mock_get_bg_id): authentication_object = MagicMock() mock_json.loads.side_effect = Exception() with self.assertRaises(VraSdkMainException): VraSdk(authentication_object, "").get_catalog() mock_json.loads.assert_called_once()
def test_get_bg_id_raises_no_content(self, mock_config, mock_catalog): mock_config.return_value.session.get.return_value.text = '{"no_content":""}' with self.assertRaises(VraSdkMainException): VraSdk(MagicMock(), "") mock_config.return_value.session.get.assert_called_once() mock_config.return_value.session.get.return_value.raise_for_status.assert_called_once( )
def test_get_bg_id_raises_no_business_group(self, mock_config, mock_catalog): mock_config.return_value.session.get.return_value.text = '{"content":[{"entitledOrganizations":[{"subtenantLabel":"wrong_bg"}]}]}' with self.assertRaises(VraSdkMainException): VraSdk(MagicMock(), "") mock_config.return_value.session.get.assert_called_once() mock_config.return_value.session.get.return_value.raise_for_status.assert_called_once( )
def test_get_catalog(self, mock_resolve, mock_os, mock_config, mock_get_bg_id): 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":[{"catalogItem":{"name":"fake_name","id":"fake_id"}}]}' VraSdk(MagicMock(), "").get_catalog() mock_config.return_value.session.get.assert_any_call( 'https://fake_server/catalog-service/api/consumer/entitledCatalogItems?limit=9999', verify=False, timeout=12)
def test_init(self, mock_config, mock_get_catalog, mock_get_bg_id): mock_get_bg_id.return_value = 'fake_bg_id' authentication_object = MagicMock() authentication_object.domain = "fake_domain" mock_get_catalog.return_value = "fake_catalog" vra_sdk = VraSdk(authentication_object, "fake_bg") self.assertEqual(vra_sdk.catalog, "fake_catalog") self.assertEqual(vra_sdk.business_group, "fake_bg") self.assertEqual(vra_sdk.business_group_id, "fake_bg_id") mock_get_catalog.assert_called_once() mock_get_bg_id.assert_called_once()
def test_request_bg_id(self, mock_config, mock_catalog): mock_config.return_value.verify = False mock_config.return_value.timeout = 12 authentication_object = MagicMock() mock_config.return_value.vcac_server = 'fake_server' mock_config.return_value.session.get.return_value.text = '{"content":[{"entitledOrganizations":[{"subtenantLabel":"bg_label", "subtenantRef":"fake_bg_id"}]}]}' vra_sdk = VraSdk(authentication_object, "bg_label") self.assertEqual(vra_sdk.business_group_id, "fake_bg_id") mock_config.return_value.session.get.assert_called_once_with( 'https://fake_server/catalog-service/api/consumer/entitledCatalogItems?limit=998', verify=False, timeout=12) mock_config.return_value.session.get.return_value.raise_for_status.assert_called_once( )
def test_request_catalog_item(self, mock_payload, mock_request, mock_config, mock_get_catalog, mock_get_bg_id): mock_payload.return_value = 'fake_payload' mock_config.return_value.config_file = {'payload_default_version': 7} vra_sdk = VraSdk(MagicMock(), "") undecorrated = vra_sdk.request_catalog_item.__wrapped__ fake_kwargs = {"fake_kwargs": ""} result = undecorrated(vra_sdk, 'fake_catalog_name', **fake_kwargs) fake_kwargs['payload_version'] = 7 mock_payload.assert_called_once_with('catalog_item', 'fake_catalog_name', None, **fake_kwargs) mock_request.assert_called_once_with('fake_payload')
def test_request_resource_action(self, mock_payload, mock_request, mock_config, mock_get_catalog, mock_get_bg_id): mock_config.return_value.config_file = {'payload_default_version': 7} mock_payload.return_value = 'fake_payload' vra_sdk = VraSdk(MagicMock(), "") fake_kwargs = {"fake_key": "fake_value"} undecorrated = vra_sdk.request_resource_action.__wrapped__.__wrapped__ undecorrated(vra_sdk, 'fake_action_name', 'fake_resource_id', **fake_kwargs) fake_kwargs['payload_version'] = 7 mock_payload.assert_called_once_with('resource_action', 'fake_action_name', 'fake_resource_id', None, **fake_kwargs) mock_request.assert_called_once_with('fake_payload')
def test_get_data_raises(self, mock_request, mock_config, mock_catalog, mock_get_bg_id): mock_request.return_value.get_object.return_value = None vra_sdk = VraSdk(MagicMock(), '') with self.assertRaises(VraSdkMainException): vra_sdk.get_data('', '', '')
import os from pprint import pprint as pprint # credential loading from environment var domain = "foo.fuu.com" environment = os.environ['vra_environment'] login = os.environ['vra_login'] password = os.environ['vra_password'] ###### # authentication part VraConfig('config.json') auth = VraAuthenticate(environment).auth_login_password( login, password, domain) my_business_group = 'my_bg_name' my_client = VraSdk(auth, my_business_group) ###### # get data on one vm from name vm_name = 'my_vm_name' vm_data = my_client.get_data('vm', 'name', vm_name)[0] print(vm_data.id) ###### # get data on one vm from id vm_id = 'my_vm_id' vm_data = my_client.get_data('vm', 'id', vm_id) print(vm_data.name) # you can either get the data as dict or as json pprint(vm_data.to_json())