def service_list(request,format=None): """ List all services, or create a new service. """ if request.method == 'GET': pluginFile = os.getcwd() #print pluginFile pluginFile += '/config/plugins.conf' #print pluginFile config = ConfigParser.ConfigParser() config.read(pluginFile) print config.get('services','plugin') plugin = ConsulPlugin() sDict = plugin.getService() if any(sDict): services = [] for item in sDict.items(): tempService = Service(name=item[0],address=item[1]) services.append(tempService) serializer = ServiceSerializer(services, many=True) return Response(serializer.data) else: return Response(status=status.HTTP_404_NOT_FOUND) elif request.method == 'POST': serviceDict = {} serviceDict[request.data['name']]=request.data['address'] plugin = ConsulPlugin() plugin.putService(serviceDict) return Response(serviceDict, status=status.HTTP_201_CREATED)
def service_list(request, format=None): """ List all services, or create a new service. """ if request.method == 'GET': pluginFile = os.getcwd() #print pluginFile pluginFile += '/config/plugins.conf' #print pluginFile config = ConfigParser.ConfigParser() config.read(pluginFile) print config.get('services', 'plugin') plugin = ConsulPlugin() sDict = plugin.getService() if any(sDict): services = [] for item in sDict.items(): tempService = Service(name=item[0], address=item[1]) services.append(tempService) serializer = ServiceSerializer(services, many=True) return Response(serializer.data) else: return Response(status=status.HTTP_404_NOT_FOUND) elif request.method == 'POST': serviceDict = {} serviceDict[request.data['name']] = request.data['address'] plugin = ConsulPlugin() plugin.putService(serviceDict) return Response(serviceDict, status=status.HTTP_201_CREATED)
def service_detail(request,pk,format=None): """ Retrieve, update or delete a service """ try: plugin = ConsulPlugin() service = plugin.getServiceDetails(name=pk) except: return Response(status=status.HTTP_404_NOT_FOUND) if any(service): if request.method == 'GET': tempService = Service(name=pk,address=service[pk]) serializer = ServiceSerializer(tempService) return Response(serializer.data) elif request.method == 'DELETE': plugin.deleteService(pk) return Response(status=status.HTTP_204_NO_CONTENT) else:return Response(status=status.HTTP_404_NOT_FOUND)
def service_detail(request, pk, format=None): """ Retrieve, update or delete a service """ try: plugin = ConsulPlugin() service = plugin.getServiceDetails(name=pk) except: return Response(status=status.HTTP_404_NOT_FOUND) if any(service): if request.method == 'GET': tempService = Service(name=pk, address=service[pk]) serializer = ServiceSerializer(tempService) return Response(serializer.data) elif request.method == 'DELETE': plugin.deleteService(pk) return Response(status=status.HTTP_204_NO_CONTENT) else: return Response(status=status.HTTP_404_NOT_FOUND)