def diagnostics_to_dict(diagnostics): """ Extract api data from diagnostics QuerySet. """ entries = list() for diagnostic in diagnostics: # format source date if set formatted_source_date = None if diagnostic.source_date: formatted_source_date = utils.isoformat(diagnostic.source_date) entry = { 'source': diagnostic.source, 'created': utils.isoformat(diagnostic.created), 'message': diagnostic.message, 'details': diagnostic.details, 'level': diagnostic.level, } if formatted_source_date: entry['source_date'] = formatted_source_date entries.append(entry) return entries
def vm_to_dict(vm, detail=False): d = dict(id=vm.id, name=vm.name) d['links'] = util.vm_to_links(vm.id) if detail: d['user_id'] = vm.userid d['tenant_id'] = vm.project d['shared_to_project'] = vm.shared_to_project d['status'] = logic_utils.get_rsapi_state(vm) d['SNF:task_state'] = logic_utils.get_task_state(vm) d['progress'] = 100 if d['status'] == 'ACTIVE' else vm.buildpercentage d['hostId'] = vm.hostid d['updated'] = utils.isoformat(vm.updated) d['created'] = utils.isoformat(vm.created) d['flavor'] = { "id": vm.flavor_id, "links": util.flavor_to_links(vm.flavor_id) } d['image'] = { "id": vm.imageid, "links": util.image_to_links(vm.imageid) } d['suspended'] = vm.suspended metadata = dict((m.meta_key, m.meta_value) for m in vm.metadata.all()) d['metadata'] = metadata nics = vm.nics.all() active_nics = filter(lambda nic: nic.state == "ACTIVE", nics) active_nics.sort(key=lambda nic: nic.id) attachments = map(nic_to_attachments, active_nics) d['attachments'] = attachments d['addresses'] = attachments_to_addresses(attachments) d['volumes'] = [ v.id for v in vm.volumes.filter(deleted=False).order_by('id') ] # include the latest vm diagnostic, if set diagnostic = vm.get_last_diagnostic() if diagnostic: d['diagnostics'] = diagnostics_to_dict([diagnostic]) else: d['diagnostics'] = [] # Fixed d["security_groups"] = [{"name": "default"}] key_names = json.loads(vm.key_names) d["key_name"] = key_names[0] if len(key_names) > 0 else None d["SNF:key_names"] = key_names d["config_drive"] = "" d["accessIPv4"] = "" d["accessIPv6"] = "" fqdn = get_server_fqdn(vm, active_nics) d["SNF:fqdn"] = fqdn d["SNF:port_forwarding"] = get_server_port_forwarding( vm, active_nics, fqdn) d['deleted'] = vm.deleted d['SNF:rescue'] = vm.rescue return d
def vm_to_dict(vm, detail=False): d = dict(id=vm.id, name=vm.name) d['links'] = util.vm_to_links(vm.id) if detail: d['user_id'] = vm.userid d['tenant_id'] = vm.project d['shared_to_project'] = vm.shared_to_project d['status'] = logic_utils.get_rsapi_state(vm) d['SNF:task_state'] = logic_utils.get_task_state(vm) d['progress'] = 100 if d['status'] == 'ACTIVE' else vm.buildpercentage d['hostId'] = vm.hostid d['updated'] = utils.isoformat(vm.updated) d['created'] = utils.isoformat(vm.created) d['flavor'] = {"id": vm.flavor_id, "links": util.flavor_to_links(vm.flavor_id)} d['image'] = {"id": vm.imageid, "links": util.image_to_links(vm.imageid)} d['suspended'] = vm.suspended metadata = dict((m.meta_key, m.meta_value) for m in vm.metadata.all()) d['metadata'] = metadata nics = vm.nics.all() active_nics = filter(lambda nic: nic.state == "ACTIVE", nics) active_nics.sort(key=lambda nic: nic.id) attachments = map(nic_to_attachments, active_nics) d['attachments'] = attachments d['addresses'] = attachments_to_addresses(attachments) d['volumes'] = [v.id for v in vm.volumes.filter(deleted=False) .order_by('id')] # include the latest vm diagnostic, if set diagnostic = vm.get_last_diagnostic() if diagnostic: d['diagnostics'] = diagnostics_to_dict([diagnostic]) else: d['diagnostics'] = [] # Fixed d["security_groups"] = [{"name": "default"}] key_names = json.loads(vm.key_names) d["key_name"] = key_names[0] if len(key_names) > 0 else None d["SNF:key_names"] = key_names d["config_drive"] = "" d["accessIPv4"] = "" d["accessIPv6"] = "" fqdn = get_server_fqdn(vm, active_nics) d["SNF:fqdn"] = fqdn d["SNF:port_forwarding"] = get_server_port_forwarding(vm, active_nics, fqdn) d['deleted'] = vm.deleted d['SNF:rescue'] = vm.rescue return d
def image_to_dict(image, detail=True): d = dict(id=image['id'], name=image['name']) if detail: d['updated'] = utils.isoformat(date_parse(image['updated_at'])) d['created'] = utils.isoformat(date_parse(image['created_at'])) d['status'] = 'DELETED' if image['deleted_at'] else 'ACTIVE' d['progress'] = 100 if image['status'] == 'available' else 0 d['user_id'] = image['owner'] d['tenant_id'] = image['owner'] d['links'] = util.image_to_links(image["id"]) if image["properties"]: d['metadata'] = image['properties'] else: d['metadata'] = {} return d
def image_to_dict(image, detail=True): d = dict(id=image["id"], name=image["name"]) if detail: d["updated"] = utils.isoformat(date_parse(image["updated_at"])) d["created"] = utils.isoformat(date_parse(image["created_at"])) d["status"] = "DELETED" if image["deleted_at"] else "ACTIVE" d["progress"] = 100 if image["status"] == "available" else 0 d["user_id"] = image["owner"] d["tenant_id"] = image["owner"] d["links"] = util.image_to_links(image["id"]) if image["properties"]: d["metadata"] = image["properties"] else: d["metadata"] = {} return d
def volume_to_dict(volume, detail=True): data = { "id": str(volume.id), "display_name": display_null_field(volume.name), "links": util.volume_to_links(volume.id), } if detail: details = { "status": volume.status.lower(), "size": volume.size, "display_description": volume.description, "created_at": utils.isoformat(volume.created), "metadata": dict((m.key, m.value) for m in volume.metadata.all()), "snapshot_id": display_null_field(volume.source_snapshot_id), "source_volid": display_null_field(volume.source_volume_id), "image_id": display_null_field(volume.source_image_id), "attachments": get_volume_attachments(volume), "volume_type": volume.volume_type_id, "deleted": volume.deleted, "delete_on_termination": volume.delete_on_termination, "user_id": volume.userid, "tenant_id": volume.project, # "availabilit_zone": None, # "bootable": None, # "os-vol-tenant-attr:tenant_id": None, # "os-vol-host-attr:host": None, # "os-vol-mig-status-attr:name_id": None, # "os-vol-mig-status-attr:migstat": None, } data.update(details) return data
def volume_to_dict(volume, detail=True): data = { "id": str(volume.id), "display_name": display_null_field(volume.name), "links": util.volume_to_links(volume.id), } if detail: details = { "status": volume.status.lower(), "size": volume.size, "display_description": volume.description, "created_at": utils.isoformat(volume.created), "metadata": dict((m.key, m.value) for m in volume.metadata.all()), "snapshot_id": display_null_field(volume.source_snapshot_id), "source_volid": display_null_field(volume.source_volume_id), "image_id": display_null_field(volume.source_image_id), "attachments": get_volume_attachments(volume), "volume_type": volume.volume_type_id, "deleted": volume.deleted, "delete_on_termination": volume.delete_on_termination, "user_id": volume.userid, "tenant_id": volume.project, "shared_to_project": volume.shared_to_project, # "availabilit_zone": None, # "bootable": None, # "os-vol-tenant-attr:tenant_id": None, # "os-vol-host-attr:host": None, # "os-vol-mig-status-attr:name_id": None, # "os-vol-mig-status-attr:migstat": None, } data.update(details) return data
def authenticate(request): try: content_length = get_content_length(request) except faults.LengthRequired: content_length = None public_mode = True if not content_length else False d = defaultdict(dict) if not public_mode: req = utils.get_json_body(request) uuid = None try: token_id = req['auth']['token']['id'] except KeyError: try: token_id = req['auth']['passwordCredentials']['password'] uuid = req['auth']['passwordCredentials']['username'] except KeyError: raise faults.BadRequest( 'Malformed request: missing credentials') tenant = req['auth'].get('tenantName') if token_id is None: raise faults.BadRequest('Malformed request: missing token') try: user = AstakosUser.objects.get(auth_token=token_id) except AstakosUser.DoesNotExist: raise faults.Unauthorized('Invalid token') validate_user(user) if uuid is not None: if user.uuid != uuid: raise faults.Unauthorized('Invalid credentials') if tenant: if user.uuid != tenant: raise faults.BadRequest('Not conforming tenantName') d["access"]["token"] = { "id": user.auth_token, "expires": utils.isoformat(user.auth_token_expires), "tenant": {"id": user.uuid, "name": user.realname}} d["access"]["user"] = { "id": user.uuid, 'name': user.realname, "roles": [dict(id=str(g['id']), name=g['name']) for g in user.groups.values('id', 'name')], "roles_links": []} d["access"]["serviceCatalog"] = get_endpoints() if request.serialization == 'xml': return xml_response({'d': d}, 'api/access.xml') else: return json_response(d)
def vm_to_dict(vm, detail=False): d = dict(id=vm.id, name=vm.name) d['links'] = util.vm_to_links(vm.id) if detail: d['user_id'] = vm.userid d['tenant_id'] = vm.userid d['status'] = get_rsapi_state(vm) d['progress'] = 100 if get_rsapi_state(vm) == 'ACTIVE' \ else vm.buildpercentage d['hostId'] = vm.hostid d['updated'] = utils.isoformat(vm.updated) d['created'] = utils.isoformat(vm.created) d['flavor'] = { "id": vm.flavor.id, "links": util.flavor_to_links(vm.flavor.id) } d['image'] = { "id": vm.imageid, "links": util.image_to_links(vm.imageid) } d['suspended'] = vm.suspended metadata = dict((m.meta_key, m.meta_value) for m in vm.metadata.all()) d['metadata'] = metadata vm_nics = vm.nics.filter(state="ACTIVE").order_by("index") attachments = map(nic_to_dict, vm_nics) d['attachments'] = attachments d['addresses'] = nics_to_addresses(vm_nics) # include the latest vm diagnostic, if set diagnostic = vm.get_last_diagnostic() if diagnostic: d['diagnostics'] = diagnostics_to_dict([diagnostic]) else: d['diagnostics'] = [] # Fixed d["security_groups"] = [{"name": "default"}] d["key_name"] = None d["config_drive"] = "" d["accessIPv4"] = "" d["accessIPv6"] = "" return d
def image_to_dict(image, detail=True): d = dict(id=image['id'], name=image['name']) if detail: d['updated'] = utils.isoformat(date_parse(image['updated_at'])) d['created'] = utils.isoformat(date_parse(image['created_at'])) img_status = image.get("status", "").upper() status = API_STATUS_FROM_IMAGE_STATUS.get(img_status, "UNKNOWN") d['status'] = status d['progress'] = 100 if status == 'ACTIVE' else 0 d['user_id'] = image['owner'] d['tenant_id'] = image['owner'] d['public'] = image["is_public"] d['links'] = util.image_to_links(image["id"]) if image["properties"]: d['metadata'] = image['properties'] else: d['metadata'] = {} d["is_snapshot"] = image["is_snapshot"] return d
def printable_header_dict(d): """Format a meta dictionary for printing out json/xml. Convert all keys to lower case and replace dashes with underscores. Format 'last_modified' timestamp. """ if 'last_modified' in d and d['last_modified']: d['last_modified'] = utils.isoformat( datetime.fromtimestamp(d['last_modified'])) return dict([(k.lower().replace('-', '_'), v) for k, v in d.iteritems()])
def printable_header_dict(d): """Format a meta dictionary for printing out json/xml. Convert all keys to lower case and replace dashes with underscores. Format 'last_modified' timestamp. """ timestamps = ("last_modified", "x_container_until_timestamp", "x_acount_until_timestamp") for timestamp in timestamps: if timestamp in d and d[timestamp]: d[timestamp] = utils.isoformat(datetime.fromtimestamp(d[timestamp])) return dict([(k.lower().replace("-", "_"), v) for k, v in d.iteritems()])
def vm_to_dict(vm, detail=False): d = dict(id=vm.id, name=vm.name) d['links'] = util.vm_to_links(vm.id) if detail: d['user_id'] = vm.userid d['tenant_id'] = vm.userid d['status'] = get_rsapi_state(vm) d['progress'] = 100 if get_rsapi_state(vm) == 'ACTIVE' \ else vm.buildpercentage d['hostId'] = vm.hostid d['updated'] = utils.isoformat(vm.updated) d['created'] = utils.isoformat(vm.created) d['flavor'] = {"id": vm.flavor.id, "links": util.flavor_to_links(vm.flavor.id)} d['image'] = {"id": vm.imageid, "links": util.image_to_links(vm.imageid)} d['suspended'] = vm.suspended metadata = dict((m.meta_key, m.meta_value) for m in vm.metadata.all()) d['metadata'] = metadata vm_nics = vm.nics.filter(state="ACTIVE").order_by("index") attachments = map(nic_to_dict, vm_nics) d['attachments'] = attachments d['addresses'] = nics_to_addresses(vm_nics) # include the latest vm diagnostic, if set diagnostic = vm.get_last_diagnostic() if diagnostic: d['diagnostics'] = diagnostics_to_dict([diagnostic]) else: d['diagnostics'] = [] # Fixed d["security_groups"] = [{"name": "default"}] d["key_name"] = None d["config_drive"] = "" d["accessIPv4"] = "" d["accessIPv6"] = "" return d
def network_to_dict(network, user_id, detail=True): d = {'id': str(network.id), 'name': network.name} d['links'] = util.network_to_links(network.id) if detail: d['user_id'] = network.userid d['tenant_id'] = network.userid d['cidr'] = network.subnet d['cidr6'] = network.subnet6 d['gateway'] = network.gateway d['gateway6'] = network.gateway6 d['dhcp'] = network.dhcp d['type'] = network.flavor d['updated'] = utils.isoformat(network.updated) d['created'] = utils.isoformat(network.created) d['status'] = network.state d['public'] = network.public attachments = [util.construct_nic_id(nic) for nic in network.nics.filter(machine__userid=user_id) .filter(state="ACTIVE") .order_by('machine')] d['attachments'] = attachments return d
def printable_header_dict(d): """Format a meta dictionary for printing out json/xml. Convert all keys to lower case and replace dashes with underscores. Format 'last_modified' timestamp. """ timestamps = ('last_modified', 'x_container_until_timestamp', 'x_acount_until_timestamp') for timestamp in timestamps: if timestamp in d and d[timestamp]: d[timestamp] = utils.isoformat( datetime.fromtimestamp(d[timestamp])) return dict([(k.lower().replace('-', '_'), v) for k, v in d.iteritems()])
def snapshot_to_dict(snapshot, detail=True): owner = snapshot['owner'] status = snapshot.get('status', "unknown").upper() status = API_STATUS_FROM_IMAGE_STATUS.get(status, "UNKNOWN") progress = "%s%%" % 100 if status == "AVAILABLE" else 0 data = { "id": snapshot["id"], "size": int(snapshot["size"]) >> 30, # gigabytes "display_name": snapshot["name"], "display_description": snapshot.get("description", ""), "status": status, "user_id": owner, "tenant_id": owner, "os-extended-snapshot-attribute:progress": progress, # "os-extended-snapshot-attribute:project_id": project, "created_at": utils.isoformat(date_parse(snapshot["created_at"])), "metadata": snapshot.get("metadata", {}), "volume_id": snapshot.get("volume_id"), "links": util.snapshot_to_links(snapshot["id"]) } return data
def _dthandler(obj): if isinstance(obj, datetime.datetime): return isoformat(obj) else: raise TypeError
def authenticate(request): try: content_length = get_content_length(request) except faults.LengthRequired: content_length = None public_mode = True if not content_length else False d = defaultdict(dict) if not public_mode: req = utils.get_json_body(request) uuid = None try: token_id = req['auth']['token']['id'] except KeyError: try: token_id = req['auth']['passwordCredentials']['password'] uuid = req['auth']['passwordCredentials']['username'] except KeyError: raise faults.BadRequest( 'Malformed request: missing credentials') tenant = req['auth'].get('tenantName') if token_id is None: raise faults.BadRequest('Malformed request: missing token') try: user = AstakosUser.objects.get(auth_token=token_id) except AstakosUser.DoesNotExist: raise faults.Unauthorized('Invalid token') validate_user(user) if uuid is not None: if user.uuid != uuid: raise faults.Unauthorized('Invalid credentials') if tenant: if user.uuid != tenant: raise faults.BadRequest('Not conforming tenantName') user_projects = user.project_set.filter(projectmembership__state__in=\ ProjectMembership.ACCEPTED_STATES).values_list("uuid", flat=True) d["access"]["token"] = { "id": user.auth_token, "expires": utils.isoformat(user.auth_token_expires), "tenant": { "id": user.uuid, "name": user.realname } } d["access"]["user"] = { "id": user.uuid, 'name': user.realname, "roles": [ dict(id=str(g['id']), name=g['name']) for g in user.groups.values('id', 'name') ], "roles_links": [], "projects": list(user_projects), } d["access"]["serviceCatalog"] = get_endpoints() if request.serialization == 'xml': return xml_response({'d': d}, 'api/access.xml') else: return json_response(d)
def authenticate(request): try: content_length = get_content_length(request) except faults.LengthRequired: content_length = None public_mode = True if not content_length else False d = defaultdict(dict) if not public_mode: req = utils.get_request_dict(request) uuid = None try: token_id = req['auth']['token']['id'] except KeyError: try: token_id = req['auth']['passwordCredentials']['password'] uuid = req['auth']['passwordCredentials']['username'] except KeyError: raise faults.BadRequest( 'Malformed request: missing credentials') tenant = req['auth'].get('tenantName') if token_id is None: raise faults.BadRequest('Malformed request: missing token') try: user = AstakosUser.objects.get(auth_token=token_id) except AstakosUser.DoesNotExist: raise faults.Unauthorized('Invalid token') validate_user(user) if uuid is not None: if user.uuid != uuid: raise faults.Unauthorized('Invalid credentials') if tenant: if user.uuid != tenant: raise faults.BadRequest('Not conforming tenantName') d["access"]["token"] = { "id": user.auth_token, "expires": utils.isoformat(user.auth_token_expires), "tenant": {"id": user.uuid, "name": user.realname}} d["access"]["user"] = { "id": user.uuid, 'name': user.realname, "roles": list(user.groups.values("id", "name")), "roles_links": []} d["access"]["serviceCatalog"] = [] append = d["access"]["serviceCatalog"].append for s in Service.objects.all().order_by("id"): endpoints = [] for l in [e.data.values('key', 'value') for e in s.endpoints.all()]: endpoint = dict((d['key'], d['value']) for d in l) endpoint["SNF:uiURL"] = s.component.url endpoint["region"] = "default" if s.name == 'astakos_weblogin': endpoint["SNF:webloginURL"] = endpoint["publicURL"] endpoints.append(endpoint) append({"name": s.name, "type": s.type, "endpoints": endpoints, "endpoints_links": []}) if request.serialization == 'xml': return xml_response({'d': d}, 'api/access.xml') else: return json_response(d)