3 # Copyright 2016 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.
20 from gi
.repository
import (
25 import rift
.openmano
.openmano_client
as openmano_client
29 class DataCenterPublisher(object):
31 This class is reponsible for exposing the data centers associated with an
32 openmano cloud account.
35 XPATH
= "D,/rw-launchpad:datacenters"
37 def __init__(self
, tasklet
):
38 """Creates an instance of a DataCenterPublisher
41 tasklet - the tasklet that this publisher is registered for
44 self
.tasklet
= tasklet
49 """The DTS instance used by this tasklet"""
50 return self
.tasklet
.dts
54 """The logger used by this tasklet"""
55 return self
.tasklet
.log
59 """The event loop used by this tasklet"""
60 return self
.tasklet
.loop
64 """The known openmano cloud accounts"""
66 for acc
in self
.tasklet
.cloud_accounts
:
67 if acc
.account_type
== "openmano":
68 accounts
.append(acc
.account_msg
)
74 """Registers the publisher with DTS"""
77 def on_prepare(xact_info
, action
, ks_path
, msg
):
79 # Create a datacenters instance to hold all of the cloud
81 datacenters
= RwLaunchpadYang
.DataCenters()
83 # Iterate over the known openmano accounts and populate cloud
84 # account instances with the corresponding data center info
85 for account
in self
.accounts
:
87 cloud_account
= RwLaunchpadYang
.CloudAccount()
88 cloud_account
.name
= account
.name
90 # Create a client for this cloud account to query for
91 # the associated data centers
92 client
= openmano_client
.OpenmanoCliAPI(
94 account
.openmano
.host
,
95 account
.openmano
.port
,
96 account
.openmano
.tenant_id
,
99 # Populate the cloud account with the data center info
100 for uuid
, name
in client
.datacenter_list():
101 cloud_account
.datacenters
.append(
102 RwLaunchpadYang
.DataCenter(
108 datacenters
.cloud_accounts
.append(cloud_account
)
110 except Exception as e
:
111 self
.log
.exception(e
)
113 xact_info
.respond_xpath(
114 RwDts
.XactRspCode
.MORE
,
115 'D,/rw-launchpad:datacenters',
119 xact_info
.respond_xpath(RwDts
.XactRspCode
.ACK
)
121 except Exception as e
:
122 self
.log
.exception(e
)
125 handler
= rift
.tasklets
.DTS
.RegistrationHandler(on_prepare
=on_prepare
)
127 with self
.dts
.group_create() as group
:
128 self
.reg
= group
.register(
129 xpath
=DataCenterPublisher
.XPATH
,
131 flags
=RwDts
.Flag
.PUBLISHER
,