Пример #1
0
        static void DescribeSnapshots(AmazonEC2Client client, Volume volume)
        {
            int generation           = 7;
            var describeSnapshotsReq = new DescribeSnapshotsRequest()
            {
                Filters = new List <Filter>()
                {
                    new Filter()
                    {
                        Name   = "volume-id",
                        Values = new List <string>()
                        {
                            volume.VolumeId
                        }
                    }
                }
            };
            var resp      = client.DescribeSnapshotsAsync(describeSnapshotsReq);
            var snapshots = resp.Result.Snapshots.OrderBy(i => i.StartTime).ToList();

            if (snapshots.Count >= generation)
            {
                var req = new DeleteSnapshotRequest(snapshots.First().SnapshotId);
                client.DeleteSnapshotAsync(req).Wait();
            }
        }
Пример #2
0
        public override void Invoke(AWSCredentials creds, RegionEndpoint region, int maxItems)
        {
            AmazonEC2Config config = new AmazonEC2Config();

            config.RegionEndpoint = region;
            ConfigureClient(config);
            AmazonEC2Client client = new AmazonEC2Client(creds, config);

            DescribeSnapshotsResponse resp = new DescribeSnapshotsResponse();

            do
            {
                DescribeSnapshotsRequest req = new DescribeSnapshotsRequest
                {
                    NextToken = resp.NextToken
                    ,
                    MaxResults = maxItems
                };

                resp = client.DescribeSnapshots(req);
                CheckError(resp.HttpStatusCode, "200");

                foreach (var obj in resp.Snapshots)
                {
                    AddObject(obj);
                }
            }while (!string.IsNullOrEmpty(resp.NextToken));
        }
Пример #3
0
        private async Task UntilSnapshotStateAsync(string snapshotId, string state, CancellationToken?cancellationToken = null)
        {
            CancellationToken token = cancellationToken.HasValue ? cancellationToken.Value : new CancellationToken();

            var describeSnapshotsRequest = new DescribeSnapshotsRequest()
            {
                SnapshotIds = new List <string>()
                {
                    snapshotId
                },
            };

            while (true)
            {
                token.ThrowIfCancellationRequested();

                var describeSnapshotsResponse = await this.Client.DescribeSnapshotsAsync(describeSnapshotsRequest);

                var status = describeSnapshotsResponse.Snapshots.FirstOrDefault(x => x.SnapshotId == snapshotId).State;

                if (status == state)
                {
                    break;
                }
                else
                {
                    await Task.Delay(1000, token);
                }
            }
        }
Пример #4
0
        private static async Task DeleteOrphanSnapshots()
        {
            var client = new AmazonEC2Client(RegionEndpoint.APSoutheast2);
            var describeSnapshotRequest = new DescribeSnapshotsRequest();

            describeSnapshotRequest.OwnerIds.Add(ownerId);

            var snapshots = await client.DescribeSnapshotsAsync(describeSnapshotRequest);

            var describeImagesRequest = new DescribeImagesRequest();

            describeImagesRequest.Owners.Add(ownerId);

            var images = await client.DescribeImagesAsync(describeImagesRequest);

            const int numberOfdaysToKeepImage = 30;

            foreach (var snapshot in snapshots.Snapshots.Where(s =>
                                                               !images.Images.Any(i => i.BlockDeviceMappings.Any(d => d.Ebs.SnapshotId == s.SnapshotId)) &&
                                                               DateTime.Today.Subtract(s.StartTime).Days > numberOfdaysToKeepImage))
            {
                Console.WriteLine($"Deleting snapshot {snapshot.Description}...");
                await client.DeleteSnapshotAsync(new DeleteSnapshotRequest(snapshot.SnapshotId));
            }
        }
Пример #5
0
        public override void Execute()
        {
            AmazonEC2Client           client   = new AmazonEC2Client(AWSAuthConnection.OUR_ACCESS_KEY_ID, AWSAuthConnection.OUR_SECRET_ACCESS_KEY);
            DescribeSnapshotsRequest  request  = new DescribeSnapshotsRequest();
            DescribeSnapshotsResponse response = client.DescribeSnapshots(request);

            Dictionary <string, List <Amazon.EC2.Model.Snapshot> > snapshots = new Dictionary <string, List <Amazon.EC2.Model.Snapshot> >();

            foreach (Amazon.EC2.Model.Snapshot r in response.DescribeSnapshotsResult.Snapshot)
            {
                if (!snapshots.ContainsKey(r.VolumeId))
                {
                    snapshots[r.VolumeId] = new List <Amazon.EC2.Model.Snapshot>();
                }

                snapshots[r.VolumeId].Add(r);
            }

            foreach (string volumeId in snapshots.Keys)
            {
                Console.WriteLine("--- Volume: {0}", volumeId);
                snapshots[volumeId].Sort(delegate(Amazon.EC2.Model.Snapshot x, Amazon.EC2.Model.Snapshot y)
                                         { return(DateTime.Parse(x.StartTime).CompareTo(DateTime.Parse(y.StartTime))); });

                foreach (Amazon.EC2.Model.Snapshot s in snapshots[volumeId])
                {
                    DateTime startTime = DateTime.Parse(s.StartTime);
                    Console.Write("{0}\t{1}\t{2}\t{3}", s.SnapshotId, startTime, s.Progress, s.Status);
                    Console.WriteLine();
                }

                Console.WriteLine();
            }
        }
        internal DescribeSnapshotsResponse DescribeSnapshots(DescribeSnapshotsRequest request)
        {
            var marshaller   = new DescribeSnapshotsRequestMarshaller();
            var unmarshaller = DescribeSnapshotsResponseUnmarshaller.Instance;

            return(Invoke <DescribeSnapshotsRequest, DescribeSnapshotsResponse>(request, marshaller, unmarshaller));
        }
Пример #7
0
        public void TestDescribeSnapshots()
        {
            DiskClient diskClient            = GetDiskClient();
            DescribeSnapshotsRequest request = new DescribeSnapshotsRequest();

            request.RegionId   = "cn-north-1";
            request.PageSize   = 100;
            request.PageNumber = 1;

            Filter snpFilter = new Filter();

            snpFilter.Name = "diskId";
            List <string> val1 = new List <string>();

            val1.Add("vol-bwxyeo32bv");
            snpFilter.Values = val1;
            //Filter stateFilter = new Filter();
            //stateFilter.Name = "status";
            //List<String> val2 = new List<String>();
            //val2.Add("available");
            //stateFilter.Values = val2;
            request.Filters = new List <Filter>();
            request.Filters.Add(snpFilter);
            // request.Filters.Add(stateFilter);
            var result = diskClient.DescribeSnapshots(request);

            _output.WriteLine(JsonConvert.SerializeObject(result));
        }
Пример #8
0
        /// <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);
                    }
                }
            }
        }
Пример #9
0
        private DescribeSnapshotsResponse GetSnapshots()
        {
            DescribeSnapshotsRequest request = new DescribeSnapshotsRequest()
                                               .WithOwner("self");
            DescribeSnapshotsResponse resp = ec2.DescribeSnapshots(request);

            return(resp);
        }
        /// <summary>
        /// Initiates the asynchronous execution of the DescribeSnapshots operation.
        /// </summary>
        ///
        /// <param name="request">Container for the necessary parameters to execute the DescribeSnapshots operation.</param>
        /// <param name="cancellationToken">
        ///     A cancellation token that can be used by other objects or threads to receive notice of cancellation.
        /// </param>
        /// <returns>The task object representing the asynchronous operation.</returns>
        public Task <DescribeSnapshotsResponse> DescribeSnapshotsAsync(DescribeSnapshotsRequest request, System.Threading.CancellationToken cancellationToken = default(CancellationToken))
        {
            var marshaller   = new DescribeSnapshotsRequestMarshaller();
            var unmarshaller = DescribeSnapshotsResponseUnmarshaller.Instance;

            return(InvokeAsync <DescribeSnapshotsRequest, DescribeSnapshotsResponse>(request, marshaller,
                                                                                     unmarshaller, cancellationToken));
        }
Пример #11
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);
        }
Пример #12
0
        DescribeSnapshotsResponse GetSnapshots(List <Filter> searchFilters)
        {
            DescribeSnapshotsResponse result = null;

            using (AmazonEC2Client EC2client = new AmazonEC2Client(creds))
            {
                DescribeSnapshotsRequest rq = new DescribeSnapshotsRequest();
                rq.Filters = searchFilters;
                result     = EC2client.DescribeSnapshots(rq);
            }

            return(result);
        }
Пример #13
0
 /// <summary>
 /// 本接口(DescribeSnapshots)用于查询快照的详细信息。
 /// 
 /// * 根据快照ID、创建快照的云硬盘ID、创建快照的云硬盘类型等对结果进行过滤,不同条件之间为与(AND)的关系,过滤信息详细请见过滤器`Filter`。
 /// *  如果参数为空,返回当前用户一定数量(`Limit`所指定的数量,默认为20)的快照列表。
 /// </summary>
 /// <param name="req">参考<see cref="DescribeSnapshotsRequest"/></param>
 /// <returns>参考<see cref="DescribeSnapshotsResponse"/>实例</returns>
 public async Task<DescribeSnapshotsResponse> DescribeSnapshots(DescribeSnapshotsRequest req)
 {
      JsonResponseModel<DescribeSnapshotsResponse> rsp = null;
      try
      {
          var strResp = await this.InternalRequest(req, "DescribeSnapshots");
          rsp = JsonConvert.DeserializeObject<JsonResponseModel<DescribeSnapshotsResponse>>(strResp);
      }
      catch (JsonSerializationException e)
      {
          throw new TencentCloudSDKException(e.Message);
      }
      return rsp.Response;
 }
Пример #14
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);
        }
Пример #15
0
        internal static void WriteSnapshots(IAmazonEC2 ec2, DateTime captureTime, string accountId, string region)
        {
            var describeSnapshotRequest = new DescribeSnapshotsRequest();
            var filters = new List <Filter>();
            var filter  = new Filter();

            filter.Name   = "owner-id";
            filter.Values = new List <string> {
                accountId
            };
            filters.Add(filter);
            describeSnapshotRequest.Filters = filters;
            var ssResponse = ec2.DescribeSnapshots(describeSnapshotRequest);

            foreach (var snapshot in ssResponse.Snapshots)
            {
                string snapshotJson = JsonConvert.SerializeObject(snapshot);
                Common.UpdateTopology(captureTime, accountId, region, "ss", snapshot.SnapshotId, snapshotJson, "UPDATE");
            }
        }
Пример #16
0
        public async Task <List <SA_Snapshot> > GetAllSnapshots()
        {
            var ret     = new List <SA_Snapshot>();
            var request = new DescribeSnapshotsRequest();

            request.OwnerIds = CredentiaslManager.GlobalAccountsCache;
            var response = await client.DescribeSnapshotsAsync(request);

            foreach (var snapshot in response.Snapshots)
            {
                ret.Add(new SA_Snapshot()
                {
                    Name         = snapshot.Tags.Find(o => o.Key == "Name").Value,
                    OwnerId      = snapshot.OwnerId,
                    SnapshotId   = snapshot.SnapshotId,
                    StartTime    = snapshot.StartTime,
                    VolumeSize   = snapshot.VolumeSize,
                    StateMessage = snapshot.StateMessage
                });
            }
            return(ret);
        }
Пример #17
0
        public override void Execute()
        {
            Amazon.EC2.AmazonEC2Client client = new Amazon.EC2.AmazonEC2Client(AWSAuthConnection.OUR_ACCESS_KEY_ID, AWSAuthConnection.OUR_SECRET_ACCESS_KEY);
            List <string> snapshotsToDelete   = new List <string>();

            if (isVolumeId)
            {
                // delete snapshots belonging to this volume
                DescribeSnapshotsRequest  request  = new DescribeSnapshotsRequest();
                DescribeSnapshotsResponse response = client.DescribeSnapshots(request);

                foreach (Amazon.EC2.Model.Snapshot s in response.DescribeSnapshotsResult.Snapshot)
                {
                    if (string.Equals(s.VolumeId, id, StringComparison.InvariantCultureIgnoreCase))
                    {
                        DateTime snapshotDate = DateTime.Parse(s.StartTime);
                        if (snapshotDate.AddDays(days) < DateTime.Now)
                        {
                            snapshotsToDelete.Add(s.SnapshotId);
                        }
                    }
                }
            }
            else
            {
                snapshotsToDelete.Add(id);
            }

            foreach (string snapshotId in snapshotsToDelete)
            {
                Console.WriteLine("Deleting snapshot ID {0}", snapshotId);
                Amazon.EC2.Model.DeleteSnapshotRequest request = new Amazon.EC2.Model.DeleteSnapshotRequest();
                request.SnapshotId = snapshotId;
                //Amazon.EC2.Model.DeleteSnapshotResponse response =
                client.DeleteSnapshot(request);
            }
        }
        /// <summary>
        /// Return csv file content for EBS snapshot
        /// </summary>
        /// <returns>The images as csv.</returns>
        /// <param name="input">Input.</param>
        /// <param name="context">Context.</param>
        private String getImagesAsCsv(EBSReportInput input, ILambdaContext context)
        {
            String accountId           = context.InvokedFunctionArn.Split(':')[4];
            var    ec2Client           = new AmazonEC2Client();
            var    getSnapshotsRequest = new DescribeSnapshotsRequest();

            getSnapshotsRequest.MaxResults = 1000;
            //show only owned completed snapshots
            getSnapshotsRequest.Filters.Add(new Amazon.EC2.Model.Filter("status", new List <string> {
                "completed"
            }));
            getSnapshotsRequest.Filters.Add(new Amazon.EC2.Model.Filter("owner-id", new List <string> {
                accountId
            }));
            List <Snapshot> snapshots = new List <Snapshot>();

            do
            {
                var taskResponse = ec2Client.DescribeSnapshotsAsync(getSnapshotsRequest);
                taskResponse.Wait();
                snapshots.AddRange(taskResponse.Result.Snapshots);
                context.Logger.LogLine($"Added {taskResponse.Result.Snapshots.Count} snapshots to results list");
                getSnapshotsRequest.NextToken = taskResponse.Result.NextToken;
            } while (getSnapshotsRequest.NextToken != null);

            var awsCommon = new AWSCommon();
            var allAMIs   = awsCommon.GetAllOwnedPrivateAMIs(context);
            var sb        = new StringBuilder();

            sb.Append("DateCreated,SnapID,Name,Description,AMIRelated,AMIExists,AMI-ID,Tags\n");
            snapshots.Sort((s1, s2) => - 1 * s1.StartTime.CompareTo(s2.StartTime));
            snapshots.ForEach(snapshot => {
                var nameTag       = snapshot.Tags.Find(t => t.Key.Equals("Name"));
                var name          = nameTag != null ? nameTag.Value : "";
                var notNameTags   = String.Join(" ", snapshot.Tags.FindAll(t => t.Key != "Name").Select(t => $"{t.Key}={t.Value}"));
                bool isAmiRelated = false;
                bool amiExists    = false;
                String amiId      = "";
                //check if ebs snapshots is related to an AMI
                if (snapshot.Description != null)
                {
                    var amiRegex = new Regex("Created by (.*) for ami-(.*) from (.*)").Match(snapshot.Description);
                    isAmiRelated = amiRegex.Success;
                    amiId        = isAmiRelated ? "ami-" + amiRegex.Groups[2] : "";
                    amiExists    = allAMIs.Find(i => i.ImageId.Equals(amiId)) != null;
                }
                //if only orphans to be reported, check if orphan (related to ami, ami does not exist)
                if (!input.OnlyAMIOrphans)
                {
                    sb.Append($"{snapshot.StartTime},{snapshot.SnapshotId},{name},{snapshot.Description},{isAmiRelated},{amiExists},{amiId},{notNameTags}\n");
                }
                else if (isAmiRelated && !amiExists)
                {
                    sb.Append($"{snapshot.StartTime},{snapshot.SnapshotId},{name},{snapshot.Description},{isAmiRelated},{amiExists},{amiId},{notNameTags}\n");
                }
                else if (isAmiRelated && amiExists)
                {
                    context.Logger.LogLine($"Skipping snap {snapshot.SnapshotId} as AMI {amiId} exists");
                }
                else if (!isAmiRelated)
                {
                    context.Logger.LogLine($"Skipping snap {snapshot.SnapshotId} as non-ami related snapshot");
                }
            });

            return(sb.ToString());
        }
Пример #19
0
 /// <summary>
 ///  查询云硬盘快照列表,filters多个过滤条件之间是逻辑与(AND),每个条件内部的多个取值是逻辑或(OR)
 /// </summary>
 /// <param name="request">请求参数信息</param>
 /// <returns>请求结果信息</returns>
 public async Task <DescribeSnapshotsResponse> DescribeSnapshots(DescribeSnapshotsRequest request)
 {
     return(await new DescribeSnapshotsExecutor().Client(this).Execute <DescribeSnapshotsResponse, DescribeSnapshotsResult, DescribeSnapshotsRequest>(request).ConfigureAwait(false));
 }
Пример #20
0
 /// <summary>
 ///  查询云硬盘快照列表,filters多个过滤条件之间是逻辑与(AND),每个条件内部的多个取值是逻辑或(OR)
 /// </summary>
 /// <param name="request">请求参数信息</param>
 /// <returns>请求结果信息</returns>
 public DescribeSnapshotsResponse DescribeSnapshots(DescribeSnapshotsRequest request)
 {
     return(new DescribeSnapshotsExecutor().Client(this).Execute <DescribeSnapshotsResponse, DescribeSnapshotsResult, DescribeSnapshotsRequest>(request));
 }
Пример #21
0
 /// <summary>
 /// Describe Snapshots
 /// </summary>
 /// <param name="request">Describe Snapshots  request</param>
 /// <returns>Describe Snapshots  Response from the service</returns>
 /// <remarks>
 /// Describes the indicated snapshots, or in lieu of that, all snapshots owned by the caller.
 ///
 /// </remarks>
 public DescribeSnapshotsResponse DescribeSnapshots(DescribeSnapshotsRequest request)
 {
     return(Invoke <DescribeSnapshotsResponse>("DescribeSnapshotsResponse.xml"));
 }