private GetChangeResult getChange(GetChangeRequest request) throws ErrorResponse{ AccessMySQL sqlaccess = AccessMySQL.getInstance(); GetChangeResult result = new GetChangeResult(); String[] changeRecord = sqlaccess.getChange(request.getId()); if(changeRecord[0] == null){ //result empty -> changeID not existing -> AccessDenied throw DNS53Faults.NoSuchChange(request.getId()); } ChangeInfo ci = new ChangeInfo(); ci.setId(changeRecord[0]); ci.setStatus(changeRecord[1]); String date = changeRecord[2]; Date submittedAt = new Date(date); ci.setSubmittedAt(submittedAt); result.setChangeInfo(ci); return result; }
private DeleteHostedZoneResult deleteHostedZone(DeleteHostedZoneRequest request) throws ErrorResponse{ String zoneId = request.getId(); logger.debug("Delete target: " + zoneId); Date submittedAt = new Date(); DeleteHostedZoneResult result = new DeleteHostedZoneResult(); AccessMySQL sqlaccess = AccessMySQL.getInstance(); String tableName = sqlaccess.getTableName(zoneId, this.getAccountId()); if(tableName.equals("FAILED")){ throw DNS53Faults.NoSuchHostedZone(zoneId); }else{ String callerReference = sqlaccess.getCallerReference(zoneId, this.getAccountId()); sqlaccess.deleteHostedZone(zoneId, tableName, callerReference); String changeID = RequestHandler.writeChange(sqlaccess, "PENDING", submittedAt.toString(), tableName, "DELETE"); ChangeInfo changeInfo = new ChangeInfo(); changeInfo.setId(changeID); changeInfo.setStatus("PENDING"); changeInfo.setSubmittedAt(submittedAt); result.setChangeInfo(changeInfo); } return result; }
public static ChangeInfo attachDomainToEip(AmazonRoute53 route53, String publicIp, String domain) { String hostedZoneId = findHostedZoneForDomain(route53, domain); if (hostedZoneId != null) { System.out.println("Found HostedZone's Id(" + hostedZoneId + ")"); } else { return null; } ResourceRecordSet resourceRecordSet = generateResourceRecordSetForARecord(publicIp, domain); ChangeInfo changeinfo = AwsRoute53Client.changeResourceRecordSet(route53, hostedZoneId, resourceRecordSet, ChangeAction.CREATE); return changeinfo; }
public static ChangeInfo changeResourceRecordSet(AmazonRoute53 route53, String hostedZoneId, ResourceRecordSet resourceRecordSet, ChangeAction changeAction) { Change change = new Change(changeAction, resourceRecordSet); ChangeBatch changeBatch = new ChangeBatch().withChanges(change); ChangeResourceRecordSetsRequest changeResourceRecordSetsRequest = new ChangeResourceRecordSetsRequest( hostedZoneId, changeBatch); ChangeResourceRecordSetsResult changeResourceRecordSetsResult = route53 .changeResourceRecordSets(changeResourceRecordSetsRequest); ChangeInfo changeinfo = changeResourceRecordSetsResult.getChangeInfo(); return changeinfo; }
public GetChangeResult getChange(GetChangeRequest req) throws AmazonServiceException, AmazonClientException{ Client c = Client.create(); WebResource r = c.resource(this.changeURL); String resultXml = r .path(req.getId()) .type(MediaType.APPLICATION_XML_TYPE) .accept(MediaType.TEXT_XML) .header("X-Amzn-Authorization", "AWS3 AWSAccessKeyId=" + this.accessKey + "," + "Algorithm=HmacSHA256," + "SignedHeaders=Host;X-Amz-Date," + "Signature=THISISANEXAMPLESIGNATURE=") .get(String.class); GetChangeResponsePOJO interResult = null; try { StringReader reader = new StringReader(resultXml); JAXBContext context = JAXBContext.newInstance(GetChangeResponsePOJO.class); Unmarshaller unmarshaller = context.createUnmarshaller(); interResult = (GetChangeResponsePOJO) unmarshaller.unmarshal(reader); } catch (JAXBException e) { e.printStackTrace(); return null; } if(interResult == null){ return null; } GetChangeResult result = new GetChangeResult(); if(interResult.getChangeInfo() != null){ ChangeInfo ci = new ChangeInfo(); ci.setComment(interResult.getChangeInfo().getComment()); ci.setId(interResult.getChangeInfo().getId()); ci.setStatus(interResult.getChangeInfo().getStatus()); ci.setSubmittedAt(interResult.getChangeInfo().getSubmittedAt()); result.setChangeInfo(ci); } return result; }
@Override public String marshall(MarshallStruct<GetChangeResult> input, HttpServletResponse resp) throws Exception { GetChangeResult result = input.getMainObject(); ChangeInfo ci = result.getChangeInfo(); XMLNode response = new XMLNode(DNS53Constants.GETCHANGERESPONSE); response.addAttr(DNS53Constants.XMLNS, DNS53Constants.XMLNS_VALUE); if(ci != null){ DNS53QueryUtil.marshallChangeInfo(ci, response); } return response.toString(); }
@Override public String marshall( MarshallStruct<ChangeResourceRecordSetsResult> input, HttpServletResponse resp) throws Exception { ChangeResourceRecordSetsResult result = input.getMainObject(); ChangeInfo ci = result.getChangeInfo(); XMLNode response = new XMLNode(DNS53Constants.CHANGERESOURCERECORDSETSRESPONSE); response.addAttr(DNS53Constants.XMLNS, DNS53Constants.XMLNS_VALUE); if(ci != null){ DNS53QueryUtil.marshallChangeInfo(ci, response); } return response.toString(); }
@Override public String marshall(MarshallStruct<DeleteHostedZoneResult> input, HttpServletResponse resp) throws Exception { DeleteHostedZoneResult result = input.getMainObject(); ChangeInfo ci = result.getChangeInfo(); XMLNode response = new XMLNode(DNS53Constants.DELETEHOSTEDZONERESPONSE); response.addAttr(DNS53Constants.XMLNS, DNS53Constants.XMLNS_VALUE); if(ci != null){ DNS53QueryUtil.marshallChangeInfo(ci, response); } return response.toString(); }
private int startByName(Ec2CommandOptions options) throws FileNotFoundException { String name = options.getName(); InputStream inputStream = new FileInputStream(new File(options.getCredentialsPath())); ConfigProvider.loadConfigure(inputStream); AmazonEC2 ec2 = AwsEc2Client.getEc2(); // Check Exists Instance Instance instance = AwsEc2Client.findInstanceByName(ec2, name); if (instance == null) { System.err.println("Not exists instance (name = " + name + ")."); return 2; } String instanceId = instance.getInstanceId(); System.out.println("Exists instance (id = " + instanceId + ")"); // Start Ec2 Instance InstanceStateChange stateChange = AwsEc2Client.startInstance(ec2, instanceId); AwsEc2Client.showStateChange(stateChange, "Starting Instance"); // Allocate Address DomainType domainType = (instance.getVpcId() == null) ? DomainType.Standard : DomainType.Vpc; Address address = AwsEc2Client.allocateAddress(ec2, domainType); String publicIp = address.getPublicIp(); System.out.println("Allocated Address(" + publicIp + ", " + address.getAllocationId() + ")"); if (address != null) { // TODO: Wait for Starting Instance. waitForStartingInstance(); try { // Associate Address String associateAddress = AwsEc2Client.associateAddress(ec2, address, instanceId); System.out.println("Associated Address(" + publicIp + ", " + associateAddress + ")"); String domain = options.getDomain(); if (domain != null) { // Attach Domain to EIP AmazonRoute53 route53 = AwsRoute53Client.getRoute53(); ChangeInfo attachedResult = AwsRoute53Client.attachDomainToEip(route53, publicIp, domain); if (attachedResult != null) { System.out.println("Attached domain(" + domain + ")"); } else { System.err.println("Not Found Available Hosted Zone for specified Domain(" + domain + ")"); } } } catch (AmazonServiceException e) { AwsEc2Client.releaseAddress(ec2, address); System.out.println("Released Address (" + publicIp + ")"); return 2; } } return 0; }
public CreateHostedZoneResult createHostedZone(CreateHostedZoneRequest req) throws AmazonServiceException, AmazonClientException{ Client c = Client.create(); WebResource r = c.resource(this.serverURL); String entity = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<CreateHostedZoneRequest xmlns=\"https://route53.amazonaws.com/doc/2012-02-29/\">" + "<Name>" + req.getName() + "</Name>" + "<CallerReference>" + req.getCallerReference() + "</CallerReference>"; if(req.getHostedZoneConfig() != null && req.getHostedZoneConfig().getComment() != null){ entity += "<HostedZoneConfig>" + "<Comment>" + req.getHostedZoneConfig().getComment() + "</Comment>" + "</HostedZoneConfig>"; } entity += "</CreateHostedZoneRequest>"; ClientResponse response = r .header("X-Amzn-Authorization", "AWS3 AWSAccessKeyId=" + this.accessKey + "," + "Algorithm=HmacSHA256," + "SignedHeaders=Host;X-Amz-Date," + "Signature=THISISANEXAMPLESIGNATURE=") .type(MediaType.APPLICATION_XML_TYPE) .accept(MediaType.TEXT_XML) .entity(entity) .post(ClientResponse.class); String resultXml = response.getEntity(String.class); if(response.getStatus() > 299 || response.getStatus() < 200){ exceptionMapper(response, resultXml); } CreateHostedZoneResponsePOJO interResult = null; try { StringReader reader = new StringReader(resultXml); JAXBContext context = JAXBContext.newInstance(CreateHostedZoneResponsePOJO.class); Unmarshaller unmarshaller = context.createUnmarshaller(); interResult = (CreateHostedZoneResponsePOJO) unmarshaller.unmarshal(reader); } catch (JAXBException e) { e.printStackTrace(); return null; } if(interResult == null){ return null; } CreateHostedZoneResult result = new CreateHostedZoneResult(); if(interResult.getHostedZone() != null){ HostedZone hostedZone = new HostedZone(); hostedZone.setId(interResult.getHostedZone().getId()); hostedZone.setName(interResult.getHostedZone().getName()); hostedZone.setCallerReference(interResult.getHostedZone().getCallerReference()); if(interResult.getHostedZone().getConfig() != null){ HostedZoneConfig config = new HostedZoneConfig(); config.setComment(interResult.getHostedZone().getConfig().getComment()); hostedZone.setConfig(config); } result.setHostedZone(hostedZone); } if(interResult.getChangeInfo() != null){ ChangeInfo changeInfo = new ChangeInfo(); changeInfo.setId(interResult.getChangeInfo().getId()); changeInfo.setStatus(interResult.getChangeInfo().getStatus()); changeInfo.setSubmittedAt(interResult.getChangeInfo().getSubmittedAt()); changeInfo.setComment(interResult.getChangeInfo().getComment()); result.setChangeInfo(changeInfo); } if(interResult.getDelegationSet() != null){ DelegationSet dSet = new DelegationSet(); dSet.setNameServers(interResult.getDelegationSet().getNameServers()); result.setDelegationSet(dSet); } return result; }
public DeleteHostedZoneResult deleteHostedZone(DeleteHostedZoneRequest req) throws AmazonServiceException, AmazonClientException{ Client c = Client.create(); WebResource r = c.resource(this.serverURL); ClientResponse response = r .path(req.getId()) .type(MediaType.APPLICATION_XML_TYPE) .accept(MediaType.TEXT_XML) .header("X-Amzn-Authorization", "AWS3 AWSAccessKeyId=" + this.accessKey + "," + "Algorithm=HmacSHA256," + "SignedHeaders=Host;X-Amz-Date," + "Signature=THISISANEXAMPLESIGNATURE=") .delete(ClientResponse.class); String resultXml = response.getEntity(String.class); if(response.getStatus() != 200){ exceptionMapper(response, resultXml); } DeleteHostedZoneResponsePOJO interResult = null; try { StringReader reader = new StringReader(resultXml); JAXBContext context = JAXBContext.newInstance(DeleteHostedZoneResponsePOJO.class); Unmarshaller unmarshaller = context.createUnmarshaller(); interResult = (DeleteHostedZoneResponsePOJO) unmarshaller.unmarshal(reader); } catch (JAXBException e) { e.printStackTrace(); return null; } if(interResult == null){ return null; } DeleteHostedZoneResult result = new DeleteHostedZoneResult(); if(interResult.getChangeInfo() != null){ ChangeInfo ci = new ChangeInfo(); if(interResult.getChangeInfo().getId() != null){ ci.setId(interResult.getChangeInfo().getId()); } if(interResult.getChangeInfo().getStatus() != null){ ci.setStatus(interResult.getChangeInfo().getStatus()); } if(interResult.getChangeInfo().getSubmittedAt() != null){ ci.setSubmittedAt(interResult.getChangeInfo().getSubmittedAt()); } if(interResult.getChangeInfo().getComment() != null){ ci.setComment(interResult.getChangeInfo().getComment()); } result.setChangeInfo(ci); } return result; }
private CreateHostedZoneResult createHostedZone(Session session, CreateHostedZoneRequest request) throws ErrorResponse{ CreateHostedZoneResult result = new CreateHostedZoneResult(); Date submittedAt = new Date(); AccessMySQL sqlaccess = AccessMySQL.getInstance(); String name = request.getName(); try { new java.net.URI("http://" + name); } catch(URISyntaxException e) { throw DNS53Faults.InvalidDomainName(); } String comment = null; if(request.getHostedZoneConfig() != null){ comment = request.getHostedZoneConfig().getComment(); } List<String> zoneInfo = sqlaccess.createHostedZone(session, name, request.getCallerReference(), comment, this.getAccountId()); String zoneId = zoneInfo.get(0); if(zoneId.equals("DUPLICATE_REFERENCE")){ throw DNS53Faults.HostedZoneAlreadyExists(); } else if(zoneId.equals("DUPLICATE_NAME")){ throw DNS53Faults.DelegationSetNotAvailable(); } else{ String status = DNS53Constants.PENDING; String tableName = zoneInfo.get(1); String changeID = RequestHandler.writeChange(sqlaccess, status, submittedAt.toString(), tableName, "CREATE"); HostedZone hz = new HostedZone(zoneId, request.getName(), request.getCallerReference()); hz.setConfig(request.getHostedZoneConfig()); result.setHostedZone(hz); ChangeInfo ci = new ChangeInfo(changeID, status, submittedAt); result.setChangeInfo(ci); List<String> nameServers = new LinkedList<String>(); for(int i = 2; i < zoneInfo.size(); ++i){ nameServers.add(zoneInfo.get(i)); } DelegationSet ds = new DelegationSet(nameServers); result.setDelegationSet(ds); } return result; }
public static void marshallChangeInfo(ChangeInfo ci, XMLNode response) { XMLNode changeInfo = QueryUtil.addNode(response, DNS53Constants.CHANGEINFO); QueryUtil.addNode(changeInfo, DNS53Constants.ID, ci.getId()); QueryUtil.addNode(changeInfo, DNS53Constants.STATUS, ci.getStatus()); QueryUtil.addNode(changeInfo, DNS53Constants.SUBMITTEDAT, ci.getSubmittedAt()); }
public void ensureCNAME(final String source, final String target) throws Exception { final HostedZone zone = findZone(source); Util.assertNotNull(zone, "missing zone for " + source); final String zoneId = zone.getId(); final boolean isPresent; final ResourceRecordSet recordOld; { final ResourceRecordSet recordFound = findRecord(zoneId, source); if (recordFound == null) { isPresent = false; recordOld = makeRecordCNAME(source, target); } else { isPresent = true; recordOld = recordFound; } } final ResourceRecordSet recordNew = makeRecordCNAME(source, target); recordNew.setTTL(recordOld.getTTL()); // final Collection<Change> changeList = new LinkedList<Change>(); if (isPresent) { changeList.add(new Change(ChangeAction.DELETE, recordOld)); changeList.add(new Change(ChangeAction.CREATE, recordNew)); } else { changeList.add(new Change(ChangeAction.CREATE, recordNew)); } final ChangeBatch changeRequest = new ChangeBatch(); changeRequest.setComment("updated : " + new Date()); changeRequest.setChanges(changeList); final ChangeResourceRecordSetsRequest request = new ChangeResourceRecordSetsRequest(); request.setHostedZoneId(zone.getId()); request.setChangeBatch(changeRequest); final ChangeResourceRecordSetsResult result = amazonClient .changeResourceRecordSets(request); final ChangeInfo changeResult = result.getChangeInfo(); logger.info("changeResult : \n{}", changeResult); }