Skip to content

Commit

Permalink
feat: Add support for Team APIs (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
SharUpOff authored Oct 11, 2024
1 parent 6f9a845 commit cc8f918
Show file tree
Hide file tree
Showing 31 changed files with 1,106 additions and 53 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 4.3.0

Add support for Team APIs

# 4.2.1

Add support for name field in contacts
Expand Down
157 changes: 157 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,160 @@ for contact in contacts:
contact_id = moira.contact.get_id(type='slack', value='#err')
print(contact_id)
```

## Team

### Get all teams
```python
teams = moira.team.get_all()
```

### Create a new team
```python
from moira_client.models.team import TeamModel

team = TeamModel(
description="Team that holds all members of infrastructure division",
name="Infrastructure Team",
)

saved_team = moira.team.create(team)
```

### Delete a team
```python
team_id = "d5d98eb3-ee18-4f75-9364-244f67e23b54"

deleted_team = moira.team.delete(team_id)
```

### Get a team by ID
```python
team_id = "d5d98eb3-ee18-4f75-9364-244f67e23b54"

team = moira.team.get(team_id)
```

### Update existing team
```python
from moira_client.models.team import TeamModel

team_id = "d5d98eb3-ee18-4f75-9364-244f67e23b54"
team = TeamModel(
description="Team that holds all members of infrastructure division",
name="Infrastructure Team",
)

updated_team = moira.team.update(team_id, team)
```

### Team Settings

#### Get team settings
```python
team_id = "d5d98eb3-ee18-4f75-9364-244f67e23b54"

settings = moira.team.settings.get(team_id)
```

### Team User

#### Get users of a team

```python
team_id = "d5d98eb3-ee18-4f75-9364-244f67e23b54"

users = moira.team.user.get(team_id)
```

#### Add users to a team

```python
from moira_client.models.team.user import TeamMembers

team_id = "d5d98eb3-ee18-4f75-9364-244f67e23b54"
users_to_add = TeamMembers(usernames=["anonymous", ])

users = moira.team.user.add(team_id, users_to_add)
```

#### Set users of a team

```python
from moira_client.models.team.user import TeamMembers

team_id = "d5d98eb3-ee18-4f75-9364-244f67e23b54"
users_to_set = TeamMembers(usernames=["anonymous", ])

users = moira.team.user.set(team_id, users_to_set)
```

#### Delete a user from a team

```python
team_id = "d5d98eb3-ee18-4f75-9364-244f67e23b54"
team_user_id = "anonymous"

users = moira.team.user.delete(team_id, team_user_id)
```

### Team Subscription

#### Create a new team subscription

```python
from moira_client.models.subscription import SubscriptionModel

team_id = "d5d98eb3-ee18-4f75-9364-244f67e23b54"
subscription_to_create = SubscriptionModel(
any_tags=False,
contacts=[
"acd2db98-1659-4a2f-b227-52d71f6e3ba1"
],
enabled=True,
ignore_recoverings=False,
ignore_warnings=False,
plotting={
"enabled": True,
"theme": "dark"
},
sched={
"days": [
{
"enabled": True,
"name": "Mon"
}
],
"endOffset": 1439,
"startOffset": 0,
"tzOffset": -60
},
tags=[
"server",
"cpu"
],
throttling=False,
user="",
)

subscription = moira.team.subscription.create(team_id, subscription_to_create)
```

### Team Contact

#### Create a new team contact

```python
from moira_client.models.contact import Contact

team_id = "d5d98eb3-ee18-4f75-9364-244f67e23b54"
contact_to_create = Contact(
name="Mail Alerts",
team_id=team_id,
type="mail",
user="",
value="[email protected]",
)

contact = moira.team.contact.create(team_id, contact_to_create)
```
2 changes: 1 addition & 1 deletion VERSION.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4.2.1
4.3.0
1 change: 1 addition & 0 deletions moira_client/models/contact.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def __init__(self, value='', type='', **kwargs):
self.user = kwargs.get('user', None)
self.name = kwargs.get('name', None)
self._id = kwargs.get('id', None)
self.team_id = kwargs.get('team_id', None)


class ContactManager:
Expand Down
134 changes: 82 additions & 52 deletions moira_client/models/subscription.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@
from ..client import ResponseStructureError
from .base import Base

class Subscription(Base):
def __init__(self, client, tags, contacts=None, enabled=None, throttling=None, sched=None,
class SubscriptionModel(Base):
def __init__(self, tags, contacts=None, enabled=None, throttling=None, sched=None,
ignore_warnings=False, ignore_recoverings=False, plotting=None, any_tags=False, **kwargs):
"""
:param client: api client
:param tags: list of str tags
:param contacts: list of contact id's
:param enabled: bool is enabled
Expand All @@ -19,8 +17,6 @@ def __init__(self, client, tags, contacts=None, enabled=None, throttling=None, s
:param any_tags: bool any tags
:param kwargs: additional parameters
"""
self._client = client

self._id = kwargs.get('id', None)
self.tags = tags if not any_tags else []
if not contacts:
Expand All @@ -38,32 +34,7 @@ def __init__(self, client, tags, contacts=None, enabled=None, throttling=None, s
if not plotting:
plotting = {'enabled': False, 'theme': 'light'}
self.plotting = plotting

def _send_request(self, subscription_id=None):
data = {
'contacts': self.contacts,
'tags': self.tags,
'enabled': self.enabled,
'any_tags': self.any_tags,
'throttling': self.throttling,
'sched': self.sched,
'ignore_warnings': self.ignore_warnings,
'ignore_recoverings': self.ignore_recoverings,
'plotting': self.plotting
}

if subscription_id:
data['id'] = subscription_id

if subscription_id:
result = self._client.put('subscription/{id}'.format(id=subscription_id), json=data)
else:
result = self._client.put('subscription', json=data)
if 'id' not in result:
raise ResponseStructureError("id doesn't exist in response", result)

self._id = result['id']
return self._id
self.team_id = kwargs.get('team_id', None)

def disable_day(self, day):
"""
Expand Down Expand Up @@ -124,26 +95,6 @@ def disable_plotting(self):
'theme': 'light'
}

def save(self):
"""
Save subscription
:return: subscription id
"""
if self._id:
return self.update()
self._send_request()

def update(self):
"""
Update subscription
:return: subscription id
"""
if not self._id:
return self.save()
self._send_request(self._id)

def set_start_hour(self, hour):
"""
Set start hour
Expand Down Expand Up @@ -185,6 +136,85 @@ def set_end_minute(self, minute):
self._end_minute = minute


class Subscription(SubscriptionModel):
def __init__(self, client, tags, contacts=None, enabled=None, throttling=None, sched=None,
ignore_warnings=False, ignore_recoverings=False, plotting=None, any_tags=False, **kwargs):
"""
:param client: api client
:param tags: list of str tags
:param contacts: list of contact id's
:param enabled: bool is enabled
:param throttling: bool throttling
:param sched: dict schedule
:param ignore_warnings: bool ignore warnings
:param ignore_recoverings: bool ignore recoverings
:param plotting: dict plotting settings
:param any_tags: bool any tags
:param kwargs: additional parameters
"""
self._client = client

super().__init__(
tags=tags,
contacts=contacts,
enabled=enabled,
throttling=throttling,
sched=sched,
ignore_warnings=ignore_warnings,
ignore_recoverings=ignore_recoverings,
plotting=plotting,
any_tags=any_tags,
**kwargs,
)

def _send_request(self, subscription_id=None):
data = {
'contacts': self.contacts,
'tags': self.tags,
'enabled': self.enabled,
'any_tags': self.any_tags,
'throttling': self.throttling,
'sched': self.sched,
'ignore_warnings': self.ignore_warnings,
'ignore_recoverings': self.ignore_recoverings,
'plotting': self.plotting,
'team_id': self.team_id,
}

if subscription_id:
data['id'] = subscription_id

if subscription_id:
result = self._client.put('subscription/{id}'.format(id=subscription_id), json=data)
else:
result = self._client.put('subscription', json=data)
if 'id' not in result:
raise ResponseStructureError("id doesn't exist in response", result)

self._id = result['id']
return self._id

def save(self):
"""
Save subscription
:return: subscription id
"""
if self._id:
return self.update()
self._send_request()

def update(self):
"""
Update subscription
:return: subscription id
"""
if not self._id:
return self.save()
self._send_request(self._id)


class SubscriptionManager:
def __init__(self, client):
self._client = client
Expand Down
8 changes: 8 additions & 0 deletions moira_client/models/team/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from ._managers import TeamManager
from ._models import TeamModel


__all__ = [
"TeamManager",
"TeamModel",
]
Loading

0 comments on commit cc8f918

Please sign in to comment.