Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions modelarmor/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"require": {
"google/cloud-dlp": "^2.4",
"google/cloud-modelarmor": "^0.1.0"
},
"autoload": {
"psr-4": {
"Google\\Cloud\\Samples\\ModelArmor\\": "test"
}
}
}
38 changes: 38 additions & 0 deletions modelarmor/phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2025 Google LLC.

Licensed under the Apache License, Version 2.0 (the 'License');
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an 'AS IS' BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<phpunit bootstrap="../testing/bootstrap.php">
<testsuites>
<testsuite name="PHP Secret Manager test">
<directory>test</directory>
</testsuite>
</testsuites>
<logging>
<log type="coverage-clover" target="build/logs/clover.xml"/>
</logging>
<filter>
<whitelist>
<directory suffix=".php">./src</directory>
<exclude>
<directory>./vendor</directory>
</exclude>
</whitelist>
</filter>
<php>
<env name="PHPUNIT_TESTS" value="1"/>
</php>
</phpunit>
97 changes: 97 additions & 0 deletions modelarmor/src/create_template_with_advanced_sdp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php
/*
* Copyright 2025 Google LLC.
*
* Licensed under the Apache License, Version 2.0 (the 'License');
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an 'AS IS' BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

declare(strict_types=1);

namespace Google\Cloud\Samples\ModelArmor;

require_once __DIR__ . '/../vendor/autoload.php';

if (count($argv) < 4 || count($argv) > 6) {
return printf("Usage: php %s PROJECT_ID LOCATION_ID TEMPLATE_ID [INSPECT_TEMPLATE] [DEIDENTIFY_TEMPLATE]\n", basename(__FILE__));
}
list($_, $projectId, $locationId, $templateId) = $argv;
$inspectTemplate = $argv[4] ?? '';
$deidentifyTemplate = $argv[5] ?? '';

// [START modelarmor_create_template_with_advanced_sdp]
// Import the ModelArmor client library.
use Google\Cloud\ModelArmor\V1\Client\ModelArmorClient;
use Google\Cloud\ModelArmor\V1\SdpAdvancedConfig;
use Google\Cloud\ModelArmor\V1\Template;
use Google\Cloud\ModelArmor\V1\FilterConfig;
use Google\Cloud\ModelArmor\V1\RaiFilterSettings;
use Google\Cloud\ModelArmor\V1\RaiFilterSettings\RaiFilter;
use Google\Cloud\ModelArmor\V1\CreateTemplateRequest;
use Google\Cloud\ModelArmor\V1\RaiFilterType;
use Google\Cloud\ModelArmor\V1\DetectionConfidenceLevel;
use Google\Cloud\ModelArmor\V1\SdpFilterSettings;

/** Uncomment and populate these variables in your code */
// $projectId = "YOUR_GOOGLE_CLOUD_PROJECT"; (e.g. 'my-project');
// $locationId = 'YOUR_LOCATION_ID'; (e.g. 'my-location');
// $templateId = 'YOUR_TEMPLATE_ID'; (e.g. 'my-template');
// $inspectTemplate = 'YOUR_INSPECT_TEMPLATE'; (e.g. 'organizations/{organization}/inspectTemplates/{inspect_template}')
// $deidentifyTemplate = 'YOUR_DEIDENTIFY_TEMPLATE'; (e.g. 'organizations/{organization}/deidentifyTemplates/{deidentify_template}')

// Specify regional endpoint.
$options = ['apiEndpoint' => "modelarmor.$locationId.rep.googleapis.com"];

$client = new ModelArmorClient($options);

// Build the resource name of the parent location.
$parent = $client->locationName($projectId, $locationId);

$filters = [
(new RaiFilter())
->setFilterType(RaiFilterType::DANGEROUS)
->setConfidenceLevel(DetectionConfidenceLevel::HIGH),
(new RaiFilter())
->setFilterType(RaiFilterType::HARASSMENT)
->setConfidenceLevel(DetectionConfidenceLevel::MEDIUM_AND_ABOVE),
(new RaiFilter())
->setFilterType(RaiFilterType::HATE_SPEECH)
->setConfidenceLevel(DetectionConfidenceLevel::HIGH),
(new RaiFilter())
->setFilterType(RaiFilterType::SEXUALLY_EXPLICIT)
->setConfidenceLevel(DetectionConfidenceLevel::HIGH)
];

$raiFilterSetting = (new RaiFilterSettings())->setRaiFilters($filters);

$sdpAdvancedConfig = (new SdpAdvancedConfig())
->setInspectTemplate($inspectTemplate)
->setDeidentifyTemplate($deidentifyTemplate);

$sdpSettings = (new SdpFilterSettings())->setAdvancedConfig($sdpAdvancedConfig);

// Define the template configuration with advanced SDP settings.
$templateFilterConfig = (new FilterConfig())
->setRaiSettings($raiFilterSetting)
->setSdpSettings($sdpSettings);

$template = (new Template())->setFilterConfig($templateFilterConfig);

$request = (new CreateTemplateRequest())
->setParent($parent)
->setTemplateId($templateId)
->setTemplate($template);

$response = $client->createTemplate($request);

printf('Template created: %s' . PHP_EOL, $response->getName());
// [END modelarmor_create_template_with_advanced_sdp]
93 changes: 93 additions & 0 deletions modelarmor/src/create_template_with_basic_sdp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php
/*
* Copyright 2025 Google LLC.
*
* Licensed under the Apache License, Version 2.0 (the 'License');
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an 'AS IS' BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

declare(strict_types=1);

namespace Google\Cloud\Samples\ModelArmor;

require_once __DIR__ . '/../vendor/autoload.php';

if (count($argv) != 4) {
return printf("Usage: php %s PROJECT_ID LOCATION_ID TEMPLATE_ID\n", basename(__FILE__));
}
list($_, $projectId, $locationId, $templateId) = $argv;

// [START modelarmor_create_template_with_basic_sdp]
// Import the ModelArmor client library.
use Google\Cloud\ModelArmor\V1\Client\ModelArmorClient;
use Google\Cloud\ModelArmor\V1\RaiFilterType;
use Google\Cloud\ModelArmor\V1\DetectionConfidenceLevel;
use Google\Cloud\ModelArmor\V1\SdpBasicConfig\SdpBasicConfigEnforcement;
use Google\Cloud\ModelArmor\V1\SdpBasicConfig;
use Google\Cloud\ModelArmor\V1\SdpFilterSettings;
use Google\Cloud\ModelArmor\V1\FilterConfig;
use Google\Cloud\ModelArmor\V1\RaiFilterSettings;
use Google\Cloud\ModelArmor\V1\CreateTemplateRequest;
use Google\Cloud\ModelArmor\V1\RaiFilterSettings\RaiFilter;
use Google\Cloud\ModelArmor\V1\Template;

/** Uncomment and populate these variables in your code */
// $projectId = "YOUR_GOOGLE_CLOUD_PROJECT"; (e.g. 'my-project');
// $locationId = 'YOUR_LOCATION_ID'; (e.g. 'my-location');
// $templateId = 'YOUR_TEMPLATE_ID'; (e.g. 'my-template');

// Specify regional endpoint.
$options = ['apiEndpoint' => "modelarmor.$locationId.rep.googleapis.com"];

// Instantiates a client.
$client = new ModelArmorClient($options);

// Build the resource name of the parent location.
$parent = $client->locationName($projectId, $locationId);

$raiFilters = [
(new RaiFilter())
->setFilterType(RaiFilterType::DANGEROUS)
->setConfidenceLevel(DetectionConfidenceLevel::HIGH),
(new RaiFilter())
->setFilterType(RaiFilterType::HARASSMENT)
->setConfidenceLevel(DetectionConfidenceLevel::MEDIUM_AND_ABOVE),
(new RaiFilter())
->setFilterType(RaiFilterType::HATE_SPEECH)
->setConfidenceLevel(DetectionConfidenceLevel::HIGH),
(new RaiFilter())
->setFilterType(RaiFilterType::SEXUALLY_EXPLICIT)
->setConfidenceLevel(DetectionConfidenceLevel::HIGH)
];

$raiFilterSetting = (new RaiFilterSettings())->setRaiFilters($raiFilters);

$sdpBasicConfig = (new SdpBasicConfig())->setFilterEnforcement(SdpBasicConfigEnforcement::ENABLED);

$sdpSettings = (new SdpFilterSettings())->setBasicConfig($sdpBasicConfig);

// Define the template configuration with basic SDP settings.
$templateFilterConfig = (new FilterConfig())
->setRaiSettings($raiFilterSetting)
->setSdpSettings($sdpSettings);

$template = (new Template())->setFilterConfig($templateFilterConfig);

$request = (new CreateTemplateRequest())
->setParent($parent)
->setTemplateId($templateId)
->setTemplate($template);

$response = $client->createTemplate($request);

printf('Template created: %s' . PHP_EOL, $response->getName());
// [END modelarmor_create_template_with_basic_sdp]
75 changes: 75 additions & 0 deletions modelarmor/test/BaseTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php
/*
* Copyright 2025 Google LLC.
*
* Licensed under the Apache License, Version 2.0 (the 'License');
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an 'AS IS' BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

declare(strict_types=1);

namespace Google\Cloud\Samples\ModelArmor;

require_once __DIR__ . '/../vendor/autoload.php';

use Google\ApiCore\ApiException as GaxApiException;
use Google\Cloud\ModelArmor\V1\Client\ModelArmorClient;
use Google\Cloud\ModelArmor\V1\DeleteTemplateRequest;
use Google\Cloud\TestUtils\TestTrait;
use PHPUnit\Framework\TestCase;

abstract class BaseTestCase extends TestCase
{
use TestTrait;

protected static $client;
protected static $templateId;
protected static $locationId = 'us-central1';

public static function setUpBeforeClass(): void
{
$options = ['apiEndpoint' => 'modelarmor.' . self::$locationId . '.rep.googleapis.com'];
self::$client = new ModelArmorClient($options);
self::$templateId = static::getTemplatePrefix() . uniqid();
}

public static function tearDownAfterClass(): void
{
$templateName = self::$client->templateName(self::$projectId, self::$locationId, self::$templateId);
try {
static::customTeardown();
$request = (new DeleteTemplateRequest())->setName($templateName);
self::$client->deleteTemplate($request);
} catch (GaxApiException $e) {
if ($e->getStatus() != 'NOT_FOUND') {
throw $e;
}
}
self::$client->close();
}

abstract protected static function getTemplatePrefix(): string;
protected static function customTeardown(): void
{
}

protected function runSnippetfile(string $snippetName, array $params = []): string
{
$output = $this->runSnippet($snippetName, $params);
return $output;
}

protected static function getProjectId()
{
return self::$projectId;
}
}
Loading