-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathUtilities.cs
169 lines (151 loc) · 5.87 KB
/
Utilities.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
/********************************************************
* *
* Copyright (C) Microsoft. All rights reserved. *
* *
*********************************************************/
namespace Microsoft.Partner.CSP.Api.V1.Samples
{
using System;
using System.IO;
using System.Net;
public class Utilities
{
/// <summary>
/// This method prints the web request to console
/// </summary>
/// <param name="request">request object</param>
/// <param name="content">content in the request</param>
public static void PrintWebRequest(HttpWebRequest request, string content)
{
Console.WriteLine("================================================================================");
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.WriteLine("{0} {1} HTTP/{2}", request.Method, request.RequestUri, request.ProtocolVersion);
foreach (var webHeaderName in request.Headers.AllKeys)
{
Console.WriteLine("{0}: {1}", webHeaderName, (webHeaderName == "Authorization" ? "<token suppressed>" : request.Headers[webHeaderName]));
}
Console.ForegroundColor = ConsoleColor.Gray;
if (request.Method != "GET")
{
Console.WriteLine("\n{0}", content);
}
Console.ResetColor();
}
/// <summary>
/// This method is for printing error responses
/// </summary>
/// <param name="response">response object</param>
/// <param name="content">content in the response</param>
public static void PrintErrorResponse(HttpWebResponse response, string content)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("\n\nHTTP/{0} {1} {2}", response.ProtocolVersion, (int)response.StatusCode, response.StatusDescription);
foreach (var webHeaderName in response.Headers.AllKeys)
{
Console.WriteLine("{0}: {1}", webHeaderName, response.Headers[webHeaderName]);
}
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("\n{0}", content);
Console.ResetColor();
}
/// <summary>
/// This method is for printing web responses
/// </summary>
/// <param name="response">response object</param>
/// <param name="content">content in the web response</param>
public static void PrintWebResponse(HttpWebResponse response, string content)
{
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.WriteLine("\n\nHTTP/{0} {1} {2}", response.ProtocolVersion, (int)response.StatusCode, response.StatusDescription);
foreach (var webHeaderName in response.Headers.AllKeys)
{
Console.WriteLine("{0}: {1}", webHeaderName, response.Headers[webHeaderName]);
}
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine("\n{0}", content);
Console.ResetColor();
}
/// <summary>
/// Prompts the user to edit the config paramerters in app.config
/// </summary>
public static void ValidateConfiguration(string microsoftId, string defaultDomain,
string appId, string key, string ExistingCustomerMicrosoftId, string azureAppId, string credentialName)
{
if (microsoftId.Equals("Your Organization Microsoft Id") || defaultDomain.Equals("Your default domain, something like contoso.onmicrosoft.com") ||
appId.Equals("Your App Id") || key.Equals("Your key goes here") || ExistingCustomerMicrosoftId.Equals("Your existing customer ID") ||
azureAppId.Equals("Your Azure App Id") || credentialName.Equals("Your credential name"))
{
Console.Write(
"\nHave you updated the app.config, with the settings from https://partnercenter.microsoft.com (y/n)? ");
string response = Console.ReadLine().Trim().ToUpperInvariant();
if (response != "Y" && response != "YES")
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(
"\nUpdate AppId, Key, MicrosoftId, DefaultDomain in the app.config and run the app again");
Console.ResetColor();
Console.Write("\n\n\nHit enter to exit the app now");
Console.ReadLine();
return;
}
}
}
/// <summary>
/// Write content to file
/// </summary>
/// <param name="path">path of file</param>
/// <param name="content">content to write</param>
/// <param name="append">if false, file is deleted before write</param>
public static void WriteRateCardToFile(string path, string content, bool append)
{
try
{
if (!append)
{
if (File.Exists(path))
{
File.Delete(path);
}
}
using (StreamWriter w = File.AppendText(path))
{
w.WriteLine("\n{0}", content);
}
}
catch (Exception exception)
{
string message = exception.Message;
return;
}
}
/// <summary>
/// Prompts the user to enter a valid number for scenario in app.config
/// </summary>
public static void PromptForScenario()
{
Console.Write("\nPlease enter a valid number (1-8) for scenario in the app.config and run the app again.");
Console.WriteLine("\n Scenarios:");
Console.WriteLine("\n 1 : Get Customer");
Console.WriteLine("\n 2 : Get Subscriptions");
Console.WriteLine("\n 3 : Get Orders");
Console.WriteLine("\n 4 : Create Order");
Console.WriteLine("\n 5 : Get Rate Card and Usage");
Console.WriteLine("\n 6 : Create Azure Virtual Machine");
Console.WriteLine("\n 7 : Transition to New SKU");
Console.WriteLine("\n 8 : List All Customers and Delete");
Console.Write("\n\n\nHit enter to exit the app now.");
Console.ReadLine();
return;
}
/// <summary>
/// Prompts the user to enter a subscription id in app.config
/// </summary>
public static void PromptForSubscriptionId()
{
Console.Write("\nPlease enter the SubscriptionId in the app.config and run the app again.");
Console.Write("\n\n\nHit enter to exit the app now.");
Console.ReadLine();
return;
}
}
}