forked from PartnerCenterSamples/Commerce-API-DotNet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Orchestrator.cs
747 lines (644 loc) · 33.4 KB
/
Orchestrator.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
using Microsoft.Partner.CSP.Api.V1.Samples.DataModels;
using System;
using System.Linq;
using System.Net;
namespace Microsoft.Partner.CSP.Api.V1.Samples
{
internal class Orchestrator
{
/// <summary>
/// Gets the customer entity for the given customer id
/// </summary>
/// <param name="defaultDomain">default domain of the reseller</param>
/// <param name="customerMicrosoftId">Microsoft Id of the customer</param>
/// <param name="resellerMicrosoftId">Microsoft Id of the reseller</param>
/// <returns>customer object</returns>
public static dynamic GetCustomer(string defaultDomain, string appId, string key, string customerMicrosoftId,
string resellerMicrosoftId)
{
// Get Active Directory token first
AuthorizationToken adAuthorizationToken = Reseller.GetAD_Token(defaultDomain, appId, key);
// Using the ADToken get the sales agent token
AuthorizationToken saAuthorizationToken = Reseller.GetSA_Token(adAuthorizationToken);
var existingCustomerCid = Customer.GetCustomerCid(customerMicrosoftId, resellerMicrosoftId,
saAuthorizationToken.AccessToken);
// Get Customer token
AuthorizationToken customerAuthorizationToken = Customer.GetCustomer_Token(existingCustomerCid,
adAuthorizationToken);
return Customer.GetCustomer(existingCustomerCid, customerAuthorizationToken.AccessToken);
}
/// <summary>
/// Get all subscriptions placed by the reseller for the customer
/// </summary>
/// <param name="defaultDomain">default domain of the reseller</param>
/// <param name="appId">appid that is registered for this application in Azure Active Directory (AAD)</param>
/// <param name="key">Key for this application in Azure Active Directory</param>
/// <param name="customerMicrosoftId">Microsoft Id of the customer</param>
/// <param name="resellerMicrosoftId">Microsoft Id of the reseller</param>
/// <returns>object that contains all of the subscriptions</returns>
public static dynamic GetSubscriptions(string defaultDomain, string appId, string key,
string customerMicrosoftId, string resellerMicrosoftId)
{
// Get Active Directory token first
AuthorizationToken adAuthorizationToken = Reseller.GetAD_Token(defaultDomain, appId, key);
// Using the ADToken get the sales agent token
AuthorizationToken saAuthorizationToken = Reseller.GetSA_Token(adAuthorizationToken);
// Get the Reseller Cid, you can cache this value
string resellerCid = Reseller.GetCid(resellerMicrosoftId, saAuthorizationToken.AccessToken);
// You can cache this value too
var customerCid = Customer.GetCustomerCid(customerMicrosoftId, resellerMicrosoftId,
saAuthorizationToken.AccessToken);
return Subscription.GetSubscriptions(customerCid, resellerCid, saAuthorizationToken.AccessToken);
}
/// <summary>
/// Get all orders placed by the reseller for this customer
/// </summary>
/// <param name="defaultDomain">default domain of the reseller</param>
/// <param name="appId">appid that is registered for this application in Azure Active Directory (AAD)</param>
/// <param name="key">Key for this application in Azure Active Directory</param>
/// <param name="customerMicrosoftId">Microsoft Id of the customer</param>
/// <param name="resellerMicrosoftId">Microsoft Id of the reseller</param>
/// <returns>object that contains orders</returns>
public static dynamic GetOrders(string defaultDomain, string appId, string key, string customerMicrosoftId,
string resellerMicrosoftId)
{
// Get Active Directory token first
AuthorizationToken adAuthorizationToken = Reseller.GetAD_Token(defaultDomain, appId, key);
// Using the ADToken get the sales agent token
AuthorizationToken saAuthorizationToken = Reseller.GetSA_Token(adAuthorizationToken);
// Get the Reseller Cid, you can cache this value
string resellerCid = Reseller.GetCid(resellerMicrosoftId, saAuthorizationToken.AccessToken);
// You can cache this value too
var customerCid = Customer.GetCustomerCid(customerMicrosoftId, resellerMicrosoftId,
saAuthorizationToken.AccessToken);
return Order.GetOrders(customerCid, resellerCid, saAuthorizationToken.AccessToken);
}
/// <summary>
/// Create an Order
/// </summary>
/// <param name="defaultDomain">default domain of the reseller</param>
/// <param name="appId">appid that is registered for this application in Azure Active Directory (AAD)</param>
/// <param name="key">Key for this application in Azure Active Directory</param>
/// <param name="customerMicrosoftId">Microsoft Id of the customer</param>
/// <param name="resellerMicrosoftId">Microsoft Id of the reseller</param>
public static string CreateOrder(string defaultDomain, string appId, string key,
string customerMicrosoftId, string resellerMicrosoftId)
{
string result = String.Empty;
// Get Active Directory token first
AuthorizationToken adAuthorizationToken = Reseller.GetAD_Token(defaultDomain, appId, key);
// Using the ADToken get the sales agent token
AuthorizationToken saAuthorizationToken = Reseller.GetSA_Token(adAuthorizationToken);
// Get the Reseller Cid, you can cache this value
string resellerCid = Reseller.GetCid(resellerMicrosoftId, saAuthorizationToken.AccessToken);
// You can cache this value too
var customerCid = Customer.GetCustomerCid(customerMicrosoftId, resellerMicrosoftId,
saAuthorizationToken.AccessToken);
// Populate a multi line item order
var existingCustomerOrder = Order.PopulateOrderFromConsole(customerCid);
// Place the order and subscription uri and entitlement uri are returned per each line item
var existingCustomerPlacedOrder = Order.PlaceOrder(existingCustomerOrder, resellerCid,
saAuthorizationToken.AccessToken);
foreach (var line_Item in existingCustomerPlacedOrder.line_items)
{
var subscription = Subscription.GetSubscriptionByUri(line_Item.resulting_subscription_uri,
saAuthorizationToken.AccessToken);
result = subscription.Id;
Console.WriteLine("Subscription: {0}", subscription.Id);
}
return result;
}
/// <summary>
/// Create an Order for Azure in Integration Sandbox
/// </summary>
/// <param name="defaultDomain">default domain of the reseller</param>
/// <param name="appId">appid that is registered for this application in Azure Active Directory (AAD)</param>
/// <param name="key">Key for this application in Azure Active Directory</param>
/// <param name="customerMicrosoftId">Microsoft Id of the customer</param>
/// <param name="resellerMicrosoftId">Microsoft Id of the reseller</param>
/// <returns></returns>
public static string CreateOrderWithAzureTip(string defaultDomain, string appId, string key,
string customerMicrosoftId, string resellerMicrosoftId)
{
string result = String.Empty;
// Get Active Directory token first
AuthorizationToken adAuthorizationToken = Reseller.GetAD_Token(defaultDomain, appId, key);
// Using the ADToken get the sales agent token
AuthorizationToken saAuthorizationToken = Reseller.GetSA_Token(adAuthorizationToken);
// Get the Reseller Cid, you can cache this value
string resellerCid = Reseller.GetCid(resellerMicrosoftId, saAuthorizationToken.AccessToken);
// You can cache this value too
var customerCid = Customer.GetCustomerCid(customerMicrosoftId, resellerMicrosoftId,
saAuthorizationToken.AccessToken);
// Populate an Azure in TIP order
var newOrder = Order.PopulateAzureOrderInTIP(customerCid);
// Place the order and subscription uri and entitlement uri are returned per each line item
var existingCustomerPlacedOrder = Order.PlaceOrder(newOrder, resellerCid,
saAuthorizationToken.AccessToken);
foreach (var line_Item in existingCustomerPlacedOrder.line_items)
{
var subscription = Subscription.GetSubscriptionByUri(line_Item.resulting_subscription_uri,
saAuthorizationToken.AccessToken);
result = subscription.Id;
Console.WriteLine("Subscription: {0}", subscription.Id);
}
return result;
}
/// <summary>
/// Create a new customer
/// </summary>
/// <param name="defaultDomain">default domain of the reseller</param>
/// <param name="appId">appid that is registered for this application in Azure Active Directory (AAD)</param>
/// <param name="key">Key for this application in Azure Active Directory</param>
/// <param name="resellerMicrosoftId">Microsoft Id of the reseller</param>
/// <returns></returns>
internal static string CreateCustomer(string defaultDomain, string appId, string key,
string resellerMicrosoftId)
{
// Get Active Directory token first
AuthorizationToken adAuthorizationToken = Reseller.GetAD_Token(defaultDomain, appId, key);
// Using the ADToken get the sales agent token
AuthorizationToken saAuthorizationToken = Reseller.GetSA_Token(adAuthorizationToken);
// Get the Reseller Cid, you can cache this value
string resellerCid = Reseller.GetCid(resellerMicrosoftId, saAuthorizationToken.AccessToken);
// Get input from the console application for creating a new customer
var customer = Customer.PopulateCustomerFromConsole();
// This is the created customer object that contains the cid, the microsoft tenant id etc
var createdCustomer = Customer.CreateCustomer(customer, resellerCid, saAuthorizationToken.AccessToken);
if (createdCustomer == null)
{
throw new Exception("Error creating customer");
}
string newCustomerMicrosoftId = createdCustomer.customer.identity.data.tid;
// Populate a multi line item order
var newCustomerOrder = Order.PopulateOrderWithMultipleLineItems(createdCustomer.customer.id);
// Place the order and subscription uri and entitlement uri are returned per each line item
var newCustomerPlacedOrder = Order.PlaceOrder(newCustomerOrder, resellerCid, saAuthorizationToken.AccessToken);
foreach (var line_Item in newCustomerPlacedOrder.line_items)
{
var subscription = Subscription.GetSubscriptionByUri(line_Item.resulting_subscription_uri, saAuthorizationToken.AccessToken);
Console.WriteLine("Subscription: {0}", subscription.Id);
}
return newCustomerMicrosoftId;
}
/// <summary>
/// Get a customer's usage information for the last 1 month, calculates the total cost using RateCard API
/// and Suspends the subscription if the total cost is more than the credit limit.
/// </summary>
/// <param name="defaultDomain">default domain of the reseller</param>
/// <param name="appId">appid that is registered for this application in Azure Active Directory (AAD)</param>
/// <param name="key">Key for this application in Azure Active Directory</param>
/// <param name="customerMicrosoftId">Microsoft Id of the customer</param>
/// <param name="resellerMicrosoftId">Microsoft Id of the reseller</param>
public static void GetRateCardAndUsage(string defaultDomain, string appId, string key,
string customerMicrosoftId, string resellerMicrosoftId)
{
var correlationId = Guid.NewGuid().ToString();
// Get Active Directory token first
AuthorizationToken adAuthorizationToken = Reseller.GetAD_Token(defaultDomain, appId, key);
// Using the ADToken get the sales agent token
AuthorizationToken saAuthorizationToken = Reseller.GetSA_Token(adAuthorizationToken);
// Get the Reseller Cid, you can cache this value
string resellerCid = Reseller.GetCid(resellerMicrosoftId, saAuthorizationToken.AccessToken);
// You can cache this value too
var customerCid = Customer.GetCustomerCid(customerMicrosoftId, resellerMicrosoftId,
saAuthorizationToken.AccessToken);
// Get Customer token
AuthorizationToken customerAuthorizationToken = Customer.GetCustomer_Token(customerCid, adAuthorizationToken);
// Gets the RateCard to get the prices
var rateCard = RateCard.GetRateCard(resellerCid, saAuthorizationToken.AccessToken);
var startTime = String.Format("{0:u}", DateTime.Today.AddDays(-30));
var endTime = String.Format("{0:u}", DateTime.Today.AddDays(-1));
// Get all of a Customer's entitlements
var entitlements = Usage.GetEntitlements(customerCid, customerAuthorizationToken.AccessToken);
try
{
foreach (var entitlement in entitlements.items)
{
// Get the usage for the given entitlement for the last 1 month
var usageRecords = Usage.GetUsageRecords(resellerCid, entitlement.id, saAuthorizationToken.AccessToken,
startTime, endTime);
if (usageRecords.items.Count > 0)
{
Console.ForegroundColor = ConsoleColor.DarkGreen;
Console.WriteLine("================================================================================");
Console.WriteLine("\nPrices for Entitlement: {0}", entitlement.id);
Console.WriteLine("================================================================================");
double totalCost = 0;
// Looping through the usage records to calculate the cost of each item
foreach (UsageType usageRecord in usageRecords.items)
{
string meterId = usageRecord.meter_id;
// Gets the price corresponding to the given meterId from the ratecard.
Console.WriteLine("\nMeter Name\t\t: {0}", usageRecord.meter_name);
double includedQty = Usage.GetIncludedQuantityByMeterID(rateCard, meterId);
Console.WriteLine("Included Quantity\t\t: {0}", includedQty);
double consumedQty = (double)usageRecord.quantity;
Console.WriteLine("Consumed Quantity\t\t: {0}", consumedQty);
double ratableUsage = Usage.GetRatableUsage(consumedQty, includedQty);
double cost = Usage.computeRatedUsagePerMeter(Usage.GetRatesForMeterID(rateCard, meterId), ratableUsage);
Console.WriteLine("Cost\t\t: {0}", cost);
totalCost += cost;
}
Console.WriteLine("\nTOTAL COST: {0}", totalCost);
// Setting the credit limit below the total cost for testing this scenario
double creditLimit = 100;
// Suspends the subscription if the total cost is above the credit limit.
if (totalCost > creditLimit)
{
var subscription = Subscription.GetSubscriptionByUri(entitlement.billing_subscription_uri,
saAuthorizationToken.AccessToken);
Subscription.SuspendSubscription(subscription.id, resellerCid, saAuthorizationToken.AccessToken);
}
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
/// <summary>
/// Transition an Office 365 subscription to another (Office 365 Enterprise E1) SKU.
/// Creates a subscription stream and gather events from the stream and show them on console.
/// </summary>
/// <param name="subscriptionId">Subscription Id</param>
/// <param name="defaultDomain">default domain of the reseller</param>
/// <param name="appId">appid that is registered for this application in Azure Active Directory (AAD)</param>
/// <param name="key">Key for this application in Azure Active Directory</param>
/// <param name="customerMicrosoftId">Microsoft Id of the customer</param>
/// <param name="resellerMicrosoftId">Microsoft Id of the reseller</param>
public static void TransitionToNewSKU(string subscriptionId, string defaultDomain, string appId, string key,
string customerMicrosoftId, string resellerMicrosoftId)
{
// Get Active Directory token first
AuthorizationToken adAuthorizationToken = Reseller.GetAD_Token(defaultDomain, appId, key);
// Using the ADToken get the sales agent token
AuthorizationToken saAuthorizationToken = Reseller.GetSA_Token(adAuthorizationToken);
// Get the Reseller Cid, you can cache this value
string resellerCid = Reseller.GetCid(resellerMicrosoftId, saAuthorizationToken.AccessToken);
// You can cache this value too
var customerCid = Customer.GetCustomerCid(customerMicrosoftId, resellerMicrosoftId,
saAuthorizationToken.AccessToken);
// Correlation Id to be used for this scenaario
var correlationId = Guid.NewGuid().ToString();
// Offer Uri for an Office 365 Enterprise E1 subscription
string newOfferUri = PromptForOfferUri();
Subscription.Transition(subscriptionId, customerCid, newOfferUri, resellerCid,
saAuthorizationToken.AccessToken);
WaitForSuccessfulTransition(resellerCid, subscriptionId, saAuthorizationToken.AccessToken);
}
/// <summary>
/// List all customers for the reseller, loops through them and delete one by one.
/// </summary>
/// <param name="defaultDomain">default domain of the reseller</param>
/// <param name="appId">appid that is registered for this application in Azure Active Directory (AAD)</param>
/// <param name="key">Key for this application in Azure Active Directory</param>
/// <param name="customerMicrosoftId">Microsoft Id of the customer</param>
/// <param name="resellerMicrosoftId">Microsoft Id of the reseller</param>
public static void ListAndDeleteAllCustomers(string defaultDomain, string appId, string key,
string resellerMicrosoftId, string customerMicrosoftId)
{
// Get Active Directory token first
AuthorizationToken adAuthorizationToken = Reseller.GetAD_Token(defaultDomain, appId, key);
// Using the ADToken get the sales agent token
AuthorizationToken saAuthorizationToken = Reseller.GetSA_Token(adAuthorizationToken);
// Get the Reseller Cid, you can cache this value
string resellerCid = Reseller.GetCid(resellerMicrosoftId, saAuthorizationToken.AccessToken);
var customers = Customer.GetAllCustomers(resellerMicrosoftId, adAuthorizationToken.AccessToken);
foreach (var customer in customers.value)
{
Console.WriteLine("\nCustomer Info");
Console.WriteLine("\tCustomer Id\t: {0}", customer.customerContextId);
Console.WriteLine("\tName\t: {0}", customer.displayName);
var existingCustomerCid = Customer.GetCustomerCid(customer.customerContextId, resellerMicrosoftId,
saAuthorizationToken.AccessToken);
Console.WriteLine("Deleting Customer " + customer.displayName);
Customer.DeleteCustomer(resellerCid, existingCustomerCid, saAuthorizationToken.AccessToken);
}
}
/// <summary>
/// Monitor the stream api, to see if the transition is successful.
/// </summary>
/// <param name="resellerCid">cid of the reseller</param>
/// <param name="subscriptionId">Subscription Id</param>
/// <param name="saToken">sales agent token</param>
private static void WaitForSuccessfulTransition(string resellerCid, string subscriptionId, string saToken)
{
Boolean transitionComplete = false;
const string transitionCompletedStatus = "subscription_provisioned";
var subscriptionUri = String.Format("/{0}/subscriptions/{1}", resellerCid, subscriptionId);
// Nmae for the subscription stream
const string streamName = "SampleCodeSubscriptionStream";
Subscription.CreateSubscriptionStream(resellerCid, streamName,
saToken);
var subStream = Subscription.GetSubscriptionStream(resellerCid, streamName,
saToken);
var items = subStream.items;
while (!transitionComplete)
{
foreach (var item in items)
{
if (subscriptionUri.Equals(item.subscription_uri) && transitionCompletedStatus.Equals(item.type))
{
transitionComplete = true;
break;
}
}
if (!transitionComplete)
{
subStream = Subscription.MarkPageAsCompletedInStream(subStream.links.completion.href,
saToken);
items = subStream.items;
}
}
}
/// <summary>
/// Prompts user to select the offer to transition to.
/// </summary>
/// <returns>offer uri</returns>
private static string PromptForOfferUri()
{
GroupedOffers selectedGroupedOffers =
OfferCatalog.Instance.GroupedOffersCollection.First(
groupedOffers => groupedOffers.OfferType == OfferType.IntuneAndOffice);
Console.Write("\nSelect Offer (by index):\n ");
foreach (
var item in
selectedGroupedOffers.Offers.Select((offer, index) => new { Offer = offer, Index = index }))
{
Console.WriteLine("{0}. {1}", item.Index + 1, item.Offer.Name);
}
bool done = false;
int selectedIndex = -1;
do
{
string input = Console.ReadLine().Trim();
if (int.TryParse(input, out selectedIndex))
{
done = true;
}
} while (!done);
var selectedOffer = selectedGroupedOffers.Offers.ElementAt(selectedIndex - 1);
return selectedOffer.Uri;
}
/// <summary>
/// Creates a virtual machine in Azure.
/// Creates all the required resources before creating VM.
/// </summary>
/// <param name="subscriptionId">Subscription Id</param>
/// <param name="credentialName">Internet name/address used to identify entry in Credential Manager</param>
/// <param name="appId">appid that is registered for this application in Azure Active Directory (AAD)</param>
/// <param name="customerTenantId">Id or Domain of the customer Azure tenant</param>
private static void CreateAzureVirtualMachine(string appId, string credentialName, string customerTenantId, string subscriptionId)
{
// Get Azure Authentication Token
string azureToken = Reseller.GetAzureAuthTokenForCustomerTenant(appId, credentialName, customerTenantId);
// Correlation Id to be used for this scenaario
var correlationId = Guid.NewGuid().ToString();
var resourceGroupName = Guid.NewGuid().ToString();
// Appending suffix to resource group name to build names of other resources
// Storage account names must be between 3 and 24 characters in length and use numbers and lower-case letters only.
// So removing hyphen and truncating.
var storageAccountName = String.Format("{0}sa", resourceGroupName.Replace("-", "").Remove(20));
var networkSecurityGroupName = String.Format("{0}_nsg", resourceGroupName);
var networkSecurityGroupId =
String.Format(
"/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.Network/networkSecurityGroups/{2}",
subscriptionId, resourceGroupName, networkSecurityGroupName);
var virtualNetworkName = String.Format("{0}_vn", resourceGroupName);
var subnetName = String.Format("{0}_sn", resourceGroupName);
var subNetId =
String.Format(
"/subscriptions/{0}/resourceGroups/{1}/providers/microsoft.network/virtualNetworks/{2}/subnets/{3}",
subscriptionId, resourceGroupName, virtualNetworkName, subnetName);
var publicIpName = String.Format("{0}_pip", resourceGroupName);
var publicIpId =
String.Format(
"/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.Network/publicIPAddresses/{2}",
subscriptionId, resourceGroupName, publicIpName);
var networkInterfaceName = String.Format("{0}_nic", resourceGroupName);
var networkInterfaceId =
String.Format(
"/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.Network/networkInterfaces/{2}",
subscriptionId, resourceGroupName, networkInterfaceName);
var ipName = String.Format("{0}_ip", resourceGroupName);
var vitualMachineName = String.Format("{0}_vm", resourceGroupName);
// Waiting Time (in seconds) For Resource to be Provisioned
var retryAfter = 5;
// Retry attempts for resource provisioning errors
var retryAttempts = 5;
var attempts = 0;
// The following resources are to be created in order before creating a virtual machine.
// #1 Create Resource Group
do
{
var createResourceGroupResponse = AzureResourceManager.CreateResourceGroup(subscriptionId, resourceGroupName,
azureToken, correlationId);
if (createResourceGroupResponse == null)
{
attempts++;
}
else
{
attempts = retryAttempts + 1;
// Waiting for the resource group to be created, if the request is just Accepted and the creation is still pending.
if ((int)(createResourceGroupResponse.StatusCode) == (int)HttpStatusCode.Accepted)
{
AzureResourceManager.WaitForResourceGroupProvisioning(subscriptionId, resourceGroupName, retryAfter,
azureToken, correlationId);
}
}
} while (attempts < retryAttempts);
// #2 Create Storage Account
// Register the subscription with Storage Resource Provider, for creating Storage Account
// Storage Resource Provider
const string storageProviderName = "Microsoft.Storage";
AzureResourceManager.RegisterProvider(subscriptionId, storageProviderName, azureToken, correlationId);
attempts = 0;
do
{
var storageAccountResponse = AzureResourceManager.CreateStorageAccount(subscriptionId, resourceGroupName,
storageAccountName, azureToken, correlationId);
if (storageAccountResponse == null)
{
attempts++;
}
else
{
attempts = retryAttempts + 1;
// Waiting for the storage account to be created, if the request is just Accepted and the creation is still pending.
if ((int)(storageAccountResponse.StatusCode) == (int)HttpStatusCode.Accepted)
{
var location = storageAccountResponse.Headers.Get("Location");
retryAfter = Int32.Parse(storageAccountResponse.Headers.Get("Retry-After"));
AzureResourceManager.WaitForStorageAccountProvisioning(location, resourceGroupName, retryAfter,
azureToken, correlationId);
}
}
} while (attempts < retryAttempts);
// Register the subscription with Network Resource Provider for creating Network Resources - Netowrk Securtiy Group, Virtual Network, Subnet, Public IP and Network Interface
// Network Resource Provider
const string networkProviderName = "Microsoft.Network";
AzureResourceManager.RegisterProvider(subscriptionId, networkProviderName, azureToken, correlationId);
// #3 Create Network Security Group
attempts = 0;
do
{
var networkSecurityGroupResponse = AzureResourceManager.CreateNetworkSecurityGroup(subscriptionId,
resourceGroupName, networkSecurityGroupName, azureToken, correlationId);
if (networkSecurityGroupResponse == null)
{
attempts++;
}
else
{
attempts = retryAttempts + 1;
// Waiting for the network security group to be created, if the request is just Accepted and the creation is still pending.
if ((int)(networkSecurityGroupResponse.StatusCode) == (int)HttpStatusCode.Accepted)
{
AzureResourceManager.WaitForNetworkSecurityGroupProvisioning(subscriptionId, resourceGroupName,
networkSecurityGroupName, retryAfter, azureToken, correlationId);
}
}
} while (attempts < retryAttempts);
// #4 Create Virtual Network
attempts = 0;
do
{
var virtualNetworkResponse = AzureResourceManager.CreateVirtualNetwork(subscriptionId, resourceGroupName,
virtualNetworkName, azureToken, correlationId);
if (virtualNetworkResponse == null)
{
attempts++;
}
else
{
attempts = retryAttempts + 1;
// Waiting for the virtual network to be created, if the request is just Accepted and the creation is still pending.
if ((int)(virtualNetworkResponse.StatusCode) == (int)HttpStatusCode.Accepted)
{
AzureResourceManager.WaitForVirtualNetworkProvisioning(subscriptionId, resourceGroupName,
virtualNetworkName, retryAfter, azureToken, correlationId);
}
}
} while (attempts < retryAttempts);
// #5 Create Subnet
attempts = 0;
do
{
var subNetResponse = AzureResourceManager.CreateSubNet(subscriptionId, resourceGroupName, virtualNetworkName,
networkSecurityGroupId, subnetName, azureToken, correlationId);
if (subNetResponse == null)
{
attempts++;
}
else
{
attempts = retryAttempts + 1;
// Waiting for the subnet to be created, if the request is just Accepted and the creation is still pending.
if ((int)(subNetResponse.StatusCode) == (int)HttpStatusCode.Accepted)
{
AzureResourceManager.WaitForSubNetProvisioning(subscriptionId, resourceGroupName, virtualNetworkName,
subnetName, retryAfter, azureToken, correlationId);
}
}
} while (attempts < retryAttempts);
// #6 Create Public IP Address
attempts = 0;
do
{
var publicIpResponse = AzureResourceManager.CreatePublicIpAddress(subscriptionId, resourceGroupName,
publicIpName, azureToken, correlationId);
if (publicIpResponse == null)
{
attempts++;
}
else
{
attempts = retryAttempts + 1;
// Waiting for the public IP to be created, if the request is just Accepted and the creation is still pending.
if ((int)(publicIpResponse.StatusCode) == (int)HttpStatusCode.Accepted)
{
AzureResourceManager.WaitForPublicIpProvisioning(subscriptionId, resourceGroupName, publicIpName,
retryAfter, azureToken, correlationId);
}
}
} while (attempts < retryAttempts);
// #7 Create Network Interface
attempts = 0;
do
{
var networkInterfaceResponse = AzureResourceManager.CreateNetworkInterface(subscriptionId, resourceGroupName,
networkInterfaceName, networkSecurityGroupId, ipName, publicIpId, subNetId, azureToken, correlationId);
if (networkInterfaceResponse == null)
{
attempts++;
}
else
{
attempts = retryAttempts + 1;
// Waiting for the network interface to be created, if the request is just Accepted and the creation is still pending.
if ((int)(networkInterfaceResponse.StatusCode) == (int)HttpStatusCode.Accepted)
{
AzureResourceManager.WaitForNetworkInterfaceProvisioning(subscriptionId, resourceGroupName,
networkInterfaceName, retryAfter, azureToken, correlationId);
}
}
} while (attempts < retryAttempts);
// #8 Create Azure Virtual Machine
// Compute Resource Provider
const string computeProviderName = "Microsoft.Compute";
AzureResourceManager.RegisterProvider(subscriptionId, computeProviderName, azureToken, correlationId);
var virtualMachineResponse = AzureResourceManager.CreateVirtualMachine(subscriptionId, resourceGroupName,
networkInterfaceId, storageAccountName, vitualMachineName, azureToken, correlationId);
if (virtualMachineResponse == null)
{
return;
}
// #9 Create a New User
var newUserInfo = User.PopulateUserFromConsole();
var newUser = AzureResourceManager.CreateUser(newUserInfo.displayName, newUserInfo.mailNickname,
newUserInfo.userPrincipalName, newUserInfo.password,
azureToken, correlationId);
// #10 Add user to Owner role
// Register the subscription with Authorization Provider
const string authorizationProviderName = "Microsoft.Authorization";
var registrationResult = AzureResourceManager.RegisterProvider(subscriptionId, authorizationProviderName, azureToken, correlationId);
// Role Id for Role 'Owner'
const string roleIdForOwner = "8e3af657-a8ff-443c-a75c-2fe8c4bcb635";
var scope = String.Format("/subscriptions/{0}", subscriptionId);
// Assigning 'Owner' role to the new user
var roleAssignment = AzureResourceManager.CreateRoleAssignment(azureToken, scope, newUser.objectId, roleIdForOwner,
correlationId);
}
/// <summary>
/// Creates a virtual machine in a new Azure subscription for a new customer.
/// </summary>
/// <param name="defaultDomain">default domain of the reseller</param>
/// <param name="ptrCtrAppId">appId for this program that is registered in the Partner Center</param>
/// <param name="ptrCtrAppSecret">app secret for this program that is registered in the Partner Center</param>
/// <param name="resellerMicrosoftId">cid of the reseller</param>
/// <param name="azureAppId">appId for this program that is registered in the reseller azure directory and configured for pre-consent</param>
/// <param name="credentialName">Internet or network address label of entry in credential manager</param>
public static void CreateVirtualMachineInNewSubscription(string defaultDomain, string ptrCtrAppId, string ptrCtrAppSecret,
string resellerMicrosoftId, string azureAppId, string credentialName)
{
// Get Active Directory token first
AuthorizationToken adAuthorizationToken = Reseller.GetAD_Token(defaultDomain, ptrCtrAppId, ptrCtrAppSecret);
// Using the ADToken get the sales agent token
AuthorizationToken saAuthorizationToken = Reseller.GetSA_Token(adAuthorizationToken);
// Get the Reseller Cid, you can cache this value
string resellerCid = Reseller.GetCid(resellerMicrosoftId, saAuthorizationToken.AccessToken);
var s = Stream.CreateStream(resellerCid, saAuthorizationToken.AccessToken);
string newCustomerMicrosoftId = Orchestrator.CreateCustomer(defaultDomain, ptrCtrAppId, ptrCtrAppSecret, resellerMicrosoftId);
string newSubscriptionId = Orchestrator.CreateOrderWithAzureTip(defaultDomain, ptrCtrAppId, ptrCtrAppSecret, newCustomerMicrosoftId, resellerMicrosoftId);
bool continueProcess = Stream.WaitForSubscriptionEvent(resellerCid, saAuthorizationToken.AccessToken, newSubscriptionId, StreamEvents.Subscription.Provisioned);
if (continueProcess)
{
CreateAzureVirtualMachine(azureAppId, credentialName, newCustomerMicrosoftId, newSubscriptionId);
}
Stream.DeleteStream(resellerCid, saAuthorizationToken.AccessToken);
}
}
}