Add OpenStackClient plugin for provider
This patch adds data protection support to the python-openstackclient through a plugin for provider. Co-Authored-By: Spencer Yu <yushb@gohighsec.com> Change-Id: I54d731f29c7f27c3b020a0de741d6efe0b062c8b Partially-Implements: blueprint karbor-support-python-openstackclient
This commit is contained in:
101
karborclient/osc/v1/providers.py
Normal file
101
karborclient/osc/v1/providers.py
Normal file
@@ -0,0 +1,101 @@
|
||||
# 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.
|
||||
|
||||
"""Data protection V1 provider action implementations"""
|
||||
|
||||
import six
|
||||
|
||||
from osc_lib.command import command
|
||||
from osc_lib import utils as osc_utils
|
||||
from oslo_log import log as logging
|
||||
|
||||
from karborclient.i18n import _
|
||||
|
||||
|
||||
class ListProviders(command.Lister):
|
||||
_description = _("List providers.")
|
||||
|
||||
log = logging.getLogger(__name__ + ".ListProviders")
|
||||
|
||||
def get_parser(self, prog_name):
|
||||
parser = super(ListProviders, self).get_parser(prog_name)
|
||||
parser.add_argument(
|
||||
'--name',
|
||||
metavar='<name>',
|
||||
help=_('Filters results by a name. Default=None.'),
|
||||
)
|
||||
parser.add_argument(
|
||||
'--description',
|
||||
metavar='<description>',
|
||||
help=_('Filters results by a description. Default=None.'),
|
||||
)
|
||||
parser.add_argument(
|
||||
'--marker',
|
||||
metavar='<provider>',
|
||||
help=_('The last provider ID of the previous page'),
|
||||
)
|
||||
parser.add_argument(
|
||||
'--limit',
|
||||
type=int,
|
||||
metavar='<num-providers>',
|
||||
help=_('Maximum number of providers to display'),
|
||||
)
|
||||
parser.add_argument(
|
||||
'--sort',
|
||||
metavar="<key>[:<direction>]",
|
||||
default=None,
|
||||
help=_("Sort output by selected keys and directions(asc or desc) "
|
||||
"(default: name:asc), multiple keys and directions can be "
|
||||
"specified separated by comma"),
|
||||
)
|
||||
return parser
|
||||
|
||||
def take_action(self, parsed_args):
|
||||
self.log.debug("take_action(%s)", parsed_args)
|
||||
data_protection_client = self.app.client_manager.data_protection
|
||||
|
||||
search_opts = {
|
||||
'name': parsed_args.name,
|
||||
'description': parsed_args.description,
|
||||
}
|
||||
|
||||
data = data_protection_client.providers.list(
|
||||
search_opts=search_opts, marker=parsed_args.marker,
|
||||
limit=parsed_args.limit, sort=parsed_args.sort)
|
||||
|
||||
column_headers = ['Id', 'Name', 'Description']
|
||||
|
||||
return (column_headers,
|
||||
(osc_utils.get_item_properties(
|
||||
s, column_headers
|
||||
) for s in data))
|
||||
|
||||
|
||||
class ShowProvider(command.ShowOne):
|
||||
_description = "Shows provider details"
|
||||
|
||||
def get_parser(self, prog_name):
|
||||
parser = super(ShowProvider, self).get_parser(prog_name)
|
||||
parser.add_argument(
|
||||
'provider',
|
||||
metavar="<provider>",
|
||||
help=_('The UUID of the provider.')
|
||||
)
|
||||
return parser
|
||||
|
||||
def take_action(self, parsed_args):
|
||||
client = self.app.client_manager.data_protection
|
||||
provider = osc_utils.find_resource(client.providers,
|
||||
parsed_args.provider)
|
||||
|
||||
provider._info.pop("links", None)
|
||||
return zip(*sorted(six.iteritems(provider._info)))
|
||||
141
karborclient/tests/unit/osc/v1/test_providers.py
Normal file
141
karborclient/tests/unit/osc/v1/test_providers.py
Normal file
@@ -0,0 +1,141 @@
|
||||
# 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 karborclient.osc.v1 import providers as osc_providers
|
||||
from karborclient.tests.unit.osc.v1 import fakes
|
||||
from karborclient.v1 import providers
|
||||
|
||||
|
||||
PROVIDER_INFO = {
|
||||
"id": "2220f8b1-975d-4621-a872-fa9afb43cb6c",
|
||||
"name": "OS Infra Provider",
|
||||
"description": "provider description",
|
||||
"extended_info_schema": {
|
||||
"options_schema": {
|
||||
"OS::Cinder::Volume": {
|
||||
"required": [
|
||||
"backup_mode"
|
||||
],
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"backup_mode": {
|
||||
"default": "auto",
|
||||
"enum": [
|
||||
"full",
|
||||
"incremental",
|
||||
"auto"
|
||||
],
|
||||
"type": "string",
|
||||
"description": "The backup mode.",
|
||||
"title": "Backup Mode"
|
||||
}
|
||||
},
|
||||
"title": "Cinder Protection Options"
|
||||
}
|
||||
},
|
||||
"saved_info_schema": {
|
||||
"OS::Cinder::Volume": {
|
||||
"required": [
|
||||
"name"
|
||||
],
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"name": {
|
||||
"type": "string",
|
||||
"description": "The name for this backup.",
|
||||
"title": "Name"
|
||||
}
|
||||
},
|
||||
"title": "Cinder Protection Saved Info"
|
||||
}
|
||||
},
|
||||
"restore_schema": {
|
||||
"OS::Cinder::Volume": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"restore_name": {
|
||||
"type": "string",
|
||||
"description": "The name of the restored volume.",
|
||||
"title": "Restore Name"
|
||||
}
|
||||
},
|
||||
"title": "Cinder Protection Restore"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class TestProviders(fakes.TestDataProtection):
|
||||
def setUp(self):
|
||||
super(TestProviders, self).setUp()
|
||||
self.providers_mock = self.app.client_manager.data_protection.providers
|
||||
self.providers_mock.reset_mock()
|
||||
|
||||
|
||||
class TestListProviders(TestProviders):
|
||||
def setUp(self):
|
||||
super(TestListProviders, self).setUp()
|
||||
self.providers_mock.list.return_value = [providers.Provider(
|
||||
None, PROVIDER_INFO)]
|
||||
|
||||
# Command to test
|
||||
self.cmd = osc_providers.ListProviders(self.app, None)
|
||||
|
||||
def test_providers_list(self):
|
||||
arglist = ['--name', 'OS Infra Provider']
|
||||
verifylist = [('name', 'OS Infra Provider')]
|
||||
|
||||
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
|
||||
|
||||
columns, data = self.cmd.take_action(parsed_args)
|
||||
|
||||
# Check that columns are correct
|
||||
expected_columns = (
|
||||
['Id', 'Name', 'Description'])
|
||||
self.assertEqual(expected_columns, columns)
|
||||
|
||||
# Check that data is correct
|
||||
expected_data = [("2220f8b1-975d-4621-a872-fa9afb43cb6c",
|
||||
"OS Infra Provider",
|
||||
"provider description")]
|
||||
self.assertEqual(expected_data, list(data))
|
||||
|
||||
|
||||
class TestShowProvider(TestProviders):
|
||||
def setUp(self):
|
||||
super(TestShowProvider, self).setUp()
|
||||
self.providers_mock.get.return_value = providers.Provider(
|
||||
None, PROVIDER_INFO)
|
||||
|
||||
# Command to test
|
||||
self.cmd = osc_providers.ShowProvider(self.app, None)
|
||||
|
||||
def test_provider_show(self):
|
||||
arglist = ['2220f8b1-975d-4621-a872-fa9afb43cb6c']
|
||||
verifylist = [('provider', '2220f8b1-975d-4621-a872-fa9afb43cb6c')]
|
||||
|
||||
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
|
||||
|
||||
columns, data = self.cmd.take_action(parsed_args)
|
||||
|
||||
# Check that columns are correct
|
||||
expected_columns = (
|
||||
'description', 'extended_info_schema', 'id', 'name')
|
||||
self.assertEqual(expected_columns, columns)
|
||||
|
||||
# Check that data is correct
|
||||
self.assertEqual(PROVIDER_INFO['description'], data[0])
|
||||
self.assertEqual(PROVIDER_INFO['extended_info_schema'], data[1])
|
||||
self.assertEqual(PROVIDER_INFO['id'], data[2])
|
||||
self.assertEqual(PROVIDER_INFO['name'], data[3])
|
||||
@@ -42,6 +42,8 @@ openstack.data_protection.v1 =
|
||||
data_protection_restore_list = karborclient.osc.v1.restores:ListRestores
|
||||
data_protection_restore_show = karborclient.osc.v1.restores:ShowRestore
|
||||
data_protection_restore_create = karborclient.osc.v1.restores:CreateRestore
|
||||
data_protection_provider_list = karborclient.osc.v1.providers:ListProviders
|
||||
data_protection_provider_show = karborclient.osc.v1.providers:ShowProvider
|
||||
|
||||
[compile_catalog]
|
||||
directory = karborclient/locale
|
||||
|
||||
Reference in New Issue
Block a user