Statistics
| Branch: | Tag: | Revision:

one / src / sunstone / models / OpenNebulaJSON / VirtualMachineJSON.rb @ 6509e422

History | View | Annotate | Download (4.73 KB)

1
# -------------------------------------------------------------------------- #
2
# Copyright 2002-2011, OpenNebula Project Leads (OpenNebula.org)             #
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

    
21
    class VirtualMachineJSON < OpenNebula::VirtualMachine
22
        include JSONUtils
23

    
24
        def create(template_json)
25
            vm_hash = parse_json(template_json, 'vm')
26
            if OpenNebula.is_error?(vm_hash)
27
                return vm_hash
28
            end
29

    
30
            if vm_hash['vm_raw']
31
                template = vm_hash['vm_raw']
32
            else
33
                template = template_to_str(vm_hash)
34
            end
35

    
36
            self.allocate(template)
37
       end
38

    
39
        def perform_action(template_json)
40
            action_hash = parse_json(template_json,'action')
41
            if OpenNebula.is_error?(action_hash)
42
                return action_hash
43
            end
44

    
45
            rc = case action_hash['perform']
46
                when "cancel"       then self.cancel
47
                when "deploy"       then self.deploy(action_hash['params'])
48
                when "finalize"     then self.finalize
49
                when "hold"         then self.hold
50
                when "livemigrate"  then self.live_migrate(action_hash['params'])
51
                when "migrate"      then self.migrate(action_hash['params'])
52
                when "resume"       then self.resume
53
                when "release"      then self.release
54
                when "stop"         then self.stop
55
                when "suspend"      then self.suspend
56
                when "restart"      then self.restart
57
                when "saveas"       then self.save_as(action_hash['params'])
58
                when "shutdown"     then self.shutdown
59
                when "resubmit"     then self.resubmit
60
                else
61
                    error_msg = "#{action_hash['perform']} action not " <<
62
                                " available for this resource"
63
                    OpenNebula::Error.new(error_msg)
64
            end
65
        end
66

    
67
        def delete
68
            self.finalize
69
        end
70

    
71
        def deploy(params=Hash.new)
72
            super(params['host_id'])
73
        end
74

    
75
        def live_migrate(params=Hash.new)
76
            super(params['host_id'])
77
        end
78

    
79
        def migrate(params=Hash.new)
80
            super(params['host_id'])
81
        end
82

    
83
        def save_as(params=Hash.new)
84
            if params['image_type']
85
                image_type = params['image_type']
86
            else
87
                image_id = self["TEMPLATE/DISK[DISK_ID=\"#{params[:disk_id]}\"]/IMAGE_ID"]
88

    
89
                if (image_id != nil)
90
                    if self["TEMPLATE/DISK[DISK_ID=\"#{disk_id}\"]/SAVE_AS"]
91
                        error_msg = "Error: The disk #{disk_id} is already" <<
92
                                    " supposed to be saved"
93
                        return OpenNebula::Error.new(error_msg)
94
                    end
95

    
96
                    # Get the image type
97
                    image = OpenNebula::Image.new(
98
                                OpenNebula::Image.build_xml(image_id), @client)
99

    
100
                    result = image.info
101
                    if OpenNebula.is_error?(result)
102
                        return result
103
                    end
104

    
105
                    image_type = image.type_str
106
                end
107
            end
108

    
109
            # Build the template and allocate the new Image
110
            template = "NAME=\"#{params['image_name']}\"\n"
111
            template << "TYPE=#{image_type}\n" if image_type
112

    
113
            image = OpenNebula::Image.new(OpenNebula::Image.build_xml, @client)
114

    
115
            result = image.allocate(template)
116
            if OpenNebula.is_error?(result)
117
                return result
118
            end
119

    
120
            super(params['disk_id'].to_i, image.id)
121
        end
122
    end
123
end