1 # Copyright 2021 Canonical Ltd.
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
18 from motor
.motor_asyncio
import AsyncIOMotorClient
19 from n2vc
.config
import EnvironConfig
20 from n2vc
.vca
.connection_data
import ConnectionData
21 from osm_common
.dbmongo
import DbMongo
, DbException
22 from osm_common
.dbbase
import Encryption
30 async def get_vca_connection_data(self
, vca_id
: str) -> ConnectionData
:
32 Get VCA connection data
34 :param: vca_id: VCA ID
36 :returns: ConnectionData with the information of the database
40 async def update_vca_endpoints(self
, hosts
: typing
.List
[str], vca_id
: str):
44 :param: endpoints: List of endpoints to write in the database
45 :param: vca_id: VCA ID
49 async def get_vca_endpoints(self
, vca_id
: str = None) -> typing
.List
[str]:
51 Get list if VCA endpoints
53 :param: vca_id: VCA ID
55 :returns: List of endpoints
59 async def get_vca_id(self
, vim_id
: str = None) -> str:
61 Get VCA id for a VIM account
63 :param: vim_id: Vim account ID
67 class DbMongoStore(Store
):
68 def __init__(self
, db
: DbMongo
):
72 :param: db: osm_common.dbmongo.DbMongo object
76 async def get_vca_connection_data(self
, vca_id
: str) -> ConnectionData
:
78 Get VCA connection data
80 :param: vca_id: VCA ID
82 :returns: ConnectionData with the information of the database
84 data
= self
.db
.get_one("vca", q_filter
={"_id": vca_id
})
85 self
.db
.encrypt_decrypt_fields(
89 schema_version
=data
["schema_version"],
92 return ConnectionData(**data
)
94 async def update_vca_endpoints(
95 self
, endpoints
: typing
.List
[str], vca_id
: str = None
100 :param: endpoints: List of endpoints to write in the database
101 :param: vca_id: VCA ID
104 data
= self
.db
.get_one("vca", q_filter
={"_id": vca_id
})
105 data
["endpoints"] = endpoints
106 self
._update
("vca", vca_id
, data
)
108 # The default VCA. Data for the endpoints is in a different place
109 juju_info
= self
._get
_juju
_info
()
110 # If it doesn't, then create it
117 except DbException
as e
:
118 # Racing condition: check if another N2VC worker has created it
119 juju_info
= self
._get
_juju
_info
()
125 {"api_endpoints": endpoints
},
128 async def get_vca_endpoints(self
, vca_id
: str = None) -> typing
.List
[str]:
130 Get list if VCA endpoints
132 :param: vca_id: VCA ID
134 :returns: List of endpoints
138 endpoints
= self
.get_vca_connection_data(vca_id
).endpoints
140 juju_info
= self
._get
_juju
_info
()
141 if juju_info
and "api_endpoints" in juju_info
:
142 endpoints
= juju_info
["api_endpoints"]
145 async def get_vca_id(self
, vim_id
: str = None) -> str:
147 Get VCA ID from the database for a given VIM account ID
149 :param: vim_id: VIM account ID
154 q_filter
={"_id": vim_id
},
161 def _update(self
, collection
: str, id: str, data
: dict):
163 Update object in database
165 :param: collection: Collection name
166 :param: id: ID of the object
167 :param: data: Object data
175 def _get_juju_info(self
):
176 """Get Juju information (the default VCA) from the admin collection"""
177 return self
.db
.get_one(
179 q_filter
={"_id": "juju"},
184 class MotorStore(Store
):
185 def __init__(self
, uri
: str):
189 :param: uri: Connection string to connect to the database.
191 self
._client
= AsyncIOMotorClient(uri
)
192 self
._secret
_key
= None
193 self
._config
= EnvironConfig(prefixes
=["OSMLCM_", "OSMMON_"])
194 self
.encryption
= Encryption(
197 encoding_type
="utf-8",
203 return self
._client
[DB_NAME
]
206 def _vca_collection(self
):
207 return self
._database
["vca"]
210 def _admin_collection(self
):
211 return self
._database
["admin"]
214 def _vim_accounts_collection(self
):
215 return self
._database
["vim_accounts"]
217 async def get_vca_connection_data(self
, vca_id
: str) -> ConnectionData
:
219 Get VCA connection data
221 :param: vca_id: VCA ID
223 :returns: ConnectionData with the information of the database
225 data
= await self
._vca
_collection
.find_one({"_id": vca_id
})
227 raise Exception("vca with id {} not found".format(vca_id
))
228 await self
.encryption
.decrypt_fields(
230 ["secret", "cacert"],
231 schema_version
=data
["schema_version"],
234 return ConnectionData(**data
)
236 async def update_vca_endpoints(
237 self
, endpoints
: typing
.List
[str], vca_id
: str = None
242 :param: endpoints: List of endpoints to write in the database
243 :param: vca_id: VCA ID
246 data
= await self
._vca
_collection
.find_one({"_id": vca_id
})
247 data
["endpoints"] = endpoints
248 await self
._vca
_collection
.replace_one({"_id": vca_id
}, data
)
250 # The default VCA. Data for the endpoints is in a different place
251 juju_info
= await self
._get
_juju
_info
()
252 # If it doesn't, then create it
255 await self
._admin
_collection
.insert_one({"_id": "juju"})
256 except Exception as e
:
257 # Racing condition: check if another N2VC worker has created it
258 juju_info
= await self
._get
_juju
_info
()
262 await self
._admin
_collection
.replace_one(
263 {"_id": "juju"}, {"api_endpoints": endpoints
}
266 async def get_vca_endpoints(self
, vca_id
: str = None) -> typing
.List
[str]:
268 Get list if VCA endpoints
270 :param: vca_id: VCA ID
272 :returns: List of endpoints
276 endpoints
= (await self
.get_vca_connection_data(vca_id
)).endpoints
278 juju_info
= await self
._get
_juju
_info
()
279 if juju_info
and "api_endpoints" in juju_info
:
280 endpoints
= juju_info
["api_endpoints"]
283 async def get_vca_id(self
, vim_id
: str = None) -> str:
285 Get VCA ID from the database for a given VIM account ID
287 :param: vim_id: VIM account ID
291 vim_account
= await self
._vim
_accounts
_collection
.find_one({"_id": vim_id
})
292 if vim_account
and "vca" in vim_account
:
293 vca_id
= vim_account
["vca"]
296 async def _get_juju_info(self
):
297 """Get Juju information (the default VCA) from the admin collection"""
298 return await self
._admin
_collection
.find_one({"_id": "juju"})