public static async Task <IActionResult> DomainPatch( [HttpTrigger(AuthorizationLevel.Anonymous, "patch", Route = "_api/v1/domain/{key}")] HttpRequest req, [Table(TableNames.Domains)] CloudTable domainTable, string key, ILogger log, ExecutionContext context, ClaimsPrincipal claimsPrincipal) { if (!claimsPrincipal.Identity.IsAuthenticated) { return(new UnauthorizedResult()); } string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); dynamic entity = JsonConvert.DeserializeObject <dynamic>(requestBody); log.LogInformation($"Getting Domain row for values {claimsPrincipal.Identity.Name} and {entity.RowKey}"); List <DomainEntity> entities = await DomainEntity.get(domainTable, key); if (entities == null) { return(new BadRequestObjectResult($"Domain with {key} doesn't exist for {claimsPrincipal.Identity.Name}")); } DomainEntity[] filteredEntities = entities.Where(domain => domain.Account == claimsPrincipal.Identity.Name).ToArray(); if (filteredEntities.Length == 0) { return(new UnauthorizedResult()); } filteredEntities[0].Configured = entity.configured ??= filteredEntities[0].Configured; filteredEntities[0].SslConfigured = entity.sslConfigured ??= filteredEntities[0].SslConfigured; bool success = await DomainEntity.put(domainTable, filteredEntities[0]); if (!success) { return(new BadRequestObjectResult($"Error occurred updating domain {key} for {claimsPrincipal.Identity.Name}")); } return(new OkResult()); }
public static async Task <IActionResult> DomainPost( [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "_api/v1/domain")] HttpRequest req, [Table(TableNames.Domains)] CloudTable domainTable, ILogger log, ExecutionContext context, ClaimsPrincipal claimsPrincipal) { if (!claimsPrincipal.Identity.IsAuthenticated) { return(new UnauthorizedResult()); } string requestBody = await new StreamReader(req.Body).ReadToEndAsync(); DomainEntity entity = JsonConvert.DeserializeObject <DomainEntity>(requestBody); if (entity.RowKey == null) { return(new BadRequestObjectResult($"Please specify the RowKey in the request body")); } log.LogInformation($"Getting Domain row for values {claimsPrincipal.Identity.Name} and {entity.RowKey}"); List <DomainEntity> existingEntities = await DomainEntity.get(domainTable, entity.RowKey); if (existingEntities != null) { return(new BadRequestObjectResult($"Domain with {entity.RowKey} already exists")); } bool success = await DomainEntity.put(domainTable, entity.RowKey, claimsPrincipal.Identity.Name); if (!success) { return(new BadRequestObjectResult($"Error occurred creating {entity.RowKey} already exists for {claimsPrincipal.Identity.Name}")); } return(new OkResult()); }
public static async void ProcessRedirectsSync( [QueueTrigger(QueueNames.SynchroniseRedirects)] string node, [Table(TableNames.Redirects)] CloudTable redirectTable, [Table(TableNames.Domains)] CloudTable domainTable, [Queue(QueueNames.ProcessClicksGeo), StorageAccount("AzureWebJobsStorage")] ICollector <HttpRequestEntity> processClicksGeoQueue, ILogger log, ExecutionContext context) { var config = new ConfigurationBuilder() .SetBasePath(context.FunctionAppDirectory) .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true) .AddEnvironmentVariables() .Build(); string connectionString = config[$"NODE_SYNC_CONNECTION_{node}"]; if (connectionString == null) { throw new Exception($"No connection string found for node [{node}]. Aborting."); } CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString); CloudTableClient tableClient = storageAccount.CreateCloudTableClient(); CloudTable destinationRedirectTable = tableClient.GetTableReference(TableNames.Redirects); CloudTable destinationDomainTable = tableClient.GetTableReference(TableNames.Domains); await destinationRedirectTable.CreateIfNotExistsAsync(); await destinationDomainTable.CreateIfNotExistsAsync(); List <DomainEntity> domains = await DomainEntity.get(domainTable, null); List <string> uniqueAccounts = new List <string>(); foreach (DomainEntity domain in domains) { await DomainEntity.put(destinationDomainTable, domain); if (uniqueAccounts.FindIndex(checkAccount => checkAccount == domain.Account) == -1) { uniqueAccounts.Add(domain.Account); } } List <DomainEntity> destinationDomains = await DomainEntity.get(destinationDomainTable, null); foreach (DomainEntity destinationDomain in destinationDomains) { if (domains.FindIndex(checkDomain => checkDomain.RowKey == destinationDomain.RowKey) == -1) { await DomainEntity.delete(destinationRedirectTable, destinationDomain); } } foreach (string account in uniqueAccounts) { List <RedirectEntity> redirects = await RedirectEntity.get(redirectTable, account); foreach (RedirectEntity redirect in redirects) { await RedirectEntity.put(destinationRedirectTable, redirect); } List <RedirectEntity> destinationRedirects = await RedirectEntity.get(destinationRedirectTable, account); foreach (RedirectEntity destinationRedirect in destinationRedirects) { if (redirects.FindIndex(checkRedirect => checkRedirect.RowKey == destinationRedirect.RowKey) == -1) { await RedirectEntity.delete(destinationRedirectTable, destinationRedirect); } } } }