3535import javax .naming .ConfigurationException ;
3636import javax .xml .parsers .ParserConfigurationException ;
3737
38- import org .apache .commons .io .IOUtils ;
3938import org .apache .log4j .Logger ;
4039import org .springframework .stereotype .Component ;
4140import org .xml .sax .SAXException ;
@@ -124,18 +123,12 @@ private void loadConfigValues() throws IOException {
124123 if (null != propertiesFile ) {
125124 logger .info ("Use EC2 properties file: " + propertiesFile .getAbsolutePath ());
126125 Properties EC2Prop = new Properties ();
127- FileInputStream ec2PropFile = null ;
128- try {
129- EC2Prop .load (new FileInputStream (propertiesFile ));
130- ec2PropFile = new FileInputStream (propertiesFile );
126+ try (FileInputStream ec2PropFile = new FileInputStream (propertiesFile )) {
131127 EC2Prop .load (ec2PropFile );
132-
133128 } catch (FileNotFoundException e ) {
134129 logger .warn ("Unable to open properties file: " + propertiesFile .getAbsolutePath (), e );
135130 } catch (IOException e ) {
136131 logger .warn ("Unable to read properties file: " + propertiesFile .getAbsolutePath (), e );
137- } finally {
138- IOUtils .closeQuietly (ec2PropFile );
139132 }
140133 managementServer = EC2Prop .getProperty ("managementServer" );
141134 cloudAPIPort = EC2Prop .getProperty ("cloudAPIPort" , null );
@@ -372,12 +365,12 @@ public boolean authorizeSecurityGroup(EC2AuthorizeRevokeSecurityGroup request) {
372365 CloudStackSecurityGroup resp = null ;
373366 if (ipPerm .getProtocol ().equalsIgnoreCase ("icmp" )) {
374367 resp =
375- getApi ().authorizeSecurityGroupIngress (null , constructList (ipPerm .getIpRangeSet ()), null , null , ipPerm .getIcmpCode (), ipPerm .getIcmpType (),
376- ipPerm .getProtocol (), null , request .getName (), null , secGroupList );
368+ getApi ().authorizeSecurityGroupIngress (null , constructList (ipPerm .getIpRangeSet ()), null , null , ipPerm .getIcmpCode (), ipPerm .getIcmpType (),
369+ ipPerm .getProtocol (), null , request .getName (), null , secGroupList );
377370 } else {
378371 resp =
379- getApi ().authorizeSecurityGroupIngress (null , constructList (ipPerm .getIpRangeSet ()), null , ipPerm .getToPort ().longValue (), null , null ,
380- ipPerm .getProtocol (), null , request .getName (), ipPerm .getFromPort ().longValue (), secGroupList );
372+ getApi ().authorizeSecurityGroupIngress (null , constructList (ipPerm .getIpRangeSet ()), null , ipPerm .getToPort ().longValue (), null , null ,
373+ ipPerm .getProtocol (), null , request .getName (), ipPerm .getFromPort ().longValue (), secGroupList );
381374 }
382375 if (resp != null ) {
383376 List <CloudStackIngressRule > ingressRules = resp .getIngressRules ();
@@ -816,7 +809,7 @@ public boolean associateAddress(EC2AssociateAddress request) {
816809 CloudStackIpAddress cloudIp = cloudIps .get (0 );
817810
818811 List <CloudStackUserVm > vmList =
819- getApi ().listVirtualMachines (null , null , true , null , null , null , null , request .getInstanceId (), null , null , null , null , null , null , null , null , null );
812+ getApi ().listVirtualMachines (null , null , true , null , null , null , null , request .getInstanceId (), null , null , null , null , null , null , null , null , null );
820813 if (vmList == null || vmList .size () == 0 ) {
821814 throw new Exception ("Instance not found" );
822815 }
@@ -973,8 +966,8 @@ public EC2CreateImageResponse createImage(EC2CreateImage request) {
973966 String osTypeId = imageSet [0 ].getOsTypeId ();
974967
975968 CloudStackTemplate resp =
976- getApi ().createTemplate ((request .getDescription () == null ? "" : request .getDescription ()), request .getName (), osTypeId , null , null , null , null , null ,
977- null , volumeId );
969+ getApi ().createTemplate ((request .getDescription () == null ? "" : request .getDescription ()), request .getName (), osTypeId , null , null , null , null , null ,
970+ null , volumeId );
978971 if (resp == null || resp .getId () == null ) {
979972 throw new Exception ("Image couldn't be created" );
980973 }
@@ -1004,9 +997,9 @@ public EC2CreateImageResponse registerImage(EC2RegisterImage request) {
1004997 EC2CreateImageResponse image = new EC2CreateImageResponse ();
1005998 try {
1006999 List <CloudStackTemplate > templates =
1007- getApi ().registerTemplate ((request .getDescription () == null ? request .getName () : request .getDescription ()), request .getFormat (),
1008- request .getHypervisor (), request .getName (), toOSTypeId (request .getOsTypeName ()), request .getLocation (), toZoneId (request .getZoneName (), null ), null ,
1009- null , null , null , null , null , null , null , null );
1000+ getApi ().registerTemplate ((request .getDescription () == null ? request .getName () : request .getDescription ()), request .getFormat (),
1001+ request .getHypervisor (), request .getName (), toOSTypeId (request .getOsTypeName ()), request .getLocation (), toZoneId (request .getZoneName (), null ), null ,
1002+ null , null , null , null , null , null , null , null );
10101003 if (templates != null ) {
10111004 // technically we will only ever register a single template...
10121005 for (CloudStackTemplate template : templates ) {
@@ -1227,7 +1220,7 @@ public EC2Volume createVolume(EC2CreateVolume request) {
12271220
12281221 // -> no volume name is given in the Amazon request but is required in the cloud API
12291222 CloudStackVolume vol =
1230- getApi ().createVolume (UUID .randomUUID ().toString (), null , diskOfferingId , null , size , snapshotId , toZoneId (request .getZoneName (), null ));
1223+ getApi ().createVolume (UUID .randomUUID ().toString (), null , diskOfferingId , null , size , snapshotId , toZoneId (request .getZoneName (), null ));
12311224 if (vol != null ) {
12321225 resp .setAttached (vol .getAttached ());
12331226 resp .setCreated (vol .getCreated ());
@@ -1429,8 +1422,7 @@ public EC2RunInstancesResponse runInstances(EC2RunInstances request) {
14291422 logger .info ("EC2 RunInstances - zone [" + request .getZoneName () + "] not found!" );
14301423 throw new Exception ("zone not found" );
14311424 }
1432- // we choose first zone?
1433- CloudStackZone zone = zones .get (0 );
1425+ zones .get (0 );
14341426
14351427 // network
14361428 //CloudStackNetwork network = findNetwork(zone);
@@ -1450,8 +1442,8 @@ public EC2RunInstancesResponse runInstances(EC2RunInstances request) {
14501442 for (int i = 0 ; i < createInstances ; i ++) {
14511443 try {
14521444 CloudStackUserVm resp =
1453- getApi ().deployVirtualMachine (svcOffering .getId (), request .getTemplateId (), zoneId , null , null , null , null , null , null , null ,
1454- request .getKeyName (), null , null , groupIds , groupNames , request .getSize ().longValue (), request .getUserData ());
1445+ getApi ().deployVirtualMachine (svcOffering .getId (), request .getTemplateId (), zoneId , null , null , null , null , null , null , null ,
1446+ request .getKeyName (), null , null , groupIds , groupNames , request .getSize ().longValue (), request .getUserData ());
14551447 EC2Instance vm = new EC2Instance ();
14561448 vm .setId (resp .getId ().toString ());
14571449 vm .setName (resp .getName ());
@@ -1674,7 +1666,7 @@ private int calculateAllowedInstances() throws Exception {
16741666 * @param ifs - filter out unwanted instances
16751667 */
16761668 private EC2DescribeInstancesResponse listVirtualMachines (String [] virtualMachineIds , EC2InstanceFilterSet ifs , List <CloudStackKeyValue > resourceTags )
1677- throws Exception {
1669+ throws Exception {
16781670 EC2DescribeInstancesResponse instances = new EC2DescribeInstancesResponse ();
16791671
16801672 if (null == virtualMachineIds || 0 == virtualMachineIds .length ) {
@@ -1698,7 +1690,7 @@ private EC2DescribeInstancesResponse listVirtualMachines(String[] virtualMachine
16981690 * @param instanceId - if interested in volumes for a specific instance, null if instance is not important
16991691 */
17001692 private EC2DescribeVolumesResponse listVolumes (String volumeId , String instanceId , EC2DescribeVolumesResponse volumes , List <CloudStackKeyValue > resourceTagSet )
1701- throws Exception {
1693+ throws Exception {
17021694
17031695 List <CloudStackVolume > vols = getApi ().listVolumes (null , null , null , volumeId , null , null , null , null , null , instanceId , null , resourceTagSet );
17041696 if (vols != null && vols .size () > 0 ) {
@@ -1890,11 +1882,11 @@ private EC2DescribeAvailabilityZonesResponse listZones(String[] interestedZones,
18901882 * EC2Instance objects loaded.
18911883 */
18921884 private EC2DescribeInstancesResponse lookupInstances (String instanceId , EC2DescribeInstancesResponse instances , List <CloudStackKeyValue > resourceTagSet )
1893- throws Exception {
1885+ throws Exception {
18941886
18951887 String instId = instanceId != null ? instanceId : null ;
18961888 List <CloudStackUserVm > vms =
1897- getApi ().listVirtualMachines (null , null , true , null , null , null , null , instId , null , null , null , null , null , null , null , null , resourceTagSet );
1889+ getApi ().listVirtualMachines (null , null , true , null , null , null , null , instId , null , null , null , null , null , null , null , null , resourceTagSet );
18981890
18991891 if (vms != null && vms .size () > 0 ) {
19001892 for (CloudStackUserVm cloudVm : vms ) {
@@ -2310,7 +2302,7 @@ private CloudStackNetwork getNetworksWithSecurityGroupEnabled(String zoneId) thr
23102302 */
23112303 private CloudStackNetwork createDefaultGuestNetwork (String zoneId , CloudStackNetworkOffering offering , CloudStackAccount owner ) throws Exception {
23122304 return getApi ().createNetwork (owner .getName () + "-network" , owner .getName () + "-network" , offering .getId (), zoneId , owner .getName (), owner .getDomainId (), true ,
2313- null , null , null , null , null , null , null , null );
2305+ null , null , null , null , null , null , null , null );
23142306 }
23152307
23162308 /**
@@ -2430,49 +2422,49 @@ public String cloudDeviceIdToDevicePath(String hypervisor, String deviceId) {
24302422 Integer devId = new Integer (deviceId );
24312423 if (null != hypervisor && hypervisor .toLowerCase ().contains ("windows" )) {
24322424 switch (devId ) {
2433- case 1 :
2434- return "xvdb" ;
2435- case 2 :
2436- return "xvdc" ;
2437- case 3 :
2438- return "xvdd" ;
2439- case 4 :
2440- return "xvde" ;
2441- case 5 :
2442- return "xvdf" ;
2443- case 6 :
2444- return "xvdg" ;
2445- case 7 :
2446- return "xvdh" ;
2447- case 8 :
2448- return "xvdi" ;
2449- case 9 :
2450- return "xvdj" ;
2451- default :
2452- return new String ("" + deviceId );
2425+ case 1 :
2426+ return "xvdb" ;
2427+ case 2 :
2428+ return "xvdc" ;
2429+ case 3 :
2430+ return "xvdd" ;
2431+ case 4 :
2432+ return "xvde" ;
2433+ case 5 :
2434+ return "xvdf" ;
2435+ case 6 :
2436+ return "xvdg" ;
2437+ case 7 :
2438+ return "xvdh" ;
2439+ case 8 :
2440+ return "xvdi" ;
2441+ case 9 :
2442+ return "xvdj" ;
2443+ default :
2444+ return new String ("" + deviceId );
24532445 }
24542446 } else { // -> assume its unix
24552447 switch (devId ) {
2456- case 1 :
2457- return "/dev/sdb" ;
2458- case 2 :
2459- return "/dev/sdc" ;
2460- case 3 :
2461- return "/dev/sdd" ;
2462- case 4 :
2463- return "/dev/sde" ;
2464- case 5 :
2465- return "/dev/sdf" ;
2466- case 6 :
2467- return "/dev/sdg" ;
2468- case 7 :
2469- return "/dev/sdh" ;
2470- case 8 :
2471- return "/dev/sdi" ;
2472- case 9 :
2473- return "/dev/sdj" ;
2474- default :
2475- return new String ("" + deviceId );
2448+ case 1 :
2449+ return "/dev/sdb" ;
2450+ case 2 :
2451+ return "/dev/sdc" ;
2452+ case 3 :
2453+ return "/dev/sdd" ;
2454+ case 4 :
2455+ return "/dev/sde" ;
2456+ case 5 :
2457+ return "/dev/sdf" ;
2458+ case 6 :
2459+ return "/dev/sdg" ;
2460+ case 7 :
2461+ return "/dev/sdh" ;
2462+ case 8 :
2463+ return "/dev/sdi" ;
2464+ case 9 :
2465+ return "/dev/sdj" ;
2466+ default :
2467+ return new String ("" + deviceId );
24762468 }
24772469 }
24782470 }
@@ -2697,8 +2689,8 @@ private void handleException(Exception e) {
26972689 if (errorMessage .contains ("Object vm_instance(uuid:" ) && errorMessage .contains (") does not exist" )) {
26982690 throw new EC2ServiceException (ClientError .InvalidInstanceID_NotFound , "Specified Instance ID does not exist" );
26992691 } else if (errorMessage .contains ("Unable to find security group by name" ) || errorMessage .contains ("Unable to find security group" ) ||
2700- (errorMessage .contains ("Object security_group(uuid:" ) && errorMessage .contains (") does not exist" )) ||
2701- errorMessage .contains ("Unable to find group by name " )) {
2692+ (errorMessage .contains ("Object security_group(uuid:" ) && errorMessage .contains (") does not exist" )) ||
2693+ errorMessage .contains ("Unable to find group by name " )) {
27022694 throw new EC2ServiceException (ClientError .InvalidGroup_NotFound , "Specified Security Group does not exist" );
27032695 } else if (errorMessage .contains ("Invalid port numbers" )) {
27042696 throw new EC2ServiceException (ClientError .InvalidPermission_Malformed , "Specified Port value is invalid" );
@@ -2717,7 +2709,7 @@ private void handleException(Exception e) {
27172709 } else if (errorMessage .contains ("Object snapshots(uuid:" ) && errorMessage .contains (") does not exist" )) {
27182710 throw new EC2ServiceException (ClientError .InvalidSnapshot_NotFound , "Specified Snapshot ID doesn't exist" );
27192711 } else if ((errorMessage .contains ("A key pair with name '" ) && errorMessage .contains ("' does not exist" )) ||
2720- (errorMessage .contains ("A key pair with name '" ) && errorMessage .contains ("' was not found" ))) {
2712+ (errorMessage .contains ("A key pair with name '" ) && errorMessage .contains ("' was not found" ))) {
27212713 throw new EC2ServiceException (ClientError .InvalidKeyPair_NotFound , "Specified Key pair name is invalid" );
27222714 } else if (errorMessage .contains ("A key pair with name '" ) && errorMessage .contains ("' already exists" )) {
27232715 throw new EC2ServiceException (ClientError .InvalidKeyPair_Duplicate , "Specified Key pair already exists" );
@@ -2744,7 +2736,7 @@ private void handleException(Exception e) {
27442736 } else if (errorMessage .contains ("Unable to find tags by parameters specified" )) {
27452737 throw new EC2ServiceException (ClientError .InvalidParameterValue , "Specified resourceTag for the specified resourceId doesn't exist" );
27462738 } else if (errorMessage .contains ("Failed to enable static nat for the ip address with specified ipId "
2747- + "as vm with specified vmId is already associated with specified currentIp" )) {
2739+ + "as vm with specified vmId is already associated with specified currentIp" )) {
27482740 throw new EC2ServiceException (ClientError .InvalidParameterValue , "Specified publicIp is already associated to the specified VM" );
27492741 } else if (errorMessage .contains ("Specified IP address id is not associated with any vm Id" )) {
27502742 throw new EC2ServiceException (ClientError .InvalidParameterValue , "Specified publicIp is not associated to any VM" );
@@ -2792,7 +2784,7 @@ private void handleException(Exception e) {
27922784 throw new EC2ServiceException (ClientError .InvalidAMIID_NotFound , "Specified ImageId is unavailable" );
27932785 } else if (errorMessage .contains ("cannot stop VM" ) && errorMessage .contains ("when it is in state Starting" )) {
27942786 throw new EC2ServiceException (ClientError .IncorrectInstanceState ,
2795- "Unable to stop. One or more of the specified instances is in an incorrect state 'pending'" );
2787+ "Unable to stop. One or more of the specified instances is in an incorrect state 'pending'" );
27962788 } else if (errorMessage .contains ("Failed to authorize security group ingress rule(s)" )) {
27972789 throw new EC2ServiceException (ClientError .InvalidParameterValue , "Specified Ip-permission is invalid" + " or the Ip-permission already exists" );
27982790 } else if (errorMessage .contains ("Failed to reboot vm instance" )) {
@@ -2807,7 +2799,7 @@ private void handleException(Exception e) {
28072799 throw new EC2ServiceException (ClientError .VolumeLimitExceeded , "You have reached the limit on the number of volumes that can be created" );
28082800 } else if (errorMessage .contains ("Maximum number of resources of type 'public_ip' for account" ) && errorMessage .contains ("has been exceeded" )) {
28092801 throw new EC2ServiceException (ClientError .AddressLimitExceeded ,
2810- "You have reached the limit on the number of elastic ip addresses your account can have" );
2802+ "You have reached the limit on the number of elastic ip addresses your account can have" );
28112803 } else if (errorMessage .contains ("Unable to apply save userdata entry on router" )) {
28122804 throw new EC2ServiceException (ClientError .InvalidParameterValue , "The value supplied for parameter UserData is invalid" );
28132805 } else {
@@ -2855,7 +2847,7 @@ private void handleException(Exception e) {
28552847 throw new EC2ServiceException (ServerError .InternalError , "Unable to start the instance that was stopped during image creation" );
28562848 } else if (errorMessage .contains ("One or more of instanceIds specified is in stopped state" )) {
28572849 throw new EC2ServiceException (ClientError .IncorrectInstanceState ,
2858- "Unable to reboot. One or more of the specified instances is in an incorrect state 'stopped'" );
2850+ "Unable to reboot. One or more of the specified instances is in an incorrect state 'stopped'" );
28592851 } else if (errorMessage .contains ("Specified ipAddress doesn't exist" )) {
28602852 throw new EC2ServiceException (ClientError .InvalidParameterValue , "Specified publicIp doesn't exist" );
28612853 } else if (errorMessage .contains ("Min Count is greater than the number of instances left to allocate" )) {
@@ -2874,7 +2866,7 @@ private void handleException(Exception e) {
28742866 throw new EC2ServiceException (ClientError .InvalidInstanceID_NotFound , "One or more of the specified instanceId not found" );
28752867 } else if (errorMessage .contains ("Cannot modify, instance should be in stopped state" )) {
28762868 throw new EC2ServiceException (ClientError .IncorrectInstanceState ,
2877- "Unable to modify instance attribute. Specified instance is not in the correct state 'stopped'" );
2869+ "Unable to modify instance attribute. Specified instance is not in the correct state 'stopped'" );
28782870 } else {
28792871 throw new EC2ServiceException (ServerError .InternalError , "An unexpected error occured" );
28802872 }
0 commit comments