Example #1
0
    def test_update_feed(self):
        # Setup Expected Response
        name = "name3373707"
        expected_response = {"name": name}
        expected_response = asset_service_pb2.Feed(**expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[expected_response])
        patch = mock.patch("google.api_core.grpc_helpers.create_channel")
        with patch as create_channel:
            create_channel.return_value = channel
            client = asset_v1.AssetServiceClient()

        # Setup Request
        feed = {}
        update_mask = {}

        response = client.update_feed(feed, update_mask)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = asset_service_pb2.UpdateFeedRequest(
            feed=feed, update_mask=update_mask)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
Example #2
0
    def test_create_feed(self):
        # Setup Expected Response
        name = "name3373707"
        expected_response = {"name": name}
        expected_response = asset_service_pb2.Feed(**expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[expected_response])
        patch = mock.patch("google.api_core.grpc_helpers.create_channel")
        with patch as create_channel:
            create_channel.return_value = channel
            client = asset_v1.AssetServiceClient()

        # Setup Request
        parent = "parent-995424086"
        feed_id = "feedId-976011428"
        feed = {}

        response = client.create_feed(parent, feed_id, feed)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = asset_service_pb2.CreateFeedRequest(parent=parent,
                                                               feed_id=feed_id,
                                                               feed=feed)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
Example #3
0
def _main(project=None,
          bq_project=None,
          bq_dataset=None,
          bq_table=None,
          read_time=None,
          verbose=False):
    'Module entry point used by cli and cloud function wrappers.'

    _configure_logging(verbose)
    if not read_time:
        read_time = datetime.datetime.now()
    client = asset_v1.AssetServiceClient()
    parent = 'projects/%s' % project
    content_type = asset_v1.ContentType.RESOURCE
    output_config = asset_v1.OutputConfig()
    output_config.bigquery_destination.dataset = 'projects/%s/datasets/%s' % (
        bq_project, bq_dataset)
    output_config.bigquery_destination.table = '%s_%s' % (
        bq_table, read_time.strftime('%Y%m%d'))
    output_config.bigquery_destination.force = True
    try:
        response = client.export_assets(
            request={
                'parent': parent,
                'read_time': read_time,
                'content_type': content_type,
                'output_config': output_config
            })
    except (GoogleAPIError, googleapiclient.errors.HttpError) as e:
        logging.debug('API Error: %s', e, exc_info=True)
        raise RuntimeError(
            'Error fetching Asset Inventory entries (project: %s)' % parent, e)
def analyze_iam_policy_longrunning_gcs(project_id, dump_file_path):
    # [START asset_quickstart_analyze_iam_policy_longrunning_gcs]
    from google.cloud import asset_v1

    # TODO project_id = 'Your Google Cloud Project ID'
    # TODO dump_file_path = 'Your analysis dump file path'

    client = asset_v1.AssetServiceClient()
    parent = "projects/{}".format(project_id)

    # Build analysis query
    analysis_query = asset_v1.IamPolicyAnalysisQuery()
    analysis_query.scope = parent
    analysis_query.resource_selector.full_resource_name = f"//cloudresourcemanager.googleapis.com/{parent}"
    analysis_query.options.expand_groups = True
    analysis_query.options.output_group_edges = True

    output_config = asset_v1.IamPolicyAnalysisOutputConfig()
    output_config.gcs_destination.uri = dump_file_path
    operation = client.analyze_iam_policy_longrunning(
        request={
            "analysis_query": analysis_query,
            "output_config": output_config
        })

    operation.result(300)
    print(operation.done())
def analyze_iam_policy_longrunning_bigquery(project_id, dataset, table):
    # [START asset_quickstart_analyze_iam_policy_longrunning_bigquery]
    from google.cloud import asset_v1

    # TODO project_id = 'Your Google Cloud Project ID'
    # TODO dataset = 'Your BigQuery dataset path'
    # TODO table = 'Your BigQuery table name'

    client = asset_v1.AssetServiceClient()
    parent = "projects/{}".format(project_id)

    # Build analysis query
    analysis_query = asset_v1.IamPolicyAnalysisQuery()
    analysis_query.scope = parent
    analysis_query.resource_selector.full_resource_name = f"//cloudresourcemanager.googleapis.com/{parent}"
    analysis_query.options.expand_groups = True
    analysis_query.options.output_group_edges = True

    output_config = asset_v1.IamPolicyAnalysisOutputConfig()
    output_config.bigquery_destination.dataset = dataset
    output_config.bigquery_destination.table_prefix = table
    output_config.bigquery_destination.write_disposition = "WRITE_TRUNCATE"
    operation = client.analyze_iam_policy_longrunning(
        request={
            "analysis_query": analysis_query,
            "output_config": output_config
        })

    operation.result(300)
    print(operation.done())
def sample_analyze_iam_policy_longrunning():
    # Create a client
    client = asset_v1.AssetServiceClient()

    # Initialize request argument(s)
    analysis_query = asset_v1.IamPolicyAnalysisQuery()
    analysis_query.scope = "scope_value"

    output_config = asset_v1.IamPolicyAnalysisOutputConfig()
    output_config.gcs_destination.uri = "uri_value"

    request = asset_v1.AnalyzeIamPolicyLongrunningRequest(
        analysis_query=analysis_query,
        output_config=output_config,
    )

    # Make the request
    operation = client.analyze_iam_policy_longrunning(request=request)

    print("Waiting for operation to complete...")

    response = operation.result()

    # Handle the response
    print(response)
Example #7
0
def search_all_resources(
    scope, query=None, asset_types=None, page_size=None, order_by=None
):
    # [START asset_quickstart_search_all_resources]
    from google.cloud import asset_v1

    # TODO scope = 'Scope of the search'
    # TODO query = 'Query statement'
    # TODO asset_types = 'List of asset types to search for'
    # TODO page_size = Size of each result page
    # TODO order_by = 'Fields to sort the results'

    client = asset_v1.AssetServiceClient()
    response = client.search_all_resources(
        request={
            "scope": scope,
            "query": query,
            "asset_types": asset_types,
            "page_size": page_size,
            "order_by": order_by,
        }
    )
    for resource in response:
        print(resource)
        break
Example #8
0
    def test_batch_get_assets_history(self):
        # Setup Expected Response
        expected_response = {}
        expected_response = asset_service_pb2.BatchGetAssetsHistoryResponse(
            **expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[expected_response])
        patch = mock.patch("google.api_core.grpc_helpers.create_channel")
        with patch as create_channel:
            create_channel.return_value = channel
            client = asset_v1.AssetServiceClient()

        # Setup Request
        parent = "parent-995424086"
        content_type = enums.ContentType.CONTENT_TYPE_UNSPECIFIED
        read_time_window = {}

        response = client.batch_get_assets_history(parent, content_type,
                                                   read_time_window)
        assert expected_response == response

        assert len(channel.requests) == 1
        expected_request = asset_service_pb2.BatchGetAssetsHistoryRequest(
            parent=parent,
            content_type=content_type,
            read_time_window=read_time_window)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
Example #9
0
    def test_export_assets(self):
        # Setup Expected Response
        expected_response = {}
        expected_response = asset_service_pb2.ExportAssetsResponse(
            **expected_response)
        operation = operations_pb2.Operation(
            name="operations/test_export_assets", done=True)
        operation.response.Pack(expected_response)

        # Mock the API response
        channel = ChannelStub(responses=[operation])
        patch = mock.patch("google.api_core.grpc_helpers.create_channel")
        with patch as create_channel:
            create_channel.return_value = channel
            client = asset_v1.AssetServiceClient()

        # Setup Request
        parent = "parent-995424086"
        output_config = {}

        response = client.export_assets(parent, output_config)
        result = response.result()
        assert expected_response == result

        assert len(channel.requests) == 1
        expected_request = asset_service_pb2.ExportAssetsRequest(
            parent=parent, output_config=output_config)
        actual_request = channel.requests[0][1]
        assert expected_request == actual_request
def list_assets(project_id, asset_types, page_size):
    # [START asset_quickstart_list_assets]
    from google.cloud import asset_v1

    # TODO project_id = 'Your Google Cloud Project ID'
    # TODO asset_types = 'Your asset type list, e.g.,
    # ["storage.googleapis.com/Bucket","bigquery.googleapis.com/Table"]'
    # TODO page_size = 'Num of assets in one page, which must be between 1 and
    # 1000 (both inclusively)'

    project_resource = "projects/{}".format(project_id)
    content_type = asset_v1.ContentType.RESOURCE
    client = asset_v1.AssetServiceClient()

    # Call ListAssets v1 to list assets.
    response = client.list_assets(
        request={
            "parent": project_resource,
            "read_time": None,
            "asset_types": asset_types,
            "content_type": content_type,
            "page_size": page_size,
        })

    for asset in response:
        print(asset)
def get_feed(feed_name):
    # [START asset_quickstart_get_feed]
    from google.cloud import asset_v1

    # TODO feed_name = 'Feed Name you want to get'

    client = asset_v1.AssetServiceClient()
    response = client.get_feed(request={"name": feed_name})
    print("gotten_feed: {}".format(response))
def delete_feed(feed_name):
    # [START asset_quickstart_delete_feed]
    from google.cloud import asset_v1

    # TODO feed_name = 'Feed name you want to delete'

    client = asset_v1.AssetServiceClient()
    client.delete_feed(request={"name": feed_name})
    print("deleted_feed")
def sample_delete_feed():
    # Create a client
    client = asset_v1.AssetServiceClient()

    # Initialize request argument(s)
    request = asset_v1.DeleteFeedRequest(name="name_value", )

    # Make the request
    client.delete_feed(request=request)
Example #14
0
def list_feeds(parent_resource):
    # [START asset_quickstart_list_feeds]
    from google.cloud import asset_v1

    # TODO parent_resource = 'Parent resource you want to list all feeds'

    client = asset_v1.AssetServiceClient()
    response = client.list_feeds(parent_resource)
    print('feeds: {}'.format(response.feeds))
Example #15
0
def export_assets(id, dump_file_path):

    from google.cloud import asset_v1
    from google.cloud.asset_v1.proto import asset_service_pb2

    client = asset_v1.AssetServiceClient()
    output_config = asset_service_pb2.OutputConfig()
    output_config.gcs_destination.uri = dump_file_path
    response = client.export_assets(id, output_config, content_type='RESOURCE')
Example #16
0
 def test_export_assets(self):
     client = asset_v1.AssetServiceClient()
     output_config = {}
     parent_inside = "projects/" + PROJECT_INSIDE
     delayed_inside = lambda: client.export_assets(parent_inside,
                                                   output_config)
     parent_outside = "projects/" + PROJECT_OUTSIDE
     delayed_outside = lambda: client.export_assets(parent_outside,
                                                    output_config)
     TestVPCServiceControl._do_test(delayed_inside, delayed_outside)
Example #17
0
 def test_batch_get_assets_history(self):
     client = asset_v1.AssetServiceClient()
     content_type = enums.ContentType.CONTENT_TYPE_UNSPECIFIED
     read_time_window = {}
     parent_inside = "projects/" + PROJECT_INSIDE
     delayed_inside = lambda: client.batch_get_assets_history(
         parent_inside, content_type, read_time_window)
     parent_outside = "projects/" + PROJECT_OUTSIDE
     delayed_outside = lambda: client.batch_get_assets_history(
         parent_outside, content_type, read_time_window)
     TestVPCServiceControl._do_test(delayed_inside, delayed_outside)
def sample_delete_saved_query():
    # Create a client
    client = asset_v1.AssetServiceClient()

    # Initialize request argument(s)
    request = asset_v1.DeleteSavedQueryRequest(
        name="name_value",
    )

    # Make the request
    client.delete_saved_query(request=request)
Example #19
0
def sample_list_feeds():
    # Create a client
    client = asset_v1.AssetServiceClient()

    # Initialize request argument(s)
    request = asset_v1.ListFeedsRequest(parent="parent_value", )

    # Make the request
    response = client.list_feeds(request=request)

    # Handle the response
    print(response)
Example #20
0
def sample_batch_get_assets_history():
    # Create a client
    client = asset_v1.AssetServiceClient()

    # Initialize request argument(s)
    request = asset_v1.BatchGetAssetsHistoryRequest(parent="parent_value", )

    # Make the request
    response = client.batch_get_assets_history(request=request)

    # Handle the response
    print(response)
def search_all_iam_policies(scope, query=None, page_size=None):
    # [START asset_quickstart_search_all_iam_policies]
    from google.cloud import asset_v1

    client = asset_v1.AssetServiceClient()
    response = client.search_all_iam_policies(scope,
                                              query=query,
                                              page_size=page_size)
    for page in response.pages:
        for policy in page:
            print(policy)
        break
def sample_get_feed():
    # Create a client
    client = asset_v1.AssetServiceClient()

    # Initialize request argument(s)
    request = asset_v1.GetFeedRequest(name="name_value", )

    # Make the request
    response = client.get_feed(request=request)

    # Handle the response
    print(response)
def sample_delete_feed():
    """Snippet for delete_feed"""

    # Create a client
    client = asset_v1.AssetServiceClient()

    # Initialize request argument(s)
    request = asset_v1.DeleteFeedRequest(
    )

    # Make the request
    response = client.delete_feed(request=request)
Example #24
0
    def test_delete_feed_exception(self):
        # Mock the API response
        channel = ChannelStub(responses=[CustomException()])
        patch = mock.patch("google.api_core.grpc_helpers.create_channel")
        with patch as create_channel:
            create_channel.return_value = channel
            client = asset_v1.AssetServiceClient()

        # Setup request
        name = client.feed_path("[PROJECT]", "[FEED]")

        with pytest.raises(CustomException):
            client.delete_feed(name)
Example #25
0
def sample_search_all_iam_policies():
    # Create a client
    client = asset_v1.AssetServiceClient()

    # Initialize request argument(s)
    request = asset_v1.SearchAllIamPoliciesRequest(scope="scope_value", )

    # Make the request
    page_result = client.search_all_iam_policies(request=request)

    # Handle the response
    for response in page_result:
        print(response)
Example #26
0
    def test_list_feeds_exception(self):
        # Mock the API response
        channel = ChannelStub(responses=[CustomException()])
        patch = mock.patch("google.api_core.grpc_helpers.create_channel")
        with patch as create_channel:
            create_channel.return_value = channel
            client = asset_v1.AssetServiceClient()

        # Setup request
        parent = "parent-995424086"

        with pytest.raises(CustomException):
            client.list_feeds(parent)
Example #27
0
def sample_list_assets():
    """Snippet for list_assets"""

    # Create a client
    client = asset_v1.AssetServiceClient()

    # Initialize request argument(s)
    request = asset_v1.ListAssetsRequest()

    # Make the request
    page_result = client.list_assets(request=request)
    for response in page_result:
        print("{}".format(response))
Example #28
0
def get_all_projects_using_asset_manager(organization, credentials):
    """Returns project ids using asset manager apis.

  Args:
    organization: (str) organization/[ORGANIZATION_ID]
    credentials: client credentials
  """
    project_prefix = "//cloudresourcemanager.googleapis.com/projects/"
    client_v1 = asset_v1.AssetServiceClient(credentials=credentials)
    all_projects = client_v1.search_all_resources(
        scope=organization,
        asset_types=["cloudresourcemanager.googleapis.com/Project"])
    return [p.name[len(project_prefix):] for p in all_projects]
def sample_list_saved_queries():
    # Create a client
    client = asset_v1.AssetServiceClient()

    # Initialize request argument(s)
    request = asset_v1.ListSavedQueriesRequest(parent="parent_value", )

    # Make the request
    page_result = client.list_saved_queries(request=request)

    # Handle the response
    for response in page_result:
        print(response)
def sample_update_saved_query():
    # Create a client
    client = asset_v1.AssetServiceClient()

    # Initialize request argument(s)
    request = asset_v1.UpdateSavedQueryRequest(
    )

    # Make the request
    response = client.update_saved_query(request=request)

    # Handle the response
    print(response)