Merge "Extract provisioned networks from stack"

This commit is contained in:
Zuul 2020-11-19 11:02:43 +00:00 committed by Gerrit Code Review
commit e7ddf5d928
4 changed files with 146 additions and 0 deletions

View File

@ -52,6 +52,7 @@ openstack.tripleoclient.v2 =
overcloud_status = tripleoclient.v1.overcloud_deploy:GetDeploymentStatus
overcloud_image_build = tripleoclient.v1.overcloud_image:BuildOvercloudImage
overcloud_image_upload = tripleoclient.v1.overcloud_image:UploadOvercloudImage
overcloud_network_extract = tripleoclient.v2.overcloud_network:OvercloudNetworkExtract
overcloud_node_configure = tripleoclient.v1.overcloud_node:ConfigureNode
overcloud_node_delete = tripleoclient.v1.overcloud_node:DeleteNode
overcloud_node_import = tripleoclient.v2.overcloud_node:ImportNode

View File

@ -0,0 +1,67 @@
# Copyright 2020 Red Hat, Inc.
#
# 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 mock
from osc_lib import exceptions as osc_lib_exc
from tripleoclient.tests import fakes
from tripleoclient.v2 import overcloud_network
class TestOvercloudNetworkExtract(fakes.FakePlaybookExecution):
def setUp(self):
super(TestOvercloudNetworkExtract, self).setUp()
# Get the command object to test
app_args = mock.Mock()
app_args.verbose_level = 1
self.app.options = fakes.FakeOptions()
self.cmd = overcloud_network.OvercloudNetworkExtract(self.app,
app_args)
@mock.patch('tripleoclient.utils.TempDirs', autospect=True)
@mock.patch('os.path.abspath', autospect=True)
@mock.patch('tripleoclient.utils.run_ansible_playbook', autospec=True)
def test_overcloud_network_extract(self, mock_playbook, mock_abspath,
mock_tempdirs):
mock_abspath.return_value = '/test/test'
arglist = ['--stack', 'overcloud', '--output', 'test', '--yes']
parsed_args = self.check_parser(self.cmd, arglist, [])
self.cmd.take_action(parsed_args)
mock_playbook.assert_called_once_with(
workdir=mock.ANY,
playbook='cli-overcloud-network-extract.yaml',
inventory=mock.ANY,
playbook_dir=mock.ANY,
verbosity=3,
extra_vars={
"stack_name": 'overcloud',
"output": '/test/test',
"overwrite": True
}
)
@mock.patch('os.path.abspath', autospect=True)
@mock.patch('os.path.exists', autospect=True)
def test_overcloud_network_extract_no_overwrite(self, mock_abspath,
mock_path_exists):
mock_abspath.return_value = '/test/test'
mock_path_exists.return_value = True
arglist = ['--stack', 'overcloud', '--output', 'test']
parsed_args = self.check_parser(self.cmd, arglist, [])
self.assertRaises(osc_lib_exc.CommandError,
self.cmd.take_action, parsed_args)

View File

@ -0,0 +1,78 @@
# Copyright 2020 Red Hat, Inc.
#
# 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 logging
import os
from osc_lib import exceptions as oscexc
from osc_lib.i18n import _
from osc_lib import utils
from tripleoclient import command
from tripleoclient import constants
from tripleoclient import utils as oooutils
class OvercloudNetworkExtract(command.Command):
log = logging.getLogger(__name__ + ".OvercloudNetworkExtract")
def get_parser(self, prog_name):
parser = super(OvercloudNetworkExtract, self).get_parser(prog_name)
parser.add_argument('--stack', dest='stack', required=True,
help=_('Name or ID of heat stack '
'(default=Env: OVERCLOUD_STACK_NAME)'),
default=utils.env('OVERCLOUD_STACK_NAME',
default='overcloud'))
parser.add_argument('-o', '--output', required=True,
metavar='<network_deployment.yaml>',
help=_('The output file path describing the '
'network deployment'))
parser.add_argument('-y', '--yes', default=False, action='store_true',
help=_('Skip yes/no prompt for existing files '
'(assume yes).'))
return parser
def take_action(self, parsed_args):
self.log.debug("take_action(%s)" % parsed_args)
output_path = os.path.abspath(parsed_args.output)
overwrite = parsed_args.yes
if (os.path.exists(output_path) and not overwrite
and not oooutils.prompt_user_for_confirmation(
'Overwrite existing file %s [y/N]?' % parsed_args.output,
self.log)):
raise oscexc.CommandError("Will not overwrite existing file:"
" %s" % parsed_args.output)
else:
overwrite = True
extra_vars = {
"stack_name": parsed_args.stack,
"output": output_path,
"overwrite": overwrite
}
with oooutils.TempDirs() as tmp:
oooutils.run_ansible_playbook(
playbook='cli-overcloud-network-extract.yaml',
inventory='localhost,',
workdir=tmp,
playbook_dir=constants.ANSIBLE_TRIPLEO_PLAYBOOKS,
verbosity=oooutils.playbook_verbosity(self=self),
extra_vars=extra_vars,
)