Skip to content
This repository has been archived by the owner on Jan 13, 2020. It is now read-only.
/ accelpy Public archive

Commit

Permalink
1.0.0-beta.22
Browse files Browse the repository at this point in the history
  • Loading branch information
JGoutin committed Aug 20, 2019
1 parent 2cfe972 commit 259a034
Show file tree
Hide file tree
Showing 11 changed files with 233 additions and 87 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ install:

script:
# Runs tests
- 'pytest -v -m "not require_csp" --cov=accelpy --cov-report=term-missing'
- 'pytest -s -m "not require_csp" --cov=accelpy --cov-report=term-missing'

after_success:
# Sends coverage to codecov.io
Expand Down
2 changes: 1 addition & 1 deletion accelpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
See the License for the specific language governing permissions and
limitations under the License.
"""
__version__ = '1.0.0-beta.21'
__version__ = '1.0.0-beta.22'
__copyright__ = "Copyright 2019 Accelize"
__licence__ = "Apache 2.0"

Expand Down
46 changes: 20 additions & 26 deletions accelpy/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ def _get_cached_app(prefix, name, after, getter):

# If no cached values, get from web server then cache values
if not values:
values = set_cli_cache(cached, getter(prefix))
values = set_cli_cache(cached, list(getter(prefix)))

# If cached values, filter before return
else:
Expand Down Expand Up @@ -331,35 +331,29 @@ def _application_completer(prefix, parsed_args, **__):
# - Only path should starts with "." or "/"
# - Product ID is in format "vendor/library/name" should not contain more
# than 2 "/"
if (not prefix.startswith('.') and not prefix.startswith('/') and
prefix.count('/') <= 2):
if (prefix.startswith('.') or prefix.startswith('/') or
prefix.count('/') > 2):
return yaml_applications

from accelpy.exceptions import (
AuthenticationException, WebServerException)
# "product_id:version" formatted
if ':' in prefix:
name = 'version'
getter = _get_versions

try:
# "product_id:version" formatted
if ':' in prefix:
name = 'version'
getter = _get_versions

# "product_id" formatted
else:
name = 'product'
getter = _get_product_ids

return _get_cached_app(prefix, name, yaml_applications, getter)

except AuthenticationException as exception:
_completer_warn(
'"--application"/"-a" argument autocompletion require '
f'Accelize authentication: {exception}')
# "product_id" formatted
else:
name = 'product'
getter = _get_product_ids

except WebServerException:
# Skip silently any other Accelize web service error.
pass
# Get from server or cache
from accelpy.exceptions import AuthenticationException
try:
return _get_cached_app(prefix, name, yaml_applications, getter)

return yaml_applications
except AuthenticationException as exception:
_completer_warn(
'"--application"/"-a" argument autocompletion require '
f'Accelize authentication: {exception}')


def _provider_completer(prefix, parsed_args, **_):
Expand Down
8 changes: 4 additions & 4 deletions accelpy/_ansible/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,12 @@ def create_configuration(self, provider=None, application_type=None,

for dep_entry in dependencies:

if isinstance(dep_entry, str):
# Formatted as "- name"
dep = dep_entry
else:
try:
# Formatted as "- role: name"
dep = dep_entry['role']
except TypeError: # pragma: no cover
# May also be Formatted as "- name"
dep = dep_entry

# Local dependencies: To initialize
if dep in roles_local and dep not in initialized_roles:
Expand Down
57 changes: 21 additions & 36 deletions accelpy/_application.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# coding=utf-8
"""Application Definition"""
from copy import deepcopy
from json import dumps
from os import fsdecode
from re import fullmatch

from accelpy._common import accelize_ws_session
from accelpy._yaml import yaml_read, yaml_write
from accelpy.exceptions import ConfigurationException
from accelpy.exceptions import ConfigurationException, RuntimeException

# Application definition format
FORMAT = {
Expand Down Expand Up @@ -90,16 +91,16 @@ class Application:
definition (path-like object or dict):
Path to yaml definition file or dict of the content of the
definition.
configuration_id (int): ID of configuration in Accelize web service.
"""

def __init__(self, definition, configuration_id=None):
def __init__(self, definition):
self._providers = set()
self._configuration_id = configuration_id
self._configuration_id = None

# Load from dict
if isinstance(definition, dict):
self._path = None
definition = deepcopy(definition)

# Load from file
else:
Expand Down Expand Up @@ -136,7 +137,9 @@ def from_id(cls, application):
"""
definition = cls._get_definition(application)
configuration_id = definition['application'].pop('configuration_id')
return cls(definition, configuration_id=configuration_id)
instance = cls(definition)
instance._configuration_id = configuration_id
return instance

@staticmethod
def _get_definition(application):
Expand Down Expand Up @@ -198,39 +201,21 @@ def push(self):
"""
self._configuration_id = accelize_ws_session.request(
'/auth/objects/productconfiguration/',
data=dumps(self._clean_definition),
data=dumps(self.to_dict()),
method='post')['application']['configuration_id']

@classmethod
def delete(cls, application):
def delete(self):
"""
Delete application definition on Accelize web service.
Args:
application (str or int): Application if format "product_id:version"
or "product_id" or configuration ID in Accelize web service.
If specific version of ID not specified, will delete the last
stable version.
"""
if not isinstance(application, int):
# Get configuration ID
application = cls._get_definition(
application)['application']['configuration_id']
if not self._configuration_id:
raise RuntimeException(
'Can only delete an application loaded with "from_id"')

accelize_ws_session.request(
f'/auth/objects/productconfiguration/{application}/',
f'/auth/objects/productconfiguration/{self._configuration_id}/',
method='delete')

@property
def configuration_id(self):
"""
Configuration ID in Accelize web service.
Returns:
int: ID
"""
return self._configuration_id

@property
def providers(self):
"""
Expand Down Expand Up @@ -301,18 +286,16 @@ def save(self, path=None):
Args:
path (path-like object): Path where save Yaml definition file.
"""
yaml_write(self._clean_definition, path or self._path)
yaml_write(self.to_dict(), path or self._path)

@property
def _clean_definition(self):
def to_dict(self):
"""
Return definition cleaned up from empty values.
Return definition as a dictionary.
Returns:
dict: definition
"""
from copy import deepcopy

# Clean up empty and None values
definition = deepcopy(self._definition)
for section_name in tuple(definition):
section = definition[section_name]
Expand All @@ -322,7 +305,9 @@ def _clean_definition(self):
for key in tuple(element):
if not element[key] and element[key] is not False:
del element[key]
if not element:
if not element: # pragma: no cover
# Should never append because currently all list
# sections have at least one mandatory key
section.remove(element)

else:
Expand Down
4 changes: 3 additions & 1 deletion accelpy/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,9 +395,11 @@ def get_cli_cache(name, recursive=False):
# Get cached value, or return None
if recursive:
names = []
while name:
while name and not name.endswith('|'):
names.append(name)
name = name[:-1]
names.append(name)

else:
names = name,

Expand Down
3 changes: 2 additions & 1 deletion accelpy/_hashicorp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ def _get_executable(cls):
exec_version = line.split(' ')[1].strip().lstrip('v')

# If file is installed and up-to-date, returns its path
if exec_version == last_release['current_version']:
current_version = last_release['current_version']
if exec_version == current_version: # pragma: no branch
return exec_file

# Download executables checksum file and associated signature
Expand Down
4 changes: 3 additions & 1 deletion accelpy/_host.py
Original file line number Diff line number Diff line change
Expand Up @@ -500,7 +500,9 @@ def _terraform_has_state(self):
"""
try:
return bool(self._terraform.state_list())
except (AccelizeException, FileNotFoundError):
except (AccelizeException, FileNotFoundError): # pragma: no cover
# Should not append, but avoid crash if file is missing for any
# reason
return False

def _clean_up(self):
Expand Down
8 changes: 6 additions & 2 deletions buildspec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@ phases:
python: 3.7
commands:
- $CODEBUILD_SRC_DIR_toolbox/install.py
- xlz install drmlib_cred_json=~/.accelize accelpy_common_tf accelpy_codecov_token
- xlz install accelpy_codebuild
- export ACCELPY_NO_COLOR=True
- python3 -m pip install -Uq setuptools pip wheel pytest-cov codecov
- python3 -m pip install -qe .

build:
commands:
- ACCELPY_NO_COLOR=True pytest -m require_csp --cov=accelpy --cov-report=term-missing
- pytest -m require_csp --cov=accelpy --cov-report=term-missing

post_build:
commands:
- codecov
Loading

0 comments on commit 259a034

Please sign in to comment.