forked from Azure/azure-powershell
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0a78c02
commit cf6fd68
Showing
2 changed files
with
73 additions
and
7 deletions.
There are no files selected for viewing
70 changes: 63 additions & 7 deletions
70
experiments/Azure.Experiments/Azure.Experiments.Tests/ComputeTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,84 @@ | ||
using Microsoft.Rest; | ||
using System; | ||
using Xunit; | ||
using System.Net.Http.Headers; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using System.Net.Http; | ||
using System; | ||
using System.IO; | ||
using Newtonsoft.Json; | ||
using Microsoft.Azure.Management.Compute; | ||
|
||
namespace Azure.Experiments.Tests | ||
{ | ||
public class ComputeTest | ||
{ | ||
sealed class TokenProvider : ITokenProvider | ||
{ | ||
public Task<AuthenticationHeaderValue> GetAuthenticationHeaderAsync( | ||
{ | ||
public TokenProvider(Configuration c) | ||
{ | ||
var parameters = new[] | ||
{ | ||
KeyValuePair.Create("grant_type", "client_credentials"), | ||
KeyValuePair.Create("client_id", c.applicationId), | ||
KeyValuePair.Create("client_secret", c.clientSecret), | ||
KeyValuePair.Create("resource", "https://management.core.windows.net/") | ||
}; | ||
Content = new FormUrlEncodedContent(parameters); | ||
Uri = new Uri("https://login.microsoftonline.com/" + c.tenantId + "/oauth2/token"); | ||
} | ||
|
||
public async Task<AuthenticationHeaderValue> GetAuthenticationHeaderAsync( | ||
CancellationToken cancellationToken) | ||
{ | ||
throw new NotImplementedException(); | ||
if (Header == null) | ||
{ | ||
using (var client = new HttpClient()) | ||
{ | ||
var response = await client | ||
.PostAsync(Uri, Content) | ||
.Result | ||
.Content | ||
.ReadAsStringAsync(); | ||
var responseObject = JsonConvert.DeserializeObject<AuthenticationResponse>( | ||
response); | ||
Header = new AuthenticationHeaderValue( | ||
responseObject.token_type, responseObject.access_token); | ||
} | ||
} | ||
return Header; | ||
} | ||
|
||
private Uri Uri { get; } | ||
|
||
private FormUrlEncodedContent Content { get; } | ||
|
||
private AuthenticationHeaderValue Header { get; set; } | ||
} | ||
|
||
private sealed class AuthenticationResponse | ||
{ | ||
public string token_type; | ||
public string access_token; | ||
} | ||
|
||
private sealed class Configuration | ||
{ | ||
public string applicationId; | ||
public string tenantId; | ||
public string clientSecret; | ||
public string subscriptionId; | ||
} | ||
|
||
[Fact] | ||
public void Test1() | ||
public async void Test1() | ||
{ | ||
var credentials = new TokenCredentials(new TokenProvider()); | ||
var client = new Microsoft.Azure.Management.Compute.ComputeManagementClient(credentials); | ||
var text = File.ReadAllText(@"c:\Users\sergey\Desktop\php-test.json"); | ||
var c = JsonConvert.DeserializeObject<Configuration>(text); | ||
var credentials = new TokenCredentials(new TokenProvider(c)); | ||
var client = new ComputeManagementClient(credentials); | ||
client.SubscriptionId = c.subscriptionId; | ||
var list = await client.VirtualMachines.ListAllAsync(); | ||
} | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
experiments/Azure.Experiments/Azure.Experiments.Tests/KeyValuePair.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using System.Collections.Generic; | ||
|
||
namespace Azure.Experiments.Tests | ||
{ | ||
internal static class KeyValuePair | ||
{ | ||
public static KeyValuePair<K, V> Create<K, V>(K k, V v) | ||
=> new KeyValuePair<K, V>(k, v); | ||
} | ||
} |