-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommon.cs
195 lines (165 loc) · 7.6 KB
/
Common.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
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Microsoft.Graph;
using Microsoft.SharePoint.News.DataModel;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Queue;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace appsvc_fnc_dev_scw_sensitivity_dotnet001
{
internal class Common
{
public static async Task<Boolean> ApplyLabel(GraphServiceClient graphClient, string labelId, string groupId, string itemId, string requestId, string spaceNameEn, string spaceNameFr, ILogger log)
{
// Digital Vault - Vault Digitale Digital Vault 3d277510-cb23-44c1-a9c4-7680fcc237fb
// PROTECTED B - PROTÉGÉ B Protect B a1ab9d1a-185f-40cc-97d9-e1177019a70b
// UNCLASSIFIED - NON CLASSIFIÉ UNCLASSIFIED - NON CLASSIFIÉ d64b0091-505a-4a12-b8e5-9f04b9078a83
// Protected B MCAS Protected B - MCAS e12d86d7-fccd-49e3-8025-027a3c2cbf3a
log.LogInformation($"ApplyLabel - groupId: {groupId} & labelId: {labelId}");
var group = new Group
{
AssignedLabels = new List<AssignedLabel>()
{
new AssignedLabel { LabelId = labelId }
}
};
try
{
var users = await graphClient.Groups[groupId].Request().UpdateAsync(group);
}
catch (Exception e)
{
log.LogError($"Message: {e.Message}");
if (e.InnerException is not null) log.LogError($"InnerException: {e.InnerException.Message}");
log.LogError($"StackTrace: {e.StackTrace}");
string status = "Failed";
var listItem = new ListItem
{
Fields = new FieldValueSet
{
AdditionalData = new Dictionary<string, object>()
{
{ "Id", requestId },
{ "groupId", groupId },
{ "SpaceName", spaceNameEn },
{ "SpaceNameFR", spaceNameFr },
{ "Status", status},
{ "FunctionApp", "Sensitivity" },
{ "Method", "ApplyLabel" },
{ "ErrorMessage", $"{e.Message}" }
}
}
};
await AddQueueMessage("email", JsonConvert.SerializeObject(listItem.Fields.AdditionalData), log);
listItem = new ListItem
{
Fields = new FieldValueSet
{
AdditionalData = new Dictionary<string, object>()
{
{"Id", itemId},
{"Status", status},
}
}
};
await AddQueueMessage("list", JsonConvert.SerializeObject(listItem.Fields.AdditionalData), log);
return false;
}
return true;
}
public static async Task<IActionResult> AddToEmailQueue(string requestId, string groupId, string spaceNameEn, string spaceNameFr, string requesterName, string requesterEmail, ILogger log)
{
log.LogInformation("AddToEmailQueue received a request.");
try
{
var listItem = new ListItem
{
Fields = new FieldValueSet
{
AdditionalData = new Dictionary<string, object>()
{
{ "Id", requestId },
{ "groupId", groupId },
{ "SpaceName", spaceNameEn },
{ "SpaceNameFR", spaceNameFr },
{ "RequesterName", requesterName },
{ "RequesterEmail", requesterEmail },
{ "Status", "Team Created" },
{ "Comment", "" }
}
}
};
await AddQueueMessage("email", JsonConvert.SerializeObject(listItem.Fields.AdditionalData), log);
}
catch (Exception e)
{
log.LogError($"Message: {e.Message}");
if (e.InnerException is not null) log.LogError($"InnerException: {e.InnerException.Message}");
log.LogError($"StackTrace: {e.StackTrace}");
}
log.LogInformation("AddToEmailQueue processed a request.");
return new OkResult();
}
public static async Task<bool> RemoveOwner(GraphServiceClient graphClient, string groupId, string userId, ILogger log)
{
log.LogInformation("RemoveOwner received a request.");
bool result = true;
try
{
await graphClient.Groups[groupId].Owners[userId].Reference.Request().DeleteAsync();
}
catch (Exception e)
{
log.LogError($"Message: {e.Message}");
if (e.InnerException is not null) log.LogError($"InnerException: {e.InnerException.Message}");
log.LogError($"StackTrace: {e.StackTrace}");
result = false;
}
log.LogInformation("RemoveOwner processed a request.");
return result;
}
public static async Task AddQueueMessage(string queueName, string serializedMessage, ILogger log)
{
log.LogInformation("AddQueueMessage received a request.");
IConfiguration config = new ConfigurationBuilder().AddJsonFile("appsettings.json", optional: true, reloadOnChange: true).AddEnvironmentVariables().Build();
string connectionString = config["AzureWebJobsStorage"];
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);
CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();
CloudQueue queue = queueClient.GetQueueReference(queueName);
CloudQueueMessage message = new CloudQueueMessage(serializedMessage);
await queue.AddMessageAsync(message);
log.LogInformation("AddQueueMessage processed a request.");
}
public static async Task<IActionResult> AddToStatusQueue(string itemId, ILogger log)
{
log.LogInformation("AddToStatusQueue received a request.");
try
{
var listItem = new ListItem
{
Fields = new FieldValueSet
{
AdditionalData = new Dictionary<string, object>()
{
{ "Id", itemId },
{ "Status", "Complete" },
}
}
};
await AddQueueMessage("status", JsonConvert.SerializeObject(listItem.Fields.AdditionalData), log);
}
catch (Exception e)
{
log.LogError($"Message: {e.Message}");
if (e.InnerException is not null) log.LogError($"InnerException: {e.InnerException.Message}");
log.LogError($"StackTrace: {e.StackTrace}");
}
log.LogInformation("AddToStatusQueue processed a request.");
return new OkResult();
}
}
}