read_file_or_url: raise UrlError with 404 on ENOENT
This makes it easier to call read_file_or_url and handle file or url errors. Now read_file_or_url will raise a UrlError in either case on errors.
This commit is contained in:
parent
953632b555
commit
2910ecb8e3
@ -20,6 +20,7 @@
|
|||||||
# You should have received a copy of the GNU General Public License
|
# You should have received a copy of the GNU General Public License
|
||||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
import httplib
|
||||||
import time
|
import time
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
@ -32,6 +33,8 @@ from cloudinit import version
|
|||||||
|
|
||||||
LOG = logging.getLogger(__name__)
|
LOG = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
NOT_FOUND = httplib.NOT_FOUND
|
||||||
|
|
||||||
# Check if requests has ssl support (added in requests >= 0.8.8)
|
# Check if requests has ssl support (added in requests >= 0.8.8)
|
||||||
SSL_ENABLED = False
|
SSL_ENABLED = False
|
||||||
CONFIG_ENABLED = False # This was added in 0.7 (but taken out in >=1.0)
|
CONFIG_ENABLED = False # This was added in 0.7 (but taken out in >=1.0)
|
||||||
@ -58,6 +61,31 @@ def _cleanurl(url):
|
|||||||
return urlunparse(parsed_url)
|
return urlunparse(parsed_url)
|
||||||
|
|
||||||
|
|
||||||
|
# Made to have same accessors as UrlResponse so that the
|
||||||
|
# read_file_or_url can return this or that object and the
|
||||||
|
# 'user' of those objects will not need to know the difference.
|
||||||
|
class StringResponse(object):
|
||||||
|
def __init__(self, contents, code=200):
|
||||||
|
self.code = code
|
||||||
|
self.headers = {}
|
||||||
|
self.contents = contents
|
||||||
|
self.url = None
|
||||||
|
|
||||||
|
def ok(self, *args, **kwargs): # pylint: disable=W0613
|
||||||
|
if self.code != 200:
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return self.contents
|
||||||
|
|
||||||
|
|
||||||
|
class FileResponse(StringResponse):
|
||||||
|
def __init__(self, path, contents, code=200):
|
||||||
|
StringResponse.__init__(self, contents, code=code)
|
||||||
|
self.url = path
|
||||||
|
|
||||||
|
|
||||||
class UrlResponse(object):
|
class UrlResponse(object):
|
||||||
def __init__(self, response):
|
def __init__(self, response):
|
||||||
self._response = response
|
self._response = response
|
||||||
|
@ -74,31 +74,6 @@ FN_ALLOWED = ('_-.()' + string.digits + string.ascii_letters)
|
|||||||
CONTAINER_TESTS = ['running-in-container', 'lxc-is-container']
|
CONTAINER_TESTS = ['running-in-container', 'lxc-is-container']
|
||||||
|
|
||||||
|
|
||||||
# Made to have same accessors as UrlResponse so that the
|
|
||||||
# read_file_or_url can return this or that object and the
|
|
||||||
# 'user' of those objects will not need to know the difference.
|
|
||||||
class StringResponse(object):
|
|
||||||
def __init__(self, contents, code=200):
|
|
||||||
self.code = code
|
|
||||||
self.headers = {}
|
|
||||||
self.contents = contents
|
|
||||||
self.url = None
|
|
||||||
|
|
||||||
def ok(self, *args, **kwargs): # pylint: disable=W0613
|
|
||||||
if self.code != 200:
|
|
||||||
return False
|
|
||||||
return True
|
|
||||||
|
|
||||||
def __str__(self):
|
|
||||||
return self.contents
|
|
||||||
|
|
||||||
|
|
||||||
class FileResponse(StringResponse):
|
|
||||||
def __init__(self, path, contents, code=200):
|
|
||||||
StringResponse.__init__(self, contents, code=code)
|
|
||||||
self.url = path
|
|
||||||
|
|
||||||
|
|
||||||
class ProcessExecutionError(IOError):
|
class ProcessExecutionError(IOError):
|
||||||
|
|
||||||
MESSAGE_TMPL = ('%(description)s\n'
|
MESSAGE_TMPL = ('%(description)s\n'
|
||||||
@ -651,8 +626,8 @@ def read_optional_seed(fill, base="", ext="", timeout=5):
|
|||||||
fill['user-data'] = ud
|
fill['user-data'] = ud
|
||||||
fill['meta-data'] = md
|
fill['meta-data'] = md
|
||||||
return True
|
return True
|
||||||
except IOError as e:
|
except url_helper.UrlError as e:
|
||||||
if e.errno == errno.ENOENT:
|
if e.code == url_helper.NOT_FOUND:
|
||||||
return False
|
return False
|
||||||
raise
|
raise
|
||||||
|
|
||||||
@ -699,7 +674,14 @@ def read_file_or_url(url, timeout=5, retries=10,
|
|||||||
if data:
|
if data:
|
||||||
LOG.warn("Unable to post data to file resource %s", url)
|
LOG.warn("Unable to post data to file resource %s", url)
|
||||||
file_path = url[len("file://"):]
|
file_path = url[len("file://"):]
|
||||||
return FileResponse(file_path, contents=load_file(file_path))
|
try:
|
||||||
|
contents = load_file(file_path)
|
||||||
|
except IOError as e:
|
||||||
|
code = e.errno
|
||||||
|
if e.errno == errno.ENOENT:
|
||||||
|
code = url_helper.NOT_FOUND
|
||||||
|
raise url_helper.UrlError(cause=e, code=code, headers=None)
|
||||||
|
return url_helper.FileResponse(file_path, contents=contents)
|
||||||
else:
|
else:
|
||||||
return url_helper.readurl(url,
|
return url_helper.readurl(url,
|
||||||
timeout=timeout,
|
timeout=timeout,
|
||||||
|
@ -196,7 +196,7 @@ class TestCmdlineUrl(MockerTestCase):
|
|||||||
mock_readurl = self.mocker.replace(url_helper.readurl,
|
mock_readurl = self.mocker.replace(url_helper.readurl,
|
||||||
passthrough=False)
|
passthrough=False)
|
||||||
mock_readurl(url, ARGS, KWARGS)
|
mock_readurl(url, ARGS, KWARGS)
|
||||||
self.mocker.result(util.StringResponse(payload))
|
self.mocker.result(url_helper.StringResponse(payload))
|
||||||
self.mocker.replay()
|
self.mocker.replay()
|
||||||
|
|
||||||
self.assertEqual((key, url, None),
|
self.assertEqual((key, url, None),
|
||||||
@ -212,7 +212,7 @@ class TestCmdlineUrl(MockerTestCase):
|
|||||||
mock_readurl = self.mocker.replace(url_helper.readurl,
|
mock_readurl = self.mocker.replace(url_helper.readurl,
|
||||||
passthrough=False)
|
passthrough=False)
|
||||||
mock_readurl(url, ARGS, KWARGS)
|
mock_readurl(url, ARGS, KWARGS)
|
||||||
self.mocker.result(util.StringResponse(payload))
|
self.mocker.result(url_helper.StringResponse(payload))
|
||||||
self.mocker.replay()
|
self.mocker.replay()
|
||||||
|
|
||||||
self.assertEqual((key, url, payload),
|
self.assertEqual((key, url, payload),
|
||||||
@ -225,7 +225,7 @@ class TestCmdlineUrl(MockerTestCase):
|
|||||||
cmdline = "ro %s=%s bar=1" % (key, url)
|
cmdline = "ro %s=%s bar=1" % (key, url)
|
||||||
|
|
||||||
self.mocker.replace(url_helper.readurl, passthrough=False)
|
self.mocker.replace(url_helper.readurl, passthrough=False)
|
||||||
self.mocker.result(util.StringResponse(""))
|
self.mocker.result(url_helper.StringResponse(""))
|
||||||
self.mocker.replay()
|
self.mocker.replay()
|
||||||
|
|
||||||
self.assertEqual((None, None, None),
|
self.assertEqual((None, None, None),
|
||||||
|
@ -122,7 +122,7 @@ class TestMAASDataSource(mocker.MockerTestCase):
|
|||||||
headers_cb=my_headers_cb,
|
headers_cb=my_headers_cb,
|
||||||
exception_cb=mocker.ANY)
|
exception_cb=mocker.ANY)
|
||||||
resp = valid.get(key)
|
resp = valid.get(key)
|
||||||
self.mocker.result(util.StringResponse(resp))
|
self.mocker.result(url_helper.StringResponse(resp))
|
||||||
self.mocker.replay()
|
self.mocker.replay()
|
||||||
|
|
||||||
(userdata, metadata) = DataSourceMAAS.read_maas_seed_url(my_seed,
|
(userdata, metadata) = DataSourceMAAS.read_maas_seed_url(my_seed,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user