Skip to content

Commit

Permalink
feat: add config methods
Browse files Browse the repository at this point in the history
  • Loading branch information
gg authored and oceane-sailorin committed Oct 13, 2022
1 parent 6c4f09d commit 82b7505
Show file tree
Hide file tree
Showing 28 changed files with 1,557 additions and 486 deletions.
3 changes: 3 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ omit =
jarvis_sdk/indykite/*
jarvis_sdk/validate/*
jarvis_sdk/cmd/helper.py
jarvis_sdk/cmdconfig/helper.py
jarvis_sdk/model/unique_name_identifier.py

tests/*

[report]
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ concurrency:

env:
INDYKITE_APPLICATION_CREDENTIALS: ${{ secrets.JARVIS_CREDENTIALS }}
INDYKITE_SERVICE_ACCOUNT_CREDENTIALS: ${{secrets.JARVIS_ACCOUNT_CREDENTIALS }}

jobs:
tests:
Expand Down
2 changes: 2 additions & 0 deletions codecov.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ ignore:
- "jarvis_sdk/indykite"
- "jarvis_sdk/validate"
- "jarvis_sdk/cmd/helper.py"
- "jarvis_sdk/cmdconfig/helper.py"
- "jarvis_sdk/model/unique_name_identifier.py"
- "tests"
72 changes: 71 additions & 1 deletion jarvis_sdk/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
from google.protobuf.json_format import MessageToJson

from jarvis_sdk.cmd import IdentityClient

from jarvis_sdk.cmdconfig import ConfigClient
from jarvis_sdk.indykite.config.v1beta1.model_pb2 import UniqueNameIdentifier

class ParseKwargs(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None): # pragma: no cover
Expand Down Expand Up @@ -108,10 +109,31 @@ def main():
enrich_token.add_argument("--session_claims", nargs='*',
help="Session claims to add (--session_claims key=value)", action=ParseKwargs)

# customer_id
customer_id_parser = subparsers.add_parser("customer_id")
#customer_parser.add_argument("access_token", help="JWT bearer token")

# customer_name
customer_name_parser = subparsers.add_parser("customer_name")
customer_name_parser.add_argument("customer_name", help="Customer name (not display name)")

# service_account
service_account_parser = subparsers.add_parser("service_account")

# app_space_id
app_space_id_parser = subparsers.add_parser("app_space_id")
app_space_id_parser.add_argument("app_space_id", help="App Space id (gid)")

# app_space_name
app_space_name_parser = subparsers.add_parser("app_space_name")
app_space_name_parser.add_argument("app_space_name", help="App Space name (not display name)")
app_space_name_parser.add_argument("customer_id", help="Customer Id (gid)")

args = parser.parse_args()

local = args.local
client = IdentityClient(local)
client_config = ConfigClient(local)

command = args.command

Expand Down Expand Up @@ -239,6 +261,54 @@ def main():
else:
print("Invalid token")

elif command == "customer_id":

try:
service_account = client_config.get_service_account()
except Exception as exception:
print(exception)
return None

print(service_account.customer_id)
customer = client_config.get_customer_by_id(service_account.customer_id)
if customer:
print_response(customer)
else:
print("Invalid customer id")

elif command == "customer_name":
customer_name = args.customer_name
customer = client_config.get_customer_by_name(customer_name)
if customer:
print_response(customer)
else:
print("Invalid customer id")

elif command == "service_account":

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

elif command == "app_space_id":
app_space_id = args.app_space_id
app_space = client_config.get_app_space_by_id(app_space_id)
if app_space:
print_response(app_space)
else:
print("Invalid app_space id")

elif command == "app_space_name":
app_space_name = args.app_space_name
customer_id = args.customer_id
app_space = client_config.get_app_space_by_name(customer_id, app_space_name)
if app_space:
print_response(app_space)
else:
print("Invalid app_space name")


def print_verify_info(digital_twin_info): # pragma: no cover
print("Digital twin info")
Expand Down
56 changes: 56 additions & 0 deletions jarvis_sdk/cmdconfig/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import certifi
import grpc
import os

from jarvis_sdk.cmdconfig import helper
from jarvis_sdk.indykite.config.v1beta1 import config_management_api_pb2_grpc as config_pb2_grpc


class ConfigClient(object):

def __init__(self, local=False):
cred = os.getenv('INDYKITE_SERVICE_ACCOUNT_CREDENTIALS')

# Load the config from File (secondary)
if (cred is False) or (cred is None):
cred = os.getenv('INDYKITE_SERVICE_ACCOUNT_CREDENTIALS_FILE')
try:
if (cred is False) or (cred is None):
raise Exception("Missing INDYKITE_SERVICE_ACCOUNT_CREDENTIALS or INDYKITE_SERVICE_ACCOUNT_CREDENTIALS_FILE environment variable")
except Exception as exception:
print(exception)
return None
credentials = os.path.join(os.path.dirname(cred), os.path.basename(cred))
credentials = helper.load_credentials(credentials)

# Load the credential json (primary)
else:
credentials = helper.load_json(cred)

service_account_token = helper.create_agent_jwt(credentials)

call_credentials = grpc.access_token_call_credentials(service_account_token.decode("utf-8"))

if local:
certificate_path = os.getenv('CAPEM')
endpoint = credentials.get("local_endpoint")
else:
certificate_path = certifi.where()
endpoint = credentials.get("endpoint")

with open(certificate_path, "rb") as cert_file:
channel_credentials = grpc.ssl_channel_credentials(cert_file.read())

composite_credentials = grpc.composite_channel_credentials(channel_credentials,
call_credentials)

self.channel = grpc.secure_channel(endpoint, composite_credentials)
self.stub = config_pb2_grpc.ConfigManagementAPIStub(channel=self.channel)
self.credentials = credentials

# Imported methods
from ._customer import get_customer_by_id, get_customer_by_name
from ._service_account import get_service_account
from ._app_space import get_app_space_by_id, get_app_space_by_name


44 changes: 44 additions & 0 deletions jarvis_sdk/cmdconfig/_app_space.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import uuid

from jarvis_sdk.cmdconfig import helper
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.app_space import ApplicationSpace
from jarvis_sdk.indykite.config.v1beta1.model_pb2 import UniqueNameIdentifier


def get_app_space_by_id(self, app_space_id):
try:
response = self.stub.ReadApplicationSpace(
pb2.ReadApplicationSpaceRequest(
id=str(app_space_id)
)
)
except Exception as exception:
print(exception)
return None

if not response:
return None

return ApplicationSpace.deserialize(response.app_space)


def get_app_space_by_name(self, customer_id, app_space_name):

try:
response = self.stub.ReadApplicationSpace(
pb2.ReadApplicationSpaceRequest(
name=UniqueNameIdentifier(location = customer_id, name = app_space_name)
)
)
except Exception as exception:
print(exception)
return None

if not response:
return None

return ApplicationSpace.deserialize(response.app_space)


43 changes: 43 additions & 0 deletions jarvis_sdk/cmdconfig/_customer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import uuid

from jarvis_sdk.cmdconfig import helper
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.customer import Customer


def get_customer_by_id(self, customer_id):
try:
response = self.stub.ReadCustomer(
pb2.ReadCustomerRequest(
id=str(customer_id)
)
)
except Exception as exception:
print(exception)
return None

if not response:
return None

return Customer.deserialize(response.customer)


def get_customer_by_name(self, customer_name):

try:
response = self.stub.ReadCustomer(
pb2.ReadCustomerRequest(
name=str(customer_name)
)
)
except Exception as exception:
print(exception)
return None

if not response:
return None

return Customer.deserialize(response.customer)


36 changes: 36 additions & 0 deletions jarvis_sdk/cmdconfig/_service_account.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from jarvis_sdk.cmdconfig import helper
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


def get_service_account(self,service_account=None):
try:
if service_account:
service_account_id = str(service_account)
else:
if self.credentials.get('serviceAccountId'):
service_account_id = self.credentials.get('serviceAccountId')
else:
raise Exception("Missing service account")
except Exception as exception:
print(exception)
return None

try:
response = self.stub.ReadServiceAccount(
pb2.ReadServiceAccountRequest(
id=str(service_account_id)
)
)
except Exception as exception:
print(exception)
return None

if not response:
return None

service_account = ServiceAccount.deserialize(response.service_account)

return service_account

0 comments on commit 82b7505

Please sign in to comment.