Skip to content

Commit e73984e

Browse files
Updated model armor code snippets to use module exports
1 parent f145160 commit e73984e

File tree

7 files changed

+72
-86
lines changed

7 files changed

+72
-86
lines changed

model-armor/snippets/getFolderFloorSettings.js

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
*
2020
* @param {string} folderId - The ID of the Google Cloud folder for which to retrieve floor settings.
2121
*/
22-
async function main(folderId) {
22+
async function getFolderFloorSettings(folderId) {
2323
// [START modelarmor_get_folder_floor_settings]
2424
/**
2525
* TODO(developer): Uncomment these variables before running the sample.
@@ -34,19 +34,14 @@ async function main(folderId) {
3434
// Instantiates a client
3535
const modelarmorClient = new ModelArmorClient();
3636

37-
async function getFolderFloorSettings() {
38-
// Construct request
39-
const request = {
40-
name,
41-
};
37+
// Construct request
38+
const request = {
39+
name,
40+
};
4241

43-
const response = await modelarmorClient.getFloorSetting(request);
44-
console.log(response);
45-
}
46-
47-
getFolderFloorSettings();
42+
const [response] = await modelarmorClient.getFloorSetting(request);
43+
return response;
4844
// [END modelarmor_get_folder_floor_settings]
4945
}
5046

51-
const args = process.argv.slice(2);
52-
main(...args).catch(console.error);
47+
module.exports = getFolderFloorSettings;

model-armor/snippets/getOrganizationFloorSettings.js

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
* @param {string} organizationId - The ID of the Google Cloud organization for which to retrieve
2121
* floor settings.
2222
*/
23-
async function main(organizationId) {
23+
async function getOrganizationFloorSettings(organizationId) {
2424
// [START modelarmor_get_organization_floor_settings]
2525
/**
2626
* TODO(developer): Uncomment these variables before running the sample.
@@ -35,20 +35,15 @@ async function main(organizationId) {
3535
// Instantiates a client
3636
const modelarmorClient = new ModelArmorClient();
3737

38-
async function getOrganizationFloorSettings() {
39-
// Construct request
40-
const request = {
41-
name,
42-
};
38+
// Construct request
39+
const request = {
40+
name,
41+
};
4342

44-
// Run request
45-
const response = await modelarmorClient.getFloorSetting(request);
46-
console.log(response);
47-
}
48-
49-
getOrganizationFloorSettings();
43+
// Run request
44+
const [response] = await modelarmorClient.getFloorSetting(request);
45+
return response;
5046
// [END modelarmor_get_organization_floor_settings]
5147
}
5248

53-
const args = process.argv.slice(2);
54-
main(...args).catch(console.error);
49+
module.exports = getOrganizationFloorSettings;

model-armor/snippets/getProjectFloorSettings.js

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
* @param {string} projectId - The ID of the Google Cloud project for which to retrieve
2121
* floor settings.
2222
*/
23-
async function main(projectId) {
23+
async function getProjectFloorSettings(projectId) {
2424
// [START modelarmor_get_project_floor_settings]
2525
/**
2626
* TODO(developer): Uncomment these variables before running the sample.
@@ -35,20 +35,15 @@ async function main(projectId) {
3535
// Instantiates a client
3636
const modelarmorClient = new ModelArmorClient();
3737

38-
async function getProjectFloorSettings() {
39-
// Construct request
40-
const request = {
41-
name,
42-
};
38+
// Construct request
39+
const request = {
40+
name,
41+
};
4342

44-
// Run request
45-
const response = await modelarmorClient.getFloorSetting(request);
46-
console.log(response);
47-
}
48-
49-
getProjectFloorSettings();
43+
// Run request
44+
const [response] = await modelarmorClient.getFloorSetting(request);
45+
return response;
5046
// [END modelarmor_get_project_floor_settings]
5147
}
5248

53-
const args = process.argv.slice(2);
54-
main(...args).catch(console.error);
49+
module.exports = getProjectFloorSettings;

model-armor/snippets/updateFolderFloorSettings.js

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
*
2020
* @param {string} folderId - Google Cloud folder ID for which floor settings need to be updated.
2121
*/
22-
async function main(folderId) {
22+
async function updateFolderFloorSettings(folderId) {
2323
// [START modelarmor_update_folder_floor_settings]
2424
/**
2525
* TODO(developer): Uncomment these variables before running the sample.
@@ -34,40 +34,35 @@ async function main(folderId) {
3434
// Instantiates a client
3535
const client = new ModelArmorClient();
3636

37-
async function updateFolderFloorSettings() {
38-
const floorSettingsName = `folders/${folderId}/locations/global/floorSetting`;
37+
const floorSettingsName = `folders/${folderId}/locations/global/floorSetting`;
3938

40-
// Build the floor settings with your preferred filters
41-
// For more details on filters, please refer to the following doc:
42-
// https://cloud.google.com/security-command-center/docs/key-concepts-model-armor#ma-filters
43-
const floorSetting = {
44-
name: floorSettingsName,
45-
filterConfig: {
46-
raiSettings: {
47-
raiFilters: [
48-
{
49-
filterType:
50-
protos.google.cloud.modelarmor.v1.RaiFilterType.HATE_SPEECH,
51-
confidenceLevel:
52-
protos.google.cloud.modelarmor.v1.DetectionConfidenceLevel.HIGH,
53-
},
54-
],
55-
},
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.HATE_SPEECH,
50+
confidenceLevel:
51+
protos.google.cloud.modelarmor.v1.DetectionConfidenceLevel.HIGH,
52+
},
53+
],
5654
},
57-
enableFloorSettingEnforcement: true,
58-
};
55+
},
56+
enableFloorSettingEnforcement: true,
57+
};
5958

60-
const request = {
61-
floorSetting: floorSetting,
62-
};
59+
const request = {
60+
floorSetting: floorSetting,
61+
};
6362

64-
const [response] = await client.updateFloorSetting(request);
65-
console.log('Updated folder floor settings', response);
66-
}
67-
68-
updateFolderFloorSettings();
63+
const [response] = await client.updateFloorSetting(request);
64+
return response;
6965
// [END modelarmor_update_folder_floor_settings]
7066
}
7167

72-
const args = process.argv.slice(2);
73-
main(...args).catch(console.error);
68+
module.exports = updateFolderFloorSettings;

model-armor/snippets/updateOrganizationFloorSettings.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
*
2020
* @param {string} organizationId - Google Cloud organization ID for which floor settings need to be updated.
2121
*/
22-
async function main(organizationId) {
22+
async function updateOrganizationFloorSettings(organizationId) {
2323
// [START modelarmor_update_organization_floor_settings]
2424
/**
2525
* TODO(developer): Uncomment these variables before running the sample.
@@ -59,10 +59,8 @@ async function main(organizationId) {
5959
};
6060

6161
const [response] = await client.updateFloorSetting(request);
62-
console.log('Updated organization floor settings:', response);
63-
62+
return response;
6463
// [END modelarmor_update_organization_floor_settings]
6564
}
6665

67-
const args = process.argv.slice(2);
68-
main(...args).catch(console.error);
66+
module.exports = updateOrganizationFloorSettings;

model-armor/snippets/updateProjectFloorSettings.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
*
2020
* @param {string} projectId - Google Cloud project ID for which floor settings need to be updated.
2121
*/
22-
async function main(projectId) {
22+
async function updateProjectFloorSettings(projectId) {
2323
// [START modelarmor_update_project_floor_settings]
2424
/**
2525
* TODO(developer): Uncomment these variables before running the sample.
@@ -59,9 +59,8 @@ async function main(projectId) {
5959
};
6060

6161
const [response] = await client.updateFloorSetting(request);
62-
console.log('Updated project floor settings:', response);
62+
return response;
6363
// [END modelarmor_update_project_floor_settings]
6464
}
6565

66-
const args = process.argv.slice(2);
67-
main(...args).catch(console.error);
66+
module.exports = updateProjectFloorSettings;

model-armor/test/modelarmor.test.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,10 @@ async function disableFloorSettings() {
9292
paths: ['enable_floor_setting_enforcement'],
9393
},
9494
});
95-
console.log('Disabled project floor settings:', updatedProjectSettings.name);
95+
console.log(
96+
'Disabled project floor settings:',
97+
updatedProjectSettings.name
98+
);
9699
}
97100

98101
// Disable folder floor settings if folderId is available
@@ -111,7 +114,10 @@ async function disableFloorSettings() {
111114
paths: ['enable_floor_setting_enforcement'],
112115
},
113116
});
114-
console.log('Disabled folder floor settings:', updatedFolderSettings.name);
117+
console.log(
118+
'Disabled folder floor settings:',
119+
updatedFolderSettings.name
120+
);
115121
}
116122
}
117123

@@ -131,7 +137,10 @@ async function disableFloorSettings() {
131137
paths: ['enable_floor_setting_enforcement'],
132138
},
133139
});
134-
console.log('Disabled organization floor settings:', updatedOrgSettings.name);
140+
console.log(
141+
'Disabled organization floor settings:',
142+
updatedOrgSettings.name
143+
);
135144
}
136145
}
137146
} catch (error) {
@@ -230,7 +239,7 @@ describe('Model Armor tests', () => {
230239
after(async () => {
231240
// Disable floor settings to restore original state
232241
await disableFloorSettings();
233-
242+
234243
// Clean up all templates
235244
const directTemplates = [emptyTemplateId, basicTemplateId];
236245
for (const templateId of directTemplates) {
@@ -316,4 +325,4 @@ describe('Model Armor tests', () => {
316325
// Check that the response contains enableFloorSettingEnforcement=true
317326
assert.match(output, /enableFloorSettingEnforcement:\s*true/);
318327
});
319-
});
328+
});

0 commit comments

Comments
 (0)