Search in sources :

Example 66 with OrganizationName

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

the class FindingSnippets method listAllFindings.

// [END securitycenter_update_finding_state]
/**
 * List all findings under an organization.
 *
 * @param organizationName The source to list all findings for.
 */
// [START securitycenter_list_all_findings]
static ImmutableList<ListFindingsResult> listAllFindings(OrganizationName organizationName) {
    try (SecurityCenterClient client = SecurityCenterClient.create()) {
        // OrganizationName organizationName = OrganizationName.of(/*organizationId=*/"123234324");
        // "-" Indicates listing across all sources.
        SourceName sourceName = SourceName.of(organizationName.getOrganization(), "-");
        ListFindingsRequest.Builder request = ListFindingsRequest.newBuilder().setParent(sourceName.toString());
        // Call the API.
        ListFindingsPagedResponse response = client.listFindings(request.build());
        // This creates one list for all findings.  If your organization has a large number of
        // findings this can cause out of memory issues.  You can process them in incrementally
        // by returning the Iterable returned response.iterateAll() directly.
        ImmutableList<ListFindingsResult> results = ImmutableList.copyOf(response.iterateAll());
        System.out.println("Findings:");
        System.out.println(results);
        return results;
    } catch (IOException e) {
        throw new RuntimeException("Couldn't create client.", e);
    }
}
Also used : SourceName(com.google.cloud.securitycenter.v1.SourceName) ListFindingsRequest(com.google.cloud.securitycenter.v1.ListFindingsRequest) IOException(java.io.IOException) SecurityCenterClient(com.google.cloud.securitycenter.v1.SecurityCenterClient) ListFindingsPagedResponse(com.google.cloud.securitycenter.v1.SecurityCenterClient.ListFindingsPagedResponse) ListFindingsResult(com.google.cloud.securitycenter.v1.ListFindingsResponse.ListFindingsResult)

Example 67 with OrganizationName

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

the class OrganizationSnippets method getOrganizationSettings.

/**
 * Gets current settings for an organization.
 *
 * @param organizationName The organization to get settings for.
 */
// [START securitycenter_get_org_settings]
static OrganizationSettings getOrganizationSettings(OrganizationName organizationName) {
    try (SecurityCenterClient client = SecurityCenterClient.create()) {
        // Start setting up a request to get OrganizationSettings for.
        // OrganizationName organizationName = OrganizationName.of(/*organizationId=*/"123234324");
        GetOrganizationSettingsRequest.Builder request = GetOrganizationSettingsRequest.newBuilder().setName(organizationName.toString() + "/organizationSettings");
        // Call the API.
        OrganizationSettings response = client.getOrganizationSettings(request.build());
        System.out.println("Organization Settings:");
        System.out.println(response);
        return response;
    } catch (IOException e) {
        throw new RuntimeException("Couldn't create client.", e);
    }
}
Also used : GetOrganizationSettingsRequest(com.google.cloud.securitycenter.v1.GetOrganizationSettingsRequest) OrganizationSettings(com.google.cloud.securitycenter.v1.OrganizationSettings) IOException(java.io.IOException) SecurityCenterClient(com.google.cloud.securitycenter.v1.SecurityCenterClient)

Example 68 with OrganizationName

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

the class OrganizationSnippets method updateOrganizationSettings.

// [END securitycenter_get_org_settings]
/**
 * Update Asset Discovery OrganizationSettings for an organization
 *
 * @param organizationName The organization to update settings for.
 */
// [START securitycenter_enable_asset_discovery]
static OrganizationSettings updateOrganizationSettings(OrganizationName organizationName) {
    try (SecurityCenterClient client = SecurityCenterClient.create()) {
        // Start setting up a request to update OrganizationSettings for.
        // OrganizationName organizationName = OrganizationName.of(/*organizationId=*/"123234324");
        OrganizationSettings organizationSettings = OrganizationSettings.newBuilder().setName(organizationName.toString() + "/organizationSettings").setEnableAssetDiscovery(true).build();
        FieldMask updateMask = FieldMask.newBuilder().addPaths("enable_asset_discovery").build();
        UpdateOrganizationSettingsRequest.Builder request = UpdateOrganizationSettingsRequest.newBuilder().setOrganizationSettings(organizationSettings).setUpdateMask(updateMask);
        // Call the API.
        OrganizationSettings response = client.updateOrganizationSettings(request.build());
        System.out.println("Organization Settings have been updated:");
        System.out.println(response);
        return response;
    } catch (IOException e) {
        throw new RuntimeException("Couldn't create client.", e);
    }
}
Also used : UpdateOrganizationSettingsRequest(com.google.cloud.securitycenter.v1.UpdateOrganizationSettingsRequest) OrganizationSettings(com.google.cloud.securitycenter.v1.OrganizationSettings) IOException(java.io.IOException) SecurityCenterClient(com.google.cloud.securitycenter.v1.SecurityCenterClient) FieldMask(com.google.protobuf.FieldMask)

Example 69 with OrganizationName

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

the class NotificationChannelServiceClientTest method listNotificationChannelDescriptorsTest2.

@Test
public void listNotificationChannelDescriptorsTest2() throws Exception {
    NotificationChannelDescriptor responsesElement = NotificationChannelDescriptor.newBuilder().build();
    ListNotificationChannelDescriptorsResponse expectedResponse = ListNotificationChannelDescriptorsResponse.newBuilder().setNextPageToken("").addAllChannelDescriptors(Arrays.asList(responsesElement)).build();
    mockNotificationChannelService.addResponse(expectedResponse);
    OrganizationName name = OrganizationName.of("[ORGANIZATION]");
    ListNotificationChannelDescriptorsPagedResponse pagedListResponse = client.listNotificationChannelDescriptors(name);
    List<NotificationChannelDescriptor> resources = Lists.newArrayList(pagedListResponse.iterateAll());
    Assert.assertEquals(1, resources.size());
    Assert.assertEquals(expectedResponse.getChannelDescriptorsList().get(0), resources.get(0));
    List<AbstractMessage> actualRequests = mockNotificationChannelService.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    ListNotificationChannelDescriptorsRequest actualRequest = ((ListNotificationChannelDescriptorsRequest) actualRequests.get(0));
    Assert.assertEquals(name.toString(), actualRequest.getName());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : OrganizationName(com.google.monitoring.v3.OrganizationName) ListNotificationChannelDescriptorsResponse(com.google.monitoring.v3.ListNotificationChannelDescriptorsResponse) AbstractMessage(com.google.protobuf.AbstractMessage) NotificationChannelDescriptor(com.google.monitoring.v3.NotificationChannelDescriptor) ListNotificationChannelDescriptorsRequest(com.google.monitoring.v3.ListNotificationChannelDescriptorsRequest) ListNotificationChannelDescriptorsPagedResponse(com.google.cloud.monitoring.v3.NotificationChannelServiceClient.ListNotificationChannelDescriptorsPagedResponse) Test(org.junit.Test)

Example 70 with OrganizationName

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

the class NotificationChannelServiceClientTest method listNotificationChannelDescriptorsExceptionTest2.

@Test
public void listNotificationChannelDescriptorsExceptionTest2() throws Exception {
    StatusRuntimeException exception = new StatusRuntimeException(io.grpc.Status.INVALID_ARGUMENT);
    mockNotificationChannelService.addException(exception);
    try {
        OrganizationName name = OrganizationName.of("[ORGANIZATION]");
        client.listNotificationChannelDescriptors(name);
        Assert.fail("No exception raised");
    } catch (InvalidArgumentException e) {
    // Expected exception.
    }
}
Also used : OrganizationName(com.google.monitoring.v3.OrganizationName) InvalidArgumentException(com.google.api.gax.rpc.InvalidArgumentException) StatusRuntimeException(io.grpc.StatusRuntimeException) Test(org.junit.Test)

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