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 samples for delete, enable or disable parameter or parameter version #10050

Closed
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* 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
*
* http://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 com.google.cloud.parametermanager.v1.ParameterManagerClient;
import com.google.cloud.parametermanager.v1.ParameterName;
import java.io.IOException;

/** This class demonstrates how to delete a parameter using the Parameter Manager SDK for GCP. */
public class DeleteParam {

public static void main(String[] args) throws IOException {
// TODO(developer): Replace these variables before running the sample.
String projectId = "your-project-id";
String parameterId = "your-parameter-id";

// Call the method to delete a parameter.
deleteParam(projectId, parameterId);
}

// This is an example snippet for deleting a parameter.
public static void deleteParam(String projectId, String parameterId) throws IOException {
// Initialize the client that will be used to send requests. This client only
// needs to be created once, and can be reused for multiple requests.
try (ParameterManagerClient client = ParameterManagerClient.create()) {
String locationId = "global";

// Build the parameter name.
ParameterName parameterName = ParameterName.of(projectId, locationId, parameterId);

// Delete the parameter.
client.deleteParameter(parameterName);
System.out.printf("Deleted parameter: %s\n", parameterName.toString());
}
}
}
// [END parametermanager_delete_param]
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* 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
*
* http://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 com.google.cloud.parametermanager.v1.ParameterManagerClient;
import com.google.cloud.parametermanager.v1.ParameterVersionName;
import java.io.IOException;

/**
* This class demonstrates how to delete a parameter version using the Parameter Manager SDK for
* GCP.
*/
public class DeleteParamVersion {

public static void main(String[] args) throws IOException {
// TODO(developer): Replace these variables before running the sample.
String projectId = "your-project-id";
String parameterId = "your-parameter-id";
String versionId = "your-version-id";

// Call the method to delete a parameter version.
deleteParamVersion(projectId, parameterId, versionId);
}

// This is an example snippet for deleting a parameter version.
public static void deleteParamVersion(String projectId, String parameterId, String versionId)
throws IOException {
// Initialize the client that will be used to send requests. This client only
// needs to be created once, and can be reused for multiple requests.
try (ParameterManagerClient client = ParameterManagerClient.create()) {
String locationId = "global";

// Build the parameter version name.
ParameterVersionName parameterVersionName =
ParameterVersionName.of(projectId, locationId, parameterId, versionId);

// Delete the parameter version.
client.deleteParameterVersion(parameterVersionName);
System.out.printf("Deleted parameter version: %s\n", parameterVersionName.toString());
}
}
}
// [END parametermanager_delete_param_version]
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* 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
*
* http://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 com.google.cloud.parametermanager.v1.ParameterManagerClient;
import com.google.cloud.parametermanager.v1.ParameterVersion;
import com.google.cloud.parametermanager.v1.ParameterVersionName;
import com.google.protobuf.FieldMask;
import com.google.protobuf.util.FieldMaskUtil;
import java.io.IOException;

/**
* This class demonstrates how to disable a parameter version using the Parameter Manager SDK for
* GCP.
*/
public class DisableParamVersion {

public static void main(String[] args) throws IOException {
// TODO(developer): Replace these variables before running the sample.
String projectId = "your-project-id";
String parameterId = "your-parameter-id";
String versionId = "your-version-id";

// Call the method to disable a parameter version.
disableParamVersion(projectId, parameterId, versionId);
}

// This is an example snippet for disabling a parameter version.
public static ParameterVersion disableParamVersion(
String projectId, String parameterId, String versionId) throws IOException {
// Initialize the client that will be used to send requests. This client only
// needs to be created once, and can be reused for multiple requests.
try (ParameterManagerClient client = ParameterManagerClient.create()) {
String locationId = "global";

// Build the parameter version name.
ParameterVersionName parameterVersionName =
ParameterVersionName.of(projectId, locationId, parameterId, versionId);

// Set the parameter version to disable.
ParameterVersion parameterVersion =
ParameterVersion.newBuilder()
.setName(parameterVersionName.toString())
.setDisabled(true)
.build();

// Build the field mask for the disabled field.
FieldMask fieldMask = FieldMaskUtil.fromString("disabled");

// Update the parameter version to disable it.
ParameterVersion disabledParameterVersion =
client.updateParameterVersion(parameterVersion, fieldMask);
System.out.printf(
"Disabled parameter version %s for parameter %s\n",
disabledParameterVersion.getName(), parameterId);

return disabledParameterVersion;
}
}
}
// [END parametermanager_disable_param_version]
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* 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
*
* http://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 com.google.cloud.parametermanager.v1.ParameterManagerClient;
import com.google.cloud.parametermanager.v1.ParameterVersion;
import com.google.cloud.parametermanager.v1.ParameterVersionName;
import com.google.protobuf.FieldMask;
import com.google.protobuf.util.FieldMaskUtil;
import java.io.IOException;

/**
* This class demonstrates how to enable a parameter version using the Parameter Manager SDK for
* GCP.
*/
public class EnableParamVersion {

public static void main(String[] args) throws IOException {
// TODO(developer): Replace these variables before running the sample.
String projectId = "your-project-id";
String parameterId = "your-parameter-id";
String versionId = "your-version-id";

// Call the method to enable a parameter version.
enableParamVersion(projectId, parameterId, versionId);
}

// This is an example snippet for enabling a parameter version.
public static ParameterVersion enableParamVersion(
String projectId, String parameterId, String versionId) throws IOException {
// Initialize the client that will be used to send requests. This client only
// needs to be created once, and can be reused for multiple requests.
try (ParameterManagerClient client = ParameterManagerClient.create()) {
String locationId = "global";

// Build the parameter version name.
ParameterVersionName parameterVersionName =
ParameterVersionName.of(projectId, locationId, parameterId, versionId);

// Set the parameter version to enable.
ParameterVersion parameterVersion =
ParameterVersion.newBuilder()
.setName(parameterVersionName.toString())
.setDisabled(false)
.build();

// Build the field mask for the disabled field.
FieldMask fieldMask = FieldMaskUtil.fromString("disabled");

// Update the parameter version to enable it.
ParameterVersion enabledParameterVersion =
client.updateParameterVersion(parameterVersion, fieldMask);
System.out.printf(
"Enabled parameter version %s for parameter %s\n",
enabledParameterVersion.getName(), parameterId);

return enabledParameterVersion;
}
}
}
// [END parametermanager_enable_param_version]
41 changes: 41 additions & 0 deletions parametermanager/src/test/java/parametermanager/SnippetsIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,47 @@ public void afterEach() {
System.setOut(null);
}

@Test
public void testDisableParamVersion() throws IOException {
ParameterVersionName parameterVersionName = TEST_PARAMETER_VERSION_NAME_TO_GET_1;
DisableParamVersion.disableParamVersion(
parameterVersionName.getProject(),
parameterVersionName.getParameter(),
parameterVersionName.getParameterVersion());

assertThat(stdOut.toString()).contains("Disabled parameter version");
}

@Test
public void testEnableParamVersion() throws IOException {
ParameterVersionName parameterVersionName = TEST_PARAMETER_VERSION_NAME_TO_GET_1;
EnableParamVersion.enableParamVersion(
parameterVersionName.getProject(),
parameterVersionName.getParameter(),
parameterVersionName.getParameterVersion());

assertThat(stdOut.toString()).contains("Enabled parameter version");
}

@Test
public void testDeleteParamVersion() throws IOException {
ParameterVersionName parameterVersionName = TEST_PARAMETER_VERSION_NAME_TO_DELETE;
DeleteParamVersion.deleteParamVersion(
parameterVersionName.getProject(),
parameterVersionName.getParameter(),
parameterVersionName.getParameterVersion());

assertThat(stdOut.toString()).contains("Deleted parameter version:");
}

@Test
public void testDeleteParam() throws IOException {
ParameterName parameterName = TEST_PARAMETER_NAME_TO_DELETE;
DeleteParam.deleteParam(parameterName.getProject(), parameterName.getParameter());

assertThat(stdOut.toString()).contains("Deleted parameter:");
}

@Test
public void testGetParam() throws IOException {
ParameterName parameterName = TEST_PARAMETER_NAME_TO_GET;
Expand Down