Extend init to maybe create and always DL dev cert

pull/7844/head
Jimmy Brisson 2018-08-16 16:05:21 +01:00 committed by Jimmy Brisson
parent 563ee0d871
commit 93309cd134
1 changed files with 38 additions and 3 deletions

View File

@ -18,15 +18,16 @@ limitations under the License.
device-management, dev-mgmt, and dm sub command
"""
from __future__ import print_function
from builtins import str
from __future__ import print_function, absolute_import
import logging
import sys
import argparse
from os.path import join, abspath, dirname, basename
from os import getenv
from manifesttool import create, parse, verify, cert, init, update
from manifesttool.argparser import MainArgumentParser
from mbed_cloud import AccountManagementAPI, CertificatesAPI
import colorama
colorama.init()
@ -77,6 +78,40 @@ def wrap_payload(func):
return inner
def wrap_init(func):
def inner(options):
accounts = AccountManagementAPI()
certs = CertificatesAPI()
api_key = accounts.list_api_keys(filter={
'key': getenv("MBED_CLOUD_SDK_API_KEY")
}).next()
user = accounts.get_user(api_key.owner_id)
certificates_owned = list(certs.list_certificates())
dev_cert_info = None
for certif in certificates_owned:
if certif.type == "developer" and (certif.owner_id == user.id or
certif.owner_id == api_key.id):
dev_cert_info = certs.get_certificate(certif.id)
LOG.info("Found developer certificate onwed by %s named %s",
user.full_name, 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(user.full_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)
return inner
def main():
options = MbedExtendedArgs().parse_args().options
@ -110,7 +145,7 @@ def main():
"parse": parse.main,
"verify": verify.main,
"cert": cert.main,
"init": init.main,
"init": wrap_init(init.main),
"update" : wrap_payload(update.main),
}[options.action](options) or 0