CloudVE/cloudbridge

Got AttributeError when running test_compute_service.py

niejn opened this issue · 2 comments

niejn commented

When I am running test_compute_service.test_crud_instance, I got error:
`Traceback (most recent call last):

File "/usr/lib/python2.7/unittest/case.py", line 329, in run
testMethod()
File "/home/niejjn/codes/github/cloudbridge/tests/helpers/init.py", line 42, in wrapper
func(self, *args, **kwargs)
File "/home/niejjn/codes/github/cloudbridge/tests/test_compute_service.py", line 70, in test_crud_instance
subnet = helpers.get_or_create_default_subnet(self.provider)
File "/home/niejjn/codes/github/cloudbridge/tests/helpers/init.py", line 135, in get_or_create_default_subnet
return provider.networking.subnets.get_or_create_default()
File "/home/niejjn/codes/github/cloudbridge/cloudbridge/providers/gcp/services.py", line 1051, in get_or_create_default
if sn.region_name == region_name:
File "/home/niejjn/codes/github/cloudbridge/cloudbridge/providers/gcp/resources.py", line 1577, in region_name
return parsed_url.parameters['region']
AttributeError: 'NoneType' object has no attribute 'parameters'`

The problem is that in function parse_url (providers.py) line 155
if url.startswith(self._root_url): url = url[len(self._root_url):]
when I am passing image url https://www.googleapis.com/compute/v1/projects/ubuntu-os-cloud/global/images/ubuntu-1804-bionic-v20181222, the if statement didn't work. As the url starts with www.googleapis.com, while the self._root_url is compute.googleapis.com , treating the url as resource name, causing the issue mentioned above.

The solution I suggest is that instead of using str.startwith, using urlparse to determine whether the url is a valid url might be better.
The code I am suggesting:

def parse_url(self, url):
        url = url.strip()
        o = urlparse(url)
        url = o.path
        url = url.lstrip('/')
        etc.

When I fixed the bug above, I still encountered the not valid resource name when I try to get a resource by its id.
The problem is the same, the url_or_name starts with www.googleapis.com, so I am using urlparse instead. Is there another reason to use _root_url other than checking whether the url is a url or name?

 def get_resource_url_with_default(self, resource, url_or_name, **kwargs):
        # If url_or_name is a valid GCP resource URL, then parse it.
        parsed_url = urlparse(url_or_name)
        _is_url = bool(parsed_url.scheme)
        if _is_url:
            return self.parse_url(url_or_name)
        etc.

Sorry for the late reply.
Thank you for all the info, I will try to get to fixing it this week.
Also, in general, PRs are always welcome if you have suggested solutions for issues that come up, we are always open to community contributors, although opening the issue is also useful if you do not have time to PR yourself :)

@niejn This should have been fixed with this: 9b0569f
Thanks for reporting!