Mendapatkan modul kustom Security Health Analytics

Contoh kode untuk mendapatkan modul kustom Security Health Analytics.

Contoh kode

Java

Untuk melakukan autentikasi ke Security Command Center, siapkan Kredensial Default Aplikasi. Untuk mengetahui informasi selengkapnya, baca Menyiapkan autentikasi untuk lingkungan pengembangan lokal.

import com.google.cloud.securitycentermanagement.v1.GetSecurityHealthAnalyticsCustomModuleRequest;
import com.google.cloud.securitycentermanagement.v1.SecurityCenterManagementClient;
import com.google.cloud.securitycentermanagement.v1.SecurityHealthAnalyticsCustomModule;
import java.io.IOException;

public class GetSecurityHealthAnalyticsCustomModule {

  public static void main(String[] args) throws IOException {
    // https://cloud.google.com/security-command-center/docs/reference/security-center-management/rest/v1/organizations.locations.securityHealthAnalyticsCustomModules/get
    // TODO: Developer should replace project_id with a real project ID before running this code
    String projectId = "project_id";

    String customModuleId = "custom_module_id";

    getSecurityHealthAnalyticsCustomModule(projectId, customModuleId);
  }

  public static SecurityHealthAnalyticsCustomModule getSecurityHealthAnalyticsCustomModule(
      String projectId, String customModuleId) throws IOException {

    // Initialize client that will be used to send requests. This client only needs
    // to be created
    // once, and can be reused for multiple requests.
    try (SecurityCenterManagementClient client = SecurityCenterManagementClient.create()) {

      String name =
          String.format(
              "projects/%s/locations/global/securityHealthAnalyticsCustomModules/%s",
              projectId, customModuleId);

      GetSecurityHealthAnalyticsCustomModuleRequest request =
          GetSecurityHealthAnalyticsCustomModuleRequest.newBuilder().setName(name).build();

      SecurityHealthAnalyticsCustomModule response =
          client.getSecurityHealthAnalyticsCustomModule(request);

      return response;
    }
  }
}

Node.js

Untuk melakukan autentikasi ke Security Command Center, siapkan Kredensial Default Aplikasi. Untuk mengetahui informasi selengkapnya, baca Menyiapkan autentikasi untuk lingkungan pengembangan lokal.

// npm install '@google-cloud/securitycentermanagement'
const {
  SecurityCenterManagementClient,
} = require('@google-cloud/securitycentermanagement');

const client = new SecurityCenterManagementClient();

/*
 * Required. Resource name of security health analytics module.
 *     Its format is
 *    `organizations/[organization_id]/locations/[location_id]/securityHealthAnalyticsCustomModules/[custom_module]`
 *    `folders/[folder_id]/locations/[location_id]/securityHealthAnalyticsCustomModules/[custom_module]`
 *    `projects/[project_id]/locations/[location_id]/securityHealthAnalyticsCustomModules/[custom_module]`
 */
const name = `organizations/${organizationId}/locations/${locationId}/securityHealthAnalyticsCustomModules/${customModuleId}`;

async function getSecurityHealthAnalyticsCustomModule() {
  const [response] = await client.getSecurityHealthAnalyticsCustomModule({
    name: name,
  });
  console.log(
    'Security Health Analytics Custom Module get succeeded: ',
    response
  );
}

getSecurityHealthAnalyticsCustomModule();

Python

Untuk melakukan autentikasi ke Security Command Center, siapkan Kredensial Default Aplikasi. Untuk mengetahui informasi selengkapnya, baca Menyiapkan autentikasi untuk lingkungan pengembangan lokal.

def get_security_health_analytics_custom_module(parent: str, module_id: str):
    """
    Retrieves a Security Health Analytics custom module.
    Args:
        parent: Use any one of the following options:
                - organizations/{organization_id}/locations/{location_id}
                - folders/{folder_id}/locations/{location_id}
                - projects/{project_id}/locations/{location_id}
    Returns:
        The retrieved Security Health Analytics custom module.
    Raises:
        NotFound: If the specified custom module does not exist.
    """
    client = securitycentermanagement_v1.SecurityCenterManagementClient()

    try:
        request = securitycentermanagement_v1.GetSecurityHealthAnalyticsCustomModuleRequest(
            name=f"{parent}/securityHealthAnalyticsCustomModules/{module_id}",
        )

        response = client.get_security_health_analytics_custom_module(request=request)
        print(f"Retrieved Security Health Analytics Custom Module: {response.name}")
        return response
    except NotFound as e:
        print(f"Custom Module not found: {response.name}")
        raise e

Langkah berikutnya

Untuk menelusuri dan memfilter contoh kode untuk produk Google Cloud lainnya, lihat Google Cloud browser contoh.