public async Task <HttpResponseMessage> CreateResource(BaseTemplate template)
        {
            var tempTemplate = WebsiteTemplate.EmptySiteTemplate;

            if (template == null)
            {
                template = WebsiteTemplate.EmptySiteTemplate;
            }
            else if (template.AppService.Equals(AppService.Function))
            {
                tempTemplate = FunctionTemplate.DefaultFunctionTemplate(template.Name);
            }
            else if (template.AppService.Equals(AppService.Containers))
            {
                var containersTemplate = ContainersTemplate.GetContainersTemplate(template.Name);
                containersTemplate.DockerContainer = template.DockerContainer;
                tempTemplate = containersTemplate;
            }
            else if (template.Name != null && !template.Name.Equals("Github Repo") && !template.AppService.Equals(AppService.Function))
            {
                var temp = TemplatesManager.GetTemplates()
                           .FirstOrDefault(t => t.Name == template.Name);

                tempTemplate = WebsiteTemplate.DefaultTemplate(temp.Name, temp.AppService, temp.Language, temp.FileName, template.DockerContainer, temp.MSDeployPackageUrl);
            }
            else if (template.Name != null && template.Name.Equals("Github Repo"))
            {
                tempTemplate = new WebsiteTemplate
                {
                    AppService = AppService.Web,
                    GithubRepo = template.GithubRepo,
                    Name       = template.Name,
                    Language   = "Github"
                };
            }

            var identity          = HttpContext.Current.User.Identity as TryWebsitesIdentity;
            var anonymousUserName = SecurityManager.GetAnonymousUserName(new HttpContextWrapper(HttpContext.Current));

            try
            {
                var resourceManager = await ResourcesManager.GetInstanceAsync();

                if ((await resourceManager.GetResourceGroup(identity.Name)) != null)
                {
                    SimpleTrace.Diagnostics.Fatal(AnalyticsEvents.MoreThanOneError, 1);
                    //This should use the server version of the error, but due to a string bug they are not the same.
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, Resources.Client.Information_YouCantHaveMoreThanOne));
                }

                ResourceGroup resourceGroup = null;

                switch (tempTemplate.AppService)
                {
                case AppService.Linux:
                    resourceGroup = await resourceManager.ActivateLinuxResource(tempTemplate, identity, anonymousUserName);

                    break;

                case AppService.VSCodeLinux:
                    resourceGroup = await resourceManager.ActivateVSCodeLinuxResource(tempTemplate, identity, anonymousUserName);

                    break;

                case AppService.Web:
                    resourceGroup = await resourceManager.ActivateWebApp(tempTemplate, identity, anonymousUserName);

                    break;

                case AppService.Api:
                    resourceGroup = await resourceManager.ActivateApiApp(tempTemplate, identity, anonymousUserName);

                    break;

                case AppService.Logic:
                    if (identity.Issuer == "OrgId")
                    {
                        return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, Resources.Server.Error_OrgIdNotSupported));
                    }
                    else if (identity.Issuer != "MSA")
                    {
                        return(SecurityManager.RedirectToAAD(tempTemplate.CreateQueryString()));
                    }
                    resourceGroup = await resourceManager.ActivateLogicApp(tempTemplate, identity, anonymousUserName);

                    break;

                case AppService.Function:
                    resourceGroup = await resourceManager.ActivateFunctionApp(tempTemplate, identity, anonymousUserName);

                    break;

                case AppService.Containers:
                    if (identity.Issuer == "OrgId")
                    {
                        return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, Resources.Server.Error_OrgIdNotSupported));
                    }
                    else if (identity.Issuer != "MSA")
                    {
                        return(SecurityManager.RedirectToAAD(template.CreateQueryString()));
                    }
                    resourceGroup = await resourceManager.ActivateContainersResource(tempTemplate as ContainersTemplate, identity, anonymousUserName);

                    break;

                case AppService.MonitoringTools:
                    if (identity.Issuer == "OrgId")
                    {
                        return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, Resources.Server.Error_OrgIdNotSupported));
                    }
                    else if (identity.Issuer != "MSA")
                    {
                        return(SecurityManager.RedirectToAAD(template.CreateQueryString()));
                    }
                    resourceGroup = await resourceManager.ActivateMonitoringToolsApp(tempTemplate as MonitoringToolsTemplate, identity, anonymousUserName);

                    break;
                }
                return(Request.CreateResponse(HttpStatusCode.OK, resourceGroup == null ? null : GetUIResource(resourceGroup)));
            }
            catch (Exception ex)
            {
                var message = ex is NullReferenceException ? Resources.Server.Error_GeneralErrorMessage : ex.Message;
                SimpleTrace.Diagnostics.Fatal(ex, AnalyticsEvents.UserGotError, message, Interlocked.Increment(ref _userGotErrorErrorCount));
                return(Request.CreateErrorResponse(HttpStatusCode.ServiceUnavailable, message));
            }
        }
Ejemplo n.º 2
0
        public async Task <HttpResponseMessage> CreateResource(BaseTemplate template)
        {
            try
            {
                var payload = JsonConvert.SerializeObject(template);
                SimpleTrace.TraceInformation($"CREATE {template?.AppService} : {payload}. Request Received");
            }
            catch (Exception ex)
            {
                SimpleTrace.TraceError($"Error Logging Create Start Information: {ex.Message} -> {ex.StackTrace}");
            }
            var tempTemplate = WebsiteTemplate.EmptySiteTemplate;

            if (template == null)
            {
                template = WebsiteTemplate.EmptySiteTemplate;
            }
            else if (template.AppService.Equals(AppService.Function))
            {
                tempTemplate = FunctionTemplate.DefaultFunctionTemplate(template.Name);
            }
            else if (template.AppService.Equals(AppService.Containers))
            {
                var containersTemplate = ContainersTemplate.DefaultContainersTemplate(template.Name);
                containersTemplate.DockerContainer = template.DockerContainer;
                tempTemplate = containersTemplate;
            }
            else if (template.Name != null && !template.Name.Equals("GitHub Repo") && !template.AppService.Equals(AppService.Function))
            {
                var temp = TemplatesManager.GetTemplates()
                           .FirstOrDefault(t => t.Name == template.Name);

                tempTemplate = WebsiteTemplate.DefaultTemplate(temp.Name, temp.AppService, temp.Language, temp.FileName, template.DockerContainer, temp.MSDeployPackageUrl);
            }
            else if (template.Name != null && template.Name.Equals("GitHub Repo"))
            {
                tempTemplate = new WebsiteTemplate
                {
                    AppService = AppService.Web,
                    GithubRepo = template.GithubRepo,
                    Name       = template.Name,
                    Language   = "GitHub"
                };
            }

            var identity          = HttpContext.Current.User.Identity as TryWebsitesIdentity;
            var anonymousUserName = SecurityManager.GetAnonymousUserName(new HttpContextWrapper(HttpContext.Current));

            try
            {
                var resourceManager = await ResourcesManager.GetInstanceAsync();

                if ((await resourceManager.GetResourceGroup(identity.Name)) != null)
                {
                    SimpleTrace.Diagnostics.Fatal(AnalyticsEvents.MoreThanOneError, 1);
                    //This should use the server version of the error, but due to a string bug they are not the same.
                    return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, Resources.Client.Information_YouCantHaveMoreThanOne));
                }

                ResourceGroup resourceGroup = null;
                SimpleTrace.TraceInformation($"Searching for {tempTemplate?.AppService} {tempTemplate?.Name}.");
                switch (tempTemplate.AppService)
                {
                case AppService.Linux:
                    resourceGroup = await resourceManager.ActivateLinuxResource(tempTemplate, identity, anonymousUserName);

                    break;

                case AppService.VSCodeLinux:
                    resourceGroup = await resourceManager.ActivateVSCodeLinuxResource(tempTemplate, identity, anonymousUserName);

                    break;

                case AppService.Web:
                    resourceGroup = await resourceManager.ActivateWebApp(tempTemplate, identity, anonymousUserName);

                    break;

                case AppService.Api:
                    resourceGroup = await resourceManager.ActivateApiApp(tempTemplate, identity, anonymousUserName);

                    break;

                case AppService.Function:
                    resourceGroup = await resourceManager.ActivateFunctionApp(tempTemplate, identity, anonymousUserName);

                    break;

                case AppService.Containers:
                    resourceGroup = await resourceManager.ActivateContainersResource(tempTemplate, identity, anonymousUserName);

                    break;
                }
                try
                {
                    SimpleTrace.TraceInformation($"CREATE {template?.AppService}. Returning { GetUIResource(resourceGroup).SiteName } with template { GetUIResource(resourceGroup).TemplateName } for user {identity.FilteredName}");
                }
                catch (Exception ex)
                {
                    SimpleTrace.TraceError($"Error Logging Create End Information: {ex.Message} -> {ex.StackTrace}");
                }
                return(Request.CreateResponse(HttpStatusCode.OK, resourceGroup == null ? null : GetUIResource(resourceGroup)));
            }
            catch (Exception ex)
            {
                var message = ex is NullReferenceException ? Resources.Server.Error_GeneralErrorMessage : ex.Message;
                SimpleTrace.Diagnostics.Fatal(ex, AnalyticsEvents.UserGotError, message, Interlocked.Increment(ref _userGotErrorErrorCount));
                return(Request.CreateErrorResponse(HttpStatusCode.ServiceUnavailable, message));
            }
        }