+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]
+