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.models.template
22 #
23 # @author Tomek Sośnicki <tom.sosnicki@gmail.com>
24 # @author Maciej Nabożny <di.dijo@gmail.com>
25 #
26 
27 from django.db import models
28 
29 from cm.utils.exception import CMException
30 from common.states import template_states
31 
32 
33 ##
34 #
35 # @model{TEMPLATE} Class for virtual machine templates
36 #
37 # Template is what defines virtual hardware params: CPU and memory.
38 # Each template specifies amount of points consumed from user's quota
39 # per hour. Template is described by its name and description.
40 #
41 class Template(models.Model):
42  name = models.CharField(max_length=45)
43  description = models.CharField(max_length=512)
44  memory = models.IntegerField()
45  cpu = models.IntegerField()
46  template_states = (
47  (0, 'active'),
48  (1, 'deleted')
49  )
50  state = models.IntegerField(choices=template_states)
51  points = models.IntegerField()
52  ec2name = models.IntegerField(default=0)
53 
54  class Meta:
55  app_label = 'cm'
56 
57  # method for printing object instance
58  def __unicode__(self):
59  return self.name
60 
61  @property
62  ##
63  #
64  # @returns{dict} this Template's data
65  # \n fields:
66  # @dictkey{id,int}
67  # @dictkey{name,string} human-readable this Template's name displayed in web-interface
68  # @dictkey{cpu,int} Number of CPUs for VM started from this Template
69  # @dictkey{memory,int} amount of memory [MB] booked by VM started from this Template
70  # @dictkey{points,int} amount of points consumed by this VM
71  # @dictkey{description,string} human-readable description of this Template
72  # @dictkey{ec2name,string} Template's name for EC2 interface
73  #
74  def dict(self):
75  d = {}
76  d['template_id'] = self.id
77  d['name'] = self.name
78  d['cpu'] = self.cpu
79  d['memory'] = self.memory
80  d['points'] = self.points
81  d['description'] = self.description
82  d['ec2name'] = self.ec2name
83  # state is not put in dictionary
84  return d
85 
86  @staticmethod
87  ##
88  #
89  # @parameter{template_id,int} id of the requested Template
90  #
91  # @returns{Template} instance of requested Template
92  #
93  # @raises{template_get,CMException} requested Template doesn't exist
94  # or it's state isn't active
95  #
96  def get(template_id):
97  try:
98  template = Template.objects.get(pk=template_id)
99  except:
100  raise CMException('template_get')
101 
102  if not template or template.state != template_states['active']:
103  raise CMException('template_get')
104 
105  return template
106