Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(parametermanager): added global and regional samples for parameter manager #5164

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
/dlp/ @GoogleCloudPlatform/go-samples-reviewers @GoogleCloudPlatform/cloud-samples-reviewers @GoogleCloudPlatform/googleapis-dlp
/healthcare/ @GoogleCloudPlatform/go-samples-reviewers @GoogleCloudPlatform/cloud-samples-reviewers @GoogleCloudPlatform/healthcare-life-sciences
/media/ @GoogleCloudPlatform/go-samples-reviewers @GoogleCloudPlatform/cloud-samples-reviewers @GoogleCloudPlatform/cloud-media-team
/parametermanager/ @GoogleCloudPlatform/go-samples-reviewers @GoogleCloudPlatform/cloud-samples-reviewers
/secretmanager/ @GoogleCloudPlatform/go-samples-reviewers @GoogleCloudPlatform/cloud-samples-reviewers @GoogleCloudPlatform/cloud-secrets-team
/securitycenter/ @GoogleCloudPlatform/go-samples-reviewers @GoogleCloudPlatform/cloud-samples-reviewers @GoogleCloudPlatform/gcp-security-command-center
/translate/ @GoogleCloudPlatform/go-samples-reviewers @GoogleCloudPlatform/cloud-samples-reviewers @GoogleCloudPlatform/cloud-ml-translate-dev
Expand Down
1 change: 1 addition & 0 deletions go.work
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ use (
./monitoring
./opentelemetry/instrumentation/app
./opentelemetry/trace
./parametermanager
./privateca
./profiler
./profiler/shakesapp
Expand Down
64 changes: 64 additions & 0 deletions parametermanager/create_param.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright 2025 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
//
// https://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.

package parametermanager

// [START parametermanager_create_param]
import (
"context"
"fmt"
"io"

parametermanager "cloud.google.com/go/parametermanager/apiv1"
parametermanagerpb "cloud.google.com/go/parametermanager/apiv1/parametermanagerpb"
)

// createParam creates a new parameter with the format type "unformatted" in Parameter Manager.
//
// w: The io.Writer object used to write the output.
// projectID: The ID of the project where the parameter is located.
// parameterID: The ID of the parameter to be created.
//
// The function returns an error if the parameter creation fails.
func createParam(w io.Writer, projectID, parameterID string) error {
// Create a context and a Parameter Manager client.
ctx := context.Background()
client, err := parametermanager.NewClient(ctx)
if err != nil {
return fmt.Errorf("Failed to create Parameter Manager client: %v\n", err)
}
defer client.Close()

// Construct the name of the create parameter.
parent := fmt.Sprintf("projects/%s/locations/global", projectID)

// Build the request to create a new parameter
req := &parametermanagerpb.CreateParameterRequest{
Parent: parent,
ParameterId: parameterID,
Parameter: &parametermanagerpb.Parameter{},
}

// Call the API to create the parameter.
parameter, err := client.CreateParameter(ctx, req)
if err != nil {
return fmt.Errorf("Failed to create parameter: %v\n", err)
}

// Print the name of the created parameter.
fmt.Fprintf(w, "Created parameter: %s\n", parameter.Name)
return nil
}

// [END parametermanager_create_param]
70 changes: 70 additions & 0 deletions parametermanager/create_param_version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright 2025 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
//
// https://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.

package parametermanager

// [START parametermanager_create_param_version]
import (
"context"
"fmt"
"io"

parametermanager "cloud.google.com/go/parametermanager/apiv1"
parametermanagerpb "cloud.google.com/go/parametermanager/apiv1/parametermanagerpb"
)

// createParamVersion creates a new version of a parameter with an unformatted payload in Parameter Manager.
//
// w: The io.Writer object used to write the output.
// projectID: The ID of the project where the parameter is located.
// parameterID: The ID of the parameter for which the version is to be created.
// versionID: The ID of the version to be created.
// payload: The unformatted string payload to be stored in the new parameter version.
//
// The function returns an error if the parameter version creation fails.
func createParamVersion(w io.Writer, projectID, parameterID, versionID, payload string) error {
// Create a context and a Parameter Manager client.
ctx := context.Background()
client, err := parametermanager.NewClient(ctx)
if err != nil {
return fmt.Errorf("Failed to create Parameter Manager client: %v\n", err)
}
defer client.Close()

// Construct the name of the create parameter version.
parent := fmt.Sprintf("projects/%s/locations/global/parameters/%s", projectID, parameterID)

// Build the request to create a new parameter version with the unformatted payload.
req := &parametermanagerpb.CreateParameterVersionRequest{
Parent: parent,
ParameterVersionId: versionID,
ParameterVersion: &parametermanagerpb.ParameterVersion{
Payload: &parametermanagerpb.ParameterVersionPayload{
Data: []byte(payload), // Set the unformatted payload data.
},
},
}

// Call the API to create the parameter version.
version, err := client.CreateParameterVersion(ctx, req)
if err != nil {
return fmt.Errorf("Failed to create parameter version: %v\n", err)
}

// Print the name of the created parameter version.
fmt.Fprintf(w, "Created parameter version: %s\n", version.Name)
return nil
}

// [END parametermanager_create_param_version]
72 changes: 72 additions & 0 deletions parametermanager/create_param_version_with_secret.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// Copyright 2025 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
//
// https://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.

package parametermanager

// [START parametermanager_create_param_version_with_secret]
import (
"context"
"fmt"
"io"

parametermanager "cloud.google.com/go/parametermanager/apiv1"
parametermanagerpb "cloud.google.com/go/parametermanager/apiv1/parametermanagerpb"
)

// createParamVersionWithSecret creates a new version of a parameter with a JSON payload that has a secret reference in it using the Parameter Manager SDK for GCP.
//
// w: The io.Writer object used to write the output.
// projectID: The ID of the project where the parameter is located.
// parameterID: The ID of the parameter for which the version is to be created.
// versionID: The ID of the version to be created.
// secretID: The ID of the secret to be referenced.
//
// The function returns an error if the parameter version creation fails.
func createParamVersionWithSecret(w io.Writer, projectID, parameterID, versionID, secretID string) error {
// Create a context and a Parameter Manager client.
ctx := context.Background()
client, err := parametermanager.NewClient(ctx)
if err != nil {
return fmt.Errorf("Failed to create Parameter Manager client: %v\n", err)
}
defer client.Close()

// Construct the name of the create parameter version.
parent := fmt.Sprintf("projects/%s/locations/global/parameters/%s", projectID, parameterID)

payload := []byte(fmt.Sprintf(`{"username": "test-user","password": "__REF__(//secretmanager.googleapis.com/%s)"}`, secretID))

// Build the request to create a new parameter version with the JSON payload that has a secret reference.
req := &parametermanagerpb.CreateParameterVersionRequest{
Parent: parent,
ParameterVersionId: versionID,
ParameterVersion: &parametermanagerpb.ParameterVersion{
Payload: &parametermanagerpb.ParameterVersionPayload{
Data: payload,
},
},
}

// Call the API to create the parameter version.
version, err := client.CreateParameterVersion(ctx, req)
if err != nil {
return fmt.Errorf("Failed to create parameter version: %v\n", err)
}

// Print the name of the created parameter version.
fmt.Fprintf(w, "Created parameter version: %s\n", version.Name)
return nil
}

// [END parametermanager_create_param_version_with_secret]
67 changes: 67 additions & 0 deletions parametermanager/create_structured_param.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Copyright 2025 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
//
// https://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.

package parametermanager

// [START parametermanager_create_structured_param]
import (
"context"
"fmt"
"io"

parametermanager "cloud.google.com/go/parametermanager/apiv1"
parametermanagerpb "cloud.google.com/go/parametermanager/apiv1/parametermanagerpb"
)

// createStructuredParam creates a new parameter with the specified format in Parameter Manager.
//
// w: The io.Writer object used to write the output.
// projectID: The ID of the project where the parameter is located.
// parameterID: The ID of the parameter to be created.
// format: The format type of the parameter (UNFORMATTED, YAML, JSON).
//
// The function returns an error if the parameter creation fails.
func createStructuredParam(w io.Writer, projectID, parameterID string, format parametermanagerpb.ParameterFormat) error {
// Create a context and a Parameter Manager client.
ctx := context.Background()
client, err := parametermanager.NewClient(ctx)
if err != nil {
return fmt.Errorf("Failed to create Parameter Manager client: %v\n", err)
}
defer client.Close()

// Construct the name of the create parameter.
parent := fmt.Sprintf("projects/%s/locations/global", projectID)

// Build the request to create a new parameter with the specified format.
req := &parametermanagerpb.CreateParameterRequest{
Parent: parent,
ParameterId: parameterID,
Parameter: &parametermanagerpb.Parameter{
Format: format,
},
}

// Call the API to create the parameter.
parameter, err := client.CreateParameter(ctx, req)
if err != nil {
return fmt.Errorf("Failed to create parameter: %v\n", err)
}

// Print the name of the created parameter.
fmt.Fprintf(w, "Created parameter with format: %s\n", parameter.Name)
return nil
}

// [END parametermanager_create_structured_param]
70 changes: 70 additions & 0 deletions parametermanager/create_structured_param_version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright 2025 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
//
// https://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.

package parametermanager

// [START parametermanager_create_structured_param_version]
import (
"context"
"fmt"
"io"

parametermanager "cloud.google.com/go/parametermanager/apiv1"
parametermanagerpb "cloud.google.com/go/parametermanager/apiv1/parametermanagerpb"
)

// createStructuredParamVersion creates a new version of a parameter with a JSON payload in Parameter Manager.
//
// w: The io.Writer object used to write the output.
// projectID: The ID of the project where the parameter is located.
// parameterID: The ID of the parameter for which the version is to be created.
// versionID: The ID of the version to be created.
// payload: The JSON dictionary payload to be stored in the new parameter version.
//
// The function returns an error if the parameter version creation fails.
func createStructuredParamVersion(w io.Writer, projectID, parameterID, versionID, payload string) error {
// Create a context and a Parameter Manager client.
ctx := context.Background()
client, err := parametermanager.NewClient(ctx)
if err != nil {
return fmt.Errorf("Failed to create Parameter Manager client: %v\n", err)
}
defer client.Close()

// Construct the name of the create parameter version.
parent := fmt.Sprintf("projects/%s/locations/global/parameters/%s", projectID, parameterID)

// Build the request to create a new parameter version with the JSON payload.
req := &parametermanagerpb.CreateParameterVersionRequest{
Parent: parent,
ParameterVersionId: versionID,
ParameterVersion: &parametermanagerpb.ParameterVersion{
Payload: &parametermanagerpb.ParameterVersionPayload{
Data: []byte(payload), // Set the JSON payload data.
},
},
}

// Call the API to create the parameter version.
version, err := client.CreateParameterVersion(ctx, req)
if err != nil {
return fmt.Errorf("Failed to create parameter version: %v\n", err)
}

// Print the name of the created parameter version.
fmt.Fprintf(w, "Created parameter version: %s\n", version.Name)
return nil
}

// [END parametermanager_create_structured_param_version]
Loading