-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
feat(parametermanager): Added samples for delete, enable and disable parameter and parameter version #5253
Changes from all commits
b381466
c3a4431
3bcc35e
c2ee70f
0e5fc16
0999727
3a10195
d23cd22
ba14271
f45dae4
293f2ca
f4db15c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 := ¶metermanagerpb.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) | ||
} | ||
|
||
fmt.Fprintf(w, "Deleted parameter: %s\n", name) | ||
return nil | ||
} | ||
|
||
// [END parametermanager_delete_param] |
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 := ¶metermanagerpb.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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider including the parameter version name in the error message for better context. How about:
Suggested change
|
||||||||
|
||||||||
fmt.Fprintf(w, "Deleted parameter version: %s\n", name) | ||||||||
return nil | ||||||||
} | ||||||||
|
||||||||
// [END parametermanager_delete_param_version] |
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 := ¶metermanagerpb.UpdateParameterVersionRequest{ | ||
UpdateMask: &field_mask.FieldMask{ | ||
Paths: []string{"disabled"}, | ||
}, | ||
ParameterVersion: ¶metermanagerpb.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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} | ||
|
||
fmt.Fprintf(w, "Disabled parameter version %s for parameter %s\n", name, parameterID) | ||
return nil | ||
} | ||
|
||
// [END parametermanager_disable_param_version] |
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 := ¶metermanagerpb.UpdateParameterVersionRequest{ | ||
UpdateMask: &field_mask.FieldMask{ | ||
Paths: []string{"disabled"}, | ||
}, | ||
ParameterVersion: ¶metermanagerpb.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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} | ||
|
||
fmt.Fprintf(w, "Enabled parameter version %s for parameter %s\n", name, parameterID) | ||
return nil | ||
} | ||
|
||
// [END parametermanager_enable_param_version] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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)