public JObject generateConnectionTemplate(JObject connectionResource, JObject connectionInstance, string apiId)
        {
            //create template
            string connectionName     = (string)connectionInstance["name"];
            var    connectionTemplate = new Models.ConnectionTemplate(AddTemplateParameter($"{connectionName}_name", "string", (string)connectionInstance["name"]), apiId);

            //displayName
            connectionTemplate.properties.displayName = $"[parameters('{AddTemplateParameter(connectionName + "_displayName", "string", (string)connectionInstance["properties"]["displayName"])}')]";
            JObject connectionParameters = new JObject();

            bool useGateway = connectionInstance["properties"]["nonSecretParameterValues"]["gateway"] != null;


            //add all parameters

            foreach (JProperty parameter in connectionResource["properties"]["connectionParameters"])
            {
                if ((string)(parameter.Value)["type"] != "oauthSetting")
                {
                    //we are not handling parameter gatewaySetting
                    if ((string)parameter.Value["type"] == "gatewaySetting")
                    {
                        continue;
                    }
                    if (parameter.Value["uiDefinition"]["constraints"]["capability"] != null)
                    {
                        var match = parameter.Value["uiDefinition"]["constraints"]["capability"].FirstOrDefault(cc => (string)cc == "gateway" && useGateway || (string)cc == "cloud" && !useGateway);
                        if (match == null)
                        {
                            continue;
                        }
                    }


                    if (parameter.Name == "accessKey" && apiId.EndsWith("azureblob"))
                    {
                        connectionParameters.Add(parameter.Name, $"[listKeys(resourceId('Microsoft.Storage/storageAccounts', parameters('{connectionName}_accountName')), providers('Microsoft.Storage', 'storageAccounts').apiVersions[0]).keys[0].value]");
                    }
                    else
                    {
                        var currentvalue = (string)connectionInstance["properties"]["nonSecretParameterValues"][parameter.Name] ?? "";
                        var addedparam   = AddTemplateParameter($"{connectionName}_{parameter.Name}", (string)(parameter.Value)["type"], currentvalue);
                        connectionParameters.Add(parameter.Name, $"[parameters('{addedparam}')]");

                        //If has an enum
                        if (parameter.Value["allowedValues"] != null)
                        {
                            var array = new JArray();
                            foreach (var allowedValue in parameter.Value["allowedValues"])
                            {
                                array.Add(allowedValue["value"]);
                            }
                            template.parameters[addedparam]["allowedValues"] = array;
                            if (parameter.Value["allowedValues"].Count() == 1)
                            {
                                template.parameters[addedparam]["defaultValue"] = parameter.Value["allowedValues"][0]["value"];
                            }
                        }

                        if (parameter.Value["uiDefinition"]["description"] != null)
                        {
                            //add meta data
                            template.parameters[addedparam]["metadata"] = new JObject();
                            template.parameters[addedparam]["metadata"]["description"] = parameter.Value["uiDefinition"]["description"];
                        }
                    }
                }
            }

            if (useGateway)
            {
                var currentvalue = (string)connectionInstance["properties"]["nonSecretParameterValues"]["gateway"]["id"];
                ///subscriptions/c107df29-a4af-4bc9-a733-f88f0eaa4296/resourceGroups/OnPremDataGateway/providers/Microsoft.Web/connectionGateways/Malos-LogicApp2015
                Regex rgx     = new Regex(@"\/subscriptions\/(?<subscription>[0-9a-zA-Z-]*)\/resourceGroups\/(?<resourcegroup>[a-zA-Z0-9-]*)\/providers\/Microsoft.Web\/connectionGateways\/(?<gatewayname>[a-zA-Z0-9-]*)");
                var   matches = rgx.Match(currentvalue);

                var gatewayname   = AddTemplateParameter($"{connectionName}_gatewayname", "string", matches.Groups["gatewayname"].Value);
                var resourcegroup = AddTemplateParameter($"{connectionName}_gatewayresourcegroup", "string", matches.Groups["resourcegroup"].Value);
                var gatewayobject = new JObject();
                gatewayobject["id"] = $"[concat('subscriptions/',subscription().subscriptionId,'/resourceGroups/',parameters('{resourcegroup}'),'/providers/Microsoft.Web/connectionGateways/',parameters('{gatewayname}'))]";
                connectionParameters.Add("gateway", gatewayobject);
                useGateway = true;
            }

            connectionTemplate.properties.parameterValues = connectionParameters;
            return(JObject.FromObject(connectionTemplate));
        }
        public JObject generateConnectionTemplate(JObject connectionResource, JObject connectionInstance, string connectionName, string concatedId, string connectionNameParam)
        {
            //create template
            var connectionTemplate = new Models.ConnectionTemplate(connectionNameParam, concatedId);

            //displayName
            connectionTemplate.properties.displayName = $"[parameters('{AddTemplateParameter(connectionName+ "_displayName", "string", (string)connectionInstance["properties"]["displayName"])}')]";
            JObject connectionParameters = new JObject();

            bool useGateway = connectionInstance["properties"]["nonSecretParameterValues"]["gateway"] != null;


            //add all parameters
            if (connectionResource["properties"]["connectionParameters"] != null)
            {
                foreach (JProperty parameter in connectionResource["properties"]["connectionParameters"])
                {
                    if ((string)(parameter.Value)["type"] != "oauthSetting")
                    {
                        //we are not handling parameter gatewaySetting
                        if ((string)parameter.Value["type"] == "gatewaySetting")
                        {
                            continue;
                        }
                        if (parameter.Value["uiDefinition"]["constraints"]["capability"] != null)
                        {
                            var match = parameter.Value["uiDefinition"]["constraints"]["capability"].FirstOrDefault(cc => (string)cc == "gateway" && useGateway || (string)cc == "cloud" && !useGateway);
                            if (match == null)
                            {
                                continue;
                            }
                        }


                        if (parameter.Name == "accessKey" && concatedId.EndsWith("/azureblob')]"))
                        {
                            connectionParameters.Add(parameter.Name, $"[listKeys(resourceId('Microsoft.Storage/storageAccounts', parameters('{connectionName}_accountName')), providers('Microsoft.Storage', 'storageAccounts').apiVersions[0]).keys[0].value]");
                        }
                        else
                        {
                            //todo check this!
                            var addedparam = AddTemplateParameter($"{connectionName}_{parameter.Name}", (string)(parameter.Value)["type"], connectionInstance["properties"]["nonSecretParameterValues"][parameter.Name]);
                            connectionParameters.Add(parameter.Name, $"[parameters('{addedparam}')]");

                            //If has an enum
                            if (parameter.Value["allowedValues"] != null)
                            {
                                var array = new JArray();
                                foreach (var allowedValue in parameter.Value["allowedValues"])
                                {
                                    array.Add(allowedValue["value"]);
                                }
                                template.parameters[addedparam]["allowedValues"] = array;
                                if (parameter.Value["allowedValues"].Count() == 1)
                                {
                                    template.parameters[addedparam]["defaultValue"] = parameter.Value["allowedValues"][0]["value"];
                                }
                            }

                            if (parameter.Value["uiDefinition"]["description"] != null)
                            {
                                //add meta data
                                template.parameters[addedparam]["metadata"] = new JObject();
                                template.parameters[addedparam]["metadata"]["description"] = parameter.Value["uiDefinition"]["description"];
                            }
                        }
                    }
                }
            }

            if (useGateway)
            {
                var currentvalue  = (string)connectionInstance["properties"]["nonSecretParameterValues"]["gateway"]["id"];
                var rid           = new AzureResourceId(currentvalue);
                var gatewayname   = AddTemplateParameter($"{connectionName}_gatewayname", "string", rid.ResourceName);
                var resourcegroup = AddTemplateParameter($"{connectionName}_gatewayresourcegroup", "string", rid.ResourceGroupName);

                var gatewayobject = new JObject();
                gatewayobject["id"] = $"[concat('subscriptions/',subscription().subscriptionId,'/resourceGroups/',parameters('{resourcegroup}'),'/providers/Microsoft.Web/connectionGateways/',parameters('{gatewayname}'))]";
                connectionParameters.Add("gateway", gatewayobject);
                useGateway = true;
            }

            connectionTemplate.properties.parameterValues = connectionParameters;
            return(JObject.FromObject(connectionTemplate));
        }
Exemplo n.º 3
0
        public JObject generateConnectionTemplate(JObject connectionResource, JObject connectionInstance, string connectionName, string concatedId, string connectionNameParam)
        {
            //create template
            var connectionTemplate = new Models.ConnectionTemplate(connectionNameParam, concatedId);

            //displayName
            connectionTemplate.properties.displayName = $"[parameters('{AddTemplateParameter(connectionName + "_displayName", "string", (string)connectionInstance["properties"]["displayName"])}')]";
            JObject connectionParameters = new JObject();

            bool useGateway = connectionInstance["properties"]["nonSecretParameterValues"]["gateway"] != null;


            //add all parameters
            if (connectionResource["properties"]["connectionParameters"] != null)
            {
                foreach (JProperty parameter in connectionResource["properties"]["connectionParameters"])
                {
                    if ((string)(parameter.Value)["type"] != "oauthSetting")
                    {
                        //we are not handling parameter gatewaySetting
                        if ((string)parameter.Value["type"] == "gatewaySetting")
                        {
                            continue;
                        }
                        if (parameter.Value["uiDefinition"]["constraints"]["capability"] != null)
                        {
                            var match = parameter.Value["uiDefinition"]["constraints"]["capability"].FirstOrDefault(cc => (string)cc == "gateway" && useGateway || (string)cc == "cloud" && !useGateway);
                            if (match == null)
                            {
                                continue;
                            }
                        }


                        if ((parameter.Name == "accessKey" && concatedId.EndsWith("/azureblob')]")) || parameter.Name == "sharedkey" && concatedId.EndsWith("/azuretables')]"))
                        {
                            connectionParameters.Add(parameter.Name, $"[listKeys(resourceId('Microsoft.Storage/storageAccounts', parameters('{connectionName}_accountName')), '2018-02-01').keys[0].value]");
                        }
                        else if (parameter.Name == "sharedkey" && concatedId.EndsWith("/azurequeues')]"))
                        {
                            connectionParameters.Add(parameter.Name, $"[listKeys(resourceId('Microsoft.Storage/storageAccounts', parameters('{connectionName}_storageaccount')), '2018-02-01').keys[0].value]");
                        }
                        else if (concatedId.EndsWith("/azureeventgridpublish')]"))
                        {
                            var url      = connectionInstance["properties"]["nonSecretParameterValues"].Value <string>("endpoint");
                            var location = connectionInstance.Value <string>("location");
                            url = url.Replace("https://", "");
                            var site = url.Substring(0, url.LastIndexOf("." + location));

                            var param = AddTemplateParameter($"{connectionInstance.Value<string>("name")}_instancename", "string", site);

                            if (parameter.Name == "endpoint")
                            {
                                connectionParameters.Add(parameter.Name, $"[reference(concat('Microsoft.EventGrid/topics/',parameters('{param}')),'2018-01-01').endpoint]");
                            }
                            else if (parameter.Name == "api_key")
                            {
                                connectionParameters.Add(parameter.Name, $"[listKeys(resourceId('Microsoft.EventGrid/topics',parameters('{param}')),'2018-01-01').key1]");
                            }
                        }
                        else
                        {
                            //todo check this!
                            var addedparam = AddTemplateParameter($"{connectionName}_{parameter.Name}", (string)(parameter.Value)["type"], connectionInstance["properties"]["nonSecretParameterValues"][parameter.Name]);
                            connectionParameters.Add(parameter.Name, $"[parameters('{addedparam}')]");

                            //If has an enum
                            if (parameter.Value["allowedValues"] != null)
                            {
                                var array = new JArray();
                                foreach (var allowedValue in parameter.Value["allowedValues"])
                                {
                                    array.Add(allowedValue["value"].Value <string>().Replace("none", "anonymous"));
                                }
                                template.parameters[addedparam]["allowedValues"] = array;
                                if (parameter.Value["allowedValues"].Count() == 1)
                                {
                                    template.parameters[addedparam]["defaultValue"] = parameter.Value["allowedValues"][0]["value"].Value <string>().Replace("none", "anonymous");
                                }
                            }

                            if (parameter.Value["uiDefinition"]["description"] != null)
                            {
                                //add meta data
                                template.parameters[addedparam]["metadata"] = new JObject();
                                template.parameters[addedparam]["metadata"]["description"] = parameter.Value["uiDefinition"]["description"];
                            }
                        }
                    }
                }
            }

            if (useGateway)
            {
                var currentvalue  = (string)connectionInstance["properties"]["nonSecretParameterValues"]["gateway"]["id"];
                var rid           = new AzureResourceId(currentvalue);
                var gatewayname   = AddTemplateParameter($"{connectionName}_gatewayname", "string", rid.ResourceName);
                var resourcegroup = AddTemplateParameter($"{connectionName}_gatewayresourcegroup", "string", rid.ResourceGroupName);

                var gatewayobject = new JObject();
                gatewayobject["id"] = $"[concat('/subscriptions/',subscription().subscriptionId,'/resourceGroups/',parameters('{resourcegroup}'),'/providers/Microsoft.Web/connectionGateways/',parameters('{gatewayname}'))]";
                connectionParameters.Add("gateway", gatewayobject);
                useGateway = true;
            }
            //only fill connectionParameters when source not empty, otherwise saved credentials will be lost.
            if (connectionParameters.HasValues)
            {
                connectionTemplate.properties.parameterValues = connectionParameters;
            }

            return(JObject.FromObject(connectionTemplate));
        }