summaryrefslogtreecommitdiff
path: root/cloudinit/url_helper.py
diff options
context:
space:
mode:
authorScott Moser <smoser@ubuntu.com>2014-01-24 22:31:28 -0500
committerScott Moser <smoser@ubuntu.com>2014-01-24 22:31:28 -0500
commit0be4922d92e874b2e3300bdde65829cdb6569524 (patch)
treede85c8d5e002377023668b5b3b8c2525be652389 /cloudinit/url_helper.py
parent15ebe2a01c0bcdaae43054e0e3559871a99f72aa (diff)
downloadvyos-cloud-init-0be4922d92e874b2e3300bdde65829cdb6569524.tar.gz
vyos-cloud-init-0be4922d92e874b2e3300bdde65829cdb6569524.zip
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.
Diffstat (limited to 'cloudinit/url_helper.py')
-rw-r--r--cloudinit/url_helper.py28
1 files changed, 28 insertions, 0 deletions
diff --git a/cloudinit/url_helper.py b/cloudinit/url_helper.py
index 227983f3..97ed75ad 100644
--- a/cloudinit/url_helper.py
+++ b/cloudinit/url_helper.py
@@ -20,6 +20,7 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
+import httplib
import time
import requests
@@ -32,6 +33,8 @@ from cloudinit import version
LOG = logging.getLogger(__name__)
+NOT_FOUND = httplib.NOT_FOUND
+
# Check if requests has ssl support (added in requests >= 0.8.8)
SSL_ENABLED = False
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)
+# 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):
def __init__(self, response):
self._response = response