cc1  v2.1
CC1 source code docs
 All Classes Namespaces Files Functions Variables Pages
template.py
Go to the documentation of this file.
1 # -*- coding: utf-8 -*-
2 # @COPYRIGHT_begin
3 #
4 # Copyright [2010-2014] Institute of Nuclear Physics PAN, Krakow, Poland
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #
18 # @COPYRIGHT_end
19 
20 ##
21 # @package src.cm.views.admin_cm.template
22 #
23 # @alldecoratedby{src.cm.utils.decorators.admin_cm_log}
24 #
25 # @author Tomek Sośnicki <tom.sosnicki@gmail.com>
26 #
27 from cm.models.template import Template
28 from cm.utils.exception import CMException
29 from common.states import template_states
30 from cm.utils.decorators import admin_cm_log
31 
32 
33 @admin_cm_log(log=False)
34 ##
35 #
36 # Returns list of Templates.
37 #
38 # @cmview_admin_cm
39 # @response{list(dict)} Template.dict property of each Template
40 #
41 def get_list(caller_id):
42 
43  try:
44  templates = [t.dict for t in Template.objects.filter(state__exact=template_states['active']).order_by('cpu', 'memory')]
45  except:
46  raise CMException("template_list")
47 
48  return templates
49 
50 
51 @admin_cm_log(log=True)
52 ##
53 #
54 # @cmview_admin_cm
55 # @param_post{template_id,int}
56 #
57 # @response{dict} Template.dict property of the requested Template
58 #
59 def get_by_id(caller_id, template_id):
60 
61  try:
62  t = Template.objects.get(id=template_id)
63 
64  except:
65  raise CMException("template_get")
66 
67  return t.dict
68 
69 
70 @admin_cm_log(log=True)
71 ##
72 #
73 # Creates and saves new VM Template.
74 #
75 # @cmview_admin_cm
76 # @param_post{name,string}
77 # @param_post{memory,int}
78 # @param_post{cpu,int}
79 # @param_post{description,string}
80 # @param_post{points,int}
81 # @param_post{ec2name,string} name for EC2 interface
82 #
83 def add(caller_id, name, memory, cpu, description, points, ec2name):
84 
85  try:
86  template = Template()
87  template.name = name
88  template.memory = memory
89  template.cpu = cpu
90  template.description = description
91  template.points = points
92  template.ec2name = ec2name
93  template.state = template_states['active']
94  template.save()
95 
96  except:
97  raise CMException('template_create')
98 
99 
100 @admin_cm_log(log=True)
101 ##
102 #
103 # Sets specified Template's state as @val{deleted}.
104 #
105 # @cmview_admin_cm
106 # @param_post{template_id,int} id of the Template to remove.
107 #
108 def delete(caller_id, template_id):
109 
110  try:
111  template = Template.objects.get(pk=template_id)
112  template.state = template_states['deleted']
113  template.save()
114  except:
115  raise CMException('template_delete')
116 
117 
118 @admin_cm_log(log=True)
119 ##
120 #
121 # Updates specified Template's attributes.
122 #
123 # @cmview_admin_cm
124 # @param_post{template_id,int} id of the Template to edit
125 # @param_post{name,string}
126 # @param_post{memory,int}
127 # @param_post{cpu,int}
128 # @param_post{description,string}
129 # @param_post{points,int}
130 # @param_post{ec2name,int}
131 #
132 def edit(caller_id, template_id, name=None, memory=None, cpu=None, description=None, points=None, ec2name=None):
133 
134  try:
135  template = Template.objects.get(pk=template_id)
136  template.name = name or template.name
137  template.memory = memory or template.memory
138  template.cpu = cpu or template.cpu
139  template.description = description or template.description
140  template.points = points or template.points
141  template.ec2name = ec2name or template.ec2name
142  template.save()
143  except:
144  raise CMException('template_edit')
145