Beispiel #1
0
    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)
Beispiel #2
0
    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
        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)
        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)
Beispiel #3
0
 def get(self, request, provider_uuid, identity_uuid):
     """
     Using provider and identity, getlist of machines
     TODO: Cache this request
     """
     try:
         request_user = request.user
         logger.debug("filtered_machine_list")
         filtered_machine_list = provider_filtered_machines(
             request, provider_uuid, identity_uuid, request_user
         )
     except ProviderNotActive as pna:
         return inactive_provider(pna)
     except LibcloudInvalidCredsError:
         return invalid_creds(provider_uuid, identity_uuid)
     except LibcloudBadResponseError:
         return malformed_response(provider_uuid, identity_uuid)
     except (socket_error, ConnectionFailure):
         return connection_failure(provider_uuid, identity_uuid)
     except ObjectDoesNotExist:
         return invalid_provider_identity(provider_uuid, identity_uuid)
     except Exception as e:
         logger.exception("Unexpected exception for user:%s" % request_user)
         return failure_response(status.HTTP_409_CONFLICT, e.message)
     serialized_data = ProviderMachineSerializer(
         filtered_machine_list, request_user=request.user, many=True
     ).data
     response = Response(serialized_data)
     return response
Beispiel #4
0
    def get(self, request, provider_uuid, identity_uuid, machine_id):
        """
        Details view for specific machine
        (Lookup using the given provider/identity)
        """
        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)
        # TODO: Need to determine that identity_uuid is ALLOWED to
        # see machine_id. if not covered by calling as the users driver..
        esh_machine = esh_driver.get_machine(machine_id)
        core_machine = convert_esh_machine(
            esh_driver, esh_machine, provider_uuid, user
        )
        serialized_data = ProviderMachineSerializer(
            core_machine, request_user=request.user
        ).data
        response = Response(serialized_data)
        return response
Beispiel #5
0
 def get(self, request, provider_uuid, identity_uuid):
     """
     Using provider and identity, getlist of machines
     TODO: Cache this request
     """
     try:
         request_user = request.user
         logger.debug("filtered_machine_list")
         filtered_machine_list = provider_filtered_machines(
             request, provider_uuid, identity_uuid, request_user)
         #logger.debug(filtered_machine_list)
     except InvalidCredsError:
         return invalid_creds(provider_uuid, identity_uuid)
     except MalformedResponseError:
         return malformed_response(provider_uuid, identity_uuid)
     except (socket_error, ConnectionFailure):
         return connection_failure(provider_uuid, identity_uuid)
     except Exception as e:
         logger.exception("Unexpected exception for user:%s" % request_user)
         return failure_response(status.HTTP_500_INTERNAL_SERVER_ERROR,
                                 e.message)
     #logger.debug(filtered_machine_list)
     serialized_data = ProviderMachineSerializer(filtered_machine_list,
                                                 request_user=request.user,
                                                 many=True).data
     response = Response(serialized_data)
     return response
Beispiel #6
0
 def get(self, request, provider_uuid, identity_uuid, machine_id):
     """
     Details view for specific machine
     (Lookup using the given provider/identity)
     """
     user = request.user
     esh_driver = prepare_driver(request, provider_uuid, identity_uuid)
     if not esh_driver:
         return invalid_creds(provider_uuid, identity_uuid)
     # TODO: Need to determine that identity_uuid is ALLOWED to
     # see machine_id. if not covered by calling as the users driver..
     esh_machine = esh_driver.get_machine(machine_id)
     core_machine = convert_esh_machine(esh_driver, esh_machine,
                                        provider_uuid, user)
     serialized_data = ProviderMachineSerializer(
         core_machine, request_user=request.user).data
     response = Response(serialized_data)
     return response
Beispiel #7
0
 def get(self, request, machine_name):
     pms = ProviderMachine.objects.filter(application__name=machine_name)
     serialized_data = ProviderMachineSerializer(pms,
                                                 request_user=None,
                                                 many=True).data
     return Response(serialized_data)