-
Notifications
You must be signed in to change notification settings - Fork 168
Expand file tree
/
Copy pathDeleteTagExample.java
More file actions
112 lines (94 loc) · 4.82 KB
/
DeleteTagExample.java
File metadata and controls
112 lines (94 loc) · 4.82 KB
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
/**
* Copyright (c) 2016, 2026, Oracle and/or its affiliates. All rights reserved.
* This software is dual-licensed to you under the Universal Permissive License (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl or Apache License 2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose either license.
*/
import com.oracle.bmc.ConfigFileReader;
import com.oracle.bmc.auth.AuthenticationDetailsProvider;
import com.oracle.bmc.auth.ConfigFileAuthenticationDetailsProvider;
import com.oracle.bmc.identity.Identity;
import com.oracle.bmc.identity.IdentityClient;
import com.oracle.bmc.identity.model.Tag;
import com.oracle.bmc.identity.model.TagNamespaceSummary;
import com.oracle.bmc.identity.model.UpdateTagDetails;
import com.oracle.bmc.identity.model.WorkRequest;
import com.oracle.bmc.identity.requests.DeleteTagRequest;
import com.oracle.bmc.identity.requests.GetWorkRequestRequest;
import com.oracle.bmc.identity.requests.ListTagNamespacesRequest;
import com.oracle.bmc.identity.requests.UpdateTagRequest;
import java.util.List;
/*
* Example Class to show the usage of Delete Tag API.
* This sample will do following things:
* 1) Get the tag namespace id using the provided name, this should exist in tenancy root compartment
* 2) Delete the tag
* 3) Get the status of work request
*/
public class DeleteTagExample {
public static void main(String[] args) throws Exception {
// TODO: Fill in this value
String configurationFilePath = "~/.oci/config";
String profile = "DEFAULT";
// TODO: Provide the tag name to be deleted and tagNamespace name in which this tag belongs
// to.
String tagNamespaceName = "CHANGE_ME";
String tagName = "CHANGE_ME";
// Configuring the AuthenticationDetailsProvider. It's assuming there is a default OCI
// config file
// "~/.oci/config", and a profile in that config with the name "DEFAULT". Make changes to
// the following
// line if needed and use ConfigFileReader.parse(configurationFilePath, profile);
final ConfigFileReader.ConfigFile configFile = ConfigFileReader.parseDefault();
final AuthenticationDetailsProvider provider =
new ConfigFileAuthenticationDetailsProvider(configFile);
final String tenantId = provider.getTenantId();
final Identity identityClient = IdentityClient.builder().build(provider);
// Get the tagnamespace of given name from tenancy
TagNamespaceSummary tagNamespace =
getTagNamespace(identityClient, tenantId, tagNamespaceName);
UpdateTagDetails updateTagDetails = UpdateTagDetails.builder().isRetired(true).build();
Tag retiredTag =
identityClient
.updateTag(
UpdateTagRequest.builder()
.tagNamespaceId(tagNamespace.getId())
.tagName(tagName)
.updateTagDetails(updateTagDetails)
.build())
.getTag();
System.out.println("\n Tag has been retired -->");
System.out.println(retiredTag);
String opcWorkRequestId =
identityClient
.deleteTag(
DeleteTagRequest.builder()
.tagNamespaceId(tagNamespace.getId())
.tagName(tagName)
.build())
.getOpcWorkRequestId();
WorkRequest workRequest =
identityClient
.getWorkRequest(
GetWorkRequestRequest.builder()
.workRequestId(opcWorkRequestId)
.build())
.getWorkRequest();
System.out.println(
"\nDelete Tag work request status : " + workRequest.getStatus() + " -->");
System.out.println(workRequest);
}
public static TagNamespaceSummary getTagNamespace(
Identity client, String tenantId, String tagNamespaceName) {
// Get the tagnamespaces in root compartment
List<TagNamespaceSummary> tagNamespaceLists =
client.listTagNamespaces(
ListTagNamespacesRequest.builder().compartmentId(tenantId).build())
.getItems();
// Find the tag namespace with given name
for (TagNamespaceSummary tagNamespace : tagNamespaceLists) {
if (tagNamespace.getName().equalsIgnoreCase(tagNamespaceName)) {
return tagNamespace;
}
}
throw new RuntimeException("TagNamespace " + tagNamespaceName + " does not exist");
}
}