-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathParameterManagerFixture.cs
131 lines (112 loc) · 4.8 KB
/
ParameterManagerFixture.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
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 string LocationId = "global";
public 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
}
}
}