90850e84cfcecc1ad42fd9314eea6f2635244d36
[pti/o2.git] / o2ims / views / ocloud_view.py
1 # Copyright (C) 2021 Wind River Systems, Inc.\r
2 #\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
6 #\r
7 #      http://www.apache.org/licenses/LICENSE-2.0\r
8 #\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
14 \r
15 from sqlalchemy import select\r
16 \r
17 from o2ims.adapter.orm import ocloud, resource, resourcetype, \\r
18     resourcepool, deploymentmanager, subscription\r
19 from o2ims.adapter import unit_of_work\r
20 from o2ims.domain.ocloud import Subscription\r
21 \r
22 \r
23 def oclouds(uow: unit_of_work.SqlAlchemyUnitOfWork):\r
24     with uow:\r
25         res = uow.session.execute(select(ocloud))\r
26     return [dict(r) for r in res]\r
27 \r
28 \r
29 def ocloud_one(ocloudid: str, uow: unit_of_work.AbstractUnitOfWork):\r
30     with uow:\r
31         res = uow.session.execute(\r
32             select(ocloud).where(ocloud.c.oCloudId == ocloudid))\r
33         first = res.first()\r
34     return None if first is None else dict(first)\r
35 \r
36 \r
37 def resource_types(uow: unit_of_work.SqlAlchemyUnitOfWork):\r
38     with uow:\r
39         res = uow.session.execute(select(resourcetype))\r
40     return [dict(r) for r in res]\r
41 \r
42 \r
43 def resource_type_one(resourceTypeId: str,\r
44                       uow: unit_of_work.SqlAlchemyUnitOfWork):\r
45     with uow:\r
46         res = uow.session.execute(select(resourcetype).where(\r
47             resourcetype.c.resourceTypeId == resourceTypeId))\r
48         first = res.first()\r
49     return None if first is None else dict(first)\r
50 \r
51 \r
52 def resource_pools(uow: unit_of_work.SqlAlchemyUnitOfWork):\r
53     with uow:\r
54         res = uow.session.execute(select(resourcepool))\r
55     return [dict(r) for r in res]\r
56 \r
57 \r
58 def resource_pool_one(resourcePoolId: str,\r
59                       uow: unit_of_work.SqlAlchemyUnitOfWork):\r
60     with uow:\r
61         res = uow.session.execute(select(resourcepool).where(\r
62             resourcepool.c.resourcePoolId == resourcePoolId))\r
63         first = res.first()\r
64     return None if first is None else dict(first)\r
65 \r
66 \r
67 def resources(resourcePoolId: str, uow: unit_of_work.SqlAlchemyUnitOfWork):\r
68     with uow:\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
72 \r
73 \r
74 def resource_one(resourceId: str, uow: unit_of_work.SqlAlchemyUnitOfWork):\r
75     with uow:\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
81         # print(res)\r
82         res = uow.session.execute(select(resource).where(\r
83             resource.c.resourceId == resourceId))\r
84         first = res.first()\r
85     return None if first is None else dict(first)\r
86 \r
87 \r
88 def deployment_managers(uow: unit_of_work.SqlAlchemyUnitOfWork):\r
89     with uow:\r
90         res = uow.session.execute(select(deploymentmanager))\r
91     return [dict(r) for r in res]\r
92 \r
93 \r
94 def deployment_manager_one(deploymentManagerId: str,\r
95                            uow: unit_of_work.SqlAlchemyUnitOfWork):\r
96     with uow:\r
97         res = uow.session.execute(select(deploymentmanager).where(\r
98             deploymentmanager.c.deploymentManagerId == deploymentManagerId))\r
99         first = res.first()\r
100     return None if first is None else dict(first)\r
101 \r
102 \r
103 def subscriptions(uow: unit_of_work.SqlAlchemyUnitOfWork):\r
104     with uow:\r
105         res = uow.session.execute(select(subscription))\r
106     return [dict(r) for r in res]\r
107 \r
108 \r
109 def subscription_one(subscriptionId: str,\r
110                      uow: unit_of_work.SqlAlchemyUnitOfWork):\r
111     with uow:\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
116 \r
117 \r
118 def subscription_create(subscription: Subscription,\r
119                         uow: unit_of_work.SqlAlchemyUnitOfWork):\r
120     with uow:\r
121         uow.subscriptions.add(subscription)\r
122         uow.commit()\r
123 \r
124 \r
125 def subscription_delete(subscriptionId: str,\r
126                         uow: unit_of_work.SqlAlchemyUnitOfWork):\r
127     with uow:\r
128         uow.subscriptions.delete(subscriptionId)\r
129         uow.commit()\r
130     return True\r