19
19
import static com .google .common .truth .Truth .assertThat ;
20
20
import static org .junit .Assert .assertFalse ;
21
21
22
+ import com .google .api .gax .longrunning .OperationFuture ;
23
+ import com .google .cloud .resourcemanager .v3 .CreateTagKeyMetadata ;
24
+ import com .google .cloud .resourcemanager .v3 .CreateTagKeyRequest ;
25
+ import com .google .cloud .resourcemanager .v3 .CreateTagValueMetadata ;
26
+ import com .google .cloud .resourcemanager .v3 .CreateTagValueRequest ;
27
+ import com .google .cloud .resourcemanager .v3 .DeleteTagKeyMetadata ;
28
+ import com .google .cloud .resourcemanager .v3 .DeleteTagKeyRequest ;
29
+ import com .google .cloud .resourcemanager .v3 .DeleteTagValueMetadata ;
30
+ import com .google .cloud .resourcemanager .v3 .DeleteTagValueRequest ;
31
+ import com .google .cloud .resourcemanager .v3 .TagKey ;
32
+ import com .google .cloud .resourcemanager .v3 .TagKeysClient ;
33
+ import com .google .cloud .resourcemanager .v3 .TagValue ;
34
+ import com .google .cloud .resourcemanager .v3 .TagValuesClient ;
22
35
import com .google .cloud .secretmanager .v1 .AddSecretVersionRequest ;
23
36
import com .google .cloud .secretmanager .v1 .CreateSecretRequest ;
24
37
import com .google .cloud .secretmanager .v1 .DeleteSecretRequest ;
36
49
import java .io .ByteArrayOutputStream ;
37
50
import java .io .IOException ;
38
51
import java .io .PrintStream ;
52
+ import java .lang .Exception ;
39
53
import java .nio .charset .StandardCharsets ;
40
54
import java .util .Arrays ;
41
55
import java .util .Base64 ;
@@ -78,6 +92,7 @@ public class SnippetsIT {
78
92
private static Secret TEST_SECRET_WITH_VERSIONS ;
79
93
private static SecretName TEST_SECRET_TO_CREATE_NAME ;
80
94
private static SecretName TEST_SECRET_WITH_LABEL_TO_CREATE_NAME ;
95
+ private static SecretName TEST_SECRET_WITH_TAGS_TO_CREATE_NAME ;
81
96
private static SecretName TEST_SECRET_WITH_ANNOTATION_TO_CREATE_NAME ;
82
97
private static SecretName TEST_UMMR_SECRET_TO_CREATE_NAME ;
83
98
private static SecretVersion TEST_SECRET_VERSION ;
@@ -88,10 +103,13 @@ public class SnippetsIT {
88
103
private static SecretVersion TEST_SECRET_VERSION_TO_ENABLE ;
89
104
private static SecretVersion TEST_SECRET_VERSION_TO_ENABLE_WITH_ETAG ;
90
105
106
+ private static TagKey TAG_KEY ;
107
+ private static TagValue TAG_VALUE ;
108
+
91
109
private ByteArrayOutputStream stdOut ;
92
110
93
111
@ BeforeClass
94
- public static void beforeAll () throws IOException {
112
+ public static void beforeAll () throws Exception {
95
113
Assert .assertFalse ("missing GOOGLE_CLOUD_PROJECT" , Strings .isNullOrEmpty (PROJECT_ID ));
96
114
97
115
TEST_SECRET = createSecret (true );
@@ -100,6 +118,7 @@ public static void beforeAll() throws IOException {
100
118
TEST_SECRET_WITH_VERSIONS = createSecret (false );
101
119
TEST_SECRET_TO_CREATE_NAME = SecretName .of (PROJECT_ID , randomSecretId ());
102
120
TEST_UMMR_SECRET_TO_CREATE_NAME = SecretName .of (PROJECT_ID , randomSecretId ());
121
+ TEST_SECRET_WITH_TAGS_TO_CREATE_NAME = SecretName .of (PROJECT_ID , randomSecretId ());
103
122
TEST_SECRET_WITH_LABEL_TO_CREATE_NAME = SecretName .of (PROJECT_ID , randomSecretId ());
104
123
TEST_SECRET_WITH_ANNOTATION_TO_CREATE_NAME = SecretName .of (PROJECT_ID , randomSecretId ());
105
124
@@ -113,6 +132,7 @@ public static void beforeAll() throws IOException {
113
132
disableSecretVersion (TEST_SECRET_VERSION_TO_ENABLE );
114
133
TEST_SECRET_VERSION_TO_ENABLE_WITH_ETAG = disableSecretVersion (
115
134
TEST_SECRET_VERSION_TO_ENABLE_WITH_ETAG );
135
+ createTags ();
116
136
}
117
137
118
138
@ Before
@@ -128,24 +148,86 @@ public void afterEach() {
128
148
}
129
149
130
150
@ AfterClass
131
- public static void afterAll () throws IOException {
151
+ public static void afterAll () throws Exception {
132
152
Assert .assertFalse ("missing GOOGLE_CLOUD_PROJECT" , Strings .isNullOrEmpty (PROJECT_ID ));
133
153
134
154
deleteSecret (TEST_SECRET .getName ());
135
155
deleteSecret (TEST_SECRET_TO_CREATE_NAME .toString ());
156
+ deleteSecret (TEST_SECRET_WITH_TAGS_TO_CREATE_NAME .toString ());
136
157
deleteSecret (TEST_SECRET_WITH_LABEL_TO_CREATE_NAME .toString ());
137
158
deleteSecret (TEST_SECRET_WITH_ANNOTATION_TO_CREATE_NAME .toString ());
138
159
deleteSecret (TEST_UMMR_SECRET_TO_CREATE_NAME .toString ());
139
160
deleteSecret (TEST_SECRET_TO_DELETE .getName ());
140
161
deleteSecret (TEST_SECRET_TO_DELETE_WITH_ETAG .getName ());
141
162
deleteSecret (TEST_SECRET_WITH_VERSIONS .getName ());
163
+ deleteTags ();
142
164
}
143
165
144
166
private static String randomSecretId () {
145
167
Random random = new Random ();
146
168
return "java-" + random .nextLong ();
147
169
}
148
170
171
+ private static void createTags () throws Exception {
172
+ try (TagKeysClient tagKeysClient = TagKeysClient .create ()) {
173
+ Random random = new Random ();
174
+ ProjectName parent = ProjectName .of (PROJECT_ID );
175
+ CreateTagKeyRequest request =
176
+ CreateTagKeyRequest .newBuilder ()
177
+ .setTagKey (
178
+ TagKey
179
+ .newBuilder ()
180
+ .setParent (parent .toString ())
181
+ .setShortName ("java-" + random .nextLong ())
182
+ .build ())
183
+ .build ();
184
+ OperationFuture <TagKey , CreateTagKeyMetadata > future =
185
+ tagKeysClient .createTagKeyOperationCallable ().futureCall (request );
186
+ TagKey response = future .get ();
187
+ TAG_KEY = response ;
188
+ }
189
+
190
+ try (TagValuesClient tagValuesClient = TagValuesClient .create ()) {
191
+ Random random = new Random ();
192
+ CreateTagValueRequest request =
193
+ CreateTagValueRequest .newBuilder ()
194
+ .setTagValue (
195
+ TagValue
196
+ .newBuilder ()
197
+ .setParent (TAG_KEY .getName ())
198
+ .setShortName ("java-" + random .nextLong ())
199
+ .build ())
200
+ .build ();
201
+ OperationFuture <TagValue , CreateTagValueMetadata > future =
202
+ tagValuesClient .createTagValueOperationCallable ().futureCall (request );
203
+ TagValue response = future .get ();
204
+ TAG_VALUE = response ;
205
+ }
206
+ }
207
+
208
+ private static void deleteTags () throws Exception {
209
+ Thread .sleep (60000 );
210
+ try (TagValuesClient tagValuesClient = TagValuesClient .create ()) {
211
+ DeleteTagValueRequest request =
212
+ DeleteTagValueRequest .newBuilder ()
213
+ .setName (TAG_VALUE .getName ())
214
+ .build ();
215
+ OperationFuture <TagValue , DeleteTagValueMetadata > future =
216
+ tagValuesClient .deleteTagValueOperationCallable ().futureCall (request );
217
+ TagValue response = future .get ();
218
+ }
219
+
220
+ try (TagKeysClient tagKeysClient = TagKeysClient .create ()) {
221
+ DeleteTagKeyRequest request =
222
+ DeleteTagKeyRequest .newBuilder ()
223
+ .setName (TAG_KEY .getName ())
224
+ .build ();
225
+ OperationFuture <TagKey , DeleteTagKeyMetadata > future =
226
+ tagKeysClient .deleteTagKeyOperationCallable ().futureCall (request );
227
+ TagKey response = future .get ();
228
+ }
229
+ }
230
+
149
231
private static Secret createSecret (boolean addAnnotation ) throws IOException {
150
232
ProjectName parent = ProjectName .of (PROJECT_ID );
151
233
@@ -257,6 +339,19 @@ public void testCreateSecretWithLabel() throws IOException {
257
339
assertThat (secret .getLabelsMap ()).containsEntry (LABEL_KEY , LABEL_VALUE );
258
340
}
259
341
342
+ @ Test
343
+ public void testCreateSecretWithTag () throws IOException {
344
+ SecretName name = TEST_SECRET_WITH_TAGS_TO_CREATE_NAME ;
345
+ Secret secret = CreateSecretWithTags .createSecretWithTags (
346
+ name .getProject (),
347
+ name .getSecret (),
348
+ TAG_KEY .getName (),
349
+ TAG_VALUE .getName ()
350
+ );
351
+
352
+ assertThat (stdOut .toString ()).contains ("Created secret with Tags" );
353
+ }
354
+
260
355
@ Test
261
356
public void testCreateSecretWithAnnotations () throws IOException {
262
357
SecretName name = TEST_SECRET_WITH_ANNOTATION_TO_CREATE_NAME ;
0 commit comments