diff options
author | Peter Stachowski <peter@tesora.com> | 2016-03-08 00:22:06 -0500 |
---|---|---|
committer | Peter Stachowski <peter@tesora.com> | 2016-03-11 18:10:50 -0500 |
commit | 457360c69f651aea92769c9e543492d770b84595 (patch) | |
tree | c9fb106454232e8372102b066d9b4a954d4c1c78 /troveclient/v1/instances.py | |
parent | 3c71e52b408bbd6c33fca85188db2c8f156fb341 (diff) | |
download | python-troveclient-457360c69f651aea92769c9e543492d770b84595.tar.gz |
Client support for instance module feature2.2.0
This adds support in the python API and Trove CLI
for instance module commands. These commands include:
- module-apply
- module-remove
- module-query
- module-retrieve
- module-list-instance
The parsing of --instance was modified to allow multiple
modules to be specified. This was extended to 'nics' as well.
Partially Implements: blueprint module-management
Change-Id: If62f5e51d4628cc6a8b10303d5c3893b3bd5057e
Diffstat (limited to 'troveclient/v1/instances.py')
-rw-r--r-- | troveclient/v1/instances.py | 87 |
1 files changed, 86 insertions, 1 deletions
diff --git a/troveclient/v1/instances.py b/troveclient/v1/instances.py index a9c5014..7d461c0 100644 --- a/troveclient/v1/instances.py +++ b/troveclient/v1/instances.py @@ -15,12 +15,15 @@ # License for the specific language governing permissions and limitations # under the License. +import os import warnings from troveclient import base from troveclient import common from troveclient import exceptions from troveclient.i18n import _LW +from troveclient import utils +from troveclient.v1 import modules as core_modules from swiftclient import client as swift_client @@ -85,7 +88,8 @@ class Instances(base.ManagerWithFind): def create(self, name, flavor_id, volume=None, databases=None, users=None, restorePoint=None, availability_zone=None, datastore=None, datastore_version=None, nics=None, configuration=None, - replica_of=None, slave_of=None, replica_count=None): + replica_of=None, slave_of=None, replica_count=None, + modules=None): """Create (boot) a new instance.""" body = {"instance": { @@ -123,6 +127,8 @@ class Instances(base.ManagerWithFind): body["instance"]["replica_of"] = base.getid(replica_of) or slave_of if replica_count: body["instance"]["replica_count"] = replica_count + if modules: + body["instance"]["modules"] = self._get_module_list(modules) return self._create("/instances", body, "instance") @@ -248,6 +254,85 @@ class Instances(base.ManagerWithFind): body = {'eject_replica_source': {}} self._action(instance, body) + def modules(self, instance): + """Get the list of modules for a specific instance.""" + return self._modules_get(instance) + + def module_query(self, instance): + """Query an instance about installed modules.""" + return self._modules_get(instance, from_guest=True) + + def module_retrieve(self, instance, directory=None, prefix=None): + """Retrieve the module data file from an instance. This includes + the contents of the module data file. + """ + if directory: + try: + os.makedirs(directory, exist_ok=True) + except TypeError: + # py27 + try: + os.makedirs(directory) + except OSError: + if not os.path.isdir(directory): + raise + else: + directory = '.' + prefix = prefix or '' + if prefix and not prefix.endswith('_'): + prefix += '_' + module_list = self._modules_get( + instance, from_guest=True, include_contents=True) + saved_modules = {} + for module in module_list: + filename = '%s%s_%s_%s.dat' % (prefix, module.name, + module.datastore, + module.datastore_version) + full_filename = os.path.expanduser( + os.path.join(directory, filename)) + with open(full_filename, 'wb') as fh: + fh.write(utils.decode_data(module.contents)) + saved_modules[module.name] = full_filename + return saved_modules + + def _modules_get(self, instance, from_guest=None, include_contents=None): + url = "/instances/%s/modules" % base.getid(instance) + query_strings = {} + if from_guest is not None: + query_strings["from_guest"] = from_guest + if include_contents is not None: + query_strings["include_contents"] = include_contents + url = common.append_query_strings(url, **query_strings) + resp, body = self.api.client.get(url) + common.check_for_exceptions(resp, body, url) + return [core_modules.Module(self, module, loaded=True) + for module in body['modules']] + + def module_apply(self, instance, modules): + """Apply modules to an instance.""" + url = "/instances/%s/modules" % base.getid(instance) + body = {"modules": self._get_module_list(modules)} + resp, body = self.api.client.post(url, body=body) + common.check_for_exceptions(resp, body, url) + return [core_modules.Module(self, module, loaded=True) + for module in body['modules']] + + def _get_module_list(self, modules): + """Build a list of module ids.""" + module_list = [] + for module in modules: + module_info = {'id': base.getid(module)} + module_list.append(module_info) + return module_list + + def module_remove(self, instance, module): + """Remove a module from an instance. + """ + url = "/instances/%s/modules/%s" % (base.getid(instance), + base.getid(module)) + resp, body = self.api.client.delete(url) + common.check_for_exceptions(resp, body, url) + def log_list(self, instance): """Get a list of all guest logs. |