def _refresh(self, http_request):
"""Refreshes the access_token.
Skip all the storage hoops and just refresh using the API.
Args:
http_request: callable, a callable that matches the method
signature of httplib2.Http.request, used to make
the refresh request.
Raises:
HttpAccessTokenRefreshError: When the refresh fails.
"""
try:
self._retrieve_info(http_request)
self.access_token, self.token_expiry = _metadata.get_token(
http_request, service_account=self.service_account_email)
except httplib2.HttpLib2Error as e:
raise HttpAccessTokenRefreshError(str(e))
python类HttpLib2Error()的实例源码
def _refresh(self, http_request):
"""Refreshes the access_token.
Skip all the storage hoops and just refresh using the API.
Args:
http_request: callable, a callable that matches the method
signature of httplib2.Http.request, used to make
the refresh request.
Raises:
HttpAccessTokenRefreshError: When the refresh fails.
"""
try:
self._retrieve_info(http_request)
self.access_token, self.token_expiry = _metadata.get_token(
http_request, service_account=self.service_account_email)
except httplib2.HttpLib2Error as e:
raise HttpAccessTokenRefreshError(str(e))
def get_instances(self, project_id):
"""Gets all CloudSQL instances for a project.
Args:
project_id (int): The project id for a GCP project.
Returns:
list: A list of database Instance resource dicts for a project_id.
https://cloud.google.com/sql/docs/mysql/admin-api/v1beta4/instances
[{"kind": "sql#instance", "name": "sql_instance1", ...}
{"kind": "sql#instance", "name": "sql_instance2", ...},
{...}]
Raises:
ApiExecutionError: ApiExecutionError is raised if the call to the
GCP ClodSQL API fails
"""
try:
paged_results = self.repository.instances.list(project_id)
return api_helpers.flatten_list_results(paged_results, 'items')
except (errors.HttpError, HttpLib2Error) as e:
LOGGER.warn(api_errors.ApiExecutionError(project_id, e))
raise api_errors.ApiExecutionError('instances', e)
def get_global_operation(self, project_id, operation_id):
"""Get the Operations Status
Args:
project_id (str): The project id.
operation_id (str): The operation id.
Returns:
dict: Global Operation status and info.
https://cloud.google.com/compute/docs/reference/latest/globalOperations/get
Raises:
ApiNotEnabledError: Returns if the api is not enabled.
ApiExecutionError: Returns if the api is not executable.
"""
try:
return self.repository.global_operations.get(
project_id, operation_id)
except (errors.HttpError, HttpLib2Error) as e:
api_not_enabled, details = _api_not_enabled(e)
if api_not_enabled:
raise api_errors.ApiNotEnabledError(details, e)
raise api_errors.ApiExecutionError(project_id, e)
def get_project(self, project_id):
"""Returns the specified Project resource.
Args:
project_id (str): The project id.
Returns:
dict: A Compute Project resource dict.
https://cloud.google.com/compute/docs/reference/latest/projects/get
"""
try:
return self.repository.projects.get(project_id)
except (errors.HttpError, HttpLib2Error) as e:
api_not_enabled, details = _api_not_enabled(e)
if api_not_enabled:
raise api_errors.ApiNotEnabledError(details, e)
raise api_errors.ApiExecutionError(project_id, e)
def get_group_members(self, group_key):
"""Get all the members for specified groups.
Args:
group_key (str): The group's unique id assigned by the Admin API.
Returns:
list: A list of member objects from the API.
Raises:
api_errors.ApiExecutionError: If group member retrieval fails.
"""
try:
paged_results = self.repository.members.list(group_key)
return api_helpers.flatten_list_results(paged_results, 'members')
except (errors.HttpError, HttpLib2Error) as e:
raise api_errors.ApiExecutionError(group_key, e)
def get_groups(self, customer_id='my_customer'):
"""Get all the groups for a given customer_id.
A note on customer_id='my_customer'. This is a magic string instead
of using the real customer id. See:
https://developers.google.com/admin-sdk/directory/v1/guides/manage-groups#get_all_domain_groups
Args:
customer_id (str): The customer id to scope the request to.
Returns:
list: A list of group objects returned from the API.
Raises:
api_errors.ApiExecutionError: If groups retrieval fails.
"""
try:
paged_results = self.repository.groups.list(customer=customer_id)
return api_helpers.flatten_list_results(paged_results, 'groups')
except (errors.HttpError, HttpLib2Error) as e:
raise api_errors.ApiExecutionError('groups', e)
def get_bigquery_projectids(self):
"""Request and page through bigquery projectids.
Returns:
list: A list of project_ids enabled for bigquery.
['project-id',
'project-id',
'...']
If there are no project_ids enabled for bigquery an empty list will
be returned.
"""
try:
results = self.repository.projects.list(
fields='nextPageToken,projects/id')
flattened = api_helpers.flatten_list_results(results, 'projects')
except (errors.HttpError, HttpLib2Error) as e:
raise api_errors.ApiExecutionError('bigquery', e)
project_ids = [result.get('id') for result in flattened
if 'id' in result]
return project_ids
def get_datasets_for_projectid(self, project_id):
"""Return BigQuery datasets stored in the requested project_id.
Args:
project_id (str): String representing the project id.
Returns:
list: A list of datasetReference objects for a given project_id.
[{'datasetId': 'dataset-id',
'projectId': 'project-id'},
{...}]
"""
try:
results = self.repository.datasets.list(
resource=project_id,
fields='datasets/datasetReference,nextPageToken',
all=True)
flattened = api_helpers.flatten_list_results(results, 'datasets')
except (errors.HttpError, HttpLib2Error) as e:
raise api_errors.ApiExecutionError(project_id, e)
datasets = [result.get('datasetReference') for result in flattened
if 'datasetReference' in result]
return datasets
def get_service(self, project_id, service_id):
"""Gets information about a specific service.
Args:
project_id (str): The id of the project.
service_id (str): The id of the service to query.
Returns:
dict: A Service resource dict for a given project_id and
service_id.
"""
try:
return self.repository.app_services.get(
project_id, target=service_id)
except (errors.HttpError, HttpLib2Error) as e:
if isinstance(e, errors.HttpError) and e.resp.status == 404:
return {}
raise api_errors.ApiExecutionError(project_id, e)
def list_services(self, project_id):
"""Lists services of a project.
Args:
project_id (str): The id of the project.
Returns:
list: A list of Service resource dicts for a project_id.
"""
try:
paged_results = self.repository.app_services.list(project_id)
return api_helpers.flatten_list_results(paged_results, 'services')
except (errors.HttpError, HttpLib2Error) as e:
if isinstance(e, errors.HttpError) and e.resp.status == 404:
return []
raise api_errors.ApiExecutionError(project_id, e)
def get_version(self, project_id, service_id, version_id):
"""Gets information about a specific version of a service.
Args:
project_id (str): The id of the project.
service_id (str): The id of the service to query.
version_id (str): The id of the version to query.
Returns:
dict: A Version resource dict for a given project_id and
service_id.
"""
try:
return self.repository.service_versions.get(
project_id, target=version_id, services_id=service_id)
except (errors.HttpError, HttpLib2Error) as e:
if isinstance(e, errors.HttpError) and e.resp.status == 404:
return {}
raise api_errors.ApiExecutionError(project_id, e)
def list_versions(self, project_id, service_id):
"""Lists versions of a given service.
Args:
project_id (str): The id of the project.
service_id (str): The id of the service to query.
Returns:
list: A list of Version resource dicts for a given Service.
"""
try:
paged_results = self.repository.service_versions.list(
project_id, services_id=service_id)
return api_helpers.flatten_list_results(paged_results, 'versions')
except (errors.HttpError, HttpLib2Error) as e:
if isinstance(e, errors.HttpError) and e.resp.status == 404:
return []
raise api_errors.ApiExecutionError(project_id, e)
def get_instance(self, project_id, service_id, version_id, instances_id):
"""Gets information about a specific instance of a service.
Args:
project_id (str): The id of the project.
service_id (str): The id of the service to query.
version_id (str): The id of the version to query.
instances_id (str): The id of the instance to query.
Returns:
dict: An Instance resource dict for a given project_id,
service_id and version_id.
"""
try:
return self.repository.version_instances.get(
project_id, target=instances_id, services_id=service_id,
versions_id=version_id)
except (errors.HttpError, HttpLib2Error) as e:
if isinstance(e, errors.HttpError) and e.resp.status == 404:
return {}
raise api_errors.ApiExecutionError(project_id, e)
cloud_resource_manager.py 文件源码
项目:forseti-security
作者: GoogleCloudPlatform
项目源码
文件源码
阅读 23
收藏 0
点赞 0
评论 0
def get_project(self, project_id):
"""Get all the projects from organization.
Args:
project_id (str): The project id (not project number).
Returns:
dict: The project response object.
Raises:
ApiExecutionError: An error has occurred when executing the API.
"""
try:
return self.repository.projects.get(project_id)
except (errors.HttpError, HttpLib2Error) as e:
raise api_errors.ApiExecutionError(project_id, e)
cloud_resource_manager.py 文件源码
项目:forseti-security
作者: GoogleCloudPlatform
项目源码
文件源码
阅读 24
收藏 0
点赞 0
评论 0
def get_org_iam_policies(self, resource_name, org_id):
"""Get all the iam policies of an org.
Args:
resource_name (str): The resource type.
org_id (int): The org id number.
Returns:
dict: Organization IAM policy for given org_id.
https://cloud.google.com/resource-manager/reference/rest/Shared.Types/Policy
Raises:
ApiExecutionError: An error has occurred when executing the API.
"""
resource_id = 'organizations/%s' % org_id
try:
iam_policy = (
self.repository.organizations.get_iam_policy(resource_id))
return {'org_id': org_id,
'iam_policy': iam_policy}
except (errors.HttpError, HttpLib2Error) as e:
raise api_errors.ApiExecutionError(resource_name, e)
cloud_resource_manager.py 文件源码
项目:forseti-security
作者: GoogleCloudPlatform
项目源码
文件源码
阅读 20
收藏 0
点赞 0
评论 0
def get_folder(self, folder_name):
"""Get a folder.
Args:
folder_name (str): The unique folder name, with the format
"folders/{folderId}".
Returns:
dict: The folder API response.
Raises:
ApiExecutionError: An error has occurred when executing the API.
"""
name = self.repository.folders.get_name(folder_name)
try:
return self.repository.folders.get(name)
except (errors.HttpError, HttpLib2Error) as e:
raise api_errors.ApiExecutionError(folder_name, e)
cloud_resource_manager.py 文件源码
项目:forseti-security
作者: GoogleCloudPlatform
项目源码
文件源码
阅读 22
收藏 0
点赞 0
评论 0
def get_folder_iam_policies(self, resource_name, folder_id):
"""Get all the iam policies of an folder.
Args:
resource_name (str): The resource name (type).
folder_id (int): The folder id.
Returns:
dict: Folder IAM policy for given folder_id.
Raises:
ApiExecutionError: An error has occurred when executing the API.
"""
resource_id = 'folders/%s' % folder_id
try:
iam_policy = self.repository.folders.get_iam_policy(resource_id)
return {'folder_id': folder_id,
'iam_policy': iam_policy}
except (errors.HttpError, HttpLib2Error) as e:
raise api_errors.ApiExecutionError(resource_name, e)
def get_objects(self, bucket):
"""Gets all GCS buckets for a project.
Args:
bucket (str): The bucket to list to objects in.
Returns:
list: a list of object resource dicts.
https://cloud.google.com/storage/docs/json_api/v1/objects
Raises:
ApiExecutionError: ApiExecutionError is raised if the call to the
GCP ClodSQL API fails
"""
try:
paged_results = self.repository.objects.list(bucket,
projection='full')
return api_helpers.flatten_list_results(paged_results, 'items')
except (errors.HttpError, HttpLib2Error) as e:
LOGGER.warn(api_errors.ApiExecutionError(bucket, e))
raise api_errors.ApiExecutionError('objects', e)
def get_service_accounts(self, project_id):
"""Get Service Accounts associated with a project.
Args:
project_id (str): The project ID to get Service Accounts for.
Returns:
list: List of service accounts associated with the project.
"""
name = self.repository.projects_serviceaccounts.get_name(project_id)
try:
paged_results = self.repository.projects_serviceaccounts.list(name)
return api_helpers.flatten_list_results(paged_results, 'accounts')
except (errors.HttpError, HttpLib2Error) as e:
LOGGER.warn(api_errors.ApiExecutionError(name, e))
raise api_errors.ApiExecutionError('serviceAccounts', e)
def __init__(self, **kwargs):
"""
Initializes object for interacting with Google Cloud Storage API.
| By default, Application Default Credentials are used.
| If gcloud SDK isn't installed, credential files have to be specified using the kwargs *json_credentials_path* and *client_id*.
:keyword max_retries: Argument specified with each API call to natively handle retryable errors.
:type max_retries: integer
:keyword chunksize: Upload/Download chunk size
:type chunksize: integer
:keyword client_secret_path: File path for client secret JSON file. Only required if credentials are invalid or unavailable.
:keyword json_credentials_path: File path for automatically generated credentials.
:keyword client_id: Credentials are stored as a key-value pair per client_id to facilitate multiple clients using the same credentials file. For simplicity, using one's email address is sufficient.
"""
self._service = get_service('storage', **kwargs)
self._max_retries = kwargs.get('max_retries', 3)
# Number of bytes to send/receive in each request.
self._chunksize = kwargs.get('chunksize', 2 * 1024 * 1024)
# Retry transport and file IO errors.
self._RETRYABLE_ERRORS = (HttpLib2Error, IOError)
def get_web_exchange(self, line_date):
'''??????????????'''
http = httplib2.Http()
if self.name not in currency_code:
raise UserError(u'?????????(%s)????' % self.currency_id.name)
url = 'http://srh.bankofchina.com/search/whpj/search.jsp'
body = {
'erectDate': line_date,
'nothing': line_date,
'pjname': currency_code[self.name]
}
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.6 Safari/537.36',
'Content-type': 'application/x-www-form-urlencoded'
}
try:
response, content = http.request(
url, 'POST', headers=headers, body=urllib.urlencode(body))
result = etree.HTML(content.decode('utf8')).xpath(
'//table[@cellpadding="0"]/tr[4]/td/text()')
except httplib2.HttpLib2Error:
raise UserError(u'??????(%s)???????qq?2201864?' % url)
return result[5]
def updatePhoto(users):
cd = buildGAPIObject(API.DIRECTORY)
filenamePattern = getString(Cmd.OB_PHOTO_FILENAME_PATTERN)
checkForExtraneousArguments()
p = re.compile(u'^(ht|f)tps?://.*$')
i, count, users = getEntityArgument(users)
for user in users:
i += 1
user, userName, _ = splitEmailAddressOrUID(user)
filename = _substituteForUser(filenamePattern, user, userName)
if p.match(filename):
try:
status, image_data = httplib2.Http(disable_ssl_certificate_validation=GC.Values[GC.NO_VERIFY_SSL]).request(filename, u'GET')
if status[u'status'] != u'200':
entityActionFailedWarning([Ent.USER, user, Ent.PHOTO, filename], Msg.NOT_ALLOWED, i, count)
continue
if status[u'content-location'] != filename:
entityActionFailedWarning([Ent.USER, user, Ent.PHOTO, filename], Msg.NOT_FOUND, i, count)
continue
except (httplib2.HttpLib2Error, httplib2.ServerNotFoundError, httplib2.CertificateValidationUnsupported) as e:
entityActionFailedWarning([Ent.USER, user, Ent.PHOTO, filename], str(e), i, count)
continue
else:
image_data = readFile(filename, mode=u'rb', continueOnError=True, displayError=True)
if image_data is None:
entityActionFailedWarning([Ent.USER, user, Ent.PHOTO, filename], None, i, count)
continue
body = {u'photoData': base64.urlsafe_b64encode(image_data)}
try:
callGAPI(cd.users().photos(), u'update',
throw_reasons=[GAPI.USER_NOT_FOUND, GAPI.FORBIDDEN, GAPI.INVALID_INPUT],
userKey=user, body=body, fields=u'')
entityActionPerformed([Ent.USER, user, Ent.PHOTO, filename], i, count)
except GAPI.invalidInput as e:
entityActionFailedWarning([Ent.USER, user, Ent.PHOTO, filename], str(e), i, count)
except (GAPI.userNotFound, GAPI.forbidden):
entityUnknownWarning(Ent.USER, user, i, count)
# gam <UserTypeEntity> delete photo
def next_chunk(self):
"""Get the next chunk of the download.
Returns:
(status, done): (MediaDownloadStatus, boolean)
The value of 'done' will be True when the media has been fully
downloaded.
Raises:
apiclient.errors.HttpError if the response was not a 2xx.
httplib2.HttpLib2Error if a transport error has occured.
"""
headers = {
'range': 'bytes=%d-%d' % (
self._progress, self._progress + self._chunksize)
}
http = self._request.http
http.follow_redirects = False
resp, content = http.request(self._uri, headers=headers)
if resp.status in [301, 302, 303, 307, 308] and 'location' in resp:
self._uri = resp['location']
resp, content = http.request(self._uri, headers=headers)
if resp.status in [200, 206]:
self._progress += len(content)
self._fd.write(content)
if 'content-range' in resp:
content_range = resp['content-range']
length = content_range.rsplit('/', 1)[1]
self._total_size = int(length)
if self._progress == self._total_size:
self._done = True
return MediaDownloadProgress(self._progress, self._total_size), self._done
else:
raise HttpError(resp, content, uri=self._uri)
def get_source(url):
"""Return the source of the supplied url argument"""
http = httplib2.Http()
try:
status, response = http.request(url,
headers={'User-Agent':' Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0'})
if status.status == 200:
return response
else:
return None
except httplib2.HttpLib2Error as e:
return None
def get(http_request, path, root=METADATA_ROOT, recursive=None):
"""Fetch a resource from the metadata server.
Args:
path: A string indicating the resource to retrieve. For example,
'instance/service-accounts/defualt'
http_request: A callable that matches the method
signature of httplib2.Http.request. Used to make the request to the
metadataserver.
root: A string indicating the full path to the metadata server root.
recursive: A boolean indicating whether to do a recursive query of
metadata. See
https://cloud.google.com/compute/docs/metadata#aggcontents
Returns:
A dictionary if the metadata server returns JSON, otherwise a string.
Raises:
httplib2.Httplib2Error if an error corrured while retrieving metadata.
"""
url = urlparse.urljoin(root, path)
url = util._add_query_parameter(url, 'recursive', recursive)
response, content = http_request(
url,
headers=METADATA_HEADERS
)
if response.status == http_client.OK:
decoded = _from_bytes(content)
if response['content-type'] == 'application/json':
return json.loads(decoded)
else:
return decoded
else:
raise httplib2.HttpLib2Error(
'Failed to retrieve {0} from the Google Compute Engine'
'metadata service. Response:\n{1}'.format(url, response))
def check(self, instance):
host, port, user, password, connect_timeout, server_name = self._get_config(instance)
tags = instance.get('tags', [])
tags = tags + ['server_name:%s' % server_name]
service_check_tags = tags + ['url:%s' % host]
version = self._get_version(instance, service_check_tags)
usrPass = user + ":" + password
b64Val = base64.b64encode(usrPass)
if version > 2:
checkURL = host + ":" + str(port) + "/db/data/transaction/commit"
else:
checkURL = host + ":" + str(port) + "/v1/service/metrics"
# Neo specific
# Create payload using built-in Neo4j queryJmx stored procedure
try:
payload = {"statements" : [{"statement" : "CALL dbms.queryJmx('org.neo4j:*') yield attributes with keys(attributes) as k, attributes unwind k as row return row, attributes[row]['value'];"}]}
headers_sent = {'Content-Type':'application/json','Authorization':'Basic ' + b64Val + '','Content-Type':'application/json'}
r = requests.post(checkURL, data=json.dumps(payload),headers=headers_sent)
except (socket.timeout, socket.error, HttpLib2Error) as e:
msg = "Unable to fetch Neo4j stats: %s" % str(e)
self._critical_service_check(service_check_tags, msg)
raise
if r.status_code != 200:
msg = "nexpected status of {0} when fetching Neo4j stats, response: {1}"
msg = msg.format(r.status_code, r.text)
self._critical_service_check(service_check_tags, msg)
r.raise_for_status()
stats = r.json()
self.service_check(
self.SERVICE_CHECK_NAME, AgentCheck.OK, tags=service_check_tags)
for doc in stats['results'][0]['data']:
if doc['row'][0].lower() in self.keys:
self.gauge(self.display.get(doc['row'][0].lower(),""), doc['row'][1], tags=tags)
def get(http_request, path, root=METADATA_ROOT, recursive=None):
"""Fetch a resource from the metadata server.
Args:
path: A string indicating the resource to retrieve. For example,
'instance/service-accounts/defualt'
http_request: A callable that matches the method
signature of httplib2.Http.request. Used to make the request to the
metadataserver.
root: A string indicating the full path to the metadata server root.
recursive: A boolean indicating whether to do a recursive query of
metadata. See
https://cloud.google.com/compute/docs/metadata#aggcontents
Returns:
A dictionary if the metadata server returns JSON, otherwise a string.
Raises:
httplib2.Httplib2Error if an error corrured while retrieving metadata.
"""
url = urlparse.urljoin(root, path)
url = util._add_query_parameter(url, 'recursive', recursive)
response, content = http_request(
url,
headers=METADATA_HEADERS
)
if response.status == http_client.OK:
decoded = _from_bytes(content)
if response['content-type'] == 'application/json':
return json.loads(decoded)
else:
return decoded
else:
raise httplib2.HttpLib2Error(
'Failed to retrieve {0} from the Google Compute Engine'
'metadata service. Response:\n{1}'.format(url, response))
def _flatten_aggregated_list_results(project_id, paged_results, item_key,
sort_key='name'):
"""Flatten results and handle exceptions.
Args:
project_id (str): The project id the results are for.
paged_results (list): A list of paged API response objects.
[{page 1 results}, {page 2 results}, {page 3 results}, ...]
item_key (str): The name of the key within the inner "items" lists
containing the objects of interest.
sort_key (str): The name of the key to sort the results by before
returning.
Returns:
list: A sorted list of items.
Raises:
ApiNotEnabledError: Raised if the API is not enabled for the project.
ApiExecutionError: Raised if there is another error while calling the
API method.
"""
try:
return sorted(
api_helpers.flatten_aggregated_list_results(paged_results,
item_key),
key=lambda d: d.get(sort_key, ''))
except (errors.HttpError, HttpLib2Error) as e:
api_not_enabled, details = _api_not_enabled(e)
if api_not_enabled:
raise api_errors.ApiNotEnabledError(details, e)
raise api_errors.ApiExecutionError(project_id, e)
# pylint: enable=invalid-name
def _flatten_list_results(project_id, paged_results, item_key):
"""Flatten results and handle exceptions.
Args:
project_id (str): The project id the results are for.
paged_results (list): A list of paged API response objects.
[{page 1 results}, {page 2 results}, {page 3 results}, ...]
item_key (str): The name of the key within the inner "items" lists
containing the objects of interest.
Returns:
list: A list of items.
Raises:
ApiNotEnabledError: Raised if the API is not enabled for the project.
ApiExecutionError: Raised if there is another error while calling the
API method.
"""
try:
return api_helpers.flatten_list_results(paged_results, item_key)
except (errors.HttpError, HttpLib2Error) as e:
api_not_enabled, details = _api_not_enabled(e)
if api_not_enabled:
raise api_errors.ApiNotEnabledError(details, e)
raise api_errors.ApiExecutionError(project_id, e)
# pylint: disable=too-many-instance-attributes