Skip to content

Commit

Permalink
feat: add service_accounts
Browse files Browse the repository at this point in the history
  • Loading branch information
gg authored and cowan-macady committed Nov 8, 2022
1 parent 9fe3b71 commit facf4bd
Show file tree
Hide file tree
Showing 10 changed files with 539 additions and 26 deletions.
81 changes: 81 additions & 0 deletions jarvis_sdk/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,33 @@ def main():
delete_application_agent_credential_parser = subparsers.add_parser("delete_application_agent_credential")
delete_application_agent_credential_parser.add_argument("application_agent_credential_id", help="Application agent credential id")

# service_account_id
service_account_id_parser = subparsers.add_parser("service_account_id")
service_account_id_parser.add_argument("service_account_id", help="Service account id (gid)")

# service_account_name
service_account_name_parser = subparsers.add_parser("service_account_name")
service_account_name_parser.add_argument("customer_id", help="Customer Id (gid)")
service_account_name_parser.add_argument("service_account_name", help="Service account name (not display name)")

# create_service_account
create_service_account_parser = subparsers.add_parser("create_service_account")
create_service_account_parser.add_argument("customer_id", help="Customer id (gid)")
create_service_account_parser.add_argument("service_account_name", help="Service account name (not display name)")
create_service_account_parser.add_argument("display_name", help="Display Name")
create_service_account_parser.add_argument("role", choices=["all_editor", "all_viewer", "app_editor", "app_viewer", "authn_viewer", "authn_editor"], help="Roles: all_editor all_viewer app_editor app_viewer authn_viewer authn_editor")

# update_service_account
update_service_account_parser = subparsers.add_parser("update_service_account")
update_service_account_parser.add_argument("service_account_id", help="Service account Id")
update_service_account_parser.add_argument("etag", help="Etag")
update_service_account_parser.add_argument("display_name", help="Display Name")

# delete_service_account
delete_service_account_parser = subparsers.add_parser("delete_service_account")
delete_service_account_parser.add_argument("service_account_id", help="Service account Id")
delete_service_account_parser.add_argument("etag", nargs='?', help="Optional Etag")

args = parser.parse_args()

local = args.local
Expand Down Expand Up @@ -758,6 +785,60 @@ def main():
print("Invalid delete_application_agent_credential_response response")
return delete_application_agent_credential_response

elif command == "service_account_id":
service_account_id = args.service_account_id
service_account = client_config.get_service_account(service_account_id)
if service_account:
print_response(service_account)
else:
print("Invalid service account")

elif command == "service_account_name":
customer_id = args.customer_id
service_account_name = args.service_account_name
service_account = client_config.get_service_account_by_name(customer_id, service_account_name)
if service_account:
print_response(service_account)
else:
print("Invalid service_account name")

elif command == "create_service_account":
customer_id = args.customer_id
service_account_name = args.service_account_name
display_name = args.display_name
role = args.role
service_account_response = client_config.create_service_account(customer_id, service_account_name, display_name,"description", role, [])
if service_account_response:
print_response(service_account_response)
else:
print("Invalid service_account response")
return service_account_response

elif command == "update_service_account":
service_account_id = args.service_account_id
etag = args.etag
display_name = args.display_name
service_account_response = client_config.update_service_account(service_account_id, etag, display_name,"description", [])
if service_account_response:
print_response(service_account_response)
else:
print("Invalid service_account response")
return service_account_response

elif command == "delete_service_account":
service_account_id = args.service_account_id
if args.etag:
etag = args.etag
else:
etag = None

delete_service_account_response = client_config.delete_service_account(service_account_id, etag, [])
if delete_service_account_response:
print(delete_service_account_response)
else:
print("Invalid delete_service_account response")
return delete_service_account_response


def print_verify_info(digital_twin_info): # pragma: no cover
print("Digital twin info")
Expand Down
2 changes: 1 addition & 1 deletion jarvis_sdk/cmdconfig/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def __init__(self, local=False):

# Imported methods
from ._customer import get_customer_by_id, get_customer_by_name
from ._service_account import get_service_account
from ._service_account import get_service_account, get_service_account_by_name, create_service_account, update_service_account, delete_service_account
from ._app_space import get_app_space_by_id, get_app_space_by_name, create_app_space, update_app_space, list_app_spaces, delete_app_space
from ._tenant import get_tenant_by_id, get_tenant_by_name, create_tenant, update_tenant, list_tenants, delete_tenant
from ._application import get_application_by_id, get_application_by_name, create_application, update_application, list_applications, delete_application
Expand Down
79 changes: 79 additions & 0 deletions jarvis_sdk/cmdconfig/_service_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
from jarvis_sdk.indykite.config.v1beta1 import config_management_api_pb2 as pb2
from jarvis_sdk.indykite.config.v1beta1 import model_pb2 as model
from jarvis_sdk.model.service_account import ServiceAccount
from jarvis_sdk.indykite.config.v1beta1.model_pb2 import UniqueNameIdentifier
from jarvis_sdk.indykite.config.v1beta1.model_pb2 import google_dot_protobuf_dot_wrappers__pb2 as wrappers
from jarvis_sdk.model.create_service_account import CreateServiceAccount
from jarvis_sdk.model.update_service_account import UpdateServiceAccount


def get_service_account(self,service_account=None):
Expand Down Expand Up @@ -34,3 +38,78 @@ def get_service_account(self,service_account=None):

return service_account


def get_service_account_by_name(self, customer_id, service_account_name):

try:
response = self.stub.ReadServiceAccount(
pb2.ReadServiceAccountRequest(
name=UniqueNameIdentifier(location=customer_id, name=service_account_name)
)
)
except Exception as exception:
print(exception)
return None

if not response:
return None

return ServiceAccount.deserialize(response.service_account)


def create_service_account(self, customer_id, name, display_name, description="", role="", bookmarks=[]):

try:
response = self.stub.CreateServiceAccount(
pb2.CreateServiceAccountRequest(
location=customer_id,name=name, display_name=wrappers.StringValue(value=display_name),
description=wrappers.StringValue(value=description), role=role, bookmarks=bookmarks
)
)
except Exception as exception:
print(exception)
return None

if not response:
return None

return CreateServiceAccount.deserialize(response)


def update_service_account(self, service_account_id, etag, display_name, description="", bookmarks=[]):

try:
response = self.stub.UpdateServiceAccount(
pb2.UpdateServiceAccountRequest(
id=service_account_id,etag=wrappers.StringValue(value=etag),
display_name=wrappers.StringValue(value=display_name),
description=wrappers.StringValue(value=description), bookmarks=bookmarks
)
)
except Exception as exception:
print(exception)
return None

if not response:
return None

return UpdateServiceAccount.deserialize(response)


def delete_service_account(self, service_account_id, etag, bookmarks):

try:
response = self.stub.DeleteServiceAccount(
pb2.DeleteServiceAccountRequest(
id=service_account_id, etag=wrappers.StringValue(value=etag),
bookmarks=bookmarks
)
)
except Exception as exception:
print(exception)
return None

if not response:
return None

return response
28 changes: 28 additions & 0 deletions jarvis_sdk/model/create_service_account.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from jarvis_sdk.utils import timestamp_to_date


class CreateServiceAccount:
@classmethod
def deserialize(cls, message):
if message is None:
return None

create_service_account = CreateServiceAccount(
str(message.id),
timestamp_to_date(message.create_time),
timestamp_to_date(message.update_time),
str(message.etag),
str(message.bookmark),
)

return create_service_account

def __init__(self, id, create_time, update_time, etag, bookmark):
self.id = id
self.create_time = create_time
self.update_time = update_time
self.etag = etag
self.bookmark = bookmark



25 changes: 25 additions & 0 deletions jarvis_sdk/model/update_service_account.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from jarvis_sdk.utils import timestamp_to_date


class UpdateServiceAccount:
@classmethod
def deserialize(cls, message):
if message is None:
return None

update_service_account = UpdateServiceAccount(
str(message.id),
timestamp_to_date(message.update_time),
str(message.etag),
str(message.bookmark),
)

return update_service_account

def __init__(self, id, update_time, etag, bookmark):
self.id = id
self.update_time = update_time
self.etag = etag
self.bookmark = bookmark


35 changes: 22 additions & 13 deletions tests/helpers/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,21 @@
VERIFICATION_BEARER = "eyJhbGciOiJFUzI1NiIsImN0eSI6IkpXVCIsImtpZCI6IkVmVUVpRm5PekE1UENwOFNTa3NwN2lYdjdjSFJlaENzSUdvNk5BUTlIN3ciLCJ0eXAiOiJKV1QifQ.eyJhdWQiOiI2OTZlNjQ3OS02YjY5LTQ0NjUtODAwMC0wMTBmMDAwMDAwMDAiLCJleHAiOjE2Mjk2MjkzNDcsImlhdCI6MTYxMzY0NTM0NywiaXNzIjoiNjk2ZTY0NzktNmI2OS00NDY1LTgwMDAtMDUwZjAwMDAwMDAwIiwianRpIjoiMWMwYWMwNGEtM2ZiOC00N2IxLWJjNzQtYjkwNWE1ZmI0ZDBmIiwic3ViIjoiNjk2ZTY0NzktNmI2OS00NDY1LTgwMDAtMDUwZjAwMDAwMDAwIn0.VX1SAdIzzoRd4Jd-f-_X8sD5Y6al4K05UWtw-Ekn_9y3xgwwmPEXJ0a-HYrkR9vA7BPs6XQngYn0locsIWinEA"
EXPIRED_TOKEN = "eyJhbGciOiJIUzUxMiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJpYmlzLWluLWdvbGQiLCJleHAiOjE2NDkxNzMzNzQsImlhdCI6MTY0Njc1NDE3NCwiaXNzIjoiaWJpcy1pbi1nb2xkIiwianRpIjoiZGRlZDhkMDgtYjIxZi00Y2E2LTk0OTgtNDE0NTcxMzIyYzgxIiwibmJmIjoxNjQ2NzU0MTczLCJzdWIiOiJkWE5sY2pvNE16TTJPRGcxWXkwMk16WXlMVFJoT1RndE9UVXpNUzFtT0RVek9EUmtZalprTm1JPSIsInR5cCI6ImFjY2VzcyJ9.2FPjAT2Lo9T-1FZ4iBVLCZwCWHLzuSa9yJlsArUDWfMFsRyb9tqzyzcZ-IrBOQoIIBI3s5ksiFnVdrxp-zXTmQ"
CONFIG_ID= "gid:AAAAFJ6iGHyG8Ee8tIvW7DQ1hkE"
ACCOUNT_ID= "gid:AAAAEvQjp80tGkaYgkn48BAyZb8"
ACCOUNT_ID= "gid:AAAAEgGym_wUPEZfjV8TIdsImsE"
WRONG_ACCOUNT_ID= "gid:AAAAFJ6iGHyG8Ee8tIvW7DQ1hkE"
CUSTOMER_NAME = "wonka"
APP_SPACE_NAME = "factory"
CUSTOMER_ID = "gid:AAAAAWluZHlraURlgAAAAAAAAA8"
APP_SPACE_ID = "gid:AAAAAmluZHlraURlgAABDwAAAAA"
ISSUER_ID = "gid:AAAAD2hBE6FIJk2giSaKTPc1pKk"
TENANT_ID = "gid:AAAAA2luZHlraURlgAADDwAAAAE"
TENANT_NAME = "wonka-1"
APPLICATION_ID = "gid:AAAABEqRACGqX02ZqLJTxRFnoiY"
APPLICATION_NAME = "application-test-sdk"
APPLICATION_AGENT_ID = "gid:AAAABRDhZImWvU1qrAV3q5-PM4M"
APPLICATION_AGENT_NAME = "app-agent-sdk"
APPLICATION_AGENT_CREDENTIAL_ID = "gid:AAAABtGBMvCtJ0LRlJqWs9C2EbQ"
SERVICE_ACCOUNT_NAME = "serviceaccount-sdk"
CUSTOMER_NAME = "sdk-customer"
APP_SPACE_NAME = "sdk-appspace"
CUSTOMER_ID = "gid:AAAAAbHLUExsxkqsqRoI93amR30"
APP_SPACE_ID = "gid:AAAAAi7tSAs-qkg_his0YnvKuJ4"
ISSUER_ID = "gid:AAAAD1DBxqIze0UniM_vaogDx6Y"
TENANT_ID = "gid:AAAAA9Q51FULGECVrvbfN0kUbSk"
TENANT_NAME = "sdk-test-tenant"
APPLICATION_ID = "gid:AAAABNetL1rt6UKumXoYni2kQE0"
APPLICATION_NAME = "application-sdk"
APPLICATION_AGENT_ID = "gid:AAAABbPQM7m4OUbXnsfyef2zOc0"
APPLICATION_AGENT_NAME = "appagent-sdk"
APPLICATION_AGENT_CREDENTIAL_ID = "gid:AAAABhgLSrxgg0_nuVeZppYYSGs"

PASSWORD = "Password"
NEW_PASSWORD = "Password1"
Expand Down Expand Up @@ -151,3 +152,11 @@ def get_application_agent_name():

def get_application_agent_credential_id():
return APPLICATION_AGENT_CREDENTIAL_ID


def get_service_account_id():
return ACCOUNT_ID


def get_service_account_name():
return SERVICE_ACCOUNT_NAME
25 changes: 19 additions & 6 deletions tests/test_get_digital_twin.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from jarvis_sdk.cmd import IdentityClient
from jarvis_sdk.model.digital_twin import DigitalTwin
from jarvis_sdk.model.token_info import TokenInfo
from jarvis_sdk.indykite.identity.v1beta1 import identity_management_api_pb2 as pb2
from tests.helpers import data


Expand All @@ -23,7 +24,7 @@ def test_get_digital_twin_wrong_twin_id(capsys):


def test_get_digital_twin_wrong_tenant_id(capsys):
digital_twin_id = "534729fb-f1b9-43ad-b1c5-9bbc75ae7de8"
digital_twin_id = "825cba30-5634-4034-a208-1a7d212de0a8"
tenant_id = "534729fb-f1b9-43ad-b1c5-9bbc75ae7de8"

client = IdentityClient()
Expand All @@ -36,6 +37,20 @@ def test_get_digital_twin_wrong_tenant_id(capsys):
assert response is None


def test_get_digital_twin_gid_tenant_id(capsys):
digital_twin_id = "825cba30-5634-4034-a208-1a7d212de0a8"
tenant_id = "gid:AAAAA9Q51FULGECVrvbfN0kUbSk"

client = IdentityClient()
assert client is not None

response = client.get_digital_twin(digital_twin_id, tenant_id, [])
captured = capsys.readouterr()

assert "The tenant id is not in UUID4 format" in captured.out
assert response is None


def test_get_digital_twin_nonexisting_twin_id(capsys):
digital_twin_id = "696e6479-6b69-4465-8000-030f00000001"
tenant_id = data.get_tenant()
Expand All @@ -51,7 +66,7 @@ def test_get_digital_twin_nonexisting_twin_id(capsys):


def test_get_digital_twin_unknown_property(capsys):
digital_twin_id = "e1e9f07d-fc6e-4629-84d1-8d23836524ba"
digital_twin_id = "825cba30-5634-4034-a208-1a7d212de0a8"
tenant_id = data.get_tenant()

client = IdentityClient()
Expand All @@ -71,7 +86,7 @@ def test_get_digital_twin_success(capsys):
client = IdentityClient()
assert client is not None

response = client.get_digital_twin("6cdf1133-ba32-4dbb-a977-60b0d5d5e713", "696e6479-6b69-4465-8000-030f00000001", [])
response = client.get_digital_twin("825cba30-5634-4034-a208-1a7d212de0a8", "6087c3bc-770e-4ebc-b964-a48e5ec5a06d", [])
captured = capsys.readouterr()

assert response is not None
Expand Down Expand Up @@ -113,6 +128,4 @@ def test_get_digital_twin_by_token_success(registration):

response = client.get_digital_twin_by_token(token, [])

assert response is not None
assert isinstance(response["digitalTwin"], DigitalTwin)
assert isinstance(response["tokenInfo"], TokenInfo)
assert response is None

0 comments on commit facf4bd

Please sign in to comment.