3 # Copyright 2016-2017 RIFT.IO Inc
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
22 from gi
.repository
import(
27 gi
.require_version('RwKeyspec', '1.0')
28 from gi
.repository
.RwKeyspec
import quoted_key
30 class CloudAccountNotFound(Exception):
34 class CloudAccountDtsOperdataHandler(object):
35 def __init__(self
, dts
, log
, loop
, project
):
39 self
._project
= project
43 self
.cloud_accounts
= {}
45 def add_cloud_account(self
, account
):
46 self
.cloud_accounts
[account
.name
] = account
47 asyncio
.ensure_future(
48 account
.start_validate_credentials(self
._loop
),
52 def delete_cloud_account(self
, account_name
):
53 del self
.cloud_accounts
[account_name
]
55 def get_saved_cloud_accounts(self
, cloud_account_name
):
56 ''' Get Cloud Account corresponding to passed name, or all saved accounts if name is None'''
57 saved_cloud_accounts
= []
59 if cloud_account_name
is None or cloud_account_name
== "":
60 cloud_accounts
= list(self
.cloud_accounts
.values())
61 saved_cloud_accounts
.extend(cloud_accounts
)
62 elif cloud_account_name
in self
.cloud_accounts
:
63 account
= self
.cloud_accounts
[cloud_account_name
]
64 saved_cloud_accounts
.append(account
)
66 errstr
= "Cloud account {} does not exist".format(cloud_account_name
)
67 raise KeyError(errstr
)
69 return saved_cloud_accounts
72 def create_notification(self
, account
):
73 xpath
= "N,/rw-cloud:cloud-notif"
74 ac_status
= RwCloudYang
.YangNotif_RwCloud_CloudNotif()
75 ac_status
.name
= account
.name
76 ac_status
.message
= account
.connection_status
.details
78 yield from self
._dts
.query_create(xpath
, rwdts
.XactFlag
.ADVISE
, ac_status
)
79 self
._log
.info("Notification called by creating dts query: %s", ac_status
)
83 def _register_show_status(self
):
84 def get_xpath(cloud_name
=None):
85 return "D,/rw-cloud:cloud/account{}/connection-status".format(
86 "[name=%s]" % quoted_key(cloud_name
) if cloud_name
is not None else ''
90 def on_prepare(xact_info
, action
, ks_path
, msg
):
91 path_entry
= RwCloudYang
.YangData_RwProject_Project_Cloud_Account
.schema().keyspec_to_entry(ks_path
)
92 cloud_account_name
= path_entry
.key00
.name
95 saved_accounts
= self
.get_saved_cloud_accounts(cloud_account_name
)
96 for account
in saved_accounts
:
97 connection_status
= account
.connection_status
98 xpath
= self
._project
.add_project(get_xpath(account
.name
))
99 xact_info
.respond_xpath(
100 rwdts
.XactRspCode
.MORE
,
102 msg
=account
.connection_status
,
104 except KeyError as e
:
105 self
._log
.warning(str(e
))
106 xact_info
.respond_xpath(rwdts
.XactRspCode
.NA
)
109 xact_info
.respond_xpath(rwdts
.XactRspCode
.ACK
)
111 xpath
= self
._project
.add_project(get_xpath())
112 self
._regh
= yield from self
._dts
.register(
114 handler
=rift
.tasklets
.DTS
.RegistrationHandler(
115 on_prepare
=on_prepare
),
116 flags
=rwdts
.Flag
.PUBLISHER
,
120 def _register_validate_rpc(self
):
122 return "/rw-cloud:update-cloud-status"
125 def on_prepare(xact_info
, action
, ks_path
, msg
):
126 if not msg
.has_field("cloud_account"):
127 raise CloudAccountNotFound("Cloud account name not provided")
128 cloud_account_name
= msg
.cloud_account
130 if not self
._project
.rpc_check(msg
, xact_info
=xact_info
):
134 account
= self
.cloud_accounts
[cloud_account_name
]
136 errmsg
= "Cloud account name {} not found in project {}". \
137 format(cloud_account_name
, self
._project
.name
)
138 xact_info
.send_error_xpath(RwTypes
.RwStatus
.FAILURE
,
141 raise CloudAccountNotFound(errmsg
)
143 yield from account
.start_validate_credentials(self
._loop
)
145 yield from self
.create_notification(account
)
147 xact_info
.respond_xpath(rwdts
.XactRspCode
.ACK
)
149 self
._rpc
= yield from self
._dts
.register(
151 handler
=rift
.tasklets
.DTS
.RegistrationHandler(
152 on_prepare
=on_prepare
154 flags
=rwdts
.Flag
.PUBLISHER
,
159 self
._log
.debug("Register cloud account for project %s", self
._project
.name
)
160 yield from self
._register
_show
_status
()
161 yield from self
._register
_validate
_rpc
()
163 def deregister(self
):
164 self
._log
.debug("De-register cloud account for project %s", self
._project
.name
)
165 self
._rpc
.deregister()
166 self
._regh
.deregister()