-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(usergroup): adds logic for creation and updation of usergroups i…
…n model class
- Loading branch information
1 parent
8c498fb
commit 634fe46
Showing
1 changed file
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
# Copyright 2022 Rapyuta Robotics | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
import json | ||
import typing | ||
|
||
from munch import unmunchify | ||
from rapyuta_io import Client | ||
|
||
from riocli.jsonschema.validate import load_schema | ||
from riocli.model import Model | ||
|
||
|
||
class UserGroup(Model): | ||
|
||
def find_object(self, client: Client) -> typing.Any: | ||
group_guid, usergroup = self.rc.find_depends({ | ||
'kind': self.kind.lower(), | ||
'nameOrGUID': self.metadata.name, | ||
'organization': self.metadata.organization | ||
}) | ||
|
||
if not usergroup: | ||
return False | ||
|
||
return usergroup | ||
|
||
def create_object(self, client: Client) -> typing.Any: | ||
usergroup = unmunchify(self) | ||
usergroup.pop('rc', None) | ||
usergroup['spec']['name'] = self.metadata.name | ||
return client.create_usergroup(self.metadata.organization, usergroup['spec']) | ||
|
||
def update_object(self, client: Client, obj: typing.Any) -> typing.Any: | ||
usergroup = unmunchify(self) | ||
usergroup.pop('rc', None) | ||
payload = self._create_update_payload(obj, usergroup) | ||
return client.update_usergroup(self.metadata.organization, obj.guid, payload) | ||
|
||
def _create_update_payload(self, old, new): | ||
payload = { | ||
'name': old.name, | ||
'guid': old.guid, | ||
'description': new['spec']['description'], | ||
'update': { | ||
'members': {'add': [], 'remove': []}, | ||
'projects': {'add': [], 'remove': []}, | ||
'admins': {'add': [], 'remove': []} | ||
} | ||
} | ||
|
||
for entity in ('members', 'projects', 'admins'): | ||
old_set = {i.guid for i in (getattr(old, entity) or [])} | ||
new_set = {i['guid'] for i in new['spec'].get(entity, [])} | ||
|
||
added = new_set - old_set | ||
removed = old_set - new_set | ||
|
||
payload['update'][entity]['add'] = [{'guid': guid} for guid in added] | ||
payload['update'][entity]['remove'] = [{'guid': guid} for guid in removed] | ||
|
||
return payload | ||
|
||
def delete_object(self, client: Client, obj: typing.Any) -> typing.Any: | ||
return client.delete_usergroup(self.metadata.organization, obj.guid) | ||
|
||
@classmethod | ||
def pre_process(cls, client: Client, d: typing.Dict) -> None: | ||
pass | ||
|
||
@staticmethod | ||
def validate(data): | ||
schema = load_schema('usergroup') | ||
schema.validate(data) |