001 /**
002 *
003 */
004 package de.jw.cloud42.webapp;
005
006 import java.util.ArrayList;
007 import java.util.Arrays;
008 import java.util.HashMap;
009 import java.util.List;
010 import java.util.TimeZone;
011 import javax.faces.application.FacesMessage;
012 import javax.faces.model.SelectItem;
013
014 import org.jboss.seam.ScopeType;
015 import org.jboss.seam.annotations.In;
016 import org.jboss.seam.annotations.Name;
017 import org.jboss.seam.annotations.Out;
018 import org.jboss.seam.annotations.Scope;
019 import org.jboss.seam.annotations.Synchronized;
020 import org.jboss.seam.faces.FacesMessages;
021
022 import com.xerox.amazonws.ec2.AvailabilityZone;
023 import com.xerox.amazonws.ec2.ConsoleOutput;
024 import com.xerox.amazonws.ec2.GroupDescription;
025 import com.xerox.amazonws.ec2.ImageDescription;
026 import com.xerox.amazonws.ec2.InstanceType;
027 import com.xerox.amazonws.ec2.KeyPairInfo;
028 import com.xerox.amazonws.ec2.RegionInfo;
029 import com.xerox.amazonws.ec2.ReservationDescription;
030 import com.xerox.amazonws.ec2.TerminatingInstanceDescription;
031 import com.xerox.amazonws.ec2.GroupDescription.IpPermission;
032
033
034
035 import de.jw.cloud42.core.domain.AwsCredentials;
036 import de.jw.cloud42.core.domain.Instance;
037 import de.jw.cloud42.core.service.Cloud42BaseFunctions;
038 import de.jw.cloud42.webapp.tree.GroupTreeBean;
039
040 /**
041 * Seam component wrapper around Cloud42BaseFunctions.
042 *
043 * @author fbitzer
044 *
045 */
046 @Name("baseFunctionsManager")
047 @Scope(ScopeType.SESSION)
048 @Synchronized(timeout=1000000000)
049 public class BaseFunctionsManager {
050
051 public final static String MSG_KEYPAIR_ERROR = "An error creating the keypair occurred! "
052 + "Maybe a keypair with the same name already exists. It is not possible to save a key.";
053
054
055 private final long MAX_FILESIZE = 16000;
056
057
058 @In
059 private UserManager userManager;
060
061 @Out
062 private String privateKey = "Please wait a second...";
063 // @Out
064 // private boolean pairNotCreated = false;
065 // private String keypairName = "";
066
067 /**
068 * Configuration of a instance that should be started.
069 */
070 @In(create=true)
071 @Out
072 private InstanceConfiguration instanceConfiguration;
073
074
075 @In(create=true)
076 @Out
077 private PermissionConfiguration permissionConfiguration;
078
079 /**
080 * Inject faces messages to trigger error and success messages.
081 */
082 @In
083 FacesMessages facesMessages;
084
085 @In(create=true)
086 @Out
087 GroupTreeBean groupTreeBean;
088
089 @Out
090 private String consoleOutput = "Please wait a second...";
091
092
093
094 // List of images, instances, keywords, groups.
095 private List<ImageDescription> imageList = null;
096
097 private List<Instance> instanceList = null;
098
099 private List<KeyPairInfo> keypairList = null;
100
101 private List<GroupDescription> groupList = null;
102
103 private List<AvailabilityZone> availabilityZoneList = null;
104
105 private List<RegionInfo> regionList = null;
106
107
108 // hashtable saves a name for each instance (identified by reservationId as
109 // key)
110 // @In(create=true)
111 // @Out(scope=ScopeType.APPLICATION)
112 // it is outjected to application scope so it is available during various
113 // sessions.
114 // Saving it to database (coupled to the user) might be the better
115 // solution...
116 // Feature is currently not used.
117 private HashMap<String, String> instanceNames = new HashMap<String, String>();
118
119
120 /**
121 * Reset imageList and force reload next time it is accessed.
122 */
123 public void resetImageList(){
124 this.imageList = null;
125 }
126
127 /**
128 * Reset instanceList and force reload next time it is accessed.
129 */
130 public void resetInstanceList(){
131 this.instanceList = null;
132 }
133
134 /**
135 * Reset groupList and force reload next time it is accessed.
136 */
137 public void resetGroupList(){
138
139 groupTreeBean.resetGroupList();
140
141 this.groupList = null;
142 }
143
144 /**
145 * Reset keypairList and force reload next time it is accessed.
146 */
147 public void resetKeypairList(){
148
149 this.keypairList = null;
150 }
151
152 /**
153 * Reset regionList and force reload next time it is accessed.
154 */
155 public void resetRegionList(){
156 this.regionList = null;
157 }
158
159
160 // /**
161 // * Set the AWS Region.
162 // * @param regionUrl
163 // */
164 // public void setRegionUrl(String regionUrl){
165 //
166 // Cloud42BaseFunctions bf = new Cloud42BaseFunctions();
167 //
168 //
169 // AwsCredentials cred = userManager.getUser().getCredentials();
170 // if (cred!=null){
171 // bf.setCredentials(cred);
172 //
173 // bf.setRegionUrl(regionUrl);
174 // }
175 //
176 // }
177
178 /**
179 * List all AMIs.
180 *
181 * @return null if no AMIs were found (because of missing or wrong
182 * credentials)
183 */
184 public List<ImageDescription> getImages(){
185
186
187 if (imageList == null){
188 Cloud42BaseFunctions bf = new Cloud42BaseFunctions();
189
190
191 AwsCredentials cred = userManager.getUser().getCredentials();
192 if (cred!=null){
193
194 bf.setCredentials(cred);
195
196 bf.setRegionUrl(userManager.getUser().getRegionUrl());
197
198
199 ImageDescription[] list = bf.listImages(true);
200 if (list!=null){
201 imageList = Arrays.asList(list);
202 }
203
204
205 }
206
207 // if (imageList==null){
208 // facesMessages.addFromResourceBundle(FacesMessage.SEVERITY_ERROR, "msg_imagesNotListed");
209 // } else {
210 // facesMessages.addFromResourceBundle(FacesMessage.SEVERITY_INFO, "msg_imagesListed");
211 // }
212
213
214 }
215
216 return imageList;
217 }
218
219
220 /**
221 * List all active instances.
222 *
223 * @return null if no instances were found
224 */
225 public List<de.jw.cloud42.core.domain.Instance> getInstances(){
226
227
228 if (instanceList == null){
229 Cloud42BaseFunctions bf = new Cloud42BaseFunctions();
230
231
232
233
234 AwsCredentials cred = userManager.getUser().getCredentials();
235 if (cred!=null){
236 bf.setCredentials(cred);
237
238 bf.setRegionUrl(userManager.getUser().getRegionUrl());
239
240 de.jw.cloud42.core.domain.Instance[] list = bf.listInstances();
241 if (list!=null){
242 instanceList = Arrays.asList(list);
243 }
244 }
245
246
247
248 }
249
250 return instanceList;
251 }
252
253 /**
254 * List all keypairs.
255 *
256 * @return null if no keypairs were found.
257 */
258 public List<KeyPairInfo> getKeypairs(){
259
260
261 if (keypairList == null){
262 Cloud42BaseFunctions bf = new Cloud42BaseFunctions();
263
264
265
266
267 AwsCredentials cred = userManager.getUser().getCredentials();
268 if (cred!=null){
269 bf.setCredentials(cred);
270 bf.setRegionUrl(userManager.getUser().getRegionUrl());
271
272 KeyPairInfo[] list = bf.listKeypairs();
273 if (list!=null){
274 keypairList = Arrays.asList(list);
275 }
276 }
277
278
279
280 }
281
282 return keypairList;
283 }
284
285 /**
286 * List all Regions.
287 *
288 * @return null if no regions were found.
289 */
290 public List<RegionInfo> getRegions(){
291
292
293 if (regionList == null){
294 Cloud42BaseFunctions bf = new Cloud42BaseFunctions();
295
296 AwsCredentials cred = userManager.getUser().getCredentials();
297 if (cred!=null){
298 bf.setCredentials(cred);
299
300 //Do NOT set a special region here!
301 bf.setRegionUrl(null);
302
303 RegionInfo[] list = bf.listRegions();
304 if (list!=null){
305 regionList = Arrays.asList(list);
306 }
307 }
308
309
310
311 }
312
313 return regionList;
314 }
315
316 /**
317 * List all groups.
318 *
319 * @return null if no groups were found.
320 */
321 public List<GroupDescription> getGroups(){
322 // GroupDescription d;
323
324
325 if (groupList == null){
326 Cloud42BaseFunctions bf = new Cloud42BaseFunctions();
327
328
329
330
331 AwsCredentials cred = userManager.getUser().getCredentials();
332 if (cred!=null){
333 bf.setCredentials(cred);
334 bf.setRegionUrl(userManager.getUser().getRegionUrl());
335
336 GroupDescription[] list = bf.listSecurityGroups();
337 if (list!=null){
338 groupList = Arrays.asList(list);
339 }
340 }
341
342
343
344 }
345
346 return groupList;
347 }
348
349
350
351 /**
352 * Return keypairs as list of SelectItems for displaying in a dropdown box.
353 *
354 * @return
355 */
356 public List<SelectItem> getKeypairItems(){
357
358 List<SelectItem> result = new ArrayList<SelectItem>();
359
360 List<KeyPairInfo> kl = this.getKeypairs();
361 if (kl!=null){
362 for (KeyPairInfo k : kl){
363
364 result.add(new SelectItem(k.getKeyName(),k.getKeyName()));
365 }
366 }
367 return result;
368
369 }
370
371 /**
372 * Return groups as list of SelectItems for displaying in a dropdown box.
373 *
374 * @return
375 */
376 public List<SelectItem> getGroupItems(){
377
378 List<SelectItem> result = new ArrayList<SelectItem>();
379
380 List<GroupDescription> gl = this.getGroups();
381 if (gl!=null){
382 for (GroupDescription d : gl){
383 result.add(new SelectItem(d.getName(),d.getName()));
384 }
385 }
386 return result;
387
388 }
389
390
391 /**
392 * Return possible instanceTypes as SelectItems.
393 *
394 * @return
395 */
396 public List<SelectItem> getInstanceTypeItems(){
397
398 List<SelectItem> result = new ArrayList<SelectItem>();
399
400
401
402 for (InstanceType t : InstanceType.values()){
403 result.add(new SelectItem(t.getTypeId(),t.getTypeId()));
404 }
405
406 return result;
407
408 }
409
410
411 /**
412 * Return keypairs as list of SelectItems for displaying in a dropdown box.
413 *
414 * @return
415 */
416 public List<SelectItem> getRegionItems(){
417
418 List<SelectItem> result = new ArrayList<SelectItem>();
419
420 List<RegionInfo> rl = this.getRegions();
421 if (rl!=null){
422 for (RegionInfo i : rl){
423
424 result.add(new SelectItem(i.getUrl(),i.getName()));
425 }
426 }
427 return result;
428
429 }
430
431
432 /**
433 * Helper function, resets current instanceConfiguration and initializes
434 * values.
435 *
436 * @param imageId
437 * @param imageLocation
438 */
439 public void createNewConfiguration(String imageId, String imageLocation){
440 instanceConfiguration = new InstanceConfiguration();
441
442 instanceConfiguration.setImageId(imageId);
443 instanceConfiguration.setImageLocation(imageLocation);
444
445
446 }
447
448 /**
449 * Starts an instance. Uses injected instanceConfiguration to set
450 * properties.
451 */
452 public void runInstance(){
453
454 if (instanceConfiguration.getAttachedFile() != null &&
455 instanceConfiguration.getAttachedFile().length > MAX_FILESIZE){
456 facesMessages.addFromResourceBundle(FacesMessage.SEVERITY_ERROR, "msg_fileTooLarge");
457 return;
458 }
459
460 Cloud42BaseFunctions bf = new Cloud42BaseFunctions();
461
462
463
464 ReservationDescription d=null;
465
466 AwsCredentials cred = userManager.getUser().getCredentials();
467 if (cred!=null){
468 bf.setCredentials(cred);
469 bf.setRegionUrl(userManager.getUser().getRegionUrl());
470
471 InstanceType t = InstanceType.getTypeFromString(instanceConfiguration.getType());
472
473
474 // if a file was attached, upload file
475 // else use string provided as userdata
476 byte[] userdata;
477 if (instanceConfiguration.getAttachedFile() != null){
478
479 userdata = instanceConfiguration.getAttachedFile();
480
481 } else {
482 userdata = instanceConfiguration.getUserData().getBytes();
483 }
484
485 d = bf.runInstance(instanceConfiguration.getImageId(),
486 instanceConfiguration.getGroupNames().toArray(new String[0]),
487 instanceConfiguration.getKeypairName(),
488 userdata,
489 t,
490 1,
491 instanceConfiguration.getAvailabilityZone(),
492 instanceConfiguration.getKernelId(),
493 instanceConfiguration.getRamdiskId());
494
495 if (d!=null){
496 // set name for the instance
497 this.setName(d.getReservationId(), instanceConfiguration.getName());
498
499 // force reload of list of current instances
500 this.resetInstanceList();
501 }
502
503 }
504
505 if (d==null){
506 facesMessages.addFromResourceBundle(FacesMessage.SEVERITY_ERROR, "msg_instanceNotStarted");
507 } else {
508 facesMessages.addFromResourceBundle(FacesMessage.SEVERITY_INFO, "msg_instanceStarted",new Object[]{d.getInstances().get(0).getInstanceId()});
509 }
510
511
512 }
513
514 /**
515 * Configures a new instance with the same configuration as the given
516 * instance. Does not start the instance.
517 *
518 * @param instanceId
519 * InstanceId of the instance from which the configuration should
520 * be taken.
521 */
522 public void runAnotherInstance(String instanceId){
523
524
525 // describe the instance first and load its data into a
526 // instanceConfiguration
527 Cloud42BaseFunctions bf = new Cloud42BaseFunctions();
528
529
530 AwsCredentials cred = userManager.getUser().getCredentials();
531 if (cred!=null){
532 bf.setCredentials(cred);
533 bf.setRegionUrl(userManager.getUser().getRegionUrl());
534
535 Instance i = bf.describeInstance(instanceId);
536
537 this.createNewConfiguration(i.getImageId(), "");
538
539 this.instanceConfiguration.setGroupNames(Arrays.asList(i.getGroups()));
540 this.instanceConfiguration.setKeypairName(i.getKeyName());
541 this.instanceConfiguration.setName(getName(i));
542 this.instanceConfiguration.setType(i.getInstanceType());
543
544 this.instanceConfiguration.setAvailabilityZone(i.getAvailabilityZone());
545 this.instanceConfiguration.setKernelId(i.getKernelId());
546 this.instanceConfiguration.setRamdiskId(i.getRamdiskId());
547
548 //userdata is not transferred!
549
550
551 }
552 }
553
554
555
556 /**
557 * Get the user defined name for an instance.
558 *
559 * @param instance
560 * @return
561 */
562 public String getName(Instance instance){
563 return instanceNames.get(instance.getReservationId());
564 }
565
566 /**
567 * Set the user defined name for an instance.
568 *
569 * @param instance
570 * @param name
571 */
572 public void setName(String reservationId, String name){
573 instanceNames.put(reservationId, name);
574 }
575
576 /**
577 * Register a new AMI at given location.
578 *
579 * @param location
580 */
581 public void registerImage(String location){
582 Cloud42BaseFunctions bf = new Cloud42BaseFunctions();
583
584 String id = null;
585
586
587 AwsCredentials cred = userManager.getUser().getCredentials();
588 if (cred!=null){
589 bf.setCredentials(cred);
590 bf.setRegionUrl(userManager.getUser().getRegionUrl());
591
592 id = bf.registerImage(location);
593
594 // reset list of images to force reload
595 if (id!=null) this.resetImageList();
596 }
597
598
599
600 if (id==null){
601 facesMessages.addFromResourceBundle(FacesMessage.SEVERITY_ERROR, "msg_imageNotRegistered");
602 } else {
603 facesMessages.addFromResourceBundle(FacesMessage.SEVERITY_INFO, "msg_imageRegistered",new Object[]{id});
604 }
605
606 }
607
608 /**
609 * Deregisters an image.
610 *
611 * @param image
612 */
613 public void deregisterImage(ImageDescription image){
614 Cloud42BaseFunctions bf = new Cloud42BaseFunctions();
615
616
617
618 boolean result = false;
619
620 AwsCredentials cred = userManager.getUser().getCredentials();
621 if (cred!=null){
622 bf.setCredentials(cred);
623 bf.setRegionUrl(userManager.getUser().getRegionUrl());
624
625 result = bf.deregisterImage(image.getImageId());
626
627 if (result) this.resetImageList();
628 }
629
630 if (!result){
631 facesMessages.addFromResourceBundle(FacesMessage.SEVERITY_ERROR, "msg_imageNotDeregistered");
632 } else {
633 facesMessages.addFromResourceBundle(FacesMessage.SEVERITY_INFO, "msg_imageDeregistered");
634 }
635 }
636
637 /**
638 * Stop an instance.
639 *
640 * @param instanceId
641 * Id if instance to shutdown.
642 */
643 public void stopInstance(String instanceId){
644 Cloud42BaseFunctions bf = new Cloud42BaseFunctions();
645
646
647
648 TerminatingInstanceDescription result = null;
649
650 AwsCredentials cred = userManager.getUser().getCredentials();
651 if (cred!=null){
652 bf.setCredentials(cred);
653 bf.setRegionUrl(userManager.getUser().getRegionUrl());
654
655 result = bf.stopInstance(instanceId);
656
657 if (result!=null) this.resetInstanceList();
658 }
659
660 if (result==null){
661 facesMessages.addFromResourceBundle(FacesMessage.SEVERITY_ERROR, "msg_instanceNotTerminated", new Object[]{instanceId});
662 } else {
663 facesMessages.addFromResourceBundle(FacesMessage.SEVERITY_INFO, "msg_instanceTerminated", new Object[]{instanceId});
664 }
665 }
666
667 /**
668 * Stop all running instances.
669 */
670 public void stopAllInstances(){
671 Cloud42BaseFunctions bf = new Cloud42BaseFunctions();
672
673
674 TerminatingInstanceDescription[] result = null;
675
676 AwsCredentials cred = userManager.getUser().getCredentials();
677 if (cred!=null){
678 bf.setCredentials(cred);
679 bf.setRegionUrl(userManager.getUser().getRegionUrl());
680
681 result = bf.stopAllInstances();
682
683 if (result!=null) this.resetInstanceList();
684 }
685
686 if (result==null){
687 facesMessages.addFromResourceBundle(FacesMessage.SEVERITY_ERROR, "msg_allInstancesNotTerminated");
688 } else {
689 facesMessages.addFromResourceBundle(FacesMessage.SEVERITY_INFO, "msg_allInstancesTerminated");
690 }
691 }
692
693 /**
694 * Reboot an instance.
695 *
696 * @param instanceId
697 * Id if instance to reboot.
698 */
699 public void rebootInstance(String instanceId){
700
701 Cloud42BaseFunctions bf = new Cloud42BaseFunctions();
702
703 AwsCredentials cred = userManager.getUser().getCredentials();
704 if (cred!=null){
705 bf.setCredentials(cred);
706 bf.setRegionUrl(userManager.getUser().getRegionUrl());
707
708 bf.rebootInstance(instanceId);
709
710
711 this.resetInstanceList();
712
713 facesMessages.addFromResourceBundle(FacesMessage.SEVERITY_INFO, "msg_instanceRebooted", new Object[]{instanceId});
714 } else {
715
716 facesMessages.addFromResourceBundle(FacesMessage.SEVERITY_ERROR, "msg_instanceNotRebooted", new Object[]{instanceId});
717 }
718
719 }
720
721
722
723 /**
724 * Helper functions, gets the current timezone to display dates correctly.
725 *
726 * @return
727 */
728 public TimeZone getTimeZone(){
729
730 return TimeZone.getDefault();
731
732 }
733
734
735 /**
736 * Create a new security group
737 */
738 public void createGroup(String name, String description){
739
740 Cloud42BaseFunctions bf = new Cloud42BaseFunctions();
741
742 boolean result = false;
743
744 AwsCredentials cred = userManager.getUser().getCredentials();
745 if (cred!=null){
746 bf.setCredentials(cred);
747 bf.setRegionUrl(userManager.getUser().getRegionUrl());
748
749 result = bf.createSecurityGroup(name, description);
750
751 if (result) this.resetGroupList();
752 }
753
754 if (!result){
755 facesMessages.addFromResourceBundle(FacesMessage.SEVERITY_ERROR, "msg_groupNotCreated");
756 } else {
757 facesMessages.addFromResourceBundle(FacesMessage.SEVERITY_INFO, "msg_groupCreated",new Object[]{name});
758 }
759
760 }
761
762 /**
763 * Delete a security group
764 */
765 public void deleteGroup(String name){
766
767 Cloud42BaseFunctions bf = new Cloud42BaseFunctions();
768
769 boolean result = false;
770
771 AwsCredentials cred = userManager.getUser().getCredentials();
772 if (cred!=null){
773 bf.setCredentials(cred);
774 bf.setRegionUrl(userManager.getUser().getRegionUrl());
775
776 result = bf.deleteSecurityGroup(name);
777
778 if (result) this.resetGroupList();
779 }
780
781 if (!result){
782 facesMessages.addFromResourceBundle(FacesMessage.SEVERITY_ERROR, "msg_groupNotDeleted");
783 } else {
784 facesMessages.addFromResourceBundle(FacesMessage.SEVERITY_INFO, "msg_groupDeleted",new Object[]{name});
785 }
786
787 }
788
789 /**
790 * Remove a permission.
791 *
792 * @param group
793 * @param permission
794 */
795 public void removePermission(){
796
797 GroupDescription group = groupTreeBean.getSelectedGroup();
798 IpPermission permission = groupTreeBean.getSelectedPermission();
799
800 Cloud42BaseFunctions bf = new Cloud42BaseFunctions();
801
802 boolean result = false;
803
804 AwsCredentials cred = userManager.getUser().getCredentials();
805 if (permission != null && cred!=null){
806
807 bf.setCredentials(cred);
808 bf.setRegionUrl(userManager.getUser().getRegionUrl());
809
810 result = true;
811
812 if (permission.getIpRanges() != null && permission.getIpRanges().size()>0){
813 for (String cidr : permission.getIpRanges()){
814
815 if (! bf.removePermission(group.getName(), permission.getProtocol(),
816 permission.getFromPort(), permission.getToPort(), cidr)) {
817 result = false;
818 }
819 }
820 } else {
821 // this is the "second group" mode
822 for (String[] pair : permission.getUidGroupPairs())
823
824 if (! bf.removePermission(group.getName(),pair[1], pair[0])){
825 result = false;
826 }
827 }
828
829 }
830
831 if (!result){
832 facesMessages.addFromResourceBundle(FacesMessage.SEVERITY_ERROR, "msg_permissionNotDeleted");
833 } else {
834
835
836 this.resetGroupList();
837
838 facesMessages.addFromResourceBundle(FacesMessage.SEVERITY_INFO, "msg_permissionDeleted");
839 }
840
841 }
842
843 /**
844 * Initializes a new permission.
845 */
846 public void createNewPermission() {
847
848
849 GroupDescription group = groupTreeBean.getSelectedGroup();
850 if (group != null){
851 permissionConfiguration = new PermissionConfiguration();
852 permissionConfiguration.setEditedGroup(group.getName());
853 }
854
855 }
856
857 /**
858 * Add a permission that was configured using a permissionConfiguration.
859 */
860 public void addPermission(){
861
862
863
864 Cloud42BaseFunctions bf = new Cloud42BaseFunctions();
865
866 boolean result = false;
867
868 AwsCredentials cred = userManager.getUser().getCredentials();
869 if (permissionConfiguration != null && cred!=null){
870
871 bf.setCredentials(cred);
872 bf.setRegionUrl(userManager.getUser().getRegionUrl());
873
874 String group = permissionConfiguration.getEditedGroup();
875
876 if (permissionConfiguration.getSource().equals("cidr")){
877
878 result = bf.addPermission(group,permissionConfiguration.getProtocol(),
879 permissionConfiguration.getFromPort().intValue(),permissionConfiguration.getToPort().intValue(),
880 permissionConfiguration.getCidr());
881
882 } else {
883
884 result = bf.addPermission(group, permissionConfiguration.getGroup(),
885 permissionConfiguration.getUser());
886 }
887
888 }
889
890 if (!result){
891 facesMessages.addFromResourceBundle(FacesMessage.SEVERITY_ERROR, "msg_permissionNotAdded");
892 } else {
893
894
895 this.resetGroupList();
896
897 facesMessages.addFromResourceBundle(FacesMessage.SEVERITY_INFO, "msg_permissionAdded");
898 }
899
900
901 }
902
903 /**
904 * Create a keypair and outject private key in component "privateKey".
905 *
906 * @param name
907 */
908 public void createKeypair(String name){
909
910 Cloud42BaseFunctions bf = new Cloud42BaseFunctions();
911
912 KeyPairInfo ki = null;
913
914 privateKey = MSG_KEYPAIR_ERROR;
915
916
917 AwsCredentials cred = userManager.getUser().getCredentials();
918 if (permissionConfiguration != null && cred!=null){
919
920 bf.setCredentials(cred);
921 bf.setRegionUrl(userManager.getUser().getRegionUrl());
922
923 ki = bf.createKeypair(name);
924
925 if (ki != null){
926 privateKey = ki.getKeyMaterial();
927 }
928 }
929
930 if (ki == null){
931 facesMessages.addFromResourceBundle(FacesMessage.SEVERITY_ERROR, "msg_keypairNotAdded");
932
933 } else {
934
935
936 this.resetKeypairList();
937
938 // keypairName = name;
939
940 facesMessages.addFromResourceBundle(FacesMessage.SEVERITY_INFO, "msg_keypairAdded");
941
942
943 }
944
945 }
946
947 /**
948 * Reset the private key.
949 */
950 public void resetPrivateKey(){
951 privateKey = "Please wait a second...";
952 }
953
954 public void deleteKeypair(KeyPairInfo kp){
955
956 Cloud42BaseFunctions bf = new Cloud42BaseFunctions();
957
958 boolean result = false;
959
960 AwsCredentials cred = userManager.getUser().getCredentials();
961 if (permissionConfiguration != null && cred!=null){
962
963 bf.setCredentials(cred);
964 bf.setRegionUrl(userManager.getUser().getRegionUrl());
965
966 result = bf.deleteKeypair(kp.getKeyName());
967
968 }
969
970 if (!result){
971 facesMessages.addFromResourceBundle(FacesMessage.SEVERITY_ERROR, "msg_keypairNotDeleted");
972 } else {
973
974 // delete mappings
975 userManager.deletePrivateKey(kp.getKeyName());
976
977 this.resetKeypairList();
978
979 facesMessages.addFromResourceBundle(FacesMessage.SEVERITY_INFO, "msg_keypairDeleted");
980 }
981
982
983 }
984
985 /**
986 * Return availability zones as list of SelectItems for displaying in a
987 * dropdown box.
988 *
989 * @return
990 */
991 public List<SelectItem> getAvailabilityZoneItems(){
992
993
994 List<SelectItem> result = new ArrayList<SelectItem>();
995
996 // add a "any" item
997 SelectItem anyItem = new SelectItem("","<any>");
998 result.add(anyItem);
999
1000 // Now list real availability zonesif not already in cache
1001 if (availabilityZoneList==null){
1002
1003 AwsCredentials cred = userManager.getUser().getCredentials();
1004 if (cred!=null){
1005 Cloud42BaseFunctions bf = new Cloud42BaseFunctions();
1006
1007 bf.setCredentials(cred);
1008 bf.setRegionUrl(userManager.getUser().getRegionUrl());
1009
1010 AvailabilityZone[] list = bf.listAvailabilityZones();
1011 if (list!=null){
1012 availabilityZoneList = Arrays.asList(list);
1013 }
1014 }
1015 }
1016 if (availabilityZoneList != null) {
1017 for (AvailabilityZone z : availabilityZoneList) {
1018 SelectItem item = new SelectItem();
1019 item.setLabel(z.getName() + " (" + z.getState() + ")");
1020 item.setValue(z.getName());
1021
1022 result.add(item);
1023
1024 }
1025 }
1026 return result;
1027
1028 }
1029
1030 /**
1031 * Read console output for an instance and assign it to outjected variable.
1032 *
1033 * @param instanceId
1034 */
1035 public String getConsoleOutput(String instanceId){
1036
1037 Cloud42BaseFunctions bf = new Cloud42BaseFunctions();
1038
1039
1040 AwsCredentials cred = userManager.getUser().getCredentials();
1041 if (cred!=null){
1042
1043 bf.setCredentials(cred);
1044 bf.setRegionUrl(userManager.getUser().getRegionUrl());
1045
1046 ConsoleOutput o = bf.getConsoleOutput(instanceId);
1047
1048 if (o != null) {
1049
1050 consoleOutput = o.getOutput();
1051
1052 }
1053
1054 }
1055
1056 return "consoleOutput";
1057 }
1058
1059 }