Merge "Move OvercloudRcAction functionality to utils"
This commit is contained in:
commit
ff01e0b2e0
|
@ -16,14 +16,12 @@ import json
|
|||
import logging
|
||||
import os
|
||||
|
||||
from heatclient import exc as heat_exc
|
||||
from mistral_lib import actions
|
||||
from swiftclient import exceptions as swiftexceptions
|
||||
import six
|
||||
|
||||
from tripleo_common.actions import base
|
||||
from tripleo_common import constants
|
||||
from tripleo_common.utils import overcloudrc
|
||||
from tripleo_common.utils import plan as plan_utils
|
||||
|
||||
LOG = logging.getLogger(__name__)
|
||||
|
||||
|
@ -44,44 +42,14 @@ class OvercloudRcAction(base.TripleOAction):
|
|||
self.no_proxy = no_proxy
|
||||
|
||||
def run(self, context):
|
||||
orchestration_client = self.get_orchestration_client(context)
|
||||
heat = self.get_orchestration_client(context)
|
||||
swift = self.get_object_client(context)
|
||||
|
||||
try:
|
||||
stack = orchestration_client.stacks.get(self.container)
|
||||
except heat_exc.HTTPNotFound:
|
||||
error = (
|
||||
"The Heat stack {} could not be found. Make sure you have "
|
||||
"deployed before calling this action.").format(self.container)
|
||||
return actions.Result(error=error)
|
||||
|
||||
# We need to check parameter_defaults first for a user provided
|
||||
# password. If that doesn't exist, we then should look in the
|
||||
# automatically generated passwords.
|
||||
# TODO(d0ugal): Abstract this operation somewhere. We shouldn't need to
|
||||
# know about the structure of the environment to get a password.
|
||||
try:
|
||||
env = plan_utils.get_env(swift, self.container)
|
||||
except swiftexceptions.ClientException as err:
|
||||
err_msg = ("Error retrieving environment for plan %s: %s" % (
|
||||
self.container, err))
|
||||
LOG.error(err_msg)
|
||||
return actions.Result(error=err_msg)
|
||||
|
||||
try:
|
||||
parameter_defaults = env['parameter_defaults']
|
||||
passwords = env['passwords']
|
||||
admin_pass = parameter_defaults.get('AdminPassword')
|
||||
if admin_pass is None:
|
||||
admin_pass = passwords['AdminPassword']
|
||||
except KeyError:
|
||||
error = ("Unable to find the AdminPassword in the plan "
|
||||
"environment.")
|
||||
return actions.Result(error=error)
|
||||
|
||||
region_name = parameter_defaults.get('KeystoneRegion')
|
||||
return overcloudrc.create_overcloudrc(stack, self.no_proxy, admin_pass,
|
||||
region_name)
|
||||
return overcloudrc.create_overcloudrc(
|
||||
swift, heat, self.container, self.no_proxy)
|
||||
except Exception as err:
|
||||
LOG.exception(six.text_type(err))
|
||||
return actions.Result(six.text_type(err))
|
||||
|
||||
|
||||
class DeploymentFailuresAction(base.TripleOAction):
|
||||
|
|
|
@ -15,99 +15,10 @@
|
|||
import json
|
||||
import mock
|
||||
|
||||
from heatclient import exc as heat_exc
|
||||
from swiftclient import exceptions as swiftexceptions
|
||||
|
||||
from tripleo_common.actions import deployment
|
||||
from tripleo_common.tests import base
|
||||
|
||||
|
||||
class OvercloudRcActionTestCase(base.TestCase):
|
||||
@mock.patch('tripleo_common.actions.base.TripleOAction.'
|
||||
'get_object_client')
|
||||
@mock.patch('tripleo_common.actions.base.TripleOAction.'
|
||||
'get_orchestration_client')
|
||||
def test_no_stack(self, mock_get_orchestration, mock_get_object):
|
||||
|
||||
mock_ctx = mock.MagicMock()
|
||||
|
||||
not_found = heat_exc.HTTPNotFound()
|
||||
mock_get_orchestration.return_value.stacks.get.side_effect = not_found
|
||||
|
||||
action = deployment.OvercloudRcAction("overcast")
|
||||
result = action.run(mock_ctx)
|
||||
|
||||
self.assertEqual(result.error, (
|
||||
"The Heat stack overcast could not be found. Make sure you have "
|
||||
"deployed before calling this action."
|
||||
))
|
||||
|
||||
@mock.patch('tripleo_common.actions.base.TripleOAction.'
|
||||
'get_object_client')
|
||||
@mock.patch('tripleo_common.actions.base.TripleOAction.'
|
||||
'get_orchestration_client')
|
||||
def test_no_env(self, mock_get_orchestration, mock_get_object):
|
||||
|
||||
mock_ctx = mock.MagicMock()
|
||||
|
||||
mock_get_object.return_value.get_object.side_effect = (
|
||||
swiftexceptions.ClientException("overcast"))
|
||||
|
||||
action = deployment.OvercloudRcAction("overcast")
|
||||
result = action.run(mock_ctx)
|
||||
self.assertEqual(result.error, "Error retrieving environment for plan "
|
||||
"overcast: overcast")
|
||||
|
||||
@mock.patch('tripleo_common.actions.base.TripleOAction.'
|
||||
'get_object_client')
|
||||
@mock.patch('tripleo_common.actions.base.TripleOAction.'
|
||||
'get_orchestration_client')
|
||||
def test_no_password(self, mock_get_orchestration, mock_get_object):
|
||||
mock_ctx = mock.MagicMock()
|
||||
|
||||
mock_get_object.return_value.get_object.return_value = (
|
||||
{}, "version: 1.0")
|
||||
|
||||
action = deployment.OvercloudRcAction("overcast")
|
||||
result = action.run(mock_ctx)
|
||||
|
||||
self.assertEqual(
|
||||
result.error,
|
||||
"Unable to find the AdminPassword in the plan environment.")
|
||||
|
||||
@mock.patch('tripleo_common.actions.base.TripleOAction.'
|
||||
'get_object_client')
|
||||
@mock.patch('tripleo_common.utils.overcloudrc.create_overcloudrc')
|
||||
@mock.patch('tripleo_common.actions.base.TripleOAction.'
|
||||
'get_orchestration_client')
|
||||
def test_success(self, mock_get_orchestration, mock_create_overcloudrc,
|
||||
mock_get_object):
|
||||
mock_ctx = mock.MagicMock()
|
||||
|
||||
mock_env = """
|
||||
version: 1.0
|
||||
|
||||
template: overcloud.yaml
|
||||
environments:
|
||||
- path: overcloud-resource-registry-puppet.yaml
|
||||
- path: environments/services/sahara.yaml
|
||||
parameter_defaults:
|
||||
BlockStorageCount: 42
|
||||
OvercloudControlFlavor: yummy
|
||||
passwords:
|
||||
AdminPassword: SUPERSECUREPASSWORD
|
||||
"""
|
||||
mock_get_object.return_value.get_object.return_value = ({}, mock_env)
|
||||
mock_create_overcloudrc.return_value = {
|
||||
"overcloudrc": "fake overcloudrc"
|
||||
}
|
||||
|
||||
action = deployment.OvercloudRcAction("overcast")
|
||||
result = action.run(mock_ctx)
|
||||
|
||||
self.assertEqual(result, {"overcloudrc": "fake overcloudrc"})
|
||||
|
||||
|
||||
class DeploymentFailuresActionTest(base.TestCase):
|
||||
|
||||
def setUp(self):
|
||||
|
|
|
@ -15,6 +15,10 @@
|
|||
|
||||
import mock
|
||||
|
||||
from heatclient import exc as heat_exc
|
||||
import six
|
||||
from swiftclient import exceptions as swiftexceptions
|
||||
|
||||
from tripleo_common.tests import base
|
||||
from tripleo_common.utils import overcloudrc
|
||||
|
||||
|
@ -34,8 +38,8 @@ class OvercloudRcTest(base.TestCase):
|
|||
]
|
||||
}
|
||||
|
||||
result = overcloudrc.create_overcloudrc(stack, "foo", "AdminPassword",
|
||||
"regionTwo")
|
||||
result = overcloudrc._create_overcloudrc(stack, "foo", "AdminPassword",
|
||||
"regionTwo")
|
||||
|
||||
self.assertIn("export no_proxy='[fd00::1],foo,foo.com'",
|
||||
result['overcloudrc'])
|
||||
|
@ -64,7 +68,7 @@ class OvercloudRcTest(base.TestCase):
|
|||
]
|
||||
}
|
||||
|
||||
result = overcloudrc.create_overcloudrc(
|
||||
result = overcloudrc._create_overcloudrc(
|
||||
stack, "foo,foo.com", "AdminPassword", "regionTwo")
|
||||
|
||||
self.assertIn("export no_proxy='[fd00::1],foo,foo.com'",
|
||||
|
@ -80,3 +84,69 @@ class OvercloudRcTest(base.TestCase):
|
|||
result['overcloudrc'])
|
||||
self.assertIn("OS_REGION_NAME=regionTwo",
|
||||
result['overcloudrc'])
|
||||
|
||||
def test_overcloudrc_no_stack(self):
|
||||
mock_swift = mock.MagicMock()
|
||||
mock_heat = mock.MagicMock()
|
||||
not_found = heat_exc.HTTPNotFound()
|
||||
mock_heat.stacks.get.side_effect = not_found
|
||||
ex = self.assertRaises(RuntimeError,
|
||||
overcloudrc.create_overcloudrc,
|
||||
mock_swift, mock_heat, "overcast")
|
||||
|
||||
self.assertEqual((
|
||||
"The Heat stack overcast could not be found. Make sure you have "
|
||||
"deployed before calling this action."
|
||||
), six.text_type(ex))
|
||||
|
||||
def test_overcloudrc_no_env(self):
|
||||
mock_swift = mock.MagicMock()
|
||||
mock_heat = mock.MagicMock()
|
||||
mock_swift.get_object.side_effect = (
|
||||
swiftexceptions.ClientException("overcast"))
|
||||
ex = self.assertRaises(RuntimeError,
|
||||
overcloudrc.create_overcloudrc,
|
||||
mock_swift, mock_heat, "overcast")
|
||||
|
||||
self.assertEqual("Error retrieving environment for plan overcast: "
|
||||
"overcast", six.text_type(ex))
|
||||
|
||||
def test_overcloudrc_no_password(self):
|
||||
mock_swift = mock.MagicMock()
|
||||
mock_heat = mock.MagicMock()
|
||||
mock_swift.get_object.return_value = (
|
||||
{}, "version: 1.0")
|
||||
ex = self.assertRaises(RuntimeError,
|
||||
overcloudrc.create_overcloudrc,
|
||||
mock_swift, mock_heat, "overcast")
|
||||
|
||||
self.assertEqual(
|
||||
"Unable to find the AdminPassword in the plan environment.",
|
||||
six.text_type(ex))
|
||||
|
||||
@mock.patch('tripleo_common.utils.overcloudrc._create_overcloudrc')
|
||||
def test_success(self, mock_create_overcloudrc):
|
||||
|
||||
mock_env = """
|
||||
version: 1.0
|
||||
|
||||
template: overcloud.yaml
|
||||
environments:
|
||||
- path: overcloud-resource-registry-puppet.yaml
|
||||
- path: environments/services/sahara.yaml
|
||||
parameter_defaults:
|
||||
BlockStorageCount: 42
|
||||
OvercloudControlFlavor: yummy
|
||||
passwords:
|
||||
AdminPassword: SUPERSECUREPASSWORD
|
||||
"""
|
||||
mock_swift = mock.MagicMock()
|
||||
mock_heat = mock.MagicMock()
|
||||
mock_swift.get_object.return_value = ({}, mock_env)
|
||||
mock_create_overcloudrc.return_value = {
|
||||
"overcloudrc": "fake overcloudrc"
|
||||
}
|
||||
|
||||
result = overcloudrc.create_overcloudrc(
|
||||
mock_swift, mock_heat, "overcast")
|
||||
self.assertEqual(result, {"overcloudrc": "fake overcloudrc"})
|
||||
|
|
|
@ -12,16 +12,23 @@
|
|||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import logging
|
||||
|
||||
from heatclient import exc as heat_exc
|
||||
from six.moves import urllib
|
||||
from swiftclient import exceptions as swiftexceptions
|
||||
|
||||
from tripleo_common import constants
|
||||
from tripleo_common.utils import common as common_utils
|
||||
from tripleo_common.utils import plan as plan_utils
|
||||
|
||||
try: # py3
|
||||
from shlex import quote
|
||||
except ImportError: # py2
|
||||
from pipes import quote
|
||||
|
||||
LOG = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def get_service_ips(stack):
|
||||
service_ips = {}
|
||||
|
@ -66,7 +73,47 @@ fi
|
|||
"""
|
||||
|
||||
|
||||
def create_overcloudrc(stack, no_proxy, admin_password, region_name):
|
||||
def create_overcloudrc(swift, heat,
|
||||
container=constants.DEFAULT_CONTAINER_NAME,
|
||||
no_proxy=""):
|
||||
try:
|
||||
stack = heat.stacks.get(container)
|
||||
except heat_exc.HTTPNotFound:
|
||||
error = (
|
||||
"The Heat stack {} could not be found. Make sure you have "
|
||||
"deployed before calling this action.").format(container)
|
||||
raise RuntimeError(error)
|
||||
|
||||
# We need to check parameter_defaults first for a user provided
|
||||
# password. If that doesn't exist, we then should look in the
|
||||
# automatically generated passwords.
|
||||
# TODO(d0ugal): Abstract this operation somewhere. We shouldn't need to
|
||||
# know about the structure of the environment to get a password.
|
||||
try:
|
||||
env = plan_utils.get_env(swift, container)
|
||||
except swiftexceptions.ClientException as err:
|
||||
err_msg = ("Error retrieving environment for plan %s: %s" % (
|
||||
container, err))
|
||||
LOG.error(err_msg)
|
||||
raise RuntimeError(err_msg)
|
||||
|
||||
try:
|
||||
parameter_defaults = env['parameter_defaults']
|
||||
passwords = env['passwords']
|
||||
admin_pass = parameter_defaults.get('AdminPassword')
|
||||
if admin_pass is None:
|
||||
admin_pass = passwords['AdminPassword']
|
||||
except KeyError:
|
||||
error = ("Unable to find the AdminPassword in the plan "
|
||||
"environment.")
|
||||
raise RuntimeError(error)
|
||||
|
||||
region_name = parameter_defaults.get('KeystoneRegion')
|
||||
return _create_overcloudrc(stack, no_proxy,
|
||||
admin_pass, region_name)
|
||||
|
||||
|
||||
def _create_overcloudrc(stack, no_proxy, admin_password, region_name):
|
||||
"""Given the stack and proxy settings, create the overcloudrc
|
||||
|
||||
stack: Heat stack containing the deployed overcloud
|
||||
|
|
Loading…
Reference in New Issue