2323import static org .junit .Assert .assertTrue ;
2424
2525import com .google .api .gax .rpc .NotFoundException ;
26+ import com .google .cloud .resourcemanager .v3 .CreateTagKeyMetadata ;
27+ import com .google .cloud .resourcemanager .v3 .CreateTagKeyRequest ;
28+ import com .google .cloud .resourcemanager .v3 .CreateTagValueMetadata ;
29+ import com .google .cloud .resourcemanager .v3 .CreateTagValueRequest ;
30+ import com .google .api .gax .longrunning .OperationFuture ;
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 ;
2635import com .google .cloud .secretmanager .v1 .AddSecretVersionRequest ;
2736import com .google .cloud .secretmanager .v1 .CreateSecretRequest ;
2837import com .google .cloud .secretmanager .v1 .DeleteSecretRequest ;
4554import com .google .iam .v1 .Policy ;
4655import com .google .protobuf .ByteString ;
4756import java .io .ByteArrayOutputStream ;
57+ import java .lang .Exception ;
4858import java .io .IOException ;
4959import java .io .PrintStream ;
5060import java .util .Map ;
@@ -86,6 +96,7 @@ public class SnippetsIT {
8696 private static Secret TEST_REGIONAL_SECRET_WITH_VERSIONS ;
8797 private static SecretName TEST_REGIONAL_SECRET_TO_CREATE_NAME ;
8898 private static SecretName TEST_REGIONAL_SECRET_WITH_LABEL_TO_CREATE_NAME ;
99+ private static SecretName TEST_REGIONAL_SECRET_WITH_TAGS_TO_CREATE_NAME ;
89100 private static SecretName TEST_REGIONAL_SECRET_WITH_ANNOTATION_TO_CREATE_NAME ;
90101 private static SecretVersion TEST_REGIONAL_SECRET_VERSION ;
91102 private static SecretVersion TEST_REGIONAL_SECRET_VERSION_TO_DESTROY ;
@@ -95,6 +106,9 @@ public class SnippetsIT {
95106 private static SecretVersion TEST_REGIONAL_SECRET_VERSION_TO_ENABLE ;
96107 private static SecretVersion TEST_REGIONAL_SECRET_VERSION_TO_ENABLE_WITH_ETAG ;
97108
109+ private static TagKey TAG_KEY ;
110+ private static TagValue TAG_VALUE ;
111+
98112 private ByteArrayOutputStream stdOut ;
99113
100114 @ BeforeClass
@@ -114,6 +128,8 @@ public static void beforeAll() throws IOException {
114128
115129 TEST_REGIONAL_SECRET_WITH_LABEL_TO_CREATE_NAME =
116130 SecretName .ofProjectLocationSecretName (PROJECT_ID , LOCATION_ID , randomSecretId ());
131+ TEST_REGIONAL_SECRET_WITH_TAGS_TO_CREATE_NAME =
132+ SecretName .ofProjectLocationSecretName (PROJECT_ID , LOCATION_ID , randomSecretId ());
117133 TEST_REGIONAL_SECRET_VERSION = addRegionalSecretVersion (TEST_REGIONAL_SECRET_WITH_VERSIONS );
118134 TEST_REGIONAL_SECRET_VERSION_TO_DESTROY =
119135 addRegionalSecretVersion (TEST_REGIONAL_SECRET_WITH_VERSIONS );
@@ -130,6 +146,7 @@ public static void beforeAll() throws IOException {
130146 disableRegionalSecretVersion (TEST_REGIONAL_SECRET_VERSION_TO_ENABLE );
131147 TEST_REGIONAL_SECRET_VERSION_TO_ENABLE_WITH_ETAG = disableRegionalSecretVersion (
132148 TEST_REGIONAL_SECRET_VERSION_TO_ENABLE_WITH_ETAG );
149+ createTags ();
133150 }
134151
135152 @ Before
@@ -151,17 +168,48 @@ public static void afterAll() throws IOException {
151168 deleteRegionalSecret (TEST_REGIONAL_SECRET .getName ());
152169 deleteRegionalSecret (TEST_REGIONAL_SECRET_TO_CREATE_NAME .toString ());
153170 deleteRegionalSecret (TEST_REGIONAL_SECRET_WITH_LABEL_TO_CREATE_NAME .toString ());
171+ deleteRegionalSecret (TEST_REGIONAL_SECRET_WITH_TAGS_TO_CREATE_NAME .toString ());
154172 deleteRegionalSecret (TEST_REGIONAL_SECRET_WITH_ANNOTATION_TO_CREATE_NAME .toString ());
155173 deleteRegionalSecret (TEST_REGIONAL_SECRET_TO_DELETE .getName ());
156174 deleteRegionalSecret (TEST_REGIONAL_SECRET_TO_DELETE_WITH_ETAG .getName ());
157175 deleteRegionalSecret (TEST_REGIONAL_SECRET_WITH_VERSIONS .getName ());
176+ deleteTags ();
158177 }
159178
160179 private static String randomSecretId () {
161180 Random random = new Random ();
162181 return "test-drz-" + random .nextLong ();
163182 }
164183
184+ private static void createTags () throws IOException {
185+ try (TagKeysClient tagKeysClient = TagKeysClient .create ()) {
186+ CreateTagKeyRequest request =
187+ CreateTagKeyRequest .newBuilder ()
188+ .setTagKey (TagKey .newBuilder ().build ())
189+ .build ();
190+ OperationFuture <TagKey , CreateTagKeyMetadata > future =
191+ tagKeysClient .createTagKeyOperationCallable ().futureCall (request );
192+ TagKey response = future .get ();
193+ TAG_KEY = response ;
194+ }catch (Exception e ){
195+ }
196+ try (TagValuesClient tagValuesClient = TagValuesClient .create ()) {
197+ CreateTagValueRequest request =
198+ CreateTagValueRequest .newBuilder ()
199+ .setTagValue (TagValue .newBuilder ().setParent (TAG_KEY .getName ()).build ())
200+ .build ();
201+ OperationFuture <TagValue , CreateTagValueMetadata > future =
202+ tagValuesClient .createTagValueOperationCallable ().futureCall (request );
203+ TagValue response = future .get ();
204+ TAG_VALUE = response ;
205+ }catch (Exception e ){
206+ }
207+
208+ }
209+
210+ private static void deleteTags () throws IOException {
211+ }
212+
165213 private static Secret createRegionalSecret () throws IOException {
166214 LocationName parent = LocationName .of (PROJECT_ID , LOCATION_ID );
167215
@@ -253,6 +301,15 @@ public void testCreateRegionalSecretWithLabel() throws IOException {
253301 assertThat (secret .getLabelsMap ()).containsEntry (LABEL_KEY , LABEL_VALUE );
254302 }
255303
304+ @ Test
305+ public void testCreateRegionalSecretWithTags () throws IOException {
306+ SecretName name = TEST_REGIONAL_SECRET_WITH_TAGS_TO_CREATE_NAME ;
307+ Secret secret = CreateRegionalSecretWithTags .createRegionalSecretWithTags (
308+ name .getProject (), name .getLocation (), name .getSecret (), TAG_KEY , TAG_VALUE );
309+
310+ assertThat (stdOut .toString ()).contains ("Created secret with Tags" );
311+ }
312+
256313 @ Test
257314 public void testAddRegionalSecretVersion () throws IOException {
258315 SecretName name = SecretName .parse (TEST_REGIONAL_SECRET_WITH_VERSIONS .getName ());
@@ -578,4 +635,4 @@ public void testEditSecretAnnotations() throws IOException {
578635 UPDATED_ANNOTATION_KEY , UPDATED_ANNOTATION_VALUE );
579636 }
580637}
581-
638+
0 commit comments