private async Task <BackendObject> HandleBackend(DeploymentTemplate template, string startname, string backendid)
        {
            var backendInstance = await resourceCollector.GetResource(GetAPIMResourceIDString() + "/backends/" + backendid);

            JObject azureResource = null;

            if (backendInstance["properties"]["resourceId"] != null)
            {
                string version = "2018-02-01";
                if (backendInstance["properties"].Value <string>("resourceId").Contains("Microsoft.Logic"))
                {
                    version = "2017-07-01";
                }

                azureResource = await resourceCollector.GetResource(backendInstance["properties"].Value <string>("resourceId"), "", version);
            }

            //sometime old endpoint are not cleaned-up, this will result in null. So skip these resources
            if (azureResource == null)
            {
                return(null);
            }

            var property = template.AddBackend(backendInstance, azureResource);

            if (property != null)
            {
                if (property.type == Property.PropertyType.LogicApp)
                {
                    var idp = this.identifiedProperties.Where(pp => pp.name.StartsWith(startname) && pp.name.Contains("-invoke")).FirstOrDefault();
                    if (idp != null)
                    {
                        idp.extraInfo = property.extraInfo;
                        idp.type      = Property.PropertyType.LogicAppRevisionGa;
                    }
                }
                else if (property.type == Property.PropertyType.Function)
                {
                    // old way of handling, removed 2019-11-03
                    //property.operationName = GetOperationName(startname);
                    property.operationName = startname;
                    identifiedProperties.Add(property);
                    foreach (var idp in this.identifiedProperties.Where(pp => pp.name.ToLower().StartsWith(property.name) && !pp.name.Contains("-invoke")))
                    {
                        idp.extraInfo = property.extraInfo;
                        idp.type      = Property.PropertyType.Function;
                    }
                }
            }

            return(new BackendObject()
            {
                backendInstance = backendInstance, backendProperty = property
            });
        }
Esempio n. 2
0
        public void ScenarioTestLogicAppUpdatedBackend()
        {
            var collector = new MockResourceCollector("UpdatedeLogicApp");
            var dtemplate = new DeploymentTemplate();

            var document = JObject.Parse(Utils.GetEmbededFileContent("APIManagementTemplate.Test.Samples.UpdatedeLogicApp.service-cramoapidev-backends-LogicApp_INT3502-PricelistErrorFileToSharePoint-DEV.json"));

            dtemplate.AddBackend(document, JObject.Parse("{\"properties\":{\"definition\": {\"triggers\": {\"manual\": {\"type\": \"Request\",\"kind\": \"Http\"}}}}}"));

            Assert.AreEqual("[substring(listCallbackUrl(resourceId(parameters('LogicApp_INT3502-PricelistErrorFileToSharePoint-DEV_resourceGroup'), 'Microsoft.Logic/workflows/triggers', parameters('LogicApp_INT3502-PricelistErrorFileToSharePoint-DEV_logicAppName'), 'manual'), '2017-07-01').basePath,0,add(10,indexOf(listCallbackUrl(resourceId(parameters('LogicApp_INT3502-PricelistErrorFileToSharePoint-DEV_resourceGroup'), 'Microsoft.Logic/workflows/triggers', parameters('LogicApp_INT3502-PricelistErrorFileToSharePoint-DEV_logicAppName'), 'manual'), '2017-07-01').basePath,'/triggers/')))]", dtemplate.resources[0]["properties"].Value <string>("url"));
            Assert.AreEqual("[concat('https://management.azure.com/','subscriptions/',subscription().subscriptionId,'/resourceGroups/',parameters('LogicApp_INT3502-PricelistErrorFileToSharePoint-DEV_resourceGroup'),'/providers/Microsoft.Logic/workflows/',parameters('LogicApp_INT3502-PricelistErrorFileToSharePoint-DEV_logicAppName'))]", dtemplate.resources[0]["properties"].Value <string>("resourceId"));
            var result = dtemplate.ToString();

            //Assert.AreEqual("other", oparation["properties"]["templateParameters"][1].Value<string>("name"));
        }
Esempio n. 3
0
        private async Task <JObject> HandleBackend(DeploymentTemplate template, string startname, string backendid)
        {
            var backendInstance = await resourceCollector.GetResource(GetAPIMResourceIDString() + "/backends/" + backendid);

            JObject azureResource = null;

            if (backendInstance["properties"]["resourceId"] != null)
            {
                string version = "2018-02-01";
                if (backendInstance["properties"].Value <string>("resourceId").Contains("Microsoft.Logic"))
                {
                    version = "2017-07-01";
                }

                azureResource = await resourceCollector.GetResource(backendInstance["properties"].Value <string>("resourceId"), "", version);
            }

            var property = template.AddBackend(backendInstance, azureResource);

            if (property != null)
            {
                if (property.type == Property.PropertyType.LogicApp)
                {
                    var idp = this.identifiedProperties.Where(pp => pp.name.StartsWith(startname) && pp.name.Contains("-invoke")).FirstOrDefault();
                    if (idp != null)
                    {
                        idp.extraInfo = property.extraInfo;
                        idp.type      = Property.PropertyType.LogicAppRevisionGa;
                    }
                }
                else if (property.type == Property.PropertyType.Function)
                {
                    property.operationName = GetOperationName(startname);
                    identifiedProperties.Add(property);
                    foreach (var idp in this.identifiedProperties.Where(pp => pp.name.ToLower().StartsWith(property.name) && !pp.name.Contains("-invoke")))
                    {
                        idp.extraInfo = property.extraInfo;
                        idp.type      = Property.PropertyType.Function;
                    }
                }
            }

            return(backendInstance);
        }
Esempio n. 4
0
        public async Task <JObject> GenerateTemplate()
        {
            DeploymentTemplate template = new DeploymentTemplate(this.parametrizePropertiesOnly);

            if (exportPIManagementInstance)
            {
                var apim = await resourceCollector.GetResource(GetAPIMResourceIDString());

                template.AddAPIManagementInstance(apim);
            }

            var apis = await resourceCollector.GetResource(GetAPIMResourceIDString() + "/apis", (string.IsNullOrEmpty(apiFilters) ? "" : $"$filter={apiFilters}"));

            foreach (JObject apiObject in (apis == null ? new JArray() : apis.Value <JArray>("value")))
            {
                var id          = apiObject.Value <string>("id");
                var apiInstance = await resourceCollector.GetResource(id);

                var apiTemplateResource = template.AddApi(apiInstance);

                var operations = await resourceCollector.GetResource(id + "/operations");

                foreach (JObject operation in (operations == null ? new JArray() : operations.Value <JArray>("value")))
                {
                    var opId = operation.Value <string>("id");

                    var operationInstance = await resourceCollector.GetResource(opId);

                    var operationTemplateResource = template.CreateOperation(operationInstance);
                    apiTemplateResource.Value <JArray>("resources").Add(operationTemplateResource);


                    var operationPolicies = await resourceCollector.GetResource(opId + "/policies");

                    foreach (JObject policy in (operationPolicies == null ? new JArray() : operationPolicies.Value <JArray>("value")))
                    {
                        var pol = template.CreatePolicy(policy);
                        //Handle Azure Resources
                        this.PolicyHandeAzureResources(pol, apiTemplateResource.Value <string>("name"), template);
                        this.PolicyHandleProperties(pol, apiTemplateResource.Value <string>("name"));
                        operationTemplateResource.Value <JArray>("resources").Add(pol);
                        //handle nextlink?
                    }
                    //handle nextlink?
                }

                var apiPolicies = await resourceCollector.GetResource(id + "/policies");

                foreach (JObject policy in (apiPolicies == null ? new JArray() : apiPolicies.Value <JArray>("value")))
                {
                    var policyTemplateResource = template.CreatePolicy(policy);
                    this.PolicyHandleProperties(policy, apiTemplateResource.Value <string>("name"));
                    apiTemplateResource.Value <JArray>("resources").Add(policyTemplateResource);


                    //Handle SOAP Backend
                    var backendid = TemplateHelper.GetBackendIdFromnPolicy(policy["properties"].Value <string>("policyContent"));

                    if (!string.IsNullOrEmpty(backendid))
                    {
                        var backendInstance = await resourceCollector.GetResource(GetAPIMResourceIDString() + "/backends/" + backendid);

                        template.AddBackend(backendInstance);

                        if (apiTemplateResource.Value <JArray>("dependsOn") == null)
                        {
                            apiTemplateResource["dependsOn"] = new JArray();
                        }

                        //add dependeOn
                        apiTemplateResource.Value <JArray>("dependsOn").Add($"[resourceId('Microsoft.ApiManagement/service/backends', parameters('service_{servicename}_name'), parameters('backend_{backendInstance.Value<string>("name")}_name'))]");
                    }

                    //handle nextlink?
                }

                //handle nextlink?
            }

            // Export all groups if we don't export the products.
            if (exportGroups && !exportProducts)
            {
                var groups = await resourceCollector.GetResource(GetAPIMResourceIDString() + "/groups");

                foreach (JObject groupObject in (groups == null ? new JArray() : groups.Value <JArray>("value")))
                {
                    //cannot edit och create built in groups
                    if (groupObject["properties"].Value <bool>("builtIn") == false)
                    {
                        template.AddGroup(groupObject);
                    }
                }
            }

            if (exportProducts)
            {
                var products = await resourceCollector.GetResource(GetAPIMResourceIDString() + "/products");

                foreach (JObject productObject in (products == null ? new JArray() : products.Value <JArray>("value")))
                {
                    var id = productObject.Value <string>("id");
                    var productInstance = await resourceCollector.GetResource(id);

                    var productApis = await resourceCollector.GetResource(id + "/apis", (string.IsNullOrEmpty(apiFilters) ? "" : $"$filter={apiFilters}"));

                    // Skip product if not related to an API in the filter.
                    if (productApis != null && productApis.Value <JArray>("value").Count > 0)
                    {
                        var productTemplateResource = template.AddProduct(productObject);

                        foreach (JObject productApi in (productApis == null ? new JArray() : productApis.Value <JArray>("value")))
                        {
                            productTemplateResource.Value <JArray>("resources").Add(template.AddProductSubObject(productApi));
                        }

                        var groups = await resourceCollector.GetResource(id + "/groups");

                        foreach (JObject group in (groups == null ? new JArray() : groups.Value <JArray>("value")))
                        {
                            if (group["properties"].Value <bool>("builtIn") == false)
                            {
                                // Add group resource
                                var groupObject = await resourceCollector.GetResource(GetAPIMResourceIDString() + "/groups/" + group.Value <string>("name"));

                                template.AddGroup(groupObject);
                                productTemplateResource.Value <JArray>("resources").Add(template.AddProductSubObject(group));
                                productTemplateResource.Value <JArray>("dependsOn").Add($"[resourceId('Microsoft.ApiManagement/service/groups', parameters('service_{servicename}_name'), '{group.Value<string>("name")}')]");
                            }
                        }
                    }
                }
            }

            var properties = await resourceCollector.GetResource(GetAPIMResourceIDString() + "/properties");

            foreach (JObject propertyObject in (properties == null ? new JArray() : properties.Value <JArray>("value")))
            {
                var id   = propertyObject.Value <string>("id");
                var name = propertyObject["properties"].Value <string>("displayName");

                var identifiedProperty = this.identifiedProperties.Where(idp => name.EndsWith(idp.name)).FirstOrDefault();
                if (identifiedProperty != null)
                {
                    if (identifiedProperty.type == Property.PropertyType.LogicApp)
                    {
                        propertyObject["properties"]["value"] = $"[concat('sv=',{identifiedProperty.extraInfo}.queries.sv,'&sig=',{identifiedProperty.extraInfo}.queries.sig)]";
                    }
                    template.AddProperty(propertyObject);

                    if (!parametrizePropertiesOnly)
                    {
                        foreach (var apiName in identifiedProperty.apis)
                        {
                            var apiTemplate = template.resources.Where(rr => rr.Value <string>("name") == apiName).FirstOrDefault();
                            apiTemplate.Value <JArray>("dependsOn").Add($"[resourceId('Microsoft.ApiManagement/service/properties', parameters('service_{servicename}_name'),parameters('property_{propertyObject.Value<string>("name")}_name'))]");
                        }
                    }
                }
            }

            return(JObject.FromObject(template));
        }