Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes issue #36 - Allows a user to select an existing resource group. I... #43

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Slingshot.Api/Content/Styles/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ pre { margin-bottom:20px; }
overflow: hidden;
}

input[type="text"]:disabled{
background-color: #969393;
}

.code {font-size:12px; }

.code-attrName{color:#ff0000;}
Expand Down
35 changes: 22 additions & 13 deletions Slingshot.Api/Controllers/ARMController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,25 +95,27 @@ public async Task<HttpResponseMessage> Get()
[Authorize]
[HttpPost]
#pragma warning disable 4014
public async Task<HttpResponseMessage> Preview([FromBody] JObject parameters, string subscriptionId, string templateUrl)
public async Task<HttpResponseMessage> Preview(DeployInputs inputs)
{
JObject responseObj = new JObject();
List<string> providers = new List<string>(32);
HttpResponseMessage response = null;
using (var client = GetRMClient(subscriptionId))
using (var client = GetRMClient(inputs.subscriptionId))
{
ResourceGroupCreateOrUpdateResult resourceResult = null;
string tempRGName = Guid.NewGuid().ToString();

try
{
resourceResult = await client.ResourceGroups.CreateOrUpdateAsync(tempRGName, new BasicResourceGroup { Location = "East US" });
resourceResult = await client.ResourceGroups.CreateOrUpdateAsync(
tempRGName,
new BasicResourceGroup { Location = inputs.resourceGroup.location });

// For now we just default to East US for the resource group location.
var basicDeployment = new BasicDeployment
{
Parameters = parameters.ToString(),
TemplateLink = new TemplateLink(new Uri(templateUrl))
Parameters = inputs.parameters.ToString(),
TemplateLink = new TemplateLink(new Uri(inputs.templateUrl))
};

var deploymentResult = await client.Deployments.ValidateAsync(tempRGName, tempRGName, basicDeployment);
Expand Down Expand Up @@ -146,7 +148,7 @@ public async Task<HttpResponseMessage> Preview([FromBody] JObject parameters, st
(resourceResult.StatusCode == HttpStatusCode.Created || resourceResult.StatusCode == HttpStatusCode.OK))
{
string token = GetTokenFromHeader();
Task.Run(() => { DeleteResourceGroup(subscriptionId, token, tempRGName); });
Task.Run(() => { DeleteResourceGroup(inputs.subscriptionId, token, tempRGName); });
}
}
}
Expand All @@ -171,25 +173,32 @@ private void DeleteResourceGroup(string subscriptionId, string token, string rgN

[Authorize]
[HttpPost]
public async Task<HttpResponseMessage> Deploy([FromBody] JObject parameters, string subscriptionId, string resourceGroup, string templateUrl)
public async Task<HttpResponseMessage> Deploy(DeployInputs inputs)
{
CreateDeploymentResponse responseObj = new CreateDeploymentResponse();
HttpResponseMessage response = null;

try
{
using (var client = GetRMClient(subscriptionId))
using (var client = GetRMClient(inputs.subscriptionId))
{
// For now we just default to East US for the resource group location.
var resourceResult = await client.ResourceGroups.CreateOrUpdateAsync(resourceGroup, new BasicResourceGroup { Location = "East US" });
var templateParams = parameters.ToString();
var resourceResult = await client.ResourceGroups.CreateOrUpdateAsync(
inputs.resourceGroup.name,
new BasicResourceGroup { Location = inputs.resourceGroup.location });

var templateParams = inputs.parameters.ToString();
var basicDeployment = new BasicDeployment
{
Parameters = templateParams,
TemplateLink = new TemplateLink(new Uri(templateUrl))
TemplateLink = new TemplateLink(new Uri(inputs.templateUrl))
};

var deploymentResult = await client.Deployments.CreateOrUpdateAsync(resourceGroup, resourceGroup, basicDeployment);
var deploymentResult = await client.Deployments.CreateOrUpdateAsync(
inputs.resourceGroup.name,
inputs.resourceGroup.name,
basicDeployment);

response = Request.CreateResponse(HttpStatusCode.OK, responseObj);
}
}
Expand Down Expand Up @@ -340,7 +349,7 @@ public async Task<HttpResponseMessage> GetTemplate(string repositoryUrl)
returnObj["subscriptions"] = JArray.FromObject(subscriptions);
returnObj["tenants"] = tenants;
returnObj["userDisplayName"] = userDisplayName;
returnObj["resourceGroup"] = resourceGroupName;
returnObj["resourceGroupName"] = resourceGroupName;
returnObj["template"] = template;
returnObj["templateUrl"] = templateUrl;
returnObj["repositoryUrl"] = repo.RepositoryUrl;
Expand Down
60 changes: 50 additions & 10 deletions Slingshot.Api/Helpers/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,11 @@
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using System.Collections.Generic;
using Slingshot.Models;

namespace Slingshot.Helpers
{
public class SubscriptionInfo
{
public string id { get; set; }
public string subscriptionId { get; set; }
public string displayName { get; set; }
public string state { get; set; }
}

public class Utils
{
public class ResultOf<T>
Expand All @@ -41,8 +35,21 @@ public static async Task<SubscriptionInfo[]> GetSubscriptionsAsync(string host,
{
if (response.IsSuccessStatusCode)
{
var result = await response.Content.ReadAsAsync<ResultOf<SubscriptionInfo>>();
return result.value;
var subs = (await response.Content.ReadAsAsync<ResultOf<SubscriptionInfo>>()).value;

var getRgTasks = new List<Task<ResourceGroupInfo[]>>();
foreach (var sub in subs)
{
getRgTasks.Add(GetResourceGroups(client, host, sub.subscriptionId));
}

var rgsForAllSubs = await Task.WhenAll(getRgTasks.ToArray());
for(int i = 0; i < rgsForAllSubs.Length; i ++)
{
subs[i].resourceGroups = rgsForAllSubs[i];
}

return subs;
}

var content = await response.Content.ReadAsStringAsync();
Expand All @@ -60,6 +67,39 @@ public static async Task<SubscriptionInfo[]> GetSubscriptionsAsync(string host,
}
}

private static async Task<ResourceGroupInfo[]> GetResourceGroups(
HttpClient client,
string host,
string subscriptionId)
{
string url = string.Format(
"{0}/subscriptions/{1}/resourceGroups?api-version={2}",
Utils.GetCSMUrl(host),
subscriptionId,
Constants.CSM.ApiVersion);

using (var response = await client.GetAsync(url))
{
if (response.IsSuccessStatusCode)
{
return (await response.Content.ReadAsAsync<ResultOf<ResourceGroupInfo>>()).value;
}

var content = await response.Content.ReadAsStringAsync();
if (content.StartsWith("{"))
{
var error = (JObject)JObject.Parse(content)["error"];
if (error != null)
{
throw new InvalidOperationException(String.Format("GetResourceGroups {0}, {1}", response.StatusCode, error.Value<string>("message")));
}
}

throw new InvalidOperationException(String.Format("GetResourceGroups {0}, {1}", response.StatusCode, await response.Content.ReadAsStringAsync()));

}
}

public static async Task<HttpResponseMessage> Execute(Task<HttpResponseMessage> task)
{
var watch = new Stopwatch();
Expand Down
16 changes: 16 additions & 0 deletions Slingshot.Api/Models/Inputs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Newtonsoft.Json.Linq;

namespace Slingshot.Models
{
public class DeployInputs
{
public JObject parameters { get; set; }
public string subscriptionId { get; set; }
public ResourceGroupInfo resourceGroup { get; set; }
public string templateUrl { get; set; }
}
}
23 changes: 23 additions & 0 deletions Slingshot.Api/Models/Subscriptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Slingshot.Models
{
public class SubscriptionInfo
{
public string id { get; set; }
public string subscriptionId { get; set; }
public string displayName { get; set; }
public string state { get; set; }
public ResourceGroupInfo[] resourceGroups { get; set; }
}

public class ResourceGroupInfo
{
//public string id { get; set; }
public string name { get; set; }
public string location { get; set; }
}
}
2 changes: 2 additions & 0 deletions Slingshot.Api/Slingshot.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@
<Compile Include="Helpers\Constants.cs" />
<Compile Include="Helpers\Utils.cs" />
<Compile Include="Models\CreateDeploymentResponse.cs" />
<Compile Include="Models\Inputs.cs" />
<Compile Include="Models\Subscriptions.cs" />
<Compile Include="Modules\AADOAuth2AccessToken.cs" />
<Compile Include="Modules\ARMOAuthModule.cs" />
<Compile Include="Modules\OpenIdConfiguration.cs" />
Expand Down
Loading