VNFs with only VLDs compatibility
[osm/RO.git] / openmano
index ccd22e7..2434f95 100755 (executable)
--- a/openmano
+++ b/openmano
@@ -3,7 +3,7 @@
 # PYTHON_ARGCOMPLETE_OK
 
 ##
-# Copyright 2015 Telefónica Investigación y Desarrollo, S.A.U.
+# Copyright 2015 Telefonica Investigacion y Desarrollo, S.A.U.
 # This file is part of openmano
 # All Rights Reserved.
 #
@@ -28,8 +28,8 @@ openmano client used to interact with openmano-server (openmanod)
 """
 __author__ = "Alfonso Tierno, Gerardo Garcia, Pablo Montes"
 __date__ = "$09-oct-2014 09:09:48$"
-__version__ = "0.4.22-r532"
-version_date = "Oct 2017"
+__version__ = "0.4.23-r533"
+version_date = "May 2018"
 
 from argcomplete.completers import FilesCompleter
 import os
@@ -273,13 +273,13 @@ def vnf_create(args):
         if not vnfds:
             vnfds = vnfd_catalog.get("vnfd")
         vnfd = vnfds[0]
-        vdu_list = vnfd["vdu"]
+        vdu_list = vnfd.get("vdu")
 
     else:  # old API
         api_version = ""
         token = "vnfs"
         vnfd = myvnf['vnf']
-        vdu_list = vnfd["VNFC"]
+        vdu_list = vnfd.get("VNFC")
 
     if args.name or args.description or args.image_path or args.image_name or args.image_checksum:
         # TODO, change this for API v3
@@ -289,62 +289,63 @@ def vnf_create(args):
                 vnfd['name'] = args.name
             if args.description:
                 vnfd['description'] = args.description
-            if args.image_path:
-                index = 0
-                for image_path_ in args.image_path.split(","):
-                    # print "image-path", image_path_
-                    if api_version == "/v3":
-                        if vdu_list[index].get("image"):
-                            vdu_list[index]['image'] = image_path_
-                            if "image-checksum" in vdu_list[index]:
-                                del vdu_list[index]["image-checksum"]
-                        else:  # image name in volumes
-                            vdu_list[index]["volumes"][0]["image"] = image_path_
-                            if "image-checksum" in vdu_list[index]["volumes"][0]:
-                                del vdu_list[index]["volumes"][0]["image-checksum"]
-                    else:
-                        vdu_list[index]['VNFC image'] = image_path_
-                        if "image name" in vdu_list[index]:
-                            del vdu_list[index]["image name"]
-                        if "image checksum" in vdu_list[index]:
-                            del vdu_list[index]["image checksum"]
-                    index += 1
-            if args.image_name:  # image name precedes if both are supplied
-                index = 0
-                for image_name_ in args.image_name.split(","):
-                    if api_version == "/v3":
-                        if vdu_list[index].get("image"):
-                            vdu_list[index]['image'] = image_name_
-                            if "image-checksum" in vdu_list[index]:
-                                del vdu_list[index]["image-checksum"]
-                            if vdu_list[index].get("alternative-images"):
-                                for a_image in vdu_list[index]["alternative-images"]:
-                                    a_image['image'] = image_name_
-                                    if "image-checksum" in a_image:
-                                        del a_image["image-checksum"]
-                        else:  # image name in volumes
-                            vdu_list[index]["volumes"][0]["image"] = image_name_
-                            if "image-checksum" in vdu_list[index]["volumes"][0]:
-                                del vdu_list[index]["volumes"][0]["image-checksum"]
-                    else:
-                        vdu_list[index]['image name'] = image_name_
-                        if "VNFC image" in vdu_list[index]:
-                            del vdu_list[index]["VNFC image"]
-                    index += 1
-            if args.image_checksum:
-                index = 0
-                for image_checksum_ in args.image_checksum.split(","):
-                    if api_version == "/v3":
-                        if vdu_list[index].get("image"):
-                            vdu_list[index]['image-checksum'] = image_checksum_
-                            if vdu_list[index].get("alternative-images"):
-                                for a_image in vdu_list[index]["alternative-images"]:
-                                    a_image['image-checksum'] = image_checksum_
-                        else:  # image name in volumes
-                            vdu_list[index]["volumes"][0]["image-checksum"] = image_checksum_
-                    else:
-                        vdu_list[index]['image checksum'] = image_checksum_
-                    index += 1
+            if vdu_list:
+                if args.image_path:
+                    index = 0
+                    for image_path_ in args.image_path.split(","):
+                        # print "image-path", image_path_
+                        if api_version == "/v3":
+                            if vdu_list[index].get("image"):
+                                vdu_list[index]['image'] = image_path_
+                                if "image-checksum" in vdu_list[index]:
+                                    del vdu_list[index]["image-checksum"]
+                            else:  # image name in volumes
+                                vdu_list[index]["volumes"][0]["image"] = image_path_
+                                if "image-checksum" in vdu_list[index]["volumes"][0]:
+                                    del vdu_list[index]["volumes"][0]["image-checksum"]
+                        else:
+                            vdu_list[index]['VNFC image'] = image_path_
+                            if "image name" in vdu_list[index]:
+                                del vdu_list[index]["image name"]
+                            if "image checksum" in vdu_list[index]:
+                                del vdu_list[index]["image checksum"]
+                        index += 1
+                if args.image_name:  # image name precedes if both are supplied
+                    index = 0
+                    for image_name_ in args.image_name.split(","):
+                        if api_version == "/v3":
+                            if vdu_list[index].get("image"):
+                                vdu_list[index]['image'] = image_name_
+                                if "image-checksum" in vdu_list[index]:
+                                    del vdu_list[index]["image-checksum"]
+                                if vdu_list[index].get("alternative-images"):
+                                    for a_image in vdu_list[index]["alternative-images"]:
+                                        a_image['image'] = image_name_
+                                        if "image-checksum" in a_image:
+                                            del a_image["image-checksum"]
+                            else:  # image name in volumes
+                                vdu_list[index]["volumes"][0]["image"] = image_name_
+                                if "image-checksum" in vdu_list[index]["volumes"][0]:
+                                    del vdu_list[index]["volumes"][0]["image-checksum"]
+                        else:
+                            vdu_list[index]['image name'] = image_name_
+                            if "VNFC image" in vdu_list[index]:
+                                del vdu_list[index]["VNFC image"]
+                        index += 1
+                if args.image_checksum:
+                    index = 0
+                    for image_checksum_ in args.image_checksum.split(","):
+                        if api_version == "/v3":
+                            if vdu_list[index].get("image"):
+                                vdu_list[index]['image-checksum'] = image_checksum_
+                                if vdu_list[index].get("alternative-images"):
+                                    for a_image in vdu_list[index]["alternative-images"]:
+                                        a_image['image-checksum'] = image_checksum_
+                            else:  # image name in volumes
+                                vdu_list[index]["volumes"][0]["image-checksum"] = image_checksum_
+                        else:
+                            vdu_list[index]['image checksum'] = image_checksum_
+                        index += 1
         except (KeyError, TypeError), e:
             if str(e) == 'vnf':           error_pos= "missing field 'vnf'"
             elif str(e) == 'name':        error_pos= "missing field  'vnf':'name'"
@@ -926,7 +927,7 @@ def instance_scenario_action(args):
     tenant = _get_tenant()
     toact = _get_item_uuid("instances", args.name, tenant=tenant)
     action={}
-    action[ args.action ] = args.param
+    action[ args.action ] = yaml.safe_load(args.param)
     if args.vnf:
         action["vnfs"] = args.vnf
     if args.vm:
@@ -940,13 +941,16 @@ def instance_scenario_action(args):
     logger.debug("openmano response: %s", mano_response.text )
     result = 0 if mano_response.status_code==200 else mano_response.status_code
     content = mano_response.json()
-    #print json.dumps(content, indent=4)
+    # print json.dumps(content, indent=4)
     if mano_response.status_code == 200:
         if args.verbose:
             print yaml.safe_dump(content, indent=4, default_flow_style=False)
             return result
-        for uuid,c in content.iteritems():
-            print ("{:38} {:20} {:20}".format(uuid, c['name'], c['description']))
+        if "instance_action_id" in content:
+            print("instance_action_id={}".format(content["instance_action_id"]))
+        else:
+            for uuid,c in content.iteritems():
+                print ("{:38} {:20} {:20}".format(uuid, c.get('name'), c.get('description')))
     else:
         print content['error']['description']
     return result
@@ -1777,9 +1781,9 @@ if __name__=="__main__":
     instance_scenario_action_parser = subparsers.add_parser('instance-scenario-action', parents=[parent_parser], help="invoke an action over part or the whole scenario instance")
     instance_scenario_action_parser.add_argument("name", action="store", help="name or uuid of the scenario instance")
     instance_scenario_action_parser.add_argument("action", action="store", type=str, \
-            choices=["start","pause","resume","shutoff","shutdown","forceOff","rebuild","reboot", "console", "add_public_key"],\
+            choices=["start","pause","resume","shutoff","shutdown","forceOff","rebuild","reboot", "console", "add_public_key","vdu-scaling"],\
             help="action to send")
-    instance_scenario_action_parser.add_argument("param", nargs='?', help="addional param of the action. e.g. console type (novnc, ...), reboot type (TODO)")
+    instance_scenario_action_parser.add_argument("param", nargs='?', help="addional param of the action. e.g. console: novnc; reboot: type; vdu-scaling: '[{vdu-id: xxx, type: create|delete, count: 1}]'")
     instance_scenario_action_parser.add_argument("--vnf", action="append", help="VNF to act on (can use several entries)")
     instance_scenario_action_parser.add_argument("--vm", action="append", help="VM to act on (can use several entries)")
     instance_scenario_action_parser.set_defaults(func=instance_scenario_action)