Esempio n. 1
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("Up and Running!");
            ParameterChecker parameterChecker = new ParameterChecker(req.Query);

            if (!parameterChecker.IsValidConfig)
            {
                return(new BadRequestResult());
            }
            ModeHelper modeHelper = new ModeHelper(req.Query, req.Headers);
            IMode      mode       = modeHelper.Mode;

            log.LogInformation("Initialize Credential Helper");
            CredentialHelper credentialHelper = new CredentialHelper();
            var serviceClientCredentials      = await credentialHelper.GetAzureCredentials();

            string SubscriptionID = Environment.GetEnvironmentVariable("SubscriptionID", EnvironmentVariableTarget.Process);

            string    resourceGroupName = req.Query["ResourceGroupName"];
            DnsHelper dnsHelper         = null;

            try
            {
                dnsHelper = new DnsHelper(serviceClientCredentials, SubscriptionID, resourceGroupName, mode.Zone);
            }
            catch (Exception e)
            {
                log.LogError(e.Message, e.StackTrace);
            }

            bool recordExists = await dnsHelper.RecordExists(mode);

            RecordSet newRecordSet = null;

            if ((!recordExists) && mode.AutoCreateZone)
            {
                try
                {
                    newRecordSet = await dnsHelper.CreateZone(mode);
                }
                catch (Exception e)
                {
                    return((ActionResult) new BadRequestObjectResult(e.Message));
                }
            }

            string answer         = string.Empty;
            bool   zoneIsUpToDate = await dnsHelper.ZoneIsUpToDate(mode.Hostname, mode.Type, mode.Address);

            if (!zoneIsUpToDate)
            {
                try
                {
                    newRecordSet = await dnsHelper.UpdateZone(mode);
                }
                catch (Exception e)
                {
                    return((ActionResult) new BadRequestObjectResult(e.Message));
                }
            }

            if (newRecordSet != null)
            {
                answer = JsonConvert.SerializeObject(newRecordSet);
                return((ActionResult) new OkObjectResult($"{answer}"));
            }
            else
            {
                return((ActionResult) new OkResult());
            }
        }