1919import static com .google .common .truth .Truth .assertThat ;
2020import static org .junit .Assert .assertFalse ;
2121
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 ;
2235import com .google .cloud .secretmanager .v1 .AddSecretVersionRequest ;
2336import com .google .cloud .secretmanager .v1 .CreateSecretRequest ;
2437import com .google .cloud .secretmanager .v1 .DeleteSecretRequest ;
3649import java .io .ByteArrayOutputStream ;
3750import java .io .IOException ;
3851import java .io .PrintStream ;
52+ import java .lang .Exception ;
3953import java .nio .charset .StandardCharsets ;
4054import java .util .Arrays ;
4155import java .util .Base64 ;
@@ -78,6 +92,7 @@ public class SnippetsIT {
7892 private static Secret TEST_SECRET_WITH_VERSIONS ;
7993 private static SecretName TEST_SECRET_TO_CREATE_NAME ;
8094 private static SecretName TEST_SECRET_WITH_LABEL_TO_CREATE_NAME ;
95+ private static SecretName TEST_SECRET_WITH_TAGS_TO_CREATE_NAME ;
8196 private static SecretName TEST_SECRET_WITH_ANNOTATION_TO_CREATE_NAME ;
8297 private static SecretName TEST_UMMR_SECRET_TO_CREATE_NAME ;
8398 private static SecretVersion TEST_SECRET_VERSION ;
@@ -88,10 +103,13 @@ public class SnippetsIT {
88103 private static SecretVersion TEST_SECRET_VERSION_TO_ENABLE ;
89104 private static SecretVersion TEST_SECRET_VERSION_TO_ENABLE_WITH_ETAG ;
90105
106+ private static TagKey TAG_KEY ;
107+ private static TagValue TAG_VALUE ;
108+
91109 private ByteArrayOutputStream stdOut ;
92110
93111 @ BeforeClass
94- public static void beforeAll () throws IOException {
112+ public static void beforeAll () throws Exception {
95113 Assert .assertFalse ("missing GOOGLE_CLOUD_PROJECT" , Strings .isNullOrEmpty (PROJECT_ID ));
96114
97115 TEST_SECRET = createSecret (true );
@@ -100,6 +118,7 @@ public static void beforeAll() throws IOException {
100118 TEST_SECRET_WITH_VERSIONS = createSecret (false );
101119 TEST_SECRET_TO_CREATE_NAME = SecretName .of (PROJECT_ID , randomSecretId ());
102120 TEST_UMMR_SECRET_TO_CREATE_NAME = SecretName .of (PROJECT_ID , randomSecretId ());
121+ TEST_SECRET_WITH_TAGS_TO_CREATE_NAME = SecretName .of (PROJECT_ID , randomSecretId ());
103122 TEST_SECRET_WITH_LABEL_TO_CREATE_NAME = SecretName .of (PROJECT_ID , randomSecretId ());
104123 TEST_SECRET_WITH_ANNOTATION_TO_CREATE_NAME = SecretName .of (PROJECT_ID , randomSecretId ());
105124
@@ -113,6 +132,7 @@ public static void beforeAll() throws IOException {
113132 disableSecretVersion (TEST_SECRET_VERSION_TO_ENABLE );
114133 TEST_SECRET_VERSION_TO_ENABLE_WITH_ETAG = disableSecretVersion (
115134 TEST_SECRET_VERSION_TO_ENABLE_WITH_ETAG );
135+ createTags ();
116136 }
117137
118138 @ Before
@@ -128,24 +148,86 @@ public void afterEach() {
128148 }
129149
130150 @ AfterClass
131- public static void afterAll () throws IOException {
151+ public static void afterAll () throws Exception {
132152 Assert .assertFalse ("missing GOOGLE_CLOUD_PROJECT" , Strings .isNullOrEmpty (PROJECT_ID ));
133153
134154 deleteSecret (TEST_SECRET .getName ());
135155 deleteSecret (TEST_SECRET_TO_CREATE_NAME .toString ());
156+ deleteSecret (TEST_SECRET_WITH_TAGS_TO_CREATE_NAME .toString ());
136157 deleteSecret (TEST_SECRET_WITH_LABEL_TO_CREATE_NAME .toString ());
137158 deleteSecret (TEST_SECRET_WITH_ANNOTATION_TO_CREATE_NAME .toString ());
138159 deleteSecret (TEST_UMMR_SECRET_TO_CREATE_NAME .toString ());
139160 deleteSecret (TEST_SECRET_TO_DELETE .getName ());
140161 deleteSecret (TEST_SECRET_TO_DELETE_WITH_ETAG .getName ());
141162 deleteSecret (TEST_SECRET_WITH_VERSIONS .getName ());
163+ deleteTags ();
142164 }
143165
144166 private static String randomSecretId () {
145167 Random random = new Random ();
146168 return "java-" + random .nextLong ();
147169 }
148170
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+
149231 private static Secret createSecret (boolean addAnnotation ) throws IOException {
150232 ProjectName parent = ProjectName .of (PROJECT_ID );
151233
@@ -257,6 +339,19 @@ public void testCreateSecretWithLabel() throws IOException {
257339 assertThat (secret .getLabelsMap ()).containsEntry (LABEL_KEY , LABEL_VALUE );
258340 }
259341
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+
260355 @ Test
261356 public void testCreateSecretWithAnnotations () throws IOException {
262357 SecretName name = TEST_SECRET_WITH_ANNOTATION_TO_CREATE_NAME ;
0 commit comments