Statistics
| Branch: | Tag: | Revision:

one / src / sunstone / models / OpenNebulaJSON / HostJSON.rb @ aaccdf92

History | View | Annotate | Download (3.64 KB)

1
# -------------------------------------------------------------------------- #
2
# Copyright 2002-2017, OpenNebula Project, OpenNebula Systems                #
3
#                                                                            #
4
# Licensed under the Apache License, Version 2.0 (the "License"); you may    #
5
# not use this file except in compliance with the License. You may obtain    #
6
# a copy of the License at                                                   #
7
#                                                                            #
8
# http://www.apache.org/licenses/LICENSE-2.0                                 #
9
#                                                                            #
10
# Unless required by applicable law or agreed to in writing, software        #
11
# distributed under the License is distributed on an "AS IS" BASIS,          #
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
13
# See the License for the specific language governing permissions and        #
14
# limitations under the License.                                             #
15
#--------------------------------------------------------------------------- #
16

    
17
require 'OpenNebulaJSON/JSONUtils'
18

    
19
module OpenNebulaJSON
20
    class HostJSON < OpenNebula::Host
21
        include JSONUtils
22

    
23
        def create(template_json)
24
            host_hash = parse_json(template_json, 'host')
25
            if OpenNebula.is_error?(host_hash)
26
                return host_hash
27
            end
28

    
29
            id = self.allocate(host_hash['name'],
30
                          host_hash['im_mad'],
31
                          host_hash['vm_mad'],
32
                          host_hash['cluster_id'].to_i)
33
            delete_values = ['name', 'im_mad', 'vm_mad', 'cluster_id']
34

    
35
            template_str = hash_to_str(host_hash, delete_values)
36
            if !template_str.nil?
37
                params=Hash.new
38
                params['template_raw'] = template_str
39
                params['append'] = true
40
                self.update(params)
41
            end
42
        end
43

    
44
        def delete
45
            if self['HOST_SHARE/RUNNING_VMS'].to_i != 0
46
                OpenNebula::Error.new("Host still has associated VMs, aborting delete.")
47
            else
48
                super
49
            end
50
        end
51

    
52
        def perform_action(template_json)
53
            action_hash = parse_json(template_json, 'action')
54
            if OpenNebula.is_error?(action_hash)
55
                return action_hash
56
            end
57

    
58
            rc = case action_hash['perform']
59
                when "enable"  then self.enable
60
                when "disable" then self.disable
61
                when "offline" then self.offline
62
                when "update" then self.update(action_hash['params'])
63
                when "rename" then self.rename(action_hash['params'])
64
                when "import_wild" then self.import_wild(action_hash['params'])
65
                else
66
                    error_msg = "#{action_hash['perform']} action not " <<
67
                                " available for this resource"
68
                    OpenNebula::Error.new(error_msg)
69
            end
70
        end
71

    
72
        def update(params=Hash.new)
73
            if !params['append'].nil?
74
                super(params['template_raw'], params['append'])
75
            else
76
                super(params['template_raw'])
77
            end
78
        end
79

    
80
        def rename(params=Hash.new)
81
            super(params['name'])
82
        end
83

    
84
        def import_wild(params=Hash.new)
85
            rc = super(params['name'])
86
            if OpenNebula.is_error?(rc)
87
                return rc
88
            else
89
                return VirtualMachineJSON.new_with_id(rc, @client)
90
            end
91
        end
92
    end
93
end