class _RestPublisherHttpClient(MsgBase): def __init__(self, configHolder): self.restEndpoints = "{}" super(_RestPublisherHttpClient, self).__init__(configHolder) self.resource = "%s/%s" % (self.msg_endpoint, self.msg_queue) self.httpClient = HttpClient(configHolder) self._assignCredentials() def _assignCredentials(self): endpts_creds = json.loads(self.restEndpoints) try: creds = endpts_creds[self.msg_endpoint] except KeyError: printWarning("WARNING: no matching credentials for " + self.msg_endpoint) else: self.httpClient.addCredentials(creds["username"], creds["password"]) def send(self, message): self.httpClient.put(self.resource, message)
class PersistentDisk(object): def __init__(self, configHolder=ConfigHolder()): self.pdiskUsername = None self.pdiskPassword = None self.username = None self.password = None self.pemCertificate = None self.pemKey = None self.verboseLevel = Util.VERBOSE_LEVEL_NORMAL self.pdiskEndpoint = None self.endpointSuffix = '' self.maxMounts = 10 self.configHolder = configHolder self.configHolder.assign(self) # This will eventually contain the sanitized endpoint # with the proper suffix attached for the authentication # method being used. self.endpoint = None if not self.pdiskEndpoint: try: self.pdiskEndpoint = configHolder.endpoint except AttributeError: raise ConfigurationException('Missing persistent disk endpoint.') def _initPDiskConnection(self, configHolder=None): self.client = HttpClient(configHolder or self.configHolder) self._addCredentials() self._buildFQNEndpoint() def _getJson(self, url): return self.client.get(url, accept='application/json') def _getGzip(self, url): return self.client.get(url, accept='application/x-gzip') def _postJson(self, url, body=None, contentType='application/x-www-form-urlencoded'): headers, content = self.client.post(url, body, contentType, accept='application/json') return headers, content.replace('\\', '') def _putJson(self, url, body, contentType='application/x-www-form-urlencoded'): self.client.put(url, body, contentType, accept='application/json') def _postMultipart(self, url, files, params=[]): headers, content = self.client.post_multipart(url, files, params, accept='application/json') return headers, content.replace('\\', '') def describeVolumes(self, filters={}): self._initPDiskConnection() self._printContacting() listVolUrl = '%s/disks/' % self.endpoint headers, jsonDiskList = self._getJson(listVolUrl) self._raiseOnErrors(headers, jsonDiskList) disks = json.loads(jsonDiskList) return self._filterDisks(disks, filters) def search(self, key, value): self._setPDiskUserCredentials() filtered = self.describeVolumes({key: value}) return [disk['uuid'] for disk in filtered] def quarantineVolume(self, uuid): self._setPDiskUserCredentials() keyvalues = {'owner': self.pdiskUsername, 'quarantine': datetime.now()} self.updateVolume(keyvalues, uuid) def updateVolume(self, keyvalues, uuid): # Need to set the user as pdisk since we need to be super # to update pdisk metadata self._setPDiskUserCredentials() self._initPDiskConnection() self._printContacting() url = '%s/disks/%s' % (self.endpoint, uuid) body = urlencode(keyvalues) self._putJson(url, body) def updateVolumeAsUser(self, keyvalues, uuid): self._initPDiskConnection() self._printContacting() url = '%s/disks/%s' % (self.endpoint, uuid) body = urlencode(keyvalues) self._putJson(url, body) def getValue(self, key, uuid): self._setPDiskUserCredentials() self._initPDiskConnection() self._printContacting() url = '%s/disks/%s' % (self.endpoint, uuid) headers, jsonDisk = self._getJson(url) self._raiseOnErrors(headers, jsonDisk) disk = json.loads(jsonDisk) return disk[key] def _setPDiskUserCredentials(self): """Assign the super pdisk username/password""" if self.pdiskUsername and self.pdiskPassword: return loader = UsernamePasswordCredentialsLoader() loader.load() self.pdiskUsername = self.persistentDiskCloudServiceUser self.pdiskPassword = loader.get_password(self.pdiskUsername) def createVolume(self, size, tag, visibility): self._initPDiskConnection() self._printContacting() url = '%s/disks/' % self.endpoint body = {'size': size, 'tag': tag, 'visibility': self._getVisibilityFromBool(visibility)} headers, uuid = self._postJson(url, urlencode(body)) if headers.status == 201: return self._getUuidFromJson(uuid) self._raiseOnErrors(headers, uuid) def createVolumeFromUrl(self, size, tag, visibility, imageUrl, bytes, sha1): self._initPDiskConnection() self._printContacting() url = '%s/disks/' % self.endpoint body = {'size': size, 'tag': tag, 'visibility': self._getVisibilityFromBool(visibility), 'url': imageUrl, 'bytes': bytes, 'sha1': sha1} headers, uuid = self._postJson(url, urlencode(body)) if headers.status == 201: return self._getUuidFromJson(uuid) self._raiseOnErrors(headers, uuid) def createCowVolume(self, uuid, tag): # TODO: add iscow check self._setPDiskUserCredentials() self._initPDiskConnection() self._printContacting() url = '%s/disks/%s' % (self.endpoint, uuid) body = None # FIXME: We can't set body and use redirect "See Other" RPC pattern, as # there seems to be a bug in httplib2, which incorrectly approximates # the accepted MIME type when doing GET on redirected resource. # See: http://jira.stratuslab.eu:8080/browse/STRATUSLAB-941 # if tag: # body = urlencode({'tag': tag}) try: _, content = self._postJson(url, body) except Exception, ex: ex.mediaType = 'json' raise return self._getUuidFromJson(content)