Example #1
0
    def post(self, request, *args, **kwargs):
        with self._handle_exception(request):
            ip = request.data.get("ip", "")
            current_appliance = request.data.get("current_appliance")
            # authenticate if not adding current appliance
            if Appliance.objects.filter(ip=ip).exists():
                e_msg = (
                    "The appliance with ip = {} already exists and "
                    "cannot be added again."
                ).format(ip)
                handle_exception(Exception(e_msg), request)

            if current_appliance is False:
                client_id = request.data.get("client_id", None)
                if client_id is None:
                    raise Exception("ID is required")
                client_secret = request.data.get("client_secret", None)
                if client_secret is None:
                    raise Exception("Secret is required")
                try:
                    mgmt_port = int(request.data["mgmt_port"])
                except Exception as e:
                    logger.exception(e)
                    e_msg = (
                        "Invalid management port ({}) supplied. Try again."
                    ).format(request.data["mgmt_port"])
                    handle_exception(Exception(e_msg), request)
                url = "https://%s" % ip
                if mgmt_port != 443:
                    url = "%s:%s" % (url, mgmt_port)
                ra_uuid = self._get_remote_appliance(
                    request, ip, mgmt_port, client_id, client_secret
                )
                appliance = Appliance(
                    uuid=ra_uuid,
                    ip=ip,
                    mgmt_port=mgmt_port,
                    client_id=client_id,
                    client_secret=client_secret,
                )
                appliance.save()
            else:
                appliance = Appliance(uuid=hostid(), ip=ip, current_appliance=True)
                if "hostname" in request.data:
                    appliance.hostname = request.data["hostname"]
                appliance.save()
                sethostname(appliance.hostname)
            return Response(ApplianceSerializer(appliance).data)
Example #2
0
class ApplianceDetailView(rfc.GenericView):
    serializer_class = ApplianceSerializer

    def get(self, *args, **kwargs):
        with self._handle_exception(self.request):
            data = Appliance.objects.get(id=self.kwargs.get('appid'))
            serialized_data = ApplianceSerializer(data)
            return Response(serialized_data.data)

    @transaction.atomic
    def put(self, request, appid):
        try:
            appliance = Appliance.objects.get(pk=appid)
        except Exception, e:
            logger.exception(e)
            e_msg = ('Appliance(%s) does not exist' % appid)
            handle_exception(Exception(e_msg), request)

        try:
            appliance.hostname = request.data['hostname']
            appliance.save()
            sethostname(appliance.hostname)
            if (EmailClient.objects.count() > 0):
                current_email = EmailClient.objects.all()[0]
                update_generic(current_email.sender)
            return Response()
        except Exception, e:
            logger.exception(e)
            e_msg = ('Failed updating hostname for appliance with id = %d' %
                     appid)
            handle_exception(e, request)
Example #3
0
    def post(self, request, *args, **kwargs):
        with self._handle_exception(request):
            ip = request.data.get('ip', '')
            current_appliance = request.data.get('current_appliance')
            # authenticate if not adding current appliance
            if (Appliance.objects.filter(ip=ip).exists()):
                e_msg = ('The appliance with ip = {} already exists and '
                         'cannot be added again.').format(ip)
                handle_exception(Exception(e_msg), request)

            if (current_appliance is False):
                client_id = request.data.get('client_id', None)
                if (client_id is None):
                    raise Exception('ID is required')
                client_secret = request.data.get('client_secret', None)
                if (client_secret is None):
                    raise Exception('Secret is required')
                try:
                    mgmt_port = int(request.data['mgmt_port'])
                except Exception as e:
                    logger.exception(e)
                    e_msg = ('Invalid management port ({}) supplied. Try '
                             'again.').format(request.data['mgmt_port'])
                    handle_exception(Exception(e_msg), request)
                url = ('https://%s' % ip)
                if (mgmt_port != 443):
                    url = ('%s:%s' % (url, mgmt_port))
                ra_uuid = self._get_remote_appliance(request, ip, mgmt_port,
                                                     client_id, client_secret)
                appliance = Appliance(uuid=ra_uuid,
                                      ip=ip,
                                      mgmt_port=mgmt_port,
                                      client_id=client_id,
                                      client_secret=client_secret)
                appliance.save()
            else:
                appliance = Appliance(uuid=hostid(),
                                      ip=ip,
                                      current_appliance=True)
                if ('hostname' in request.data):
                    appliance.hostname = request.data['hostname']
                appliance.save()
                sethostname(appliance.hostname)
            return Response(ApplianceSerializer(appliance).data)
Example #4
0
    def post(self, request, *args, **kwargs):
        with self._handle_exception(request):
            ip = request.data.get('ip', '')
            current_appliance = request.data.get('current_appliance')
            # authenticate if not adding current appliance
            if (Appliance.objects.filter(ip=ip).exists()):
                e_msg = ('The appliance with ip = %s already exists and '
                         'cannot be added again' % ip)
                handle_exception(Exception(e_msg), request)

            if (current_appliance is False):
                client_id = request.data.get('client_id', None)
                if (client_id is None):
                    raise Exception('ID is required')
                client_secret = request.data.get('client_secret', None)
                if (client_secret is None):
                    raise Exception('Secret is required')
                try:
                    mgmt_port = int(request.data['mgmt_port'])
                except Exception as e:
                    logger.exception(e)
                    e_msg = ('Invalid management port(%s) supplied. Try '
                             'again' % request.data['mgmt_port'])
                    handle_exception(Exception(e_msg), request)
                url = ('https://%s' % ip)
                if (mgmt_port != 443):
                    url = ('%s:%s' % (url, mgmt_port))
                ra_uuid = self._get_remote_appliance(request, ip, mgmt_port,
                                                     client_id, client_secret)
                appliance = Appliance(uuid=ra_uuid, ip=ip, mgmt_port=mgmt_port,
                                      client_id=client_id,
                                      client_secret=client_secret)
                appliance.save()
            else:
                appliance = Appliance(uuid=hostid(), ip=ip,
                                      current_appliance=True)
                if ('hostname' in request.data):
                    appliance.hostname = request.data['hostname']
                appliance.save()
                sethostname(appliance.hostname)
            return Response(ApplianceSerializer(appliance).data)
Example #5
0
    def put(self, request, appid):
        try:
            appliance = Appliance.objects.get(pk=appid)
        except Exception as e:
            logger.exception(e)
            e_msg = ('Appliance(%s) does not exist' % appid)
            handle_exception(Exception(e_msg), request)

        try:
            appliance.hostname = request.data['hostname']
            appliance.save()
            sethostname(appliance.hostname)
            if (EmailClient.objects.count() > 0):
                current_email = EmailClient.objects.all()[0]
                update_generic(current_email.sender)
            return Response()
        except Exception as e:
            logger.exception(e)
            e_msg = ('Failed updating hostname for appliance with id = %d'
                     % appid)
            handle_exception(e, request)
Example #6
0
    def put(self, request, appid):
        try:
            appliance = Appliance.objects.get(pk=appid)
        except Exception as e:
            logger.exception(e)
            e_msg = 'Appliance id ({}) does not exist.'.format(appid)
            handle_exception(Exception(e_msg), request)

        try:
            appliance.hostname = request.data['hostname']
            appliance.save()
            sethostname(appliance.hostname)
            if (EmailClient.objects.count() > 0):
                current_email = EmailClient.objects.all()[0]
                update_generic(current_email.sender)
            return Response()
        except Exception as e:
            logger.exception(e)
            e_msg = ('Failed updating hostname for appliance with '
                     'id = ({}).').format(appid)
            handle_exception(e, request)
Example #7
0
                    url = ('%s:%s' % (url, mgmt_port))
                ra_uuid = self._get_remote_appliance(request, ip, mgmt_port,
                                                     client_id, client_secret)
                appliance = Appliance(uuid=ra_uuid, ip=ip, mgmt_port=mgmt_port,
                                      client_id=client_id,
                                      client_secret=client_secret)
                appliance.save()
            else:
                appliance_uuid = ('%s-%s' % (hostid()[0][0],
                                             str(uuid.uuid4())))
                appliance = Appliance(uuid=appliance_uuid, ip=ip,
                                      current_appliance=True)
                if ('hostname' in request.data):
                    appliance.hostname = request.data['hostname']
                appliance.save()
                sethostname(appliance.hostname)
            return Response(ApplianceSerializer(appliance).data)


class ApplianceDetailView(rfc.GenericView):
    serializer_class = ApplianceSerializer

    def get(self, *args, **kwargs):
        if 'ip' in self.kwargs or 'id' in self.kwargs:
            try:
                if 'ip' in self.kwargs:
                    data = Appliance.objects.get(ip=self.kwargs['ip'])
                else:
                    data = Appliance.objects.get(id=self.kwargs['id'])
                serialized_data = ApplianceSerializer(data)
                return Response(serialized_data.data)
Example #8
0
                appliance = Appliance(uuid=ra_uuid,
                                      ip=ip,
                                      mgmt_port=mgmt_port,
                                      client_id=client_id,
                                      client_secret=client_secret)
                appliance.save()
            else:
                appliance_uuid = ('%s-%s' %
                                  (hostid()[0][0], str(uuid.uuid4())))
                appliance = Appliance(uuid=appliance_uuid,
                                      ip=ip,
                                      current_appliance=True)
                if ('hostname' in request.DATA):
                    appliance.hostname = request.DATA['hostname']
                appliance.save()
                sethostname(ip, appliance.hostname)
            return Response(ApplianceSerializer(appliance).data)
        except RockStorAPIException:
            raise
        except Exception, e:
            handle_exception(e, request)

    def delete(self, request, id):
        try:
            appliance = Appliance.objects.get(pk=id)
        except Exception, e:
            logger.exception(e)
            e_msg = ('Appliance with id = %d does not exist' % id)
            handle_exception(Exception(e_msg), request)

        try:
Example #9
0
                url = ('%s/api/login' % url)
                self._connect_to_appliance(request, url, ip, username,
                                           password)
                ra_uuid = self._get_remote_appliance(request, ip, mgmt_port,
                                                     username, password)
                appliance = Appliance(uuid=ra_uuid, ip=ip, mgmt_port=mgmt_port)
                appliance.save()
            else:
                appliance_uuid = ('%s:%s' % (hostid()[0][0],
                                             str(uuid.uuid4())))
                appliance = Appliance(uuid=appliance_uuid, ip=ip,
                                      current_appliance=True)
                if ('hostname' in request.DATA):
                    appliance.hostname = request.DATA['hostname']
                appliance.save()
                sethostname(ip, appliance.hostname)
            return Response(ApplianceSerializer(appliance).data)
        except RockStorAPIException:
            raise
        except Exception, e:
            handle_exception(e, request)

    def delete(self, request, id):
        try:
            appliance = Appliance.objects.get(pk=id)
        except Exception, e:
            logger.exception(e)
            e_msg = ('Appliance with id = %d does not exist' % id)
            handle_exception(Exception(e_msg), request)

        try:
Example #10
0
                ra_uuid = self._get_remote_appliance(request, ip, mgmt_port,
                                                     client_id, client_secret)
                appliance = Appliance(uuid=ra_uuid,
                                      ip=ip,
                                      mgmt_port=mgmt_port,
                                      client_id=client_id,
                                      client_secret=client_secret)
                appliance.save()
            else:
                appliance = Appliance(uuid=hostid(),
                                      ip=ip,
                                      current_appliance=True)
                if ('hostname' in request.data):
                    appliance.hostname = request.data['hostname']
                appliance.save()
                sethostname(appliance.hostname)
            return Response(ApplianceSerializer(appliance).data)


class ApplianceDetailView(rfc.GenericView):
    serializer_class = ApplianceSerializer

    def get(self, *args, **kwargs):
        with self._handle_exception(self.request):
            data = Appliance.objects.get(id=self.kwargs.get('appid'))
            serialized_data = ApplianceSerializer(data)
            return Response(serialized_data.data)

    @transaction.atomic
    def put(self, request, appid):
        try: