1919import static com .google .common .truth .Truth .assertThat ;
2020import static org .junit .Assert .assertFalse ;
2121
22+ import com .google .cloud .resourcemanager .v3 .CreateTagKeyMetadata ;
23+ import com .google .cloud .resourcemanager .v3 .CreateTagKeyRequest ;
24+ import com .google .cloud .resourcemanager .v3 .CreateTagValueMetadata ;
25+ import com .google .cloud .resourcemanager .v3 .CreateTagValueRequest ;
26+ import com .google .api .gax .longrunning .OperationFuture ;
27+ import com .google .cloud .resourcemanager .v3 .TagKey ;
28+ import com .google .cloud .resourcemanager .v3 .TagKeysClient ;
29+ import com .google .cloud .resourcemanager .v3 .TagValue ;
30+ import com .google .cloud .resourcemanager .v3 .TagValuesClient ;
2231import com .google .cloud .secretmanager .v1 .AddSecretVersionRequest ;
2332import com .google .cloud .secretmanager .v1 .CreateSecretRequest ;
2433import com .google .cloud .secretmanager .v1 .DeleteSecretRequest ;
3342import com .google .cloud .secretmanager .v1 .SecretVersionName ;
3443import com .google .common .base .Strings ;
3544import com .google .protobuf .ByteString ;
45+ import java .lang .Exception ;
3646import java .io .ByteArrayOutputStream ;
3747import java .io .IOException ;
3848import java .io .PrintStream ;
@@ -78,6 +88,7 @@ public class SnippetsIT {
7888 private static Secret TEST_SECRET_WITH_VERSIONS ;
7989 private static SecretName TEST_SECRET_TO_CREATE_NAME ;
8090 private static SecretName TEST_SECRET_WITH_LABEL_TO_CREATE_NAME ;
91+ private static SecretName TEST_SECRET_WITH_TAGS_TO_CREATE_NAME ;
8192 private static SecretName TEST_SECRET_WITH_ANNOTATION_TO_CREATE_NAME ;
8293 private static SecretName TEST_UMMR_SECRET_TO_CREATE_NAME ;
8394 private static SecretVersion TEST_SECRET_VERSION ;
@@ -88,6 +99,9 @@ public class SnippetsIT {
8899 private static SecretVersion TEST_SECRET_VERSION_TO_ENABLE ;
89100 private static SecretVersion TEST_SECRET_VERSION_TO_ENABLE_WITH_ETAG ;
90101
102+ private static TagKey TAG_KEY ;
103+ private static TagValue TAG_VALUE ;
104+
91105 private ByteArrayOutputStream stdOut ;
92106
93107 @ BeforeClass
@@ -100,6 +114,7 @@ public static void beforeAll() throws IOException {
100114 TEST_SECRET_WITH_VERSIONS = createSecret (false );
101115 TEST_SECRET_TO_CREATE_NAME = SecretName .of (PROJECT_ID , randomSecretId ());
102116 TEST_UMMR_SECRET_TO_CREATE_NAME = SecretName .of (PROJECT_ID , randomSecretId ());
117+ TEST_SECRET_WITH_TAGS_TO_CREATE_NAME = SecretName .of (PROJECT_ID , randomSecretId ());
103118 TEST_SECRET_WITH_LABEL_TO_CREATE_NAME = SecretName .of (PROJECT_ID , randomSecretId ());
104119 TEST_SECRET_WITH_ANNOTATION_TO_CREATE_NAME = SecretName .of (PROJECT_ID , randomSecretId ());
105120
@@ -113,6 +128,7 @@ public static void beforeAll() throws IOException {
113128 disableSecretVersion (TEST_SECRET_VERSION_TO_ENABLE );
114129 TEST_SECRET_VERSION_TO_ENABLE_WITH_ETAG = disableSecretVersion (
115130 TEST_SECRET_VERSION_TO_ENABLE_WITH_ETAG );
131+ createTags ();
116132 }
117133
118134 @ Before
@@ -133,19 +149,50 @@ public static void afterAll() throws IOException {
133149
134150 deleteSecret (TEST_SECRET .getName ());
135151 deleteSecret (TEST_SECRET_TO_CREATE_NAME .toString ());
152+ deleteSecret (TEST_SECRET_WITH_TAGS_TO_CREATE_NAME .toString ());
136153 deleteSecret (TEST_SECRET_WITH_LABEL_TO_CREATE_NAME .toString ());
137154 deleteSecret (TEST_SECRET_WITH_ANNOTATION_TO_CREATE_NAME .toString ());
138155 deleteSecret (TEST_UMMR_SECRET_TO_CREATE_NAME .toString ());
139156 deleteSecret (TEST_SECRET_TO_DELETE .getName ());
140157 deleteSecret (TEST_SECRET_TO_DELETE_WITH_ETAG .getName ());
141158 deleteSecret (TEST_SECRET_WITH_VERSIONS .getName ());
159+ deleteTags ();
142160 }
143161
144162 private static String randomSecretId () {
145163 Random random = new Random ();
146164 return "java-" + random .nextLong ();
147165 }
148166
167+ private static void createTags () throws IOException {
168+ try (TagKeysClient tagKeysClient = TagKeysClient .create ()) {
169+ CreateTagKeyRequest request =
170+ CreateTagKeyRequest .newBuilder ()
171+ .setTagKey (TagKey .newBuilder ().build ())
172+ .build ();
173+ OperationFuture <TagKey , CreateTagKeyMetadata > future =
174+ tagKeysClient .createTagKeyOperationCallable ().futureCall (request );
175+ TagKey response = future .get ();
176+ TAG_KEY = response ;
177+ }catch (Exception e ){
178+ }
179+ try (TagValuesClient tagValuesClient = TagValuesClient .create ()) {
180+ CreateTagValueRequest request =
181+ CreateTagValueRequest .newBuilder ()
182+ .setTagValue (TagValue .newBuilder ().setParent (TAG_KEY .getName ()).build ())
183+ .build ();
184+ OperationFuture <TagValue , CreateTagValueMetadata > future =
185+ tagValuesClient .createTagValueOperationCallable ().futureCall (request );
186+ TagValue response = future .get ();
187+ TAG_VALUE = response ;
188+ }catch (Exception e ){
189+ }
190+
191+ }
192+
193+ private static void deleteTags () throws IOException {
194+ }
195+
149196 private static Secret createSecret (boolean addAnnotation ) throws IOException {
150197 ProjectName parent = ProjectName .of (PROJECT_ID );
151198
@@ -257,6 +304,19 @@ public void testCreateSecretWithLabel() throws IOException {
257304 assertThat (secret .getLabelsMap ()).containsEntry (LABEL_KEY , LABEL_VALUE );
258305 }
259306
307+ @ Test
308+ public void testCreateSecretWithTag () throws IOException {
309+ SecretName name = TEST_SECRET_WITH_TAGS_TO_CREATE_NAME ;
310+ Secret secret = CreateSecretWithTags .createSecretWithTags (
311+ name .getProject (),
312+ name .getSecret (),
313+ TAG_KEY .getName (),
314+ TAG_VALUE .getName ()
315+ );
316+
317+ assertThat (secret .getTagsMap ()).containsEntry (TAG_KEY , TAG_VALUE );
318+ }
319+
260320 @ Test
261321 public void testCreateSecretWithAnnotations () throws IOException {
262322 SecretName name = TEST_SECRET_WITH_ANNOTATION_TO_CREATE_NAME ;
0 commit comments