This repository was archived by the owner on Sep 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathadvanced.js
More file actions
231 lines (190 loc) · 7.11 KB
/
advanced.js
File metadata and controls
231 lines (190 loc) · 7.11 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
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
//----------------------------------------------------------------------------------
// Microsoft Developer & Platform Evangelism
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
// EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES
// OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
//----------------------------------------------------------------------------------
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious. No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//----------------------------------------------------------------------------------
var fs = require('fs');
var util = require('util');
var config = require('./config.js');
var storage = require('azure-storage');
var queueNamePrefix = "storagesampleforqueue";
var queueService;
function advancedSamples() {
// Create a queue service client for interacting with the Queue service from account name and account key or the connection string.
// You can either connect to an Azure storage account with account name and account key specified OR use the storage emulator for development.
// How to create a storage connection string - http://msdn.microsoft.com/en-us/library/azure/ee758697.aspx
if (config.connectionString) {
queueService = storage.createQueueService(config.connectionString);
} else {
queueService = storage.createQueueService(config.accountName, config.accountKey);
}
return scenarios = [
{
action: corsRules,
message: 'Queue CORS Sample\n'
},
{
action: serviceProperties,
message: 'Queue Service Properties Sample\n'
},
{
action: queueMetadata,
message: 'Queue Metadata Sample\n'
},
{
action: queueAcl,
message: 'Queue Access Policy Sample\n'
}
];
}
// Get Cors properties, change them and revert back to original
function corsRules(callback) {
console.log('Get service properties');
queueService.getServiceProperties(function (error, properties) {
if (error) return callback(error);
console.log('Set Cors rules in the service properties');
// Keeps the original Cors rules
var originalCors = properties.Cors;
properties.Cors = {
CorsRule: [{
AllowedOrigins: ['*'],
AllowedMethods: ['POST', 'GET', 'HEAD', 'PUT'],
AllowedHeaders: ['*'],
ExposedHeaders: ['*'],
MaxAgeInSeconds: 3600
}]
};
queueService.setServiceProperties(properties, function (error) {
if (error) return callback(error);
console.log('Cors rules set successfuly');
// reverts the cors rules back to the original ones so they do not get corrupted by the ones set in this sample
properties.Cors = originalCors;
queueService.setServiceProperties(properties, function (error) {
return callback(error);
});
});
});
}
// Manage logging and metrics service properties
function serviceProperties(callback) {
// Create a blob client for interacting with the blob service from connection string
// How to create a storage connection string - http://msdn.microsoft.com/en-us/library/azure/ee758697.aspx
var blobService = storage.createBlobService(config.connectionString);
console.log('Get service properties');
blobService.getServiceProperties(function (error, properties) {
if (error) return callback(error);
var originalProperties = properties;
properties = serviceProperties = {
Logging: {
Version: '1.0',
Delete: true,
Read: true,
Write: true,
RetentionPolicy: {
Enabled: true,
Days: 10,
},
},
HourMetrics: {
Version: '1.0',
Enabled: true,
IncludeAPIs: true,
RetentionPolicy: {
Enabled: true,
Days: 10,
},
},
MinuteMetrics: {
Version: '1.0',
Enabled: true,
IncludeAPIs: true,
RetentionPolicy: {
Enabled: true,
Days: 10,
},
}
};
console.log('Set service properties');
blobService.setServiceProperties(properties, function (error) {
if (error) return callback(error);
// reverts the cors rules back to the original ones so they do not get corrupted by the ones set in this sample
blobService.setServiceProperties(originalProperties, function (error) {
return callback(error);
});
});
});
}
// Retrieve statistics related to replication for the Queue service
function serviceStats(callback) {
console.log('Get service statistics');
queueService.getServiceStats(function (error, serviceStats){
if (error) return callback(error);
callback(null);
});
}
// Manage queue user-defined metadata
function queueMetadata(callback) {
var queueName = queueNamePrefix + "myqueueformetadata";
var metadata = { color: 'blue', foo: 'Bar' };
console.log('Create queue');
queueService.createQueueIfNotExists(queueName, function (error) {
if (error) return callback(error);
console.log('Set queue metadata');
queueService.setQueueMetadata(queueName, metadata, function (error) {
if (error) return callback(error);
console.log('Get queue metadata');
queueService.getQueueMetadata(queueName, function (error, queue) {
if (error) return callback(error);
console.log(' color: ' + queue.metadata.color);
console.log(' foo: ' + queue.metadata.foo);
console.log('Delete queue');
queueService.deleteQueue(queueName, function () {
callback(error);
});
});
});
});
}
// Manage access policies of the queue
function queueAcl(callback) {
var queueName = queueNamePrefix + "myqueueforacl";
console.log('Create queue');
queueService.createQueueIfNotExists(queueName, function() {
// Set access policy
var expiryDate = new Date();
expiryDate.setMinutes(expiryDate.getMinutes() + 10);
var id = 'sampleIDForQueuePolicy';
var sharedAccessPolicy = {
sampleIDForQueuePolicy: {
Permissions: storage.QueueUtilities.SharedAccessPermissions.PROCESS,
Expiry: expiryDate
}
};
console.log('Set queue access policy');
queueService.setQueueAcl(queueName, sharedAccessPolicy, function (error, result, response) {
if (error) return callback(error);
// Get access policy
console.log('Get queue access policy');
queueService.getQueueAcl(queueName, function(error, result, response) {
if (error) return callback(error);
console.log(' Permissions: ' + result.signedIdentifiers.sampleIDForQueuePolicy.Permissions);
console.log(' Expiry: ' + result.signedIdentifiers.sampleIDForQueuePolicy.Expiry.toISOString());
console.log('Delete queue');
queueService.deleteQueue(queueName, function () {
callback(error);
});
});
});
});
}
module.exports = advancedSamples();