|
18 | 18 |
|
19 | 19 | device-management, dev-mgmt, and dm sub command
|
20 | 20 | """
|
21 |
| -from __future__ import print_function |
22 |
| -from builtins import str |
| 21 | +from __future__ import print_function, absolute_import |
23 | 22 | import logging
|
24 | 23 | import sys
|
25 | 24 | import argparse
|
26 | 25 | from os.path import join, abspath, dirname, basename
|
| 26 | +from os import getenv |
27 | 27 |
|
28 | 28 | from manifesttool import create, parse, verify, cert, init, update
|
29 | 29 | from manifesttool.argparser import MainArgumentParser
|
| 30 | +from mbed_cloud import AccountManagementAPI, CertificatesAPI |
30 | 31 | import colorama
|
31 | 32 | colorama.init()
|
32 | 33 |
|
@@ -77,6 +78,40 @@ def inner(options):
|
77 | 78 | return inner
|
78 | 79 |
|
79 | 80 |
|
| 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 | + |
80 | 115 | def main():
|
81 | 116 | options = MbedExtendedArgs().parse_args().options
|
82 | 117 |
|
@@ -110,7 +145,7 @@ def main():
|
110 | 145 | "parse": parse.main,
|
111 | 146 | "verify": verify.main,
|
112 | 147 | "cert": cert.main,
|
113 |
| - "init": init.main, |
| 148 | + "init": wrap_init(init.main), |
114 | 149 | "update" : wrap_payload(update.main),
|
115 | 150 | }[options.action](options) or 0
|
116 | 151 |
|
|
0 commit comments