From a7f0d49e961eb432c87b946205c82b37304f002c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paco=20G=C3=B3mez?= Date: Fri, 26 Jan 2018 10:32:18 -0600 Subject: [PATCH] VCDA-415: validate settings before creating template (#65) - validates settings before creating template - updated requirements version - added format-code.sh script - added travis support Signed-off-by: Paco Gomez --- .travis.yml | 14 ++++++++++++ container_service_extension/broker.py | 32 +++++++++------------------ container_service_extension/config.py | 27 +++++++++++++--------- container_service_extension/cse.py | 5 ++--- format-code.sh | 8 +++++++ requirements.txt | 10 ++++----- test-requirements.txt | 2 -- tests/cluster.py | 11 ++------- 8 files changed, 57 insertions(+), 52 deletions(-) create mode 100644 .travis.yml create mode 100755 format-code.sh diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 000000000..d2f0aa3e8 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,14 @@ +language: python + +python: + - '3.6' + +install: + - pip install -r requirements.txt + - python setup.py install + - pip install tox + +script: + - pip list --format=columns | grep container-service-extension + - python -c 'import pkg_resources; print(pkg_resources.require("pyvcloud")[0].version)' + - tox -e flake8 diff --git a/container_service_extension/broker.py b/container_service_extension/broker.py index b0a762cd8..7caf18263 100644 --- a/container_service_extension/broker.py +++ b/container_service_extension/broker.py @@ -104,28 +104,16 @@ SAMPLE_CONFIG = { 'broker': { - 'type': - 'default', - 'org': - 'Admin', - 'vdc': - 'Catalog', - 'catalog': - 'cse', - 'network': - 'admin_network', - 'ip_allocation_mode': - 'pool', - 'storage_profile': - '*', - 'default_template': - SAMPLE_TEMPLATE_PHOTON_V2['name'], - 'templates': [ - SAMPLE_TEMPLATE_PHOTON_V2, - SAMPLE_TEMPLATE_UBUNTU_16_04 - ], - 'cse_msg_dir': - '/tmp/cse' + 'type': 'default', + 'org': 'Admin', + 'vdc': 'Catalog', + 'catalog': 'cse', + 'network': 'admin_network', + 'ip_allocation_mode': 'pool', + 'storage_profile': '*', + 'default_template': SAMPLE_TEMPLATE_PHOTON_V2['name'], + 'templates': [SAMPLE_TEMPLATE_PHOTON_V2, SAMPLE_TEMPLATE_UBUNTU_16_04], + 'cse_msg_dir': '/tmp/cse' } } diff --git a/container_service_extension/config.py b/container_service_extension/config.py index ed66d2bd0..086c13478 100644 --- a/container_service_extension/config.py +++ b/container_service_extension/config.py @@ -115,7 +115,7 @@ def get_config(config_file_name): return config -def check_config(config_file_name, template='*'): +def check_config(config_file_name, template=None): click.secho('Validating CSE on vCD from file: %s' % config_file_name) if sys.version_info.major >= 3 and sys.version_info.minor >= 6: python_valid = True @@ -160,10 +160,6 @@ def check_config(config_file_name, template='*'): 'administrator (%s:%s): %s' % (config['vcd']['host'], config['vcd']['port'], bool_to_msg(True))) - click.secho('Validating \'%s\' service broker' % config['broker']['type']) - if config['broker']['type'] == 'default': - validate_broker_config_content(config, client, template) - platform = Platform(client) for vc in platform.list_vcenters(): found = False @@ -187,11 +183,20 @@ def check_config(config_file_name, template='*'): vsphere_url.hostname, vsphere_url.port, bool_to_msg(True))) + if template is None: + pass + else: + click.secho( + 'Validating \'%s\' service broker' % config['broker']['type']) + if config['broker']['type'] == 'default': + validate_broker_config_content(config, client, template) + return config def install_cse(ctx, config_file_name, template_name, no_capture, update, amqp_install, ext_install): + check_config(config_file_name) click.secho('Installing CSE on vCD from file: %s, template: %s' % (config_file_name, template_name)) config = get_config(config_file_name) @@ -242,8 +247,7 @@ def install_cse(ctx, config_file_name, template_name, no_capture, update, k8s_template = None try: k8s_template = org.get_catalog_item( - config['broker']['catalog'], - template['catalog_item']) + config['broker']['catalog'], template['catalog_item']) click.echo('Find template \'%s\', \'%s\': %s' % (config['broker']['catalog'], template['catalog_item'], @@ -259,7 +263,8 @@ def install_cse(ctx, config_file_name, template_name, no_capture, update, create_template(ctx, config, client, org, vdc_resource, catalog, no_capture, template) k8s_template = org.get_catalog_item( - config['broker']['catalog'], template['catalog_item']) + config['broker']['catalog'], + template['catalog_item']) if update: click.echo('Updated template \'%s\', \'%s\': %s' % (config['broker']['catalog'], @@ -273,9 +278,9 @@ def install_cse(ctx, config_file_name, template_name, no_capture, update, except Exception: LOGGER.error(traceback.format_exc()) click.echo('Can\'t create or update template \'%s\' ' - '\'%s\': %s' % (template['name'], - config['broker']['catalog'], - template['catalog_item'])) + '\'%s\': %s' % + (template['name'], config['broker']['catalog'], + template['catalog_item'])) configure_amqp_settings(ctx, client, config, amqp_install) register_extension(ctx, client, config, ext_install) click.secho('Start CSE with: \'cse run %s\'' % config_file_name) diff --git a/container_service_extension/cse.py b/container_service_extension/cse.py index 96889439c..5e856ccc3 100755 --- a/container_service_extension/cse.py +++ b/container_service_extension/cse.py @@ -9,7 +9,6 @@ import click from vcd_cli.utils import stdout -from vcd_cli.vcd import abort_if_false from container_service_extension.config import check_config from container_service_extension.config import generate_sample_config @@ -164,8 +163,8 @@ def check(ctx, config, template): default='prompt', type=click.Choice(['prompt', 'skip', 'config']), help='API Extension configuration') -def install(ctx, config, template, update, - no_capture, amqp_install, ext_install): +def install(ctx, config, template, update, no_capture, amqp_install, + ext_install): """Install CSE on vCloud Director.""" try: install_cse(ctx, config, template, no_capture, update, amqp_install, diff --git a/format-code.sh b/format-code.sh new file mode 100755 index 000000000..853f52df4 --- /dev/null +++ b/format-code.sh @@ -0,0 +1,8 @@ +#!/usr/bin/env bash + +yapf -i container_service_extension/*.py +yapf -i container_service_extension/client/*.py +yapf -i tests/*.py +flake8 container_service_extension/*.py +flake8 container_service_extension/client/*.py +flake8 tests/*.py diff --git a/requirements.txt b/requirements.txt index 4dcf2e1b2..65698aec5 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,6 +1,6 @@ -cachetools -humanfriendly -pika >= 0.10.0 +cachetools >= 2.0.1 +humanfriendly >= 4.8 +pika >= 0.11.2 +pyvcloud >= 19.1.1 +vcd-cli >= 20.1.1 vsphere-guest-run >= 0.0.3 -pyvcloud >= 19.1.1.dev16 -vcd-cli >= 20.1.1.dev11 diff --git a/test-requirements.txt b/test-requirements.txt index 571040718..c479d1ce3 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -3,6 +3,4 @@ flake8 flake8-import-order flake8_docstrings hacking -nose>=1.3.6 -nose-testconfig>=0.9.1 yapf diff --git a/tests/cluster.py b/tests/cluster.py index e4a5b4ca5..81987007c 100644 --- a/tests/cluster.py +++ b/tests/cluster.py @@ -2,18 +2,15 @@ # Copyright (c) 2017 VMware, Inc. All Rights Reserved. # SPDX-License-Identifier: BSD-2-Clause -import os import unittest -import yaml -from pyvcloud.vcd.client import Client + from pyvcloud.vcd.client import TaskStatus from pyvcloud.vcd.test import TestCase - from container_service_extension.client.cluster import Cluster -class TestCluster(TestCase): +class TestCluster(TestCase): def test_01_template_list(self): logged_in_org = self.client.get_org() assert self.config['vcd']['org'] == logged_in_org.get('name') @@ -56,9 +53,5 @@ def test_05_cluster_list(self): assert len(clusters) == 0 - - - - if __name__ == '__main__': unittest.main()