Skip to content

Commit 595f2e9

Browse files
rudrakhsha-crestharshnasitcrestglasnt
authored
feat(modelarmor): Added snippets for floor settings (#4053)
* Added CRUD code snippets with codeowners file * Solved linting errors * Added header comment * Removed hardcoded value from test * Added floor settings snippets * Removed crud specific files * Updated test to disable floor settings in the after code * Updated variable names in tests for org id and folder id * Updated model armor code snippets to use module exports * add-endofline-and-fix-copyright-year * resolve-merge-conflicts * fix-package-json-formatting * change-metadata-in-create-template-snippet * add testing envvars * Removed floor setting tests --------- Co-authored-by: Harsh Nasit <[email protected]> Co-authored-by: harshnasitcrest <[email protected]> Co-authored-by: Katie McLaughlin <[email protected]>
1 parent fa66587 commit 595f2e9

10 files changed

+401
-26
lines changed

model-armor/package.json

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,26 @@
11
{
2-
"name": "nodejs-model-armor-samples",
3-
"private": true,
4-
"license": "Apache-2.0",
5-
"files": [
6-
"*.js"
7-
],
8-
"author": "Google LLC",
9-
"repository": "googleapis/nodejs-model-armor",
10-
"engines": {
11-
"node": ">=16.0.0"
12-
},
13-
"scripts": {
14-
"test": "c8 mocha -p -j 2 --recursive test/ --timeout=60000"
15-
},
16-
"dependencies": {
17-
"@google-cloud/modelarmor": "^0.1.0",
18-
"@google-cloud/dlp": "^5.0.0"
19-
},
20-
"devDependencies": {
21-
"c8": "^10.0.0",
22-
"chai": "^4.5.0",
23-
"mocha": "^10.0.0",
24-
"uuid": "^10.0.0"
25-
}
2+
"name": "nodejs-model-armor-samples",
3+
"private": true,
4+
"license": "Apache-2.0",
5+
"files": [
6+
"*.js"
7+
],
8+
"author": "Google LLC",
9+
"repository": "googleapis/nodejs-model-armor",
10+
"engines": {
11+
"node": ">=16.0.0"
12+
},
13+
"scripts": {
14+
"test": "c8 mocha -p -j 2 --recursive test/ --timeout=60000"
15+
},
16+
"dependencies": {
17+
"@google-cloud/modelarmor": "^0.1.0",
18+
"@google-cloud/dlp": "^5.0.0"
19+
},
20+
"devDependencies": {
21+
"c8": "^10.0.0",
22+
"chai": "^4.5.0",
23+
"mocha": "^10.0.0",
24+
"uuid": "^10.0.0"
25+
}
2626
}
27-

model-armor/snippets/ci-setup.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"env": {
3+
"MA_FOLDER_ID": 695279264361,
4+
"MA_ORG_ID": 951890214235
5+
}
6+
}

model-armor/snippets/createTemplateWithMetadata.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ async function createTemplateWithMetadata(projectId, locationId, templateId) {
6666
},
6767
},
6868
templateMetadata: {
69-
ignorePartialInvocationFailures: true,
69+
logTemplateOperations: true,
7070
logSanitizeOperations: true,
7171
},
7272
};
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
/**
18+
* Retrieves the floor settings for a Google Cloud folder.
19+
*
20+
* @param {string} folderId - The ID of the Google Cloud folder for which to retrieve floor settings.
21+
*/
22+
async function getFolderFloorSettings(folderId) {
23+
// [START modelarmor_get_folder_floor_settings]
24+
/**
25+
* TODO(developer): Uncomment these variables before running the sample.
26+
*/
27+
// const folderId = 'your-folder-id';
28+
29+
const name = `folders/${folderId}/locations/global/floorSetting`;
30+
31+
// Imports the Modelarmor library
32+
const {ModelArmorClient} = require('@google-cloud/modelarmor').v1;
33+
34+
// Instantiates a client
35+
const modelarmorClient = new ModelArmorClient();
36+
37+
// Construct request
38+
const request = {
39+
name,
40+
};
41+
42+
const [response] = await modelarmorClient.getFloorSetting(request);
43+
return response;
44+
// [END modelarmor_get_folder_floor_settings]
45+
}
46+
47+
module.exports = getFolderFloorSettings;
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
/**
18+
* Retrieves the floor settings for a Google Cloud organization.
19+
*
20+
* @param {string} organizationId - The ID of the Google Cloud organization for which to retrieve
21+
* floor settings.
22+
*/
23+
async function getOrganizationFloorSettings(organizationId) {
24+
// [START modelarmor_get_organization_floor_settings]
25+
/**
26+
* TODO(developer): Uncomment these variables before running the sample.
27+
*/
28+
// const organizationId = 'your-organization-id';
29+
30+
const name = `organizations/${organizationId}/locations/global/floorSetting`;
31+
32+
// Imports the Modelarmor library
33+
const {ModelArmorClient} = require('@google-cloud/modelarmor').v1;
34+
35+
// Instantiates a client
36+
const modelarmorClient = new ModelArmorClient();
37+
38+
// Construct request
39+
const request = {
40+
name,
41+
};
42+
43+
// Run request
44+
const [response] = await modelarmorClient.getFloorSetting(request);
45+
return response;
46+
// [END modelarmor_get_organization_floor_settings]
47+
}
48+
49+
module.exports = getOrganizationFloorSettings;
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
/**
18+
* Retrieves the floor settings for a Google Cloud project.
19+
*
20+
* @param {string} projectId - The ID of the Google Cloud project for which to retrieve
21+
* floor settings.
22+
*/
23+
async function getProjectFloorSettings(projectId) {
24+
// [START modelarmor_get_project_floor_settings]
25+
/**
26+
* TODO(developer): Uncomment these variables before running the sample.
27+
*/
28+
// const projectId = 'your-project-id';
29+
30+
const name = `projects/${projectId}/locations/global/floorSetting`;
31+
32+
// Imports the Modelarmor library
33+
const {ModelArmorClient} = require('@google-cloud/modelarmor').v1;
34+
35+
// Instantiates a client
36+
const modelarmorClient = new ModelArmorClient();
37+
38+
// Construct request
39+
const request = {
40+
name,
41+
};
42+
43+
// Run request
44+
const [response] = await modelarmorClient.getFloorSetting(request);
45+
return response;
46+
// [END modelarmor_get_project_floor_settings]
47+
}
48+
49+
module.exports = getProjectFloorSettings;
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
/**
18+
* Updates the floor settings of a folder in Model Armor.
19+
*
20+
* @param {string} folderId - Google Cloud folder ID for which floor settings need to be updated.
21+
*/
22+
async function updateFolderFloorSettings(folderId) {
23+
// [START modelarmor_update_folder_floor_settings]
24+
/**
25+
* TODO(developer): Uncomment these variables before running the sample.
26+
*/
27+
// const folderId = 'your-folder-id';
28+
29+
// Imports the Model Armor library
30+
const modelarmor = require('@google-cloud/modelarmor');
31+
const {ModelArmorClient} = modelarmor.v1;
32+
const {protos} = modelarmor;
33+
34+
// Instantiates a client
35+
const client = new ModelArmorClient();
36+
37+
const floorSettingsName = `folders/${folderId}/locations/global/floorSetting`;
38+
39+
// Build the floor settings with your preferred filters
40+
// For more details on filters, please refer to the following doc:
41+
// https://cloud.google.com/security-command-center/docs/key-concepts-model-armor#ma-filters
42+
const floorSetting = {
43+
name: floorSettingsName,
44+
filterConfig: {
45+
raiSettings: {
46+
raiFilters: [
47+
{
48+
filterType:
49+
protos.google.cloud.modelarmor.v1.RaiFilterType.HARASSMENT,
50+
confidenceLevel:
51+
protos.google.cloud.modelarmor.v1.DetectionConfidenceLevel
52+
.LOW_AND_ABOVE,
53+
},
54+
{
55+
filterType:
56+
protos.google.cloud.modelarmor.v1.RaiFilterType.SEXUALLY_EXPLICIT,
57+
confidenceLevel:
58+
protos.google.cloud.modelarmor.v1.DetectionConfidenceLevel.HIGH,
59+
},
60+
],
61+
},
62+
},
63+
enableFloorSettingEnforcement: true,
64+
};
65+
66+
const request = {
67+
floorSetting: floorSetting,
68+
};
69+
70+
const [response] = await client.updateFloorSetting(request);
71+
return response;
72+
// [END modelarmor_update_folder_floor_settings]
73+
}
74+
75+
module.exports = updateFolderFloorSettings;
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
'use strict';
16+
17+
/**
18+
* Updates the floor settings of an organization in Model Armor.
19+
*
20+
* @param {string} organizationId - Google Cloud organization ID for which floor settings need to be updated.
21+
*/
22+
async function updateOrganizationFloorSettings(organizationId) {
23+
// [START modelarmor_update_organization_floor_settings]
24+
/**
25+
* TODO(developer): Uncomment these variables before running the sample.
26+
*/
27+
// const organizationId = 'your-organization-id';
28+
29+
const modelarmor = require('@google-cloud/modelarmor');
30+
const {ModelArmorClient} = modelarmor.v1;
31+
const {protos} = modelarmor;
32+
33+
const client = new ModelArmorClient();
34+
35+
const floorSettingsName = `organizations/${organizationId}/locations/global/floorSetting`;
36+
37+
// Build the floor settings with your preferred filters
38+
// For more details on filters, please refer to the following doc:
39+
// https://cloud.google.com/security-command-center/docs/key-concepts-model-armor#ma-filters
40+
const floorSetting = {
41+
name: floorSettingsName,
42+
filterConfig: {
43+
raiSettings: {
44+
raiFilters: [
45+
{
46+
filterType:
47+
protos.google.cloud.modelarmor.v1.RaiFilterType.HARASSMENT,
48+
confidenceLevel:
49+
protos.google.cloud.modelarmor.v1.DetectionConfidenceLevel
50+
.LOW_AND_ABOVE,
51+
},
52+
{
53+
filterType:
54+
protos.google.cloud.modelarmor.v1.RaiFilterType.SEXUALLY_EXPLICIT,
55+
confidenceLevel:
56+
protos.google.cloud.modelarmor.v1.DetectionConfidenceLevel.HIGH,
57+
},
58+
],
59+
},
60+
},
61+
enableFloorSettingEnforcement: true,
62+
};
63+
64+
const request = {
65+
floorSetting: floorSetting,
66+
};
67+
68+
const [response] = await client.updateFloorSetting(request);
69+
return response;
70+
// [END modelarmor_update_organization_floor_settings]
71+
}
72+
73+
module.exports = updateOrganizationFloorSettings;

0 commit comments

Comments
 (0)