1414 * limitations under the License.
1515 */
1616
17- package vtwo .assets ;
18-
1917// [START securitycenter_add_security_marks_assets_v2]
18+ package vtwo .assets ;
2019
2120import autovalue .shaded .com .google .common .collect .ImmutableMap ;
2221import com .google .cloud .securitycenter .v2 .SecurityCenterClient ;
@@ -29,59 +28,50 @@ public class AddSecurityMarksToAssets {
2928
3029 public static void main (String [] args ) throws IOException {
3130 // organizationId: Google Cloud Organization id.
32- String organizationId = "{google-cloud-organization-id} " ;
31+ String organizationId = "ORGANIZATION_ID " ;
3332
34- // Specify the finding- id.
35- String assetId = "{asset-id} " ;
33+ // Specify the asset id.
34+ String assetId = "ASSET_ID " ;
3635
37- // Specify the location.
38- String location = "global" ;
39-
40- addToAsset (organizationId , location , assetId );
36+ addToAsset (organizationId , assetId );
4137 }
4238
43- // Demonstrates adding security marks to findings.
44- // To add or change security marks, you must have an IAM role that includes permission:
45- public static SecurityMarks addToAsset (String organizationId , String location , String assetId )
46- throws IOException {
39+ public static SecurityMarks addToAsset (String organizationId , String assetId ) throws IOException {
4740 // Initialize client that will be used to send requests. This client only needs to be created
4841 // once, and can be reused for multiple requests.
49- SecurityCenterClient client = SecurityCenterClient .create ();
50-
51- // Specify the value of 'assetName' in one of the following formats:
52- // String assetName = "organizations/{org-id}/assets/{asset-id}";
53- // String assetName = "projects/{project-id}/assets/{asset-id}";
54- // String assetName = "folders/{folder-id}/assets/{asset-id}";
55- String assetName = String .format ("organizations/%s/assets/%s" , organizationId , assetId );
56-
57- // Start setting up a request to add security marks for a finding.
58- ImmutableMap markMap = ImmutableMap .of ("key_a" , "value_a" , "key_b" , "value_b" );
59-
60- // Add security marks and field mask for security marks.
61- SecurityMarks securityMarks =
62- SecurityMarks .newBuilder ()
63- .setName (assetName + "/securityMarks" )
64- .putAllMarks (markMap )
65- .build ();
66-
67- // Set the update mask to specify which properties should be updated.
68- // If empty, all mutable fields will be updated.
69- // For more info on constructing field mask path, see the proto or:
70- // https://cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.FieldMask
71- FieldMask updateMask =
72- FieldMask .newBuilder ().addPaths ("marks.key_a" ).addPaths ("marks.key_b" ).build ();
73-
74- UpdateSecurityMarksRequest request =
75- UpdateSecurityMarksRequest .newBuilder ()
76- .setSecurityMarks (securityMarks )
77- .setUpdateMask (updateMask )
78- .build ();
79-
80- // Call the API.
81- SecurityMarks response = client .updateSecurityMarks (request );
82-
83- System .out .println ("Security Marks:" + response );
84- return response ;
42+ try (SecurityCenterClient client = SecurityCenterClient .create ()) {
43+
44+ // Specify the value of 'assetName' in one of the following formats:
45+ // String assetName = "organizations/{org-id}/assets/{asset-id}";
46+ String assetName = String .format ("organizations/%s/assets/%s" , organizationId , assetId );
47+
48+ // Start setting up a request to add security marks for a finding.
49+ ImmutableMap markMap = ImmutableMap .of ("key_a" , "value_a" , "key_b" , "value_b" );
50+
51+ // Add security marks and field mask for security marks.
52+ SecurityMarks securityMarks =
53+ SecurityMarks .newBuilder ()
54+ .setName (assetName + "/securityMarks" )
55+ .putAllMarks (markMap )
56+ .build ();
57+
58+ // Set the update mask to specify which properties should be updated.
59+ // If empty, all mutable fields will be updated.
60+ // For more info on constructing field mask path, see the proto or:
61+ // https://cloud.google.com/java/docs/reference/protobuf/latest/com.google.protobuf.FieldMask
62+ FieldMask updateMask =
63+ FieldMask .newBuilder ().addPaths ("marks.key_a" ).addPaths ("marks.key_b" ).build ();
64+
65+ UpdateSecurityMarksRequest request =
66+ UpdateSecurityMarksRequest .newBuilder ()
67+ .setSecurityMarks (securityMarks )
68+ .setUpdateMask (updateMask )
69+ .build ();
70+
71+ // Call the API and return the response.
72+ SecurityMarks response = client .updateSecurityMarks (request );
73+ return response ;
74+ }
8575 }
8676}
8777
0 commit comments