Merge branch 'master' of ssh://git.onelab.eu/git/sfa-gui
[sfa-gui.git] / RequestPanel.py
1 from pyjamas.ui.VerticalPanel import VerticalPanel
2 from pyjamas.ui.HorizontalPanel import HorizontalPanel
3 from pyjamas.ui.Button import Button
4 from pyjamas.ui.HTML import HTML
5 from pyjamas.ui import HasAlignment
6 from pyjamas.ui.Label import Label
7 from pyjamas.ui.ListBox import ListBox
8 from pyjamas.ui.TextBox import TextBox
9
10 from lxml import etree
11
12 ##
13 # Extracts information from a RSpec and turns it
14 # into a dictionary.
15 # @return A dict
16 def extractRSpec(rspec):
17     rspecXML = etree.XML(rspec)
18     data = {}
19     data['images'] = {}
20     data['images']['kernel'] = []
21     data['images']['ramdisk'] = []
22     data['images']['machine'] = []
23     images = rspecXML.findall('.//image')
24     for img in images:
25         imgType = ''
26         location = ''
27         for element in img.iter():
28             if element.tag == 'type': imgType = element.text
29             if element.tag == 'location': location = element.text
30         data['images'][imgType].append((img.get('id'), location))
31
32     data['keys'] = []
33     keys = rspecXML.findall('.//keypair')
34     for key in keys:
35         data['keys'].append(key.text)
36
37     data['vmtypes'] = []
38     vmtypes = rspecXML.findall('.//vm_type')
39     for vmtype in vmtypes:
40         data['vmtypes'].append(vmtype.get('name'))
41
42     return data
43
44 ##
45 # A panel that represents a request stanza.
46 class RequestInstanceWidget(VerticalPanel):
47     def __init__(self, rdata):
48         VerticalPanel.__init__(self)
49         self.setBorderWidth(1)
50         self.rspecData = rdata
51     
52         self.createKeyPanel()
53         self.createVmTypePanel()
54         self.createImagePanels()
55         self.createNumPanel()
56     
57     ##
58     # Creates the horizontal panels for kernel, ramdisk, 
59     # and disk images.
60     def createImagePanels(self):
61         self.add(self.__createImagePanel('machine'))
62         self.add(self.__createImagePanel('kernel'))
63         self.add(self.__createImagePanel('ramdisk'))
64
65     ##
66     # Helper method to create individual panel for each image
67     # types.
68     # @param[String] The image type.
69     # @return An instance of HorizontalPanel with a label and 
70     #         a droplist box.
71     def __createImagePanel(self, image):
72         hp = HorizontalPanel()
73         panelLabel = Label(image.capitalize())
74         lb = ListBox()
75         for imgData in self.rspecData['images'][image]:
76             lb.addItem(imgData[1], imgData[0])
77         hp.add(panelLabel)
78         hp.add(lb)
79
80         return hp
81
82     ##
83     # Creates a H. panel for key pairs.
84     def createKeyPanel(self):
85         hp = HorizontalPanel()
86         panelLabel = Label('Key Pairs')
87         lb = ListBox()
88         for kp in self.rspecData['keys']:
89             lb.addItem(kp)
90         hp.add(panelLabel)
91         hp.add(lb)
92         self.add(hp)
93         
94     ##
95     # Creates a H. panel for Vm Types.
96     # TODO: refactor by combining this with createKeyPanel()
97     def createVmTypePanel(self):
98         hp = HorizontalPanel()
99         panelLabel = Label('VM Types')
100         lb = ListBox()
101         for vt in self.rspecData['vmtypes']:
102             lb.addItem(vt)
103         hp.add(panelLabel)
104         hp.add(lb)
105         self.add(hp)
106
107     ##
108     # Creates a panel for specifying the number of instances 
109     # to create.
110     def createNumPanel(self):
111         hp = HorizontalPanel()
112         panelLabel = Label('VM Types')
113         tb = TextBox()
114         hp.add(panelLabel)
115         hp.add(tb)
116         self.add(hp)
117
118 class RequestPanel(VerticalPanel):
119     def __init__(self, sfadata):
120         VerticalPanel.__init__(self)
121         self.sfadata = sfadata
122         self.setStyleName("ks-layouts")
123
124         self.rspec = self.sfadata.getRSpec()
125         self.rspecData = extractRSpec(self.rspec)
126
127         self.add(RequestInstanceWidget(self.rspecData))
128         
129     def refresh(self):
130         pass
131                     
132     def apply(self, sender):
133         self.sfadata.applyRSpec()
134
135     def reset(self, sender):
136         self.sfadata.refreshRSpec()
137         self.rspecData = extractRSpec(self.rspec)
138