Example #1
0
        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);
            }

            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);
            }

            // 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 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));
        }
Example #2
0
        public override async Task <ActionResponse> ExecuteActionAsync(ActionRequest request)
        {
            var azureToken   = request.DataStore.GetJson("AzureToken")["access_token"].ToString();
            var subscription = request.DataStore.GetJson("SelectedSubscription")["SubscriptionId"].ToString();

            var webserviceFile     = request.DataStore.GetValue("WebServiceFile");
            var webserviceName     = request.DataStore.GetValue("WebServiceName");
            var commitmentPlanName = request.DataStore.GetValue("CommitmentPlan");
            var resourceGroup      = request.DataStore.GetValue("SelectedResourceGroup");
            var storageAccountName = request.DataStore.GetValue("StorageAccountName");

            string         sqlConnectionString = request.DataStore.GetValueAtIndex("SqlConnectionString", "SqlServerIndex");
            SqlCredentials sqlCredentials      = SqlUtility.GetSqlCredentialsFromConnectionString(sqlConnectionString);

            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 createdsCommitmentPlan = await commitmentClient.CommitmentPlans.CreateOrUpdateAsync(commitmentPlan, resourceGroup, commitmentPlanName);

            // Get key from storage account
            var response = await RequestUtility.CallAction(request, "Microsoft-GetStorageAccountKey");

            var    responseObject = JsonUtility.GetJObjectFromObject(response.Body);
            string key            = responseObject["StorageAccountKey"].ToString();

            // Get webservicedefinition
            string jsonDefinition = File.ReadAllText(request.Info.App.AppFilePath + "/" + webserviceFile);
            string jsonFinal      = ReplaceSqlPasswords(sqlCredentials, jsonDefinition);

            // Create WebService - fixed to southcentralus
            WebService webService = ModelsSerializationUtil.GetAzureMLWebServiceFromJsonDefinition(jsonFinal);

            webService.Properties.StorageAccount = new StorageAccount
            {
                Key  = key,
                Name = storageAccountName
            };

            webService.Properties.CommitmentPlan = new CommitmentPlan(createdsCommitmentPlan.Id);
            webService.Name = webserviceName;

            var result = await client.WebServices.CreateOrUpdateAsync(resourceGroup, webserviceName, webService);

            var keys = await client.WebServices.ListKeysAsync(resourceGroup, webserviceName);

            var swaggerLocation = result.Properties.SwaggerLocation;

            string url        = swaggerLocation.Replace("swagger.json", "jobs?api-version=2.0");
            string serviceKey = keys.Primary;

            request.DataStore.AddToDataStore("AzureMLUrl", url);
            request.DataStore.AddToDataStore("AzureMLKey", serviceKey);

            return(new ActionResponse(ActionStatus.Success));
        }