Skip to content
Merged
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
63 changes: 63 additions & 0 deletions secretmanager/src/update_secret_with_alias.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
/*
* Copyright 2020 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.
*/

/*
* For instructions on how to run the full sample:
*
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/master/secretmanager/README.md
*/

declare(strict_types=1);

namespace Google\Cloud\Samples\SecretManager;

// [START secretmanager_update_secret_with_alias]
// Import the Secret Manager client library.
use Google\Cloud\SecretManager\V1\Secret;
use Google\Cloud\SecretManager\V1\SecretManagerServiceClient;
use Google\Protobuf\FieldMask;

/**
* @param string $projectId Your Google Cloud Project ID (e.g. 'my-project')
* @param string $secretId Your secret ID (e.g. 'my-secret')
*/
function update_secret_with_alias(string $projectId, string $secretId): void
{
// Create the Secret Manager client.
$client = new SecretManagerServiceClient();

// Build the resource name of the secret.
$name = $client->secretName($projectId, $secretId);

// Update the secret.
$secret = (new Secret())
->setName($name)
->setVersionAliases(['test' => '1']);

$updateMask = (new FieldMask())
->setPaths(['version_aliases']);

$response = $client->updateSecret($secret, $updateMask);

// Print the upated secret.
printf('Updated secret: %s', $response->getName());
}
// [END secretmanager_update_secret_with_alias]

// The following 2 lines are only needed to execute the samples on the CLI
require_once __DIR__ . '/../../testing/sample_helpers.php';
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);
12 changes: 12 additions & 0 deletions secretmanager/test/secretmanagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -303,4 +303,16 @@ public function testUpdateSecret()

$this->assertStringContainsString('Updated secret', $output);
}

public function testUpdateSecretWithAlias()
{
$name = self::$client->parseName(self::$testSecretWithVersions->getName());

$output = $this->runFunctionSnippet('update_secret_with_alias', [
$name['project'],
$name['secret'],
]);

$this->assertStringContainsString('Updated secret', $output);
}
}