diff --git a/modelarmor/composer.json b/modelarmor/composer.json new file mode 100644 index 0000000000..037778bf23 --- /dev/null +++ b/modelarmor/composer.json @@ -0,0 +1,10 @@ +{ + "require": { + "google/cloud-modelarmor": "^0.1.0" + }, + "autoload": { + "psr-4": { + "Google\\Cloud\\Samples\\ModelArmor\\": "test" + } + } +} diff --git a/modelarmor/phpunit.xml.dist b/modelarmor/phpunit.xml.dist new file mode 100644 index 0000000000..8e2959e6a6 --- /dev/null +++ b/modelarmor/phpunit.xml.dist @@ -0,0 +1,38 @@ + + + + + + + test + + + + + + + + ./src + + ./vendor + + + + + + + diff --git a/modelarmor/src/create_template.php b/modelarmor/src/create_template.php new file mode 100644 index 0000000000..0a0f9a51f9 --- /dev/null +++ b/modelarmor/src/create_template.php @@ -0,0 +1,89 @@ + "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); + +/** Build the Model Armor template with preferred filters. + * For more details on filters, refer to: + * https://cloud.google.com/security-command-center/docs/key-concepts-model-armor#ma-filters + */ + +$raiFilters = [ + (new RaiFilter()) + ->setFilterType(RaiFilterType::DANGEROUS) + ->setConfidenceLevel(DetectionConfidenceLevel::HIGH), + (new RaiFilter()) + ->setFilterType(RaiFilterType::HATE_SPEECH) + ->setConfidenceLevel(DetectionConfidenceLevel::HIGH), + (new RaiFilter()) + ->setFilterType(RaiFilterType::SEXUALLY_EXPLICIT) + ->setConfidenceLevel(DetectionConfidenceLevel::LOW_AND_ABOVE), + (new RaiFilter()) + ->setFilterType(RaiFilterType::HARASSMENT) + ->setConfidenceLevel(DetectionConfidenceLevel::MEDIUM_AND_ABOVE), +]; + +$raiFilterSetting = (new RaiFilterSettings())->setRaiFilters($raiFilters); + +$templateFilterConfig = (new FilterConfig())->setRaiSettings($raiFilterSetting); + +$template = (new Template())->setFilterConfig($templateFilterConfig); + +// Prepare the create template request. +$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] diff --git a/modelarmor/src/delete_template.php b/modelarmor/src/delete_template.php new file mode 100644 index 0000000000..97c14b015b --- /dev/null +++ b/modelarmor/src/delete_template.php @@ -0,0 +1,54 @@ + "modelarmor.$locationId.rep.googleapis.com"]; + +// Instantiates a client. +$client = new ModelArmorClient($options); + +// Prepare the name of the template. +$name = sprintf('projects/%s/locations/%s/templates/%s', $projectId, $locationId, $templateId); + +// Prepare the request. +$dltTemplateRequest = new DeleteTemplateRequest()->setName($name); + +$client->deleteTemplate($dltTemplateRequest); + +printf('Deleted template: %s' . PHP_EOL, $name); +// [END modelarmor_delete_template] diff --git a/modelarmor/src/get_template.php b/modelarmor/src/get_template.php new file mode 100644 index 0000000000..31252b5656 --- /dev/null +++ b/modelarmor/src/get_template.php @@ -0,0 +1,54 @@ + "modelarmor.$locationId.rep.googleapis.com"]; + +// Instantiates a client. +$client = new ModelArmorClient($options); + +// Construct the full resource name of the template. +$name = sprintf('projects/%s/locations/%s/templates/%s', $projectId, $locationId, $templateId); + +// Prepare the request. +$getTemplateRequest = new GetTemplateRequest()->setName($name); + +$response = $client->getTemplate($getTemplateRequest); + +printf('Template retrieved: %s' . PHP_EOL, $response->getName()); +// [END modelarmor_get_template] diff --git a/modelarmor/src/list_templates.php b/modelarmor/src/list_templates.php new file mode 100644 index 0000000000..0f6167c1a2 --- /dev/null +++ b/modelarmor/src/list_templates.php @@ -0,0 +1,52 @@ + "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); + +// Prepare the request. +$listTemplatesrequest = new ListTemplatesRequest()->setParent($parent); + +// Send the request and collect all results. +$templates = iterator_to_array($client->listTemplates($listTemplatesrequest)->iterateAllElements()); + +foreach ($templates as $template) { + printf('Template: %s' . PHP_EOL, $template->getName()); +} +// [END modelarmor_list_templates] diff --git a/modelarmor/test/BaseTestCase.php b/modelarmor/test/BaseTestCase.php new file mode 100644 index 0000000000..679c8635ef --- /dev/null +++ b/modelarmor/test/BaseTestCase.php @@ -0,0 +1,75 @@ + '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; + } +} diff --git a/modelarmor/test/deleteTemplateTest.php b/modelarmor/test/deleteTemplateTest.php new file mode 100644 index 0000000000..f9667ab6ba --- /dev/null +++ b/modelarmor/test/deleteTemplateTest.php @@ -0,0 +1,49 @@ +runSnippetfile('create_template', [ + $projectId, + self::$locationId, + self::$templateId, + ]); + + $output = $this->runSnippetfile('delete_template', [ + $projectId, + self::$locationId, + self::$templateId, + ]); + + $expectedTemplateString = 'Deleted template: projects/' . $projectId . '/locations/' . self::$locationId . '/templates/' . self::$templateId; + $this->assertStringContainsString($expectedTemplateString, $output); + } +} diff --git a/modelarmor/test/getTemplateTest.php b/modelarmor/test/getTemplateTest.php new file mode 100644 index 0000000000..9b4fc4fb9e --- /dev/null +++ b/modelarmor/test/getTemplateTest.php @@ -0,0 +1,49 @@ +runSnippetfile('create_template', [ + $projectId, + self::$locationId, + self::$templateId, + ]); + + $output = $this->runSnippetfile('get_template', [ + $projectId, + self::$locationId, + self::$templateId, + ]); + + $expectedTemplateString = 'Template retrieved: projects/' . $projectId . '/locations/' . self::$locationId . '/templates/' . self::$templateId; + $this->assertStringContainsString($expectedTemplateString, $output); + } +} diff --git a/modelarmor/test/listTemplatesTest.php b/modelarmor/test/listTemplatesTest.php new file mode 100644 index 0000000000..18de5829bf --- /dev/null +++ b/modelarmor/test/listTemplatesTest.php @@ -0,0 +1,48 @@ +runSnippetfile('create_template', [ + $projectId, + self::$locationId, + self::$templateId, + ]); + + $output = $this->runSnippetfile('list_templates', [ + $projectId, + self::$locationId + ]); + + $expectedTemplateString = 'Template: projects/' . $projectId . '/locations/' . self::$locationId . '/templates/' . self::$templateId; + $this->assertStringContainsString($expectedTemplateString, $output); + } +}