From 8f62677eed174aa5c6ce17e4bdfd88022ed917c5 Mon Sep 17 00:00:00 2001 From: Bin Yang Date: Mon, 11 Sep 2017 14:49:30 +0800 Subject: Fix bug in service proxy fix bug in service proxy, along with unittests Change-Id: I946af21f4b8384ae920c322245741bf990515f1a Issue-Id: MULTICLOUD-58 Signed-off-by: Bin Yang --- newton/newton/proxy/tests/test_service_proxy.py | 3 ++ newton/newton/proxy/views/services.py | 59 ++++++++++++------------ newton/newton/registration/views/registration.py | 8 ++-- 3 files changed, 37 insertions(+), 33 deletions(-) (limited to 'newton') diff --git a/newton/newton/proxy/tests/test_service_proxy.py b/newton/newton/proxy/tests/test_service_proxy.py index e9f2691d..2dd558d5 100644 --- a/newton/newton/proxy/tests/test_service_proxy.py +++ b/newton/newton/proxy/tests/test_service_proxy.py @@ -698,6 +698,7 @@ MOCK_PATCH_IMAGE_RESPONSE = { class mock_get_servers_response_specs(object): status_code = 200 + content = '' def json(self): pass @@ -735,6 +736,7 @@ class TestServiceProxy(unittest.TestCase): mock_session = mock.Mock(name='mock_session', spec=mock_session_specs) mock_get_servers_response_obj = mock.Mock(spec=mock_get_servers_response_specs) mock_get_servers_response_obj.status_code=200 + mock_get_servers_response_obj.content = MOCK_GET_SERVERS_RESPONSE mock_get_servers_response_obj.json.return_value=MOCK_GET_SERVERS_RESPONSE mock_session.get.return_value = mock_get_servers_response_obj @@ -782,6 +784,7 @@ class TestServiceProxy(unittest.TestCase): mock_session = mock.Mock(name='mock_session', spec=mock_session_specs) mock_post_server_response_obj = mock.Mock(spec=mock_get_servers_response_specs) mock_post_server_response_obj.status_code=202 + mock_post_server_response_obj.content = MOCK_POST_SERVER_RESPONSE mock_post_server_response_obj.json.return_value=MOCK_POST_SERVER_RESPONSE mock_session.post.return_value = mock_post_server_response_obj diff --git a/newton/newton/proxy/views/services.py b/newton/newton/proxy/views/services.py index 1a478f35..85de3aa6 100644 --- a/newton/newton/proxy/views/services.py +++ b/newton/newton/proxy/views/services.py @@ -72,7 +72,8 @@ class Services(APIView): resp = sess.head(req_resource, endpoint_filter=service) #update token cache in case the token was required during the requests tmp_auth_token = VimDriverUtils.update_token_cache(vim, sess, tmp_auth_token, tmp_auth_state) - content = resp.json() + content = resp.json() if resp.content else None + return Response(headers={'X-Subject-Token': tmp_auth_token}, data=content, status=resp.status_code) #return resp except VimDriverNewtonException as e: @@ -142,13 +143,13 @@ class Services(APIView): resp = sess.get(req_resource, endpoint_filter=service) #update token cache in case the token was required during the requests tmp_auth_token = VimDriverUtils.update_token_cache(vim, sess, tmp_auth_token, tmp_auth_state) - content = resp.json() - - #filter the resp content and replace all endpoint prefix - tmp_content = json.dumps(content) - tmp_pattern = re.compile(real_prefix) - tmp_content = tmp_pattern.sub(proxy_prefix, tmp_content) - content = json.loads(tmp_content) + content = resp.json() if resp.content else None + if content: + #filter the resp content and replace all endpoint prefix + tmp_content = json.dumps(content) + tmp_pattern = re.compile(real_prefix) + tmp_content = tmp_pattern.sub(proxy_prefix, tmp_content) + content = json.loads(tmp_content) return Response(headers={'X-Subject-Token': tmp_auth_token}, data=content, status=resp.status_code) #return resp @@ -220,13 +221,13 @@ class Services(APIView): resp = sess.post(req_resource, data=json.JSONEncoder().encode(request.data),endpoint_filter=service) # update token cache in case the token was required during the requests tmp_auth_token = VimDriverUtils.update_token_cache(vim, sess, tmp_auth_token, tmp_auth_state) - content = resp.json() - - #filter the resp content and replace all endpoint prefix - tmp_content = json.dumps(content) - tmp_pattern = re.compile(real_prefix) - tmp_content = tmp_pattern.sub(proxy_prefix, tmp_content) - content = json.loads(tmp_content) + content = resp.json() if resp.content else None + if content: + #filter the resp content and replace all endpoint prefix + tmp_content = json.dumps(content) + tmp_pattern = re.compile(real_prefix) + tmp_content = tmp_pattern.sub(proxy_prefix, tmp_content) + content = json.loads(tmp_content) return Response(headers={'X-Subject-Token': tmp_auth_token}, data=content, status=resp.status_code) @@ -298,13 +299,13 @@ class Services(APIView): resp = sess.put(req_resource, data=json.JSONEncoder().encode(request.data),endpoint_filter=service) # update token cache in case the token was required during the requests tmp_auth_token = VimDriverUtils.update_token_cache(vim, sess, tmp_auth_token, tmp_auth_state) - content = resp.json() - - #filter the resp content and replace all endpoint prefix - tmp_content = json.dumps(content) - tmp_pattern = re.compile(real_prefix) - tmp_content = tmp_pattern.sub(proxy_prefix, tmp_content) - content = json.loads(tmp_content) + content = resp.json() if resp.content else None + if content: + #filter the resp content and replace all endpoint prefix + tmp_content = json.dumps(content) + tmp_pattern = re.compile(real_prefix) + tmp_content = tmp_pattern.sub(proxy_prefix, tmp_content) + content = json.loads(tmp_content) return Response(headers={'X-Subject-Token': tmp_auth_token}, data=content, status=resp.status_code) @@ -377,13 +378,13 @@ class Services(APIView): resp = sess.patch(req_resource, data=json.JSONEncoder().encode(request.data),endpoint_filter=service) # update token cache in case the token was required during the requests tmp_auth_token = VimDriverUtils.update_token_cache(vim, sess, tmp_auth_token, tmp_auth_state) - content = resp.json() - - #filter the resp content and replace all endpoint prefix - tmp_content = json.dumps(content) - tmp_pattern = re.compile(real_prefix) - tmp_content = tmp_pattern.sub(proxy_prefix, tmp_content) - content = json.loads(tmp_content) + content = resp.json() if resp.content else None + if content: + #filter the resp content and replace all endpoint prefix + tmp_content = json.dumps(content) + tmp_pattern = re.compile(real_prefix) + tmp_content = tmp_pattern.sub(proxy_prefix, tmp_content) + content = json.loads(tmp_content) return Response(headers={'X-Subject-Token': tmp_auth_token}, data=content, status=resp.status_code) diff --git a/newton/newton/registration/views/registration.py b/newton/newton/registration/views/registration.py index fa388b73..131a58e0 100644 --- a/newton/newton/registration/views/registration.py +++ b/newton/newton/registration/views/registration.py @@ -387,7 +387,7 @@ class Registry(APIView): ret = self.update_az(cloud_owner, cloud_region_id, vg_info) if ret != 0: # failed to update image - self._logger.debug("failed to populate az info into AAI: %s, volume-group-id: %s, ret:%s" + self._logger.debug("failed to populate volumegroup info into AAI: %s, volume-group-id: %s, ret:%s" % (vimid, vg_info['volume-group-id'], ret)) continue pass @@ -454,7 +454,7 @@ class Registry(APIView): ret = self.update_az(cloud_owner, cloud_region_id, snapshot_info) if ret != 0: # failed to update image - self._logger.debug("failed to populate az info into AAI: %s, snapshot-id: %s, ret:%s" + self._logger.debug("failed to populate snapshot info into AAI: %s, snapshot-id: %s, ret:%s" % (vimid, snapshot_info['snapshot-id'], ret)) continue pass @@ -554,7 +554,7 @@ class Registry(APIView): req_to_aai("/cloud-infrastructure/pservers/pserver/%s/relationship-list/relationship" % (pserverinfo['hostname']), "PUT", content=relationship_data) - self._logger.debug("update_snapshot,vimid:%s_%s req_to_aai: %s, return %s, %s, %s" + self._logger.debug("update_pserver,vimid:%s_%s req_to_aai: %s, return %s, %s, %s" % (cloud_owner, cloud_region_id, pserverinfo['hostname'], retcode, content, status_code)) pass @@ -578,7 +578,7 @@ class Registry(APIView): for hypervisor in content.get('hypervisors'): hypervisor_info = { 'hostname': hypervisor['hypervisor_hostname'], - 'in-maint': hypervisor['name'], + 'in-maint': hypervisor['state'], 'pserver-id': hypervisor.get('id'), 'ptnii-equip-name': hypervisor.get('id'), -- cgit 1.2.3-korg '>124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339