public override async Task <ActionResponse> ExecuteActionAsync(ActionRequest request) { var azureToken = request.DataStore.GetJson("AzureToken", "access_token"); var environId = request.DataStore.GetValue("EnvironmentID").ToString(); var entityName = request.DataStore.GetValue("EntityName").ToString(); var namespaceID = environId; var cdmEntity = System.IO.File.ReadAllText(System.IO.Path.Combine(request.ControllerModel.SiteCommonFilePath, "Service/Arm/sampleCDMEntity.json")); //var cdmEntity = JsonUtility.GetJObjectFromJsonString(System.IO.File.ReadAllText(System.IO.Path.Combine(request.ControllerModel.SiteCommonFilePath, "Service/Arm/sampleCDMEntity.json"))); if (environId.Contains("Legacy")) { int indexFrom = environId.IndexOf("Legacy-") + "Legacy-".Length; int indexTo = environId.Length; namespaceID = environId.Substring(indexFrom, indexTo - indexFrom); } AzureHttpClient client = new AzureHttpClient(azureToken); var checkEntities = await RequestUtility.CallAction(request, "Microsoft-CheckCDMEntities"); if (!checkEntities.IsSuccess) { return(new ActionResponse(ActionStatus.FailureExpected)); } var response = await client.ExecuteGenericRequestWithHeaderAsync(HttpMethod.Put, $"https://management.azure.com/providers/Microsoft.CommonDataModel/environments/{environId}/namespaces/{namespaceID}/entities/{entityName}_?api-version=2016-11-01", cdmEntity); var responseString = await response.Content.ReadAsStringAsync(); return(new ActionResponse(ActionStatus.Success)); }
public override async Task <ActionResponse> ExecuteActionAsync(ActionRequest request) { string targetName = request.DataStore.GetValue("CredentialTarget2") ?? request.DataStore.GetValue("CredentialTarget"); string userName = request.DataStore.GetValue("CredentialUsername2") ?? request.DataStore.GetValue("CredentialUsername"); string password = request.DataStore.GetValue("CredentialPassword2") ?? request.DataStore.GetValue("CredentialPassword"); ActionResponse response = await RequestUtility.CallAction(request, "Microsoft-CredentialManagerDelete"); if (response.Status == ActionStatus.Failure) { return(response); } if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(password)) { return(new ActionResponse(ActionStatus.Success, new JObject())); } Credential c = new Credential(userName, password, targetName, CredentialType.Generic) { PersistenceType = PersistenceType.LocalComputer }; if (c.Save()) { return(new ActionResponse(ActionStatus.Success, new JObject())); } else { return(new ActionResponse(ActionStatus.Failure, new JObject(), new Win32Exception(Marshal.GetLastWin32Error()), "CredMgrWriteError")); } }
private async Task <ActionResponse> ConfirmStatus(ActionRequest request, DataTable recordCounts, string actionName) { ActionResponse response = new ActionResponse(IsAtLeastOneRecordComingIn(recordCounts) ? ActionStatus.Success : ActionStatus.InProgress, JsonUtility.Serialize <DataPullStatus>(new DataPullStatus() { IsFinished = false, Status = JsonUtility.SerializeTable(recordCounts) })); ActionResponse confirmation = await RequestUtility.CallAction(request, actionName); if (confirmation != null) { switch (confirmation.Status) { case ActionStatus.Failure: response = confirmation; break; case ActionStatus.Success: response = new ActionResponse(ActionStatus.Success, JsonUtility.Serialize <DataPullStatus>(new DataPullStatus() { IsFinished = true, Slices = JObject.FromObject(confirmation.Body)["value"]?.ToString(), Status = JsonUtility.SerializeTable(recordCounts) })); break; } } return(response); }
public override async Task <ActionResponse> ExecuteActionAsync(ActionRequest request) { var azureToken = request.DataStore.GetJson("AzureToken", "access_token"); string environmentId = request.DataStore.GetValue("PowerAppEnvironment"); string sqlConnectionId = request.DataStore.GetValue("PowerAppSqlConnectionId"); try { if (environmentId != null && sqlConnectionId != null) { ActionResponse wrangledFile = await RequestUtility.CallAction(request, "Microsoft-WranglePowerApp"); if (wrangledFile.IsSuccess && wrangledFile.Body != null) { string path = wrangledFile.Body.ToString(); AzureHttpClient ahc = new AzureHttpClient(azureToken); PowerAppResourceStorage resourceStorage = await ahc.Request <PowerAppResourceStorage>(HttpMethod.Post, string.Format(PowerAppUtility.URL_POWERAPPS_GENERATE_RESOURCE_STORAGE, JsonUtility.GetWebToken(azureToken, "oid")), JsonUtility.Serialize <PowerAppEnvironmentWrapper>(new PowerAppEnvironmentWrapper(environmentId))); if (!string.IsNullOrEmpty(path) && resourceStorage != null && !string.IsNullOrEmpty(resourceStorage.SharedAccessSignature)) { string uri = resourceStorage.SharedAccessSignature.Replace("?", "/document.msapp?"); CloudBlockBlob blob = new CloudBlockBlob(new Uri(uri)); using (WebClient wc = new WebClient()) { byte[] file = wc.DownloadData(path); await blob.UploadFromStreamAsync(new MemoryStream(file)); PowerAppMetadata metadata = await ahc.Request <PowerAppMetadata>(HttpMethod.Post, PowerAppUtility.URL_POWERAPPS_PUBLISH_APP, JsonUtility.Serialize <PowerAppPublish>(new PowerAppPublish(uri, $"TwitterTemplate{RandomGenerator.GetDateStamp()}", environmentId, sqlConnectionId))); if (metadata != null) { if (await ahc.IsSuccess(HttpMethod.Post, string.Format(PowerAppUtility.URL_POWERAPPS_SQL_CONNECTION_UPDATE, sqlConnectionId, environmentId), JsonUtility.Serialize <PowerAppSqlConnectionUpdate>(new PowerAppSqlConnectionUpdate(metadata.Name)))) { if (await ahc.IsSuccess(HttpMethod.Post, string.Format(PowerAppUtility.URL_POWERAPPS_RECORD_SCOPES_CONSENT, metadata.Name), JsonUtility.Serialize <PowerAppConsents>(new PowerAppConsents(sqlConnectionId)))) { request.DataStore.AddToDataStore("PowerAppUri", string.Format(PowerAppUtility.URL_POWERAPPS_WEB, metadata.Name)); } } } } } } } } catch { PowerAppUtility.SkipPowerApp(request.DataStore); } return(new ActionResponse(ActionStatus.Success)); }
public override async Task <ActionResponse> ExecuteActionAsync(ActionRequest request) { var azureToken = request.DataStore.GetJson("AzureToken", "access_token"); var createEnvironment = await RequestUtility.CallAction(request, "Microsoft-CreateEnvironment"); var createDatabase = await RequestUtility.CallAction(request, "Microsoft-CreateDatabase"); return(new ActionResponse(ActionStatus.Success)); }
public override async Task <ActionResponse> ExecuteActionAsync(ActionRequest request) { ActionResponse response; bool isWaitingForAtLeastOneRecord = request.DataStore.GetValue("IsWaiting") == null ? false : bool.Parse(request.DataStore.GetValue("IsWaiting")); string connectionString = request.DataStore.GetValueAtIndex("SqlConnectionString", "SqlServerIndex"); // Must specify Initial Catalog string finishedActionName = request.DataStore.GetValue("FinishedActionName"); string targetSchema = request.DataStore.GetValue("TargetSchema"); // Specifies the schema used by the template string query = $"[{targetSchema}].sp_get_replication_counts"; DataTable recordCounts; try { recordCounts = SqlUtility.InvokeStoredProcedure(connectionString, query, null); } catch { // It's ok for this to fail, we'll just return an empty table recordCounts = new DataTable(); } bool isAtLeastOneRecordComingIn = false; if (isWaitingForAtLeastOneRecord) { foreach (DataRow row in recordCounts.Rows) { isAtLeastOneRecordComingIn = Convert.ToInt64(row["Count"]) > 0; if (isAtLeastOneRecordComingIn) { break; } } response = isAtLeastOneRecordComingIn ? new ActionResponse(ActionStatus.Success, JsonUtility.GetEmptyJObject()) : new ActionResponse(ActionStatus.BatchNoState, JsonUtility.GetEmptyJObject()); } else { response = new ActionResponse(ActionStatus.Success, JsonUtility.GetJsonObjectFromJsonString("{isFinished:false,status:" + JsonUtility.Serialize(recordCounts) + "}")); } if (string.IsNullOrEmpty(finishedActionName)) { return(response); } ActionResponse finishedResponse = await RequestUtility.CallAction(request, finishedActionName); if (response.Status == ActionStatus.BatchNoState && finishedResponse.Status == ActionStatus.BatchNoState) { return(response); } var content = JObject.FromObject(finishedResponse.Body)["value"]?.ToString(); if ((isAtLeastOneRecordComingIn && finishedResponse.Status != ActionStatus.Failure) || finishedResponse.Status == ActionStatus.Success) { var resp = new ActionResponse(); if (!string.IsNullOrEmpty(content)) { resp = new ActionResponse(ActionStatus.Success, JsonUtility.GetJsonObjectFromJsonString( "{isFinished:true,FinishedActionName:\"" + finishedActionName + "\",TargetSchema:\"" + targetSchema + "\",status:" + JsonUtility.Serialize(recordCounts) + ", slices:" + JObject.FromObject(finishedResponse.Body)["value"]?.ToString() + "}")); } else { resp = new ActionResponse(ActionStatus.Success, JsonUtility.GetJsonObjectFromJsonString( "{isFinished:true, status:" + JsonUtility.Serialize(recordCounts) + "}")); } return(resp); } if (finishedResponse.Status == ActionStatus.BatchNoState || finishedResponse.Status == ActionStatus.BatchWithState) { var resp = new ActionResponse(); if (!string.IsNullOrEmpty(content)) { resp = new ActionResponse(ActionStatus.Success, JsonUtility.GetJsonObjectFromJsonString( "{isFinished:false,FinishedActionName:\"" + finishedActionName + "\",TargetSchema:\"" + targetSchema + "\",status:" + JsonUtility.Serialize(recordCounts) + ", slices:" + JObject.FromObject(finishedResponse.Body)["value"]?.ToString() + "}")); } else { resp = new ActionResponse(ActionStatus.Success, JsonUtility.GetJsonObjectFromJsonString( "{isFinished:false, status:" + JsonUtility.Serialize(recordCounts) + "}")); } return(resp); } return(finishedResponse); }
public override async Task <ActionResponse> ExecuteActionAsync(ActionRequest request) { string azureToken = request.DataStore.GetJson("AzureToken", "access_token"); string subscription = request.DataStore.GetJson("SelectedSubscription", "SubscriptionId"); string resourceGroup = request.DataStore.GetValue("SelectedResourceGroup"); request.DataStore.AddToDataStore("requestparameters", "AzureProvider", "Microsoft.CognitiveServices"); var location = request.DataStore.GetValue("CognitiveLocation"); string permissionsToCheck = request.DataStore.GetValue("CognitiveServices"); if (!(await RequestUtility.CallAction(request, "Microsoft-RegisterProviderBeta")).IsSuccess) { return(new ActionResponse(ActionStatus.Failure, null, null, null, "Unable to register Cognitive Services")); } List <string> cognitiveServicesToCheck = permissionsToCheck.Split(',').Select(p => p.Trim()).ToList(); AzureHttpClient client = new AzureHttpClient(azureToken, subscription, resourceGroup); bool passPermissionCheck = true; // Check if permissions are fine var getPermissionsResponse = await client.ExecuteWithSubscriptionAsync(HttpMethod.Get, $"providers/Microsoft.CognitiveServices/locations/{location}/settings/accounts", "2016-02-01-preview", string.Empty); var getPermissionsBody = JsonUtility.GetJsonObjectFromJsonString(await getPermissionsResponse.Content.ReadAsStringAsync()); foreach (var permission in getPermissionsBody["settings"]) { if (cognitiveServicesToCheck.Contains(permission["kind"].ToString()) && permission["allowCreate"].ToString().ToLowerInvariant() == "false") { passPermissionCheck = false; } if (cognitiveServicesToCheck.Contains(permission["kind"].ToString()) && permission["allowCreate"].ToString().ToLowerInvariant() == "true") { cognitiveServicesToCheck.Remove(permission["kind"].ToString()); } } if (passPermissionCheck && cognitiveServicesToCheck.Count == 0) { return(new ActionResponse(ActionStatus.Success)); } // IF not then check if user can enable var getOwnerResponse = await client.ExecuteWithSubscriptionAsync(HttpMethod.Post, $"providers/Microsoft.CognitiveServices/locations/{location}/checkAccountOwner", "2016-02-01-preview", JsonUtility.GetEmptyJObject().ToString()); var getOwnerBody = JsonUtility.GetJsonObjectFromJsonString(await getOwnerResponse.Content.ReadAsStringAsync()); if (getOwnerBody["isAccountOwner"].ToString().ToLowerInvariant() == "false") { return(new ActionResponse(ActionStatus.Failure, getOwnerBody, null, null, $"Your account admin ({getOwnerBody["accountOwnerEmail"].ToString()}) needs to enable cognitive services for this subscription. Ensure the account admin has at least contributor privileges to the Azure subscription. " + $"The following cognitive service should be enabled in order to proceed- {permissionsToCheck}")); } // User does not have permission but we can enable permission for the user as they are the admin dynamic obj = new ExpandoObject(); obj.resourceType = "accounts"; obj.settings = new ExpandoObject[cognitiveServicesToCheck.Count]; for (int i = 0; i < cognitiveServicesToCheck.Count; i++) { obj.settings[i] = new ExpandoObject(); obj.settings[i].kind = cognitiveServicesToCheck[i]; obj.settings[i].allowCreate = true; } var setPermissionsResponse = await client.ExecuteWithSubscriptionAsync(HttpMethod.Post, $"providers/Microsoft.CognitiveServices/locations/{location}/updateSettings", "2016-02-01-preview", JsonUtility.GetJObjectFromObject(obj).ToString()); if (!setPermissionsResponse.IsSuccessStatusCode) { return(new ActionResponse(ActionStatus.Failure, await setPermissionsResponse.Content.ReadAsStringAsync(), null, null, $"Unable to assign permissions for the cogntivie services {permissionsToCheck}. Use the Azure Portal to enable these services. Ensure you have at least contributor privilige to the subscription")); } return(new ActionResponse(ActionStatus.Success)); }
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)); }
public override async Task <ActionResponse> ExecuteActionAsync(ActionRequest request) { string azureToken = request.DataStore.GetJson("AzureToken", "access_token"); string refreshToken = request.DataStore.GetJson("AzureToken", "refresh_token"); string subscription = request.DataStore.GetJson("SelectedSubscription", "SubscriptionId"); string resourceGroup = request.DataStore.GetValue("SelectedResourceGroup"); string triggerUrl = request.DataStore.GetValue("NotifierTriggerUrl"); string connectionString = request.DataStore.GetValue("SqlConnectionString"); string deploymentIdsConnection = Constants.BpstDeploymentIdDatabase; string deploymentId = Guid.NewGuid().ToString(); string dataPullCompleteThreshold = "80"; var asDisabled = request.DataStore.GetValue("ssasDisabled"); Dictionary <string, string> configValues = new Dictionary <string, string>() { { "NotifierUrl", Constants.BpstNotifierUrl }, { "NotificationEmails", request.DataStore.GetValue("EmailAddress") }, { "DeploymentId", deploymentId }, { "TemplateName", request.Info.AppName }, { "DeploymentTimestamp", DateTime.UtcNow.ToString("o") }, { "ASDeployment", string.IsNullOrEmpty(asDisabled) ? "False" : (!Convert.ToBoolean(asDisabled)).ToString() }, { "DataPullCompleteThreshold", dataPullCompleteThreshold }, { "DataPullStatus", "-1" } }; for (int i = 0; i < configValues.Count; i++) { dynamic payload = new ExpandoObject(); payload.SqlGroup = "SolutionTemplate"; payload.SqlSubGroup = "Notifier"; payload.SqlEntryName = configValues.ElementAt(i).Key; payload.SqlEntryValue = configValues.ElementAt(i).Value; request.DataStore.AddToDataStore("NotifierValues" + i, "SqlGroup", "SolutionTemplate"); request.DataStore.AddToDataStore("NotifierValues" + i, "SqlSubGroup", "Notifier"); request.DataStore.AddToDataStore("NotifierValues" + i, "SqlEntryName", configValues.ElementAt(i).Key); request.DataStore.AddToDataStore("NotifierValues" + i, "SqlEntryValue", configValues.ElementAt(i).Value); } var configResponse = await RequestUtility.CallAction(request, "Microsoft-SetConfigValueInSql"); if (!configResponse.IsSuccess) { return(configResponse); } //OnPrem scenario if (request.Info.WebsiteRootUrl.Contains("https://msi")) { var post = PostDeploymentId(deploymentId, azureToken, refreshToken); if (!post) { request.Logger.LogEvent("ConfigureNotifier failed for on prem scenario - couldn't reach service.", new Dictionary <string, string>()); } } else { //Website scenario var cmd = $"INSERT INTO deploymentids VALUES('{deploymentId}','{DateTime.UtcNow.ToString("o")}')"; SqlUtility.InvokeSqlCommand(deploymentIdsConnection, cmd, new Dictionary <string, string>()); } AzureHttpClient azureClient = new AzureHttpClient(azureToken, subscription, resourceGroup); var response = await azureClient.ExecuteGenericRequestNoHeaderAsync(HttpMethod.Post, triggerUrl, string.Empty); return(new ActionResponse(ActionStatus.Success)); }
public override async Task <ActionResponse> ExecuteActionAsync(ActionRequest request) { string azureToken = request.DataStore.GetJson("AzureToken", "access_token"); string refreshToken = request.DataStore.GetJson("AzureToken", "refresh_token"); string subscription = request.DataStore.GetJson("SelectedSubscription", "SubscriptionId"); string resourceGroup = request.DataStore.GetValue("SelectedResourceGroup"); string connectionString = request.DataStore.GetValue("SqlConnectionString"); string sendNotification = request.DataStore.GetValue("SendCompletionNotification"); Dictionary <string, string> configValues = new Dictionary <string, string>(); if (!bool.Parse(sendNotification)) { configValues = new Dictionary <string, string>() { { "SendCompletionNotification", "0" }, }; CreatePayload(request, configValues); var resp = await RequestUtility.CallAction(request, "Microsoft-SetConfigValueInSql"); if (resp.IsSuccess) { return(resp); } } string triggerUrl = request.DataStore.GetValue("NotifierTriggerUrl"); string deploymentIdsConnection = Constants.BpstDeploymentIdDatabase; string deploymentId = Guid.NewGuid().ToString(); string dataPullCompleteThreshold = "80"; var asDisabled = request.DataStore.GetValue("ssasDisabled"); configValues = new Dictionary <string, string>() { { "SendCompletionNotification", "1" }, { "NotifierUrl", Constants.BpstNotifierUrl }, { "NotificationEmails", request.DataStore.GetValue("EmailAddress") }, { "DeploymentId", deploymentId }, { "TemplateName", request.Info.AppName }, { "DeploymentTimestamp", DateTime.UtcNow.ToString("o") }, { "ASDeployment", string.IsNullOrEmpty(asDisabled) ? "False" : (!Convert.ToBoolean(asDisabled)).ToString() }, { "DataPullCompleteThreshold", dataPullCompleteThreshold }, { "DataPullStatus", "-1" } }; CreatePayload(request, configValues); var configResponse = await RequestUtility.CallAction(request, "Microsoft-SetConfigValueInSql"); if (!configResponse.IsSuccess) { return(configResponse); } //OnPrem scenario if (request.Info.WebsiteRootUrl.Contains("https://msi")) { var post = PostDeploymentId(deploymentId, azureToken, refreshToken); if (!post) { request.Logger.LogEvent("ConfigureNotifier failed for on prem scenario - couldn't reach service.", new Dictionary <string, string>()); } } else { //Website scenario SqlParameter[] parameters = SqlUtility.MapValuesToSqlParameters(deploymentId, DateTime.UtcNow); SqlUtility.ExecuteQueryWithParameters(deploymentIdsConnection, "INSERT INTO deploymentids VALUES(@p1, @p2)", parameters); } AzureHttpClient azureClient = new AzureHttpClient(azureToken, subscription, resourceGroup); var response = await azureClient.ExecuteGenericRequestNoHeaderAsync(HttpMethod.Post, triggerUrl, string.Empty); return(new ActionResponse(ActionStatus.Success)); }