public void SetUp() { IPRangeRepositoryMock = new Mock <IRepository <IPRange> >(); ProfileRepositoryMock = new Mock <IRepository <AwsProfile> >(); ClientFactoryMock = new Mock <IAwsClientFactory>(); Command = new RefreshIpRanges(ProfileRepositoryMock.Object, IPRangeRepositoryMock.Object, ClientFactoryMock.Object); _profileId = Guid.NewGuid(); var profile = new AwsProfile { Id = _profileId }; ProfileRepositoryMock.Setup(x => x.Find(_profileId)).Returns(profile); AwsClientMock = new Mock <IAwsClient>(); NetworkServiceMock = new Mock <INetworkService>(); AwsClientMock.Setup(x => x.NetworkService).Returns(NetworkServiceMock.Object); ClientFactoryMock.Setup(x => x.GetClient(profile)).Returns(AwsClientMock.Object); _ipsInRange = Enumerable.Range(8, 4) .Select(x => string.Format("255.255.255.{0}", x)).ToList(); var allocatedIps = _ipsInRange.Concat(new List <string> { "192.168.1.1" }); NetworkServiceMock.Setup(x => x.GetAllocatedIpAddresses()).Returns(allocatedIps); }
// GET: Profile/Delete/5 public ActionResult Delete(Guid id) { AwsProfile profile = _profileRepository.Find(id); var viewModel = new AwsProfileViewModel { Name = profile.Name, AccessKeyId = profile.AccessKeyId, Id = profile.Id }; return(View(viewModel)); }
public void ExitEarlyIfInstanceIdIsNotAtAmazon() { // Arrange var profile = new AwsProfile(); AwsProfileRepositoryMock.Setup(x => x.Find(profile.Id)).Returns(profile); InstanceServiceMock.Setup(x => x.GetInstance(It.IsAny<string>())).Returns((AwsInstance) null); AwsClientFactoryMock.Setup(x => x.GetClient(profile).InstanceService).Returns(InstanceServiceMock.Object); // Act Command.Execute(profile.Id, "HoojeyId"); // Assert InstanceServiceMock.Verify(x => x.GetSecurityGroups(It.IsAny<AwsInstance>()), Times.Never); }
public void Execute(Guid profileId, string subnetId) { AwsProfile profile = _profileRepository.Find(profileId); if (profile == null) { return; } INetworkService networkService = _awsClientFactory.GetClient(profile).NetworkService; string cidrRange; try { cidrRange = networkService.GetCidrBySubnetId(subnetId); } catch (AmazonEC2Exception e) { Debug.WriteLine(e.Message); return; } if (_ipRangeRepository.FindAll().Any(x => x.Cidr == cidrRange)) { return; } DateTime utcNow = DateTime.UtcNow; IPNetwork network = IPNetwork.Parse(cidrRange); List <SubnetIpAddress> subnetIpAddresses = IPNetwork.ListIPAddress(network) .Skip(5) // Amazon reserves the first four IP addresses... (x.x.x.1 - x.x.x.4) .Select(x => new SubnetIpAddress { Address = x, IsInUse = false, LastUpdateTime = utcNow }).ToList(); if (subnetIpAddresses.Any()) { subnetIpAddresses.RemoveAt(subnetIpAddresses.Count - 1); // and last IP address. } var ipRange = new IPRange { AwsProfileId = profileId, Cidr = cidrRange, Addresses = subnetIpAddresses.ToDictionary(x => x.Address.ToString()) }; _ipRangeRepository.Add(ipRange); }
public void SetUp() { AwsClientFactoryMock = new Mock <IAwsClientFactory>(); StackRepositoryMock = new Mock <IRepository <Stack> >(); ProfileRepositoryMock = new Mock <IRepository <AwsProfile> >(); StackServiceMock = new Mock <IStackService>(); Profile = new AwsProfile { Id = Guid.NewGuid() }; ProfileRepositoryMock.Setup(x => x.Find(Profile.Id)).Returns(Profile); Command = new RemoveStaleStacks(AwsClientFactoryMock.Object, StackRepositoryMock.Object, ProfileRepositoryMock.Object); }
protected bool TryInitialize(Guid profileId, out IAwsClient awsClient, out AwsProfile profile) { var localProfile = ProfileRepository.Find(profileId); if (localProfile == null) { profile = null; awsClient = null; return false; } awsClient = _clientFactory.GetClient(localProfile); profile = localProfile; return true; }
public void ExitEarlyIfInstanceIdIsNotAtAmazon() { // Arrange var profile = new AwsProfile(); AwsProfileRepositoryMock.Setup(x => x.Find(profile.Id)).Returns(profile); InstanceServiceMock.Setup(x => x.GetInstance(It.IsAny <string>())).Returns((AwsInstance)null); AwsClientFactoryMock.Setup(x => x.GetClient(profile).InstanceService).Returns(InstanceServiceMock.Object); // Act Command.Execute(profile.Id, "HoojeyId"); // Assert InstanceServiceMock.Verify(x => x.GetSecurityGroups(It.IsAny <AwsInstance>()), Times.Never); }
protected bool TryInitialize(Guid profileId, out IAwsClient awsClient, out AwsProfile profile) { var localProfile = ProfileRepository.Find(profileId); if (localProfile == null) { profile = null; awsClient = null; return(false); } awsClient = _clientFactory.GetClient(localProfile); profile = localProfile; return(true); }
private void RefreshDataForPeriod(AwsProfile profile, string period, IAwsClient client, DateTime currentTime) { string s3Url = MonthlyCsvUrl.Create(profile.DetailedBillingS3Bucket, profile.Account, period); var lastModified = new DateTime(); if (profile.BillingMetadata.ContainsKey(period)) { lastModified = profile.BillingMetadata[period].LastModified; } DateTime newLastModified; Stream file = client.StorageService.GetFileIfChangedSince(s3Url, lastModified, out newLastModified); if (file == null) { return; } using (file) { using (Stream zipStream = OpenFirstZipEntry(file)) { using (var streamReader = new StreamReader(zipStream, Encoding.UTF8, detectEncodingFromByteOrderMarks: true, bufferSize: 32768, leaveOpen: true)) { using (var parser = new LineItemCsvParser(streamReader)) { _billingManager.LoadLineItems(parser.GetLineItems(), period); } } } } BillingMetadata metadata; if (!profile.BillingMetadata.ContainsKey(period)) { metadata = new BillingMetadata(); profile.BillingMetadata.Add(period, metadata); } else { metadata = profile.BillingMetadata[period]; } metadata.LastModified = newLastModified; metadata.LastLoaded = currentTime; }
public void SkipsMissingS3Bucket() { // Arrange Guid profileId = Guid.NewGuid(); var profile = new AwsProfile { Id = profileId, DetailedBillingS3Bucket = null }; AwsProfileRepositoryMock.Setup(x => x.Find(profileId)).Returns(profile); var command = new UpdateBillingData(AwsClientFactory, AwsProfileRepository, BillingManager, Clock, new S3PathParser()); // Act command.LoadDeltas(profileId); // Assert AwsProfileRepositoryMock.Verify(x => x.Update(It.IsAny <AwsProfile>()), Times.Never); }
public void SetUp() { AwsClientFactoryMock = new Mock <IAwsClientFactory>(); StackRepositoryMock = new Mock <IRepository <Stack> >(); InstanceRepositoryMock = new Mock <IRepository <Instance> >(); ProfileRepositoryMock = new Mock <IRepository <AwsProfile> >(); StackServiceMock = new Mock <IStackService>(); Profile = new AwsProfile { Id = Guid.NewGuid() }; ProfileRepositoryMock.Setup(x => x.Find(Profile.Id)).Returns(Profile); var stackLoader = new StackLoader(StackRepositoryMock.Object, InstanceRepositoryMock.Object); Command = new UpdateStack(AwsClientFactoryMock.Object, ProfileRepositoryMock.Object, stackLoader); }
public ActionResult Edit(Guid id, AwsProfileViewModel profileViewModel) { try { AwsProfile profile = _profileRepository.Find(id); if (profileViewModel.SecretAccessKey == _encryptedMessagePlaceholder) { profileViewModel.SecretAccessKey = _cryptoProvider.Decrypt(profile.EncryptedKey, profile.EncryptedSecretAccessKey); } if (ValidateViewModelAgainstExternalSources(profileViewModel, newProfile: false)) { profile.Name = profileViewModel.Name; profile.Account = profileViewModel.Account; profile.AccessKeyId = profileViewModel.AccessKeyId; profile.DefaultVpcId = profileViewModel.DefaultVpcId; profile.DefaultSubnetId = profileViewModel.DefaultSubnetId; profile.HostedZone = profileViewModel.HostedZone; profile.Groups = profileViewModel.Groups.ToList(); profile.DetailedBillingS3Bucket = profileViewModel.DetailedBillingS3Bucket; if (profileViewModel.SecretAccessKey != _encryptedMessagePlaceholder) { string dataKey; string encryptedAccessKey = _cryptoProvider.Encrypt(out dataKey, profileViewModel.SecretAccessKey); profile.EncryptedSecretAccessKey = encryptedAccessKey; profile.EncryptedKey = dataKey; } _profileRepository.Update(profile); _backgroundJobClient.Enqueue <CreateIpRange>(x => x.Execute(profileViewModel.Id, profileViewModel.DefaultSubnetId)); return(RedirectToAction("Index")); } } catch { // err, todo? return(View(profileViewModel)); } return(View(profileViewModel)); }
public ActionResult Create(AwsProfileViewModel profileViewModel) { try { if (ValidateViewModelAgainstExternalSources(profileViewModel, newProfile: true)) { string dataKey; string encryptedStrings = _cryptoProvider.Encrypt( out dataKey, profileViewModel.SecretAccessKey ); var profile = new AwsProfile { Name = profileViewModel.Name, Account = profileViewModel.Account, AccessKeyId = profileViewModel.AccessKeyId, EncryptedKey = dataKey, EncryptedSecretAccessKey = encryptedStrings, DefaultVpcId = profileViewModel.DefaultVpcId, DefaultSubnetId = profileViewModel.DefaultSubnetId, HostedZone = profileViewModel.HostedZone, Groups = profileViewModel.Groups, DetailedBillingS3Bucket = profileViewModel.DetailedBillingS3Bucket, }; _profileRepository.Add(profile); _backgroundJobClient.Enqueue <CreateDefaultSecurityGroup>(x => x.Execute(profile.Id, profileViewModel.DefaultVpcId)); _backgroundJobClient.Enqueue <CreateIpRange>(x => x.Execute(profile.Id, profileViewModel.DefaultSubnetId)); return(RedirectToAction("Index")); } } catch (Exception e) { // TODO: Just for debugging! Remove this. Don't show this to the user, but log it somewhere for troubleshooting purposes. ModelState.AddModelError("", string.Format("There was a problem creating the profile. {0}", e.Message)); } // If we make it this far, something bad happened. Display the error message to the user. return(View(profileViewModel)); }
// GET: Profile/Details/5 public ActionResult Details(Guid id) { AwsProfile profile = _profileRepository.Find(id); var viewModel = new AwsProfileViewModel { Name = profile.Name, AccessKeyId = profile.AccessKeyId, SecretAccessKey = _encryptedMessagePlaceholder, DefaultVpcId = profile.DefaultVpcId, DefaultSubnetId = profile.DefaultSubnetId, Id = profile.Id, Groups = profile.Groups.ToList(), HostedZone = profile.HostedZone, DetailedBillingS3Bucket = profile.DetailedBillingS3Bucket, }; return(View(viewModel)); }
public void LoadsCsv() { // Arrange Guid profileId = Guid.NewGuid(); var profile = new AwsProfile { Id = profileId, DetailedBillingS3Bucket = "my-bucket", IsBillingHistoryLoaded = true, Account = "12345", }; AwsProfileRepositoryMock.Setup(x => x.Find(profileId)).Returns(profile); AwsClientFactoryMock.Setup(x => x.GetClient(profile)).Returns(AwsClientMock.Object); var storage = new TestStorageService(); AwsClientMock.Setup(x => x.StorageService).Returns(storage); storage.Contents = new MemoryStream(Resources.LineItemsZip); var now = new DateTime(2014, 6, 14, 16, 15, 14, DateTimeKind.Utc); ClockMock.Setup(x => x.UtcNow).Returns(now); var command = new UpdateBillingData(AwsClientFactory, AwsProfileRepository, BillingManager, Clock, new S3PathParser()); string loadedPeriod = null; LineItem lineItem = null; BillingManagerMock.Setup(x => x.LoadLineItems(It.IsAny <IEnumerable <LineItem> >(), It.IsAny <string>())) .Callback((IEnumerable <LineItem> lineItems, string period) => { lineItem = lineItems.FirstOrDefault(); loadedPeriod = period; }); // Act command.LoadDeltas(profileId); // Assert loadedPeriod.Should().Be("2014-06"); lineItem.RecordId.Should().Be("31861622192480759163092020", "should match first line item from line-items.csv embedded resource"); }
public void SetUp() { AwsClientFactoryMock = new Mock <IAwsClientFactory>(); StackRepositoryMock = new Mock <IRepository <Instance> >(); ProfileRepositoryMock = new Mock <IRepository <AwsProfile> >(); InstanceServiceMock = new Mock <IInstanceService>(); BackgroundJobClientMock = new Mock <IBackgroundJobClient>(); Profile = new AwsProfile { Id = Guid.NewGuid() }; ProfileRepositoryMock.Setup(x => x.Find(Profile.Id)).Returns(Profile); Command = new RemoveStaleInstances( AwsClientFactoryMock.Object, StackRepositoryMock.Object, ProfileRepositoryMock.Object, BackgroundJobClientMock.Object ); }
public void SetUp() { IPRangeRepositoryMock = new Mock <IRepository <IPRange> >(); ProfileRepositoryMock = new Mock <IRepository <AwsProfile> >(); ClientFactoryMock = new Mock <IAwsClientFactory>(); Command = new CreateIpRange(IPRangeRepositoryMock.Object, ProfileRepositoryMock.Object, ClientFactoryMock.Object); _profileId = Guid.NewGuid(); var profile = new AwsProfile { Id = _profileId }; ProfileRepositoryMock.Setup(x => x.Find(_profileId)).Returns(profile); AwsClientMock = new Mock <IAwsClient>(); NetworkServiceMock = new Mock <INetworkService>(); AwsClientMock.Setup(x => x.NetworkService).Returns(NetworkServiceMock.Object); ClientFactoryMock.Setup(x => x.GetClient(profile)).Returns(AwsClientMock.Object); }
// GET: Profile/Edit/5 public ActionResult Edit(Guid id) { AwsProfile profile = _profileRepository.Find(id); IEnumerable <string> activeDirectoryGroups = _userClient.GetAllRoles(); var viewModel = new AwsProfileViewModel { Name = profile.Name, Account = profile.Account, AccessKeyId = profile.AccessKeyId, SecretAccessKey = _encryptedMessagePlaceholder, DefaultVpcId = profile.DefaultVpcId, DefaultSubnetId = profile.DefaultSubnetId, Groups = profile.Groups.ToList(), ServerGroups = activeDirectoryGroups.ToList(), HostedZone = profile.HostedZone, DetailedBillingS3Bucket = profile.DetailedBillingS3Bucket, }; return(View(viewModel)); }
public void QueriesCorrectS3Object() { // Arrange Guid profileId = Guid.NewGuid(); var lastModified = new DateTime(2014, 6, 14, 13, 12, 11, DateTimeKind.Utc); var profile = new AwsProfile { Id = profileId, DetailedBillingS3Bucket = "my-bucket", Account = "12345", IsBillingHistoryLoaded = true, BillingMetadata = { { "2014-06", new BillingMetadata { LastModified = lastModified } } } }; AwsProfileRepositoryMock.Setup(x => x.Find(profileId)).Returns(profile); AwsClientFactoryMock.Setup(x => x.GetClient(profile)).Returns(AwsClientMock.Object); var storage = new TestStorageService(); AwsClientMock.Setup(x => x.StorageService).Returns(storage); var now = new DateTime(2014, 6, 14, 16, 15, 14, DateTimeKind.Utc); ClockMock.Setup(x => x.UtcNow).Returns(now); var command = new UpdateBillingData(AwsClientFactory, AwsProfileRepository, BillingManager, Clock, new S3PathParser()); // Act command.LoadDeltas(profileId); // Assert string expectedUrl = UpdateBillingData.MonthlyCsvUrl.Create("my-bucket", "12345", "2014-06"); storage.S3Url.Should().Be(expectedUrl); storage.LastModified.Should().Be(lastModified); storage.CallCount.Should().Be(1); }
public void SetsLastModified() { // Arrange Guid profileId = Guid.NewGuid(); var profile = new AwsProfile { Id = profileId, DetailedBillingS3Bucket = "my-bucket", IsBillingHistoryLoaded = true, Account = "12345", }; AwsProfileRepositoryMock.Setup(x => x.Find(profileId)).Returns(profile); AwsClientFactoryMock.Setup(x => x.GetClient(profile)).Returns(AwsClientMock.Object); var now = new DateTime(2014, 6, 14, 16, 15, 14, DateTimeKind.Utc); ClockMock.Setup(x => x.UtcNow).Returns(now); var storage = new TestStorageService(); AwsClientMock.Setup(x => x.StorageService).Returns(storage); storage.NewLastModified = new DateTime(2020, 1, 1, 1, 1, 1, DateTimeKind.Utc); storage.Contents = new MemoryStream(Resources.LineItemsZip); var command = new UpdateBillingData(AwsClientFactory, AwsProfileRepository, BillingManager, Clock, new S3PathParser()); // Act command.LoadDeltas(profileId); // Assert AwsProfileRepositoryMock.Verify( x => x.Update(It.Is((AwsProfile p) => p.Id == profileId && p.BillingMetadata["2014-06"].LastModified == storage.NewLastModified) )); }
public void SetUp() { AwsClientFactoryMock = new Mock<IAwsClientFactory>(); StackRepositoryMock = new Mock<IRepository<Stack>>(); ProfileRepositoryMock = new Mock<IRepository<AwsProfile>>(); StackServiceMock = new Mock<IStackService>(); Profile = new AwsProfile {Id = Guid.NewGuid()}; ProfileRepositoryMock.Setup(x => x.Find(Profile.Id)).Returns(Profile); Command = new RemoveStaleStacks(AwsClientFactoryMock.Object, StackRepositoryMock.Object, ProfileRepositoryMock.Object); }
public IAwsClient GetClient(AwsProfile profile) { AWSCredentials awsCredentials = _credentialService.GetCredentials(profile); return(new AwsClient(awsCredentials, _configuration)); }
public IAwsClient GetClient(AwsProfile profile) { AWSCredentials awsCredentials = _credentialService.GetCredentials(profile); return new AwsClient(awsCredentials, _configuration); }
public void SetUp() { AwsClientFactoryMock = new Mock<IAwsClientFactory>(); StackRepositoryMock = new Mock<IRepository<Stack>>(); InstanceRepositoryMock = new Mock<IRepository<Instance>>(); ProfileRepositoryMock = new Mock<IRepository<AwsProfile>>(); StackServiceMock = new Mock<IStackService>(); Profile = new AwsProfile {Id = Guid.NewGuid()}; ProfileRepositoryMock.Setup(x => x.Find(Profile.Id)).Returns(Profile); var stackLoader = new StackLoader(StackRepositoryMock.Object, InstanceRepositoryMock.Object); Command = new UpdateStack(AwsClientFactoryMock.Object, ProfileRepositoryMock.Object, stackLoader); }
public void SetUp() { IPRangeRepositoryMock = new Mock<IRepository<IPRange>>(); ProfileRepositoryMock = new Mock<IRepository<AwsProfile>>(); ClientFactoryMock = new Mock<IAwsClientFactory>(); Command = new RefreshIpRanges(ProfileRepositoryMock.Object, IPRangeRepositoryMock.Object, ClientFactoryMock.Object); _profileId = Guid.NewGuid(); var profile = new AwsProfile { Id = _profileId }; ProfileRepositoryMock.Setup(x => x.Find(_profileId)).Returns(profile); AwsClientMock = new Mock<IAwsClient>(); NetworkServiceMock = new Mock<INetworkService>(); AwsClientMock.Setup(x => x.NetworkService).Returns(NetworkServiceMock.Object); ClientFactoryMock.Setup(x => x.GetClient(profile)).Returns(AwsClientMock.Object); _ipsInRange = Enumerable.Range(8, 4) .Select(x => string.Format("255.255.255.{0}", x)).ToList(); var allocatedIps = _ipsInRange.Concat(new List<string> { "192.168.1.1" }); NetworkServiceMock.Setup(x => x.GetAllocatedIpAddresses()).Returns(allocatedIps); }
private void RefreshDataForPeriod(AwsProfile profile, string period, IAwsClient client, DateTime currentTime) { string s3Url = MonthlyCsvUrl.Create(profile.DetailedBillingS3Bucket, profile.Account, period); var lastModified = new DateTime(); if (profile.BillingMetadata.ContainsKey(period)) { lastModified = profile.BillingMetadata[period].LastModified; } DateTime newLastModified; Stream file = client.StorageService.GetFileIfChangedSince(s3Url, lastModified, out newLastModified); if (file == null) { return; } using (file) { using (Stream zipStream = OpenFirstZipEntry(file)) { using (var streamReader = new StreamReader(zipStream, Encoding.UTF8, detectEncodingFromByteOrderMarks: true, bufferSize: 32768, leaveOpen: true)) { using (var parser = new LineItemCsvParser(streamReader)) { _billingManager.LoadLineItems(parser.GetLineItems(), period); } } } } BillingMetadata metadata; if (! profile.BillingMetadata.ContainsKey(period)) { metadata = new BillingMetadata(); profile.BillingMetadata.Add(period, metadata); } else { metadata = profile.BillingMetadata[period]; } metadata.LastModified = newLastModified; metadata.LastLoaded = currentTime; }
public ActionResult Create(AwsProfileViewModel profileViewModel) { try { if (ValidateViewModelAgainstExternalSources(profileViewModel, newProfile: true)) { string dataKey; string encryptedStrings = _cryptoProvider.Encrypt( out dataKey, profileViewModel.SecretAccessKey ); var profile = new AwsProfile { Name = profileViewModel.Name, Account = profileViewModel.Account, AccessKeyId = profileViewModel.AccessKeyId, EncryptedKey = dataKey, EncryptedSecretAccessKey = encryptedStrings, DefaultVpcId = profileViewModel.DefaultVpcId, DefaultSubnetId = profileViewModel.DefaultSubnetId, HostedZone = profileViewModel.HostedZone, Groups = profileViewModel.Groups, DetailedBillingS3Bucket = profileViewModel.DetailedBillingS3Bucket, }; _profileRepository.Add(profile); _backgroundJobClient.Enqueue<CreateDefaultSecurityGroup>(x => x.Execute(profile.Id, profileViewModel.DefaultVpcId)); _backgroundJobClient.Enqueue<CreateIpRange>(x => x.Execute(profile.Id, profileViewModel.DefaultSubnetId)); return RedirectToAction("Index"); } } catch (Exception e) { // TODO: Just for debugging! Remove this. Don't show this to the user, but log it somewhere for troubleshooting purposes. ModelState.AddModelError("", string.Format("There was a problem creating the profile. {0}", e.Message)); } // If we make it this far, something bad happened. Display the error message to the user. return View(profileViewModel); }
public AWSCredentials GetCredentials(AwsProfile profile) { var secretAccessKey = _cryptoProvider.Decrypt(profile.EncryptedKey, profile.EncryptedSecretAccessKey); return new BasicAWSCredentials(profile.AccessKeyId, secretAccessKey); }
public AWSCredentials GetCredentials(AwsProfile profile) { var secretAccessKey = _cryptoProvider.Decrypt(profile.EncryptedKey, profile.EncryptedSecretAccessKey); return(new BasicAWSCredentials(profile.AccessKeyId, secretAccessKey)); }