Skip to content

Commit

Permalink
Merge branch 'feature/aut-q4-fixes' into 'develop'
Browse files Browse the repository at this point in the history
Feature/aut q4 fixes

See merge request core/sevenbridges-python!54
  • Loading branch information
borislavd88 committed Dec 6, 2019
2 parents c618611 + bc470df commit 5538268
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 32 deletions.
29 changes: 28 additions & 1 deletion docs/quickstart.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1629,7 +1629,10 @@ The following operations are supported for automations:
- ``remove_member()`` - Remove member from an automation
- ``get_runs()`` - Get automation runs

The following operations are supported for automation package:
The following operations are supported for automation packages:
- ``query()`` - Query all automation packages
- ``get()`` - Get automation package with the provided id
- ``create()`` - Create automation package
- ``archive()`` - Archive an package
- ``restore()`` - Restore an archived package

Expand Down Expand Up @@ -1666,6 +1669,24 @@ Each automation has the following properties:

``modified_on`` - Date of the last modification of the automation.

Each automation package has the following properties:

``id`` - Automation package identifier.

``automation`` - Identifier of the automation the package belongs to.

``version`` - Automation package version.

``location`` - Location identifier of uploaded automation package.

``created_by`` - Username of the user that created the automation.

``created_on`` - Date of the automation package creation.

``archived`` - Flag indicating wheather automation package is arhived or not.

``custom_url`` - Link to custom frontend page.

Each automation run has the following properties:

``href`` - The URL of the automation run on the API server.
Expand Down Expand Up @@ -1710,6 +1731,12 @@ Examples
# Get details of an automation
automation = api.automations.get('automation_id')
# Create automation package from uploaded code package
automation_package = api.automation_packages.create('automation_id', 'version', 'location_id'):
# Get automation package details
automation_package = api.automation_packages.get('package_id')
# Get automation runs with name parameter for existing automation
automation.get_runs(name='automation_run_name')
Expand Down
4 changes: 3 additions & 1 deletion sevenbridges/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
from sevenbridges.models.storage_export import Export
from sevenbridges.models.storage_import import Import
from sevenbridges.models.billing_group import BillingGroup
from sevenbridges.models.automation import Automation, AutomationRun
from sevenbridges.models.automation import Automation, AutomationRun, \
AutomationPackage


class Api(HttpClient):
Expand All @@ -32,6 +33,7 @@ class Api(HttpClient):
async_jobs = AsyncJob
automations = Automation
automation_runs = AutomationRun
automation_packages = AutomationPackage
billing_groups = BillingGroup
datasets = Dataset
divisions = Division
Expand Down
15 changes: 11 additions & 4 deletions sevenbridges/meta/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,7 @@ def __set__(self, instance, value):
def __get__(self, instance, cls):
try:
data = instance._data[self.name]
if data and isinstance(self, DateTimeField):
return datetime.strptime(data, '%Y-%m-%dT%H:%M:%SZ')
else:
return data
return data
except (KeyError, AttributeError):
return None

Expand Down Expand Up @@ -153,6 +150,16 @@ class DateTimeField(Field):
def __init__(self, name=None, read_only=True):
super(DateTimeField, self).__init__(name=name, read_only=read_only)

def __get__(self, instance, cls):
data = super(DateTimeField, self).__get__(instance, cls)
if data:
format = "%Y-%m-%dT%H:%M:%S"
if '.' in data:
format += ".%f"
if 'Z' in data:
format += "Z"
return datetime.strptime(data, format)


class BooleanField(Field):
def __init__(self, name=None, read_only=False):
Expand Down
59 changes: 34 additions & 25 deletions sevenbridges/models/automation.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,21 @@ class AutomationPackage(Resource):
"""
_URL = {
'query': '/automation/automations/{automation_id}/packages',
'get': '/automation/packages/{package_id}',
'get': '/automation/packages/{id}',
'archive': "/automation/automations/{automation_id}"
"/packages/{package_id}/actions/archive",
"/packages/{id}/actions/archive",
'restore': "/automation/automations/{automation_id}"
"/packages/{package_id}/actions/restore",
"/packages/{id}/actions/restore",
}

id = StringField(read_only=True)
automation = UuidField(read_only=True)
version = StringField(read_only=True)
location = StringField(read_only=True)
created_by = StringField(read_only=True)
created_on = StringField(read_only=True)
created_on = DateTimeField(read_only=True)
archived = BooleanField(read_only=True)
custom_url = StringField()

def __eq__(self, other):
if not hasattr(other, '__class__'):
Expand Down Expand Up @@ -116,22 +117,6 @@ def create(cls, automation, version, location, api=None):
)
return AutomationPackage(api=api, **package_data)

# noinspection PyMethodOverriding
@classmethod
def get(cls, package_id, api=None):
"""
Fetches the resource from the server.
:param package_id: Automation package id
:param api: sevenbridges Api instance.
:return: AutomationPackage object.
"""

api = api or cls._API
code_package = api.get(url=cls._URL['get'].format(
package_id=package_id,
)).json()
return AutomationPackage(api=api, **code_package)

@inplace_reload
def archive(self):
"""
Expand All @@ -150,7 +135,7 @@ def archive(self):

package_data = self._api.post(
url=self._URL['archive'].format(
automation_id=automation_id, package_id=self.id
automation_id=automation_id, id=self.id
)
).json()
return AutomationPackage(api=self._api, **package_data)
Expand All @@ -172,11 +157,35 @@ def restore(self):

package_data = self._api.post(
url=self._URL['restore'].format(
automation_id=automation_id, package_id=self.id
automation_id=automation_id, id=self.id
)
).json()
return AutomationPackage(api=self._api, **package_data)

@inplace_reload
def save(self, inplace=True):
"""
Saves all modification to the automation package on the server.
:param inplace Apply edits on the current instance or get a new one.
:return: AutomationPackage instance.
"""
modified_data = self._modified_data()
if bool(modified_data):
extra = {
'resource': self.__class__.__name__,
'query': {
'id': self.id,
'modified_data': modified_data
}
}
logger.info('Saving automation package', extra=extra)
data = self._api.patch(url=self._URL['get'].format(id=self.id),
data=modified_data).json()
return AutomationPackage(api=self._api, **data)

else:
raise ResourceNotModified()


class AutomationMember(Resource):
"""
Expand Down Expand Up @@ -331,9 +340,9 @@ class Automation(Resource):
billing_group = UuidField(read_only=False)
owner = StringField(read_only=True)
created_by = StringField(read_only=True)
created_on = StringField(read_only=True)
created_on = DateTimeField(read_only=True)
modified_by = StringField(read_only=True)
modified_on = StringField(read_only=False)
modified_on = DateTimeField(read_only=False)
archived = BooleanField(read_only=True)
secret_settings = DictField(read_only=False)

Expand Down Expand Up @@ -501,7 +510,7 @@ def get_package(cls, package, api=None):
package_id = Transform.to_automation_package(package)
api = api or cls._API
return AutomationPackage.get(
package_id=package_id, api=api
id=package_id, api=api
)

def add_package(self, version, location, api=None):
Expand Down
11 changes: 10 additions & 1 deletion sevenbridges/tests/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1531,7 +1531,8 @@ def default_automation_package(
'location': location,
'created_by': generator.user_name(),
'created_on': generator.date(),
'archived': False
'archived': False,
'custom_url': generator.url()
}

def exists(self, **kwargs):
Expand Down Expand Up @@ -1571,6 +1572,14 @@ def can_be_restored(self, **kwargs):
json=package
)

def can_be_saved(self, **kwargs):
package = self.default_automation_package()
package.update(**kwargs)
id = package['id']
self.request_mocker.patch(
'/automation/packages/{id}'.format(id=id), json=package
)


class AutomationMemberProvider(object):

Expand Down
23 changes: 23 additions & 0 deletions sevenbridges/tests/test_automations.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,29 @@ def test_archive_package(api, given, verifier):
)


def test_modify_package(api, given, verifier):
# preconditions
automation_id = generator.uuid4()
package_id = generator.uuid4()
new_custom_url = generator.url()
given.automation_packages.exists(
id=package_id, automation=automation_id
)
given.automation_packages.can_be_saved(
id=package_id, automation=automation_id, custom_url=new_custom_url
)

# action
package = api.automation_packages.get(package_id)
package.custom_url = new_custom_url
package.save()

assert package.custom_url == new_custom_url

# verification
verifier.automation_packages.saved(id=package_id)


def test_restore_package(api, given, verifier):
# preconditions
automation_id = generator.uuid4()
Expand Down
3 changes: 3 additions & 0 deletions sevenbridges/tests/verifiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,9 @@ def action_restore_performed(self, automation_id, package_id):
)
)

def saved(self, id):
self.checker.check_url('/automation/packages/{id}'.format(id=id))


class AutomationRunVerifier(object):
def __init__(self, request_mocker):
Expand Down

0 comments on commit 5538268

Please sign in to comment.