Skip to content

Commit

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

See merge request core/sevenbridges-python!102
  • Loading branch information
borislavd88 committed Nov 2, 2022
2 parents fb98173 + f479281 commit 2ad4478
Show file tree
Hide file tree
Showing 5 changed files with 245 additions and 10 deletions.
2 changes: 2 additions & 0 deletions docs/quickstart.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1804,6 +1804,8 @@ Each automation package has the following properties:

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

``python`` - Python version to run package with.

Each automation run has the following properties:

``href`` - The URL of the automation run on the API server.
Expand Down
2 changes: 1 addition & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
-r requirements.txt

flake8==3.9.2
flake8==4.0.0
pytest==6.2.4
pytest-cov==2.11.1
requests-mock==1.8.0
Expand Down
79 changes: 77 additions & 2 deletions sevenbridges/models/automation.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class AutomationPackage(Resource):
archived = BooleanField(read_only=True)
custom_url = StringField(read_only=False)
memory_limit = IntegerField(read_only=False)
python = StringField(read_only=True)

def __eq__(self, other):
if type(other) is not type(self):
Expand Down Expand Up @@ -76,13 +77,15 @@ def query(cls, automation, offset=None, limit=None, api=None):

@classmethod
def create(cls, automation, version, location, schema, memory_limit=None,
api=None):
python=None, api=None):
"""
Create a code package.
:param automation: Automation id.
:param version: File ID of the uploaded code package.
:param location: The code package version.
:param schema: IO schema for main step of execution.
:param python: Version of Python to execute Code Package with. Allowed
values are '3.6', '3.7', '3.8'. Default is '3.6'.
:param memory_limit: Memory limit in MB.
:param api: Api instance.
:return:
Expand All @@ -105,6 +108,7 @@ def create(cls, automation, version, location, schema, memory_limit=None,
'location': location,
'schema': schema,
'memory_limit': memory_limit,
'python': python
}

extra = {
Expand Down Expand Up @@ -284,6 +288,34 @@ def add(cls, user, permissions, automation, api=None):
).json()
return AutomationMember(api=api, **member_data)

@classmethod
def add_team(cls, team, permissions, automation, api=None):
"""
Add a team as a member to the automation.
:param team: Team object or team id
:param permissions: Permissions dictionary.
:param automation: Automation object or id
:param api: sevenbridges Api instance
:return: Automation member object.
"""
team = Transform.to_team(team)
automation = Transform.to_automation(automation)

api = api or cls._API
data = {'id': team,
'type': 'team'}

if isinstance(permissions, dict):
data.update({
'permissions': permissions
})

member_data = api.post(
url=cls._URL['query'].format(automation_id=automation),
data=data
).json()
return AutomationMember(api=api, **member_data)

@classmethod
def remove(cls, user, automation, api=None):
"""
Expand All @@ -301,6 +333,23 @@ def remove(cls, user, automation, api=None):
cls._URL['get'].format(automation_id=automation, id=user)
)

@classmethod
def remove_team(cls, team, automation, api=None):
"""
Remove a team member from the automation.
:param team: Team object or team id
:param automation: Automation id
:param api: sevenbridges Api instance
:return: None
"""
team = Transform.to_team(team)
automation = Transform.to_automation(automation)

api = api or cls._API
api.delete(
cls._URL['get'].format(automation_id=automation, id=team)
)

@inplace_reload
def save(self, inplace=True):
"""
Expand Down Expand Up @@ -519,7 +568,7 @@ def get_package(cls, package, api=None):

def add_package(
self, version, file_path, schema, file_name=None,
retry_count=RequestParameters.DEFAULT_RETRY_COUNT,
python=None, retry_count=RequestParameters.DEFAULT_RETRY_COUNT,
timeout=RequestParameters.DEFAULT_TIMEOUT, part_size=None,
api=None
):
Expand All @@ -530,6 +579,8 @@ def add_package(
:param schema: IO schema for main step of execution.
:param part_size: Size of upload part in bytes.
:param file_name: Optional file name.
:param python: Version of Python to execute Code Package with. Allowed
values are '3.6', '3.7', '3.8'. Default is '3.6'.
:param retry_count: Upload retry count.
:param timeout: Timeout for s3/google session.
:param api: sevenbridges Api instance.
Expand Down Expand Up @@ -562,6 +613,7 @@ def add_package(
version=version,
location=package_file.id,
schema=schema,
python=python,
api=api
)

Expand Down Expand Up @@ -614,6 +666,29 @@ def remove_member(self, user, api=None):
api = api or self._API
AutomationMember.remove(automation=self.id, user=user, api=api)

def add_team_member(self, team, permissions, api=None):
"""
Add team member to the automation
:param team: Team object or team id
:param permissions: Member permissions
:param api: sevenbridges Api instance
:return: AutomationMember object
"""
api = api or self._API
return AutomationMember.add_team(
automation=self.id, team=team, permissions=permissions, api=api
)

def remove_team_member(self, team, api=None):
"""
Remove a team member from the automation
:param team: Team object or team id
:param api: sevenbridges Api instance
:return: None
"""
api = api or self._API
AutomationMember.remove_team(automation=self.id, team=team, api=api)

def get_runs(self, package=None, status=None, name=None,
created_by=None, created_from=None, created_to=None,
project_id=None, order_by=None, order=None, offset=None,
Expand Down
64 changes: 60 additions & 4 deletions tests/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1390,6 +1390,15 @@ def has_member(self, id, username):
)
self.request_mocker.get(href, json=member)

def has_team_member(self, id, team_id):
member = self.member_provider.default_automation_team_member(
team_id=team_id,
)
href = (
f'{self.base_url}/automation/automations/{id}/members/{team_id}'
)
self.request_mocker.get(href, json=member)

def has_members(self, id, total):
items = [
self.member_provider.default_automation_member()
Expand All @@ -1412,12 +1421,25 @@ def can_add_member(self, id, username):
href = f'{self.base_url}/automation/automations/{id}/members'
self.request_mocker.post(href, json=member)

def can_add_team_member(self, id, team_id):
member = self.member_provider.default_automation_team_member(
id, team_id
)
href = f'{self.base_url}/automation/automations/{id}/members'
self.request_mocker.post(href, json=member)

def can_remove_member(self, id, username):
href = (
f'{self.base_url}/automation/automations/{id}/members/{username}'
)
self.request_mocker.delete(href)

def can_remove_team_member(self, id, team_id):
href = (
f'{self.base_url}/automation/automations/{id}/members/{team_id}'
)
self.request_mocker.delete(href)

def has_package(self, package_id):
package = self.package_provider.default_automation_package(
package_id=package_id
Expand All @@ -1426,10 +1448,10 @@ def has_package(self, package_id):
self.request_mocker.get(href, json=package)

def can_add_package(self, automation_id, package_id,
location, version, schema):
location, version, schema, python):
package = self.package_provider.default_automation_package(
package_id=package_id, location=location, version=version,
schema=schema
schema=schema, python=python
)
href = (
f'{self.base_url}/automation/automations/{automation_id}/packages'
Expand Down Expand Up @@ -1475,7 +1497,8 @@ def __init__(self, request_mocker, base_url):

@staticmethod
def default_automation_package(
package_id=None, version=None, location=None, schema=None
package_id=None, version=None, location=None, schema=None,
python=None
):
package_id = package_id or generator.uuid4()
version = version or generator.slug()
Expand All @@ -1491,7 +1514,8 @@ def default_automation_package(
'created_by': generator.user_name(),
'created_on': generator.date(),
'archived': False,
'custom_url': generator.url()
'custom_url': generator.url(),
'python': python
}

def exists(self, **kwargs):
Expand All @@ -1503,6 +1527,16 @@ def exists(self, **kwargs):
json=package
)

def can_be_created(self, **kwargs):
package = self.default_automation_package()
automation_id = package['automation']
[package.pop(key) for key in ['id']]
package.update(**kwargs)
self.request_mocker.request(
'POST', f'/automation/automations/{automation_id}/packages',
json=package
)

def can_be_archived(self, **kwargs):
package = self.default_automation_package()
package['archived'] = True
Expand Down Expand Up @@ -1563,6 +1597,28 @@ def default_automation_member(self, automation=None, username=None,
'permissions': permissions,
}

def default_automation_team_member(self, automation=None, team_id=None,
permissions=None):
team_id = team_id or generator.generator.uuid4()
automation = automation or generator.uuid4()
permissions = permissions or {
'admin': False,
'copy': True,
'read': True,
'write': True,
'execute': False,
}
url = (
f'{self.base_url}/automation/automations/{automation}'
f'/members/{team_id}'
)
return {
'href': url,
'id': team_id,
'type': 'TEAM',
'permissions': permissions,
}

def exists(self, **kwargs):
automation_member = self.default_automation_member(**kwargs)
automation_member.update(kwargs)
Expand Down
Loading

0 comments on commit 2ad4478

Please sign in to comment.