public async Task Read_Success_Is_Success_Result_And_Contains_DNSRecord()
        {
            _httpTest.RespondWith(_readResponse);
            IDigitalOceanClient client = new DigitalOceanClient(_httpClient);

            DigitalOceanDomain domain = new DigitalOceanDomain
            {
                Name = "test.com"
            };

            Result <DigitalOceanGetDomainRecordsResponse> response = await client.GetDNSRecordsAsync(domain, string.Empty, CancellationToken.None);

            Assert.True(response.IsSuccess);

            IList <DigitalOceanGetDomainRecordResponse> domainRecords = response.Value.DomainRecords.ToList();

            Assert.True(domainRecords.Count() == 1);

            DigitalOceanGetDomainRecordResponse dnsRecord = domainRecords.First();

            Assert.Equal("100.100.100.100", dnsRecord.Data);
            Assert.Equal(28448432, dnsRecord.Id);
            Assert.Equal("test", dnsRecord.Name);
            Assert.Equal(DNSRecordType.A, dnsRecord.Type);
        }
예제 #2
0
        public async Task ReturnsFailureWhenAnyFail()
        {
            DigitalOceanDomain domainOne = new DigitalOceanDomain();
            DigitalOceanDomain domainTwo = new DigitalOceanDomain();

            IDigitalOceanDomainProcessor domainProcessor = A.Fake <IDigitalOceanDomainProcessor>();

            A.CallTo(() => domainProcessor.ProcessAsync(domainOne, A <ExternalAddress> .Ignored, A <string> .Ignored, A <CancellationToken> .Ignored)).Returns(Result.Ok());
            A.CallTo(() => domainProcessor.ProcessAsync(domainTwo, A <ExternalAddress> .Ignored, A <string> .Ignored, A <CancellationToken> .Ignored)).Returns(Result.Fail("This failed"));
            DigitalOceanAccountProcessor processor = new DigitalOceanAccountProcessor(domainProcessor);

            DigitalOceanAccount account = new DigitalOceanAccount()
            {
                Domains = new List <DigitalOceanDomain>()
                {
                    domainOne, domainTwo
                }
            };
            ExternalAddress externalAddress = new ExternalAddress()
            {
                IPv4Address = IPAddress.Parse("100.100.100.100")
            };

            Result result = await processor.ProcessAsync(account, externalAddress, new CancellationToken());

            Assert.True(result.IsFailed);
        }
        public async Task ReturnsFailureWhenUpdateFails()
        {
            DNSRecordCollection dnsRecordCollection = DNSRecordCollection.Empty();
            DigitalOceanDomain  domain          = new DigitalOceanDomain();
            ExternalAddress     externalAddress = new ExternalAddress();

            IDNSRecordCollectionMutator dnsRecordMutator = A.Fake <IDNSRecordCollectionMutator>();

            A.CallTo(() => dnsRecordMutator.Mutate(A <DNSRecordCollection> .Ignored, A <IDNSRecordCollectionMutation> .Ignored)).Returns(dnsRecordCollection);

            IDigitalOceanDNSRecordCreator dnsRecordCreator = A.Fake <IDigitalOceanDNSRecordCreator>();

            A.CallTo(() => dnsRecordCreator.CreateAsync(A <string> .Ignored, A <DNSRecordCollection> .Ignored, A <string> .Ignored, A <CancellationToken> .Ignored)).Returns(Result.Ok());

            IDigitalOceanDNSRecordReader dnsRecordReader = A.Fake <IDigitalOceanDNSRecordReader>();

            A.CallTo(() => dnsRecordReader.ReadAsync(A <DigitalOceanDomain> .Ignored, A <string> .Ignored, A <CancellationToken> .Ignored)).Returns(Result.Ok(dnsRecordCollection));

            IDigitalOceanDNSRecordUpdater dnsRecordUpdater = A.Fake <IDigitalOceanDNSRecordUpdater>();

            A.CallTo(() => dnsRecordUpdater.UpdateAsync(A <string> .Ignored, A <DNSRecordCollection> .Ignored, A <string> .Ignored, A <CancellationToken> .Ignored)).Returns(Result.Fail("Error."));

            IDigitalOceanDomainProcessor processor = new DigitalOceanDomainProcessor(dnsRecordMutator, dnsRecordCreator, dnsRecordReader, dnsRecordUpdater);
            Result result = await processor.ProcessAsync(domain, externalAddress, string.Empty, new CancellationToken());

            Assert.True(result.IsFailed);
        }
        public async Task Record_Retrieval_Failed()
        {
            DigitalOceanDomain           domain = new DigitalOceanDomain();
            IDigitalOceanClient          client = A.Fake <IDigitalOceanClient>();
            IDigitalOceanDNSRecordReader reader = new DigitalOceanDNSRecordReader(client, _mappingHelper.Mapper);

            A.CallTo(() => client.GetDNSRecordsAsync(A <DigitalOceanDomain> .Ignored, A <string> .Ignored, A <CancellationToken> .Ignored)).Returns(Result.Fail("Error"));

            Result <DNSRecordCollection> result = await reader.ReadAsync(domain, string.Empty, CancellationToken.None);

            Assert.True(result.IsFailed);
        }
예제 #5
0
        public async Task <Result <DNSRecordCollection> > ReadAsync(DigitalOceanDomain domain, string token, CancellationToken cancellation)
        {
            Result <DigitalOceanGetDomainRecordsResponse> result = await _client.GetDNSRecordsAsync(domain, token, cancellation);

            if (result.IsFailed)
            {
                return(result.ToResult());
            }

            IEnumerable <DNSRecord> records = _mapper.Map <IEnumerable <DNSRecord> >(result.Value.DomainRecords);

            return(Result.Ok(new DNSRecordCollection(records)).Merge(result));
        }
        public void ValidateValidDigitalOceanDomain()
        {
            DigitalOceanDomain domain = new DigitalOceanDomain
            {
                Name    = "test.com",
                Records = ValidDNSRecordCollection()
            };

            IValidator <DigitalOceanDomain> validator = new DigitalOceanDomainValidator();
            ValidationResult validationResult         = validator.Validate(domain);

            Assert.True(validationResult.IsValid);
        }
        public async Task Read_Failure_Is_Fail_Result()
        {
            _httpTest.RespondWith(string.Empty, (int)HttpStatusCode.NotFound);
            IDigitalOceanClient client = new DigitalOceanClient(_httpClient);

            DigitalOceanDomain domain = new DigitalOceanDomain
            {
                Name = "test.com"
            };

            Result <DigitalOceanGetDomainRecordsResponse> response = await client.GetDNSRecordsAsync(domain, string.Empty, CancellationToken.None);

            Assert.True(response.IsFailed);
        }
        public void InvalidDigitalOceanDomain()
        {
            DigitalOceanDomain domain = new DigitalOceanDomain
            {
                Name    = string.Empty,
                Records = ValidDNSRecordCollection()
            };

            IValidator <DigitalOceanDomain> validator = new DigitalOceanDomainValidator();
            ValidationResult validationResult         = validator.Validate(domain);

            Assert.False(validationResult.IsValid);
            Assert.True(validationResult.Errors.All(m => m.ErrorMessage.Equals(DigitalOceanDomainValidator.NameErrorMessage)));
        }
        public async Task Record_Is_Successfully_Retrieved()
        {
            string        ipAddress = "100.100.100.100";
            string        name      = "test";
            int           TTL       = 1800;
            DNSRecordType type      = DNSRecordType.A;

            DigitalOceanDomain domain = new DigitalOceanDomain();
            DigitalOceanGetDomainRecordsResponse clientResponse = new DigitalOceanGetDomainRecordsResponse
            {
                DomainRecords = new List <DigitalOceanGetDomainRecordResponse>
                {
                    new DigitalOceanGetDomainRecordResponse
                    {
                        Data = ipAddress,
                        Name = name,
                        Ttl  = TTL,
                        Type = type
                    }
                }
            };
            Result <DigitalOceanGetDomainRecordsResponse> clientResponeResult = Result.Ok(clientResponse);

            IDigitalOceanClient          client = A.Fake <IDigitalOceanClient>();
            IDigitalOceanDNSRecordReader reader = new DigitalOceanDNSRecordReader(client, _mappingHelper.Mapper);

            A.CallTo(() => client.GetDNSRecordsAsync(A <DigitalOceanDomain> .Ignored, A <string> .Ignored, A <CancellationToken> .Ignored)).Returns(clientResponeResult);

            Result <DNSRecordCollection> result = await reader.ReadAsync(domain, string.Empty, CancellationToken.None);

            Assert.True(result.IsSuccess);

            DNSRecordCollection dnsRecords = result.Value;

            Assert.True(dnsRecords.Count == 1);

            DNSRecord dnsRecord = result.Value.First();

            Assert.Equal(dnsRecord.Data, ipAddress);
            Assert.Equal(dnsRecord.Name, name);
            Assert.Equal(dnsRecord.TTL, TTL);
            Assert.Equal(dnsRecord.Type, type);
        }
        public async Task <Result> ProcessAsync(DigitalOceanDomain domain, ExternalAddress externalAddress, string token, CancellationToken cancellation)
        {
            Result <DNSRecordCollection> activeDnsRecordsResult = await _dnsRecordReader.ReadAsync(domain, token, cancellation);

            if (activeDnsRecordsResult.IsFailed)
            {
                return(activeDnsRecordsResult);
            }

            IDNSRecordCollectionMutation[] mutations = GetMutations(externalAddress, activeDnsRecordsResult.Value);

            DNSRecordCollection configurationRecords = new DNSRecordCollection(domain.Records);
            DNSRecordCollection hydratedDnsRecords   = _dnsRecordMutator.Mutate(configurationRecords, mutations);
            DNSRecordCollection newRecords           = activeDnsRecordsResult.Value.WhereNew(hydratedDnsRecords);
            DNSRecordCollection updatedRecords       = activeDnsRecordsResult.Value.WhereUpdated(hydratedDnsRecords);

            Result create = await _dnsRecordCreator.CreateAsync(domain.Name, newRecords, token, cancellation);

            Result update = await _dnsRecordUpdater.UpdateAsync(domain.Name, updatedRecords, token, cancellation);

            return(activeDnsRecordsResult.Merge(create, update));
        }
예제 #11
0
        public async Task <Result <DigitalOceanGetDomainRecordsResponse> > GetDNSRecordsAsync(DigitalOceanDomain domain, string token, CancellationToken cancellation)
        {
            string         path        = string.Format(_getDNSRecordsFormat, domain.Name);
            IFlurlRequest  httpRequest = BuildRequest(token, path);
            IFlurlResponse response    = await httpRequest.GetAsync(cancellation);

            HttpResponseMessage message = response.ResponseMessage;

            if (message.IsSuccessStatusCode)
            {
                string content = await message.Content.ReadAsStringAsync();

                DigitalOceanGetDomainRecordsResponse records = JsonConvert.DeserializeObject <DigitalOceanGetDomainRecordsResponse>(content);
                return(Result.Ok(records).WithSuccess(string.Format(_getDNSRecordsSuccessMessageTemplate, records.DomainRecords.Count(), domain.Name)));
            }
            return(Result.Fail(string.Format(_getDNSRecordsFailureMessageTemplate, domain.Name)));
        }