Skip to content

feat(parametermanager): Added samples for delete, enable or disable parameter or parameter version in specified region #2935

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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.
*/

using Google.Cloud.ParameterManager.V1;

[Collection(nameof(ParameterManagerRegionalFixture))]
public class DeleteRegionalParameterTests
{
private readonly ParameterManagerRegionalFixture _fixture;
private readonly DeleteRegionalParameterSample _sample;

public DeleteRegionalParameterTests(ParameterManagerRegionalFixture fixture)
{
_fixture = fixture;
_sample = new DeleteRegionalParameterSample();
}

[Fact]
public void DeleteRegionalParameter()
{
ParameterName parameterName = _fixture.ParameterNameToDelete;
_sample.DeleteRegionalParameter(projectId: parameterName.ProjectId, locationId: ParameterManagerRegionalFixture.LocationId, parameterId: parameterName.ParameterId);

ParameterManagerClient client = ParameterManagerClient.Create();
Assert.Throws<Grpc.Core.RpcException>(() => client.GetParameter(parameterName));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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.
*/

using Google.Cloud.ParameterManager.V1;

[Collection(nameof(ParameterManagerRegionalFixture))]
public class DeleteRegionalParameterVersionTests
{
private readonly ParameterManagerRegionalFixture _fixture;
private readonly DeleteRegionalParameterVersionSample _sample;

public DeleteRegionalParameterVersionTests(ParameterManagerRegionalFixture fixture)
{
_fixture = fixture;
_sample = new DeleteRegionalParameterVersionSample();
}

[Fact]
public void DeleteRegionalParameterVersion()
{
ParameterVersionName parameterVersionName = _fixture.ParameterVersionNameToDelete;
_sample.DeleteRegionalParameterVersion(projectId: parameterVersionName.ProjectId, locationId: ParameterManagerRegionalFixture.LocationId, parameterId: parameterVersionName.ParameterId, versionId: parameterVersionName.ParameterVersionId);

ParameterManagerClient client = ParameterManagerClient.Create();
Assert.Throws<Grpc.Core.RpcException>(() => client.GetParameterVersion(parameterVersionName));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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.
*/

using Google.Cloud.ParameterManager.V1;

[Collection(nameof(ParameterManagerRegionalFixture))]
public class DisableRegionalParameterVersionTests
{
private readonly ParameterManagerRegionalFixture _fixture;
private readonly DisableRegionalParameterVersionSample _sample;

public DisableRegionalParameterVersionTests(ParameterManagerRegionalFixture fixture)
{
_fixture = fixture;
_sample = new DisableRegionalParameterVersionSample();
}

[Fact]
public void DisableRegionalParameterVersion()
{
ParameterVersion result = _sample.DisableRegionalParameterVersion(projectId: _fixture.ProjectId, locationId: ParameterManagerRegionalFixture.LocationId, parameterId: _fixture.ParameterId, versionId: _fixture.ParameterVersionId);

Assert.NotNull(result);
Assert.Equal(result.Name, _fixture.ParameterVersionToDisableAndEnable.Name);
Assert.True(result.Disabled);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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.
*/

using Google.Cloud.ParameterManager.V1;

[Collection(nameof(ParameterManagerRegionalFixture))]
public class EnableRegionalParameterVersionTests
{
private readonly ParameterManagerRegionalFixture _fixture;
private readonly EnableRegionalParameterVersionSample _sample;

public EnableRegionalParameterVersionTests(ParameterManagerRegionalFixture fixture)
{
_fixture = fixture;
_sample = new EnableRegionalParameterVersionSample();
}

[Fact]
public void EnableRegionalParameterVersion()
{
ParameterVersion result = _sample.EnableRegionalParameterVersion(projectId: _fixture.ProjectId, locationId: ParameterManagerRegionalFixture.LocationId, parameterId: _fixture.ParameterId, versionId: _fixture.ParameterVersionId);

Assert.NotNull(result);
Assert.Equal(result.Name, _fixture.ParameterVersionToDisableAndEnable.Name);
Assert.False(result.Disabled);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.2" />
<PackageReference Include="Google.Cloud.Iam.V1" Version="3.4.0" />
<PackageReference Include="Google.Cloud.SecretManager.V1" Version="2.5.0" />
<PackageReference Include="JUnitTestLogger" Version="1.1.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2" />
</ItemGroup>

<ItemGroup>
<Using Include="Xunit" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\ParameterManager.RegionalSamples\ParameterManager.RegionalSamples.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
// Copyright(c) 2025 Google Inc.
//
// 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.

using Google.Api.Gax.ResourceNames;
using Google.Cloud.Iam.V1;
using Google.Cloud.ParameterManager.V1;
using Google.Cloud.SecretManager.V1;
using Google.Protobuf;
using System.Text;

[CollectionDefinition(nameof(ParameterManagerRegionalFixture))]
public class ParameterManagerRegionalFixture : IDisposable, ICollectionFixture<ParameterManagerRegionalFixture>
{
public string ProjectId { get; }
public const string LocationId = "us-central1";
public const string Payload = "test123";

public string ParameterId { get; }

public Parameter ParameterToDelete { get; }
public ParameterName ParameterNameToDelete { get; }

public string ParameterVersionId { get; }
public Parameter ParameterToDeleteVersion { get; }
public ParameterVersion ParameterVersionToDelete { get; }
public ParameterVersionName ParameterVersionNameToDelete { get; }

public ParameterVersion ParameterVersionToDisableAndEnable { get; }
public ParameterVersionName ParameterVersionNameToDisableAndEnable { get; }
public ParameterManagerRegionalFixture()
{
ProjectId = Environment.GetEnvironmentVariable("GOOGLE_PROJECT_ID");
if (String.IsNullOrEmpty(ProjectId))
{
throw new Exception("missing GOOGLE_PROJECT_ID");
}

ParameterId = RandomId();
ParameterToDelete = CreateParameter(ParameterId, ParameterFormat.Unformatted);
ParameterNameToDelete = new ParameterName(ProjectId, LocationId, ParameterId);

ParameterId = RandomId();
ParameterVersionId = RandomId();
ParameterToDeleteVersion = CreateParameter(ParameterId, ParameterFormat.Unformatted);
ParameterVersionToDelete = CreateParameterVersion(ParameterId, ParameterVersionId, Payload);
ParameterVersionNameToDelete = new ParameterVersionName(ProjectId, LocationId, ParameterId, ParameterVersionId);

ParameterVersionId = RandomId();
ParameterVersionToDisableAndEnable = CreateParameterVersion(ParameterId, ParameterVersionId, Payload);
ParameterVersionNameToDisableAndEnable = new ParameterVersionName(ProjectId, LocationId, ParameterId, ParameterVersionId);
}

public void Dispose()
{
DeleteParameter(ParameterNameToDelete);
DeleteParameterVersion(ParameterVersionNameToDelete);
DeleteParameterVersion(ParameterVersionNameToDisableAndEnable);
DeleteParameter(ParameterToDeleteVersion.ParameterName);
}

public String RandomId()
{
return $"csharp-{System.Guid.NewGuid()}";
}

public Parameter CreateParameter(string parameterId, ParameterFormat format)
{
// Define the regional endpoint
string regionalEndpoint = $"parametermanager.{LocationId}.rep.googleapis.com";

// Create the client with the regional endpoint
ParameterManagerClient client = new ParameterManagerClientBuilder
{
Endpoint = regionalEndpoint
}.Build();

LocationName parent = new LocationName(ProjectId, LocationId);

Parameter parameter = new Parameter
{
Format = format
};

return client.CreateParameter(parent, parameter, parameterId);
}

public ParameterVersion CreateParameterVersion(string parameterId, string versionId, string payload)
{
// Define the regional endpoint
string regionalEndpoint = $"parametermanager.{LocationId}.rep.googleapis.com";

// Create the client with the regional endpoint
ParameterManagerClient client = new ParameterManagerClientBuilder
{
Endpoint = regionalEndpoint
}.Build();

ParameterName parameterName = new ParameterName(ProjectId, LocationId, parameterId);
ParameterVersion parameterVersion = new ParameterVersion
{
Payload = new ParameterVersionPayload
{
Data = ByteString.CopyFrom(payload, Encoding.UTF8)
}
};

return client.CreateParameterVersion(parameterName, parameterVersion, versionId);
}

private void DeleteParameter(ParameterName name)
{
// Define the regional endpoint
string regionalEndpoint = $"parametermanager.{LocationId}.rep.googleapis.com";

// Create the client with the regional endpoint
ParameterManagerClient client = new ParameterManagerClientBuilder
{
Endpoint = regionalEndpoint
}.Build();
try
{
client.DeleteParameter(name);
}
catch (Grpc.Core.RpcException e) when (e.StatusCode == Grpc.Core.StatusCode.NotFound)
{
// Ignore error - Parameter was already deleted
}
}

private void DeleteParameterVersion(ParameterVersionName name)
{
// Define the regional endpoint
string regionalEndpoint = $"parametermanager.{LocationId}.rep.googleapis.com";

// Create the client with the regional endpoint
ParameterManagerClient client = new ParameterManagerClientBuilder
{
Endpoint = regionalEndpoint
}.Build();
try
{
client.DeleteParameterVersion(name);
}
catch (Grpc.Core.RpcException e) when (e.StatusCode == Grpc.Core.StatusCode.NotFound)
{
// Ignore error - Parameter version was already deleted
}
}
}
Loading