Skip to content

Commit

Permalink
First test
Browse files Browse the repository at this point in the history
  • Loading branch information
sergey-shandar committed Oct 13, 2017
1 parent 0a78c02 commit cf6fd68
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 7 deletions.
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();
}
}
}
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);
}
}

0 comments on commit cf6fd68

Please sign in to comment.