Cleanup nexus repo command for Python 3 17/11717/5
authorAndrew Grimberg <agrimberg@linuxfoundation.org>
Tue, 10 Jul 2018 21:59:24 +0000 (14:59 -0700)
committerAndrew Grimberg <agrimberg@linuxfoundation.org>
Wed, 11 Jul 2018 21:23:14 +0000 (14:23 -0700)
The original Nexus repo sub-command was written for Python 2 and needs
to be updated to work properly under Python 3.

Change-Id: I5558f833f4fc213e8eea64fc5a53ebcf1714d7be
Signed-off-by: Andrew Grimberg <agrimberg@linuxfoundation.org>
lftools/nexus/__init__.py

index 67d5fdb..5501e8b 100644 (file)
@@ -70,10 +70,13 @@ class Nexus:
             }
         }
 
-        json_data = json.dumps(targetencoding='latin-1')
+        json_data = json.dumps(target).encode(encoding='latin-1')
 
         r = requests.post(url, auth=self.auth, headers=self.headers, data=json_data)
 
+        if r.status_code != requests.codes.created:
+            raise Exception("Target not created for '{}', code '{}'".format(name, r.status_code))
+
         return r.json()['data']['id']
 
     def get_priv(self, name, priv):
@@ -115,8 +118,12 @@ class Nexus:
             }
         }
 
-        json_data = json.dumps(privileges, encoding='latin-1')
-        privileges = requests.post(url, auth=self.auth, headers=self.headers, data=json_data).json()
+        json_data = json.dumps(privileges).encode(encoding='latin-1')
+        r = requests.post(url, auth=self.auth, headers=self.headers, data=json_data)
+        privileges = r.json()
+
+        if r.status_code != requests.codes.created:
+            raise Exception("Privilege not created for '{}', code '{}'".format(name, r.status_code))
 
         return privileges['data'][0]['id']
 
@@ -148,10 +155,13 @@ class Nexus:
             }
         }
 
-        json_data = json.dumps(roleencoding='latin-1')
+        json_data = json.dumps(role).encode(encoding='latin-1')
 
         r = requests.post(url, auth=self.auth, headers=self.headers, data=json_data)
 
+        if r.status_code != requests.codes.created:
+            raise Exception("Role not created for '{}', code '{}'".format(name, r.status_code))
+
         return r.json()['data']['id']
 
     def get_user(self, user_id):
@@ -190,9 +200,12 @@ class Nexus:
         for role in extra_roles:
             user['data']['roles'].append(self.get_role(role))
 
-        json_data = json.dumps(user, encoding='latin-1')
+        json_data = json.dumps(user).encode(encoding='latin-1')
+
+        user = requests.post(url, auth=self.auth, headers=self.headers, data=json_data)
 
-        requests.post(url, auth=self.auth, headers=self.headers, data=json_data)
+        if user.status_code != requests.codes.created:
+            raise Exception("User not created for '{}', code '{}'".format(name, user.status_code))
 
     def get_repo_group(self, name):
         """Get the repository ID for a repo group that has a specific name."""
@@ -220,6 +233,6 @@ class Nexus:
             'data': data
         }
 
-        json_data = json.dumps(repoencoding='latin-1')
+        json_data = json.dumps(repo).encode(encoding='latin-1')
 
         requests.put(url, auth=self.auth, headers=self.headers, data=json_data)