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 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 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(): api = CertificatesAPI() # Create a new developer certificate print("Creating new developer certificate...") certificate = {"name": "my_dev_certificate"} new_certificate = api.add_developer_certificate(**certificate) print("Successfully created developer certificate with id: %r" % new_certificate.id) # Update certificate updated_certificate = api.update_certificate( new_certificate.id, description="my updated certificate") # Delete the certificate print("Attempting to delete certificate...") api.delete_certificate(updated_certificate.id) print("Successfully deleted certificate (ID: %r)" % updated_certificate.id)
def generate_dev_credentials(api_key, api_host): try: api_config = {"api_key": api_key, "host": api_host} api = CertificatesAPI(api_config) # Create a new developer certificate print("Creating new developer certificate...") certificate = {"name": "dev_certificate_" + str(time.time())} new_certificate = api.add_developer_certificate(**certificate) print("Successfully created developer certificate with id: %r" % new_certificate.id) file = open("mbed_cloud_dev_credentials.c", "w+") file.write(new_certificate.header_file) file.close() print( "Created developer credentials file at mbed_cloud_dev_credentials.c" ) except Exception as exc: print("[ERROR] Failed to create developer credentials \n %s" % str(exc))
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 _main(): api = CertificatesAPI() certificates = list(api.list_certificates(limit=10, order='desc')) for idx, certificate in enumerate(certificates): print(certificate)
def __init__(self, api_key): """Initialise the API.""" self._cert_api = CertificatesAPI(dict(api_key=api_key))