Ejemplo n.º 1
0
        /// <summary>
        /// Removes all tags then adds all tags specified in the dictionary
        /// </summary>
        public static async Task <bool> UpdateTagsAsync(this EC2Helper ec2, string instanceId, Dictionary <string, string> tags, CancellationToken cancellationToken = default(CancellationToken))
        {
            var instance = await ec2.GetInstanceById(instanceId);

            var deleteTags = await ec2.DeleteAllInstanceTags(instanceId);

            if (tags.IsNullOrEmpty())
            {
                instance = await ec2.GetInstanceById(instanceId);

                return(instance.Tags.IsNullOrEmpty());
            }

            var createTags = await ec2.CreateTagsAsync(
                resourceIds : new List <string>()
            {
                instanceId
            },
                tags : tags);

            instance = await ec2.GetInstanceById(instanceId);

            return(instance.Tags.ToDictionary(x => x.Key, y => y.Value).CollectionEquals(tags, trim: true));
        }
Ejemplo n.º 2
0
        public static async Task <Dictionary <string, string> > GetEnvironmentTags(
            string instanceId    = null,
            bool throwIfNotFound = true,
            int timeoutSeconds   = 10)
        {
            if (instanceId.IsNullOrEmpty())
            {
                instanceId = await GetEnvironmentInstanceId(timeoutSeconds : (timeoutSeconds / 2));
            }

            //Console.WriteLine($"Fetching tag's from instance {instanceId ?? "undefined"} in region {region ?? "undefined"}");
            var ec2      = new EC2Helper();
            var instance = await ec2.GetInstanceById(instanceId : instanceId, throwIfNotFound : throwIfNotFound);

            return(instance?.Tags?.ToDictionary(x => x.Key, y => y.Value));
        }