/// <summary>
        /// Check for any snapshots set to expire -- that have a tag key of "expires" with a value that is in the past.
        /// </summary>
        public static void CheckForExpiredSnapshots()
        {
            Console.WriteLine("Checking for expired snapshots...");

            AmazonEC2 ec2 = Ec2Helper.CreateClient();

            DescribeSnapshotsRequest rq = new DescribeSnapshotsRequest();

            rq.WithOwner("self");
            rq.WithFilter(new Filter()
            {
                Name = "tag-key", Value = new List <string>()
                {
                    "expires"
                }
            });

            DescribeSnapshotsResponse rs = ec2.DescribeSnapshots(rq);

            foreach (Snapshot s in rs.DescribeSnapshotsResult.Snapshot)
            {
                string expireText = Ec2Helper.GetTagValue(s.Tag, "expires");

                DateTime expires;

                if (DateTime.TryParse(expireText, out expires))
                {
                    if (expires < DateTime.UtcNow)
                    {
                        Console.WriteLine("Deleting " + s.SnapshotId + " for " + s.VolumeId + "...");
                        Ec2Helper.DeleteSnapsot(s.SnapshotId);
                    }
                }
            }
        }
Exemple #2
0
        static IEnumerable <Snapshot> GetSnapshotsWithBackupDate()
        {
            AmazonEC2 ec2      = GetEC2Client();
            Filter    filter   = new Filter().WithName("tag:BackupDate").WithValue("*");
            var       request  = new DescribeSnapshotsRequest().WithFilter(filter);
            var       response = ec2.DescribeSnapshots(request);

            return(response.DescribeSnapshotsResult.Snapshot);
        }
Exemple #3
0
        static bool CheckSnapshotCompletion(string snapshotID)
        {
            bool      status   = false;
            AmazonEC2 ec2      = GetEC2Client();
            Filter    filter   = new Filter();
            var       request  = new DescribeSnapshotsRequest().WithSnapshotId(snapshotID);
            var       response = ec2.DescribeSnapshots(request);

            foreach (Snapshot s in response.DescribeSnapshotsResult.Snapshot)
            {
                if (s.SnapshotId == snapshotID)
                {
                    if (s.Status == "completed")
                    {
                        status = true;
                    }
                }
            }
            return(status);
        }
        /// <summary>
        /// Describes the indicated snapshots, or in lieu of that, all snapshots owned by the caller.
        /// 
        /// </summary>
        /// <param name="service">Instance of AmazonEC2 service</param>
        /// <param name="request">DescribeSnapshotsRequest request</param>
        public static void InvokeDescribeSnapshots(AmazonEC2 service, DescribeSnapshotsRequest request)
        {
            try 
            {
                DescribeSnapshotsResponse response = service.DescribeSnapshots(request);
                
                
                Console.WriteLine ("Service Response");
                Console.WriteLine ("=============================================================================");
                Console.WriteLine ();

                Console.WriteLine("        DescribeSnapshotsResponse");
                if (response.IsSetDescribeSnapshotsResult())
                {
                    Console.WriteLine("            DescribeSnapshotsResult");
                    DescribeSnapshotsResult  describeSnapshotsResult = response.DescribeSnapshotsResult;
                    List<Snapshot> snapshotList = describeSnapshotsResult.Snapshot;
                    foreach (Snapshot snapshot in snapshotList)
                    {
                        Console.WriteLine("                Snapshot");
                        if (snapshot.IsSetSnapshotId())
                        {
                            Console.WriteLine("                    SnapshotId");
                            Console.WriteLine("                        {0}", snapshot.SnapshotId);
                        }
                        if (snapshot.IsSetVolumeId())
                        {
                            Console.WriteLine("                    VolumeId");
                            Console.WriteLine("                        {0}", snapshot.VolumeId);
                        }
                        if (snapshot.IsSetStatus())
                        {
                            Console.WriteLine("                    Status");
                            Console.WriteLine("                        {0}", snapshot.Status);
                        }
                        if (snapshot.IsSetStartTime())
                        {
                            Console.WriteLine("                    StartTime");
                            Console.WriteLine("                        {0}", snapshot.StartTime);
                        }
                        if (snapshot.IsSetProgress())
                        {
                            Console.WriteLine("                    Progress");
                            Console.WriteLine("                        {0}", snapshot.Progress);
                        }
                    }
                }
                if (response.IsSetResponseMetadata())
                {
                    Console.WriteLine("            ResponseMetadata");
                    ResponseMetadata  responseMetadata = response.ResponseMetadata;
                    if (responseMetadata.IsSetRequestId())
                    {
                        Console.WriteLine("                RequestId");
                        Console.WriteLine("                    {0}", responseMetadata.RequestId);
                    }
                }

            } 
            catch (AmazonEC2Exception ex) 
            {
                Console.WriteLine("Caught Exception: " + ex.Message);
                Console.WriteLine("Response Status Code: " + ex.StatusCode);
                Console.WriteLine("Error Code: " + ex.ErrorCode);
                Console.WriteLine("Error Type: " + ex.ErrorType);
                Console.WriteLine("Request ID: " + ex.RequestId);
                Console.WriteLine("XML: " + ex.XML);
            }
        }