Skip to content

Commit 93309cd

Browse files
Jimmy Brissontheotherjimmy
authored andcommitted
Extend init to maybe create and always DL dev cert
1 parent 563ee0d commit 93309cd

File tree

1 file changed

+38
-3
lines changed

1 file changed

+38
-3
lines changed

tools/device_management.py

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,16 @@
1818
1919
device-management, dev-mgmt, and dm sub command
2020
"""
21-
from __future__ import print_function
22-
from builtins import str
21+
from __future__ import print_function, absolute_import
2322
import logging
2423
import sys
2524
import argparse
2625
from os.path import join, abspath, dirname, basename
26+
from os import getenv
2727

2828
from manifesttool import create, parse, verify, cert, init, update
2929
from manifesttool.argparser import MainArgumentParser
30+
from mbed_cloud import AccountManagementAPI, CertificatesAPI
3031
import colorama
3132
colorama.init()
3233

@@ -77,6 +78,40 @@ def inner(options):
7778
return inner
7879

7980

81+
def wrap_init(func):
82+
def inner(options):
83+
accounts = AccountManagementAPI()
84+
certs = CertificatesAPI()
85+
api_key = accounts.list_api_keys(filter={
86+
'key': getenv("MBED_CLOUD_SDK_API_KEY")
87+
}).next()
88+
user = accounts.get_user(api_key.owner_id)
89+
certificates_owned = list(certs.list_certificates())
90+
dev_cert_info = None
91+
for certif in certificates_owned:
92+
if certif.type == "developer" and (certif.owner_id == user.id or
93+
certif.owner_id == api_key.id):
94+
dev_cert_info = certs.get_certificate(certif.id)
95+
LOG.info("Found developer certificate onwed by %s named %s",
96+
user.full_name, dev_cert_info.name)
97+
break
98+
else:
99+
LOG.warning(
100+
"Could not find developer certificate for this account."
101+
" Generting a new developer certificate."
102+
)
103+
dev_cert_info = CertificatesAPI().add_developer_certificate(
104+
"mbed-cli-auto {}".format(user.full_name),
105+
description="cetificate auto-generated by Mbed CLI"
106+
)
107+
LOG.info("Writing developer certificate %s into c file "
108+
"mbed_cloud_dev_credentials.c", dev_cert_info.name)
109+
with open("mbed_cloud_dev_credentials.c", "w") as fout:
110+
fout.write(dev_cert_info.header_file)
111+
return func(options)
112+
return inner
113+
114+
80115
def main():
81116
options = MbedExtendedArgs().parse_args().options
82117

@@ -110,7 +145,7 @@ def main():
110145
"parse": parse.main,
111146
"verify": verify.main,
112147
"cert": cert.main,
113-
"init": init.main,
148+
"init": wrap_init(init.main),
114149
"update" : wrap_payload(update.main),
115150
}[options.action](options) or 0
116151

0 commit comments

Comments
 (0)