def test_details(self): data_dict = { "hostname": "127.0.0.1", "netapi_port": 80, "auth_mode": "pam", } serializer = MasterSerializer(data=data_dict) current_date_time = datetime.datetime.now() if serializer.is_valid(): serializer.object.created_by = self.user_obj serializer.object.created_at = current_date_time serializer.object.modified_by = self.user_obj serializer.object.modified_at = current_date_time # Save the POST form data serializer.save() # Check if one master is created self.assertEqual(len(Master.objects.all()), 2) self.assertTrue(Master.objects.get(hostname="127.0.0.1"))
def post(self, request): """ Use the post parameters to validate the values against serializer and create entries in the Master table Parameters: { "hostname": <hostname>, "netapi_port": <netapi_port>, "auth_mode": <auth_mode>, "master_username": <master_username>, "master_password": <master_password> } API: http://<hostname>/master/add/ Please make sure to add auth token in the headers. Once the master is added successfully, code will try to login to the master and see if the host is up. """ # Test if master is reachable validation_status, response_code, message, content = validate_host( request.DATA) # Master created successfully but login was unsuccessful if not validation_status and response_code == 401: transaction.rollback() return Response(dict(error=["invalid_master_credentials"], data={}), status=status.HTTP_401_UNAUTHORIZED) # Unable to reach master elif not validation_status and response_code == 400 and message == "duplicate_master": transaction.rollback() return Response(dict(error=["duplicate_master"], data={}), status=status.HTTP_400_BAD_REQUEST) # Unable to reach master elif not validation_status and response_code == 400: transaction.rollback() return Response(dict(error=["no_route_to_host"], data={}), status=status.HTTP_400_BAD_REQUEST) # If yes, create an entry in master and master token table elif validation_status and response_code == 200: serializer = MasterSerializer(data=request.DATA) if serializer.is_valid(): serializer.object.created_by = serializer.object.modified_by = request.user serializer.save() try: salt_user = User.objects.get( username=request.DATA['master_username']) except ObjectDoesNotExist: salt_user = User.objects.create_user( username=request.DATA['master_username'], password=request.DATA['master_password']) # Add Master Token Data token_serializer = MasterTokenSerializer( data={ 'master': serializer.object.id, 'user': salt_user.id, 'allowed_functions': content.get('perms', []), 'token': content.get('token', None) }) if not token_serializer.is_valid(): # roll back db created values in case there are any errors transaction.rollback() return Response(dict(error=token_serializer.errors, data={}), status=status.HTTP_400_BAD_REQUEST) token_serializer.save() response_dict = dict( hostname=serializer.object.hostname, netapi_port=serializer.object.netapi_port, eauth=serializer.object.auth_mode, allowed_function=token_serializer.object.allowed_functions) wheel_call_status = self._set_master_config( serializer.object, request.DATA) if not wheel_call_status: # delete the entries created in the db in case there is an error transaction.rollback() return Response(dict(error=["wheel_call_error"], data={}), status=status.HTTP_400_BAD_REQUEST) # set master config in DB set_config_status, master_config = self._set_master_details( serializer.object, request.DATA) if not set_config_status: # delete the entries created in the db in case there is an error transaction.rollback() return Response(dict(error=["master_set_config_error"], data={}), status=status.HTTP_400_BAD_REQUEST) serializer.object.config = master_config master = serializer.save() try: sync_call_resp = master.sync_all_grains() # fix for 1102 except Exception as err: logger.error( 'Error:{0} while trying to sync grains for master: {1}' .format(err, master)) transaction.rollback() return Response(dict(error=['sync_grains_error'], data={}), status=500) # if all is success, commit the transaction transaction.commit() return Response(dict(error=[], data=response_dict), status=status.HTTP_200_OK) else: # delete the entries created in the db in case there is an error transaction.rollback() # Error processing error_list = [ e for error in serializer.errors.values() for e in error ] return Response(dict(error=error_list, data={}), status=status.HTTP_400_BAD_REQUEST) else: return Response(dict(error=["error"], data={}), status=status.HTTP_400_BAD_REQUEST)