blob: 9c31b76fb7ea62fb892382155969c0fe5d1502c0 [file] [log] [blame]
Sean Dague70112362012-04-03 13:48:49 -04001# Copyright 2011 Quanta Research Cambridge, Inc.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
Sean Dague70112362012-04-03 13:48:49 -040014
15
David Kranz779c7f82012-05-01 16:50:32 -040016class ClusterState(object):
17 """A class to store the state of various persistent objects in the Nova
18 cluster, e.g. instances, volumes. Use methods to query to state which than
Attila Fazekasb2902af2013-02-16 16:22:44 +010019 can be compared to the current state of the objects in Nova.
20 """
Sean Dague70112362012-04-03 13:48:49 -040021
22 def __init__(self, **kwargs):
23 self._max_vms = kwargs.get('max_vms', 32)
24 self._instances = {}
David Kranz779c7f82012-05-01 16:50:32 -040025 self._floating_ips = []
26 self._keypairs = []
27 self._volumes = []
Sean Dague70112362012-04-03 13:48:49 -040028
David Kranz779c7f82012-05-01 16:50:32 -040029 # instance state methods
Sean Dague70112362012-04-03 13:48:49 -040030 def get_instances(self):
31 """return the instances dictionary that we believe are in cluster."""
32 return self._instances
33
34 def get_max_instances(self):
35 """return the maximum number of instances we can create."""
36 return self._max_vms
37
38 def set_instance_state(self, key, val):
39 """Store `val` in the dictionary indexed at `key`."""
40 self._instances[key] = val
41
42 def delete_instance_state(self, key):
43 """Delete state indexed at `key`."""
44 del self._instances[key]
David Kranz779c7f82012-05-01 16:50:32 -040045
46 #floating_ip state methods
47 def get_floating_ips(self):
48 """return the floating ips list for the cluster."""
49 return self._floating_ips
50
51 def add_floating_ip(self, floating_ip_state):
52 """Add floating ip."""
53 self._floating_ips.append(floating_ip_state)
54
55 def remove_floating_ip(self, floating_ip_state):
56 """Remove floating ip."""
57 self._floating_ips.remove(floating_ip_state)
58
59 # keypair methods
60 def get_keypairs(self):
61 """return the keypairs list for the cluster."""
62 return self._keypairs
63
64 def add_keypair(self, keypair_state):
65 """Add keypair."""
66 self._keypairs.append(keypair_state)
67
68 def remove_keypair(self, keypair_state):
69 """Remove keypair."""
70 self._keypairs.remove(keypair_state)
71
72 # volume methods
73 def get_volumes(self):
74 """return the volumes list for the cluster."""
75 return self._volumes
76
77 def add_volume(self, volume_state):
78 """Add volume."""
79 self._volumes.append(volume_state)
80
81 def remove_volume(self, volume_state):
82 """Remove volume."""
83 self._volumes.remove(volume_state)
84
85
86class ServerAssociatedState(object):
87 """Class that tracks resources that are associated with a particular server
Attila Fazekasb2902af2013-02-16 16:22:44 +010088 such as a volume or floating ip.
89 """
David Kranz779c7f82012-05-01 16:50:32 -040090
91 def __init__(self, resource_id):
92 # The id of the server.
93 self.server_id = None
94 # The id of the resource that is attached to the server.
95 self.resource_id = resource_id
96 # True if in the process of attaching/detaching the resource.
97 self.change_pending = False
98
99
100class FloatingIpState(ServerAssociatedState):
101
102 def __init__(self, ip_desc):
103 super(FloatingIpState, self).__init__(ip_desc['id'])
104 self.address = ip_desc['ip']
105
106
107class VolumeState(ServerAssociatedState):
108
109 def __init__(self, volume_desc):
110 super(VolumeState, self).__init__(volume_desc['id'])
111
112
113class KeyPairState(object):
114
115 def __init__(self, keypair_spec):
116 self.name = keypair_spec['name']
117 self.private_key = keypair_spec['private_key']