def delete_user(account_id, name): try: api_client = kapua_config.get_api_client() api_instance = swagger_client.UsersApi(api_client) user_id = get_user_id(account_id, name) api_client = kapua_config.get_api_client() api_instance.user_delete(account_id, user_id) print(f'Deleted user id: {user_id}') except ApiException as e: print(f'Exception when calling API: {e}')
def add_user_key(account_id, user_id, password=None): try: api_client = kapua_config.get_api_client() api_instance = swagger_client.CredentialsApi(api_client) expiration_date = datetime.date.today() + datetime.timedelta(days=365) if not password: # API key is generated by the server body = swagger_client.CredentialCreator( user_id=user_id, credential_type='API_KEY', credential_status='ENABLED', expiration_date=expiration_date) else: body = swagger_client.CredentialCreator( user_id=user_id, credential_type='PASSWORD', credential_status='ENABLED', credential_key=password, expiration_date=expiration_date) api_response = api_instance.credential_create(account_id, body) return api_response.credential_key except ApiException as e: print(f'Exception when calling API: {e}')
def grant_permissions(account_id, user_id): try: api_client = kapua_config.get_api_client() api_instance = swagger_client.AccessInfoApi(api_client) # create access info body = swagger_client.AccessInfoCreator(userid=user_id, scopeId=account_id) print('Create access info') pprint(body) print('start creating access info') api_response = api_instance.access_info_create(account_id, body) pprint(api_response) # add access permission access_info_id = api_response.id print('start getting permission') permission = swagger_client.Permission(target_scope_id=account_id) body = swagger_client.AccessPermissionCreator(permission=permission) print('start grant permission') api_response = api_instance.access_permission_create( account_id, access_info_id, body) pprint(api_response) except ApiException as e: print(f'Exception when calling API: {e}')
def delete_account(name): scope_id = kapua_config.get_scope_id() try: api_client = kapua_config.get_api_client() api_instance = swagger_client.AccountsApi(api_client) account_id = get_account_id(name) api_instance.account_delete(scope_id, account_id) print(f'Deleted account id: {account_id}') except ApiException as e: print(f'Exception when calling API: {e}')
def create_user(account_id, name): try: api_client = kapua_config.get_api_client() api_instance = swagger_client.UsersApi(api_client) body = swagger_client.UserCreator(name=name) api_response = api_instance.user_create(account_id, body) pprint(api_response) print( f'Created user {name} for account {account_id} with id: {api_response.id}' ) return api_response.id except ApiException as e: print(f'Exception when calling API: {e}')
def get_user_id(account_id, name): try: api_client = kapua_config.get_api_client() api_instance = swagger_client.UsersApi(api_client) api_response = api_instance.user_simple_query(account_id, name=name, limit=1) if len(api_response.items) > 0: user_id = api_response.items[0].id print(f'Found user id: {user_id} for {name}') return user_id else: raise NameError(f'No user named {name} found') except ApiException as e: print(f'Exception when calling API: {e}')
def get_account_id(name): scope_id = kapua_config.get_scope_id() try: api_client = kapua_config.get_api_client() api_instance = swagger_client.AccountsApi(api_client) api_response = api_instance.account_simple_query(scope_id, name=name, limit=1) if len(api_response.items) > 0: account_id = api_response.items[0].id print(f'Found account id: {account_id} for {name}') return account_id else: raise NameError(f'No account named {name} found') except ApiException as e: print(f'Exception when calling API: {e}')
def create_account(name, organization, email): scope_id = kapua_config.get_scope_id() try: api_client = kapua_config.get_api_client() api_instance = swagger_client.AccountsApi(api_client) # AccountCreator | Provides the information for the new Account to be created body = swagger_client.AccountCreator(name=name, organization_name=organization, organization_email=email) # Create an Account api_response = api_instance.account_create(scope_id, body) pprint(api_response) print(f'Created account with id: {api_response.id}') except ApiException as e: print(f'Exception when calling API: {e}')
def list_permissions(account_id, user_id): try: api_client = kapua_config.get_api_client() api_instance = swagger_client.AccessInfoApi(api_client) body = swagger_client.AccessInfoQuery() api_response = api_instance.access_info_query(account_id, body) print('AccessInfo:') pprint(api_response) access_info_id = api_response.items[0].id body = swagger_client.AccessPermissionQuery() api_response = api_instance.access_permission_query( account_id, access_info_id, body) print('Permissions:') pprint(api_response) except ApiException as e: print(f'Exception when calling API: {e}')
def delete_user_keys(account_id, user_id, key_type='API_KEY'): """ key_type: API_KEY, PASSWORD, JWT See: kapua-python-client/swagger_client/models/credential_creator.py """ try: api_client = kapua_config.get_api_client() api_instance = swagger_client.CredentialsApi(api_client) api_response = api_instance.credential_simple_query(account_id, limit=50, user_id=user_id) for i in api_response.items: # delete all keys matching the type if i.credential_type == key_type: api_instance.credential_delete(account_id, i.id) print(f'Deleted {key_type} credential {i.id}') except ApiException as e: print(f'Exception when calling API: {e}')
# test values account_name = 'diec1' user_name = 'user1' parser = argparse.ArgumentParser(description='Automates creation of Kapua devices') parser.add_argument('--add', nargs='+', type=str, help='Adds one or more devices with the given names') parser.add_argument('--list', nargs='*', type=str, help='Lists all devices') parser.add_argument('--delete', nargs='+', type=str, help='Removes one or more devices with the given names') args = parser.parse_args() try: account_id = kapua_accounts.get_account_id(name=account_name) user_id = kapua_accounts.get_user_id(account_id=account_id, name=user_name) # create an API key api_key = kapua_accounts.add_user_key(account_id, user_id) print(f'Generated API KEY: {api_key}') api_client = kapua_config.get_api_client(api_key=api_key) if args.add is not None: add_devices(api_client, account_id, args.add) elif args.list is not None: list_devices(api_client, account_id) elif args.delete is not None: delete_devices(api_client, account_id, args.delete) finally: # cleanup the API keys (note: will delete all API KEYS) kapua_accounts.delete_user_keys(account_id, user_id)
# # Kapua REST API test script # Author: Lisa Ong, NUS/ISS # import time from pprint import pprint # Generated python client import swagger_client from swagger_client.rest import ApiException import kapua_config try: # Authenticate an API user api_client = kapua_config.get_api_client() pprint(api_client.configuration.api_key) users_api = swagger_client.UsersApi(api_client) body = swagger_client.UserQuery( ) # UserQuery | The UserQuery to use to filter count results # Count the number of users users_response = users_api.user_count(kapua_config.get_scope_id(), body) pprint(users_response) except ApiException as e: print("Exception when calling API: %s\n" % e)