Implement download Plan for the OpenStack client

Change-Id: I79cba08518d85d423dd023a5918fe6389a6e4ddf
This commit is contained in:
Dougal Matthews 2015-04-30 17:59:51 +01:00
parent 622802052e
commit af2597d124
2 changed files with 70 additions and 3 deletions

View File

@ -13,6 +13,8 @@
from __future__ import print_function
import logging
import os
import shutil
import sys
from cliff import command
@ -263,13 +265,58 @@ class RemoveManagementPlanRole(show.ShowOne):
class DownloadManagementPlan(command.Command):
"""Download the a Management Plan."""
"""Download a Management Plan."""
log = logging.getLogger(__name__ + '.DownloadManagementPlan')
def get_parser(self, prog_name):
parser = super(DownloadManagementPlan, self).get_parser(prog_name)
parser.add_argument(
'plan_uuid',
help="The UUID of the plan to download."
)
parser.add_argument(
'-O', '--output-dir', metavar='<OUTPUT DIR>',
required=True,
help=('Directory to write template files into. It will be created '
'if it does not exist and any existing files in the '
'directory will be removed.')
)
return parser
def take_action(self, parsed_args):
self.log.debug("take_action(%s)" % parsed_args)
client = self.app.client_manager.management
output_dir = parsed_args.output_dir
if os.path.isdir(output_dir):
shutil.rmtree(output_dir)
os.mkdir(output_dir)
# retrieve templates
templates = client.plans.templates(parsed_args.plan_uuid)
# write file for each key-value in templates
print("The following templates will be written:")
for template_name, template_content in templates.items():
# It's possible to organize the role templates and their dependent
# files into directories, in which case the template_name will
# carry the directory information. If that's the case, first
# create the directory structure (if it hasn't already been
# created by another file in the templates list).
template_dir = os.path.dirname(template_name)
output_template_dir = os.path.join(output_dir, template_dir)
if template_dir and not os.path.exists(output_template_dir):
os.makedirs(output_template_dir)
filename = os.path.join(output_dir, template_name)
with open(filename, 'w+') as template_file:
template_file.write(template_content)
print(filename)

View File

@ -10,6 +10,9 @@
# License for the specific language governing permissions and limitations
# under the License.
import os
import tempfile
from tuskarclient.osc.v2 import plan
from tuskarclient.tests.osc.v2 import fakes
@ -312,9 +315,26 @@ class TestDownloadManagementPlan(TestPlans):
self.cmd = plan.DownloadManagementPlan(self.app, None)
def test_download_plan_templates(self):
arglist = []
verifylist = []
temp_dir = tempfile.mkdtemp()
arglist = ['UUID1', '-O', temp_dir]
verifylist = [
('plan_uuid', 'UUID1'),
('output_dir', temp_dir),
]
mock_result = {
'template-1-name': 'template 1 content',
'template-2-name': 'template 2 content',
}
self.management_mock.plans.templates.return_value = mock_result
parsed_args = self.check_parser(self.cmd, arglist, verifylist)
self.cmd.take_action(parsed_args)
for template_name in mock_result:
full_path = os.path.join(temp_dir, template_name)
self.assertTrue(os.path.exists(full_path))