def _update_machine(self, request, provider_uuid, identity_uuid, machine_id): # TODO: Determine who is allowed to edit machines besides # core_machine.owner user = request.user data = request.DATA esh_driver = prepare_driver(request, provider_uuid, identity_uuid) if not esh_driver: return invalid_creds(provider_uuid, identity_uuid) esh_machine = esh_driver.get_machine(machine_id) core_machine = convert_esh_machine(esh_driver, esh_machine, provider_uuid, user) if not user.is_staff and user is not core_machine.application_version.application.created_by: logger.warn("%s is Non-staff/non-owner trying to update a machine" % (user.username)) return failure_response( status.HTTP_401_UNAUTHORIZED, "Only Staff and the machine Owner " "are allowed to change machine info." ) partial_update = True if request.method == "PATCH" else False serializer = ProviderMachineSerializer( core_machine, request_user=request.user, data=data, partial=partial_update ) if serializer.is_valid(): logger.info("metadata = %s" % data) update_machine_metadata(esh_driver, esh_machine, data) machine = serializer.save() if "created_by_identity" in request.DATA: identity = machine.created_by_identity update_application_owner(core_machine.application_version.application, identity) logger.info(serializer.data) return Response(serializer.data) return failure_response(status.HTTP_400_BAD_REQUEST, serializer.errors)
def put(self, request, provider_id, identity_id, machine_id): """ TODO: Determine who is allowed to edit machines besides core_machine.owner """ user = request.user data = request.DATA esh_driver = prepare_driver(request, provider_id, identity_id) if not esh_driver: return invalid_creds(provider_id, identity_id) esh_machine = esh_driver.get_machine(machine_id) core_machine = convert_esh_machine(esh_driver, esh_machine, provider_id) if not user.is_staff\ and user is not core_machine.application.created_by: logger.error('Non-staff/non-owner trying to update a machine') return failure_response( status.HTTP_401_UNAUTHORIZED, 'Only Staff and the machine Owner ' + 'are allowed to change machine info.') core_machine.application.update(data) serializer = ProviderMachineSerializer(core_machine, data=data, partial=True) if serializer.is_valid(): logger.info('metadata = %s' % data) update_machine_metadata(esh_driver, esh_machine, data) serializer.save() logger.info(serializer.data) return Response(serializer.data) return failure_response( status.HTTP_400_BAD_REQUEST, serializer.errors)
def patch(self, request, provider_id, identity_id, machine_id): """ TODO: Determine who is allowed to edit machines besides coreMachine.owner """ user = request.user data = request.DATA esh_driver = prepare_driver(request, identity_id) esh_machine = esh_driver.get_machine(machine_id) coreMachine = convert_esh_machine(esh_driver, esh_machine, provider_id) if not user.is_staff and user is not coreMachine.application.created_by: logger.warn('%s is Non-staff/non-owner trying to update a machine' % (user.username)) errorObj = failureJSON([{ 'code': 401, 'message': 'Only Staff and the machine Owner ' + 'are allowed to change machine info.'}]) return Response(errorObj, status=status.HTTP_401_UNAUTHORIZED) coreMachine.application.update(request.DATA) serializer = ProviderMachineSerializer(coreMachine, data=data, partial=True) if serializer.is_valid(): logger.info('metadata = %s' % data) update_machine_metadata(esh_driver, esh_machine, data) serializer.save() logger.info(serializer.data) return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def put(self, request, provider_id, identity_id, machine_id): """ TODO: Determine who is allowed to edit machines besides core_machine.owner """ user = request.user data = request.DATA esh_driver = prepare_driver(request, provider_id, identity_id) if not esh_driver: return invalid_creds(provider_id, identity_id) esh_machine = esh_driver.get_machine(machine_id) core_machine = convert_esh_machine(esh_driver, esh_machine, provider_id) if not user.is_staff\ and user is not core_machine.application.created_by: logger.error('Non-staff/non-owner trying to update a machine') return failure_response( status.HTTP_401_UNAUTHORIZED, 'Only Staff and the machine Owner ' + 'are allowed to change machine info.') core_machine.application.update(data) serializer = ProviderMachineSerializer(core_machine, data=data, partial=True) if serializer.is_valid(): logger.info('metadata = %s' % data) update_machine_metadata(esh_driver, esh_machine, data) serializer.save() logger.info(serializer.data) return Response(serializer.data) return failure_response(status.HTTP_400_BAD_REQUEST, serializer.errors)
def _os_update_owner(provider_machine, tenant_name): from core.models import Provider from service.driver import get_admin_driver from service.cache import get_cached_machines, get_cached_driver provider = provider_machine.provider if provider not in Provider.get_active(type_name='openstack'): raise Exception("An active openstack provider is required to" " update image owner") esh_driver = get_cached_driver(provider) if not esh_driver: raise Exception("The account driver of Provider %s is required to" " update image metadata" % provider) esh_machines = get_cached_machines(provider, force=True) esh_machine = [ mach for mach in esh_machines if mach.alias == provider_machine.identifier ] if not esh_machine: raise Exception("Machine with ID %s not found" % provider_machine.identifier) esh_machine = esh_machine[0] tenant_id = _tenant_name_to_id(provider_machine.provider, tenant_name) update_machine_metadata(esh_driver, esh_machine, { "owner": tenant_id, "application_owner": tenant_name })
def write_app_to_metadata(application, provider_machine, **extras): image_kwargs= {} image_id = provider_machine.identifier #These calls are better served connecting to chromogenic Image Manager accounts = get_os_account_driver(provider_machine.provider) if not accounts: raise Exception("The account driver of Provider %s is required to" " write image metadata" % provider_machine.provider) image = accounts.image_manager.get_image(image_id) if not image: raise Exception("The account driver of Provider %s could not find" " the image: %s" % (provider_machine.provider,image_id)) return img_properties = image.properties properties = { # Specific to the provider machine "application_version": str(provider_machine.version), # Specific to the application "application_uuid": application.uuid, "application_name": application.name, "application_owner": application.created_by.username, "application_tags": json.dumps( [tag.name for tag in application.tags.all()]), "application_description": application.description, } properties.update(extras) #NOTE: DON'T overlook the fact that there is metadata # that you aren't over control of. Make sure you have that too! img_properties.update(properties) #Setting 'properties' will override ALL properties of the img. USE # CAREFULLY (Like above) image_kwargs['properties'] = img_properties #Set the owner explicitly if necessary if 'owner' in extras: image_kwargs['owner'] = extras['owner'] #Set the 'min_ram' and 'min_disk' values explicitly. app_threshold = application.get_threshold() if app_threshold: image_kwargs['min_ram'] = app_threshold.memory_min image_kwargs['min_disk'] = app_threshold.storage_min logger.info("Including App Threshold: %s" % (app_threshold,)) image_manager = accounts.image_manager # Using Glance: #image = image_manager.get_image(image_id) #image_manager.update_image(image, **image_kwargs) # Using rtwo/libcloud: esh_driver = image_manager.admin_driver esh_machine = esh_driver.get_machine(image_id) update_machine_metadata(esh_driver, esh_machine, properties) logger.info("Machine<%s> new app data: %s" % (image_id, properties)) return
def write_app_data(provider_machine, **extras): image_kwargs= {} image_id = provider_machine.identifier #These calls are better served connecting to chromogenic Image Manager accounts = get_os_account_driver(provider_machine.provider) if not accounts: raise Exception("The account driver of Provider %s is required to" " write image metadata" % provider_machine.provider) image = accounts.image_manager.get_image(image_id) if not image: raise Exception("The account driver of Provider %s could not find" " the image: %s" % (provider_machine.provider,image_id)) return img_properties = image.properties image_manager = accounts.image_manager properties = { # Specific to the provider machine "application_version": str(provider_machine.version), # Specific to the application "application_uuid": provider_machine.application.uuid, "application_name": provider_machine.application.name, "application_owner": provider_machine.application.created_by.username, "application_tags": json.dumps( [tag.name for tag in provider_machine.application.tags.all()]), "application_description": provider_machine.application.description, } properties.update(extras) #NOTE: DON'T overlook the fact that there is metadata # that you aren't over control of. Make sure you have that too! img_properties.update(properties) #Setting 'properties' will override ALL properties of the img. USE # CAREFULLY (Like above) image_kwargs['properties'] = img_properties #Set the owner explicitly if necessary if 'owner' in extras: image_kwargs['owner'] = extras['owner'] # Using Glance: #image = image_manager.get_image(image_id) #image_manager.update_image(image, **image_kwargs) # Using rtwo/libcloud: esh_driver = image_manager.admin_driver esh_machine = esh_driver.get_machine(image_id) update_machine_metadata(esh_driver, esh_machine, properties) logger.info("Machine<%s> new app data: %s" % (image_id, properties)) return
def _os_update_owner(provider_machine, tenant_name): from core.models import Provider from service.driver import get_admin_driver provider = provider_machine.provider if provider not in Provider.get_active(type_name='openstack'): raise Exception("An active openstack provider is required to" " update image owner") esh_driver = get_admin_driver(provider) if not esh_driver: raise Exception("The account driver of Provider %s is required to" " update image metadata" % provider) esh_machine = esh_driver.get_machine(provider_machine.identifier) if not esh_machine: raise Exception("Machine with ID %s not found" % provider_machine.identifier) tenant_id = _tenant_name_to_id(provider_machine.provider, tenant_name) update_machine_metadata(esh_driver, esh_machine, {"owner": tenant_id, "application_owner": tenant_name})
def clear_app_data(provider_machine): esh_driver = get_app_driver(provider_machine) esh_machine = esh_driver.get_machine(provider_machine.identifier) mach_data = { # Specific to the provider machine "application_version": "", # Specific to the application "application_uuid": "", "application_name": "", "application_owner": "", "application_tags": "", "application_description": "", } return update_machine_metadata(esh_driver, esh_machine, mach_data)
def _update_machine(self, request, provider_uuid, identity_uuid, machine_id): #TODO: Determine who is allowed to edit machines besides #core_machine.owner user = request.user data = request.DATA esh_driver = prepare_driver(request, provider_uuid, identity_uuid) if not esh_driver: return invalid_creds(provider_uuid, identity_uuid) esh_machine = esh_driver.get_machine(machine_id) core_machine = convert_esh_machine(esh_driver, esh_machine, provider_uuid, user) if not user.is_staff\ and user is not core_machine.application.created_by: logger.warn( '%s is Non-staff/non-owner trying to update a machine' % (user.username)) return failure_response( status.HTTP_401_UNAUTHORIZED, "Only Staff and the machine Owner " + "are allowed to change machine info.") partial_update = True if request.method == 'PATCH' else False serializer = ProviderMachineSerializer(core_machine, request_user=request.user, data=data, partial=partial_update) if serializer.is_valid(): logger.info('metadata = %s' % data) update_machine_metadata(esh_driver, esh_machine, data) serializer.save() if 'created_by_identity' in request.DATA: identity = serializer.object.created_by_identity update_application_owner(core_machine.application, identity) logger.info(serializer.data) return Response(serializer.data) return failure_response(status.HTTP_400_BAD_REQUEST, serializer.errors)
def write_app_data(esh_driver, provider_machine): esh_driver = get_app_driver(provider_machine) esh_machine = esh_driver.get_machine(provider_machine.identifier) mach_data = { # Specific to the provider machine "application_version": str(provider_machine.version), # Specific to the application "application_uuid": provider_machine.application.uuid, "application_name": provider_machine.application.name, "application_owner": provider_machine.application.created_by.username, "application_tags": json.dumps( [tag.name for tag in provider_machine.application.tags.all()]), "application_description": provider_machine.application.description, } return update_machine_metadata(esh_driver, esh_machine, mach_data)
def write_app_data(provider_machine): esh_driver = get_app_driver(provider_machine) esh_machine = esh_driver.get_machine(provider_machine.identifier) mach_data = { # Specific to the provider machine "application_version": str(provider_machine.version), # Specific to the application "application_uuid": provider_machine.application.uuid, "application_name": provider_machine.application.name, "application_owner": provider_machine.application.created_by.username, "application_tags": json.dumps( [tag.name for tag in provider_machine.application.tags.all()]), "application_description": provider_machine.application.description, } logger.info("Machine<%s> new app data: %s" % (provider_machine.identifier, mach_data)) return update_machine_metadata(esh_driver, esh_machine, mach_data)