public IEnumerable<Guid> GetTargetAppKeys(Guid targetKey) { try { var indexes = new Indexes(Context); var index = indexes.LoadIndex(GetTargetAppsIndexPath(targetKey)); return index.Entries.Select(e => e.Key); } catch (Exception ex) { throw new DeploymentException(string.Format("Failed getting app keys for target \"{0}\".", targetKey), ex); } }
public void CreateVersion(Version version) { if (version == null) throw new ArgumentNullException("version", "Version cannot be null."); if (version.AppKey == Guid.Empty) throw new ArgumentException("App key cannot be empty.", "version.AppKey"); try { using (var client = new AmazonS3Client(Context.AwsAccessKeyId, Context.AwsSecretAccessKey)) { var appsController = new Apps(Context); var app = appsController.GetApp(version.AppKey); version.GroupKey = app.GroupKey; var indexesController = new Internal.Indexes(Context); using (var stream = version.Serialise()) { string indexPath = GetAppVersionsIndexPath(version.AppKey); var index = indexesController.LoadIndex(indexPath); if (index.Entries.Any(e => e.Key == version.Key)) { throw new DeploymentException("Index already contains entry for given key!"); } using (var putResponse = client.PutObject(new PutObjectRequest() { BucketName = Context.BucketName, Key = string.Format("{0}/{1}/{2}", STR_VERSIONS_CONTAINER_PATH, version.Key.ToString("N"), STR_INFO_FILE_NAME), InputStream = stream, })) { } index.Entries.Add(new Internal.EntityIndexEntry() { Key = version.Key, Name = CreateVersionIndexName(version) }); Internal.Indexes.NameSortIndex(index, true); indexesController.UpdateIndex(indexPath, index); } } } catch (AmazonS3Exception awsEx) { throw new DeploymentException("Failed creating version.", awsEx); } catch (Exception ex) { throw new DeploymentException("Failed creating version.", ex); } }
public void CreateInstance(Instance instance) { if (instance == null) throw new ArgumentNullException("instance", "Instance cannot be null."); if (instance.TargetKey == Guid.Empty) throw new ArgumentException("Target key cannot be empty.", "instance.TargetKey"); using (var stream = instance.Serialise()) { try { using (var client = new AmazonS3Client(Context.AwsAccessKeyId, Context.AwsSecretAccessKey)) { var targetsController = new Targets(Context); if (!targetsController.TargetExists(instance.TargetKey)) throw new TargetNotFoundException(String.Format("Target with the key \"{0}\" could not be found.", instance.TargetKey)); var indexesController = new Internal.Indexes(Context); string indexPath = GetTargetInstancesIndexPath(instance.TargetKey); var instanceIndex = indexesController.LoadIndex(indexPath); if (instanceIndex.Entries.Any(e => e.Key == instance.Key)) { throw new DeploymentException("Target instances index already contains entry for new instance key!"); } using (var putResponse = client.PutObject(new PutObjectRequest() { BucketName = Context.BucketName, Key = string.Format("{0}/{1}/{2}", STR_INSTANCES_CONTAINER_PATH, instance.Key.ToString("N"), STR_INFO_FILE_NAME), InputStream = stream, })) { } instanceIndex.Entries.Add(new Internal.EntityIndexEntry() { Key = instance.Key, Name = instance.Name }); Internal.Indexes.NameSortIndex(instanceIndex); indexesController.UpdateIndex(indexPath, instanceIndex); } } catch (AmazonS3Exception awsEx) { throw new DeploymentException("Failed creating instance.", awsEx); } } }
public void CreateTarget(Target target) { if (target == null) throw new ArgumentNullException("target", "Target cannot be null."); if (target.GroupKey == Guid.Empty) throw new ArgumentException("Group key cannot be empty.", "target.GroupKey"); using (var stream = target.Serialise()) { try { using (var client = new AmazonS3Client(Context.AwsAccessKeyId, Context.AwsSecretAccessKey)) { var groupsController = new Groups(Context); if (!groupsController.GroupExists(target.GroupKey)) throw new GroupNotFoundException(String.Format("Group with the key \"{0}\" could not be found.", target.GroupKey)); var indexesController = new Internal.Indexes(Context); string indexPath = GetGroupTargetsIndexPath(target.GroupKey); var appIndex = indexesController.LoadIndex(indexPath); if (appIndex.Entries.Any(e => e.Key == target.Key)) { throw new DeploymentException("Index already contains entry for given key!"); } using (var putResponse = client.PutObject(new PutObjectRequest() { BucketName = Context.BucketName, Key = string.Format("{0}/{1}/{2}", STR_TARGETS_CONTAINER_PATH, target.Key.ToString("N"), STR_INFO_FILE_NAME), InputStream = stream, })) { } appIndex.Entries.Add(new Internal.EntityIndexEntry() { Key = target.Key, Name = target.Name }); Internal.Indexes.NameSortIndex(appIndex); indexesController.UpdateIndex(indexPath, appIndex); } } catch (AmazonS3Exception awsEx) { throw new DeploymentException("Failed creating target.", awsEx); } } }
public void CreateGroup(Group group) { if (group == null) throw new ArgumentNullException("group", "Group cannot be null."); using (var stream = group.Serialise()) { try { using (var client = new AmazonS3Client(Context.AwsAccessKeyId, Context.AwsSecretAccessKey)) { var indexesController = new Internal.Indexes(Context); var index = indexesController.LoadIndex(STR_GROUP_INDEX_PATH); if (index.Entries.Any(e => e.Key == group.Key)) { throw new DeploymentException("Index already contains entry for given key!"); } using (var putResponse = client.PutObject(new PutObjectRequest() { BucketName = Context.BucketName, Key = string.Format("{0}/{1}/{2}", STR_GROUPS_CONTAINER_PATH, group.Key.ToString("N"), STR_INFO_FILE_NAME), InputStream = stream, })) { } index.Entries.Add(new Internal.EntityIndexEntry() { Key = group.Key, Name = group.Name }); Internal.Indexes.NameSortIndex(index); indexesController.UpdateIndex(STR_GROUP_INDEX_PATH, index); } } catch (AmazonS3Exception awsEx) { throw new DeploymentException("Failed creating group.", awsEx); } } }
public void DeleteInstance(Guid instanceKey) { var instance = GetInstance(instanceKey); try { Plywood.Internal.AwsHelpers.SoftDeleteFolders(Context, string.Format("{0}/{1}", STR_INSTANCES_CONTAINER_PATH, instanceKey.ToString("N"))); var indexesController = new Internal.Indexes(Context); string indexPath = GetTargetInstancesIndexPath(instance.TargetKey); var appIndex = indexesController.LoadIndex(indexPath); if (appIndex.Entries.Any(e => e.Key == instanceKey)) { appIndex.Entries.Remove(appIndex.Entries.Single(e => e.Key == instanceKey)); Internal.Indexes.NameSortIndex(appIndex); indexesController.UpdateIndex(indexPath, appIndex); } } catch (AmazonS3Exception awsEx) { throw new DeploymentException("Failed deleting instance.", awsEx); } }
public InstanceList SearchInstances(Guid targetKey, string query = null, int offset = 0, int pageSize = 50) { if (offset < 0) throw new ArgumentOutOfRangeException("offset", "Offset cannot be a negative number."); if (pageSize < 1) throw new ArgumentOutOfRangeException("pageSize", "Page size cannot be less than 1."); if (pageSize > 100) throw new ArgumentOutOfRangeException("pageSize", "Page size cannot be greater than 100."); try { var indexesController = new Internal.Indexes(Context); var index = indexesController.LoadIndex(GetTargetInstancesIndexPath(targetKey)); var filteredIndex = index.Entries.AsQueryable(); if (!string.IsNullOrWhiteSpace(query)) { var queryParts = query.ToLower().Split(new char[] { ' ', '\t', ',' }).Where(qp => !string.IsNullOrWhiteSpace(qp)).ToArray(); filteredIndex = filteredIndex.Where(e => queryParts.Any(q => e.Name.ToLower().Contains(q))); } var count = filteredIndex.Count(); var listItems = filteredIndex.Skip(offset).Take(pageSize).Select(e => new InstanceListItem() { Key = e.Key, Name = e.Name }).ToList(); var list = new InstanceList() { TargetKey = targetKey, Instances = listItems, Query = query, Offset = offset, PageSize = pageSize, TotalCount = count, }; return list; } catch (AmazonS3Exception awsEx) { throw new DeploymentException("Failed searcing instances.", awsEx); } }
public TargetAppList SearchTargetApps(Guid targetKey, string query = null, int offset = 0, int pageSize = 50) { try { var indexes = new Indexes(Context); var targets = new Targets(Context); var apps = new Apps(Context); var index = indexes.LoadIndex(GetTargetAppsIndexPath(targetKey)); var target = targets.GetTarget(targetKey); var groupAppIndex = indexes.LoadIndex(Apps.GetGroupAppsIndexPath(target.GroupKey)); var filteredIndex = index.Entries.AsQueryable(); if (!string.IsNullOrWhiteSpace(query)) { var queryParts = query.ToLower().Split(new char[] { ' ', '\t', ',' }).Where(qp => !string.IsNullOrWhiteSpace(qp)).ToArray(); filteredIndex = filteredIndex.Where(e => queryParts.Any(q => e.Name.ToLower().Contains(q))); } var count = filteredIndex.Count(); var listItems = filteredIndex.Skip(offset).Take(pageSize).ToList().Select(e => { var groupApp = groupAppIndex.Entries.FirstOrDefault(a => a.Key == e.Key); return new AppListItem() { Key = e.Key, Name = (groupApp != null) ? groupApp.Name : TryResolveAppNameDirect(e.Key, apps) }; }).ToList().OrderBy(app => app.Name); var list = new TargetAppList() { Apps = listItems, TargetKey = targetKey, Query = query, Offset = offset, PageSize = pageSize, TotalCount = count, }; return list; } catch (Exception ex) { throw new DeploymentException(string.Format("Failed getting app keys for target \"{0}\".", targetKey), ex); } }
public void DeleteApp(Guid key) { var app = GetApp(key); try { Plywood.Internal.AwsHelpers.SoftDeleteFolders(Context, string.Format("{0}/{1}", STR_APPS_CONTAINER_PATH, key.ToString("N"))); var indexesController = new Internal.Indexes(Context); string indexPath = GetGroupAppsIndexPath(app.GroupKey); var appIndex = indexesController.LoadIndex(indexPath); if (appIndex.Entries.Any(e => e.Key == key)) { appIndex.Entries.Remove(appIndex.Entries.Single(e => e.Key == key)); Internal.Indexes.NameSortIndex(appIndex); indexesController.UpdateIndex(indexPath, appIndex); } } catch (AmazonS3Exception awsEx) { throw new DeploymentException("Failed deleting app.", awsEx); } }
public void DeleteVersion(Guid key) { var version = GetVersion(key); try { Plywood.Internal.AwsHelpers.SoftDeleteFolders(Context, string.Format("{0}/{1}", STR_VERSIONS_CONTAINER_PATH, key.ToString("N"))); var indexesController = new Internal.Indexes(Context); string indexPath = GetAppVersionsIndexPath(version.AppKey); var appIndex = indexesController.LoadIndex(indexPath); if (appIndex.Entries.Any(e => e.Key == key)) { appIndex.Entries.Remove(appIndex.Entries.Single(e => e.Key == key)); Internal.Indexes.NameSortIndex(appIndex, true); indexesController.UpdateIndex(indexPath, appIndex); } } catch (AmazonS3Exception awsEx) { throw new DeploymentException(string.Format("Failed deleting version with key \"{0}\"", key), awsEx); } catch (Exception ex) { throw new DeploymentException(string.Format("Failed deleting version with key \"{0}\"", key), ex); } }
public VersionList SearchAppVersions(Guid appKey, DateTime? fromDate = null, DateTime? toDate = null, string query = null, int offset = 0, int pageSize = 50) { if (offset < 0) throw new ArgumentOutOfRangeException("offset", "Offset cannot be a negative number."); if (pageSize < 1) throw new ArgumentOutOfRangeException("pageSize", "Page size cannot be less than 1."); if (pageSize > 100) throw new ArgumentOutOfRangeException("pageSize", "Page size cannot be greater than 100."); try { var indexesController = new Internal.Indexes(Context); var index = indexesController.LoadIndex(GetAppVersionsIndexPath(appKey)); var filteredIndex = index.Entries.AsQueryable(); if (fromDate != null) { filteredIndex = filteredIndex.Where(e => DateTime.Parse(e.Name.Substring(0, e.Name.IndexOf(' '))) >= fromDate); } if (toDate != null) { filteredIndex = filteredIndex.Where(e => DateTime.Parse(e.Name.Substring(0, e.Name.IndexOf(' '))) <= toDate); } if (!string.IsNullOrWhiteSpace(query)) { var queryParts = query.ToLower().Split(new char[] { ' ', '\t', ',' }).Where(qp => !string.IsNullOrWhiteSpace(qp)).ToArray(); filteredIndex = filteredIndex.Where(e => queryParts.Any(q => e.Name.ToLower().Contains(q))); } var count = filteredIndex.Count(); var listItems = filteredIndex.Skip(offset).Take(pageSize).Select(e => new VersionListItem() { Key = e.Key, Timestamp = DateTime.Parse(e.Name.Substring(0, e.Name.IndexOf(' '))), VersionNumber = e.Name.Substring(e.Name.IndexOf(' ') + 1, e.Name.IndexOf(' ', e.Name.IndexOf(' ') + 1) - (e.Name.IndexOf(' ') + 1)), Comment = e.Name.Substring(e.Name.IndexOf(' ', e.Name.IndexOf(' ') + 1) + 1), }).ToList(); var list = new VersionList() { AppKey = appKey, Versions = listItems, Offset = offset, PageSize = pageSize, TotalCount = count, }; return list; } catch (AmazonS3Exception awsEx) { throw new DeploymentException("Failed searcing groups.", awsEx); } catch (Exception ex) { throw new DeploymentException("Failed searcing groups.", ex); } }