change hpc_config and legacyapi into new-style api views

Change-Id: I24070e08102c898dae676bf6b12c41e8d4eb3134
diff --git a/xos/api/service/hpc/hpc_config.py b/xos/api/service/hpc/hpc_config.py
index 7cb60c0..0e708fd 100644
--- a/xos/api/service/hpc/hpc_config.py
+++ b/xos/api/service/hpc/hpc_config.py
@@ -1,5 +1,6 @@
 from django.http import HttpResponse, HttpResponseServerError
 from core.models import *
+from rest_framework.views import APIView
 from services.hpc.models import *
 #from services.requestrouter.models import *
 import xos.settings
@@ -14,118 +15,122 @@
        # this field used to be improperly named, and makemigrations won't fix it
        return service.service.all()
 
-def HpcConfig(request):
-    hpcSlice=None
-    cmiSlice=None
-    redirSlice=None
-    demuxSlice=None
+class HpcConfig(APIView):
+    method_kind = "list"
+    method_name = "hpcconfig"
 
-    node_slicename = request.GET.get("slicename", None)
-    if not node_slicename:
-        return HttpResponseServerError("Error: no slicename passed in request")
+    def get(self, request, format=None):
+        hpcSlice=None
+        cmiSlice=None
+        redirSlice=None
+        demuxSlice=None
 
-    # search for an HPC Service that owns the slicename that was passed
-    # to us.
-    hpc=None
-    for candidate in HpcService.objects.all():
-        if candidate.cmi_hostname == node_slicename:
-            # A hack for standalone CMIs that aren't managed by XOS. Set
-            # /etc/slicename to cmi_hostname that's configured in the
-            # HPCService object.
-            hpc = candidate
+        node_slicename = request.GET.get("slicename", None)
+        if not node_slicename:
+            return HttpResponseServerError("Error: no slicename passed in request")
 
-        for slice in get_service_slices(candidate):
-            if slice.name == node_slicename:
+        # search for an HPC Service that owns the slicename that was passed
+        # to us.
+        hpc=None
+        for candidate in HpcService.objects.all():
+            if candidate.cmi_hostname == node_slicename:
+                # A hack for standalone CMIs that aren't managed by XOS. Set
+                # /etc/slicename to cmi_hostname that's configured in the
+                # HPCService object.
                 hpc = candidate
 
-    if (not hpc):
-        return HttpResponseServerError("Error: no HPC service")
+            for slice in get_service_slices(candidate):
+                if slice.name == node_slicename:
+                    hpc = candidate
 
-    for slice in get_service_slices(hpc):
-        if "cmi" in slice.name:
-            cmiSlice = slice
-        elif ("hpc" in slice.name) or ("vcoblitz" in slice.name):
-            hpcSlice = slice
-        elif "redir" in slice.name:
-            redirSlice = slice
-        elif "demux" in slice.name:
-            demuxSlice = slice
+        if (not hpc):
+            return HttpResponseServerError("Error: no HPC service")
 
-    if (hpc.cmi_hostname):
-        cmi_hostname = hpc.cmi_hostname
-    else:
-        if not cmiSlice:
-            return HttpResponseServerError("Error: no CMI slice")
+        for slice in get_service_slices(hpc):
+            if "cmi" in slice.name:
+                cmiSlice = slice
+            elif ("hpc" in slice.name) or ("vcoblitz" in slice.name):
+                hpcSlice = slice
+            elif "redir" in slice.name:
+                redirSlice = slice
+            elif "demux" in slice.name:
+                demuxSlice = slice
 
-        if len(cmiSlice.instances.all())==0:
-            return HttpResponseServerError("Error: CMI slice has no instances")
+        if (hpc.cmi_hostname):
+            cmi_hostname = hpc.cmi_hostname
+        else:
+            if not cmiSlice:
+                return HttpResponseServerError("Error: no CMI slice")
 
-        # for now, assuming using NAT
-        cmi_hostname = cmiSlice.instances.all()[0].node.name
+            if len(cmiSlice.instances.all())==0:
+                return HttpResponseServerError("Error: CMI slice has no instances")
 
-    if not hpcSlice:
-        return HttpResponseServerError("Error: no HPC slice")
+            # for now, assuming using NAT
+            cmi_hostname = cmiSlice.instances.all()[0].node.name
 
-#    if (redirSlice==None) or (demuxSlice==None):
-#        # The HPC Service didn't have a dnsredir or a dnsdemux, so try looking
-#        # in the RequestRouterService for one.
-#
-#        rr = RequestRouterService.objects.all()
-#        if not (rr):
-#            return HttpResponseServerError("Error: no RR service")
-#
-#        rr = rr[0]
-#        try:
-#           slices = rr.slices.all()
-#        except:
-#           # this field used to be improperly named, and makemigrations won't fix it
-#           slices = rr.service.all()
-#        for slice in slices:
-#            if "redir" in slice.name:
-#                redirSlice = slice
-#            elif "demux" in slice.name:
-#                demuxSlice = slice
+        if not hpcSlice:
+            return HttpResponseServerError("Error: no HPC slice")
 
-    if not redirSlice:
-        return HttpResponseServerError("Error: no dnsredir slice")
+    #    if (redirSlice==None) or (demuxSlice==None):
+    #        # The HPC Service didn't have a dnsredir or a dnsdemux, so try looking
+    #        # in the RequestRouterService for one.
+    #
+    #        rr = RequestRouterService.objects.all()
+    #        if not (rr):
+    #            return HttpResponseServerError("Error: no RR service")
+    #
+    #        rr = rr[0]
+    #        try:
+    #           slices = rr.slices.all()
+    #        except:
+    #           # this field used to be improperly named, and makemigrations won't fix it
+    #           slices = rr.service.all()
+    #        for slice in slices:
+    #            if "redir" in slice.name:
+    #                redirSlice = slice
+    #            elif "demux" in slice.name:
+    #                demuxSlice = slice
 
-    if not demuxSlice:
-        return HttpResponseServerError("Error: no dnsdemux slice")
+        if not redirSlice:
+            return HttpResponseServerError("Error: no dnsredir slice")
 
-    d = {}
-    d["hpc_slicename"] = hpcSlice.name
-    d["redir_slicename"] = redirSlice.name
-    d["demux_slicename"] = demuxSlice.name
-    d["cmi_hostname"] = cmi_hostname
-    d["xos_hostname"] = xos.settings.RESTAPI_HOSTNAME
-    d["xos_port"] = str(xos.settings.RESTAPI_PORT)
+        if not demuxSlice:
+            return HttpResponseServerError("Error: no dnsdemux slice")
 
-    if hpc.hpc_port80:
-        d["hpc_port80"] = "True"
-    else:
-        d["hpc_port80"] = "False"
+        d = {}
+        d["hpc_slicename"] = hpcSlice.name
+        d["redir_slicename"] = redirSlice.name
+        d["demux_slicename"] = demuxSlice.name
+        d["cmi_hostname"] = cmi_hostname
+        d["xos_hostname"] = xos.settings.RESTAPI_HOSTNAME
+        d["xos_port"] = str(xos.settings.RESTAPI_PORT)
 
-    return HttpResponse("""# auto-generated by HpcConfig
-ENABLE_PLC=False
-ENABLE_PS=True
-BASE_HRN="princeton"
-RELEVANT_SERVICE_NAMES=['vcoblitz', 'coredirect', 'codnsdemux', "syndicate_comon_server"]
-COBLITZ_SLICE_NAME=BASE_HRN+"_vcoblitz"
-COBLITZ_SLICE_ID=70
-COBLITZ_PS_SLICE_NAME="{hpc_slicename}"
-DNSREDIR_SLICE_NAME=BASE_HRN+"_coredirect"
-DNSREDIR_SLICE_ID=71
-DNSREDIR_PS_SLICE_NAME="{redir_slicename}"
-DNSDEMUX_SLICE_NAME=BASE_HRN+"_codnsdemux"
-DNSDEMUX_SLICE_ID=69
-DNSDEMUX_PS_SLICE_NAME="{demux_slicename}"
-CMI_URL="http://{cmi_hostname}/"
-CMI_HTTP_PORT="8004"
-CMI_HTTPS_PORT="8003"
-PUPPET_MASTER_HOSTNAME="{cmi_hostname}"
-PUPPET_MASTER_PORT="8140"
-PS_HOSTNAME="{xos_hostname}"
-PS_PORT="{xos_port}"
-COBLITZ_PORT_80={hpc_port80}
-""".format(**d))
+        if hpc.hpc_port80:
+            d["hpc_port80"] = "True"
+        else:
+            d["hpc_port80"] = "False"
+
+        return HttpResponse("""# auto-generated by HpcConfig
+    ENABLE_PLC=False
+    ENABLE_PS=True
+    BASE_HRN="princeton"
+    RELEVANT_SERVICE_NAMES=['vcoblitz', 'coredirect', 'codnsdemux', "syndicate_comon_server"]
+    COBLITZ_SLICE_NAME=BASE_HRN+"_vcoblitz"
+    COBLITZ_SLICE_ID=70
+    COBLITZ_PS_SLICE_NAME="{hpc_slicename}"
+    DNSREDIR_SLICE_NAME=BASE_HRN+"_coredirect"
+    DNSREDIR_SLICE_ID=71
+    DNSREDIR_PS_SLICE_NAME="{redir_slicename}"
+    DNSDEMUX_SLICE_NAME=BASE_HRN+"_codnsdemux"
+    DNSDEMUX_SLICE_ID=69
+    DNSDEMUX_PS_SLICE_NAME="{demux_slicename}"
+    CMI_URL="http://{cmi_hostname}/"
+    CMI_HTTP_PORT="8004"
+    CMI_HTTPS_PORT="8003"
+    PUPPET_MASTER_HOSTNAME="{cmi_hostname}"
+    PUPPET_MASTER_PORT="8140"
+    PS_HOSTNAME="{xos_hostname}"
+    PS_PORT="{xos_port}"
+    COBLITZ_PORT_80={hpc_port80}
+    """.format(**d))
 
diff --git a/xos/api/service/hpc/legacyapi.py b/xos/api/service/hpc/legacyapi.py
index b5592c0..c3efa8f 100644
--- a/xos/api/service/hpc/legacyapi.py
+++ b/xos/api/service/hpc/legacyapi.py
@@ -10,6 +10,7 @@
 
 from django.http import HttpResponse
 from django.views.decorators.csrf import csrf_exempt
+from rest_framework.views import APIView
 
 def ps_id_to_pl_id(x):
     # Since we don't want the XOS object IDs to conflict with existing
@@ -327,9 +328,12 @@
          "GetNodes2": HandleGetNodes2,
          "GetSlices2": HandleGetSlices2}
 
-@csrf_exempt
-def LegacyXMLRPC(request):
-    if request.method == "POST":
+#@csrf_exempt
+class LegacyApi(APIView):
+    method_kind = "list"
+    method_name = "legacyapi"
+
+    def post(self, request, format=None):
         try:
             (args, method) = xmlrpclib.loads(request.body)
             result = None
@@ -339,8 +343,6 @@
         except:
             traceback.print_exc()
             return HttpResponseServerError()
-    else:
-        return HttpResponse("Not Implemented")
 
 if __name__ == '__main__':
     slices = GetSlices(slice_remap = DEFAULT_REMAP)