def create_server(request): # Normal Response Code: 202 # Error Response Codes: computeFault (400, 500), # serviceUnavailable (503), # unauthorized (401), # badMediaType(415), # itemNotFound (404), # badRequest (400), # serverCapacityUnavailable (503), # overLimit (413) req = utils.get_json_body(request) user_id = request.user_uniq log.info('create_server user: %s request: %s', user_id, req) try: server = req['server'] name = server['name'] metadata = server.get('metadata', {}) assert isinstance(metadata, dict) image_id = server['imageRef'] flavor_id = server['flavorRef'] personality = server.get('personality', []) assert isinstance(personality, list) networks = server.get("networks") if networks is not None: assert isinstance(networks, list) project = server.get("project") except (KeyError, AssertionError): raise faults.BadRequest("Malformed request") volumes = None dev_map = server.get("block_device_mapping_v2") if dev_map is not None: volumes = parse_block_device_mapping(dev_map) # Verify that personalities are well-formed util.verify_personality(personality) # Get flavor (ensure it is active) flavor = util.get_flavor(flavor_id, include_deleted=False) if not flavor.allow_create: msg = ("It is not allowed to create a server from flavor with id '%d'," " see 'allow_create' flavor attribute") raise faults.Forbidden(msg % flavor.id) # Generate password password = util.random_password() vm = servers.create(user_id, name, password, flavor, image_id, metadata=metadata, personality=personality, project=project, networks=networks, volumes=volumes) server = vm_to_dict(vm, detail=True) server['status'] = 'BUILD' server['adminPass'] = password response = render_server(request, server, status=202) return response
def create_server(request): # Normal Response Code: 202 # Error Response Codes: computeFault (400, 500), # serviceUnavailable (503), # unauthorized (401), # badMediaType(415), # itemNotFound (404), # badRequest (400), # serverCapacityUnavailable (503), # overLimit (413) req = utils.get_request_dict(request) user_id = request.user_uniq log.info('create_server user: %s request: %s', user_id, req) try: server = req['server'] name = server['name'] metadata = server.get('metadata', {}) assert isinstance(metadata, dict) image_id = server['imageRef'] flavor_id = server['flavorRef'] personality = server.get('personality', []) assert isinstance(personality, list) networks = server.get("networks") if networks is not None: assert isinstance(networks, list) except (KeyError, AssertionError): raise faults.BadRequest("Malformed request") # Verify that personalities are well-formed util.verify_personality(personality) # Get image information image = util.get_image_dict(image_id, user_id) # Get flavor (ensure it is active) flavor = util.get_flavor(flavor_id, include_deleted=False) if not flavor.allow_create: msg = ("It is not allowed to create a server from flavor with id '%d'," " see 'allow_create' flavor attribute") raise faults.Forbidden(msg % flavor.id) # Generate password password = util.random_password() vm = servers.create(user_id, name, password, flavor, image, metadata=metadata, personality=personality, networks=networks) server = vm_to_dict(vm, detail=True) server['status'] = 'BUILD' server['adminPass'] = password response = render_server(request, server, status=202) return response
def create_server(request): # Normal Response Code: 202 # Error Response Codes: computeFault (400, 500), # serviceUnavailable (503), # unauthorized (401), # badMediaType(415), # itemNotFound (404), # badRequest (400), # serverCapacityUnavailable (503), # overLimit (413) req = utils.get_request_dict(request) log.info('create_server %s', req) user_id = request.user_uniq try: server = req['server'] name = server['name'] metadata = server.get('metadata', {}) assert isinstance(metadata, dict) image_id = server['imageRef'] flavor_id = server['flavorRef'] personality = server.get('personality', []) assert isinstance(personality, list) except (KeyError, AssertionError): raise faults.BadRequest("Malformed request") # Verify that personalities are well-formed util.verify_personality(personality) # Get image information image = util.get_image_dict(image_id, user_id) # Get flavor (ensure it is active) flavor = util.get_flavor(flavor_id, include_deleted=False) # Generate password password = util.random_password() vm = do_create_server(user_id, name, password, flavor, image, metadata=metadata, personality=personality) server = vm_to_dict(vm, detail=True) server['status'] = 'BUILD' server['adminPass'] = password response = render_server(request, server, status=202) return response
def create_server(request): # Normal Response Code: 202 # Error Response Codes: computeFault (400, 500), # serviceUnavailable (503), # unauthorized (401), # badMediaType(415), # itemNotFound (404), # badRequest (400), # serverCapacityUnavailable (503), # overLimit (413) req = utils.get_json_body(request) credentials = request.credentials user_id = credentials.userid log.info("User: %s, Action: create_server, Request: %s", user_id, req) try: server = req['server'] name = server['name'] metadata = server.get('metadata', {}) assert isinstance(metadata, dict) image_id = server['imageRef'] flavor_id = server['flavorRef'] personality = server.get('personality', []) assert isinstance(personality, list) networks = server.get("networks") if networks is not None: assert isinstance(networks, list) project = server.get("project") shared_to_project = server.get("shared_to_project", False) key_name = server.get('key_name') user_data = server.get('user_data', "") SNF_key_names = server.get('SNF:key_names', []) assert isinstance(SNF_key_names, list) except (KeyError, AssertionError): raise faults.BadRequest("Malformed request") volumes = None dev_map = server.get("block_device_mapping_v2") if dev_map is not None: allowed_types = VOLUME_SOURCE_TYPES[:] if snapshots_enabled_for_user(request.user): allowed_types.append('snapshot') volumes = parse_block_device_mapping(dev_map, allowed_types) # If no project is provided, use the user's system project as default. if project is None: project = user_id # Verify that personalities are well-formed util.verify_personality(personality) # Verify that user_data are well-formed util.verify_user_data(user_data) # Get flavor (ensure it is active and project has access) flavor = util.get_flavor(flavor_id, credentials, include_deleted=False, for_project=project) if not util.can_create_flavor(flavor, request.user): msg = ("It is not allowed to create a server from flavor with id '%d'," " see 'allow_create' flavor attribute") raise faults.Forbidden(msg % flavor.id) # Generate password password = util.random_password() if key_name is not None: # If both key_name and SNF:key_names are provided we should # raise an error if len(SNF_key_names) > 0: raise faults.BadRequest('Only one of the SNF:key_names and' 'key_name can be set') # If only key_name is provided then we will set key_names as # a list with only one element else: key_names = [key_name] else: # In case key_name is not provided we will set key_names to the # value of SNF:key_names. We don't need to check if it is provided # since even if it is not, its value will be [] # Remove duplicate key names key_names = list(set(SNF_key_names)) vm = servers.create(credentials, name, password, flavor, image_id, metadata=metadata, personality=personality, user_data=user_data, project=project, networks=networks, volumes=volumes, shared_to_project=shared_to_project, key_names=key_names) log.info("User %s created VM %s, shared: %s", user_id, vm.id, shared_to_project) server = vm_to_dict(vm, detail=True) server['status'] = 'BUILD' server['adminPass'] = password set_password_in_cache(server['id'], password) response = render_server(request, server, status=202) return response