Search in sources :

Example 16 with OrganizationName

use of com.google.cloud.securitycenter.v1.OrganizationName in project gapic-generator-java by googleapis.

the class ConfigClientTest method listExclusionsTest3.

@Test
public void listExclusionsTest3() throws Exception {
    LogExclusion responsesElement = LogExclusion.newBuilder().build();
    ListExclusionsResponse expectedResponse = ListExclusionsResponse.newBuilder().setNextPageToken("").addAllExclusions(Arrays.asList(responsesElement)).build();
    mockConfigServiceV2.addResponse(expectedResponse);
    OrganizationName parent = OrganizationName.of("[ORGANIZATION]");
    ListExclusionsPagedResponse pagedListResponse = client.listExclusions(parent);
    List<LogExclusion> resources = Lists.newArrayList(pagedListResponse.iterateAll());
    Assert.assertEquals(1, resources.size());
    Assert.assertEquals(expectedResponse.getExclusionsList().get(0), resources.get(0));
    List<AbstractMessage> actualRequests = mockConfigServiceV2.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    ListExclusionsRequest actualRequest = ((ListExclusionsRequest) actualRequests.get(0));
    Assert.assertEquals(parent.toString(), actualRequest.getParent());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : OrganizationName(com.google.logging.v2.OrganizationName) ListExclusionsRequest(com.google.logging.v2.ListExclusionsRequest) AbstractMessage(com.google.protobuf.AbstractMessage) ListExclusionsResponse(com.google.logging.v2.ListExclusionsResponse) LogExclusion(com.google.logging.v2.LogExclusion) ListExclusionsPagedResponse(com.google.cloud.logging.v2.ConfigClient.ListExclusionsPagedResponse) Test(org.junit.Test)

Example 17 with OrganizationName

use of com.google.cloud.securitycenter.v1.OrganizationName in project gapic-generator-java by googleapis.

the class ConfigClientTest method createSinkExceptionTest3.

@Test
public void createSinkExceptionTest3() throws Exception {
    StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
    mockConfigServiceV2.addException(exception);
    try {
        OrganizationName parent = OrganizationName.of("[ORGANIZATION]");
        LogSink sink = LogSink.newBuilder().build();
        client.createSink(parent, sink);
        Assert.fail("No exception raised");
    } catch (InvalidArgumentException e) {
    // Expected exception.
    }
}
Also used : OrganizationName(com.google.logging.v2.OrganizationName) LogSink(com.google.logging.v2.LogSink) InvalidArgumentException(com.google.api.gax.rpc.InvalidArgumentException) StatusRuntimeException(io.grpc.StatusRuntimeException) Test(org.junit.Test)

Example 18 with OrganizationName

use of com.google.cloud.securitycenter.v1.OrganizationName in project gapic-generator-java by googleapis.

the class ConfigClientTest method createExclusionExceptionTest3.

@Test
public void createExclusionExceptionTest3() throws Exception {
    StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
    mockConfigServiceV2.addException(exception);
    try {
        OrganizationName parent = OrganizationName.of("[ORGANIZATION]");
        LogExclusion exclusion = LogExclusion.newBuilder().build();
        client.createExclusion(parent, exclusion);
        Assert.fail("No exception raised");
    } catch (InvalidArgumentException e) {
    // Expected exception.
    }
}
Also used : OrganizationName(com.google.logging.v2.OrganizationName) InvalidArgumentException(com.google.api.gax.rpc.InvalidArgumentException) LogExclusion(com.google.logging.v2.LogExclusion) StatusRuntimeException(io.grpc.StatusRuntimeException) Test(org.junit.Test)

Example 19 with OrganizationName

use of com.google.cloud.securitycenter.v1.OrganizationName in project google-cloud-java by googleapis.

the class SourceSnippets method listSources.

// [END securitycenter_create_source]
/**
 * List sources under an organization.
 *
 * @param organizationName The organization for the source.
 */
// [START securitycenter_list_sources]
static ImmutableList<Source> listSources(OrganizationName organizationName) {
    try (SecurityCenterClient client = SecurityCenterClient.create()) {
        // Start setting up a request to list sources in an organization.
        // OrganizationName organizationName = OrganizationName.of(/*organizationId=*/"123234324");
        ListSourcesRequest.Builder request = ListSourcesRequest.newBuilder().setParent(organizationName.toString());
        // Call the API.
        ListSourcesPagedResponse response = client.listSources(request.build());
        // This creates one list for all sources.  If your organization has a large number of sources
        // this can cause out of memory issues.  You can process them batches by returning
        // the Iterable returned response.iterateAll() directly.
        ImmutableList<Source> results = ImmutableList.copyOf(response.iterateAll());
        System.out.println("Sources:");
        System.out.println(results);
        return results;
    } catch (IOException e) {
        throw new RuntimeException("Couldn't create client.", e);
    }
}
Also used : ListSourcesRequest(com.google.cloud.securitycenter.v1.ListSourcesRequest) IOException(java.io.IOException) SecurityCenterClient(com.google.cloud.securitycenter.v1.SecurityCenterClient) ListSourcesPagedResponse(com.google.cloud.securitycenter.v1.SecurityCenterClient.ListSourcesPagedResponse) Source(com.google.cloud.securitycenter.v1.Source)

Example 20 with OrganizationName

use of com.google.cloud.securitycenter.v1.OrganizationName in project google-cloud-java by googleapis.

the class AssetSnippets method listAssetsAsOfYesterday.

// [END securitycenter_list_assets_with_filter]
/**
 * Lists all project assets for an organization at a given point in time.
 *
 * @param organizationName The organization to list assets for.
 * @param asOf The snapshot time to query for assets. If null defaults to one day ago.
 */
// [START securitycenter_list_assets_at_time]
static ImmutableList<ListAssetsResult> listAssetsAsOfYesterday(OrganizationName organizationName, Instant asOf) {
    try (SecurityCenterClient client = SecurityCenterClient.create()) {
        // Start setting up a request for to search for all assets in an organization.
        // OrganizationName organizationName = OrganizationName.of(/*organizationId=*/"123234324");
        // Initialize the builder with the organization and filter
        ListAssetsRequest.Builder request = ListAssetsRequest.newBuilder().setParent(organizationName.toString()).setFilter("security_center_properties.resource_type=\"google.cloud.resourcemanager.Project\"");
        // Set read time to either the instant passed in or one day ago.
        asOf = MoreObjects.firstNonNull(asOf, Instant.now().minus(Duration.ofDays(1)));
        request.getReadTimeBuilder().setSeconds(asOf.getEpochSecond()).setNanos(asOf.getNano());
        // Call the API.
        ListAssetsPagedResponse response = client.listAssets(request.build());
        // This creates one list for all assets.  If your organization has a large number of assets
        // this can cause out of memory issues.  You can process them incrementally by returning
        // the Iterable returned response.iterateAll() directly.
        ImmutableList<ListAssetsResult> results = ImmutableList.copyOf(response.iterateAll());
        System.out.println("Projects:");
        System.out.println(results);
        return results;
    } catch (IOException e) {
        throw new RuntimeException("Couldn't create client.", e);
    }
}
Also used : ListAssetsRequest(com.google.cloud.securitycenter.v1.ListAssetsRequest) ListAssetsResult(com.google.cloud.securitycenter.v1.ListAssetsResponse.ListAssetsResult) ListAssetsPagedResponse(com.google.cloud.securitycenter.v1.SecurityCenterClient.ListAssetsPagedResponse) IOException(java.io.IOException) SecurityCenterClient(com.google.cloud.securitycenter.v1.SecurityCenterClient)

Aggregations

Test (org.junit.Test)66 AbstractMessage (com.google.protobuf.AbstractMessage)35 InvalidArgumentException (com.google.api.gax.rpc.InvalidArgumentException)31 SecurityCenterClient (com.google.cloud.securitycenter.v1.SecurityCenterClient)31 StatusRuntimeException (io.grpc.StatusRuntimeException)31 OrganizationName (com.google.monitoring.v3.OrganizationName)30 IOException (java.io.IOException)30 OrganizationName (com.google.logging.v2.OrganizationName)22 OrganizationName (com.google.privacy.dlp.v2.OrganizationName)12 ListAssetsRequest (com.google.cloud.securitycenter.v1.ListAssetsRequest)10 ListAssetsResult (com.google.cloud.securitycenter.v1.ListAssetsResponse.ListAssetsResult)10 ListAssetsPagedResponse (com.google.cloud.securitycenter.v1.SecurityCenterClient.ListAssetsPagedResponse)10 GroupResult (com.google.cloud.securitycenter.v1.GroupResult)8 LogExclusion (com.google.logging.v2.LogExclusion)7 LogSink (com.google.logging.v2.LogSink)7 ArrayList (java.util.ArrayList)7 GroupAssetsRequest (com.google.cloud.securitycenter.v1.GroupAssetsRequest)6 GroupAssetsPagedResponse (com.google.cloud.securitycenter.v1.SecurityCenterClient.GroupAssetsPagedResponse)6 MetricDescriptor (com.google.api.MetricDescriptor)3 MockGrpcService (com.google.api.gax.grpc.testing.MockGrpcService)3