Skip to content

Commit 41f0779

Browse files
feat(parametermanager): Added samples for get and list global parameters (#5258)
* feat(parametermanager): Added samples for get and list global parameters * fix(parametermanager): fix linting issue --------- Co-authored-by: Arpan Goswami <[email protected]>
1 parent 0463f52 commit 41f0779

File tree

3 files changed

+175
-0
lines changed

3 files changed

+175
-0
lines changed

parametermanager/get_param.go

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package parametermanager
16+
17+
// [START parametermanager_get_param]
18+
import (
19+
"context"
20+
"fmt"
21+
"io"
22+
23+
parametermanager "cloud.google.com/go/parametermanager/apiv1"
24+
parametermanagerpb "cloud.google.com/go/parametermanager/apiv1/parametermanagerpb"
25+
)
26+
27+
// getParam get parameter using the Parameter Manager SDK for GCP.
28+
//
29+
// w: The io.Writer object used to write the output.
30+
// projectID: The ID of the project where the parameter is located.
31+
// parameterID: The ID of the parameter to retrieved.
32+
//
33+
// The function returns an error if the parameter retrieval fails.
34+
func getParam(w io.Writer, projectID, parameterID string) error {
35+
// Create a context and a Parameter Manager client.
36+
ctx := context.Background()
37+
client, err := parametermanager.NewClient(ctx)
38+
if err != nil {
39+
return fmt.Errorf("failed to create Parameter Manager client: %w", err)
40+
}
41+
defer client.Close()
42+
43+
// Construct the name of the parameter to get parameter.
44+
name := fmt.Sprintf("projects/%s/locations/global/parameters/%s", projectID, parameterID)
45+
46+
// Build the request to get parameter.
47+
req := &parametermanagerpb.GetParameterRequest{
48+
Name: name,
49+
}
50+
51+
// Call the API to get parameter.
52+
param, err := client.GetParameter(ctx, req)
53+
if err != nil {
54+
return fmt.Errorf("failed to get parameter: %w", err)
55+
}
56+
57+
// Find more details for the Parameter object here:
58+
// https://cloud.google.com/secret-manager/parameter-manager/docs/reference/rest/v1/projects.locations.parameters#Parameter
59+
fmt.Fprintf(w, "Found parameter %s with format %s\n", param.Name, param.Format.String())
60+
return nil
61+
}
62+
63+
// [END parametermanager_get_param]

parametermanager/list_param.go

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
// Copyright 2025 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// https://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package parametermanager
16+
17+
// [START parametermanager_list_params]
18+
import (
19+
"context"
20+
"fmt"
21+
"io"
22+
23+
parametermanager "cloud.google.com/go/parametermanager/apiv1"
24+
parametermanagerpb "cloud.google.com/go/parametermanager/apiv1/parametermanagerpb"
25+
"google.golang.org/api/iterator"
26+
)
27+
28+
// listParam lists parameters using the Parameter Manager SDK for GCP.
29+
//
30+
// w: The io.Writer object used to write the output.
31+
// projectID: The ID of the project where the parameters are located.
32+
//
33+
// The function returns an error if the parameter listing fails.
34+
func listParams(w io.Writer, projectID string) error {
35+
// Create a context and a Parameter Manager client.
36+
ctx := context.Background()
37+
client, err := parametermanager.NewClient(ctx)
38+
if err != nil {
39+
return fmt.Errorf("failed to create Parameter Manager client: %w", err)
40+
}
41+
defer client.Close()
42+
43+
// Construct the name of the list parameter.
44+
parent := fmt.Sprintf("projects/%s/locations/global", projectID)
45+
// Build the request to list parameters.
46+
req := &parametermanagerpb.ListParametersRequest{
47+
Parent: parent,
48+
}
49+
50+
// Call the API to list parameters.
51+
it := client.ListParameters(ctx, req)
52+
for {
53+
resp, err := it.Next()
54+
if err == iterator.Done {
55+
break
56+
}
57+
if err != nil {
58+
return fmt.Errorf("failed to list parameters: %w", err)
59+
}
60+
61+
fmt.Fprintf(w, "Found parameter %s with format %s \n", resp.Name, resp.Format.String())
62+
}
63+
64+
return nil
65+
}
66+
67+
// [END parametermanager_list_params]

parametermanager/parametermanager_test.go

+45
Original file line numberDiff line numberDiff line change
@@ -171,3 +171,48 @@ func TestCreateParamVersionWithSecret(t *testing.T) {
171171
t.Errorf("createParameterVersion: expected %q to contain %q", got, want)
172172
}
173173
}
174+
175+
// TestGetParam tests the getParam function by creating a parameter,
176+
// then attempts to retrieve the created parameter. It verifies if the parameter
177+
// was successfully retrieved by checking the output.
178+
func TestGetParam(t *testing.T) {
179+
tc := testutil.SystemTest(t)
180+
181+
parameter, parameterID := testParameter(t, tc.ProjectID, parametermanagerpb.ParameterFormat_JSON)
182+
defer testCleanupParameter(t, parameter.Name)
183+
184+
var b bytes.Buffer
185+
if err := getParam(&b, tc.ProjectID, parameterID); err != nil {
186+
t.Fatal(err)
187+
}
188+
189+
if got, want := b.String(), fmt.Sprintf("Found parameter %s with format JSON", parameter.Name); !strings.Contains(got, want) {
190+
t.Errorf("GetParameter: expected %q to contain %q", got, want)
191+
}
192+
}
193+
194+
// TestListParam tests the listParam function by creating multiple parameters,
195+
// then attempts to list the created parameters. It verifies if the parameters
196+
// were successfully listed by checking the output.
197+
func TestListParam(t *testing.T) {
198+
tc := testutil.SystemTest(t)
199+
200+
parameter1, _ := testParameter(t, tc.ProjectID, parametermanagerpb.ParameterFormat_JSON)
201+
parameter2, _ := testParameter(t, tc.ProjectID, parametermanagerpb.ParameterFormat_UNFORMATTED)
202+
203+
defer testCleanupParameter(t, parameter1.Name)
204+
defer testCleanupParameter(t, parameter2.Name)
205+
206+
var b bytes.Buffer
207+
if err := listParams(&b, tc.ProjectID); err != nil {
208+
t.Fatal(err)
209+
}
210+
211+
if got, want := b.String(), fmt.Sprintf("Found parameter %s with format %s \n", parameter1.Name, parameter1.Format); !strings.Contains(got, want) {
212+
t.Errorf("ListParameter: expected %q to contain %q", got, want)
213+
}
214+
215+
if got, want := b.String(), fmt.Sprintf("Found parameter %s with format %s \n", parameter2.Name, parameter2.Format); !strings.Contains(got, want) {
216+
t.Errorf("ListParameter: expected %q to contain %q", got, want)
217+
}
218+
}

0 commit comments

Comments
 (0)