diff options
author | Vaidas Jablonskis <jablonskis@gmail.com> | 2014-02-07 09:43:32 +0000 |
---|---|---|
committer | Vaidas Jablonskis <jablonskis@gmail.com> | 2014-02-07 09:43:32 +0000 |
commit | e1710e06fd09c3f643fdca851c011da3a3eb7a98 (patch) | |
tree | 0df851ca45e9d419735c2467acb25c0a99bd3e49 | |
parent | 29b838af062549ef5e2f12c3df74721d294dea37 (diff) | |
download | vyos-cloud-init-e1710e06fd09c3f643fdca851c011da3a3eb7a98.tar.gz vyos-cloud-init-e1710e06fd09c3f643fdca851c011da3a3eb7a98.zip |
use url_helper instead of requests
-rw-r--r-- | cloudinit/sources/DataSourceGCE.py | 38 |
1 files changed, 12 insertions, 26 deletions
diff --git a/cloudinit/sources/DataSourceGCE.py b/cloudinit/sources/DataSourceGCE.py index b6a82245..f9cdc1da 100644 --- a/cloudinit/sources/DataSourceGCE.py +++ b/cloudinit/sources/DataSourceGCE.py @@ -14,10 +14,10 @@ # 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 requests from cloudinit import log as logging from cloudinit import sources +from cloudinit import url_helper LOG = logging.getLogger(__name__) @@ -30,16 +30,6 @@ class DataSourceGCE(sources.DataSource): self.metadata_address = MD_URL self.metadata = {} - # GCE takes sshKeys attribute in the format of '<user>:<public_key>' - # so we have to trim each key to remove the username part - def _trim_key(self, public_key): - try: - index = public_key.index(':') - if index > 0: - return public_key[(index + 1):] - except: - return public_key - def get_data(self): # GCE metadata server requires a custom header since v1 headers = {'X-Google-Metadata-Request': True} @@ -51,22 +41,18 @@ class DataSourceGCE(sources.DataSource): 'local-hostname': self.metadata_address + 'instance/hostname', } - with requests.Session() as s: - for mkey in url_map.iterkeys(): - try: - r = s.get(url_map[mkey], headers=headers) - except requests.exceptions.ConnectionError: - return False - if r.ok: - if mkey == 'public-keys': - pub_keys = [self._trim_key(k) for k in r.text.splitlines()] - self.metadata[mkey] = pub_keys - else: - self.metadata[mkey] = r.text + for mkey in url_map.iterkeys(): + resp = url_helper.readurl(url=url_map[mkey], headers=headers) + if resp.ok(): + if mkey == 'public-keys': + pub_keys = [self._trim_key(k) for k in resp.contents.splitlines()] + self.metadata[mkey] = pub_keys else: - self.metadata[mkey] = None - return False - return True + self.metadata[mkey] = resp.contents + else: + self.metadata[mkey] = None + return False + return True @property def launch_index(self): |