List of usage examples for com.amazonaws.services.ec2 AmazonEC2Client deleteTags
@Override
public DeleteTagsResult deleteTags(DeleteTagsRequest request)
Deletes the specified set of tags from the specified set of resources.
From source file:com.pearson.eidetic.aws.EC2ClientMethods.java
public static void deleteTags(AmazonEC2Client ec2Client, DeleteTagsRequest deleteTagsRequest, Integer numRetries, Integer maxApiRequestsPerSecond, String uniqueAwsAccountIdentifier) { for (int i = 0; i <= numRetries; i++) { try {//from w w w. j ava 2 s. c om // if the initial download attempt failed, wait for i * 500ms if (i > 0) { long sleepTimeInMilliseconds = 500 * i; Threads.sleepMilliseconds(sleepTimeInMilliseconds); } AtomicLong requestAttemptCounter = GlobalVariables.apiRequestAttemptCountersByAwsAccount .get(uniqueAwsAccountIdentifier); long currentRequestCount = requestAttemptCounter.incrementAndGet(); while (currentRequestCount > maxApiRequestsPerSecond) { Threads.sleepMilliseconds(50); currentRequestCount = requestAttemptCounter.incrementAndGet(); } try { ec2Client.deleteTags(deleteTagsRequest); try { GlobalVariables.apiRequestCountersByAwsAccount.get(uniqueAwsAccountIdentifier) .incrementAndGet(); } catch (Exception e) { logger.error("awsAccountNickname=\"" + uniqueAwsAccountIdentifier + "\"," + deleteTagsRequest.toString() + System.lineSeparator() + e.toString() + System.lineSeparator() + StackTrace.getStringFromStackTrace(e)); } break; } catch (Exception e) { logger.error("awsAccountNickname=\"" + uniqueAwsAccountIdentifier + "\"," + deleteTagsRequest.toString() + System.lineSeparator() + e.toString() + System.lineSeparator() + StackTrace.getStringFromStackTrace(e)); } } catch (Exception e) { logger.error("awsAccountNickname=\"" + uniqueAwsAccountIdentifier + "\"," + deleteTagsRequest.toString() + System.lineSeparator() + e.toString() + System.lineSeparator() + StackTrace.getStringFromStackTrace(e)); } } }
From source file:dsmwatcher.DSMWatcher.java
License:Open Source License
public void removeIsolation(Instance instance, AmazonEC2Client ec2) throws Exception { List<InstanceNetworkInterface> ienis = instance.getNetworkInterfaces(); for (InstanceNetworkInterface ieni : ienis) { DescribeNetworkInterfacesRequest netReq = new DescribeNetworkInterfacesRequest() .withNetworkInterfaceIds(ieni.getNetworkInterfaceId()); DescribeNetworkInterfacesResult netResult = ec2.describeNetworkInterfaces(netReq); List<com.amazonaws.services.ec2.model.NetworkInterface> enis = netResult.getNetworkInterfaces(); for (com.amazonaws.services.ec2.model.NetworkInterface eni : enis) { List<Tag> tagSet = eni.getTagSet(); List<Tag> tagSetRemove = new LinkedList<Tag>(); boolean isolatedENI = false; boolean IRENI = false; String origSecGroups = null; List<String> origSecGroupsList = new ArrayList<String>(); for (Tag tag : tagSet) { if (tag.getKey().compareTo("PreIsolationSG") == 0) { origSecGroups = tag.getValue(); tagSetRemove.add(tag); isolatedENI = true;//from w w w . j av a2s . c om } else if (tag.getKey().compareTo("InIsolation") == 0) { tagSetRemove.add(tag); } else if (tag.getKey().compareTo("IRENI") == 0) { IRENI = true; } } if (isolatedENI) { for (String s : origSecGroups.split(",")) { origSecGroupsList.add(s); } ModifyNetworkInterfaceAttributeRequest netReqest = new ModifyNetworkInterfaceAttributeRequest() .withNetworkInterfaceId(eni.getNetworkInterfaceId()).withGroups(origSecGroupsList); ec2.modifyNetworkInterfaceAttribute(netReqest); DeleteTagsRequest dtr = new DeleteTagsRequest().withResources(eni.getNetworkInterfaceId()) .withTags(tagSetRemove); ec2.deleteTags(dtr); } if (IRENI) { DetachNetworkInterfaceRequest detachNetworkInterfaceRequest = new DetachNetworkInterfaceRequest() .withAttachmentId(eni.getAttachment().getAttachmentId()); ec2.detachNetworkInterface(detachNetworkInterfaceRequest); TimeUnit.SECONDS.sleep(30); if (eni.getStatus().compareTo("available") != 0) { //detach is taking awhile, wait another 30 seconds TimeUnit.SECONDS.sleep(30); } DeleteNetworkInterfaceRequest deleteNetworkInterfaceRequest = new DeleteNetworkInterfaceRequest() .withNetworkInterfaceId(eni.getNetworkInterfaceId()); ec2.deleteNetworkInterface(deleteNetworkInterfaceRequest); } } } log("Instance " + instance.getInstanceId() + " with IP address of " + instance.getPrivateIpAddress() + " has been removed from isolation"); }
From source file:org.elasticdroid.model.ControlInstancesModel.java
License:Open Source License
public Object deleteTags(List<String> instances) { //create credentials using the BasicAWSCredentials class BasicAWSCredentials credentials = new BasicAWSCredentials(connectionData.get("accessKey"), connectionData.get("secretAccessKey")); //create Amazon EC2 Client object, and set tye end point to the region. params[3] //contains endpoint AmazonEC2Client amazonEC2Client = new AmazonEC2Client(credentials); //override the default connection endpoint if provided. if (connectionData.get("endpoint") != null) { amazonEC2Client.setEndpoint(connectionData.get("endpoint")); }/*from w w w. ja v a 2s . c om*/ //create empty tags for each of the instances from which the name tag is to be deleted. for (String instance : instances) { Log.v(TAG, "Tagging " + instance); //create a tag with Name for each instance from which Name tag is to be deleted. ec2Tags.add(new Tag("Name")); } DeleteTagsRequest request = new DeleteTagsRequest(instances); request.setTags(ec2Tags); //okay, tag the instance try { amazonEC2Client.deleteTags(request); } catch (AmazonServiceException amazonServiceException) { return amazonServiceException; } catch (AmazonClientException amazonClientException) { return amazonClientException; } return new Boolean(true); //return true to indicate success! }