/** * Creates a snapshot and return the snapshot id. */ public static String createSnapshot(VerificationHost host, AmazonEC2Client client, String volumeId) { CreateSnapshotRequest req = new CreateSnapshotRequest() .withVolumeId(volumeId); CreateSnapshotResult res = client.createSnapshot(req); String snapshotId = res.getSnapshot().getSnapshotId(); Filter filter = new Filter().withName(SNAPSHOT_ID_ATTRIBUTE).withValues(snapshotId); DescribeSnapshotsRequest snapshotsRequest = new DescribeSnapshotsRequest() .withSnapshotIds(snapshotId) .withFilters(filter); host.waitFor("Timeout waiting for creating snapshot", () -> { DescribeSnapshotsResult snapshotsResult = client.describeSnapshots(snapshotsRequest); String state = snapshotsResult.getSnapshots().get(0).getState(); if (state.equalsIgnoreCase(SNAPSHOT_STATUS_COMPLETE)) { return true; } return false; }); return snapshotId; }
void attachSnapshotTags(AmazonEC2Async client, String sourceSnapshotId, String snapshotId) { DescribeSnapshotsResult result = client .describeSnapshots(new DescribeSnapshotsRequest().withSnapshotIds(sourceSnapshotId)); List<Snapshot> snapshots = result.getSnapshots(); if (snapshots.size() != 1) { throw new RuntimeException("snapshot can not found. sourceSnapshotId[" + snapshotId + "]"); } List<Tag> sourceSnapshotTags = snapshots.get(0).getTags(); List<Tag> tags = new ArrayList<Tag>(); tags.addAll(sourceSnapshotTags); tags.add(new Tag("SourceSnapshotId", sourceSnapshotId)); tags.add(new Tag("BackupType", "copy-snapshot")); // overwrite CreateTagsRequest snapshotTagsRequest = new CreateTagsRequest().withResources(snapshotId); snapshotTagsRequest.setTags(tags); client.createTags(snapshotTagsRequest); }
@Override public List<AbstractResource<?>> describeSnapshots(Account account, Region region, DateTime dt, Ec2Filter... filters) { AmazonEC2 ec2 = findClient(account, region); DescribeSnapshotsRequest req = new DescribeSnapshotsRequest(); for (Ec2Filter filter : filters) { Filter f = new Filter().withName(filter.getName()).withValues(filter.getValues()); req.withFilters(f); } log.debug("start describing snapshots for account:{} in region:{} via api", account.getId() + "=>" + account.getName(), region); DescribeSnapshotsResult res = ec2.describeSnapshots(req); List<List<CreateVolumePermission>> createVolumePermissions = new ArrayList<>(); List<List<ProductCode>> productCodes = new ArrayList<>(); for (Snapshot snapShot : res.getSnapshots()) { productCodes.add(findSnapshotProductCodes(account, region, snapShot.getSnapshotId())); createVolumePermissions.add(findCreateVolumePermissions(account, region, snapShot.getSnapshotId())); } return converter.toEc2Snapshots(res.getSnapshots(), createVolumePermissions, productCodes, account.getId(), region, dt); }
void pargeEbsSnapshot(AmazonEC2Async client, String volumeId, int generationCount, Context context) { LambdaLogger logger = context.getLogger(); logger.log("Parge snapshot start. VolumeId[" + volumeId + "] generationCount[" + generationCount + "]"); List<Filter> filters = new ArrayList<>(); filters.add(new Filter().withName("volume-id").withValues(volumeId)); filters.add(new Filter().withName("tag:VolumeId").withValues(volumeId)); filters.add(new Filter().withName("tag:BackupType").withValues("snapshot")); DescribeSnapshotsRequest snapshotRequest = new DescribeSnapshotsRequest().withFilters(filters); DescribeSnapshotsResult snapshotResult = client.describeSnapshots(snapshotRequest); List<Snapshot> snapshots = snapshotResult.getSnapshots(); Collections.sort(snapshots, new SnapshotComparator()); int snapshotSize = snapshots.size(); if (generationCount < snapshotSize) { for (int i = 0; i < snapshotSize - generationCount; i++) { Snapshot snapshot = snapshots.get(i); if (SnapshotState.Completed.toString().equals(snapshot.getState())) { String snapshotId = snapshot.getSnapshotId(); pargeSnapshot(client, snapshotId); logger.log("Parge EBS snapshot. snapshotId[" + snapshotId + "] volumeId[" + volumeId + "]"); } } } }
private List<Snapshot> describeSnapshot(AmazonEC2Async client, String imageId, Context context) { Filter filter = new Filter().withName("description") .withValues("Created by CreateImage(*) for " + imageId + " from *"); DescribeSnapshotsRequest request = new DescribeSnapshotsRequest().withFilters(filter); DescribeSnapshotsResult result = client.describeSnapshots(request); return result.getSnapshots(); }
void pargeEbsSnapshot(AmazonEC2Async client, String sourceSnapshotId, String snapshotId, int generationCount, Context context) { LambdaLogger logger = context.getLogger(); logger.log( "Parge snapshot start. SnapshotId[" + sourceSnapshotId + "] generationCount[" + generationCount + "]"); // get volumeId from tags String volumeId = getVolumeIdFromTag(client, snapshotId); // describe filter tag VolumeId List<Filter> filters = new ArrayList<>(); filters.add(new Filter().withName("tag:VolumeId").withValues(volumeId)); filters.add(new Filter().withName("tag:BackupType").withValues("copy-snapshot")); DescribeSnapshotsRequest snapshotRequest = new DescribeSnapshotsRequest().withFilters(filters); DescribeSnapshotsResult snapshotResult = client.describeSnapshots(snapshotRequest); // snapshot作成開始日でソートします。(古い→新しい) List<Snapshot> snapshots = snapshotResult.getSnapshots(); Collections.sort(snapshots, new SnapshotComparator()); // 世代管理保持数 < snapshotの数の場合、対象をpargeします。 int snapshotSize = snapshots.size(); if (generationCount < snapshotSize) { for (int i = 0; i < snapshotSize - generationCount; i++) { Snapshot snapshot = snapshots.get(i); // (念のため)snapshotのステータスが完了しているものだけをparge対象とする。 if (SnapshotState.Completed.toString().equals(snapshot.getState())) { String pargeSnapshotId = snapshot.getSnapshotId(); pargeSnapshot(client, pargeSnapshotId); logger.log("Parge EBS snapshot. snapshotId[" + pargeSnapshotId + "]"); } } } }
String getVolumeIdFromTag(AmazonEC2Async client, String snapshotId) { List<Tag> snapshotTag = client.describeSnapshots(new DescribeSnapshotsRequest().withSnapshotIds(snapshotId)) .getSnapshots().get(0).getTags(); String volumeId = null; for (Tag tag : snapshotTag) { if ("VolumeId".equals(tag.getKey())) { volumeId = tag.getValue(); } } if (volumeId == null) { throw new RuntimeException("volumeId can not found snapshot. snapshotId[" + snapshotId + "]"); } return volumeId; }
@Override public SnapshotCollection getSnapshots(DescribeSnapshotsRequest request) { ResourceCollectionImpl result = resource.getCollection("Snapshots", request); if (result == null) return null; return new SnapshotCollectionImpl(result); }
@Override public SnapshotCollection getSnapshots(DescribeSnapshotsRequest request) { ResourceCollectionImpl result = service.getCollection("Snapshots", request); if (result == null) return null; return new SnapshotCollectionImpl(result); }
@Override public DescribeSnapshotsResult describeSnapshots(DescribeSnapshotsRequest describeSnapshotsRequest) throws AmazonServiceException, AmazonClientException { throw new UnsupportedOperationException("Not supported in mock"); }
/** * Find the snapshot the volumeId the key, to get a copy of the latest * Snapshot * * @param volumeIdRequest * VolumeIdRequest * @param context * Context */ public void copySnapshotFromVolumeId(VolumeIdRequest volumeIdRequest, Context context) { LambdaLogger logger = context.getLogger(); String regionName = System.getenv("AWS_DEFAULT_REGION"); AmazonEC2Async client = RegionUtils.getRegion(regionName).createClient(AmazonEC2AsyncClient.class, new DefaultAWSCredentialsProviderChain(), cc); try { String volumeId = volumeIdRequest.getVolumeId(); boolean isLockAcquisition = new LambdaLock().lock(volumeId, context); if (!isLockAcquisition) { logger.log("[ERROR][EBSCopySnapshot][" + volumeId + "]You can not acquire a lock."); return; } List<Filter> filters = new ArrayList<>(); filters.add(new Filter().withName("volume-id").withValues(volumeId)); filters.add(new Filter().withName("tag:VolumeId").withValues(volumeId)); filters.add(new Filter().withName("tag:BackupType").withValues("snapshot")); DescribeSnapshotsRequest snapshotRequest = new DescribeSnapshotsRequest().withFilters(filters); DescribeSnapshotsResult snapshotResult = client.describeSnapshots(snapshotRequest); // sort and get latest snapshot List<Snapshot> snapshots = snapshotResult.getSnapshots(); Collections.sort(snapshots, new SnapshotComparator()); int snapshotsSize = snapshots.size(); if (snapshotsSize == 0) { throw new RuntimeException("The copy source snapshot can not be found"); } Snapshot snapshot = snapshots.get(snapshots.size() - 1); String sourceSnapshotId = snapshot.getSnapshotId(); copySnapshot(sourceSnapshotId, volumeIdRequest.getDestinationRegion(), volumeIdRequest.getGenerationCount(), context); } catch (Exception e) { logger.log("[ERROR][EBSCopySnapshot][" + volumeIdRequest.getVolumeId() + "] message[" + e.getMessage() + "] stackTrace[" + getStackTrace(e) + "]"); } finally { client.shutdown(); } }
@Override public SnapshotCollection getSnapshots() { return getSnapshots((DescribeSnapshotsRequest)null); }
@Override public boolean load(DescribeSnapshotsRequest request) { return load(request, null); }
@Override public boolean load(DescribeSnapshotsRequest request, ResultCapture<DescribeSnapshotsResult> extractor) { return resource.load(request, extractor); }
private DescribeSnapshotsRequest prepareDescribeSnapshotsRequest(AwsInstanceView awsInstanceView) { return new DescribeSnapshotsRequest().withFilters(prepareFilters(awsInstanceView)); }
@Override protected Boolean doCall() { LOGGER.info("Checking if AWS EBS snapshot '{}' is ready.", snapshotId); DescribeSnapshotsResult result = ec2Client.describeSnapshots(new DescribeSnapshotsRequest().withSnapshotIds(snapshotId)); return result.getSnapshots() != null && !result.getSnapshots().isEmpty() && "completed".equals(result.getSnapshots().get(0).getState()); }
/** * Makes a call to the service to load this resource's attributes if they * are not loaded yet. * The following request parameters will be populated from the data of this * <code>Snapshot</code> resource, and any conflicting parameter value set * in the request will be overridden: * <ul> * <li> * <b><code>SnapshotIds.0</code></b> * - mapped from the <code>Id</code> identifier. * </li> * </ul> * * <p> * * @return Returns {@code true} if the resource is not yet loaded when this * method was invoked, which indicates that a service call has been * made to retrieve the attributes. * @see DescribeSnapshotsRequest */ boolean load(DescribeSnapshotsRequest request);
/** * Makes a call to the service to load this resource's attributes if they * are not loaded yet, and use a ResultCapture to retrieve the low-level * client response * The following request parameters will be populated from the data of this * <code>Snapshot</code> resource, and any conflicting parameter value set * in the request will be overridden: * <ul> * <li> * <b><code>SnapshotIds.0</code></b> * - mapped from the <code>Id</code> identifier. * </li> * </ul> * * <p> * * @return Returns {@code true} if the resource is not yet loaded when this * method was invoked, which indicates that a service call has been * made to retrieve the attributes. * @see DescribeSnapshotsRequest */ boolean load(DescribeSnapshotsRequest request, ResultCapture<DescribeSnapshotsResult> extractor);
/** * Retrieves the Snapshots collection referenced by this resource. */ SnapshotCollection getSnapshots(DescribeSnapshotsRequest request);