summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorying.yunlong <ying.yunlong@zte.com.cn>2017-02-24 14:23:18 +0800
committerying.yunlong <ying.yunlong@zte.com.cn>2017-02-24 14:23:18 +0800
commitdc69a33e70194bbbdefcf93762e7f09006e8acd2 (patch)
tree55093fff5b96135515d68d798e3079c155f43af1
parent5746a092fac918b20643f3174b401ba54838fa7b (diff)
Refactor code and testcase of gvnfmres
Change-Id: I9157c036290411675fcb5f5601f0108f6e880e8e Issue-Id: GVNFM-12 Signed-off-by: ying.yunlong <ying.yunlong@zte.com.cn>
-rw-r--r--res/res/resources/tests.py59
-rw-r--r--res/res/resources/urls.py14
-rw-r--r--res/res/resources/views.py17
3 files changed, 44 insertions, 46 deletions
diff --git a/res/res/resources/tests.py b/res/res/resources/tests.py
index 689ceff..8ce9550 100644
--- a/res/res/resources/tests.py
+++ b/res/res/resources/tests.py
@@ -124,7 +124,6 @@ class ResourceTest(TestCase):
"relatedtype": 0
}]
}
-
self.volumes_data = {
"resp_data": [{
"storageid": "st0",
@@ -144,67 +143,77 @@ class ResourceTest(TestCase):
def test_get_vnf(self):
vnf_inst_id = "1"
NfInstModel(nfinstid=vnf_inst_id, nf_name='VNF1').save()
- StorageInstModel(storageid='s02', vimid='vim01', resouceid='resource01', insttype=1,\
+ StorageInstModel(storageid='s02', vimid='vim01', resouceid='resource01', insttype=1,
instid=vnf_inst_id, storageDesc='desc01').save()
response = self.client.get("/openoapi/vnfres/v1/vnfs/%s" % vnf_inst_id)
+ self.failUnlessEqual(status.HTTP_200_OK, response.status_code)
self.assertEqual(self.test_data, response.data)
- # self.failUnlessEqual(status.HTTP_200_OK, response.status_code)
def test_get_vnfs(self):
- for i in range(1):
+ for i in range(1, 5):
NfInstModel(nfinstid='%s' % i, nf_name='VNF%s' % i).save()
- StorageInstModel(storageid='s0%s' % i, vimid='vim0%s' % i, resouceid='resource0%s' % i, insttype=1, instid='%s' % i, storageDesc='desc%s' % i).save()
+ StorageInstModel(storageid='s0%s' % i, vimid='vim0%s' % i, resouceid='resource0%s' % i,
+ insttype=1, instid='%s' % i, storageDesc='desc%s' % i).save()
response = self.client.get("/openoapi/vnfres/v1/vnfs")
- # self.assertEqual(self.test_data, response.data)
self.failUnlessEqual(status.HTTP_200_OK, response.status_code)
+ # self.assertEqual(self.test_data, response.data)
def test_get_vms(self):
NfInstModel(nfinstid='%s' % '01', nf_name='VFS%s' % '01').save()
- nfInst = NfInstModel.objects.get(pk='01')
+ nfinst = NfInstModel.objects.get(pk='01')
for i in range(1):
- VmInstModel(vmid='vm%s' % i, vimid='vim%s' % i, resouceid='res%s' % i, instid='%s' % nfInst.nfinstid, insttype=0, vmname='v1').save()
- response = self.client.get("/openoapi/vnfres/v1/%s/vms" % nfInst.nfinstid)
+ VmInstModel(vmid='vm%s' % i, vimid='vim%s' % i, resouceid='res%s' % i,
+ instid='%s' % nfinst.nfinstid, insttype=0, vmname='v1').save()
+ response = self.client.get("/openoapi/vnfres/v1/%s/vms" % nfinst.nfinstid)
+ self.failUnlessEqual(status.HTTP_200_OK, response.status_code)
self.assertEqual(self.vms_data, response.data)
- # self.failUnlessEqual(status.HTTP_200_OK, response.status_code)
def test_get_flavors(self):
NfInstModel(nfinstid='%s' % '01', nf_name='VFS%s' % '01').save()
- nfInst = NfInstModel.objects.get(pk='01')
+ nfinst = NfInstModel.objects.get(pk='01')
for i in range(1):
- FlavourInstModel(flavourid='fla%s' % i, name='fname%s' % i, vcpu='cpu%s' % i, instid='%s' % nfInst.nfinstid, memory='mem%s' % i, extraspecs='ext%s' % i, vmid='vm%s' % i).save()
- response = self.client.get("/openoapi/vnfres/v1/%s/flavors" % nfInst.nfinstid)
+ FlavourInstModel(flavourid='fla%s' % i, name='fname%s' % i, vcpu='cpu%s' % i,
+ instid='%s' % nfinst.nfinstid, memory='mem%s' % i,
+ extraspecs='ext%s' % i, vmid='vm%s' % i).save()
+ response = self.client.get("/openoapi/vnfres/v1/%s/flavors" % nfinst.nfinstid)
+ self.failUnlessEqual(status.HTTP_200_OK, response.status_code)
self.assertEqual(self.flavors_data, response.data)
- # self.failUnlessEqual(status.HTTP_200_OK, response.status_code)
def test_get_networks(self):
for i in range(1):
- NetworkInstModel(networkid='net%s' % i, name='net_name%s' % i, vimid='vim%s' % i, instid='%s' % self.nf_inst_id,
- resouceid='res%s' % i, insttype='%d' % i).save()
+ NetworkInstModel(networkid='net%s' % i, name='net_name%s' % i, vimid='vim%s' % i,
+ instid='%s' % self.nf_inst_id, resouceid='res%s' % i, insttype='%d' % i).save()
response = self.client.get("/openoapi/vnfres/v1/%s/networks" % self.nf_inst_id)
+ self.failUnlessEqual(status.HTTP_200_OK, response.status_code)
self.assertEqual(self.networks_data, response.data)
- # self.failUnlessEqual(status.HTTP_200_OK, response.status_code)
def test_get_subnets(self):
for i in range(1):
- SubNetworkInstModel(subnetworkid="sub%s" % i, vimid='vim%s' % i, resouceid='res%s' % i, networkid='net%s' % i, insttype='%d' % i, instid='%s' % self.nf_inst_id, name='sub_name%s' % i, cidr="cidr%s" % i).save()
+ SubNetworkInstModel(subnetworkid="sub%s" % i, vimid='vim%s' % i, resouceid='res%s' % i,
+ networkid='net%s' % i, insttype='%d' % i, instid='%s' % self.nf_inst_id,
+ name='sub_name%s' % i, cidr="cidr%s" % i).save()
response = self.client.get("/openoapi/vnfres/v1/%s/subnets" % self.nf_inst_id)
+ self.failUnlessEqual(status.HTTP_200_OK, response.status_code)
self.assertEqual(self.subnets_data, response.data)
- # self.failUnlessEqual(status.HTTP_200_OK, response.status_code)
def test_get_cps(self):
for i in range(1):
- CPInstModel(cpinstanceid="cp%s" % i, cpdid='cpd%s' % i, cpinstancename='cpinstname%s' % i, vlinstanceid='vlinst%s' % i, ownertype='%d' % i, ownerid='%s' % self.nf_inst_id, relatedtype='%d' % i).save()
+ CPInstModel(cpinstanceid="cp%s" % i, cpdid='cpd%s' % i, cpinstancename='cpinstname%s' % i,
+ vlinstanceid='vlinst%s' % i, ownertype='%d' % i, ownerid='%s' % self.nf_inst_id,
+ relatedtype='%d' % i).save()
response = self.client.get("/openoapi/vnfres/v1/%s/cps" % self.nf_inst_id)
+ self.failUnlessEqual(status.HTTP_200_OK, response.status_code)
self.assertEqual(self.cps_data, response.data)
- # self.failUnlessEqual(status.HTTP_200_OK, response.status_code)
def test_get_volumes(self):
for i in range(1):
- StorageInstModel(storageid="st%s" % i, vimid='vim%s' % i, resouceid='res%s' % i, insttype='%d' % i, instid='%s' % self.nf_inst_id, storagetype='stype%s' % i, size='%s' % i, disktype='disk%s' % i).save()
+ StorageInstModel(storageid="st%s" % i, vimid='vim%s' % i, resouceid='res%s' % i, insttype='%d' % i,
+ instid='%s' % self.nf_inst_id, storagetype='stype%s' % i, size='%s' % i,
+ disktype='disk%s' % i).save()
response = self.client.get("/openoapi/vnfres/v1/%s/volumes" % self.nf_inst_id)
+ self.failUnlessEqual(status.HTTP_200_OK, response.status_code)
self.assertEqual(self.volumes_data, response.data)
- # self.failUnlessEqual(status.HTTP_200_OK, response.status_code)
def test_swagger_ok(self):
- resp = self.client.get("/openoapi/vnfres/v1/resources/swagger.json", format='json')
- self.assertEqual(resp.status_code, status.HTTP_200_OK) \ No newline at end of file
+ resp = self.client.get("/openoapi/vnfres/v1/swagger.json", format='json')
+ self.assertEqual(resp.status_code, status.HTTP_200_OK)
diff --git a/res/res/resources/urls.py b/res/res/resources/urls.py
index d7447d0..6d7c792 100644
--- a/res/res/resources/urls.py
+++ b/res/res/resources/urls.py
@@ -21,13 +21,13 @@ from res.resources.views import SwaggerJsonView
urlpatterns = [
url(r'^openoapi/vnfres/v1/vnfs/(?P<vnfInstanceId>[0-9a-zA-Z\-\_]+)$', views.get_vnf, name='get_vnf'),
url(r'^openoapi/vnfres/v1/vnfs$', views.get_vnfs, name='get_vnfs'),
- url(r'^'+'openoapi/vnfres/v1/(?P<vnfInstanceId>[0-9a-zA-Z\-\_]+)/vms$', views.get_vms, name='get_vms'),
- url(r'^'+'openoapi/vnfres/v1/(?P<vnfInstanceId>[0-9a-zA-Z\-\_]+)/flavors$', views.get_flavors, name='get_flavors'),
- url(r'^'+'openoapi/vnfres/v1/(?P<vnfInstanceId>[0-9a-zA-Z\-\_]+)/networks$', views.get_networks, name='get_networks'),
- url(r'^'+'openoapi/vnfres/v1/(?P<vnfInstanceId>[0-9a-zA-Z\-\_]+)/subnets$', views.get_subnets, name='get_subnets'),
- url(r'^'+'openoapi/vnfres/v1/(?P<vnfInstanceId>[0-9a-zA-Z\-\_]+)/cps$', views.get_cps, name='get_cps'),
- url(r'^'+'openoapi/vnfres/v1/(?P<vnfInstanceId>[0-9a-zA-Z\-\_]+)/volumes$', views.get_volumes, name='get_volumes'),
- url(r'^openoapi/vnfres/v1/resources/swagger.json$', SwaggerJsonView.as_view()),
+ url(r'^openoapi/vnfres/v1/(?P<vnfInstanceId>[0-9a-zA-Z\-\_]+)/vms$', views.get_vms, name='get_vms'),
+ url(r'^openoapi/vnfres/v1/(?P<vnfInstanceId>[0-9a-zA-Z\-\_]+)/flavors$', views.get_flavors, name='get_flavors'),
+ url(r'^openoapi/vnfres/v1/(?P<vnfInstanceId>[0-9a-zA-Z\-\_]+)/networks$', views.get_networks, name='get_networks'),
+ url(r'^openoapi/vnfres/v1/(?P<vnfInstanceId>[0-9a-zA-Z\-\_]+)/subnets$', views.get_subnets, name='get_subnets'),
+ url(r'^openoapi/vnfres/v1/(?P<vnfInstanceId>[0-9a-zA-Z\-\_]+)/cps$', views.get_cps, name='get_cps'),
+ url(r'^openoapi/vnfres/v1/(?P<vnfInstanceId>[0-9a-zA-Z\-\_]+)/volumes$', views.get_volumes, name='get_volumes'),
+ url(r'^openoapi/vnfres/v1/swagger.json$', SwaggerJsonView.as_view()),
]
urlpatterns = format_suffix_patterns(urlpatterns)
diff --git a/res/res/resources/views.py b/res/res/resources/views.py
index fa639a6..cfbe8c5 100644
--- a/res/res/resources/views.py
+++ b/res/res/resources/views.py
@@ -38,8 +38,6 @@ def get_vnf(request, *args, **kwargs):
vnf_inst = NfInstModel.objects.filter(nfinstid=vnf_inst_id)
if not vnf_inst:
return Response(data={'error': 'Vnf(%s) does not exist' % vnf_inst_id}, status=status.HTTP_404_NOT_FOUND)
- # TODO: fill resp_data
- # resp_data = {"vnfInstanceId": vnf_inst_id, "vnfInstanceName": vnf_inst[0].nf_name, "vnfInstanceDescription":}
resp_data = fill_resp_data(vnf_inst[0])
return Response(data=resp_data, status=status.HTTP_200_OK)
except:
@@ -48,10 +46,6 @@ def get_vnf(request, *args, **kwargs):
def fill_resp_data(vnf):
- # cp_insts = CPInstModel.objects.filter(ownerid=vnf_inst_id)
- # if not cp_insts:
- # raise VNFRESException('CpInst(%s) does not exist.' % vnf_inst_id)
- # cp_inst = cp_insts.first()
logger.info('Get the StorageInstModel of list')
storage_inst = StorageInstModel.objects.filter(instid=vnf.nfinstid)
arr = []
@@ -164,7 +158,6 @@ def get_vnfs(request):
vnf_insts = NfInstModel.objects.all()
if not vnf_insts:
return Response(data={'error': 'Vnfs does not exist'}, status=status.HTTP_404_NOT_FOUND)
- # FIXME: fill resp_data
arr = []
for vnf_inst in vnf_insts:
arr.append(fill_resp_data(vnf_inst))
@@ -182,7 +175,6 @@ def get_vms(request, *args, **kwargs):
vms = VmInstModel.objects.filter(instid=vnf_inst_id)
if not vms:
return Response(data={'error': 'Vms does not exist'}, status=status.HTTP_404_NOT_FOUND)
- # FIXME: fill resp_data
arr = []
for vm in vms:
arr.append(fill_vms_data(vm))
@@ -218,7 +210,6 @@ def get_flavors(request, *args, **kwargs):
flavours = FlavourInstModel.objects.filter(instid=vnf_inst_id)
if not flavours:
return Response(data={'error': 'Flavours does not exist'}, status=status.HTTP_404_NOT_FOUND)
- # FIXME: fill resp_data
arr = []
for flavour in flavours:
arr.append(fill_flavours_data(flavour))
@@ -242,6 +233,7 @@ def fill_flavours_data(f):
}
return flavours_data
+
@api_view(http_method_names=['GET'])
def get_networks(request, *args, **kwargs):
logger.debug("Query all the networks by vnfInstanceId[%s]", fun_name())
@@ -250,7 +242,6 @@ def get_networks(request, *args, **kwargs):
networks = NetworkInstModel.objects.filter(instid=vnf_inst_id)
if not networks:
return Response(data={'error': 'Networks does not exist'}, status=status.HTTP_404_NOT_FOUND)
- # FIXME: fill resp_data
arr = []
for network in networks:
arr.append(fill_networks_data(network))
@@ -291,7 +282,6 @@ def get_subnets(request, *args, **kwargs):
subnets = SubNetworkInstModel.objects.filter(instid=vnf_inst_id)
if not subnets:
return Response(data={'error': 'Subnets does not exist'}, status=status.HTTP_404_NOT_FOUND)
- # FIXME: fill resp_data
arr = []
for subnet in subnets:
arr.append(fill_subnets_data(subnet))
@@ -323,7 +313,6 @@ def get_cps(request, *args, **kwargs):
cps = CPInstModel.objects.filter(ownerid=vnf_inst_id)
if not cps:
return Response(data={'error': 'Cps does not exist'}, status=status.HTTP_404_NOT_FOUND)
- # FIXME: fill resp_data
arr = []
for cp in cps:
arr.append(fill_cps_data(cp))
@@ -345,6 +334,7 @@ def fill_cps_data(cp):
}
return cps_data
+
@api_view(http_method_names=['GET'])
def get_volumes(request, *args, **kwargs):
logger.debug("Query all the volumes by vnfInstanceId[%s]", fun_name())
@@ -353,7 +343,6 @@ def get_volumes(request, *args, **kwargs):
volumes = StorageInstModel.objects.filter(instid=vnf_inst_id)
if not volumes:
return Response(data={'error': 'Volumes does not exist'}, status=status.HTTP_404_NOT_FOUND)
- # FIXME: fill resp_data
arr = []
for v in volumes:
arr.append(fill_volumes_data(v))
@@ -383,4 +372,4 @@ class SwaggerJsonView(APIView):
f = open(json_file)
json_data = json.JSONDecoder().decode(f.read())
f.close()
- return Response(json_data) \ No newline at end of file
+ return Response(json_data)