plugin interface for manila to backends
This commit contains the interface code to connect between the manila charm and a backend plugin that will provide configuration for the manila. Plugin charms are subordinate because they need to be on the same unit/machine as the manila file share service. This interface also implements a 1-many interface and so contains code to manage that. That code will be abstracted back to charms.openstack at some point. Change-Id: Ied0ad014ab7b1d4778113b0d3f2bbae08075372e
This commit is contained in:
parent
99afa9509e
commit
a4f85602af
9
.gitignore
vendored
Normal file
9
.gitignore
vendored
Normal file
@ -0,0 +1,9 @@
|
||||
bin
|
||||
.coverage
|
||||
.testrepository
|
||||
.tox
|
||||
tags
|
||||
*.sw[nop]
|
||||
*.pyc
|
||||
.unit-state.db
|
||||
.local
|
202
LICENSE
Normal file
202
LICENSE
Normal file
@ -0,0 +1,202 @@
|
||||
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
95
README.md
Normal file
95
README.md
Normal file
@ -0,0 +1,95 @@
|
||||
# Overview
|
||||
|
||||
This interface provides a mechanism for 'plugins' to the the Manila charm to
|
||||
provide configuration sections/parts to to the Manila charm.
|
||||
|
||||
It is intended to be used to enable manila share backends to be configured
|
||||
independently of the main charm, so that vendors/maintainers can supply their
|
||||
own backend configuration charms as the project continues.
|
||||
|
||||
# Manila usage
|
||||
|
||||
This interface is used between the Manila charm (share backend role or all) to
|
||||
provide the manila service user auth details to backend configuraiton charms,
|
||||
and to obtain configuration information that needs to be written into 8
|
||||
No explicit handler is required to consume this interface in charms
|
||||
that consume this interface.
|
||||
|
||||
The interface provides `manila-plugin.connected` and `manila-plugin.available`
|
||||
states.
|
||||
|
||||
## For a Manila backend subordinate charm
|
||||
|
||||
A Manila subordinate charm uses this interface to configure the manila
|
||||
configuration files to 'plugin' its functionality. e.g. the generic backend
|
||||
needs authentication information (the manila service user) so that it can
|
||||
generate the configuration information for nova, neutron and cinder as part of
|
||||
the backend configuration in manila.conf.
|
||||
|
||||
The generic charm then needs to set the configuration that the manila principal
|
||||
charm will use to write the manila.conf and other files. i.e. the manila charm
|
||||
will fold in the data presented into the its configuration information.
|
||||
|
||||
## How the conversation 'goes' between the principal (manila) and subordinate
|
||||
|
||||
1. Juju connects the two charms.
|
||||
2. When the 'requires' side connects (hook
|
||||
`{requires:manila-plugin}-relation-joined` fires), the Manila principal
|
||||
charm will receive the state({relation_name}.connected).
|
||||
3. When the 'provides' side connect (hook
|
||||
`{provides:manila-plugin}-relation-joined` fires) the subordinate charm
|
||||
will receive the state({relation_name}.connected).
|
||||
4. When the Manila charm has received keystone authentication information it
|
||||
will set that on the relation, and on receipt the 'provides' side will set
|
||||
the `{relation_name}.available` state. This allows the providing
|
||||
charm (subordinate backend) to use this information.
|
||||
5. If the subordinate needs the auth info, then it should block until
|
||||
`{relation_name}.available` is available.
|
||||
6. The subordinate should present its configuration information on the
|
||||
interface, which will be received on the 'requires' side. This will set
|
||||
the `{relation_name}.available`.
|
||||
7. If either side changes 'their' data (e.g. the data they set on the
|
||||
interface), then the receiving side will set the `{relation_name}.changed`
|
||||
state. IF the consuming charm uses the `...changed` state, it MUST remove
|
||||
it as otherwise it will not 'receive' it again.
|
||||
|
||||
The important points are:
|
||||
|
||||
- the `...available` state indicates the _first_ time that the data from the
|
||||
_other_ side of the interface.
|
||||
- the `...changed` state indicates the _next_ and subsequent times that data
|
||||
from the _other_ side has changed. This may not be important to the
|
||||
subordinate, but Manila will use this to rewrite the configuration files.
|
||||
- If the subordinate consumes the `...changed` state, then it must remove it
|
||||
at the end of processing, so that it can 'see' it the next time it is set.
|
||||
|
||||
|
||||
# metadata
|
||||
|
||||
To consume this interface in your charm or layer, add the following to
|
||||
`layer.yaml`:
|
||||
|
||||
```yaml
|
||||
includes: ['interface:manila-plugin']
|
||||
```
|
||||
|
||||
and add a provides interface of type `manila` to your charm or layers
|
||||
`metadata.yaml`:
|
||||
|
||||
```yaml
|
||||
provides:
|
||||
manila:
|
||||
interface: manila-plugin
|
||||
scope: container
|
||||
```
|
||||
|
||||
Please see the example 'Manila generic backend' charm for an example of how to
|
||||
author a manila backend configuration charm.
|
||||
|
||||
# Bugs
|
||||
|
||||
Please report bugs on
|
||||
[Launchpad](https://bugs.launchpad.net/openstack-charms/+filebug).
|
||||
|
||||
For development questions please refer to the OpenStack
|
||||
[Charm Guide](https://github.com/openstack/charm-guide).
|
4
interface.yaml
Normal file
4
interface.yaml
Normal file
@ -0,0 +1,4 @@
|
||||
name: manila-plugin
|
||||
summary: Interface for configuration plugins to charm-manila
|
||||
maintainer: OpenStack Charmers <openstack-charmers@lists.ubuntu.com>
|
||||
repo: https://github.com/openstack/charm-interface-manila-plugin
|
166
provides.py
Normal file
166
provides.py
Normal file
@ -0,0 +1,166 @@
|
||||
# Copyright 2016 Canonical Ltd
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import json
|
||||
|
||||
import charms.reactive as reactive
|
||||
|
||||
|
||||
class ManilaPluginProvides(reactive.RelationBase):
|
||||
"""This is the subordinate end of the relation. i.e. the configuration
|
||||
provider to the manila plugin.
|
||||
|
||||
The purpose of the provides side of the interface is to provide the manila
|
||||
charm with configuration information to correctly link to whatever the
|
||||
plugin wants to do. e.g. for a backend, to configure manila to use the
|
||||
appropriate backend.
|
||||
|
||||
The manila charm provides the service user manila, and other authentication
|
||||
information that it can use to configure other services in the OpenStack
|
||||
system.
|
||||
"""
|
||||
scope = reactive.scopes.GLOBAL
|
||||
|
||||
# These remote data fields will be automatically mapped to accessors
|
||||
# with a basic documentation string provided.
|
||||
auto_accessors = ['_authentication_data']
|
||||
|
||||
@reactive.hook('{provides:manila-plugin}-relation-joined')
|
||||
def joined(self):
|
||||
self.set_state('{relation_name}.connected')
|
||||
self.update_status()
|
||||
|
||||
@reactive.hook('{provides:manila-plugin}-relation-changed')
|
||||
def changed(self):
|
||||
"""This hook is used to indicate that something has changed on the
|
||||
interface and that interested parties should recheck the properties
|
||||
exposed to see if they need to do anything.
|
||||
|
||||
The handler should clear the state after it has been consumed so that
|
||||
the next change gets registered too.
|
||||
"""
|
||||
self.update_status()
|
||||
|
||||
@reactive.hook('{provides:manila-plugin}-relation-{broken,departed}')
|
||||
def departed(self):
|
||||
self.remove_state('{relation_name}.changed')
|
||||
self.remove_state('{relation_name}.available')
|
||||
self.remove_state('{relation_name}.connected')
|
||||
|
||||
def update_status(self):
|
||||
"""Set the .available and .changed state if both the plugin name and
|
||||
the authentication data are available.
|
||||
|
||||
Note that the .changed state can be used if the plugin changes the
|
||||
data. Thus, a subordinate charm (e.g. generic backend) can watched
|
||||
changed and then clear it using the method clear_changed() to update
|
||||
configuration files as needed.
|
||||
|
||||
The interface will NOT set .changed without having .available at the
|
||||
same time.
|
||||
"""
|
||||
if self._authentication_data() is not None:
|
||||
self.set_state('{relation_name}.available')
|
||||
self.set_state('{relation_name}.changed')
|
||||
|
||||
def clear_changed(self):
|
||||
"""Provide a convenient method to clear the .changed relation"""
|
||||
self.remove_state('{relation_name}.changed')
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
"""Returns the name if it has been set"""
|
||||
scope = self.conversations()[0].scope
|
||||
return self.get_local('_name', default=None, scope=scope)
|
||||
|
||||
@name.setter
|
||||
def name(self, name):
|
||||
"""Set the name plugin -- this is for logs, and to distinguish between
|
||||
multiple plugins.
|
||||
|
||||
:param name: a string indicating the name of the plugin (or None)
|
||||
"""
|
||||
scope = self.conversations()[0].scope
|
||||
self.set_local(_name=name, scope=scope)
|
||||
self.set_remote(_name=name, scope=scope)
|
||||
|
||||
@property
|
||||
def authentication_data(self):
|
||||
"""Return authentication data provided by the Manila charm, or None if
|
||||
the data has not yet been set.
|
||||
|
||||
The authentication data is set when the Manila charm has received it
|
||||
over its identity interface; thus this may return None until that data
|
||||
has become available. This means that the configuration data may be
|
||||
delayed until this is available.
|
||||
|
||||
The authentication data format is:
|
||||
{
|
||||
'username': <value>
|
||||
'password': <value>
|
||||
'project_domain_id': <value>
|
||||
'project_name': <value>
|
||||
'user_domain_id': <value>
|
||||
'auth_uri': <value>
|
||||
'auth_url': <value>
|
||||
'auth_type': <value> # 'password', typically
|
||||
}
|
||||
|
||||
:returns: data object that was passed.
|
||||
"""
|
||||
data = self._authentication_data()
|
||||
if data is None:
|
||||
return None
|
||||
return json.loads(data)["data"]
|
||||
|
||||
@property
|
||||
def configuration_data(self):
|
||||
"""Get the configuration data (if it has been set yet) or None"""
|
||||
scope = self.conversations()[0].scope
|
||||
data = self.get_local('_configuration_data', default=None, scope=scope)
|
||||
if data is None:
|
||||
return
|
||||
return json.loads(data)["data"]
|
||||
|
||||
@configuration_data.setter
|
||||
def configuration_data(self, data):
|
||||
"""
|
||||
|
||||
NOTE that the data is wrapped in a dictionary, converted to JSON and
|
||||
then placed in the juju remote variable. The other 'end' unpacks this
|
||||
and provides the original data to Manila charm.
|
||||
|
||||
If complete is False (or missing) then the configuration data is only
|
||||
partially complete OR the subordinate charm is not ready yet -- e.g. it
|
||||
still has to configure something.
|
||||
|
||||
The format of the data is:
|
||||
{
|
||||
"complete": <boolean>,
|
||||
'<config file>': {
|
||||
'<section>: (
|
||||
(key, value),
|
||||
(key, value),
|
||||
)
|
||||
}
|
||||
|
||||
Thus data has to be JSONable.
|
||||
|
||||
:param data: object that describes the plugin data to be sent.
|
||||
"""
|
||||
scope = self.conversations()[0].scope
|
||||
self.set_local(_configuration_data=json.dumps({"data": data}),
|
||||
scope=scope)
|
||||
self.set_remote(_configuration_data=json.dumps({"data": data}),
|
||||
scope=scope)
|
235
requires.py
Normal file
235
requires.py
Normal file
@ -0,0 +1,235 @@
|
||||
# Copyright 2016 Canonical Ltd
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
import json
|
||||
|
||||
import charmhelpers.core.hookenv as hookenv
|
||||
import charms.reactive as reactive
|
||||
|
||||
|
||||
class ManilaPluginRequires(reactive.RelationBase):
|
||||
"""The is the Manila 'end' of the relation.
|
||||
|
||||
The auto accessors are underscored as for some reason RelationBase only
|
||||
provides these as 'calls'; i.e. they have to be used as `self._name()`.
|
||||
This class therefore provides @properties `name` and `plugin_data` that can
|
||||
be used directly.
|
||||
|
||||
This side of the interface sends the manila service user authentication
|
||||
information to the plugin charm (which is a subordinate) and gets
|
||||
configuration segments for the various files that the manila charm 'owns'
|
||||
and, therefore, writes out.
|
||||
|
||||
The most important property is the 'ready' property which indicates that
|
||||
the configuration data on the interface is valid and thus can be written to
|
||||
the configuration files by the manila charm.
|
||||
"""
|
||||
scope = reactive.scopes.UNIT
|
||||
|
||||
# These remote data fields will be automatically mapped to accessors
|
||||
# with a basic documentation string provided.
|
||||
auto_accessors = ['_name', '_configuration_data']
|
||||
|
||||
@reactive.hook('{requires:manila-plugin}-relation-joined')
|
||||
def joined(self):
|
||||
"""At least one manila-plugin has joined. Thus we set the connected
|
||||
state to allow the consumer to start setting authentication data.
|
||||
|
||||
We also update the status, as this may or may not be another plugin.
|
||||
"""
|
||||
self.set_state('{relation_name}.connected')
|
||||
self.update_status()
|
||||
|
||||
@reactive.hook('{requires:manila-plugin}-relation-changed')
|
||||
def changed(self):
|
||||
"""Something has changed in one of the plugins, so we use update_status
|
||||
to update the relation states to allow the consumer of the interface to
|
||||
update any structures that it needs.
|
||||
"""
|
||||
self.update_status()
|
||||
|
||||
@reactive.hook('{requires:manila-plugin}-relation-{broken,departed}')
|
||||
def departed(self):
|
||||
self.update_status()
|
||||
|
||||
def update_status(self):
|
||||
"""Set the .available and .changed state if at least one of the
|
||||
conversations (with the subordinate) has a name and some configuration
|
||||
data (regardless of whether it is complete).
|
||||
|
||||
As there can be multiple conversations, it is up to the subordinate
|
||||
charm to flag up problems with its juju status as the principal charm
|
||||
deals with multiple backends.
|
||||
|
||||
Note that the .changed state can be used if a plugin changes the data.
|
||||
Thus, Manila can watch changes and then clear it using the method
|
||||
clear_changed() to update configuration files as needed.
|
||||
|
||||
The interface will NOT set .changed without having .available at the
|
||||
same time.
|
||||
"""
|
||||
count_available = 0
|
||||
count_changed = 0
|
||||
count_conversations = 0
|
||||
for conversation in self.conversations():
|
||||
if conversation.scope is None:
|
||||
# the conversation has gone away; ignore it
|
||||
continue
|
||||
count_conversations += 1
|
||||
# try to see if we've already had this conversation
|
||||
conversation_available = self.get_local(
|
||||
'_available', default=None, scope=conversation.scope)
|
||||
name = self.get_remote(
|
||||
'_name', default=None, scope=conversation.scope)
|
||||
configuration_data = self.get_remote(
|
||||
'_configuration_data',
|
||||
default=None,
|
||||
scope=conversation.scope)
|
||||
if name is not None and configuration_data is not None:
|
||||
count_available += 1
|
||||
available = True
|
||||
else:
|
||||
available = False
|
||||
# if we've changed state (or just connected)
|
||||
if available != conversation_available:
|
||||
self.set_local(_available=available, scope=conversation.scope)
|
||||
count_changed += 1
|
||||
|
||||
# now update the relation states to convey what is happening.
|
||||
if count_changed:
|
||||
self.set_state('{relation_name}.changed')
|
||||
if count_available:
|
||||
self.set_state('{relation_name}.available')
|
||||
else:
|
||||
self.remove_state('{relation_name}.available')
|
||||
if not count_conversations:
|
||||
self.remove_state('{relation_name}.connected')
|
||||
self.remove_state('{relation_name}.changed')
|
||||
|
||||
def clear_changed(self):
|
||||
"""Provide a convenient method to clear the .changed relation"""
|
||||
self.remove_state('{relation_name}.changed')
|
||||
|
||||
def set_authentication_data(self, value, name=None):
|
||||
"""Set the authentication data to the plugin charm. This is to enable
|
||||
the plugin to either 'talk' to OpenStack or to provide authentication
|
||||
data into the configuraiton sections that it needs to set (the generic
|
||||
backend needs to do this).
|
||||
|
||||
The authentication data format is:
|
||||
{
|
||||
'username': <value>
|
||||
'password': <value>
|
||||
'project_domain_id': <value>
|
||||
'project_name': <value>
|
||||
'user_domain_id': <value>
|
||||
'auth_uri': <value>
|
||||
'auth_url': <value>
|
||||
'auth_type': <value> # 'password', typically
|
||||
}
|
||||
|
||||
:param value: a dictionary of data to set.
|
||||
:param name: OPTIONAL - target the config at a particular name only
|
||||
"""
|
||||
keys = {'username', 'password', 'project_domain_id', 'project_name',
|
||||
'user_domain_id', 'auth_uri', 'auth_url', 'auth_type'}
|
||||
passed_keys = set(value.keys())
|
||||
if passed_keys.difference(keys) or keys.difference(passed_keys):
|
||||
hookenv.log(
|
||||
"Setting Authentication data; there may be missing or mispelt "
|
||||
"keys: passed: {}".format(passed_keys),
|
||||
level=hookenv.WARNING)
|
||||
# need to check for each conversation whether we've sent the data, or
|
||||
# whether it is different, and then set the local & remote only if that
|
||||
# is the case.
|
||||
for conversation in self.conversations():
|
||||
if conversation.scope is None:
|
||||
# the conversation has gone away; ignore it
|
||||
continue
|
||||
if name is not None:
|
||||
conversation_name = self.get_remote('_name', default=None,
|
||||
scope=conversation.scope)
|
||||
if name != conversation_name:
|
||||
continue
|
||||
existing_auth_data = self.get_local('_authentication_data',
|
||||
default=None,
|
||||
scope=conversation.scope)
|
||||
if existing_auth_data is not None:
|
||||
# see if they are different
|
||||
existing_auth = json.loads(existing_auth_data)["data"]
|
||||
if (existing_auth.keys() == value.keys() and
|
||||
all([v == value[k]
|
||||
for k, v in existing_auth.items()])):
|
||||
# the values haven't changed, so don't set them again
|
||||
continue
|
||||
self.set_local(_authentication_data=json.dumps({"data": value}),
|
||||
scope=conversation.scope)
|
||||
self.set_remote(_authentication_data=json.dumps({"data": value}),
|
||||
scope=conversation.scope)
|
||||
|
||||
@property
|
||||
def names(self):
|
||||
"""Response with a list of names of backends where there is
|
||||
configuration data on the interface.
|
||||
|
||||
:returns: list of names from the interfaces which have config data
|
||||
"""
|
||||
names = []
|
||||
for conversation in self.conversations():
|
||||
if conversation.scope is None:
|
||||
# the conversation has gone away; ignore it
|
||||
continue
|
||||
name = self.get_remote('_name', default=None,
|
||||
scope=conversation.scope)
|
||||
config = self.get_remote('_configuration_data', default=None,
|
||||
scope=conversation.scope)
|
||||
if name and config:
|
||||
names.append(name)
|
||||
return names
|
||||
|
||||
def get_configuration_data(self, name=None):
|
||||
"""Return the configuration_data from the plugin if it is available.
|
||||
|
||||
The format of the data returned is:
|
||||
{
|
||||
'<config file>': {
|
||||
'<section>: (
|
||||
(key, value),
|
||||
(key, value),
|
||||
"or string",
|
||||
)
|
||||
}
|
||||
|
||||
if 'name' is provided, then only the configuration data for that name
|
||||
is returned, otherwise all of the configuration data for all
|
||||
conversations is returned as an amalgamated dict.
|
||||
|
||||
:param name: OPTIONAL: specify the name of the interface (_name)
|
||||
:returns: data object that was passed.
|
||||
"""
|
||||
result = {}
|
||||
for conversation in self.conversations():
|
||||
if conversation.scope is None:
|
||||
# the conversation has gone away; ignore it
|
||||
continue
|
||||
if name:
|
||||
_name = self.get_remote('_name', default=None,
|
||||
scope=conversation.scope)
|
||||
if _name != name:
|
||||
continue
|
||||
config = self.get_remote('_configuration_data', default=None,
|
||||
scope=conversation.scope)
|
||||
if config:
|
||||
result.update(json.loads(config)["data"])
|
||||
return result
|
2
test-requirements.txt
Normal file
2
test-requirements.txt
Normal file
@ -0,0 +1,2 @@
|
||||
flake8>=2.2.4,<=2.4.1
|
||||
os-testr>=0.4.1
|
34
tox.ini
Normal file
34
tox.ini
Normal file
@ -0,0 +1,34 @@
|
||||
[tox]
|
||||
envlist = pep8,py34,py35
|
||||
skipsdist = True
|
||||
skip_missing_interpreters = True
|
||||
|
||||
[testenv]
|
||||
setenv = VIRTUAL_ENV={envdir}
|
||||
PYTHONHASHSEED=0
|
||||
install_command =
|
||||
pip install --allow-unverified python-apt {opts} {packages}
|
||||
commands = ostestr {posargs}
|
||||
|
||||
[testenv:py34]
|
||||
basepython = python3.4
|
||||
deps = -r{toxinidir}/test-requirements.txt
|
||||
# TODO: Need to write unit tests then remove the following command.
|
||||
commands = /bin/true
|
||||
|
||||
[testenv:py35]
|
||||
basepython = python3.5
|
||||
deps = -r{toxinidir}/test-requirements.txt
|
||||
# TODO: Need to write unit tests then remove the following command.
|
||||
commands = /bin/true
|
||||
|
||||
[testenv:pep8]
|
||||
basepython = python2.7
|
||||
deps = -r{toxinidir}/test-requirements.txt
|
||||
commands = flake8 {posargs}
|
||||
|
||||
[testenv:venv]
|
||||
commands = {posargs}
|
||||
|
||||
[flake8]
|
||||
ignore = E402,E226
|
Loading…
Reference in New Issue
Block a user