summaryrefslogtreecommitdiff
path: root/ironic/drivers/modules/fake.py
blob: 6bd70173969f8db8524beb80ba9561653a23f4c5 (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# -*- encoding: utf-8 -*-
#
# Copyright 2013 Hewlett-Packard Development Company, L.P.
#
# 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.
"""
Fake driver interfaces used in testing.

This is also an example of some kinds of things which can be done within
drivers.  For instance, the MultipleVendorInterface class demonstrates how to
load more than one interface and wrap them in some logic to route incoming
vendor_passthru requests appropriately. This can be useful eg. when mixing
functionality between a power interface and a deploy interface, when both rely
on seprate vendor_passthru methods.
"""

from ironic.common import boot_devices
from ironic.common import exception
from ironic.common import states
from ironic.drivers import base


def _raise_unsupported_error(method=None):
    if method:
        raise exception.InvalidParameterValue(_(
            "Unsupported method (%s) passed through to vendor extension.")
            % method)
    raise exception.InvalidParameterValue(_(
        "Method not specified when calling vendor extension."))


class FakePower(base.PowerInterface):
    """Example implementation of a simple power interface."""

    def validate(self, task, node):
        pass

    def get_power_state(self, task):
        return task.node.power_state

    def set_power_state(self, task, power_state):
        if power_state not in [states.POWER_ON, states.POWER_OFF]:
            raise exception.InvalidParameterValue(_("set_power_state called "
                    "with an invalid power state: %s.") % power_state)
        task.node.power_state = power_state

    def reboot(self, task):
        pass


class FakeDeploy(base.DeployInterface):
    """Example imlementation of a deploy interface that uses a
       separate power interface.
    """

    def validate(self, task, node):
        pass

    def deploy(self, task):
        pass

    def tear_down(self, task):
        pass

    def prepare(self, task):
        pass

    def clean_up(self, task):
        pass

    def take_over(self, task):
        pass


class FakeVendorA(base.VendorInterface):
    """Example implementation of a vendor passthru interface."""

    def validate(self, task, **kwargs):
        method = kwargs.get('method')
        if method == 'first_method':
            bar = kwargs.get('bar')
            if not bar:
                raise exception.InvalidParameterValue(_(
                    "Parameter 'bar' not passed to method 'first_method'."))
            return
        _raise_unsupported_error(method)

    def _private_method(self, task, bar):
        return True if bar == 'baz' else False

    def vendor_passthru(self, task, **kwargs):
        method = kwargs.get('method')
        if method == 'first_method':
            bar = kwargs.get('bar')
            return self._private_method(task, bar)
        _raise_unsupported_error(method)


class FakeVendorB(base.VendorInterface):
    """Example implementation of a secondary vendor passthru."""

    def validate(self, task, **kwargs):
        method = kwargs.get('method')
        if method == 'second_method':
            bar = kwargs.get('bar')
            if not bar:
                raise exception.InvalidParameterValue(_(
                    "Parameter 'bar' not passed to method 'second_method'."))
            return
        _raise_unsupported_error(method)

    def _private_method(self, task, bar):
        return True if bar == 'kazoo' else False

    def vendor_passthru(self, task, **kwargs):
        method = kwargs.get('method')
        if method == 'second_method':
            bar = kwargs.get('bar')
            return self._private_method(task, bar)
        _raise_unsupported_error(method)


class FakeConsole(base.ConsoleInterface):
    """Example implementation of a simple console interface."""

    def validate(self, task, node):
        return True

    def start_console(self, task, node):
        pass

    def stop_console(self, task, node):
        pass

    def get_console(self, task, node):
        return {}


class FakeManagement(base.ManagementInterface):
    """Example implementation of a simple management interface."""

    def validate(self, task, node):
        return True

    def get_supported_boot_devices(self):
        return [boot_devices.PXE]

    def set_boot_device(self, task, device, **kwargs):
        if device not in self.get_supported_boot_devices():
            raise exception.InvalidParameterValue(_(
                "Invalid boot device %s specified.") % device)

    def get_boot_device(self, task):
        return boot_devices.PXE