uow: unit_of_work.AbstractUnitOfWork):
with uow:
first = uow.resource_pools.get(resourcePoolId)
- return first.serialize() if first is not None else None
+ return first.serialize() if first else None
def resources(resourcePoolId: str, uow: unit_of_work.AbstractUnitOfWork,
**kwargs):
+ with uow:
+ first = uow.resource_pools.get(resourcePoolId)
+ if first is None:
+ raise NotFoundException("ResourcePool {} doesn't exist".format(
+ resourcePoolId))
pagination = Pagination(**kwargs)
# filter key should be the same with database name
query_kwargs = pagination.get_pagination()
query_kwargs['resourceTypeId'] = restype_id
args = gen_filter(
ocloud.Resource, kwargs['filter']) if 'filter' in kwargs else []
- args.append(ocloud.Resource.resourcePoolId == resourcePoolId)
- # args.append(ocloud.Resource.parentId == None)
if 'parentId' in kwargs:
query_kwargs['parentId'] = kwargs['parentId']
return pagination.get_result(ret)
-def resource_one(resourceId: str, uow: unit_of_work.AbstractUnitOfWork):
+def resource_one(resourceId: str,
+ uow: unit_of_work.AbstractUnitOfWork, resourcePoolId: str):
with uow:
- first = uow.resources.get(resourceId)
- return first.serialize() if first is not None else None
+ resoucePool = uow.resource_pools.get(resourcePoolId)
+ if resoucePool is None:
+ raise NotFoundException("ResourcePool {} doesn't exist".format(
+ resourcePoolId))
+
+ first = uow.resources.get(resourceId)
+ if first is None:
+ raise NotFoundException("Resource {} doesn't exist".format(
+ resourceId))
+ return first.serialize()
def deployment_managers(uow: unit_of_work.AbstractUnitOfWork, **kwargs):
def _gen_kube_config(dmId: str, kubeconfig: dict) -> dict:
+ shared_folder = config.get_containers_shared_folder()
data = config.gen_k8s_config_dict(
kubeconfig.pop('cluster_api_endpoint', None),
current_time = datetime.now().strftime("%Y%m%d%H%M%S")
tmp_file_name = 'kubeconfig_' + name_key + "_" + current_time
kube_config_name = 'kubeconfig_' + name_key + '.config'
+ kube_config_path = f'{shared_folder}/{kube_config_name}'
# write down the yaml file of kubectl into tmp folder
with open('/tmp/' + tmp_file_name, 'w') as file:
# generate the kube config file if not exist or update the file if it
# changes
- if not os.path.exists('/configs/' + kube_config_name) or not \
- filecmp.cmp('/tmp/'+tmp_file_name, '/configs/'+kube_config_name):
+ if not os.path.exists(kube_config_path) or not \
+ filecmp.cmp('/tmp/'+tmp_file_name, kube_config_path):
shutil.move(os.path.join('/tmp', tmp_file_name),
- os.path.join('/configs', kube_config_name))
+ os.path.join(shared_folder, kube_config_name))
- return '/configs/'+kube_config_name
+ return kube_config_path
def subscriptions(uow: unit_of_work.AbstractUnitOfWork, **kwargs):
filter = subscriptionDto.get('filter', '')
consumer_subs_id = subscriptionDto.get('consumerSubscriptionId', '')
- check_filter(ocloud.Resource, filter)
+ _check_subscription_filter(filter)
sub_uuid = str(uuid.uuid4())
subscription = Subscription(
return first.serialize()
+def _check_subscription_filter(filter: str):
+ if not filter:
+ return
+
+ def _sub_filter_checking(sub_filter: str):
+ exprs = sub_filter.split(';')
+ objectType = False
+ objectTypeValue = ''
+ for expr in exprs:
+ expr_strip = expr.strip(' ()')
+ items = expr_strip.split(',')
+ if len(items) < 3:
+ raise BadRequestException("invalid filter {}".format(expr))
+ item_key = items[1].strip()
+ if item_key != 'objectType':
+ continue
+ item_op = items[0].strip()
+ if item_op != 'eq':
+ raise BadRequestException(
+ "Filter objectType only support 'eq' operation")
+ objectType = True
+ objectTypeValue = items[2].strip()
+ if not objectType:
+ # if there has no objectType specific, by default is ResourceInfo
+ check_filter(ocloud.Resource, sub_filter)
+ # return 'ResourceInfo'
+ return
+ if objectTypeValue == 'ResourceTypeInfo':
+ check_filter(ocloud.ResourceType, sub_filter)
+ elif objectTypeValue == 'ResourcePoolInfo':
+ check_filter(ocloud.ResourcePool, sub_filter)
+ elif objectTypeValue == 'DeploymentManagerInfo':
+ check_filter(ocloud.DeploymentManager, sub_filter)
+ elif objectTypeValue == 'CloudInfo':
+ check_filter(ocloud.Ocloud, sub_filter)
+ elif objectTypeValue == 'ResourceInfo':
+ check_filter(ocloud.Resource, sub_filter)
+ else:
+ raise BadRequestException(
+ "Filter ObjectType {} not support.".format(items[2]))
+ # return objectTypeValue
+ filter_strip = filter.strip(' []')
+ filter_list = filter_strip.split('|')
+ # check_duplication = dict()
+ for sub_filter in filter_list:
+ _sub_filter_checking(sub_filter)
+ # obj_type = _sub_filter_checking(sub_filter)
+ # if obj_type not in check_duplication:
+ # check_duplication[obj_type] = 0
+ # check_duplication[obj_type] += 1
+ # if check_duplication[obj_type] > 1:
+ # raise BadRequestException(
+ # "Filter objectType {} only support one in each."
+ # .format(obj_type))
+
+
def subscription_delete(subscriptionId: str,
uow: unit_of_work.AbstractUnitOfWork):
with uow: