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 #2934

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
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(ParameterManagerFixture))]
public class DeleteParameterTests
{
private readonly ParameterManagerFixture _fixture;
private readonly DeleteParameterSample _sample;

public DeleteParameterTests(ParameterManagerFixture fixture)
{
_fixture = fixture;
_sample = new DeleteParameterSample();
}

[Fact]
public void DeleteParameter()
{
ParameterName parameterName = _fixture.ParameterNameToDelete;
_sample.DeleteParameter(projectId: parameterName.ProjectId, 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(ParameterManagerFixture))]
public class DeleteParameterVersionTests
{
private readonly ParameterManagerFixture _fixture;
private readonly DeleteParameterVersionSample _sample;

public DeleteParameterVersionTests(ParameterManagerFixture fixture)
{
_fixture = fixture;
_sample = new DeleteParameterVersionSample();
}

[Fact]
public void DeleteParameterVersion()
{
ParameterVersionName parameterVersionName = _fixture.ParameterVersionNameToDelete;
_sample.DeleteParameterVersion(projectId: parameterVersionName.ProjectId, 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(ParameterManagerFixture))]
public class DisableParameterVersionTests
{
private readonly ParameterManagerFixture _fixture;
private readonly DisableParameterVersionSample _sample;

public DisableParameterVersionTests(ParameterManagerFixture fixture)
{
_fixture = fixture;
_sample = new DisableParameterVersionSample();
}

[Fact]
public void DisableParameterVersion()
{
ParameterVersion result = _sample.DisableParameterVersion(projectId: _fixture.ProjectId, 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(ParameterManagerFixture))]
public class EnableParameterVersionTests
{
private readonly ParameterManagerFixture _fixture;
private readonly EnableParameterVersionSample _sample;

public EnableParameterVersionTests(ParameterManagerFixture fixture)
{
_fixture = fixture;
_sample = new EnableParameterVersionSample();
}

[Fact]
public void EnableParameterVersion()
{
ParameterVersion result = _sample.EnableParameterVersion(projectId: _fixture.ProjectId, 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.Samples\ParameterManager.Samples.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
// 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(ParameterManagerFixture))]
public class ParameterManagerFixture : IDisposable, ICollectionFixture<ParameterManagerFixture>
{
public string ProjectId { get; }
public const string LocationId = "global";
public const string Payload = "test123";

public string ParameterId { get; }
public string ParameterVersionId { get; }

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

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

public ParameterVersion ParameterVersionToDisableAndEnable { get; }
public ParameterVersionName ParameterVersionNameToDisableAndEnable { get; }
public ParameterManagerFixture()
{
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)
{
ParameterManagerClient client = ParameterManagerClient.Create();
LocationName projectName = new LocationName(ProjectId, LocationId);

Parameter parameter = new Parameter
{
Format = format
};

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

public ParameterVersion CreateParameterVersion(string parameterId, string versionId, string payload)
{
ParameterManagerClient client = ParameterManagerClient.Create();

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)
{
ParameterManagerClient client = ParameterManagerClient.Create();
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)
{
ParameterManagerClient client = ParameterManagerClient.Create();
try
{
client.DeleteParameterVersion(name);
}
catch (Grpc.Core.RpcException e) when (e.StatusCode == Grpc.Core.StatusCode.NotFound)
{
// Ignore error - Parameter version was already deleted
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* 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.
*/

// [START parametermanager_delete_param]

using Google.Cloud.ParameterManager.V1;

public class DeleteParameterSample
{
/// <summary>
/// This function deletes a parameter using the Parameter Manager SDK for GCP.
/// </summary>
/// <param name="projectId">The ID of the project where the parameter is located.</param>
/// <param name="parameterId">The ID of the parameter to be deleted.</param>
public void DeleteParameter(
string projectId,
string parameterId)
{
// Create the client.
ParameterManagerClient client = ParameterManagerClient.Create();

// Build the resource name for the parameter.
ParameterName parameterName = new ParameterName(projectId, "global", parameterId);

// Call the API to delete the parameter.
client.DeleteParameter(parameterName);

// Print a confirmation message.
Console.WriteLine($"Deleted parameter: {parameterName}");
}
}
// [END parametermanager_delete_param]
Loading