summaryrefslogtreecommitdiff
path: root/stress/state.py
blob: 3a9f12e13e9d066dc2af3cf7feb61626cff7858f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# Copyright 2011 Quanta Research Cambridge, Inc.
#
#    Licensed under the Apache License, Version 2.0 (the "License");
#    you may not use this file except in compliance with the License.
#    You may obtain a copy of the License at
#
#        http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS,
#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#    See the License for the specific language governing permissions and
#    limitations under the License.


class ClusterState(object):
    """A class to store the state of various persistent objects in the Nova
    cluster, e.g. instances, volumes.  Use methods to query to state which than
    can be compared to the current state of the objects in Nova"""

    def __init__(self, **kwargs):
        self._max_vms = kwargs.get('max_vms', 32)
        self._instances = {}
        self._floating_ips = []
        self._keypairs = []
        self._volumes = []

    # instance state methods
    def get_instances(self):
        """return the instances dictionary that we believe are in cluster."""
        return self._instances

    def get_max_instances(self):
        """return the maximum number of instances we can create."""
        return self._max_vms

    def set_instance_state(self, key, val):
        """Store `val` in the dictionary indexed at `key`."""
        self._instances[key] = val

    def delete_instance_state(self, key):
        """Delete state indexed at `key`."""
        del self._instances[key]

    #floating_ip state methods
    def get_floating_ips(self):
        """return the floating ips list for the cluster."""
        return self._floating_ips

    def add_floating_ip(self, floating_ip_state):
        """Add floating ip."""
        self._floating_ips.append(floating_ip_state)

    def remove_floating_ip(self, floating_ip_state):
        """Remove floating ip."""
        self._floating_ips.remove(floating_ip_state)

    # keypair methods
    def get_keypairs(self):
        """return the keypairs list for the cluster."""
        return self._keypairs

    def add_keypair(self, keypair_state):
        """Add keypair."""
        self._keypairs.append(keypair_state)

    def remove_keypair(self, keypair_state):
        """Remove keypair."""
        self._keypairs.remove(keypair_state)

    # volume methods
    def get_volumes(self):
        """return the volumes list for the cluster."""
        return self._volumes

    def add_volume(self, volume_state):
        """Add volume."""
        self._volumes.append(volume_state)

    def remove_volume(self, volume_state):
        """Remove volume."""
        self._volumes.remove(volume_state)


class ServerAssociatedState(object):
    """Class that tracks resources that are associated with a particular server
    such as a volume or floating ip"""

    def __init__(self, resource_id):
        # The id of the server.
        self.server_id = None
        # The id of the resource that is attached to the server.
        self.resource_id = resource_id
        # True if in the process of attaching/detaching the resource.
        self.change_pending = False


class FloatingIpState(ServerAssociatedState):

    def __init__(self, ip_desc):
        super(FloatingIpState, self).__init__(ip_desc['id'])
        self.address = ip_desc['ip']


class VolumeState(ServerAssociatedState):

    def __init__(self, volume_desc):
        super(VolumeState, self).__init__(volume_desc['id'])


class KeyPairState(object):

    def __init__(self, keypair_spec):
        self.name = keypair_spec['name']
        self.private_key = keypair_spec['private_key']