33from rest_framework .views import APIView
44from rest_framework .response import Response
55from rest_framework .parsers import MultiPartParser , FileUploadParser
6+ from rest_framework import status
7+ from django .db import IntegrityError
68
79from . import tasks
8- from .models import Blueprint
9- from .serializers import BlueprintSerializer
10+ from .models import Blueprint , Container
11+ from .serializers import BlueprintSerializer , ContainerSerializer
1012
1113logger = logging .getLogger ("views" )
1214
@@ -23,43 +25,90 @@ class BlueprintsView(APIView):
2325
2426 def get (self , request ):
2527 """
26- # List all available blueprints
28+ List all available blueprints
2729 """
2830 s = BlueprintSerializer (Blueprint .objects .all (), many = True )
2931 return Response (s .data )
3032
31- def put (self , request ):
32- """
33- # Upload new blueprint archive
34- """
35- b = Blueprint .objects .create (archive = request .data ["file" ])
36- s = BlueprintSerializer (b )
37- pipe = (
38- tasks .upload_blueprint .si (b .cfy_id ) |
39- tasks .create_deployment .si (b .cfy_id ) |
40- tasks .install .si (b .cfy_id )
41- )
42- pipe .apply_async ()
43- return Response (s .data , status = 201 )
44-
4533
4634class BlueprintIdView (APIView ):
4735 def get (self , request , blueprint_id ):
4836 """
49- # Return selected blueprint details
37+ Return selected blueprint details
5038 """
5139 s = BlueprintSerializer (Blueprint .get (blueprint_id ))
5240 return Response (s .data )
5341
5442 def delete (self , request , blueprint_id ):
5543 """
56- # Delete selected blueprint
44+ Delete selected blueprint
45+ """
46+ blueprint = Blueprint .get (blueprint_id )
47+ blueprint .pipe_undeploy_blueprint ()
48+ return Response (status = status .HTTP_202_ACCEPTED )
49+
50+
51+ class ContainersView (APIView ):
52+ def get (self , request ):
53+ """
54+ List all virtual containers and their status information
55+ """
56+ contaiers = Container .objects .all ()
57+ s = ContainerSerializer (contaiers , many = True )
58+ return Response (data = s .data )
59+
60+ def post (self , request ):
61+ container = Container ()
62+ container .save () # all default is good
63+ s = ContainerSerializer (container )
64+ return Response (data = s .data , status = status .HTTP_201_CREATED )
65+
66+
67+ class ContainerIdView (APIView ):
68+ def get (self , request , container_id ):
69+ """
70+ Display the status information about the selected virtual container
71+ """
72+ container = Container .get (container_id )
73+ s = ContainerSerializer (container )
74+ return Response (s .data )
75+
76+ def delete (self , request , container_id ):
77+ """
78+ Remove virtual container and undeploy its blueprint.
5779 """
58- b = Blueprint .get (blueprint_id )
59- pipe = (
60- tasks .uninstall .si (b .cfy_id ) |
61- tasks .delete_deployment .si (b .cfy_id ) |
62- tasks .delete_blueprint .si (b .cfy_id )
63- )
64- pipe .apply_async ()
65- return Response (status = 202 )
80+ container = Container .get (container_id )
81+ try :
82+ container .delete ()
83+ except IntegrityError , e :
84+ return Response ({'msg' : e .message }, status = status .HTTP_400_BAD_REQUEST )
85+
86+ return Response (status = status .HTTP_204_NO_CONTENT )
87+
88+
89+ class ContainerBlueprint (APIView ):
90+ def post (self , request , container_id ):
91+ cont = Container .get (container_id )
92+ blueprint_old = cont .blueprint
93+ blueprint_new = Blueprint .objects .create (archive = request .data ["file" ])
94+
95+ # bind new blueprint to this container
96+ cont .blueprint = blueprint_new
97+ cont .save ()
98+
99+ # deploy the new blueprint
100+ blueprint_new .pipe_deploy_blueprint ()
101+
102+ # undeploy the old blueprint
103+ if blueprint_old :
104+ # TODO: keep container-blueprint binding to old blueprint until cloudify undeploys it
105+ blueprint_old .pipe_undeploy_blueprint ()
106+
107+ cont_ser = ContainerSerializer (cont ).data
108+ return Response (cont_ser , status = status .HTTP_202_ACCEPTED )
109+
110+
111+
112+
113+
114+
0 commit comments