1 # Copyright (C) 2021 Wind River Systems, Inc.
\r
3 # Licensed under the Apache License, Version 2.0 (the "License");
\r
4 # you may not use this file except in compliance with the License.
\r
5 # You may obtain a copy of the License at
\r
7 # http://www.apache.org/licenses/LICENSE-2.0
\r
9 # Unless required by applicable law or agreed to in writing, software
\r
10 # distributed under the License is distributed on an "AS IS" BASIS,
\r
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
12 # See the License for the specific language governing permissions and
\r
13 # limitations under the License.
\r
15 from sqlalchemy import select
\r
17 from o2ims.adapter.orm import ocloud, resource, resourcetype, \
\r
18 resourcepool, deploymentmanager, subscription
\r
19 from o2common.service import unit_of_work
\r
20 from o2ims.domain.ocloud import Subscription
\r
23 def oclouds(uow: unit_of_work.AbstractUnitOfWork):
\r
25 res = uow.session.execute(select(ocloud))
\r
26 return [dict(r) for r in res]
\r
29 def ocloud_one(ocloudid: str, uow: unit_of_work.AbstractUnitOfWork):
\r
31 res = uow.session.execute(
\r
32 select(ocloud).where(ocloud.c.oCloudId == ocloudid))
\r
34 return None if first is None else dict(first)
\r
37 def resource_types(uow: unit_of_work.AbstractUnitOfWork):
\r
39 res = uow.session.execute(select(resourcetype))
\r
40 return [dict(r) for r in res]
\r
43 def resource_type_one(resourceTypeId: str,
\r
44 uow: unit_of_work.AbstractUnitOfWork):
\r
46 res = uow.session.execute(select(resourcetype).where(
\r
47 resourcetype.c.resourceTypeId == resourceTypeId))
\r
49 return None if first is None else dict(first)
\r
52 def resource_pools(uow: unit_of_work.AbstractUnitOfWork):
\r
54 res = uow.session.execute(select(resourcepool))
\r
55 return [dict(r) for r in res]
\r
58 def resource_pool_one(resourcePoolId: str,
\r
59 uow: unit_of_work.AbstractUnitOfWork):
\r
61 res = uow.session.execute(select(resourcepool).where(
\r
62 resourcepool.c.resourcePoolId == resourcePoolId))
\r
64 return None if first is None else dict(first)
\r
67 def resources(resourcePoolId: str, uow: unit_of_work.AbstractUnitOfWork):
\r
69 res = uow.session.execute(select(resource).where(
\r
70 resource.c.resourcePoolId == resourcePoolId))
\r
71 return [dict(r) for r in res]
\r
74 def resource_one(resourceId: str, uow: unit_of_work.AbstractUnitOfWork):
\r
76 # topq = uow.session.query(resource).filter(
\r
77 # resource.c.resourceId == resourceId).cte('cte', recursive=True)
\r
78 # bootomq = uow.session.query(resource).join(
\r
79 # topq, resource.c.parentId == topq.c.resourceId)
\r
80 # res = uow.session.query(topq.union(bootomq))
\r
82 res = uow.session.execute(select(resource).where(
\r
83 resource.c.resourceId == resourceId))
\r
85 return None if first is None else dict(first)
\r
88 def deployment_managers(uow: unit_of_work.AbstractUnitOfWork):
\r
90 res = uow.session.execute(select(deploymentmanager))
\r
91 return [dict(r) for r in res]
\r
94 def deployment_manager_one(deploymentManagerId: str,
\r
95 uow: unit_of_work.AbstractUnitOfWork):
\r
97 res = uow.session.execute(select(deploymentmanager).where(
\r
98 deploymentmanager.c.deploymentManagerId == deploymentManagerId))
\r
100 return None if first is None else dict(first)
\r
103 def subscriptions(uow: unit_of_work.AbstractUnitOfWork):
\r
105 res = uow.session.execute(select(subscription))
\r
106 return [dict(r) for r in res]
\r
109 def subscription_one(subscriptionId: str,
\r
110 uow: unit_of_work.AbstractUnitOfWork):
\r
112 res = uow.session.execute(select(subscription).where(
\r
113 subscription.c.subscriptionId == subscriptionId))
\r
114 first = res.first()
\r
115 return None if first is None else dict(first)
\r
118 def subscription_create(subscription: Subscription,
\r
119 uow: unit_of_work.AbstractUnitOfWork):
\r
121 uow.subscriptions.add(subscription)
\r
125 def subscription_delete(subscriptionId: str,
\r
126 uow: unit_of_work.AbstractUnitOfWork):
\r
128 uow.subscriptions.delete(subscriptionId)
\r