Пример #1
0
 public Target(Target prototype)
 {
     this.Key = prototype.Key;
     this.Name = prototype.Name;
     this.Tags = prototype.Tags;
     this.GroupKey = prototype.GroupKey;
 }
Пример #2
0
        public static Target Run(ControllerConfiguration context, Guid groupKey)
        {
            var targetsController = new Targets(context);

            var searchEmpty = targetsController.SearchGroupTargets(groupKey);
            Debug.Assert(searchEmpty.TotalCount == 0);
            Debug.Assert(searchEmpty.Targets.Count() == 0);

            var testTarget = new Target() { Name = "Test Target", GroupKey = groupKey };
            targetsController.CreateTarget(testTarget);
            var createdTarget = targetsController.GetTarget(testTarget.Key);
            Debug.Assert(createdTarget.Name == testTarget.Name);
            Debug.Assert(createdTarget.GroupKey == testTarget.GroupKey);

            var searchSingle = targetsController.SearchGroupTargets(groupKey);
            Debug.Assert(searchSingle.TotalCount == 1);
            Debug.Assert(searchSingle.Targets.Count() == 1);
            Debug.Assert(searchSingle.Targets.First().Key == createdTarget.Key);
            Debug.Assert(searchSingle.Targets.First().Name == createdTarget.Name);

            createdTarget.Tags.Add("Foo", "Bar");
            targetsController.UpdateTarget(createdTarget);

            var taggedTarget = targetsController.GetTarget(testTarget.Key);
            Debug.Assert(taggedTarget.GroupKey == createdTarget.GroupKey);
            Debug.Assert(taggedTarget.Tags.ContainsKey("Foo"));
            Debug.Assert(taggedTarget.Tags["Foo"] == "Bar");
            var searchUpdated = targetsController.SearchGroupTargets(groupKey);
            Debug.Assert(searchUpdated.TotalCount == 1);
            Debug.Assert(searchUpdated.Targets.Count() == 1);
            Debug.Assert(searchUpdated.Targets.First().Key == createdTarget.Key);
            Debug.Assert(searchUpdated.Targets.First().Name == createdTarget.Name);

            taggedTarget.Name = "Updated Test Target";
            targetsController.UpdateTarget(taggedTarget);

            var renamedTarget = targetsController.GetTarget(testTarget.Key);
            Debug.Assert(renamedTarget.Name == taggedTarget.Name);
            Debug.Assert(renamedTarget.GroupKey == taggedTarget.GroupKey);
            var searchRenamed = targetsController.SearchGroupTargets(groupKey);
            Debug.Assert(searchRenamed.TotalCount == 1);
            Debug.Assert(searchRenamed.Targets.Count() == 1);
            Debug.Assert(searchRenamed.Targets.First().Key == taggedTarget.Key);
            Debug.Assert(searchRenamed.Targets.First().Name == taggedTarget.Name);

            Searching(groupKey, targetsController, createdTarget);

            targetsController.UpdateTarget(testTarget);
            var searchReset = targetsController.SearchGroupTargets(groupKey);
            Debug.Assert(searchReset.TotalCount == 1);
            Debug.Assert(searchReset.Targets.Count() == 1);
            Debug.Assert(searchReset.Targets.First().Key == testTarget.Key);
            Debug.Assert(searchReset.Targets.First().Name == testTarget.Name);

            // Create default test target.
            targetsController.CreateTarget(new Target() { Key = new Guid("2c50a0af-bc66-41f5-bf52-498118217d12"), GroupKey = new Guid("5615c002-2d9a-46c4-a9a3-26b2b19cd790"), Name = "Test App Server" });

            return testTarget;
        }
Пример #3
0
        private static void Searching(Guid groupKey, Targets targetsController, Target createdTarget)
        {
            var aSecondTarget = new Target() { Name = "A Second Target", GroupKey = groupKey };
            targetsController.CreateTarget(aSecondTarget);
            var zThirdTarget = new Target() { Name = "Z Third Target", GroupKey = groupKey };
            targetsController.CreateTarget(zThirdTarget);

            var search1 = targetsController.SearchGroupTargets(groupKey);
            Debug.Assert(search1.TotalCount == 3);
            Debug.Assert(search1.Targets.Count() == 3);
            Debug.Assert(search1.Targets.ElementAt(0).Key == aSecondTarget.Key);
            Debug.Assert(search1.Targets.ElementAt(1).Key == createdTarget.Key);
            Debug.Assert(search1.Targets.ElementAt(2).Key == zThirdTarget.Key);

            var search2 = targetsController.SearchGroupTargets(groupKey, "Updated");
            Debug.Assert(search2.TotalCount == 1);
            Debug.Assert(search2.Targets.Count() == 1);
            Debug.Assert(search2.Targets.ElementAt(0).Key == createdTarget.Key);

            var search3 = targetsController.SearchGroupTargets(groupKey, pageSize: 1);
            Debug.Assert(search3.TotalCount == 3);
            Debug.Assert(search3.Targets.Count() == 1);
            Debug.Assert(search3.Targets.ElementAt(0).Key == aSecondTarget.Key);

            var search4 = targetsController.SearchGroupTargets(groupKey, pageSize: 2);
            Debug.Assert(search4.TotalCount == 3);
            Debug.Assert(search4.Targets.Count() == 2);
            Debug.Assert(search4.Targets.ElementAt(0).Key == aSecondTarget.Key);
            Debug.Assert(search4.Targets.ElementAt(1).Key == createdTarget.Key);

            var search5 = targetsController.SearchGroupTargets(groupKey, offset: 1);
            Debug.Assert(search5.TotalCount == 3);
            Debug.Assert(search5.Targets.Count() == 2);
            Debug.Assert(search5.Targets.ElementAt(0).Key == createdTarget.Key);
            Debug.Assert(search5.Targets.ElementAt(1).Key == zThirdTarget.Key);

            var search6 = targetsController.SearchGroupTargets(groupKey, offset: 1, pageSize: 1);
            Debug.Assert(search6.TotalCount == 3);
            Debug.Assert(search6.Targets.Count() == 1);
            Debug.Assert(search6.Targets.ElementAt(0).Key == createdTarget.Key);

            targetsController.DeleteTarget(aSecondTarget.Key);
            targetsController.DeleteTarget(zThirdTarget.Key);

            var searchDeleted = targetsController.SearchGroupTargets(groupKey);
            Debug.Assert(searchDeleted.TotalCount == 1);
            Debug.Assert(searchDeleted.Targets.Count() == 1);
            Debug.Assert(searchDeleted.Targets.First().Key == createdTarget.Key);
        }
Пример #4
0
        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);
                }
            }
        }
Пример #5
0
        public void UpdateTarget(Target target)
        {
            if (target == null)
                throw new ArgumentNullException("target", "Target cannot be null.");

            var existingTarget = GetTarget(target.Key);
            // Don't allow moving between groups.
            target.GroupKey = existingTarget.GroupKey;

            using (var stream = target.Serialise())
            {
                try
                {
                    using (var client = new AmazonS3Client(Context.AwsAccessKeyId, Context.AwsSecretAccessKey))
                    {
                        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,
                        })) { }

                        var indexesController = new Internal.Indexes(Context);
                        string indexPath = GetGroupTargetsIndexPath(target.GroupKey);
                        indexesController.PutIndexEntry(indexPath, new Internal.EntityIndexEntry() { Key = target.Key, Name = target.Name });
                    }
                }
                catch (AmazonS3Exception awsEx)
                {
                    throw new DeploymentException("Failed updating target.", awsEx);
                }
            }
        }
Пример #6
0
        public static Stream Serialise(Target target)
        {
            if (target == null)
                throw new ArgumentNullException("target", "Target cannot be null.");
            if (!Validation.IsNameValid(target.Name))
                throw new ArgumentException("Name must be valid (not blank & only a single line).");

            var doc = new XDocument(
                new XDeclaration("1.0", "UTF-8", "yes"),
                new XElement("target",
                    new XAttribute("key", target.Key),
                    new XElement("groupKey", target.GroupKey),
                    new XElement("name", target.Name),
                    new XElement("tags")));

            if (target.Tags != null && target.Tags.Count > 0)
            {
                doc.Root.Element("tags").Add(
                    target.Tags.Select(t =>
                        new XElement("tag",
                            new XAttribute("key", t.Key),
                            t.Value
                            )));
            }

            return Serialisation.Serialise(doc);
        }
Пример #7
0
        public static Target Parse(XmlReader source)
        {
            XDocument doc;
            try
            {
                doc = XDocument.Load(source);
            }
            catch (Exception ex)
            {
                throw new DeserialisationException("Failed deserialising target.", ex);
            }

            if (!ValidateTargetXml(doc))
                throw new DeserialisationException("Serialised target xml is not valid.");

            Guid key, groupKey;

            if (!Guid.TryParse(doc.Root.Attribute("key").Value, out key))
                throw new DeserialisationException("Serialised target key is not a valid guid.");
            if (!Guid.TryParse(doc.Root.Element("groupKey").Value, out groupKey))
                throw new DeserialisationException("Serialised target group key is not a valid guid.");

            var target = new Target()
            {
                Key = key,
                GroupKey = groupKey,
                Name = doc.Root.Element("name").Value,
            };

            var tagsElement = doc.Root.Element("tags");
            if (tagsElement != null && tagsElement.HasElements)
            {
                target.Tags = tagsElement.Elements("tag").ToDictionary(t => t.Attribute("key").Value, t => t.Value);
            }
            else
            {
                target.Tags = new Dictionary<string, string>();
            }

            return target;
        }