Class: OCCIClient::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/deltacloud/drivers/opennebula/occi_client.rb

Overview

Client Library to interface with the OpenNebula OCCI Service

Instance Method Summary collapse

Constructor Details

#initialize(endpoint_str = nil, user = nil, pass = nil, debug_flag = true) ⇒ Client

Initialize client library



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
# File 'lib/deltacloud/drivers/opennebula/occi_client.rb', line 37

def initialize(endpoint_str=nil, user=nil, pass=nil, debug_flag=true)
    @debug = debug_flag

    # Server location
    if endpoint_str
        @endpoint =  endpoint_str
    elsif ENV["OCCI_URL"]
        @endpoint = ENV["OCCI_URL"]
    else
        @endpoint = "http://localhost:4567"
    end

    # Autentication
    if user && pass
        @occiauth = [user, pass]
    else
        @occiauth = CloudClient::get_one_auth
    end

    if !@occiauth
        raise "No authorization data present"
    end

    @occiauth[1] = Digest::SHA1.hexdigest(@occiauth[1])
end

Instance Method Details

#get_image(image_uuid) ⇒ Object

Retieves an Image :image_uuid Image identifier



187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
# File 'lib/deltacloud/drivers/opennebula/occi_client.rb', line 187

def get_image(image_uuid)
    url = URI.parse(@endpoint+"/storage/"+image_uuid)
    req = Net::HTTP::Get.new(url.path)

    req.basic_auth @occiauth[0], @occiauth[1]

    res = CloudClient::http_start(url) {|http|
        http.request(req)
    }

    if CloudClient::is_error?(res)
        return res
    else
        return res.body
    end
end

#get_imagesObject

Retieves the pool of Images owned by the user



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/deltacloud/drivers/opennebula/occi_client.rb', line 116

def get_images
    url = URI.parse(@endpoint+"/storage")
    req = Net::HTTP::Get.new(url.path)

    req.basic_auth @occiauth[0], @occiauth[1]

    res = CloudClient::http_start(url) {|http|
        http.request(req)
    }

    if CloudClient::is_error?(res)
        return res
    else
        return res.body
    end
end

#get_vm(id) ⇒ Object

:id VM identifier



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/deltacloud/drivers/opennebula/occi_client.rb', line 140

def get_vm(id)
    url = URI.parse(@endpoint+"/compute/" + id.to_s)
    req = Net::HTTP::Get.new(url.path)

    req.basic_auth @occiauth[0], @occiauth[1]

    res = CloudClient::http_start(url) {|http|
        http.request(req)
    }

    if CloudClient::is_error?(res)
        return res
    else
        return res.body
    end
end

#get_vmsObject

Retieves the pool of Virtual Machines



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/deltacloud/drivers/opennebula/occi_client.rb', line 96

def get_vms
    url = URI.parse(@endpoint+"/compute")
    req = Net::HTTP::Get.new(url.path)

    req.basic_auth @occiauth[0], @occiauth[1]

    res = CloudClient::http_start(url) {|http|
        http.request(req)
    }

    if CloudClient::is_error?(res)
        return res
    else
        return res.body
    end
end

#post_vms(xmlfile) ⇒ Object

Post a new VM to the VM Pool :instance_type :xmlfile



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/deltacloud/drivers/opennebula/occi_client.rb', line 72

def post_vms(xmlfile)
    xml=File.read(xmlfile)

    url = URI.parse(@endpoint+"/compute")

    req = Net::HTTP::Post.new(url.path)
    req.body=xml

    req.basic_auth @occiauth[0], @occiauth[1]

    res = CloudClient::http_start(url) do |http|
        http.request(req)
    end

    if CloudClient::is_error?(res)
        return res
    else
        return res.body
    end
end

#put_vm(xmlfile) ⇒ Object

Puts a new Compute representation in order to change its state :xmlfile Compute OCCI xml representation



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/deltacloud/drivers/opennebula/occi_client.rb', line 161

def put_vm(xmlfile)
    xml=File.read(xmlfile)
    vm_info=REXML::Document.new(xml).root.elements

    url = URI.parse(@endpoint+'/compute/' + vm_info['ID'].text)

    req = Net::HTTP::Put.new(url.path)
    req.body = xml

    req.basic_auth @occiauth[0], @occiauth[1]

    res = CloudClient::http_start(url) do |http|
        http.request(req)
    end

    if CloudClient::is_error?(res)
        return res
    else
        return res.body
    end
end