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.
AzureObject, ResourceGroupObject, VirtualNetworkObject.
- Loading branch information
1 parent
cf6fd68
commit ebfa508
Showing
12 changed files
with
325 additions
and
67 deletions.
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
experiments/Azure.Experiments/Azure.Experiments.Tests/AuthenticationResponse.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,8 @@ | ||
namespace Azure.Experiments.Tests | ||
{ | ||
internal sealed class AuthenticationResponse | ||
{ | ||
public string token_type; | ||
public string access_token; | ||
} | ||
} |
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
83 changes: 19 additions & 64 deletions
83
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
10 changes: 10 additions & 0 deletions
10
experiments/Azure.Experiments/Azure.Experiments.Tests/Configuration.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 @@ | ||
namespace Azure.Experiments.Tests | ||
{ | ||
internal sealed class Configuration | ||
{ | ||
public string applicationId; | ||
public string tenantId; | ||
public string clientSecret; | ||
public string subscriptionId; | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
experiments/Azure.Experiments/Azure.Experiments.Tests/Credentials.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,16 @@ | ||
using Microsoft.Rest; | ||
using Newtonsoft.Json; | ||
using System.IO; | ||
|
||
namespace Azure.Experiments.Tests | ||
{ | ||
internal static class Credentials | ||
{ | ||
public static Context Get() | ||
{ | ||
var text = File.ReadAllText(@"c:\Users\sergey\Desktop\php-test.json"); | ||
var c = JsonConvert.DeserializeObject<Configuration>(text); | ||
return new Context(new TokenCredentials(new TokenProvider(c)), c.subscriptionId); | ||
} | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
experiments/Azure.Experiments/Azure.Experiments.Tests/TokenProvider.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,54 @@ | ||
using Microsoft.Rest; | ||
using System.Net.Http.Headers; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using System.Net.Http; | ||
using System; | ||
using Newtonsoft.Json; | ||
|
||
namespace Azure.Experiments.Tests | ||
{ | ||
sealed class TokenProvider : ITokenProvider | ||
{ | ||
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) | ||
{ | ||
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; } | ||
} | ||
} |
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
88 changes: 88 additions & 0 deletions
88
experiments/Azure.Experiments/Azure.Experiments/AzureObject.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,88 @@ | ||
using Microsoft.Rest.Azure; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net; | ||
using System.Threading.Tasks; | ||
|
||
namespace Azure.Experiments | ||
{ | ||
public abstract class AzureObject | ||
{ | ||
public static IEnumerable<AzureObject> NoDependencies { get; } | ||
= Enumerable.Empty<AzureObject>(); | ||
|
||
public string Name { get; } | ||
|
||
public IEnumerable<AzureObject> Dependencies { get; } | ||
|
||
public abstract Task CheckOrCreateAsync(Context c); | ||
|
||
protected AzureObject(string name, IEnumerable<AzureObject> dependencies) | ||
{ | ||
Name = name; | ||
Dependencies = dependencies; | ||
} | ||
} | ||
|
||
public abstract class AzureObject<T, C> : AzureObject | ||
where T: class | ||
{ | ||
public async Task<T> GetOrNullAsync(Context c) | ||
{ | ||
if (!IsGetCalled) | ||
{ | ||
IsGetCalled = true; | ||
try | ||
{ | ||
Info = await GetOrThrowAsync(CreateClient(c)); | ||
} | ||
catch (CloudException e) | ||
when (e.Response.StatusCode == HttpStatusCode.NotFound) | ||
{ | ||
} | ||
} | ||
return Info; | ||
} | ||
|
||
public async Task<T> GetOrCreateAsync(Context c) | ||
{ | ||
Info = await GetOrNullAsync(c); | ||
if (Info == null) | ||
{ | ||
// this can be optimized by using WaitForAll and a state | ||
// machine for `Task<T> Info`. The state machine is required to | ||
// avoid multiple creations of the same resource group. | ||
foreach (var d in Dependencies) | ||
{ | ||
await d.CheckOrCreateAsync(c); | ||
} | ||
Info = await CreateAsync(CreateClient(c)); | ||
} | ||
return Info; | ||
} | ||
|
||
public Task DeleteAsync(Context c) | ||
=> DeleteAsync(CreateClient(c)); | ||
|
||
public override Task CheckOrCreateAsync(Context c) | ||
=> GetOrCreateAsync(c); | ||
|
||
protected AzureObject(string name, IEnumerable<AzureObject> dependencies) | ||
: base(name, dependencies) | ||
{ | ||
} | ||
|
||
protected abstract C CreateClient(Context c); | ||
|
||
protected abstract Task<T> GetOrThrowAsync(C c); | ||
|
||
protected abstract Task<T> CreateAsync(C c); | ||
|
||
protected abstract Task DeleteAsync(C c); | ||
|
||
private bool IsGetCalled; | ||
|
||
private T Info; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
experiments/Azure.Experiments/Azure.Experiments/AzureResource.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,27 @@ | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace Azure.Experiments | ||
{ | ||
public abstract class AzureResource<T, C> : AzureObject<T, C> | ||
where T : class | ||
{ | ||
protected string ResourceGroupName { get; } | ||
|
||
protected AzureResource( | ||
string name, | ||
ResourceGroupObject rg, | ||
IEnumerable<AzureObject> dependencies) | ||
: base(name, dependencies.Concat(new[] { rg })) | ||
{ | ||
ResourceGroupName = rg.Name; | ||
} | ||
|
||
protected AzureResource( | ||
string name, | ||
ResourceGroupObject rg) | ||
: this(name, rg, Enumerable.Empty<AzureObject>()) | ||
{ | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
experiments/Azure.Experiments/Azure.Experiments/Context.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,17 @@ | ||
using Microsoft.Rest; | ||
|
||
namespace Azure.Experiments | ||
{ | ||
public class Context | ||
{ | ||
public Context(ServiceClientCredentials credentials, string subscriptionId) | ||
{ | ||
Credentials = credentials; | ||
SubscriptionId = subscriptionId; | ||
} | ||
|
||
public ServiceClientCredentials Credentials { get; } | ||
|
||
public string SubscriptionId { get; } | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
experiments/Azure.Experiments/Azure.Experiments/ResourceGroupObject.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,34 @@ | ||
using System.Linq; | ||
using Microsoft.Azure.Management.ResourceManager.Models; | ||
using Microsoft.Azure.Management.ResourceManager; | ||
using System.Threading.Tasks; | ||
|
||
namespace Azure.Experiments | ||
{ | ||
public sealed class ResourceGroupObject : AzureObject< | ||
ResourceGroup, IResourceGroupsOperations> | ||
{ | ||
public ResourceGroupObject(string name) | ||
: base(name, NoDependencies) | ||
{ | ||
} | ||
|
||
protected override IResourceGroupsOperations CreateClient(Context c) | ||
=> new ResourceManagementClient(c.Credentials) | ||
{ | ||
SubscriptionId = c.SubscriptionId | ||
} | ||
.ResourceGroups; | ||
|
||
protected override Task<ResourceGroup> CreateAsync(IResourceGroupsOperations c) | ||
=> c.CreateOrUpdateAsync( | ||
Name, | ||
new ResourceGroup { Location = "eastus" }); | ||
|
||
protected override Task<ResourceGroup> GetOrThrowAsync(IResourceGroupsOperations c) | ||
=> c.GetAsync(Name); | ||
|
||
protected override Task DeleteAsync(IResourceGroupsOperations c) | ||
=> c.DeleteAsync(Name); | ||
} | ||
} |
Oops, something went wrong.