def post(self, request, *args, **kwargs): with self._handle_exception(request): ip = request.data['ip'] current_appliance = request.data['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, 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:
def post(self, request): try: ip = request.DATA['ip'] current_appliance = request.DATA['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['client_id'] client_secret = request.DATA['client_secret'] try: mgmt_port = int(request.DATA['mgmt_port']) except Exception, e: logger.exception(e) e_msg = ('Invalid managemetn 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:
def post(self, request): try: ip = request.DATA['ip'] current_appliance = request.DATA['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): username = request.DATA['username'] password = request.DATA['password'] try: mgmt_port = int(request.DATA['mgmt_port']) except Exception, e: logger.exception(e) e_msg = ('Invalid managemetn 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)) 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:
def setUpClass(cls): super(AppliancesTests, cls).setUpClass() # post mocks cls.patch_set_token = patch('storageadmin.views.appliances.set_token') cls.mock_set_token = cls.patch_set_token.start() cls.mock_set_token.return_value = {} cls.patch_api_call = patch('storageadmin.views.appliances.api_call') cls.mock_api_call = cls.patch_api_call.start() cls.mock_api_call.return_value = {'uuid': '01'} # Mock gethostname() to return hostname under our control, # _update_hostname() uses gethostname to update the db. cls.patch_gethostname = patch( 'storageadmin.views.appliances.gethostname') cls.mock_gethostname = cls.patch_gethostname.start() cls.mock_gethostname.return_value = 'test-host' # Mock sethostname() so we don't actually set our host's hostname. cls.patch_sethostname = patch( 'storageadmin.views.appliances.sethostname') cls.mock_sethostname = cls.patch_sethostname.start() cls.mock_sethostname.return_value = [''], [''], 0 # all values as per fixture cls.temp_appliance = \ Appliance(id=1, uuid='679E27FE-EB1A-4DE4-98EF-D9416830C4F5', ip='', current_appliance=True, mgmt_port=443)
def setUpClass(cls): super(EmailTests, cls).setUpClass() cls.patch_systemctl = patch( 'storageadmin.views.email_client.systemctl') cls.mock_systemctl = cls.patch_systemctl.start() cls.patch_send_test_email = patch( 'storageadmin.views.email_client.send_test_email') cls.mock_send_test_email = cls.patch_send_test_email.start() cls.patch_update_postfix = patch( 'storageadmin.views.email_client.update_postfix') cls.mock_update_postfix = cls.patch_update_postfix.start() cls.patch_update_generic = patch( 'storageadmin.views.email_client.update_generic') cls.mock_update_generic = cls.patch_update_generic.start() cls.patch_update_forward = patch( 'storageadmin.views.email_client.update_forward') cls.mock_update_forward = cls.patch_update_forward.start() cls.patch_update_sasl = patch( 'storageadmin.views.email_client.update_sasl') cls.mock_update_sasl = cls.patch_update_sasl.start() # all values as per fixture cls.temp_appliance = \ Appliance(id=1, uuid='679E27FE-EB1A-4DE4-98EF-D9416830C4F5', ip='', current_appliance=True, mgmt_port=443)
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)
def post(self, request): try: ip = request.DATA['ip'] current_appliance = request.DATA['current_appliance'] # authenticate if not adding current appliance if not current_appliance: username = request.DATA['username'] password = request.DATA['password'] url = 'https://%s/api/login' % ip logger.info('adding appliance %s at %s with %s %s' % (ip, url, username, password)) r = requests.post( url, headers = {'content-type': 'application/json'}, verify=False, data = json.dumps({'username': username, 'password': password})) logger.debug('in AppliancesView ') logger.debug(r.status_code) if (r.status_code != 200): raise Exception('Could not verify appliance %s' % ip) appliance = Appliance(ip = ip) appliance.save() else: appliance_uuid = ('%s:%s' % (hostid()[0][0], str(uuid.uuid4()))) appliance = Appliance(uuid=appliance_uuid, ip=ip, current_appliance=True) appliance.save() return Response(ApplianceSerializer(appliance).data) except Exception, e: handle_exception(e, request)
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)
def post(self, request): try: ip = request.DATA['ip'] current_appliance = request.DATA['current_appliance'] # authenticate if not adding current appliance if not current_appliance: username = request.DATA['username'] password = request.DATA['password'] url = 'https://%s/api/login' % ip logger.info('adding appliance %s at %s with %s %s' % (ip, url, username, password)) r = requests.post( url, headers = {'content-type': 'application/json'}, verify=False, data = json.dumps({'username': username, 'password': password})) logger.debug('in AppliancesView ') logger.debug(r.status_code) if (r.status_code != 200): raise Exception('Could not verify appliance %s' % ip) appliance = Appliance(ip = ip) appliance.save() else: appliance_uuid = ('%s:%s' % (hostid()[0][0], str(uuid.uuid4()))) appliance = Appliance(uuid=appliance_uuid, ip=ip, current_appliance=True) appliance.save() # the current appliance is created - open a support case # and send email try: notes = 'Appliance %s initialized' % appliance.uuid sc = SupportCase(notes=notes, status='created', case_type='auto') sc.save() emsg = EmailMessage(subject='support case', body=notes, from_email='*****@*****.**', to=[settings.SUPPORT['email']]) emsg.send() sc.status = 'submitted' sc.save() except Exception, e: # if an exception occurs during the above, dont exit, # the appliance should still be usable. logger.exception('exception while creating support \ case for Appliance initialization') return Response(ApplianceSerializer(appliance).data)
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_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'])
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_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:
'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)) 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)
'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) 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)