Skip to content

feat(parametermanager): Added samples for delete, enable and disable parameter and parameter version #5253

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

Merged
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
63 changes: 63 additions & 0 deletions parametermanager/delete_param.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// 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_delete_param]
import (
"context"
"fmt"
"io"

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

// deleteParam deletes a parameter 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 to be deleted.
//
// The function returns an error if the parameter deletion fails.
func deleteParam(w io.Writer, projectID, parameterID string) error {
// Create a new context.
ctx := context.Background()

// Initialize a Parameter Manager client.
client, err := parametermanager.NewClient(ctx)
if err != nil {
return fmt.Errorf("failed to create Parameter Manager client: %w", err)
}
defer client.Close()

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

// Build the request to delete the parameter.
req := &parametermanagerpb.DeleteParameterRequest{
Name: name,
}

// Call the API to delete the parameter.
err = client.DeleteParameter(ctx, req)
if err != nil {
return fmt.Errorf("failed to delete parameter: %w", err)
}
Comment on lines +55 to +57

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Consider including the parameter name in the error message for better context.

How about: return fmt.Errorf("failed to delete parameter %s: %w", name, err)

return fmt.Errorf("failed to delete parameter %s: %w", name, err)


fmt.Fprintf(w, "Deleted parameter: %s\n", name)
return nil
}

// [END parametermanager_delete_param]
63 changes: 63 additions & 0 deletions parametermanager/delete_param_version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// 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_delete_param_version]
import (
"context"
"fmt"
"io"

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

// deleteParamVersion deletes a parameter version 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 deleted.
// versionID: The ID of the version to be deleted.
//
// The function returns an error if the parameter version deletion fails.
func deleteParamVersion(w io.Writer, projectID, parameterID, versionID string) error {
// Create a new context.
ctx := context.Background()

// Initialize a Parameter Manager client.
client, err := parametermanager.NewClient(ctx)
if err != nil {
return fmt.Errorf("failed to create Parameter Manager client: %w", err)
}
defer client.Close()

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

// Build the request to delete the parameter version.
req := &parametermanagerpb.DeleteParameterVersionRequest{
Name: name,
}

// Call the API to delete the parameter version.
if err := client.DeleteParameterVersion(ctx, req); err != nil {
return fmt.Errorf("failed to delete parameter version: %w", err)
}
Comment on lines +56 to +57

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Consider including the parameter version name in the error message for better context.

How about: return fmt.Errorf("failed to delete parameter version %s: %w", name, err)

Suggested change
return fmt.Errorf("failed to delete parameter version: %w", err)
}
return fmt.Errorf("failed to delete parameter version %s: %w", name, err)


fmt.Fprintf(w, "Deleted parameter version: %s\n", name)
return nil
}

// [END parametermanager_delete_param_version]
68 changes: 68 additions & 0 deletions parametermanager/disable_param_vesion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// 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_disable_param_version]
import (
"context"
"fmt"
"io"

parametermanager "cloud.google.com/go/parametermanager/apiv1"
parametermanagerpb "cloud.google.com/go/parametermanager/apiv1/parametermanagerpb"
"google.golang.org/genproto/protobuf/field_mask"
)

// disableParamVersion disables a parameter version.
//
// 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 disabled.
// versionID: The ID of the version to be disabled.
//
// The function returns an error if the parameter version update fails.
func disableParamVersion(w io.Writer, projectID, parameterID, versionID string) error {
// Create the client.
ctx := context.Background()
client, err := parametermanager.NewClient(ctx)
if err != nil {
return fmt.Errorf("failed to create parametermanager client: %w", err)
}
defer client.Close()

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

// Build the request to disable the parameter version by updating the parameter version.
req := &parametermanagerpb.UpdateParameterVersionRequest{
UpdateMask: &field_mask.FieldMask{
Paths: []string{"disabled"},
},
ParameterVersion: &parametermanagerpb.ParameterVersion{
Name: name,
Disabled: true,
},
}

// Call the API to disable the parameter version.
if _, err := client.UpdateParameterVersion(ctx, req); err != nil {
return fmt.Errorf("failed to disable parameter version: %w", err)
Comment on lines +60 to +61

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Consider including the parameter version name in the error message for better context.

How about: return fmt.Errorf("failed to disable parameter version %s: %w", name, err)

return fmt.Errorf("failed to disable parameter version %s: %w", name, err)

}

fmt.Fprintf(w, "Disabled parameter version %s for parameter %s\n", name, parameterID)
return nil
}

// [END parametermanager_disable_param_version]
68 changes: 68 additions & 0 deletions parametermanager/enable_param_version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// 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_enable_param_version]
import (
"context"
"fmt"
"io"

parametermanager "cloud.google.com/go/parametermanager/apiv1"
parametermanagerpb "cloud.google.com/go/parametermanager/apiv1/parametermanagerpb"
"google.golang.org/genproto/protobuf/field_mask"
)

// enableParamVersion enables a parameter version.
//
// 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 enabled.
// versionID: The ID of the version to be enabled.
//
// The function returns an error if the parameter version update fails.
func enableParamVersion(w io.Writer, projectID, parameterID, versionID string) error {
// Create the client.
ctx := context.Background()
client, err := parametermanager.NewClient(ctx)
if err != nil {
return fmt.Errorf("failed to create parametermanager client: %w", err)
}
defer client.Close()

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

// Build the request to enable the parameter version by updating the parameter version.
req := &parametermanagerpb.UpdateParameterVersionRequest{
UpdateMask: &field_mask.FieldMask{
Paths: []string{"disabled"},
},
ParameterVersion: &parametermanagerpb.ParameterVersion{
Name: name,
Disabled: false,
},
}

// Call the API to enable the parameter version.
if _, err := client.UpdateParameterVersion(ctx, req); err != nil {
return fmt.Errorf("failed to enable parameter version: %w", err)
Comment on lines +60 to +61

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Consider including the parameter version name in the error message for better context.

How about: return fmt.Errorf("failed to enable parameter version %s: %w", name, err)

return fmt.Errorf("failed to enable parameter version %s: %w", name, err)

}

fmt.Fprintf(w, "Enabled parameter version %s for parameter %s\n", name, parameterID)
return nil
}

// [END parametermanager_enable_param_version]
50 changes: 25 additions & 25 deletions parametermanager/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,25 @@ go 1.23.0

require (
cloud.google.com/go/parametermanager v0.2.0
github.com/GoogleCloudPlatform/golang-samples v0.0.0-20250409195806-b3db6e18b370
github.com/GoogleCloudPlatform/golang-samples v0.0.0-20250414233709-a153c3293cb0
github.com/gofrs/uuid v4.4.0+incompatible
google.golang.org/api v0.228.0
google.golang.org/api v0.229.0
google.golang.org/genproto v0.0.0-20250414145226-207652e42e2e
google.golang.org/grpc v1.71.1
)

require (
cel.dev/expr v0.19.1 // indirect
cloud.google.com/go v0.118.3 // indirect
cloud.google.com/go/auth v0.15.0 // indirect
cel.dev/expr v0.19.2 // indirect
cloud.google.com/go v0.120.0 // indirect
cloud.google.com/go/auth v0.16.0 // indirect
cloud.google.com/go/auth/oauth2adapt v0.2.8 // indirect
cloud.google.com/go/compute/metadata v0.6.0 // indirect
cloud.google.com/go/iam v1.4.2 // indirect
cloud.google.com/go/monitoring v1.24.0 // indirect
cloud.google.com/go/iam v1.5.0 // indirect
cloud.google.com/go/monitoring v1.24.1 // indirect
cloud.google.com/go/storage v1.50.0 // indirect
github.com/GoogleCloudPlatform/opentelemetry-operations-go/detectors/gcp v1.25.0 // indirect
github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/metric v0.49.0 // indirect
github.com/GoogleCloudPlatform/opentelemetry-operations-go/internal/resourcemapping v0.49.0 // indirect
github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/metric v0.50.0 // indirect
github.com/GoogleCloudPlatform/opentelemetry-operations-go/internal/resourcemapping v0.50.0 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/cncf/xds/go v0.0.0-20250121191232-2f005788dc42 // indirect
github.com/envoyproxy/go-control-plane/envoy v1.32.4 // indirect
Expand All @@ -36,22 +37,21 @@ require (
github.com/planetscale/vtprotobuf v0.6.1-0.20240319094008-0393e58bdf10 // indirect
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
go.opentelemetry.io/contrib/detectors/gcp v1.34.0 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.59.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.59.0 // indirect
go.opentelemetry.io/otel v1.34.0 // indirect
go.opentelemetry.io/otel/metric v1.34.0 // indirect
go.opentelemetry.io/otel/sdk v1.34.0 // indirect
go.opentelemetry.io/otel/sdk/metric v1.34.0 // indirect
go.opentelemetry.io/otel/trace v1.34.0 // indirect
golang.org/x/crypto v0.36.0 // indirect
golang.org/x/net v0.37.0 // indirect
golang.org/x/oauth2 v0.28.0 // indirect
golang.org/x/sync v0.12.0 // indirect
golang.org/x/sys v0.31.0 // indirect
golang.org/x/text v0.23.0 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.60.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.60.0 // indirect
go.opentelemetry.io/otel v1.35.0 // indirect
go.opentelemetry.io/otel/metric v1.35.0 // indirect
go.opentelemetry.io/otel/sdk v1.35.0 // indirect
go.opentelemetry.io/otel/sdk/metric v1.35.0 // indirect
go.opentelemetry.io/otel/trace v1.35.0 // indirect
golang.org/x/crypto v0.37.0 // indirect
golang.org/x/net v0.39.0 // indirect
golang.org/x/oauth2 v0.29.0 // indirect
golang.org/x/sync v0.13.0 // indirect
golang.org/x/sys v0.32.0 // indirect
golang.org/x/text v0.24.0 // indirect
golang.org/x/time v0.11.0 // indirect
google.golang.org/genproto v0.0.0-20250303144028-a0af3efb3deb // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20250313205543-e70fdf4c4cb4 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20250313205543-e70fdf4c4cb4 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20250409194420-de1ac958c67a // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20250414145226-207652e42e2e // indirect
google.golang.org/protobuf v1.36.6 // indirect
)
Loading
Loading