summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cloudinit/sources/DataSourceGCE.py38
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):