Exemplo n.º 1
0
        private async Task <bool> ApplyDnsChange(HostedZone zone, ResourceRecordSet recordSet, ChangeAction action)
        {
            // prepare change
            var changeDetails = new Change()
            {
                ResourceRecordSet = recordSet,
                Action            = action
            };

            var changeBatch = new ChangeBatch()
            {
                Changes = new List <Change> {
                    changeDetails
                }
            };

            // Update the zone's resource record sets
            var recordsetRequest = new ChangeResourceRecordSetsRequest()
            {
                HostedZoneId = zone.Id,
                ChangeBatch  = changeBatch
            };

            _log?.Debug($"Route53 :: ApplyDnsChange : ChangeResourceRecordSetsAsync: {JsonConvert.SerializeObject(recordsetRequest.ChangeBatch)} ");

            var recordsetResponse = await _route53Client.ChangeResourceRecordSetsAsync(recordsetRequest);

            _log?.Debug($"Route53 :: ApplyDnsChange : ChangeResourceRecordSetsAsync Response: {JsonConvert.SerializeObject(recordsetResponse)} ");

            // Monitor the change status
            var changeRequest = new GetChangeRequest()
            {
                Id = recordsetResponse.ChangeInfo.Id
            };

            while (ChangeStatus.PENDING == (await _route53Client.GetChangeAsync(changeRequest)).ChangeInfo.Status)
            {
                System.Diagnostics.Debug.WriteLine("DNS change is pending.");
                await Task.Delay(1500);
            }

            _log?.Information("DNS change completed.");

            return(true);
        }
Exemplo n.º 2
0
        private async Task <bool> WaitForChange(AmazonRoute53Client client, string id, int interval = 10000, int maxIterations = 60)
        {
            bool pending = true;
            bool timeout = (maxIterations > 0);
            int  count   = 0;

            while (pending && !timeout)
            {
                //Check Status
                GetChangeResponse response = await client.GetChangeAsync(new GetChangeRequest(id));

                if ((response != null) && (response.HttpStatusCode == HttpStatusCode.OK))
                {
                    if (ChangeStatus.INSYNC == response.ChangeInfo.Status)
                    {
                        pending = false;
                    }
                }
                else
                {
                    timeout = true;
                }



                //Sleep
                if (count < maxIterations)
                {
                    Console.WriteLine("Change is pending...");
                    Thread.Sleep(interval);
                }
                else
                {
                    timeout = true;
                }

                count++;
            }

            return(timeout);
        }
        public void SaveClients()
        {
            //Output
            Output($"MCS: Starting Save Clients");

            string dataText = "";

            //Validate
            if (Clients == null || !Clients.Any())
            {
                //Output
                Output($"MCS: Had no Clients.");

                //Will update with an empty string - no clients
            }
            else
            {
                //Output
                Output($"MCS: Found {Clients.Count} Clients.");

                //1) Serialize to JSON text
                dataText = JsonConvert.SerializeObject(Clients);

                //2) Prepare the string (encrypt, compress)
                //Encrypt and Compress
                dataText = dataText.CompressThenEncrypt(Config.MultiClientSettings.Route53.EncryptionPassword);
            }

            //Add quotation marks
            dataText = "\"" + dataText + "\"";

            //3) Update to R53
            //New the Route 53 Client
            AmazonRoute53Client r53Client =
                new AmazonRoute53Client(
                    Config.BaseSettings.AWSAccessKeyID,
                    Config.BaseSettings.AWSSecretAccessKey,
                    new AmazonRoute53Config
            {
                RegionEndpoint = RegionEndpoint
            });

            //New the Change Record to push
            Change change =
                new Change
            {
                Action            = ChangeAction.UPSERT, //Insert or Update
                ResourceRecordSet =
                    new ResourceRecordSet
                {
                    Name            = Config.MultiClientSettings.Route53.Name,
                    TTL             = 300,
                    Type            = RRType.TXT,
                    ResourceRecords = new List <ResourceRecord> {
                        new ResourceRecord(dataText)
                    }
                }
            };

            //New the Change Request
            ChangeResourceRecordSetsRequest recordSetsRequest =
                new ChangeResourceRecordSetsRequest
            {
                HostedZoneId = Config.MultiClientSettings.Route53.R53ZoneId,
                ChangeBatch  = new ChangeBatch(new List <Change> {
                    change
                })
            };

            //Submitting the Change Request to the API and receiving back the ID
            ChangeResourceRecordSetsResponse recordSetResponse =
                r53Client
                .ChangeResourceRecordSetsAsync(recordSetsRequest)
                .GetAwaiter()
                .GetResult();

            //The ID of the response
            string changeId = recordSetResponse.ChangeInfo.Id;

            //Output
            Output($"MCS-R53: Change ID \"{changeId}\": ChangeRequest was submitted.");

            //Enquire with R53 the status of the change (R53 updates can be VERY slow business)
            GetChangeRequest changeRequest =
                new GetChangeRequest
            {
                Id = changeId
            };

            while (r53Client.GetChangeAsync(changeRequest).GetAwaiter().GetResult().ChangeInfo.Status == ChangeStatus.PENDING)
            {
                //Output
                Output($"MCS-R53: Change ID \"{changeId}\": Change is still pending. (Can take a while.)");

                //Wait
                Thread.Sleep(10 * 1000); //Wait ten seconds
            }

            //Output DONE
            Output($"MCS-R53: Change ID \"{changeId}\": Change IN SYNC. Done.");
            Output($"MCS: Completed Save of {Clients.Count} Clients");
        }
        /// <summary>
        /// Run a single item
        /// </summary>
        /// <param name="item">The item to run</param>
        public void RunItem(Route53Host item)
        {
            //Output
            Output($"R53: Starting item: {item}, new IP address: {IpState.NewIP}");

            //New the Route 53 Client
            AmazonRoute53Client r53Client =
                new AmazonRoute53Client(
                    Config.BaseSettings.AWSAccessKeyID,
                    Config.BaseSettings.AWSSecretAccessKey,
                    new AmazonRoute53Config {
                RegionEndpoint = RegionEndpoint
            });

            //New the Change Record to push
            Change change =
                new Change
            {
                Action            = ChangeAction.UPSERT, //Insert or Update
                ResourceRecordSet =
                    new ResourceRecordSet
                {
                    Name            = item.Name,
                    TTL             = item.TTL,
                    Type            = RRType.A,
                    ResourceRecords = new List <ResourceRecord> {
                        new ResourceRecord(IpState.NewIP)
                    }
                }
            };

            //New the Change Request
            ChangeResourceRecordSetsRequest recordSetsRequest =
                new ChangeResourceRecordSetsRequest
            {
                HostedZoneId = item.ZoneId,
                ChangeBatch  = new ChangeBatch(new List <Change> {
                    change
                })
            };

            //Submitting the Change Request to the API and receiving back the ID
            ChangeResourceRecordSetsResponse recordSetResponse = r53Client.ChangeResourceRecordSetsAsync(recordSetsRequest).GetAwaiter().GetResult();

            //The ID of the response
            string changeId = recordSetResponse.ChangeInfo.Id;

            //Output
            Output($"R53: Change ID \"{changeId}\": ChangeRequest was submitted.");

            //Enquire with R53 the status of the change (R53 updates can be VERY slow business)
            GetChangeRequest changeRequest =
                new GetChangeRequest
            {
                Id = changeId
            };

            while (r53Client.GetChangeAsync(changeRequest).GetAwaiter().GetResult().ChangeInfo.Status == ChangeStatus.PENDING)
            {
                //Output
                Output($"R53: Change ID \"{changeId}\": Change is still pending. (Can take a while.)");

                //Wait
                Thread.Sleep(10 * 1000); //Wait ten seconds
            }

            //Output DONE
            Output($"R53: Change ID \"{changeId}\": Change IN SYNC. Done.");
        }