python-manilaclient/manilaclient/v2/services.py
Goutham Pacha Ravi c7b1c6a1d3 Refactor code from oslo_incubator
At some point, common code for this project
originated in the oslo_incubator project; and
we performed a final sync of this code in 2014 [1].

However, this sync'ed code was unused; so lets
delete this code and consolidate common code for
ease of use.

[1] https://review.opendev.org/c/openstack/python-manilaclient/+/98088

Change-Id: Ifd9c7516aab8e3c2d017a1ce421d2e65e76d5640
Signed-off-by: Goutham Pacha Ravi <gouthampravi@gmail.com>
2022-01-21 19:47:04 +00:00

93 lines
3.3 KiB
Python

# Copyright 2014 OpenStack Foundation
# All Rights Reserved.
#
# 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.
from manilaclient import api_versions
from manilaclient import base
RESOURCE_PATH_LEGACY = '/os-services'
RESOURCE_PATH = '/services'
RESOURCE_NAME = 'services'
class Service(base.Resource):
def __repr__(self):
return "<Service: %s>" % self.id
def server_api_version(self, **kwargs):
"""Get api version."""
return self.manager.api_version(self, kwargs)
class ServiceManager(base.Manager):
"""Manage :class:`Service` resources."""
resource_class = Service
def _do_list(self, search_opts=None, resource_path=RESOURCE_PATH):
"""Get a list of all services.
:rtype: list of :class:`Service`
"""
query_string = self._build_query_string(search_opts)
return self._list(resource_path + query_string, RESOURCE_NAME)
@api_versions.wraps("1.0", "2.6")
def list(self, search_opts=None):
return self._do_list(
search_opts=search_opts, resource_path=RESOURCE_PATH_LEGACY)
@api_versions.wraps("2.7") # noqa
def list(self, search_opts=None): # noqa
return self._do_list(
search_opts=search_opts, resource_path=RESOURCE_PATH)
def _do_enable(self, host, binary, resource_path=RESOURCE_PATH):
"""Enable the service specified by hostname and binary."""
body = {"host": host, "binary": binary}
return self._update("%s/enable" % resource_path, body)
@api_versions.wraps("1.0", "2.6")
def enable(self, host, binary):
return self._do_enable(host, binary, RESOURCE_PATH_LEGACY)
@api_versions.wraps("2.7") # noqa
def enable(self, host, binary): # noqa
return self._do_enable(host, binary, RESOURCE_PATH)
def _do_disable(self, host, binary, resource_path=RESOURCE_PATH):
"""Disable the service specified by hostname and binary."""
body = {"host": host, "binary": binary}
return self._update("%s/disable" % resource_path, body)
@api_versions.wraps("1.0", "2.6")
def disable(self, host, binary):
return self._do_disable(host, binary, RESOURCE_PATH_LEGACY)
@api_versions.wraps("2.7") # noqa
def disable(self, host, binary): # noqa
return self._do_disable(host, binary, RESOURCE_PATH)
def server_api_version(self, url_append=""):
"""Returns the API Version supported by the server.
:param url_append: String to append to url to obtain specific version
:return: Returns response obj for a server that supports microversions.
Returns an empty list for Kilo and prior Manila servers.
"""
try:
return self._get_with_base_url(url_append, 'versions')
except LookupError:
return []