Exemplo n.º 1
0
        public override async Task <ActionResponse> ExecuteActionAsync(ActionRequest request)
        {
            AzureHttpClient ahc = new AzureHttpClient(request.DataStore.GetJson("AzureToken", "access_token"));

            List <PowerAppEnvironment> environments = await ahc.RequestValue <List <PowerAppEnvironment> >(HttpMethod.Get, PowerAppUtility.URL_POWERAPPS_ENVIRONMENTS);

            bool foundEnvironment = false;

            if (environments.IsNullOrEmpty())
            {
                PowerAppUtility.SkipPowerApp(request.DataStore);
                foundEnvironment = true;
            }
            else
            {
                for (int i = 0; i < environments.Count && !foundEnvironment; i++)
                {
                    PowerAppEnvironment environment = environments[i];
                    if (environment.Properties != null && environment.Properties.IsDefault && environment.Properties.Permissions != null && environment.Properties.Permissions.CreatePowerApp != null)
                    {
                        request.DataStore.AddToDataStore("PowerAppEnvironment", environment.Name, DataStoreType.Private);
                        foundEnvironment = true;
                    }
                }
            }

            return(foundEnvironment
                ? new ActionResponse(ActionStatus.Success)
                : new ActionResponse(ActionStatus.Failure, JsonUtility.GetEmptyJObject(), "PowerAppNoEnvironment"));
        }
Exemplo n.º 2
0
        public override async Task <ActionResponse> ExecuteActionAsync(ActionRequest request)
        {
            string powerAppEnvironmentId = request.DataStore.GetValue("PowerAppEnvironment");

            if (powerAppEnvironmentId != null)
            {
                AzureHttpClient ahc = new AzureHttpClient(request.DataStore.GetJson("AzureToken", "access_token"));

                string newSqlConnectionId  = RandomGenerator.GetRandomHexadecimal(PowerAppUtility.SQL_CONNECTION_ID_LENGTH);
                string sqlConnectionString = request.DataStore.GetValueAtIndex("SqlConnectionString", "SqlServerIndex");

                SqlCredentials sqlCredentials = SqlUtility.GetSqlCredentialsFromConnectionString(sqlConnectionString);

                PowerAppSqlConnection powerAppSqlConnection = new PowerAppSqlConnection(sqlCredentials, powerAppEnvironmentId);

                string body = JsonUtility.Serialize <PowerAppSqlConnection>(powerAppSqlConnection);
                string url  = string.Format(PowerAppUtility.URL_POWERAPPS_SQL_CONNECTION, newSqlConnectionId, powerAppEnvironmentId);

                if (await ahc.IsSuccess(HttpMethod.Put, url, body))
                {
                    request.DataStore.AddToDataStore("PowerAppSqlConnectionId", newSqlConnectionId, DataStoreType.Private);
                }
                else
                {
                    PowerAppUtility.SkipPowerApp(request.DataStore);
                }
            }

            return(new ActionResponse(ActionStatus.Success));
        }
Exemplo n.º 3
0
        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));
        }