Skip to content

Commit ebfa508

Browse files
AzureObject, ResourceGroupObject, VirtualNetworkObject.
1 parent cf6fd68 commit ebfa508

File tree

12 files changed

+325
-67
lines changed

12 files changed

+325
-67
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace Azure.Experiments.Tests
2+
{
3+
internal sealed class AuthenticationResponse
4+
{
5+
public string token_type;
6+
public string access_token;
7+
}
8+
}

experiments/Azure.Experiments/Azure.Experiments.Tests/Azure.Experiments.Tests.csproj

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0-preview-20170425-07" />
11-
<PackageReference Include="xunit" Version="2.2.0" />
12-
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
10+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.3.0" />
11+
<PackageReference Include="System.ValueTuple" Version="4.4.0" />
12+
<PackageReference Include="xunit" Version="2.3.0" />
13+
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.0" />
1314
</ItemGroup>
1415

1516
<ItemGroup>

experiments/Azure.Experiments/Azure.Experiments.Tests/ComputeTest.cs

Lines changed: 19 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,38 @@
1-
using Microsoft.Rest;
21
using Xunit;
3-
using System.Net.Http.Headers;
4-
using System.Threading;
5-
using System.Threading.Tasks;
6-
using System.Net.Http;
7-
using System;
8-
using System.IO;
9-
using Newtonsoft.Json;
102
using Microsoft.Azure.Management.Compute;
113

124
namespace Azure.Experiments.Tests
135
{
146
public class ComputeTest
157
{
16-
sealed class TokenProvider : ITokenProvider
17-
{
18-
public TokenProvider(Configuration c)
19-
{
20-
var parameters = new[]
21-
{
22-
KeyValuePair.Create("grant_type", "client_credentials"),
23-
KeyValuePair.Create("client_id", c.applicationId),
24-
KeyValuePair.Create("client_secret", c.clientSecret),
25-
KeyValuePair.Create("resource", "https://management.core.windows.net/")
26-
};
27-
Content = new FormUrlEncodedContent(parameters);
28-
Uri = new Uri("https://login.microsoftonline.com/" + c.tenantId + "/oauth2/token");
29-
}
30-
31-
public async Task<AuthenticationHeaderValue> GetAuthenticationHeaderAsync(
32-
CancellationToken cancellationToken)
33-
{
34-
if (Header == null)
35-
{
36-
using (var client = new HttpClient())
37-
{
38-
var response = await client
39-
.PostAsync(Uri, Content)
40-
.Result
41-
.Content
42-
.ReadAsStringAsync();
43-
var responseObject = JsonConvert.DeserializeObject<AuthenticationResponse>(
44-
response);
45-
Header = new AuthenticationHeaderValue(
46-
responseObject.token_type, responseObject.access_token);
47-
}
48-
}
49-
return Header;
50-
}
51-
52-
private Uri Uri { get; }
53-
54-
private FormUrlEncodedContent Content { get; }
55-
56-
private AuthenticationHeaderValue Header { get; set; }
57-
}
58-
59-
private sealed class AuthenticationResponse
8+
[Fact]
9+
public async void ResourceGroupTest()
6010
{
61-
public string token_type;
62-
public string access_token;
11+
var c = Credentials.Get();
12+
var rg = new ResourceGroupObject("My");
13+
var info = await rg.GetOrNullAsync(c);
14+
var infoCreate = await rg.GetOrCreateAsync(c);
15+
// await rg.DeleteAsync(c);
6316
}
6417

65-
private sealed class Configuration
18+
[Fact]
19+
public async void VirtualNetworkTest()
6620
{
67-
public string applicationId;
68-
public string tenantId;
69-
public string clientSecret;
70-
public string subscriptionId;
21+
var c = Credentials.Get();
22+
var rg = new ResourceGroupObject("My1");
23+
var vn = new VirtualNetworkObject("My1", rg, "192.168.0.0/16");
24+
var info = await vn.GetOrNullAsync(c);
25+
var infoCreate = await vn.GetOrCreateAsync(c);
7126
}
7227

7328
[Fact]
7429
public async void Test1()
7530
{
76-
var text = File.ReadAllText(@"c:\Users\sergey\Desktop\php-test.json");
77-
var c = JsonConvert.DeserializeObject<Configuration>(text);
78-
var credentials = new TokenCredentials(new TokenProvider(c));
79-
var client = new ComputeManagementClient(credentials);
80-
client.SubscriptionId = c.subscriptionId;
31+
var c = Credentials.Get();
32+
var client = new ComputeManagementClient(c.Credentials)
33+
{
34+
SubscriptionId = c.SubscriptionId
35+
};
8136
var list = await client.VirtualMachines.ListAllAsync();
8237
}
8338
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace Azure.Experiments.Tests
2+
{
3+
internal sealed class Configuration
4+
{
5+
public string applicationId;
6+
public string tenantId;
7+
public string clientSecret;
8+
public string subscriptionId;
9+
}
10+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using Microsoft.Rest;
2+
using Newtonsoft.Json;
3+
using System.IO;
4+
5+
namespace Azure.Experiments.Tests
6+
{
7+
internal static class Credentials
8+
{
9+
public static Context Get()
10+
{
11+
var text = File.ReadAllText(@"c:\Users\sergey\Desktop\php-test.json");
12+
var c = JsonConvert.DeserializeObject<Configuration>(text);
13+
return new Context(new TokenCredentials(new TokenProvider(c)), c.subscriptionId);
14+
}
15+
}
16+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using Microsoft.Rest;
2+
using System.Net.Http.Headers;
3+
using System.Threading;
4+
using System.Threading.Tasks;
5+
using System.Net.Http;
6+
using System;
7+
using Newtonsoft.Json;
8+
9+
namespace Azure.Experiments.Tests
10+
{
11+
sealed class TokenProvider : ITokenProvider
12+
{
13+
public TokenProvider(Configuration c)
14+
{
15+
var parameters = new[]
16+
{
17+
KeyValuePair.Create("grant_type", "client_credentials"),
18+
KeyValuePair.Create("client_id", c.applicationId),
19+
KeyValuePair.Create("client_secret", c.clientSecret),
20+
KeyValuePair.Create("resource", "https://management.core.windows.net/")
21+
};
22+
Content = new FormUrlEncodedContent(parameters);
23+
Uri = new Uri("https://login.microsoftonline.com/" + c.tenantId + "/oauth2/token");
24+
}
25+
26+
public async Task<AuthenticationHeaderValue> GetAuthenticationHeaderAsync(
27+
CancellationToken cancellationToken)
28+
{
29+
if (Header == null)
30+
{
31+
using (var client = new HttpClient())
32+
{
33+
var response = await client
34+
.PostAsync(Uri, Content)
35+
.Result
36+
.Content
37+
.ReadAsStringAsync();
38+
var responseObject = JsonConvert.DeserializeObject<AuthenticationResponse>(
39+
response);
40+
Header = new AuthenticationHeaderValue(
41+
responseObject.token_type,
42+
responseObject.access_token);
43+
}
44+
}
45+
return Header;
46+
}
47+
48+
private Uri Uri { get; }
49+
50+
private FormUrlEncodedContent Content { get; }
51+
52+
private AuthenticationHeaderValue Header { get; set; }
53+
}
54+
}

experiments/Azure.Experiments/Azure.Experiments/Azure.Experiments.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
<ItemGroup>
88
<PackageReference Include="Microsoft.Azure.Management.Compute" Version="16.3.0" />
9+
<PackageReference Include="Microsoft.Azure.Management.Network" Version="15.0.0-preview" />
10+
<PackageReference Include="Microsoft.Azure.Management.ResourceManager" Version="1.6.0-preview" />
911
</ItemGroup>
1012

1113
</Project>
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
using Microsoft.Rest.Azure;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Net;
6+
using System.Threading.Tasks;
7+
8+
namespace Azure.Experiments
9+
{
10+
public abstract class AzureObject
11+
{
12+
public static IEnumerable<AzureObject> NoDependencies { get; }
13+
= Enumerable.Empty<AzureObject>();
14+
15+
public string Name { get; }
16+
17+
public IEnumerable<AzureObject> Dependencies { get; }
18+
19+
public abstract Task CheckOrCreateAsync(Context c);
20+
21+
protected AzureObject(string name, IEnumerable<AzureObject> dependencies)
22+
{
23+
Name = name;
24+
Dependencies = dependencies;
25+
}
26+
}
27+
28+
public abstract class AzureObject<T, C> : AzureObject
29+
where T: class
30+
{
31+
public async Task<T> GetOrNullAsync(Context c)
32+
{
33+
if (!IsGetCalled)
34+
{
35+
IsGetCalled = true;
36+
try
37+
{
38+
Info = await GetOrThrowAsync(CreateClient(c));
39+
}
40+
catch (CloudException e)
41+
when (e.Response.StatusCode == HttpStatusCode.NotFound)
42+
{
43+
}
44+
}
45+
return Info;
46+
}
47+
48+
public async Task<T> GetOrCreateAsync(Context c)
49+
{
50+
Info = await GetOrNullAsync(c);
51+
if (Info == null)
52+
{
53+
// this can be optimized by using WaitForAll and a state
54+
// machine for `Task<T> Info`. The state machine is required to
55+
// avoid multiple creations of the same resource group.
56+
foreach (var d in Dependencies)
57+
{
58+
await d.CheckOrCreateAsync(c);
59+
}
60+
Info = await CreateAsync(CreateClient(c));
61+
}
62+
return Info;
63+
}
64+
65+
public Task DeleteAsync(Context c)
66+
=> DeleteAsync(CreateClient(c));
67+
68+
public override Task CheckOrCreateAsync(Context c)
69+
=> GetOrCreateAsync(c);
70+
71+
protected AzureObject(string name, IEnumerable<AzureObject> dependencies)
72+
: base(name, dependencies)
73+
{
74+
}
75+
76+
protected abstract C CreateClient(Context c);
77+
78+
protected abstract Task<T> GetOrThrowAsync(C c);
79+
80+
protected abstract Task<T> CreateAsync(C c);
81+
82+
protected abstract Task DeleteAsync(C c);
83+
84+
private bool IsGetCalled;
85+
86+
private T Info;
87+
}
88+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
4+
namespace Azure.Experiments
5+
{
6+
public abstract class AzureResource<T, C> : AzureObject<T, C>
7+
where T : class
8+
{
9+
protected string ResourceGroupName { get; }
10+
11+
protected AzureResource(
12+
string name,
13+
ResourceGroupObject rg,
14+
IEnumerable<AzureObject> dependencies)
15+
: base(name, dependencies.Concat(new[] { rg }))
16+
{
17+
ResourceGroupName = rg.Name;
18+
}
19+
20+
protected AzureResource(
21+
string name,
22+
ResourceGroupObject rg)
23+
: this(name, rg, Enumerable.Empty<AzureObject>())
24+
{
25+
}
26+
}
27+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using Microsoft.Rest;
2+
3+
namespace Azure.Experiments
4+
{
5+
public class Context
6+
{
7+
public Context(ServiceClientCredentials credentials, string subscriptionId)
8+
{
9+
Credentials = credentials;
10+
SubscriptionId = subscriptionId;
11+
}
12+
13+
public ServiceClientCredentials Credentials { get; }
14+
15+
public string SubscriptionId { get; }
16+
}
17+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System.Linq;
2+
using Microsoft.Azure.Management.ResourceManager.Models;
3+
using Microsoft.Azure.Management.ResourceManager;
4+
using System.Threading.Tasks;
5+
6+
namespace Azure.Experiments
7+
{
8+
public sealed class ResourceGroupObject : AzureObject<
9+
ResourceGroup, IResourceGroupsOperations>
10+
{
11+
public ResourceGroupObject(string name)
12+
: base(name, NoDependencies)
13+
{
14+
}
15+
16+
protected override IResourceGroupsOperations CreateClient(Context c)
17+
=> new ResourceManagementClient(c.Credentials)
18+
{
19+
SubscriptionId = c.SubscriptionId
20+
}
21+
.ResourceGroups;
22+
23+
protected override Task<ResourceGroup> CreateAsync(IResourceGroupsOperations c)
24+
=> c.CreateOrUpdateAsync(
25+
Name,
26+
new ResourceGroup { Location = "eastus" });
27+
28+
protected override Task<ResourceGroup> GetOrThrowAsync(IResourceGroupsOperations c)
29+
=> c.GetAsync(Name);
30+
31+
protected override Task DeleteAsync(IResourceGroupsOperations c)
32+
=> c.DeleteAsync(Name);
33+
}
34+
}

0 commit comments

Comments
 (0)