Skip to content

Commit

Permalink
chore: Add regional code samples for Secret Manager (#28115)
Browse files Browse the repository at this point in the history
  • Loading branch information
YashSaraf11 authored Dec 24, 2024
1 parent 2ad551c commit 409ca00
Show file tree
Hide file tree
Showing 51 changed files with 3,499 additions and 1 deletion.
46 changes: 45 additions & 1 deletion google-cloud-secret_manager/samples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ These samples show how to use the [Google Secret Manager API]
$ export GOOGLE_CLOUD_PROJECT="YOUR_PROJECT_ID"
```
1. **Run samples**
```text
$ export GOOGLE_CLOUD_LOCATION="YOUR_LOCATION_ID"
```
1. **Run global samples**
```text
$ bundle exec ruby snippets.rb
Expand Down Expand Up @@ -65,6 +69,46 @@ These samples show how to use the [Google Secret Manager API]
Environment variables:
GOOGLE_CLOUD_PROJECT ID of the Google Cloud project to run snippets
```
1. **Run regional samples**
```text
$ bundle exec ruby regional_snippets.rb
```
The output will show the help text:
```text
Usage: bundle exec ruby regional_snippets.rb [command] [arguments]
Commands:
access_regional_secret_version <secret> <version> Access a regional secret version
add_regional_secret_version <secret> Add a new regional secret version
create_regional_secret <secret> Create a new regional secret
delete_regional_secret_with_etag <secret> <etag> Delete an existing regional secret with associated etag
delete_regional_secret <secret> Delete an existing regional secret
destroy_regional_secret_version_with_etag <secret> <version> <etag> Destroy a regional secret version with associated etag
destroy_regional_secret_version <secret> <version> <etag> Destroy a regional secret version
disable_regional_secret_version_with_etag <secret> <version> <etag> Disable a regional secret version with associated etag
disable_regional_secret_version <secret> <version> Disable a regional secret version
enable_regional_secret_version_with_etag <secret> <version> <etag> Enable a regional secret version with associated etag
enable_regional_secret_version <secret> <version> Enable a regional secret version
get_regional_secret <secret> Get a regional secret
get_regional_secret_version <secret> <version> Get a regional secret version
iam_grant_access_regional <secret> <version> <member> Grant the member access to the regional secret
iam_revoke_access_regional <secret> <version> <member> Revoke the member access to the regional secret
list_regional_secret_versions_with_filter <secret> <filter> List all versions for a regional secret which passes filter
list_regional_secret_versions <secret> List all versions for a regional secret
list_regional_secrets_with_filter <filter> List all regional secrets which passes filter
list_regional_secrets List all regional secrets
update_regional_secret_with_alias <secret> Update a regional secret with alias
update_regional_secret_with_etag <secret> <etag> Update a regional secret with associated etag
update_regional_secret <secret> Update a regional secret
Environment variables:
GOOGLE_CLOUD_PROJECT ID of the Google Cloud project to run the regional snippets
GOOGLE_CLOUD_LOCATION ID of the Google Cloud location to run the regional snippets
```
## Contributing changes
Expand Down
10 changes: 10 additions & 0 deletions google-cloud-secret_manager/samples/Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@
require "rake/testtask"
require "rubocop/rake_task"

Rake::TestTask.new "global_test" do |t|
t.test_files = FileList["acceptance/*_test.rb"].exclude(/regional/)
t.warning = false
end

Rake::TestTask.new "regional_test" do |t|
t.test_files = FileList["acceptance/*_test.rb"].select { |file| file =~ /regional/ }
t.warning = false
end

Rake::TestTask.new "test" do |t|
t.test_files = FileList["acceptance/*_test.rb"]
t.warning = false
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Copyright 2022 Google, Inc
#
# 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.

require "uri"

require_relative "regional_helper"

describe "#access_regional_secret_version", :regional_secret_manager_snippet do
it "accesses the regional secret version" do
sample = SampleLoader.load "access_regional_secret_version.rb"

assert_output(/Plaintext: hello world!/) do
sample.run project_id: project_id, location_id: location_id, secret_id: secret_id, version_id: version_id
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Copyright 2022 Google, Inc
#
# 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.

require "uri"

require_relative "regional_helper"

describe "#add_regional_secret_version", :regional_secret_manager_snippet do
it "adds a secret version" do
sample = SampleLoader.load "add_regional_secret_version.rb"

o_list = client.list_secret_versions(parent: secret.name).to_a
assert_empty o_list

out, _err = capture_io do
sample.run project_id: project_id, location_id: location_id, secret_id: secret_id
end
assert_match(/Added regional secret version: \S+/, out)
version = /Added regional secret version: (\S+)/.match(out)[1]

n_list = client.list_secret_versions(parent: secret.name).to_a
assert_includes n_list.map(&:name), version
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Copyright 2022 Google, Inc
#
# 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.

require "uri"

require_relative "regional_helper"

describe "#create_regional_secret", :regional_secret_manager_snippet do
it "creates a regional secret" do
sample = SampleLoader.load "create_regional_secret.rb"

out, _err = capture_io do
sample.run project_id: project_id, location_id: location_id, secret_id: secret_id
end
secret_id_regex = Regexp.escape secret_id
assert_match %r{Created regional secret: projects/\S+locations/\S+/secrets/#{secret_id_regex}}, out
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Copyright 2022 Google, Inc
#
# 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.

require "uri"

require_relative "regional_helper"

describe "#delete_regional_secret", :regional_secret_manager_snippet do
it "deletes the secret" do
sample = SampleLoader.load "delete_regional_secret.rb"

refute_nil secret
client.get_secret name: secret_name

assert_output(/Deleted regional secret/) do
sample.run project_id: project_id, location_id: location_id, secret_id: secret_id
end

assert_raises Google::Cloud::NotFoundError do
client.get_secret name: secret_name
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Copyright 2022 Google, Inc
#
# 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.

require "uri"

require_relative "regional_helper"

describe "#delete_regional_secret_with_etag", :regional_secret_manager_snippet do
it "deletes the regional secret" do
sample = SampleLoader.load "delete_regional_secret_with_etag.rb"

refute_nil secret
get_secret_reponse = client.get_secret name: secret_name

updated_etag = get_secret_reponse.etag

assert_output(/Deleted regional secret/) do
sample.run project_id: project_id, location_id: location_id, secret_id: secret_id, etag: updated_etag
end

assert_raises Google::Cloud::NotFoundError do
client.get_secret name: secret_name
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Copyright 2022 Google, Inc
#
# 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.

require "uri"

require_relative "regional_helper"

describe "#destroy_regional_secret_version", :regional_secret_manager_snippet do
it "destroys the secret version" do
sample = SampleLoader.load "destroy_regional_secret_version.rb"

refute_nil secret_version

assert_output(/Destroyed regional secret version/) do
sample.run project_id: project_id, location_id: location_id, secret_id: secret_id, version_id: version_id
end

n_version = client.get_secret_version name: version_name
refute_nil n_version
assert_equal "destroyed", n_version.state.to_s.downcase
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Copyright 2022 Google, Inc
#
# 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.

require "uri"

require_relative "regional_helper"

describe "#destroy_regional_secret_version_with_etag", :regional_secret_manager_snippet do
it "destroys the regional secret version with etag" do
sample = SampleLoader.load "destroy_regional_secret_version_with_etag.rb"

refute_nil secret_version

assert_output(/Destroyed regional secret version/) do
sample.run project_id: project_id, location_id: location_id, secret_id: secret_id, version_id: version_id, etag: etag
end

n_version = client.get_secret_version name: version_name
refute_nil n_version
assert_equal "destroyed", n_version.state.to_s.downcase
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Copyright 2022 Google, Inc
#
# 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.

require "uri"

require_relative "regional_helper"

describe "#disable_regional_secret_version", :regional_secret_manager_snippet do
it "disables the secret version" do
sample = SampleLoader.load "disable_regional_secret_version.rb"

refute_nil secret_version

assert_output(/Disabled regional secret version/) do
sample.run project_id: project_id, location_id: location_id, secret_id: secret_id, version_id: version_id
end

n_version = client.get_secret_version name: version_name
refute_nil n_version
assert_equal "disabled", n_version.state.to_s.downcase
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Copyright 2022 Google, Inc
#
# 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.

require "uri"

require_relative "regional_helper"

describe "#disable_regional_secret_version_with_etag", :regional_secret_manager_snippet do
it "disables the regional secret version with the etag" do
sample = SampleLoader.load "disable_regional_secret_version_with_etag.rb"

refute_nil secret_version

assert_output(/Disabled regional secret version/) do
sample.run project_id: project_id, location_id: location_id, secret_id: secret_id, version_id: version_id, etag: etag
end

n_version = client.get_secret_version name: version_name
refute_nil n_version
assert_equal "disabled", n_version.state.to_s.downcase
end
end
Loading

0 comments on commit 409ca00

Please sign in to comment.