in Source/Actions/Microsoft.Deployment.Actions.AzureCustom/Reddit/DeployRedditAzureMLWebServiceFromFile.cs [27:130]
public override async Task<ActionResponse> ExecuteActionAsync(ActionRequest request)
{
string azureToken = request.DataStore.GetJson("AzureToken", "access_token");
string subscription = request.DataStore.GetJson("SelectedSubscription", "SubscriptionId");
string webserviceFile = request.DataStore.GetValue("WebServiceFile");
string webserviceName = request.DataStore.GetValue("WebServiceName");
string commitmentPlanName = request.DataStore.GetValue("CommitmentPlan");
string resourceGroup = request.DataStore.GetValue("SelectedResourceGroup");
string storageAccountName = request.DataStore.GetValue("StorageAccountName");
string storageAccountKey = request.DataStore.GetValue("StorageAccountKey");
string responseType = request.DataStore.GetValue("IsRequestResponse");
bool isRequestResponse = false;
if (responseType != null)
{
isRequestResponse = bool.Parse(responseType);
}
string sentimentModelUri = request.DataStore.GetValue("SentimentModelUri");
string sentimentModelAssetName = request.DataStore.GetValue("AssetModelAzureMLName");
ServiceClientCredentials creds = new TokenCredentials(azureToken);
AzureMLWebServicesManagementClient client = new AzureMLWebServicesManagementClient(creds);
AzureMLCommitmentPlansManagementClient commitmentClient = new AzureMLCommitmentPlansManagementClient(creds);
client.SubscriptionId = subscription;
commitmentClient.SubscriptionId = subscription;
// Create commitment plan
var commitmentPlan = new Azure.Management.MachineLearning.CommitmentPlans.Models.CommitmentPlan();
commitmentPlan.Sku = new ResourceSku()
{
Capacity = 1,
Name = "S1",
Tier = "Standard"
};
commitmentPlan.Location = "South Central US";
var createdCommitmentPlan = await commitmentClient.CommitmentPlans.CreateOrUpdateAsync(commitmentPlan, resourceGroup, commitmentPlanName);
request.Logger.LogResource(request.DataStore, createdCommitmentPlan.Name,
DeployedResourceType.MlWebServicePlan, CreatedBy.BPST, DateTime.UtcNow.ToString("o"), createdCommitmentPlan.Id, commitmentPlan.Sku.Tier);
// Get webservicedefinition
string sqlConnectionString = request.DataStore.GetValueAtIndex("SqlConnectionString", "SqlServerIndex");
SqlCredentials sqlCredentials;
string jsonDefinition = File.ReadAllText(request.Info.App.AppFilePath + "/" + webserviceFile);
if (!string.IsNullOrWhiteSpace(sqlConnectionString))
{
sqlCredentials = SqlUtility.GetSqlCredentialsFromConnectionString(sqlConnectionString);
jsonDefinition = ReplaceSqlPasswords(sqlCredentials, jsonDefinition);
}
if (!string.IsNullOrWhiteSpace(sentimentModelUri) && !string.IsNullOrWhiteSpace(sentimentModelAssetName))
{
jsonDefinition = ReplaceSentimentModel(sentimentModelUri, sentimentModelAssetName, jsonDefinition);
}
// Create WebService - fixed to southcentralus
WebService webService = ModelsSerializationUtil.GetAzureMLWebServiceFromJsonDefinition(jsonDefinition);
webService.Properties.StorageAccount = new StorageAccount
{
Key = storageAccountKey,
Name = storageAccountName
};
webService.Properties.CommitmentPlan = new CommitmentPlan(createdCommitmentPlan.Id);
// A little bit of juggling to change the name
webService = new WebService(webService.Location, webService.Properties, null, webserviceName, webService.Type, webService.Tags);
webService.Validate();
WebService result = null;
try
{
result = client.WebServices.CreateOrUpdate(resourceGroup, webserviceName, webService);
var keys = client.WebServices.ListKeys(resourceGroup, webserviceName);
var swaggerLocation = result.Properties.SwaggerLocation;
string url = swaggerLocation.Replace("swagger.json", "jobs?api-version=2.0");
if (isRequestResponse)
{
url = swaggerLocation.Replace("swagger.json", "execute?api-version=2.0&format=swagger");
}
string serviceKey = keys.Primary;
request.DataStore.AddToDataStore("AzureMLUrl", url);
request.DataStore.AddToDataStore("AzureMLKey", serviceKey);
}
catch (CloudException e)
{
return new ActionResponse(ActionStatus.Failure, JsonUtility.GetJObjectFromStringValue(e.Message), e, "DefaultError", ((CloudException)e).Response.Content);
}
request.Logger.LogResource(request.DataStore, result.Name,
DeployedResourceType.MlWebService, CreatedBy.BPST, DateTime.UtcNow.ToString("o"), result.Id);
return new ActionResponse(ActionStatus.Success);
}