예제 #1
0
파일: views.py 프로젝트: senglobe/ignite
    def post(self, request, format=None):

        serializer = FabricSerializer(data=request.data)
        topology = Topology.objects.get(id=request.data['topology_id'])
        topology_json = json.loads(topology.topology_json)
        me = RequestValidator(request.META)
        if serializer.is_valid():
            if request.data['instance'] < 1:
                logger.error("Fabric Instances cannot be less than 1")
            else:
                fabric_obj = Fabric()
                fabric_obj.name = request.data['name']
                fabric_obj.user_id = me.user_is_exist().user_id
                fabric_obj.topology = topology
                topology.used += 1
                topology.save()
                fabric_obj.instance = request.data['instance']
                fabric_obj.validate = request.data['validate']
                fabric_obj.locked = request.data['locked']
                fabric_obj.config_json = json.dumps(request.data['config_json'])
                fabric_obj.submit = request.data['submit']
                fabric_obj.save()
                if (generate_fabric_rules(request.data['name'],\
                request.data['instance'], fabric_obj, request.data['config_json'],\
                topology_json)):
                    serializer = FabricGetSerializer(fabric_obj)
                    logger.info("Successfully created Fabric id: " + str(id))
                    return Response(serializer.data, status=status.HTTP_201_CREATED)
                else:
                    Fabric.objects.filter(id = fabric_obj.id).delete()
                    logger.error("Failed to create Fabric: " + fabric_obj.name)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
예제 #2
0
파일: views.py 프로젝트: salran40/POAP
    def post(self, request, format=None):

        success = True
        resp = {}
        resp['Error'] = ' '
        serializer = FabricSerializer(data=request.data)
        topology = Topology.objects.get(id=request.data['topology_id'])
        topology_json = json.loads(topology.topology_json)
        me = RequestValidator(request.META)
        if serializer.is_valid():
            if request.data['instance'] < 1:
                logger.error("Fabric Instances cannot be less than 1")
            else:
                fabric_obj = Fabric()
                fabric_obj.name = request.data['name']
                fabric_obj.user_id = me.user_is_exist().user_id
                fabric_obj.topology = topology
                topology.used += 1
                topology.save()
                fabric_obj.instance = request.data['instance']
                fabric_obj.validate = request.data['validate']
                fabric_obj.locked = request.data['locked']
                fabric_obj.config_json = json.dumps(
                    request.data['config_json'])
                for config in request.data['config_json']:
                    config_obj = Configuration.objects.get(
                        id=config['configuration_id'])
                    config_obj.used += 1
                    config_obj.save()
                fabric_obj.submit = request.data['submit']
                try:
                    fabric_obj.system_id = json.dumps(
                        request.data['system_id'])
                except:
                    fabric_obj.system_id = []
                try:
                    fabric_obj.save()
                except:
                    logger.error("Failed to create Fabric: " + fabric_obj.name)
                    resp['Error'] = 'Failed to create Fabric'
                    return JsonResponse(resp,
                                        status=status.HTTP_400_BAD_REQUEST)

                # filling discovery rule with system_id
                try:
                    sys_id_obj = json.loads(fabric_obj.system_id)
                    config_obj = json.loads(fabric_obj.config_json)
                    regex = fabric_obj.name + "(_)([1-9][0-9]*)(_)([a-zA-Z]*-[1-9])"
                    for switch_systemId_info in sys_id_obj:
                        if not success:
                            break
                        switch_name = (re.search(
                            regex, switch_systemId_info['name'])).group(4)
                        replica_num = int(
                            (re.search(regex,
                                       switch_systemId_info['name'])).group(2))
                        system_id = switch_systemId_info['system_id']
                        for config in config_obj:
                            if switch_name == config['name']:
                                discoveryrule_name = 'serial_' + switch_systemId_info[
                                    'system_id']
                                old_dis_rule = DiscoveryRule.objects.filter(
                                    name=discoveryrule_name)
                                if old_dis_rule:
                                    obj = DiscoveryRule.objects.get(
                                        name=discoveryrule_name)
                                    logger.error("DIscovery Rule existed with system id: "+switch_systemId_info\
                                                 ['system_id']+" wiht switch_name: "+obj.switch_name)
                                    if obj.fabric_id == fabric_obj.id:
                                        resp['Error'] = 'Serial id: '+switch_systemId_info['system_id']+\
                                                        ' is repeated in this Fabric'
                                    else:
                                        resp['Error'] = "serial id: "+switch_systemId_info['system_id']+\
                                                        " is existed with switch: "+obj.switch_name
                                    logger.error(resp['Error'])
                                    success = False
                                    break

                                dis_rule_obj = DiscoveryRule()
                                dis_rule_obj.priority = 100
                                dis_rule_obj.name = discoveryrule_name
                                dis_rule_obj.config_id = config[
                                    'configuration_id']
                                dis_rule_obj.match = 'serial_id'
                                dis_rule_obj.subrules = [system_id]
                                dis_rule_obj.fabric_id = fabric_obj.id
                                dis_rule_obj.replica_num = replica_num
                                dis_rule_obj.switch_name = switch_systemId_info[
                                    'name']
                                dis_rule_obj.save()
                except:
                    success = False
                    logger.error('Failed to update discoveryRule DB with system_id rule for fabric_id: '+\
                                 str(fabric_obj.id))
                    resp['Error'] = 'Failed to update DiscoveryRule DB'

                if success:
                    if (generate_fabric_rules(request.data['name'],\
                    request.data['instance'], fabric_obj, request.data['config_json'],\
                    topology_json)):
                        serializer = FabricGetSerializer(fabric_obj)
                        logger.info("Successfully created Fabric id: " +
                                    str(fabric_obj.id))
                        return Response(serializer.data,
                                        status=status.HTTP_201_CREATED)
                    else:
                        success = False
                        logger.error("Failed to update FabricRuleDb: " +
                                     fabric_obj.name)
                        resp['Error'] = 'Failed to update FabricRule DB'

                if not success:
                    try:
                        DiscoveryRule.objects.filter(
                            fabric_id=fabric_obj.id).delete()
                    except:
                        pass
                    Fabric.objects.filter(id=fabric_obj.id).delete()
                    logger.error("Failed to create Fabric: " + fabric_obj.name)
                    return JsonResponse(resp,
                                        status=status.HTTP_400_BAD_REQUEST)

        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
예제 #3
0
파일: views.py 프로젝트: javaos74/ignite
 def post(self, request, format=None):
     
     success = True
     resp = {}
     resp['Error'] = ' '
     serializer = FabricSerializer(data=request.data)
     topology = Topology.objects.get(id=request.data['topology_id'])
     topology_json = json.loads(topology.topology_json)
     me = RequestValidator(request.META)
     if serializer.is_valid():
         if request.data['instance'] < 1:
             logger.error("Fabric Instances cannot be less than 1")
         else:
             fabric_obj = Fabric()
             find_dup_data = {'system_id':['system_id','name'], 'config_json':['name']}
             for key,val in find_dup_data.iteritems():
                 for value in val:
                     err_msg, isError = findDuplicate(request.data[key], value)
                     if isError:
                         resp['Error'] = err_msg
                         return Response(resp, status=status.HTTP_400_BAD_REQUEST)
             err = uniqueSystenmId(request.data['system_id'],fabric_obj.id)
             if err != "":
                 resp['Error'] = err
                 return Response(resp, status=status.HTTP_400_BAD_REQUEST)
             fabric_obj.name = request.data['name']
             fabric_obj.user_id = me.user_is_exist().user_id
             fabric_obj.topology = topology
             topology.used += 1
             topology.save()
             fabric_obj.instance = request.data['instance']
             fabric_obj.validate = request.data['validate']
             fabric_obj.locked = request.data['locked']
             fabric_obj.config_json = json.dumps(request.data['config_json'])
             for config in request.data['config_json']:
                 config_obj = Configuration.objects.get(id = config['configuration_id'])
                 config_obj.used += 1
                 config_obj.save()
             fabric_obj.submit = request.data['submit']
             try:
                 fabric_obj.system_id = json.dumps(request.data['system_id'])
             except:
                 fabric_obj.system_id = []
             """
             try:
                 fabric_obj.profiles = json.dumps(request.data['profiles'])
             except:
                 fabric_obj.profiles = json.dumps({})
             """    
             try:   # fill image details
                 fabric_obj.image_details = json.dumps(request.data['image_details'])
             except:
                 fabric_obj.image_details = json.dumps({})
             try:  # save object
                 fabric_obj.save()
             except:
                 logger.error("Failed to create Fabric: " + fabric_obj.name)
                 resp['Error'] = 'Failed to create Fabric' 
                 return JsonResponse(resp,status=status.HTTP_400_BAD_REQUEST)
                             
             # filling discovery rule with system_id
             success, resp, dis_bulk_obj = add_dis_rule(request.data,success,resp,fabric_obj.id)
             
             if success:   
                 if (generate_fabric_rules(request.data['name'],\
                 request.data['instance'], fabric_obj, request.data['config_json'],\
                 topology_json)):
                     serializer = FabricGetSerializer(fabric_obj)
                     logger.info("Successfully created Fabric id: " + str(fabric_obj.id))
                     try:
                         for obj in dis_bulk_obj:
                             obj.save()
                     except:
                         logger.error('failed to save dis_rule_obj')
                         resp['Error']='Failed to save DiscoveryRules'
                         return JsonResponse(resp,status=status.HTTP_400_BAD_REQUEST)
                     return Response(serializer.data, status=status.HTTP_201_CREATED)
                 else:
                     success = False
                     logger.error("Failed to update FabricRuleDb: " + fabric_obj.name)
                     resp['Error'] = 'Failed to update FabricRule DB'
                     
                 
             if not success:
                 try:
                     DiscoveryRule.objects.filter(fabric_id = fabric_obj.id).delete()
                 except:
                     pass
                 Fabric.objects.filter(id = fabric_obj.id).delete()
                 logger.error("Failed to create Fabric: " + fabric_obj.name)
                 return JsonResponse(resp,status=status.HTTP_400_BAD_REQUEST)
                     
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
예제 #4
0
    def post(self, request, format=None):

        success = True
        resp = {}
        resp['Error'] = ' '
        serializer = FabricSerializer(data=request.data)
        topology = Topology.objects.get(id=request.data['topology_id'])
        topology_json = json.loads(topology.topology_json)
        me = RequestValidator(request.META)
        if serializer.is_valid():
            if request.data['instance'] < 1:
                logger.error("Fabric Instances cannot be less than 1")
            else:
                fabric_obj = Fabric()
                find_dup_data = {
                    'system_id': ['system_id', 'name'],
                    'config_json': ['name']
                }
                for key, val in find_dup_data.iteritems():
                    for value in val:
                        err_msg, isError = findDuplicate(
                            request.data[key], value)
                        if isError:
                            resp['Error'] = err_msg
                            return Response(resp,
                                            status=status.HTTP_400_BAD_REQUEST)
                err = uniqueSystenmId(request.data['system_id'], fabric_obj.id)
                if err != "":
                    resp['Error'] = err
                    return Response(resp, status=status.HTTP_400_BAD_REQUEST)
                fabric_obj.name = request.data['name']
                fabric_obj.user_id = me.user_is_exist().user_id
                fabric_obj.topology = topology
                topology.used += 1
                topology.save()
                fabric_obj.instance = request.data['instance']
                fabric_obj.validate = request.data['validate']
                fabric_obj.locked = request.data['locked']
                fabric_obj.config_json = json.dumps(
                    request.data['config_json'])
                for config in request.data['config_json']:
                    config_obj = Configuration.objects.get(
                        id=config['configuration_id'])
                    config_obj.used += 1
                    config_obj.save()
                fabric_obj.submit = request.data['submit']
                try:
                    fabric_obj.system_id = json.dumps(
                        request.data['system_id'])
                except:
                    fabric_obj.system_id = []
                """
                try:
                    fabric_obj.profiles = json.dumps(request.data['profiles'])
                except:
                    fabric_obj.profiles = json.dumps({})
                """
                try:  # fill image details
                    fabric_obj.image_details = json.dumps(
                        request.data['image_details'])
                except:
                    fabric_obj.image_details = json.dumps({})
                try:  # save object
                    fabric_obj.save()
                except:
                    logger.error("Failed to create Fabric: " + fabric_obj.name)
                    resp['Error'] = 'Failed to create Fabric'
                    return JsonResponse(resp,
                                        status=status.HTTP_400_BAD_REQUEST)

                # filling discovery rule with system_id
                success, resp, dis_bulk_obj = add_dis_rule(
                    request.data, success, resp, fabric_obj.id)

                if success:
                    if (generate_fabric_rules(request.data['name'],\
                    request.data['instance'], fabric_obj, request.data['config_json'],\
                    topology_json)):
                        serializer = FabricGetSerializer(fabric_obj)
                        logger.info("Successfully created Fabric id: " +
                                    str(fabric_obj.id))
                        try:
                            for obj in dis_bulk_obj:
                                obj.save()
                        except:
                            logger.error('failed to save dis_rule_obj')
                            resp['Error'] = 'Failed to save DiscoveryRules'
                            return JsonResponse(
                                resp, status=status.HTTP_400_BAD_REQUEST)
                        return Response(serializer.data,
                                        status=status.HTTP_201_CREATED)
                    else:
                        success = False
                        logger.error("Failed to update FabricRuleDb: " +
                                     fabric_obj.name)
                        resp['Error'] = 'Failed to update FabricRule DB'

                if not success:
                    try:
                        DiscoveryRule.objects.filter(
                            fabric_id=fabric_obj.id).delete()
                    except:
                        pass
                    Fabric.objects.filter(id=fabric_obj.id).delete()
                    logger.error("Failed to create Fabric: " + fabric_obj.name)
                    return JsonResponse(resp,
                                        status=status.HTTP_400_BAD_REQUEST)

        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)