ia64/xen-unstable

view tools/python/xen/xend/server/SrvVnetDir.py @ 13642:ba3ec84c9423

[XEND] Add missing ConsoleController.py

Signed-off-by: Alastair Tse <atse@xensource.com>
author Alastair Tse <atse@xensource.com>
date Fri Jan 26 02:44:35 2007 +0000 (2007-01-26)
parents 06d84bf87159
children
line source
1 #============================================================================
2 # This library is free software; you can redistribute it and/or
3 # modify it under the terms of version 2.1 of the GNU Lesser General Public
4 # License as published by the Free Software Foundation.
5 #
6 # This library is distributed in the hope that it will be useful,
7 # but WITHOUT ANY WARRANTY; without even the implied warranty of
8 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9 # Lesser General Public License for more details.
10 #
11 # You should have received a copy of the GNU Lesser General Public
12 # License along with this library; if not, write to the Free Software
13 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
14 #============================================================================
15 # Copyright (C) 2004, 2005 Mike Wray <mike.wray@hp.com>
16 #============================================================================
18 from xen.xend import sxp
19 from xen.xend.Args import FormFn
20 from xen.xend import PrettyPrint
21 from xen.xend import XendVnet
22 from xen.xend.XendError import XendError
24 from xen.web.SrvDir import SrvDir
26 class SrvVnet(SrvDir):
28 def __init__(self, vnetinfo):
29 SrvDir.__init__(self)
30 self.vnetinfo = vnetinfo
31 self.xvnet = XendVnet.instance()
33 def op_delete(self, op, req):
34 val = self.xvnet.vnet_delete(self.vnetinfo.id)
35 return val
37 def render_POST(self, req):
38 return self.perform(req)
40 def render_GET(self, req):
41 if self.use_sxp(req):
42 req.setHeader("Content-Type", sxp.mime_type)
43 sxp.show(self.vnetinfo.sxpr(), out=req)
44 else:
45 req.write('<html><head></head><body>')
46 self.print_path(req)
47 req.write('<p>Vnet %s</p>' % self.vnetinfo.id)
48 req.write("<code><pre>")
49 PrettyPrint.prettyprint(self.vnetinfo.sxpr(), out=req)
50 req.write("</pre></code>")
51 self.form(req)
52 req.write('</body></html>')
53 return ''
55 def form(self, req):
56 url = req.prePathURL()
57 req.write('<form method="post" action="%s">' % url)
58 req.write('<input type="submit" name="op" value="delete">')
59 req.write('</form>')
61 class SrvVnetDir(SrvDir):
62 """Vnet directory.
63 """
65 def __init__(self):
66 SrvDir.__init__(self)
67 self.xvnet = XendVnet.instance()
69 def vnet(self, x):
70 val = None
71 vnetinfo = self.xvnet.vnet_get(x)
72 if not vnetinfo:
73 raise XendError('No such vnet ' + str(x))
74 val = SrvVnet(vnetinfo)
75 return val
77 def get(self, x):
78 v = SrvDir.get(self, x)
79 if v is not None:
80 return v
81 v = self.vnet(x)
82 return v
84 def op_create(self, op, req):
85 fn = FormFn(self.xvnet.vnet_create,
86 [['config', 'sxpr']])
87 val = fn(req.args, {})
88 return val
90 def render_POST(self, req):
91 return self.perform(req)
93 def render_GET(self, req):
94 if self.use_sxp(req):
95 req.setHeader("Content-Type", sxp.mime_type)
96 self.ls_vnet(req, 1)
97 else:
98 req.write("<html><head></head><body>")
99 self.print_path(req)
100 self.ls(req)
101 self.ls_vnet(req)
102 self.form(req)
103 req.write("</body></html>")
105 def ls_vnet(self, req, use_sxp=0):
106 url = req.prePathURL()
107 if not url.endswith('/'):
108 url += '/'
109 if use_sxp:
110 vnets = self.xvnet.vnet_ls()
111 sxp.show(vnets, out=req)
112 else:
113 vnets = self.xvnet.vnets()
114 vnets.sort(lambda x, y: cmp(x.id, y.id))
115 req.write('<ul>')
116 for v in vnets:
117 req.write('<li><a href="%s%s"> Vnet %s</a>' % (url, v.id, v.id))
118 req.write('</li>')
119 req.write('</ul>')
121 def form(self, req):
122 """Generate the form(s) for vnet dir operations.
123 """
124 req.write('<form method="post" action="%s" enctype="multipart/form-data">'
125 % req.prePathURL())
126 req.write('<button type="submit" name="op" value="create">Create Vnet</button>')
127 req.write('Config <input type="file" name="config"><br>')
128 req.write('</form>')