コード例 #1
0
def MakeGetAssetsHistoryHttpRequests(args,
                                     service,
                                     api_version=DEFAULT_API_VERSION):
    """Manually make the get assets history request."""
    messages = GetMessages(api_version)

    encoding.AddCustomJsonFieldMapping(
        messages.CloudassetBatchGetAssetsHistoryRequest,
        'readTimeWindow_startTime', 'readTimeWindow.startTime')
    encoding.AddCustomJsonFieldMapping(
        messages.CloudassetBatchGetAssetsHistoryRequest,
        'readTimeWindow_endTime', 'readTimeWindow.endTime')

    content_type = arg_utils.ChoiceToEnum(
        args.content_type, messages.CloudassetBatchGetAssetsHistoryRequest.
        ContentTypeValueValuesEnum)
    parent = asset_utils.GetParentNameForGetHistory(args.organization,
                                                    args.project)
    start_time = times.FormatDateTime(args.start_time)
    end_time = None
    if args.IsSpecified('end_time'):
        end_time = times.FormatDateTime(args.end_time)

    response = service.BatchGetAssetsHistory(
        messages.CloudassetBatchGetAssetsHistoryRequest(
            assetNames=args.asset_names,
            relationshipTypes=args.relationship_types,
            contentType=content_type,
            parent=parent,
            readTimeWindow_endTime=end_time,
            readTimeWindow_startTime=start_time,
        ))

    for asset in response.assets:
        yield asset
コード例 #2
0
def MakeGetAssetsHistoryHttpRequests(args, api_version=DEFAULT_API_VERSION):
    """Manually make the get assets history request."""
    http_client = http.Http()
    query_params = [('assetNames', asset_name)
                    for asset_name in args.asset_names or []]
    query_params.extend([
        ('contentType', ContentTypeTranslation(args.content_type)),
        ('readTimeWindow.startTime', times.FormatDateTime(args.start_time))
    ])
    if args.IsSpecified('end_time'):
        query_params.extend([('readTimeWindow.endTime',
                              times.FormatDateTime(args.end_time))])
    parent = asset_utils.GetParentNameForGetHistory(args.organization,
                                                    args.project)
    endpoint = apis.GetEffectiveApiEndpoint(API_NAME, api_version)
    url = '{0}{1}/{2}:{3}'.format(endpoint, api_version, parent,
                                  'batchGetAssetsHistory')
    encoded_query_params = six.moves.urllib.parse.urlencode(query_params)
    response, raw_content = http_client.request(uri=url,
                                                headers=_HEADERS,
                                                method='POST',
                                                body=encoded_query_params)

    content = core_encoding.Decode(raw_content)

    if int(response['status']) != httplib.OK:
        http_error = api_exceptions.HttpError(response, content, url)
        raise exceptions.HttpException(http_error)

    response_message_class = GetMessages(
        api_version).BatchGetAssetsHistoryResponse
    try:
        history_response = encoding.JsonToMessage(response_message_class,
                                                  content)
    except ValueError as e:
        err_msg = ('Failed receiving proper response from server, cannot'
                   'parse received assets. Error details: ' + six.text_type(e))
        raise MessageDecodeError(err_msg)

    for asset in history_response.assets:
        yield asset
コード例 #3
0
 def testGetParentNameForGetHistory_Project(self):
     self.assertEqual(utils.GetParentNameForGetHistory(None, 'project_id'),
                      'projects/project_id')
コード例 #4
0
 def testGetParentNameForGetHistory_Organization(self):
     self.assertEqual(utils.GetParentNameForGetHistory('org_id', None),
                      'organizations/org_id')