def create_instance(self, start_date=None): if not start_date: start_date = timezone.now() provider_alias = str(uuid.uuid4()) identity = Identity.objects.filter(created_by=self.user).last() if not identity: identity = Identity(created_by=self.user, provider=self.provider) identity.save() instance_source = InstanceSource( provider=self.provider, identifier=str(uuid.uuid4()), created_by=self.user, created_by_identity=identity ) instance_source.save() instance = Instance( source=instance_source, provider_alias=provider_alias, created_by=self.user, start_date=start_date ) instance.save() self.create_instance_status_history( instance, start_date=start_date, status='active' ) return instance
def create_instance(self, start_date=None): if not start_date: start_date = timezone.now() provider_alias = str(uuid.uuid4()) identity = Identity.objects.filter(created_by=self.user).last() if not identity: identity = Identity(created_by=self.user, provider=self.provider) identity.save() instance_source = InstanceSource(provider=self.provider, identifier=str(uuid.uuid4()), created_by=self.user, created_by_identity=identity) instance_source.save() instance = Instance(source=instance_source, provider_alias=provider_alias, created_by=self.user, start_date=start_date) instance.save() self.create_instance_status_history(instance, start_date=start_date, status='active') return instance
def create_identity(self, username, password, project_name, max_quota=False, account_admin=False): if not self.core_provider: raise Exception("AccountDriver not initialized by provider, " "cannot create identity") identity = Identity.create_identity( username, self.core_provider.location, #Flags.. max_quota=max_quota, account_admin=account_admin, ##Pass in credentials with cred_ namespace cred_key=username, cred_secret=password, cred_ex_tenant_name=project_name, cred_ex_project_name=project_name) #Return the identity return identity
def get_identity_list(user, provider=None): """ Given the (request) user return all identities on all active providers """ identity_list = CoreIdentity.shared_with_user(user) if provider: identity_list = identity_list.filter(provider=provider) return identity_list
def create_identity(self, username, access_key, secret_key, max_quota=False, account_admin=False): """ """ identity = Identity.create_identity( username, self.core_provider.location, max_quota=max_quota, account_admin=account_admin, cred_key=access_key, cred_secret=secret_key) return identity
def create_identity(self, username, access_key, secret_key, max_quota=False, account_admin=False): """ max_quota - Set this user to have the maximum quota, instead of the default quota """ identity = Identity.create_identity( username, self.core_provider.location, max_quota=max_quota, account_admin=account_admin, cred_key=access_key, cred_secret=secret_key) return identity
def get_identity(user, identity_uuid): """ Given the (request) user and an identity uuid, return None or an Active Identity """ try: identity_list = get_identity_list(user) if not identity_list: raise CoreIdentity.DoesNotExist("No identities found for user %s" % user.username) identity = identity_list.get(uuid=identity_uuid) return identity except CoreIdentity.DoesNotExist: logger.warn("Identity %s DoesNotExist" % identity_uuid) return None
def create_identity(self, username, access_key, secret_key, max_quota=False, account_admin=False): """ """ identity = Identity.create_identity(username, self.core_provider.location, max_quota=max_quota, account_admin=account_admin, cred_key=access_key, cred_secret=secret_key) return identity
def create_identity(self, username, access_key, secret_key, max_quota=False, account_admin=False): """ max_quota - Set this user to have the maximum quota, instead of the default quota """ identity = Identity.create_identity(username, self.core_provider.location, max_quota=max_quota, account_admin=account_admin, cred_key=access_key, cred_secret=secret_key) return identity
def create_identity(self, username, password, project_name, quota=None, max_quota=False, account_admin=False): if not self.core_provider: raise Exception("AccountDriver not initialized by provider, " "cannot create identity") identity = Identity.create_identity( username, self.core_provider.location, quota=quota, #Flags.. max_quota=max_quota, account_admin=account_admin, ##Pass in credentials with cred_ namespace cred_key=username, cred_secret=password, cred_ex_tenant_name=project_name, cred_ex_project_name=project_name) #Return the identity return identity
def prepare_driver(request, provider_uuid, identity_uuid, raise_exception=False): """ Return an rtwo.EshDriver for the given provider_uuid and identity_uuid. If invalid credentials, provider_uuid or identity_uuid is used return None. """ try: core_identity = CoreIdentity.shared_with_user(request.user)\ .get(provider__uuid=provider_uuid, uuid=identity_uuid) return get_esh_driver(core_identity=core_identity) except (CoreIdentity.DoesNotExist, ValueError): logger.exception("Unable to prepare driver.") if raise_exception: raise ValueError( "User %s is NOT the owner of Identity UUID: %s" % (request.user.username, core_identity.uuid)) return None
def prepare_driver(request, provider_uuid, identity_uuid, raise_exception=False): """ Return an rtwo.EshDriver for the given provider_uuid and identity_uuid. If invalid credentials, provider_uuid or identity_uuid is used return None. """ try: core_identity = CoreIdentity.shared_with_user(request.user)\ .get(provider__uuid=provider_uuid, uuid=identity_uuid) return get_esh_driver(core_identity=core_identity) except (CoreIdentity.DoesNotExist, ValueError): logger.exception("Unable to prepare driver.") if raise_exception: raise ValueError("User %s is NOT the owner of Identity UUID: %s" % (request.user.username, core_identity.uuid)) return None
def get(self, request, provider_uuid, identity_uuid): """ Returns a list of all instances """ user = request.user try: esh_driver = prepare_driver(request, provider_uuid, identity_uuid) except ProviderNotActive as pna: return inactive_provider(pna) except Exception as e: return failure_response( status.HTTP_409_CONFLICT, e.message) if not esh_driver: return invalid_creds(provider_uuid, identity_uuid) identity = Identity.shared_with_user(user).get(uuid=identity_uuid) try: esh_instance_list = get_cached_instances(identity=identity) except LibcloudBadResponseError: return malformed_response(provider_uuid, identity_uuid) except (socket_error, ConnectionFailure): return connection_failure(provider_uuid, identity_uuid) except LibcloudInvalidCredsError: return invalid_creds(provider_uuid, identity_uuid) core_instance_list = [convert_esh_instance(esh_driver, inst, provider_uuid, identity_uuid, user) for inst in esh_instance_list] # TODO: Core/Auth checks for shared instances serialized_data = InstanceSerializer(core_instance_list, context={"request": request}, many=True).data response = Response(serialized_data) response['Cache-Control'] = 'no-cache' return response
def current_identities(self): return Identity.shared_with_group(self).filter(only_current_provider(), only_active_provider())
def delete_account(self, username, projectname): self.os_delete_account(username, projectname) Identity.delete_identity(username, self.core_provider.location)
def post(self, request, provider_uuid, identity_uuid, format=None): """ Instance Class: Launches an instance based on the params Returns a single instance Parameters: machine_alias, size_alias, username TODO: Create a 'reverse' using the instance-id to pass the URL for the newly created instance I.e: url = "/provider/1/instance/1/i-12345678" """ data = request.data user = request.user # Check the data is valid missing_keys = valid_post_data(data) if missing_keys: return keys_not_found(missing_keys) identity = Identity.shared_with_user( user, is_leader=True).filter(uuid=identity_uuid).first() if not identity: failure_msg = "User %s does not have permission to POST with this identity. Promote user to leader or use a different Identity." % ( user, ) return failure_response(status.HTTP_403_FORBIDDEN, failure_msg) # Pass these as args size_alias = data.pop("size_alias") allocation_source_uuid = data.pop("allocation_source_uuid", None) machine_alias = data.pop("machine_alias") hypervisor_name = data.pop("hypervisor", None) if hypervisor_name: # Previous method passed this with 'None' but that fails now. # This check will only add the ex_ value if it is 'truthy'. data['ex_hypervisor_name'] = hypervisor_name deploy = data.pop("deploy", True) if type(deploy) in [str, unicode] and deploy.lower() == "false": deploy = False elif not isinstance(deploy, bool): deploy = True boot_scripts = data.pop("scripts", []) try: logger.debug(data) allocation_source = AllocationSource.objects.get( uuid=allocation_source_uuid) core_instance = launch_instance( user, identity_uuid, size_alias, machine_alias, deploy=deploy, allocation_source=allocation_source, **data) except UnderThresholdError as ute: return under_threshold(ute) except OverQuotaError as oqe: return over_quota(oqe) except OverAllocationError as oae: return over_quota(oae) except Unauthorized as auth_invalid: return invalid_creds(provider_uuid, identity_uuid) except SizeNotAvailable as snae: return size_not_available(snae) except SecurityGroupNotCreated: return connection_failure(provider_uuid, identity_uuid) except (socket_error, ConnectionFailure): return connection_failure(provider_uuid, identity_uuid) except LibcloudInvalidCredsError: return invalid_creds(provider_uuid, identity_uuid) except Exception as exc: logger.exception("Encountered a generic exception. " "Returning 409-CONFLICT") return failure_response(status.HTTP_409_CONFLICT, str(exc.message)) serializer = InstanceSerializer(core_instance, context={"request": request}, data=data) if serializer.is_valid(): instance = serializer.save() if boot_scripts: _save_scripts_to_instance(instance, boot_scripts) instance.change_allocation_source(allocation_source) logger.info( "DEBUG- Instance launch completed - Returning instance %s (%s) to user %s" % (instance, instance.created_by_identity, request.user)) return Response(serializer.data, status=status.HTTP_201_CREATED) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def post(self, request, provider_uuid, identity_uuid, format=None): """ Instance Class: Launches an instance based on the params Returns a single instance Parameters: machine_alias, size_alias, username TODO: Create a 'reverse' using the instance-id to pass the URL for the newly created instance I.e: url = "/provider/1/instance/1/i-12345678" """ data = request.data user = request.user # Check the data is valid missing_keys = valid_post_data(data) if missing_keys: return keys_not_found(missing_keys) identity = Identity.shared_with_user(user, is_leader=True).filter(uuid=identity_uuid).first() if not identity: failure_msg = "User %s does not have permission to POST with this identity. Promote user to leader or use a different Identity." % (user,) return failure_response(status.HTTP_403_FORBIDDEN, failure_msg) # Pass these as args size_alias = data.pop("size_alias") allocation_source_uuid = data.pop("allocation_source_uuid",None) machine_alias = data.pop("machine_alias") hypervisor_name = data.pop("hypervisor", None) if hypervisor_name: # Previous method passed this with 'None' but that fails now. # This check will only add the ex_ value if it is 'truthy'. data['ex_hypervisor_name'] = hypervisor_name deploy = data.pop("deploy", True) if type(deploy) in [str, unicode] and deploy.lower() == "false": deploy = False elif not isinstance(deploy, bool): deploy = True boot_scripts = data.pop("scripts", []) try: logger.debug(data) allocation_source = AllocationSource.objects.get( uuid=allocation_source_uuid) core_instance = launch_instance( user, identity_uuid, size_alias, machine_alias, deploy=deploy, allocation_source=allocation_source, **data) except UnderThresholdError as ute: return under_threshold(ute) except OverQuotaError as oqe: return over_quota(oqe) except OverAllocationError as oae: return over_quota(oae) except AllocationBlacklistedError as e: return failure_response( status.HTTP_403_FORBIDDEN, e.message) except Unauthorized: return invalid_creds(provider_uuid, identity_uuid) except SizeNotAvailable as snae: return size_not_available(snae) except SecurityGroupNotCreated: return connection_failure(provider_uuid, identity_uuid) except (socket_error, ConnectionFailure): return connection_failure(provider_uuid, identity_uuid) except LibcloudInvalidCredsError: return invalid_creds(provider_uuid, identity_uuid) except Exception as exc: logger.exception("Encountered a generic exception. " "Returning 409-CONFLICT") return failure_response(status.HTTP_409_CONFLICT, str(exc.message)) serializer = InstanceSerializer(core_instance, context={"request": request}, data=data) if serializer.is_valid(): instance = serializer.save() if boot_scripts: _save_scripts_to_instance(instance, boot_scripts) instance.change_allocation_source(allocation_source) logger.info("DEBUG- Instance launch completed - Returning instance %s (%s) to user %s" % (instance, instance.created_by_identity, request.user)) return Response(serializer.data, status=status.HTTP_201_CREATED) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def current_identities(self): return Identity.shared_with_group(self).filter(only_current_provider())