88import zipfile
99
1010from dotenv import load_dotenv
11+ from requests .auth import HTTPBasicAuth
1112
13+ from c8y_api import CumulocityApi
1214from c8y_api .app import SimpleCumulocityApp
1315from c8y_api .model import Application
1416
@@ -46,8 +48,8 @@ def register_microservice(sample_name: str):
4648
4749 # Read prepared binary .zip, extract manifest and parse
4850 zip_path = f'{ _DIST_DIR } /samples/{ sample_name } /{ application_name } .zip'
49- zip_file = zipfile .ZipFile (zip_path , 'r' )
50- manifest_json = json .loads (zip_file .read ('cumulocity.json' ))
51+ with zipfile .ZipFile (zip_path , 'r' ) as zip_file :
52+ manifest_json = json .loads (zip_file .read ('cumulocity.json' ))
5153
5254 # Create application stub in Cumulocity
5355 required_roles = manifest_json ['requiredRoles' ]
@@ -57,7 +59,11 @@ def register_microservice(sample_name: str):
5759 required_roles = required_roles )
5860 app = app .create ()
5961
60- print (f"Microservice application '{ application_name } ' created. (ID { app .id } )" )
62+ # Subscribe to newly created microservice
63+ subscription_json = {'application' : {'self' : f'{ c8y .base_url } /application/applications/{ app .id } ' }}
64+ c8y .post (f'/tenant/tenants/{ c8y .tenant_id } /applications' , json = subscription_json )
65+
66+ print (f"Microservice application '{ application_name } ' (ID { app .id } ) created. Tenant '{ c8y .tenant_id } ' subscribed." )
6167
6268
6369def unregister_microservice (sample_name : str ):
@@ -82,8 +88,8 @@ def unregister_microservice(sample_name: str):
8288 app = c8y .applications .get_all (name = application_name )[0 ]
8389 # delete by ID
8490 app .delete ()
85- except IndexError :
86- raise LookupError (f"Cannot retrieve information for an application named '{ application_name } '." )
91+ except IndexError as e :
92+ raise LookupError (f"Cannot retrieve information for an application named '{ application_name } '." ) from e
8793
8894 print (f"Microservice application '{ application_name } ' (ID { app .id } ) deleted." )
8995
@@ -107,12 +113,22 @@ def get_credentials(sample_name: str) -> (str, str):
107113 application_name = format_application_name (sample_name )
108114 load_dotenv ()
109115
116+ c8y = SimpleCumulocityApp ()
110117 try :
111- c8y = SimpleCumulocityApp ()
112118 # read applications by name, will throw IndexError if there is none
113119 app = c8y .applications .get_all (name = application_name )[0 ]
114- # read bootstrap user details, parse and print
115- user_json = c8y .get (f'/application/applications/{ app .id } /bootstrapUser' )
116- return user_json ['name' ], user_json ['password' ]
117- except IndexError :
118- raise LookupError (f"Cannot retrieve information for an application named '{ application_name } '." )
120+ except IndexError as e :
121+ raise LookupError (f"Cannot retrieve information for an application named '{ application_name } '." ) from e
122+
123+ # read bootstrap user details
124+ bootstrap_user_json = c8y .get (f'/application/applications/{ app .id } /bootstrapUser' )
125+ # create bootstrap instance
126+ bootstrap_c8y = CumulocityApi (base_url = c8y .base_url ,
127+ tenant_id = bootstrap_user_json ['tenant' ],
128+ auth = HTTPBasicAuth (bootstrap_user_json ['name' ], bootstrap_user_json ['password' ]))
129+ # read all subscribed tenants, print first
130+ users_json = bootstrap_c8y .get ('/application/currentApplication/subscriptions' )['users' ]
131+ if not users_json : # empty users element?
132+ raise LookupError (f"Cannot retrieve subscribed tenants for application named '{ application_name } '." )
133+
134+ return users_json [0 ]['tenant' ], users_json [0 ]['name' ], users_json [0 ]['password' ]
0 commit comments