Sean Dague | 7011236 | 2012-04-03 13:48:49 -0400 | [diff] [blame] | 1 | # 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 Dague | 7011236 | 2012-04-03 13:48:49 -0400 | [diff] [blame] | 14 | |
| 15 | |
David Kranz | 779c7f8 | 2012-05-01 16:50:32 -0400 | [diff] [blame] | 16 | class 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 |
| 19 | can be compared to the current state of the objects in Nova""" |
Sean Dague | 7011236 | 2012-04-03 13:48:49 -0400 | [diff] [blame] | 20 | |
| 21 | def __init__(self, **kwargs): |
| 22 | self._max_vms = kwargs.get('max_vms', 32) |
| 23 | self._instances = {} |
David Kranz | 779c7f8 | 2012-05-01 16:50:32 -0400 | [diff] [blame] | 24 | self._floating_ips = [] |
| 25 | self._keypairs = [] |
| 26 | self._volumes = [] |
Sean Dague | 7011236 | 2012-04-03 13:48:49 -0400 | [diff] [blame] | 27 | |
David Kranz | 779c7f8 | 2012-05-01 16:50:32 -0400 | [diff] [blame] | 28 | # instance state methods |
Sean Dague | 7011236 | 2012-04-03 13:48:49 -0400 | [diff] [blame] | 29 | def get_instances(self): |
| 30 | """return the instances dictionary that we believe are in cluster.""" |
| 31 | return self._instances |
| 32 | |
| 33 | def get_max_instances(self): |
| 34 | """return the maximum number of instances we can create.""" |
| 35 | return self._max_vms |
| 36 | |
| 37 | def set_instance_state(self, key, val): |
| 38 | """Store `val` in the dictionary indexed at `key`.""" |
| 39 | self._instances[key] = val |
| 40 | |
| 41 | def delete_instance_state(self, key): |
| 42 | """Delete state indexed at `key`.""" |
| 43 | del self._instances[key] |
David Kranz | 779c7f8 | 2012-05-01 16:50:32 -0400 | [diff] [blame] | 44 | |
| 45 | #floating_ip state methods |
| 46 | def get_floating_ips(self): |
| 47 | """return the floating ips list for the cluster.""" |
| 48 | return self._floating_ips |
| 49 | |
| 50 | def add_floating_ip(self, floating_ip_state): |
| 51 | """Add floating ip.""" |
| 52 | self._floating_ips.append(floating_ip_state) |
| 53 | |
| 54 | def remove_floating_ip(self, floating_ip_state): |
| 55 | """Remove floating ip.""" |
| 56 | self._floating_ips.remove(floating_ip_state) |
| 57 | |
| 58 | # keypair methods |
| 59 | def get_keypairs(self): |
| 60 | """return the keypairs list for the cluster.""" |
| 61 | return self._keypairs |
| 62 | |
| 63 | def add_keypair(self, keypair_state): |
| 64 | """Add keypair.""" |
| 65 | self._keypairs.append(keypair_state) |
| 66 | |
| 67 | def remove_keypair(self, keypair_state): |
| 68 | """Remove keypair.""" |
| 69 | self._keypairs.remove(keypair_state) |
| 70 | |
| 71 | # volume methods |
| 72 | def get_volumes(self): |
| 73 | """return the volumes list for the cluster.""" |
| 74 | return self._volumes |
| 75 | |
| 76 | def add_volume(self, volume_state): |
| 77 | """Add volume.""" |
| 78 | self._volumes.append(volume_state) |
| 79 | |
| 80 | def remove_volume(self, volume_state): |
| 81 | """Remove volume.""" |
| 82 | self._volumes.remove(volume_state) |
| 83 | |
| 84 | |
| 85 | class ServerAssociatedState(object): |
| 86 | """Class that tracks resources that are associated with a particular server |
| 87 | such as a volume or floating ip""" |
| 88 | |
| 89 | def __init__(self, resource_id): |
| 90 | # The id of the server. |
| 91 | self.server_id = None |
| 92 | # The id of the resource that is attached to the server. |
| 93 | self.resource_id = resource_id |
| 94 | # True if in the process of attaching/detaching the resource. |
| 95 | self.change_pending = False |
| 96 | |
| 97 | |
| 98 | class FloatingIpState(ServerAssociatedState): |
| 99 | |
| 100 | def __init__(self, ip_desc): |
| 101 | super(FloatingIpState, self).__init__(ip_desc['id']) |
| 102 | self.address = ip_desc['ip'] |
| 103 | |
| 104 | |
| 105 | class VolumeState(ServerAssociatedState): |
| 106 | |
| 107 | def __init__(self, volume_desc): |
| 108 | super(VolumeState, self).__init__(volume_desc['id']) |
| 109 | |
| 110 | |
| 111 | class KeyPairState(object): |
| 112 | |
| 113 | def __init__(self, keypair_spec): |
| 114 | self.name = keypair_spec['name'] |
| 115 | self.private_key = keypair_spec['private_key'] |