forked from oracle/oci-dotnet-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ObjectStorageBucketTaggingExample.cs
256 lines (228 loc) · 11.6 KB
/
ObjectStorageBucketTaggingExample.cs
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
* Copyright (c) 2020, 2024, 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.
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Oci.Common;
using Oci.Common.Auth;
using Oci.ObjectstorageService;
using Oci.ObjectstorageService.Models;
using Oci.ObjectstorageService.Requests;
using Oci.ObjectstorageService.Responses;
namespace Oci.Examples
{
/**
* This example demonstrates the following operations:
* 1. Create a bucket
* 2. Add freeform and defined tags to the bucket
* 3. Update tags
* 4. Remove tags and delete the bucket
*/
public class ObjectStorageBucketTagging
{
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
public static async Task MainOSBucketTagging()
{
string compartmentId = Environment.GetEnvironmentVariable("OCI_COMPARTMENT_ID");
string bucketName = Environment.GetEnvironmentVariable("BUCKET_NAME");
string tagNamespace = Environment.GetEnvironmentVariable("TAG_NAMESPACE");
string tagName = Environment.GetEnvironmentVariable("TAG_NAME");
logger.Info("Starting example");
// Create Object Storage Client
var provider = new ConfigFileAuthenticationDetailsProvider("DEFAULT");
var osClient = new ObjectStorageClient(provider, new ClientConfiguration());
logger.Info("Object Storage client created.");
GetNamespaceRequest getNamespaceRequest = new GetNamespaceRequest
{
CompartmentId = compartmentId
};
GetNamespaceResponse getNamespaceResponse = await osClient.GetNamespace(getNamespaceRequest);
string nSpace = getNamespaceResponse.Value.Trim('"');
logger.Info($"namespace is {nSpace}");
/*
We can assign tags to a bucket at creation time. Like other taggable resources, we can
assign freeform and defined tags to a bucket. Freeform tags are a dictionary of
string-to-string, where the key is the tag name and the value is the tag value.
Defined tags are a dictionary where the key is the tag namespace (string) and the value is another dictionary. In
this second dictionary, the key is the tag name (string) and the value is the tag value. The tag names have to
correspond to the name of a tag within the specified namespace (and the namespace must exist).
*/
try
{
Dictionary<string, string> freeformTags = new Dictionary<string, string>(){
{"free","form"},
{"another","item"}
};
Dictionary<string, object> definedTagsMap = new Dictionary<string, object>(){
{tagName, "original value"}
};
Dictionary<string, Dictionary<string, object>> definedTags = new Dictionary<string, Dictionary<string, object>>(){
{tagNamespace, definedTagsMap}
};
CreateBucketDetails createBucketDetails = new CreateBucketDetails
{
Name = bucketName,
CompartmentId = compartmentId,
FreeformTags = freeformTags,
DefinedTags = definedTags,
ObjectEventsEnabled = false
};
CreateBucketRequest createBucketRequest = new CreateBucketRequest
{
CreateBucketDetails = createBucketDetails,
NamespaceName = nSpace
};
CreateBucketResponse createBucketResponse = await osClient.CreateBucket(createBucketRequest);
logger.Info($"Created a bucket with tags Bucket name: {createBucketResponse.Bucket.Name}");
logger.Info("========================================");
definedTags = createBucketResponse.Bucket.DefinedTags;
foreach (KeyValuePair<string, Dictionary<string, object>> kvp in definedTags)
{
foreach (KeyValuePair<string, object> tags in kvp.Value)
{
logger.Info($"tag key name: {tags.Key} and tag value is {tags.Value}");
}
}
// Tags come back when retrieving the bucket
GetBucketRequest getBucketRequest = new GetBucketRequest
{
NamespaceName = nSpace,
BucketName = bucketName
};
GetBucketResponse getBucketResponse = await osClient.GetBucket(getBucketRequest);
logger.Info($"Retrieved a bucket with tags Bucket name: {getBucketResponse.Bucket.Name}");
logger.Info("==========================================");
definedTags = getBucketResponse.Bucket.DefinedTags;
foreach (KeyValuePair<string, Dictionary<string, object>> kvp in definedTags)
{
foreach (KeyValuePair<string, object> tags in kvp.Value)
{
logger.Info($"tag key name: {tags.Key} and tag value is {tags.Value}");
}
}
/*
Unlike other resources (e.g. instances, VCNs, and block volumes), when listing buckets
tags are not returned by default. Instead, you need to provide a value to the fields
parameter when listing buckets in order to have those tags returned.
Here we can see the result of providing and not providing that parameter.
*/
ListBucketsRequest listBucketsRequest = new ListBucketsRequest
{
CompartmentId = compartmentId,
NamespaceName = nSpace
};
IEnumerable<BucketSummary> bucketSummaries = osClient.Paginators.ListBucketsRecordEnumerator(listBucketsRequest);
foreach (BucketSummary bucketSummary in bucketSummaries)
{
if (bucketSummary.Name.Equals(bucketName))
{
logger.Info($"Bucket summary without tags: Bucket Name: {bucketSummary.Name}");
logger.Info("=========================================");
if (bucketSummary.DefinedTags != null)
{
logger.Error($"expected tags to be zero but got: {bucketSummary.DefinedTags.Count}");
}
break;
}
}
List<ListBucketsRequest.FieldsEnum> fields = new List<ListBucketsRequest.FieldsEnum>(){
ListBucketsRequest.FieldsEnum.Tags
};
listBucketsRequest = new ListBucketsRequest
{
CompartmentId = compartmentId,
NamespaceName = nSpace,
Fields = fields
};
bucketSummaries = osClient.Paginators.ListBucketsRecordEnumerator(listBucketsRequest);
foreach (BucketSummary bucketSummary in bucketSummaries)
{
if (bucketSummary.Name.Equals(bucketName))
{
logger.Info($"Bucket summary with tags: Bucket Name: {bucketSummary.Name}");
logger.Info("======================================");
definedTags = bucketSummary.DefinedTags;
foreach (KeyValuePair<string, Dictionary<string, object>> kvp in definedTags)
{
foreach (KeyValuePair<string, object> tags in kvp.Value)
{
logger.Info($"tag key name: {tags.Key} and tag value is {tags.Value}");
}
}
}
}
/*
We can also update tags on a bucket. Note that this is a total replacement for any
previously set freeform or defined tags.
*/
Dictionary<string, string> updateFreeformTags = new Dictionary<string, string>(){
{"new","freeform"}
};
Dictionary<string, object> updateDefinedTagsMap = new Dictionary<string, object>(){
{tagName, "replaced"}
};
Dictionary<string, Dictionary<string, object>> updateDefinedTags = new Dictionary<string, Dictionary<string, object>>(){
{tagNamespace, updateDefinedTagsMap}
};
UpdateBucketDetails updateBucketDetails = new UpdateBucketDetails
{
Name = bucketName,
FreeformTags = updateFreeformTags,
DefinedTags = updateDefinedTags
};
UpdateBucketRequest updateBucketRequest = new UpdateBucketRequest
{
NamespaceName = nSpace,
BucketName = bucketName,
UpdateBucketDetails = updateBucketDetails
};
UpdateBucketResponse updateBucketResponse = await osClient.UpdateBucket(updateBucketRequest);
logger.Info($"Updated the bucket with new tags Bucket name: {updateBucketResponse.Bucket.Name}");
logger.Info("==============================================");
definedTags = updateBucketResponse.Bucket.DefinedTags;
foreach (KeyValuePair<string, Dictionary<string, object>> kvp in definedTags)
{
foreach (KeyValuePair<string, object> tags in kvp.Value)
{
logger.Info($"tag key name: {tags.Key} and tag value is {tags.Value}");
}
}
updateBucketDetails = new UpdateBucketDetails
{
Name = bucketName,
FreeformTags = new Dictionary<string, string>(),
DefinedTags = new Dictionary<string, Dictionary<string, object>>()
};
updateBucketRequest = new UpdateBucketRequest
{
NamespaceName = nSpace,
BucketName = bucketName,
UpdateBucketDetails = updateBucketDetails
};
updateBucketResponse = await osClient.UpdateBucket(updateBucketRequest);
logger.Info($"cleared the tags for the bucket: {updateBucketResponse.Bucket.Name}");
logger.Info("=================================");
// Clean up
DeleteBucketRequest deleteBucketRequest = new DeleteBucketRequest
{
NamespaceName = nSpace,
BucketName = bucketName
};
await osClient.DeleteBucket(deleteBucketRequest);
logger.Info("Deleted the bucket");
logger.Info("Ending example.");
}
catch (Exception e)
{
logger.Error($"Failed Object Storage example: {e.Message}");
}
finally
{
osClient.Dispose();
}
}
}
}