← Back to team overview

canonical-hw-cert team mailing list archive

[Merge] ~kevinyeh/cc-lab-manager:get-rid-of-c3-cli into cc-lab-manager:master

 

Kevin Yeh has proposed merging ~kevinyeh/cc-lab-manager:get-rid-of-c3-cli into cc-lab-manager:master.

Commit message:
get rid of c3-cli part and use request directly instead.
add checking status code from request()

Requested reviews:
  Canonical Hardware Certification (canonical-hw-cert)

For more details, see:
https://code.launchpad.net/~kevinyeh/cc-lab-manager/+git/cc-lab-manager/+merge/428437
-- 
Your team Canonical Hardware Certification is requested to review the proposed merge of ~kevinyeh/cc-lab-manager:get-rid-of-c3-cli into cc-lab-manager:master.
diff --git a/cc_lab_manager/c3_db/c3_db.py b/cc_lab_manager/c3_db/c3_db.py
index 38a49e4..ec1e5aa 100644
--- a/cc_lab_manager/c3_db/c3_db.py
+++ b/cc_lab_manager/c3_db/c3_db.py
@@ -1,18 +1,19 @@
 import sqlite3
 import logging
 import argparse
-import c3.api.query as c3query
-from c3.api.api_utils import APIQuery
 import os
+import json
+import requests
 
 logger = logging.getLogger('cc-lab-manager')
+logging.basicConfig(level=logging.INFO)
 
 
-def c3_to_db(db_path):
+def c3_to_db(db_path, rparam):
     """
     Query machine info from c3 and write to db.
     """
-
+    URL = 'https://certification.canonical.com/api/v1/hardware/{CID}'
     # Read CID from db
     try:
         hwdb = sqlite3.connect(db_path)
@@ -30,7 +31,11 @@ def c3_to_db(db_path):
         # Query hardware info from c3
         result = ''
         try:
-            result = c3query.query_over_api_hardware(cid)
+            res = requests.get(URL.format(CID=cid), rparam)
+            assert res.ok ,"request to {} is failed, status code is {}".format(cid,res.status_code)
+            result = res.json()
+        except AssertionError as error_msg:
+            logger.error(error_msg)
         except Exception as e:
             logger.error("Error when query c3: ", e)
 
@@ -107,21 +112,15 @@ def environ_or_required(key):
 
 def main():
     parser = argparse.ArgumentParser()
-    parser.add_argument('--conf', help="Config file for C3 API", default = os.environ.get('CCLM_CC_TOOL_BOX') + '/config/c3-cli/my_conf.ini')
     parser.add_argument('--c3user', help="C3 User Account", **environ_or_required('CCLM_C3_USERNAME'))
     parser.add_argument('--c3apikey', help="C3 API Key", **environ_or_required('CCLM_C3_APIKEY'))
     parser.add_argument('--hwdb', help="C3 API Key", **environ_or_required('CCLM_HARDWARE_DB'))
     args = parser.parse_args()
 
-    ci = c3query.configuration.get_instance()
-    ci.read_configuration(args.conf)
-
-    api = APIQuery(ci.config['C3']['URI'])
     rparam = {"username": args.c3user,
               "api_key": args.c3apikey}
-
-    c3query.api_instance.set_api_params(api, rparam)
-    c3_to_db(args.hwdb)
+    
+    c3_to_db(args.hwdb,rparam)
 
 
 if __name__ == '__main__':