Skip to content

Commit a153c32

Browse files
feat(parametermanager): Added samples for create global parameter (#5255)
* feat(parametermanager): Added samples for create global parameter * fix(parametermanager): Fix lint issue * fix(parametermanager): fix linting issue * fix(parametermanager): update variable's name
1 parent 49adefe commit a153c32

File tree

3 files changed

+165
-0
lines changed

3 files changed

+165
-0
lines changed

parametermanager/create_param.go

Lines changed: 63 additions & 0 deletions
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_create_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+
// createParam creates a new parameter with the format type "unformatted" in Parameter Manager.
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 be created.
32+
//
33+
// The function returns an error if the parameter creation fails.
34+
func createParam(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 create parameter.
44+
parent := fmt.Sprintf("projects/%s/locations/global", projectID)
45+
46+
// Build the request to create a new parameter
47+
req := &parametermanagerpb.CreateParameterRequest{
48+
Parent: parent,
49+
ParameterId: parameterID,
50+
Parameter: &parametermanagerpb.Parameter{},
51+
}
52+
53+
// Call the API to create the parameter.
54+
parameter, err := client.CreateParameter(ctx, req)
55+
if err != nil {
56+
return fmt.Errorf("failed to create parameter: %w", err)
57+
}
58+
59+
fmt.Fprintf(w, "Created parameter: %s\n", parameter.Name)
60+
return nil
61+
}
62+
63+
// [END parametermanager_create_param]
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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_create_structured_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+
// createStructuredParam creates a new parameter with the specified format in Parameter Manager.
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 be created.
32+
// format: The format type of the parameter (UNFORMATTED, YAML, JSON).
33+
//
34+
// The function returns an error if the parameter creation fails.
35+
func createStructuredParam(w io.Writer, projectID, parameterID string, format parametermanagerpb.ParameterFormat) error {
36+
// Create a context and a Parameter Manager client.
37+
ctx := context.Background()
38+
client, err := parametermanager.NewClient(ctx)
39+
if err != nil {
40+
return fmt.Errorf("failed to create Parameter Manager client: %w", err)
41+
}
42+
defer client.Close()
43+
44+
// Construct the name of the create parameter.
45+
parent := fmt.Sprintf("projects/%s/locations/global", projectID)
46+
47+
// Build the request to create a new parameter with the specified format.
48+
req := &parametermanagerpb.CreateParameterRequest{
49+
Parent: parent,
50+
ParameterId: parameterID,
51+
Parameter: &parametermanagerpb.Parameter{
52+
Format: format,
53+
},
54+
}
55+
56+
// Call the API to create the parameter.
57+
parameter, err := client.CreateParameter(ctx, req)
58+
if err != nil {
59+
return fmt.Errorf("failed to create parameter: %w", err)
60+
}
61+
62+
fmt.Fprintf(w, "Created parameter %s with format %s\n", parameter.Name, parameter.Format.String())
63+
return nil
64+
}
65+
66+
// [END parametermanager_create_structured_param]

parametermanager/parametermanager_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,42 @@ func testCleanupParameterVersion(t *testing.T, name string) {
111111
}
112112
}
113113

114+
// TestCreateParam tests the createParam function by creating a parameter,
115+
// then verifies if the parameter was successfully created by checking the output.
116+
func TestCreateParam(t *testing.T) {
117+
tc := testutil.SystemTest(t)
118+
119+
parameterID := testName(t)
120+
121+
var buf bytes.Buffer
122+
if err := createParam(&buf, tc.ProjectID, parameterID); err != nil {
123+
t.Fatal(err)
124+
}
125+
defer testCleanupParameter(t, fmt.Sprintf("projects/%s/locations/global/parameters/%s", tc.ProjectID, parameterID))
126+
127+
if got, want := buf.String(), "Created parameter:"; !strings.Contains(got, want) {
128+
t.Errorf("createParameter: expected %q to contain %q", got, want)
129+
}
130+
}
131+
132+
// TestCreateStructuredParam tests the createStructuredParam function by creating a structured parameter,
133+
// then verifies if the parameter was successfully created by checking the output.
134+
func TestCreateStructuredParam(t *testing.T) {
135+
tc := testutil.SystemTest(t)
136+
137+
parameterID := testName(t)
138+
139+
var buf bytes.Buffer
140+
if err := createStructuredParam(&buf, tc.ProjectID, parameterID, parametermanagerpb.ParameterFormat_JSON); err != nil {
141+
t.Fatal(err)
142+
}
143+
defer testCleanupParameter(t, fmt.Sprintf("projects/%s/locations/global/parameters/%s", tc.ProjectID, parameterID))
144+
145+
if got, want := buf.String(), fmt.Sprintf("Created parameter %s with format JSON", fmt.Sprintf("projects/%s/locations/global/parameters/%s", tc.ProjectID, parameterID)); !strings.Contains(got, want) {
146+
t.Errorf("createParameter: expected %q to contain %q", got, want)
147+
}
148+
}
149+
114150
// TestCreateStructuredParamVersion tests the createStructuredParamVersion function by creating a structured parameter version,
115151
// then verifies if the parameter version was successfully created by checking the output.
116152
func TestCreateStructuredParamVersion(t *testing.T) {

0 commit comments

Comments
 (0)