Group assets
Stay organized with collections
Save and categorize content based on your preferences.
Demonstrates how to group assets by attributes
Code sample
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],[],[],[],null,["# Group assets\n\nDemonstrates how to group assets by attributes\n\nCode sample\n-----------\n\n### Java\n\n\nTo authenticate to Security Command Center, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n static ImmutableList\u003cGroupResult\u003e groupAssets(OrganizationName organizationName) {\n try (SecurityCenterClient client = SecurityCenterClient.create()) {\n // Start setting up a request to group all assets by type in an organization, project, or folder.\n //\n // Parent must be in one of the following formats:\n // OrganizationName organizationName = OrganizationName.of(\"organization-id\");\n // ProjectName projectName = ProjectName.of(\"project-id\");\n // FolderName folderName = FolderName.of(\"folder-id\");\n GroupAssetsRequest.Builder request =\n GroupAssetsRequest.newBuilder()\n .setGroupBy(\"security_center_properties.resource_type\")\n .setParent(organizationName.toString());\n\n // Call the API.\n GroupAssetsPagedResponse response = client.groupAssets(request.build());\n\n // This creates one list for all assets. If your organization has a large number of assets\n // this can cause out of memory issues. You can process them batches by returning\n // the Iterable returned response.iterateAll() directly.\n ImmutableList\u003cGroupResult\u003e results = ImmutableList.copyOf(response.iterateAll());\n System.out.println(\"All assets:\");\n System.out.println(results);\n return results;\n } catch (IOException e) {\n throw new RuntimeException(\"Couldn't create client.\", e);\n }\n }\n\n### Python\n\n\nTo authenticate to Security Command Center, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n from google.cloud import securitycenter\n\n client = securitycenter.SecurityCenterClient()\n\n # 'parent' must be in one of the following formats:\n # \"organizations/{organization_id}\"\n # \"projects/{project_id}\"\n # \"folders/{folder_id}\"\n parent = f\"organizations/{organization_id}\"\n\n group_by_type = \"security_center_properties.resource_type\"\n\n result_iterator = client.https://cloud.google.com/python/docs/reference/securitycenter/latest/google.cloud.securitycenter_v1.services.security_center.SecurityCenterClient.html#google_cloud_securitycenter_v1_services_security_center_SecurityCenterClient_group_assets(\n request={\"parent\": parent, \"group_by\": group_by_type}\n )\n for i, result in enumerate(result_iterator):\n print((i + 1), result)\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=securitycenter)."]]