Search in sources :

Example 91 with OrganizationName

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

the class ConfigClientTest method createExclusionTest3.

@Test
public void createExclusionTest3() throws Exception {
    LogExclusion expectedResponse = LogExclusion.newBuilder().setName(LogExclusionName.ofProjectExclusionName("[PROJECT]", "[EXCLUSION]").toString()).setDescription("description-1724546052").setFilter("filter-1274492040").setDisabled(true).setCreateTime(Timestamp.newBuilder().build()).setUpdateTime(Timestamp.newBuilder().build()).build();
    mockConfigServiceV2.addResponse(expectedResponse);
    OrganizationName parent = OrganizationName.of("[ORGANIZATION]");
    LogExclusion exclusion = LogExclusion.newBuilder().build();
    LogExclusion actualResponse = client.createExclusion(parent, exclusion);
    Assert.assertEquals(expectedResponse, actualResponse);
    List<AbstractMessage> actualRequests = mockConfigServiceV2.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    CreateExclusionRequest actualRequest = ((CreateExclusionRequest) actualRequests.get(0));
    Assert.assertEquals(parent.toString(), actualRequest.getParent());
    Assert.assertEquals(exclusion, actualRequest.getExclusion());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : OrganizationName(com.google.logging.v2.OrganizationName) CreateExclusionRequest(com.google.logging.v2.CreateExclusionRequest) AbstractMessage(com.google.protobuf.AbstractMessage) LogExclusion(com.google.logging.v2.LogExclusion) Test(org.junit.Test)

Example 92 with OrganizationName

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

the class ConfigClientTest method listSinksTest3.

@Test
public void listSinksTest3() throws Exception {
    LogSink responsesElement = LogSink.newBuilder().build();
    ListSinksResponse expectedResponse = ListSinksResponse.newBuilder().setNextPageToken("").addAllSinks(Arrays.asList(responsesElement)).build();
    mockConfigServiceV2.addResponse(expectedResponse);
    OrganizationName parent = OrganizationName.of("[ORGANIZATION]");
    ListSinksPagedResponse pagedListResponse = client.listSinks(parent);
    List<LogSink> resources = Lists.newArrayList(pagedListResponse.iterateAll());
    Assert.assertEquals(1, resources.size());
    Assert.assertEquals(expectedResponse.getSinksList().get(0), resources.get(0));
    List<AbstractMessage> actualRequests = mockConfigServiceV2.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    ListSinksRequest actualRequest = ((ListSinksRequest) 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) LogSink(com.google.logging.v2.LogSink) ListSinksResponse(com.google.logging.v2.ListSinksResponse) AbstractMessage(com.google.protobuf.AbstractMessage) ListSinksPagedResponse(com.google.cloud.logging.v2.ConfigClient.ListSinksPagedResponse) ListSinksRequest(com.google.logging.v2.ListSinksRequest) Test(org.junit.Test)

Example 93 with OrganizationName

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

the class AssetSnippets method groupAssetsWithFilter.

// [END securitycenter_group_all_assets]
/**
 * Filters all assets by their specified properties and groups them by specified properties for an
 * organization.
 *
 * @param organizationName The organization to group assets for.
 */
// [START securitycenter_group_all_assets_with_filter]
static ImmutableList<GroupResult> groupAssetsWithFilter(OrganizationName organizationName) {
    try (SecurityCenterClient client = SecurityCenterClient.create()) {
        // Start setting up a request for to filter all assets by type and group them by project in an
        // organization.
        // OrganizationName organizationName = OrganizationName.of("123234324");
        GroupAssetsRequest.Builder request = GroupAssetsRequest.newBuilder().setFilter("security_center_properties.resource_type=\"google.cloud.resourcemanager.Project\"").setGroupBy("security_center_properties.resource_project").setParent(organizationName.toString());
        // Call the API.
        GroupAssetsPagedResponse response = client.groupAssets(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 batches by returning
        // the Iterable returned response.iterateAll() directly.
        ImmutableList<GroupResult> results = ImmutableList.copyOf(response.iterateAll());
        System.out.println("All assets:");
        System.out.println(results);
        return results;
    } catch (IOException e) {
        throw new RuntimeException("Couldn't create client.", e);
    }
}
Also used : GroupAssetsPagedResponse(com.google.cloud.securitycenter.v1.SecurityCenterClient.GroupAssetsPagedResponse) GroupResult(com.google.cloud.securitycenter.v1.GroupResult) IOException(java.io.IOException) SecurityCenterClient(com.google.cloud.securitycenter.v1.SecurityCenterClient) GroupAssetsRequest(com.google.cloud.securitycenter.v1.GroupAssetsRequest)

Example 94 with OrganizationName

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

the class AssetSnippets method listAssets.

/**
 * Lists all assets for an organization.
 *
 * @param organizationName The organization to list assets for.
 */
// [START securitycenter_list_all_assets]
static ImmutableList<ListAssetsResult> listAssets(OrganizationName organizationName) {
    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");
        ListAssetsRequest.Builder request = ListAssetsRequest.newBuilder().setParent(organizationName.toString());
        // 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("All assets:");
        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)

Example 95 with OrganizationName

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

the class AssetSnippets method listAssetAndStatusChanges.

// [END securitycenter_list_assets_at_time]
/**
 * Returns Assets and metadata about assets activity (e.g. added, removed, no change) between
 * between <code>asOf.minus(timespan)</code> and <code>asOf</code>.
 *
 * @param timeSpan The time-range to compare assets over.
 * @param asOf The instant in time to query for. If null, current time is assumed.
 */
// [START securitycenter_list_assets_and_changes]
static ImmutableList<ListAssetsResult> listAssetAndStatusChanges(OrganizationName organizationName, Duration timeSpan, 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");
        ListAssetsRequest.Builder request = ListAssetsRequest.newBuilder().setParent(organizationName.toString()).setFilter("security_center_properties.resource_type=\"google.cloud.resourcemanager.Project\"");
        request.getCompareDurationBuilder().setSeconds(timeSpan.getSeconds()).setNanos(timeSpan.getNano());
        // Set read time to either the instant passed in or now.
        asOf = MoreObjects.firstNonNull(asOf, Instant.now());
        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