-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathOrchestrator.cs
More file actions
109 lines (86 loc) · 3.5 KB
/
Orchestrator.cs
File metadata and controls
109 lines (86 loc) · 3.5 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
/********************************************************
* *
* Copyright (C) Microsoft. All rights reserved. *
* *
*********************************************************/
using System;
using System.Threading;
namespace Microsoft.Partner.CSP.APISamples.SubscriptionStream
{
class Orchestrator
{
ILocalCSPStreamManager localManager;
string resellerCid;
AuthorizationToken adAuthorizationToken = null;
AuthorizationToken saAuthorizationToken = null;
private DateTime lastCSPEventTime;
public Orchestrator() { }
public Orchestrator(ILocalCSPStreamManager mgr)
{
localManager = mgr;
}
public Orchestrator(ILocalCSPStreamManager mgr, string defaultDomain, string ptrCtrAppId, string ptrCtrAppSecret, string resellerMicrosoftId)
{
localManager = mgr;
Initialize(defaultDomain, ptrCtrAppId, ptrCtrAppSecret, resellerMicrosoftId);
}
void Initialize(string defaultDomain, string ptrCtrAppId, string ptrCtrAppSecret, string resellerMicrosoftId)
{
// Get Active Directory token first
adAuthorizationToken = Reseller.GetAD_Token(defaultDomain, ptrCtrAppId, ptrCtrAppSecret);
// Using the ADToken get the sales agent token
saAuthorizationToken = Reseller.GetSA_Token(adAuthorizationToken);
// Get the Reseller Cid, you can cache this value
resellerCid = Reseller.GetCid(resellerMicrosoftId, saAuthorizationToken.AccessToken);
}
public void StartProcessing()
{
if (String.IsNullOrEmpty(resellerCid) ||
adAuthorizationToken == null ||
saAuthorizationToken == null)
{
throw new ArgumentException("Call Initialize() before processing");
}
lastCSPEventTime = localManager.GetLastCSPEventTime();
Stream.CreateStream(lastCSPEventTime, resellerCid, saAuthorizationToken.AccessToken);
}
public void GetAndProcessPage()
{
if (String.IsNullOrEmpty(resellerCid) ||
adAuthorizationToken == null ||
saAuthorizationToken == null)
{
throw new ArgumentException("Call Initialize() before processing");
}
int retryAfter = 60; // seconds - Make a new call after this interval
TimeSpan abortAfter = new TimeSpan(0, 10, 0); // stop waiting after this interval
DateTime start = DateTime.Now;
do
{
StreamPageResponse subscriptionPage = Stream.GetStreamPage(resellerCid, saAuthorizationToken.AccessToken);
foreach (var subscriptionEvent in subscriptionPage.items)
{
CSPStreamEvent streamEvent = new CSPStreamEvent(subscriptionEvent);
lastCSPEventTime = streamEvent.EventDate;
Console.WriteLine("Entry: " + streamEvent.SubscriptionEventType);
localManager.WriteCSPStreamEvent(streamEvent);
}
// event not found - mark page as read
Stream.MarkStreamPageComplete(subscriptionPage.links.completion.href, subscriptionPage.links.completion.method, saAuthorizationToken.AccessToken);
Console.WriteLine("sleeping");
Thread.Sleep(retryAfter * 1000);
} while (DateTime.Now < start + abortAfter);
}
public void EndProcessing()
{
if (String.IsNullOrEmpty(resellerCid) ||
adAuthorizationToken == null ||
saAuthorizationToken == null)
{
throw new ArgumentException("Call Initialize() before processing");
}
Stream.DeleteStream(resellerCid, saAuthorizationToken.AccessToken);
localManager.SaveLastCSPEventTime(lastCSPEventTime.AddMinutes(1));
}
}
}