def inner(options): accounts = AccountManagementAPI() certs = CertificatesAPI() api_key = accounts.list_api_keys( filter={ 'key': getenv("MBED_CLOUD_SDK_API_KEY") }).next() certificates_owned = list(certs.list_certificates()) dev_cert_info = None for certif in certificates_owned: if certif.type == "developer" and ( certif.owner_id == api_key.owner_id or certif.owner_id == api_key.id): dev_cert_info = certs.get_certificate(certif.id) LOG.info("Found developer certificate named %s", dev_cert_info.name) break else: LOG.warning( "Could not find developer certificate for this account." " Generting a new developer certificate.") dev_cert_info = CertificatesAPI().add_developer_certificate( "mbed-cli-auto {}".format(api_key.name), description="cetificate auto-generated by Mbed CLI") LOG.info( "Writing developer certificate %s into c file " "mbed_cloud_dev_credentials.c", dev_cert_info.name) with open("mbed_cloud_dev_credentials.c", "w") as fout: fout.write(dev_cert_info.header_file) return func(options)
def _main(): api = AccountManagementAPI() header = "Account details" print("%s\n%s" % (header, len(header) * "-")) account = api.get_account() print(account)
def test_configuration(self): with self.assertRaises(CloudApiException): # an example: configuring the SDK from mbed_cloud import AccountManagementAPI config = dict(api_key='ak_1234abc') # alternatively, configuration can be loaded from json files or environment variables # if the host is not the default Pelion Device Management, it needs to be specified config['host'] = 'https://custom-mbed-cloud-host.com' # create an instance of one of the SDK modules api = AccountManagementAPI(params=config) # do something with the SDK print(api.get_account())
def valid_api_key(api_key): """Call the Pelion Account Management API to validate an API key. :param str api_key: API key to validate. """ api = AccountManagementAPI({"api_key": api_key}) try: # We need to make a call to the api for the key to be validated. api.get_account() except CloudApiException: return False return True
def setup(self): # Check if API key is in environmental vars if 'MBED_CLOUD_SDK_API_KEY' in os.environ: api_key = (os.environ[str('MBED_CLOUD_SDK_API_KEY')]) else: api_key = self.config.get("api_key") if not api_key.startswith('ak_'): raise TestStepFail( "No API key in MBED_CLOUD_SDK_API_KEY or in pelion.tc_cfg") self.device_id = self.config.get("device_id") host = self.config.get("host") self.test_config = {"api_key": api_key, "host": host} self.account_api = AccountManagementAPI(self.test_config) self.connect_api = ConnectAPI(self.test_config) self.device_api = DeviceDirectoryAPI(self.test_config) # Additional parameters for handling REST requests without SDK self.rest_headers = {'Authorization': 'Bearer ' + api_key} self.rest_address = self.config.get("host") # Init delay due to internal usage of PULL notification in SDK. Notications might be lost between # tests. # TODO: Remove workaround after limitation in SDK has been fixed. self.delay(5)
def _main(): api = AccountManagementAPI() header = "All registered users in Organisation" print("%s\n%s" % (header, len(header) * "-")) for idx, u in enumerate(api.list_users()): print("\t- %s (%s - %s)" % (u.full_name, u.email, u.username)) header = "\nAll registered API keys in Organisation" presp = api.list_api_keys(limit=2) print("%s \n%s" % (header, len(header) * "-")) for idx, k in enumerate(presp): last_used = "Never" if k.last_login_time > 0: last_used = datetime.datetime.fromtimestamp(k.last_login_time / 1000).strftime('%c') print("\t- %s (Last used: %s)" % (k.name, last_used))
def inner(options): config = {} if getattr(options, 'api_key'): config["api_key"] = options.api_key if getattr(options, 'server_address'): config["host"] = options.server_address try: accounts = AccountManagementAPI(config) certs = CertificatesAPI(config) except Exception as e: LOG.error('Missing api key. Set it with ' '"mbed config -G CLOUD_SDK_API_KEY <api key>"') exit(1) # Get the currently in-use API key (may come from environment or # configuration files, which is handled by the cloud SDK) api_key_value = accounts.config.get("api_key") api_key = accounts.list_api_keys(filter={"key": api_key_value}).next() certificates_owned = list(certs.list_certificates()) dev_cert_info = None for certif in certificates_owned: if certif.type == "developer" and ( certif.owner_id == api_key.owner_id or certif.owner_id == api_key.id): dev_cert_info = certs.get_certificate(certif.id) LOG.info("Found developer certificate named %s", dev_cert_info.name) break else: LOG.warning( "Could not find developer certificate for this account." " Generting a new developer certificate.") dev_cert_info = CertificatesAPI().add_developer_certificate( "mbed-cli-auto {}".format(api_key.name), description="cetificate auto-generated by Mbed CLI") if getattr(options, 'no_developer_cert'): LOG.info("Skipping download of developer certificate") else: LOG.info( "Writing developer certificate %s into c file " "mbed_cloud_dev_credentials.c", dev_cert_info.name) with open("mbed_cloud_dev_credentials.c", "w") as fout: fout.write(dev_cert_info.header_file) return func(options)
def _main(): api = AccountManagementAPI() header = "List all groups in Organisation" print("%s\n%s" % (header, len(header) * "-")) groups = api.list_groups(limit=5, order='desc') for idx, group in enumerate(groups): print("\t- %s" % (group.name)) header = "\nAll registered API keys in Group: %s" % group.name keys = api.list_group_api_keys(group.id) print("%s \n%s" % (header, len(header) * "-")) for idx, k in enumerate(keys): print("\t- %s" % (k.name)) # list users from single group header = "\nAll registered Users in Group: %s" % group.name users = api.list_group_users(group.id) print("%s \n%s" % (header, len(header) * "-")) for idx, u in enumerate(users): print("\t- %s (%s - %s)" % (u.full_name, u.email, u.username))
def inner(options): if getattr(options, 'api_key', None): api_key = options.api_key else: api_key = getenv("MBED_CLOUD_SDK_API_KEY") if getattr(options, 'server_address', None): host_addr = options.server_address else: host_addr = getenv("MBED_CLOUD_SDK_HOST", "https://api.us-east-1.mbedcloud.com/") config = { "api_key": api_key, "host": host_addr, } accounts = AccountManagementAPI(config) certs = CertificatesAPI(config) api_key = accounts.list_api_keys(filter={'key': api_key}).next() certificates_owned = list(certs.list_certificates()) dev_cert_info = None for certif in certificates_owned: if certif.type == "developer" and ( certif.owner_id == api_key.owner_id or certif.owner_id == api_key.id): dev_cert_info = certs.get_certificate(certif.id) LOG.info("Found developer certificate named %s", dev_cert_info.name) break else: LOG.warning( "Could not find developer certificate for this account." " Generting a new developer certificate.") dev_cert_info = CertificatesAPI().add_developer_certificate( "mbed-cli-auto {}".format(api_key.name), description="cetificate auto-generated by Mbed CLI") LOG.info( "Writing developer certificate %s into c file " "mbed_cloud_dev_credentials.c", dev_cert_info.name) with open("mbed_cloud_dev_credentials.c", "w") as fout: fout.write(dev_cert_info.header_file) return func(options)
def _main(): config = {} try: config["api_key"] = os.environ['CLOUD_SDK_API_KEY'] except KeyError as e: LOG.error('Missing CLOUD_SDK_API_KEY enviromental key !') exit(1) accounts = AccountManagementAPI(config) certs = CertificatesAPI(config) api_key_value = accounts.config.get("api_key") api_key = next(accounts.list_api_keys(filter={"key": api_key_value})) certificates_owned = list(certs.list_certificates()) dev_cert_info = None for certif in certificates_owned: if certif.type == "developer" and (certif.owner_id == api_key.owner_id or certif.owner_id == api_key.id): dev_cert_info = certs.get_certificate(certif.id) LOG.info("Found developer certificate named '%s'", dev_cert_info.name) break else: LOG.warning("Could not find developer certificate for this account." " Generating a new developer certificate.") dev_cert_info = certs.add_developer_certificate( "mbed-cli-auto {}".format(api_key.name), description="cetificate auto-generated by pelion-device-simulator") LOG.info( "Writing developer certificate %s into c file " "'mbed_cloud_dev_credentials.c'", dev_cert_info.name) with open("mbed_cloud_dev_credentials.c", "w") as fout: fout.write(dev_cert_info.header_file)
def setup(self): self.device_id = self.config.get("device_id") api_key = self.config.get("api_key") host = self.config.get("host") self.test_config = {"api_key": api_key, "host": host} self.account_api = AccountManagementAPI(self.test_config) self.connect_api = ConnectAPI(self.test_config) self.device_api = DeviceDirectoryAPI(self.test_config) # Additional parameters for handling REST requests without SDK self.rest_headers = {'Authorization': 'Bearer ' + self.config.get("api_key")} self.rest_address = self.config.get("host") # Init delay due to internal usage of PULL notification in SDK. Notications might be lost between # tests. # TODO: Remove workaround after limitation in SDK has been fixed. self.delay(5)