-def create_vim_res(data, do_notify, do_rollback):
- try:
- for vol in ignore_case_get(data, "volume_storages"):
- create_volume(vol, do_notify, OPT_CREATE_VOLUME)
- for network in ignore_case_get(data, "vls"):
- create_network(network, do_notify, OPT_CREATE_NETWORK)
- for subnet in ignore_case_get(data, "vls"):
- create_subnet(subnet, do_notify, OPT_CREATE_SUBNET)
- for port in ignore_case_get(data, "cps"):
- create_port(port, do_notify, OPT_CREATE_PORT)
- for flavor in ignore_case_get(data, "vdus"):
- create_flavor(flavor, do_notify, OPT_CREATE_FLAVOR)
- for vm in ignore_case_get(data, "vdus"):
- create_vm(vm, do_notify, OPT_CREATE_VM)
- do_notify(RES_END, {})
- except VimException as e:
- logger.error(e.message)
- do_rollback(e.message)
- except:
- logger.error(traceback.format_exc())
- do_rollback(str(sys.exc_info()))
-
-def create_volume(vol, do_notify, progress):
+
+def get_tenant_id(vim_cache, vim_id, tenant_name):
+ if vim_id not in vim_cache:
+ tenants = api.list_tenant(vim_id)
+ vim_cache[vim_id] = {}
+ for tenant in tenants["tenants"]:
+ id, name = tenant["id"], tenant["name"]
+ vim_cache[vim_id][name] = id
+ if tenant_name not in vim_cache[vim_id]:
+ raise VimException("Tenant(%s) not found in vim(%s)" % (tenant_name, vim_id), ERR_CODE)
+ return vim_cache[vim_id][tenant_name]
+
+def set_res_cache(res_cache, res_type, key, val):
+ if res_type not in res_cache:
+ res_cache[res_type] = {}
+ if key in res_cache[res_type]:
+ raise VimException("Duplicate key(%s) of %s" % (key, res_type), ERR_CODE)
+ res_cache[res_type][key] = val
+
+def get_res_id(res_cache, res_type, key):
+ if res_type not in res_cache:
+ raise VimException("%s not found in cache" % res_type, ERR_CODE)
+ if key not in res_cache[res_type]:
+ raise VimException("%s(%s) not found in cache" % (res_type, key), ERR_CODE)
+ return res_cache[res_type][key]
+
+def create_vim_res(data, do_notify):
+ vim_cache, res_cache = {}, {}
+ for vol in ignore_case_get(data, "volume_storages"):
+ create_volume(vim_cache, res_cache, vol, do_notify, RES_VOLUME)
+ for network in ignore_case_get(data, "vls"):
+ create_network(vim_cache, res_cache, network, do_notify, RES_NETWORK)
+ for subnet in ignore_case_get(data, "vls"):
+ create_subnet(vim_cache, res_cache, subnet, do_notify, RES_SUBNET)
+ for port in ignore_case_get(data, "cps"):
+ create_port(vim_cache, res_cache, data, port, do_notify, RES_PORT)
+ for flavor in ignore_case_get(data, "vdus"):
+ create_flavor(vim_cache, res_cache, data, flavor, do_notify, RES_FLAVOR)
+ for vm in ignore_case_get(data, "vdus"):
+ create_vm(vim_cache, res_cache, data, vm, do_notify, RES_VM)
+
+def delete_vim_res(data, do_notify):
+ res_types = [RES_VM, RES_FLAVOR, RES_PORT, RES_SUBNET, RES_NETWORK, RES_VOLUME]
+ res_del_funs = [api.delete_vm, api.delete_flavor, api.delete_port,
+ api.delete_subnet, api.delete_network, api.delete_volume]
+ for res_type, res_del_fun in zip(res_types, res_del_funs):
+ for res in ignore_case_get(data, res_type):
+ try:
+ res_del_fun(res["vim_id"], res["tenant_id"], res["res_id"])
+ except VimException as e:
+ logger.error("Failed to delete %s(%s)", res_type, res["res_id"])
+ logger.error("%s:%s", e.http_code, e.message)
+ do_notify(res_type, res["res_id"])
+
+def create_volume(vim_cache, res_cache, vol, do_notify, res_type):
+ location_info = vol["properties"]["location_info"]