列出 Security Health Analytics 自訂模組

列出 Security Health Analytics 自訂模組的程式碼範例。

程式碼範例

Java

如要向 Security Command Center 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。

import com.google.cloud.securitycentermanagement.v1.ListSecurityHealthAnalyticsCustomModulesRequest;
import com.google.cloud.securitycentermanagement.v1.SecurityCenterManagementClient;
import com.google.cloud.securitycentermanagement.v1.SecurityCenterManagementClient.ListSecurityHealthAnalyticsCustomModulesPagedResponse;
import java.io.IOException;

public class ListSecurityHealthAnalyticsCustomModules {

  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/list
    // TODO: Developer should replace project_id with a real project ID before running this code
    String projectId = "project_id";

    listSecurityHealthAnalyticsCustomModules(projectId);
  }

  public static ListSecurityHealthAnalyticsCustomModulesPagedResponse
      listSecurityHealthAnalyticsCustomModules(String projectId) 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()) {

      ListSecurityHealthAnalyticsCustomModulesRequest request =
          ListSecurityHealthAnalyticsCustomModulesRequest.newBuilder()
              .setParent(String.format("projects/%s/locations/global", projectId))
              .build();

      ListSecurityHealthAnalyticsCustomModulesPagedResponse response =
          client.listSecurityHealthAnalyticsCustomModules(request);

      return response;
    }
  }
}

Node.js

如要向 Security Command Center 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。

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

const client = new SecurityCenterManagementClient();

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

async function listSecurityHealthAnalyticsCustomModule() {
  const [response] = await client.listSecurityHealthAnalyticsCustomModules({
    parent: parent,
  });
  console.log(
    'Security Health Analytics Custom Module list succeeded: ',
    response
  );
}

listSecurityHealthAnalyticsCustomModule();

Python

如要向 Security Command Center 進行驗證,請設定應用程式預設憑證。 詳情請參閱「為本機開發環境設定驗證」。

def list_security_health_analytics_custom_module(parent: str):
    """
    Retrieves list of 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:
        List of retrieved Security Health Analytics custom modules.
    Raises:
        NotFound: If the specified custom module does not exist.
    """

    client = securitycentermanagement_v1.SecurityCenterManagementClient()

    try:
        request = securitycentermanagement_v1.ListSecurityHealthAnalyticsCustomModulesRequest(
            parent=parent,
        )

        response = client.list_security_health_analytics_custom_modules(request=request)

        custom_modules = []
        for custom_module in response:
            print(f"Custom Module: {custom_module.name}")
            custom_modules.append(custom_module)
        return custom_modules
    except NotFound as e:
        print(f"Parent resource not found: {parent}")
        raise e
    except Exception as e:
        print(f"An error occurred while listing custom modules: {e}")
        raise e

後續步驟

如要搜尋及篩選其他 Google Cloud 產品的程式碼範例,請參閱Google Cloud 範例瀏覽器