def test_creating_instance_when_response_is_ok(self): api = AtmosphereAPI('token', base_url=self.mock_users_base_url) payload = { "identity": "a5a6140d-1122-4581-87dc-bd9704fa07ec", "name": "myfirstinstance", "project": "7c8d34b1-1b2d-4f7f-bd62-4e0929295fd4", "size_alias": "100", "source_alias": "ec4fb434-a7b7-4c57-b882-0a1bf34506df", "allocation_source_id": "f4cca788-e039-4f82-bc77-9fb92141eca6", "scripts": [], "deploy": True, "extra": {} } response = api.create_instance(json.dumps(payload)) assert response.ok assert response.message['id'] == 1 and response.message['name'] == 'myfirstinstance'
def test_creating_instance_when_response_is_not_ok(self): api = AtmosphereAPI('token', base_url=self.mock_users_base_url) payload = { "identity": "a5a6140d-1122-4581-87dc-bd9704fa07ec", "name": "myfirstinstance", "project": "7c8d34b1-1b2d-4f7f-bd62-4e0929295fd4", "size_alias": "100", "source_alias": "ec4fb434-a7b7-4c57-b882-0a1bf34506df", "scripts": [], "deploy": True, "extra": {} } response = api.create_instance(json.dumps(payload)) assert not response.ok assert response.message['errors'][0]['code'] == 400 assert response.message['errors'][0]['message']['allocation_source_id'] == 'This field is required.'
def test_creating_instance_when_size_is_invalid(self): api = AtmosphereAPI('token', base_url=self.mock_users_base_url) payload = { "identity": "a5a6140d-1122-4581-87dc-bd9704fa07ec", "name": "myfirstinstance", "project": "7c8d34b1-1b2d-4f7f-bd62-4e0929295fd4", "size_alias": "-1", "source_alias": "ec4fb434-a7b7-4c57-b882-0a1bf34506df", "allocation_source_id": "f4cca788-e039-4f82-bc77-9fb92141eca6", "scripts": [], "deploy": True, "extra": {} } response = api.create_instance(json.dumps(payload)) assert not response.ok assert response.message['errors'][0]['code'] == 413 assert response.message['errors'][0]['message'] == 'Size Not Available. Disk is 8 but image requires at least 20'
def take_action(self, parsed_args): api = AtmosphereAPI(self.app_args.auth_token, base_url=self.app_args.base_url, timeout=self.app_args.api_server_timeout, verify=self.app_args.verify_cert) payload = { "name": parsed_args.name, "identity": parsed_args.identity, "size_alias": parsed_args.size_alias, "project": parsed_args.project, "allocation_source_id": parsed_args.allocation_source_id, "deploy": parsed_args.deploy, "scripts": [], "extra": {} } if parsed_args.image: # get the provider from the identity payload['source_alias'] = None id_data = api.get_identity(parsed_args.identity) if id_data.ok: provider_id = id_data.message['provider']['id'] self.log.debug( 'Found provider id {} for identity uuid {}'.format( provider_id, parsed_args.identity)) # get the provider machine from the latest image version v_data = api.get_image_versions(parsed_args.image) if v_data.ok: # get latest image version latest_version = v_data.message['results'][-1] machines = latest_version['machines'] machine_id = next((m['uuid'] for m in machines if m['provider']['id'] == provider_id), None) self.log.debug( 'Found machine id {} for latest image version {} and provider id {}' .format(machine_id, latest_version['name'], provider_id)) payload['source_alias'] = machine_id else: self.app.stdout.write( 'Error, instance not created. Could not lookup latest image version from image uuid supplied: {}' .format(parsed_args.image)) else: self.app.stdout.write( 'Error, instance not created. Could not lookup provider from identity uuid supplied: {}' .format(parsed_args.identity)) else: payload['source_alias'] = parsed_args.source_alias self.log.debug('INPUT: {}'.format(json.dumps(payload))) instance = () column_headers = ('id', 'uuid', 'name', 'username', 'allocation_source', 'image_id', 'image_version', 'launched', 'image_size', 'provider') if payload['source_alias']: data = api.create_instance(json.dumps(payload)) if data.ok: message = data.message launched = ts_to_isodate(message['start_date']) instance = (message['id'], message['uuid'], message['name'], message['user']['username'], message['allocation_source']['name'], message['image']['id'], message['version']['name'], launched, message['size']['name'], message['provider']['name']) else: self.app.stdout.write( 'Error, instance not created! Make sure to supply name, identity, project, size, source (or image), and allocation_source.' ) return (column_headers, instance)