Skip to content

Commit

Permalink
samples(secret_manager): add annotation and label samples (#28817)
Browse files Browse the repository at this point in the history
  • Loading branch information
YashSaraf11 authored Feb 7, 2025
1 parent cbfec8f commit 45da8e0
Show file tree
Hide file tree
Showing 26 changed files with 1,548 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Copyright 2025 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_with_annotations", :regional_secret_manager_snippet do
it "creates a regional secret with annotations" do
sample = SampleLoader.load "create_regional_secret_with_annotations.rb"

out, _err = capture_io do
sample.run project_id: project_id, location_id: location_id, secret_id: secret_id, annotation_key: annotation_key, annotation_value: annotation_value
end
secret_id_regex = Regexp.escape secret_id
assert_match %r{Created regional secret with annotations: projects/\S+locations/\S+/secrets/#{secret_id_regex}}, out
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Copyright 2025 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_with_labels", :regional_secret_manager_snippet do
it "creates a regional secret with labels" do
sample = SampleLoader.load "create_regional_secret_with_labels.rb"

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

describe "#create_secret_with_annotations", :secret_manager_snippet do
it "creates a secret with annotations" do
sample = SampleLoader.load "create_secret_with_annotations.rb"

out, _err = capture_io do
sample.run project_id: project_id, secret_id: secret_id, annotation_key: annotation_key, annotation_value: annotation_value
end
secret_id_regex = Regexp.escape secret_id
assert_match %r{Created secret: projects/\S+/secrets/#{secret_id_regex}}, out
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Copyright 2025 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 "helper"

describe "#create_secret_with_labels", :secret_manager_snippet do
it "creates a secret with labels" do
sample = SampleLoader.load "create_secret_with_labels.rb"

out, _err = capture_io do
sample.run project_id: project_id, secret_id: secret_id, label_key: label_key, label_value: label_value
end
secret_id_regex = Regexp.escape secret_id
assert_match %r{Created secret with label: projects/\S+/secrets/#{secret_id_regex}}, out
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Copyright 2025 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 "#edit_regional_secret_annotations", :regional_secret_manager_snippet do
it "edits a regional secret annotations" do
sample = SampleLoader.load "edit_regional_secret_annotations.rb"

refute_nil secret

out, _err = capture_io do
sample.run project_id: project_id, location_id: location_id, secret_id: secret_id, annotation_key: updated_annotation_key, annotation_value: updated_annotation_value
end

assert_match(/Updated regional secret/, out)
assert_match(/New updated annotations/, out)
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Copyright 2025 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 "helper"

describe "#edit_secret_annotations", :secret_manager_snippet do
it "edits the secret annotations" do
sample = SampleLoader.load "edit_secret_annotations.rb"

refute_nil secret

out, _err = capture_io do
sample.run project_id: project_id, secret_id: secret_id, annotation_key: updated_annotation_key, annotation_value: updated_annotation_value
end

assert_match(/Updated secret/, out)
assert_match(/New updated annotations/, out)
end
end
15 changes: 15 additions & 0 deletions google-cloud-secret_manager/samples/acceptance/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,28 @@ class SecretManagerSnippetSpec < Minitest::Spec

let(:iam_user) { "user:[email protected]" }

let(:annotation_key) { "annotation-key" }
let(:annotation_value) { "annotation-value" }

let(:updated_annotation_key) { "updated-annotation-key" }
let(:updated_annotation_value) { "updated-annotation-value" }

let(:label_key) { "label-key" }
let(:label_value) { "label-value" }

let :secret do
client.create_secret(
parent: "projects/#{project_id}",
secret_id: secret_id,
secret: {
replication: {
automatic: {}
},
annotations: {
annotation_key => annotation_value
},
labels: {
label_key => label_value
}
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ class RegionalSecretManagerSnippetSpec < Minitest::Spec
let(:api_endpoint) { "secretmanager.#{location_id}.rep.googleapis.com" }
let(:filter) { "name : ruby-quickstart-" }

let(:annotation_key) { "annotation-key" }
let(:annotation_value) { "annotation-value" }

let(:updated_annotation_key) { "updated-annotation-key" }
let(:updated_annotation_value) { "updated-annotation-value" }

let(:label_key) { "label-key" }
let(:label_value) { "label-value" }

let :client do
Google::Cloud::SecretManager.secret_manager_service do |config|
config.endpoint = api_endpoint
Expand All @@ -41,7 +50,14 @@ class RegionalSecretManagerSnippetSpec < Minitest::Spec
client.create_secret(
parent: "projects/#{project_id}/locations/#{location_id}",
secret_id: secret_id,
secret: {}
secret: {
annotations: {
annotation_key => annotation_value
},
labels: {
label_key => label_value
}
}
)
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,27 @@
let(:secret_name) { "projects/#{project_id}/locations/#{location_id}/secrets/#{secret_id}" }
let(:iam_user) { "user:[email protected]" }

let(:annotation_key) { "annotation-key" }
let(:annotation_value) { "annotation-value" }

let(:updated_annotation_key) { "updated-annotation-key" }
let(:updated_annotation_value) { "updated-annotation-value" }

let(:label_key) { "label-key" }
let(:label_value) { "label-value" }

let :secret do
client.create_secret(
parent: "projects/#{project_id}/locations/#{location_id}",
secret_id: secret_id,
secret: {}
secret: {
annotations: {
annotation_key => annotation_value
},
labels: {
label_key => label_value
}
}
)
end

Expand Down Expand Up @@ -466,4 +482,94 @@
}.must_output(/Updated regional secret/)
end
end

describe "#create_regional_secret_with_annotations" do
it "create regional secret with annotations" do
expect {
secret = create_regional_secret_with_annotations(
project_id: project_id,
location_id: location_id,
secret_id: secret_id,
annotation_key: annotation_key,
annotation_value: annotation_value
)

expect(secret).wont_be_nil
expect(secret.name).must_include(secret_id)
expect(secret.annotations[annotation_key]).must_equal(annotation_value)
}.must_output(/Created regional secret/)
end
end

describe "#create_regional_secret_with_labels" do
it "create regional secret with labels" do
expect {
secret = create_regional_secret_with_labels(
project_id: project_id,
location_id: location_id,
secret_id: secret_id,
label_key: label_key,
label_value: label_value
)

expect(secret).wont_be_nil
expect(secret.name).must_include(secret_id)
expect(secret.labels[label_key]).must_equal(label_value)
}.must_output(/Created regional secret with label/)
end
end

describe "#edit_regional_secret_annotations" do
it "edit regional secret annotations" do
expect(secret).wont_be_nil

expect {
n_secret = edit_regional_secret_annotations(
project_id: project_id,
location_id: location_id,
secret_id: secret_id,
annotation_key: updated_annotation_key,
annotation_value: updated_annotation_value
)

expect(n_secret).wont_be_nil
expect(n_secret.name).must_include(secret_id)
expect(n_secret.annotations[updated_annotation_key]).must_equal(updated_annotation_value)
}.must_output(/Updated regional secret/)
end
end

describe "#view_regional_secret_annotations" do
it "view regional secret annotations" do
expect(secret).wont_be_nil

expect {
secret_annotations = view_regional_secret_annotations(
project_id: project_id,
location_id: location_id,
secret_id: secret_id
)

expect(secret_annotations).wont_be_nil
expect(secret_annotations[annotation_key]).must_equal(annotation_value)
}.must_output(/Annotation Key: #{annotation_key}, Annotation Value: #{annotation_value}/)
end
end

describe "#view_regional_secret_labels" do
it "view regional secret labels" do
expect(secret).wont_be_nil

expect {
secret_labels = view_regional_secret_labels(
project_id: project_id,
location_id: location_id,
secret_id: secret_id
)

expect(secret_labels).wont_be_nil
expect(secret_labels[label_key]).must_equal(label_value)
}.must_output(/Label Key: #{label_key}, Label Value: #{label_value}/)
end
end
end
Loading

0 comments on commit 45da8e0

Please sign in to comment.