Skip to content

Commit

Permalink
feat: eng-114 register digital twin without cred
Browse files Browse the repository at this point in the history
  • Loading branch information
cowan-macady committed Mar 20, 2023
1 parent c2fa58c commit 5a0295f
Show file tree
Hide file tree
Showing 34 changed files with 973 additions and 491 deletions.
2 changes: 2 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ omit =
indykite_sdk/model/oauth2_provider_config.py
indykite_sdk/model/import_digital_twin.py
indykite_sdk/utils/hash_methods.py
indykite_sdk/utils/message_to_value.py
indykite_sdk/utils/logger.py

tests/*

Expand Down
2 changes: 1 addition & 1 deletion buf.gen.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ managed:
plugins:
- remote: buf.build/protocolbuffers/plugins/python:v3.19.3-1
out: indykite_sdk
- remote: buf.build/grpc/plugins/python:v1.44.0-1
- remote: buf.build/grpc/plugins/python
out: indykite_sdk
# https://buf.build/protocolbuffers/plugins/python
# https://buf.build/grpc/plugins/python
2 changes: 2 additions & 0 deletions codecov.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ ignore:
- "indykite_sdk/model/oauth2_provider_config.py"
- "indykite_sdk/model/import_digital_twin.py"
- "indykite_sdk/utils/hash_methods.py"
- "indykite_sdk/utils/message_to_value.py"
- "indykite_sdk/utils/logger.py"
- "tests"
15 changes: 13 additions & 2 deletions indykite_sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ The config file is generated in the [admin console](https://console.indykite.id/

You should use absolute paths for the files.

## Running the sdk with the api.py script
# Running the sdk with the api.py script: examples for each function

With the [api.py](indykite_sdk/api.py) script you can simply run the sdk against the system you set up in the configuration
files
Expand Down Expand Up @@ -1296,7 +1296,7 @@ positional arguments:
email String
```
81. Check invitation state : check if invitation invalid, in future, pending, accepted, expired, cancelled
81. Check invitation state : check if invitation invalid, in future, pending, accepted, expired, cancelled, processing
```shell
python3 api.py check_invitation_state REFERENCE_ID
Expand Down Expand Up @@ -1330,6 +1330,17 @@ positional arguments:
reference_id id used to create the invitation
```
84. Register a DigitalTwin without credential
```shell
python3 api.py register_digital_twin_without_credential TENANT_ID
```
```shell
positional arguments:
tenant_id id of the tenant you want to create the DT into
```
----------------
To see all available options, run
Expand Down
25 changes: 25 additions & 0 deletions indykite_sdk/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
from indykite_sdk.indykite.identity.v1beta2.import_pb2 import Email as EmailIdentity
from indykite_sdk.model.is_authorized import IsAuthorizedResource
from indykite_sdk.model.tenant import Tenant
from indykite_sdk.indykite.identity.v1beta2 import attributes_pb2 as attributes
from indykite_sdk.identity import helper
import logging


Expand Down Expand Up @@ -536,6 +538,10 @@ def main():
cancel_invitation = subparsers.add_parser("cancel_invitation")
cancel_invitation.add_argument("reference_id", help="external ID of the invitation reference")

# register_digital_twin_without_credential
register_digital_twin_without_credential = subparsers.add_parser("register_digital_twin_without_credential")
register_digital_twin_without_credential.add_argument("tenant_id", help="gid ID of the tenant")

args = parser.parse_args()
local = args.local
client = IdentityClient(local)
Expand Down Expand Up @@ -1889,6 +1895,25 @@ def main():
else:
print("Invalid invitation response")

elif command == "register_digital_twin_without_credential":
tenant_id = args.tenant_id
properties = []
definition1 = attributes.PropertyDefinition(
property="extid"
)
property1 = helper.create_property(definition1, None, "44")
properties.append(property1)
register_response = client.register_digital_twin_without_credential(
tenant_id,
1,
[],
properties,
[])
if register_response is not None:
print_response(register_response)
else:
print("Invalid invitation response")


def print_verify_info(digital_twin_info): # pragma: no cover
print("Digital twin info")
Expand Down
10 changes: 5 additions & 5 deletions indykite_sdk/authorization/is_authorized.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from indykite_sdk.indykite.authorization.v1beta1 import authorization_service_pb2 as pb2
from indykite_sdk.indykite.identity.v1beta2 import identity_management_api_pb2 as pb2_ident
from indykite_sdk.indykite.identity.v1beta2 import attributes_pb2 as attributes
from indykite_sdk.indykite.identity.v1beta2 import model_pb2 as model
from indykite_sdk.indykite.objects.v1beta1 import struct_pb2 as pb2_struct
from indykite_sdk.model.is_authorized import IsAuthorizedResponse
Expand All @@ -12,7 +12,7 @@ def is_authorized_digital_twin(self, digital_twin_id, tenant_id, resources=[], a
try:
response = self.stub.IsAuthorized(
pb2.IsAuthorizedRequest(
digital_twin_identifier=pb2_ident.DigitalTwinIdentifier(
digital_twin_identifier=model.DigitalTwinIdentifier(
digital_twin=model.DigitalTwin(
id=str(digital_twin_id),
tenant_id=str(tenant_id)
Expand All @@ -36,7 +36,7 @@ def is_authorized_token(self, access_token, resources=[], actions=[]):
try:
response = self.stub.IsAuthorized(
pb2.IsAuthorizedRequest(
digital_twin_identifier=pb2_ident.DigitalTwinIdentifier(
digital_twin_identifier=model.DigitalTwinIdentifier(
access_token=str(access_token)
),
resources=request_resource(resources),
Expand All @@ -57,8 +57,8 @@ def is_authorized_property_filter(self, type_filter, value, resources=[], action
try:
response = self.stub.IsAuthorized(
pb2.IsAuthorizedRequest(
digital_twin_identifier=pb2_ident.DigitalTwinIdentifier(
property_filter=pb2_ident.PropertyFilter(
digital_twin_identifier=model.DigitalTwinIdentifier(
property_filter=attributes.PropertyFilter(
type=str(type_filter),
value=pb2_struct.Value(string_value=value)
)
Expand Down
1 change: 1 addition & 0 deletions indykite_sdk/identity/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,4 @@ def __init__(self, local=False):
from .consent import create_consent, list_consents, revoke_consent
from .forgotten_password import start_forgotten_password_flow
from .invitation import create_email_invitation, create_mobile_invitation, check_invitation_state, resend_invitation, cancel_invitation
from .register_digital_twins_no_cred import register_digital_twin_without_credential
4 changes: 2 additions & 2 deletions indykite_sdk/identity/delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def del_digital_twin(self, digital_twin_id, tenant_id):
try:
response = self.stub.DeleteDigitalTwin(
pb2.DeleteDigitalTwinRequest(
id=pb2.DigitalTwinIdentifier(
id=model.DigitalTwinIdentifier(
digital_twin=model.DigitalTwin(
id=str(digital_twin_id),
tenant_id=str(tenant_id)
Expand Down Expand Up @@ -38,7 +38,7 @@ def del_digital_twin_by_token(self, token):
try:
response = self.stub.DeleteDigitalTwin(
pb2.DeleteDigitalTwinRequest(
id=pb2.DigitalTwinIdentifier(access_token=token)
id=model.DigitalTwinIdentifier(access_token=token)
)
)
except Exception as exception:
Expand Down
4 changes: 2 additions & 2 deletions indykite_sdk/identity/get_digital_twin.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def get_digital_twin(self, digital_twin_id, tenant_id, fields):
try:
response = self.stub.GetDigitalTwin(
pb2.GetDigitalTwinRequest(
id=pb2.DigitalTwinIdentifier(
id=model.DigitalTwinIdentifier(
digital_twin=model.DigitalTwin(
id=str(digital_twin_id),
tenant_id=str(tenant_id)
Expand All @@ -39,7 +39,7 @@ def get_digital_twin_by_token(self, token, fields):
try:
response = self.stub.GetDigitalTwin(
pb2.GetDigitalTwinRequest(
id=pb2.DigitalTwinIdentifier(access_token=token),
id=model.DigitalTwinIdentifier(access_token=token),
properties=helper.create_property_mask(fields)
)
)
Expand Down
12 changes: 10 additions & 2 deletions indykite_sdk/identity/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,8 @@ def get_int_from_datetime(dt):

def create_property_mask(fields):
props = []
print(fields)
for f in fields:
props.append(attributes.PropertyMask(definition=attributes.PropertyDefinition(property=f)))

# print(props)
return props

Expand Down Expand Up @@ -103,3 +101,13 @@ def create_property_batch_operations(value_dict):
property_batch_ops.append(e)

return property_batch_ops


def create_property(definition, meta, value):
prop = attributes.Property(
id=None,
definition=definition,
meta=meta,
object_value=objects.Value(string_value=value)
)
return prop
4 changes: 2 additions & 2 deletions indykite_sdk/identity/patch_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def patch_properties(self, digital_twin_id, tenant_id, fields_in_dict):
try:
response = self.stub.PatchDigitalTwin(
pb2.PatchDigitalTwinRequest(
id=pb2.DigitalTwinIdentifier(
id=model.DigitalTwinIdentifier(
digital_twin=model.DigitalTwin(
id=str(digital_twin_id),
tenant_id=str(tenant_id)
Expand Down Expand Up @@ -42,7 +42,7 @@ def patch_properties_by_token(self, token, fields_in_dict):

response = self.stub.PatchDigitalTwin(
pb2.PatchDigitalTwinRequest(
id=pb2.DigitalTwinIdentifier(access_token=token),
id=model.DigitalTwinIdentifier(access_token=token),
operations=helper.create_property_batch_operations(fields_in_dict)
)
)
Expand Down
34 changes: 34 additions & 0 deletions indykite_sdk/identity/register_digital_twins_no_cred.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from indykite_sdk.indykite.identity.v1beta2 import identity_management_api_pb2 as pb2
from indykite_sdk.model.digital_twin_kind import DigitalTwinKind
from indykite_sdk.model.register_digital_twin_without_credential import RegisterDigitalTwinWithoutCredential
import sys
import indykite_sdk.utils.logger as logger


def register_digital_twin_without_credential(self, tenant_id, kind, tags, properties, bookmarks=[]):
sys.excepthook = logger.handle_excepthook
try:
if kind and validate_kind(kind):
response = self.stub.RegisterDigitalTwinWithoutCredential(
pb2.RegisterDigitalTwinWithoutCredentialRequest(
tenant_id=str(tenant_id),
digital_twin_kind=kind,
digital_twin_tags=tags,
properties=properties,
bookmarks=bookmarks
)
)
return RegisterDigitalTwinWithoutCredential.deserialize(response)
return None
except Exception as exception:
return logger.logger_error(exception)


def validate_kind(kind):
try:
kinds = [k.value for k in DigitalTwinKind]
if kind in kinds:
return True
return False
except Exception as exception:
return logger.logger_error(exception)

0 comments on commit 5a0295f

Please sign in to comment.