Class: Zona::Client

Inherits:
Object
  • Object
show all
Defined in:
zona.rb

Overview

OZones Client provides functionality to send and retrieve information from the OZones server side. It is used by CLI and API to handle the http requests to the server and basic error control on the responses.

Constant Summary

OZONES_VERSION =

Provides current version information. Should match server’s oZones version.

"oZones 1.0\nCopyright 2002-2012, OpenNebula Project Leads (OpenNebula.org)\n\nLicensed under the Apache License, Version 2.0 (the \"License\"); you may\nnot use this file except in compliance with the License. You may obtain\na copy of the License at http://www.apache.org/licenses/LICENSE-2.0\n"

Instance Method Summary (collapse)

Constructor Details

- (Client) initialize(user = nil, pass = nil, endpoint_str = nil, timeout = nil, debug_flag = true)

Initialize client instance

Parameters:

  • user (String) (defaults to: nil)

    oZones username

  • pass (String) (defaults to: nil)

    oZones password

  • endpoint (String)

    Server endpoint

  • timeout (Integer) (defaults to: nil)

    client timout, defaults means no timeout

  • debug_flag (Boolean) (defaults to: true)

    produce debug information



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
# File 'zona.rb', line 65

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

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

    # Autentication
    if user && pass
        @ozonesauth = [user, pass]
    elsif ENV['OZONES_AUTH']
        @ozonesauth=File.read(ENV['OZONES_AUTH']).strip.split(':')
    end

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

    if @ozonesauth.size != 2
        raise "Authorization data malformed"
    end
end

Instance Method Details

- (String, Zona::Error) delete_resource(kind, id)

Deletes a resource

Parameters:

  • kind (String)

    resource kind: vdc, zone…

  • id (#to_i)

    resource id

Returns:



215
216
217
218
219
220
221
222
223
224
225
226
# File 'zona.rb', line 215

def delete_resource(kind, id)
    url = URI.parse("#{@endpoint}/#{kind}/#{id}")
    req = Net::HTTP::Delete.new(url.path)

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

    res = Client.http_start(url, @timeout) {|http|
        http.request(req)
    }

    return Client.parse_error(res, kind)
end

- (String, Zone::Error) get_pool(kind)

Retrieves all elements of a kind (pool)

Parameters:

  • kind (String)

    element kind

Returns:

  • (String, Zone::Error)

    Response string or Error



98
99
100
101
102
103
104
105
106
107
108
109
# File 'zona.rb', line 98

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

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

    res = Client.http_start(url, @timeout) {|http|
        http.request(req)
    }

    return Client.parse_error(res, kind)
end

- (String, Zona::Error) get_resource(kind, id)

Retrieves a resource

Parameters:

  • Kind (String)

    resource kind: vdc, zone…

  • id (#to_i)

    resource id

Returns:



181
182
183
184
185
186
187
188
189
190
191
192
# File 'zona.rb', line 181

def get_resource(kind, id)
    url = URI.parse("#{@endpoint}/#{kind}/#{id}")
    req = Net::HTTP::Get.new(url.path)

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

    res = Client.http_start(url, @timeout) {|http|
        http.request(req)
    }

    return Client.parse_error(res, kind)
end

- (String, Zona::Error) get_resource_pool(kind, id, pool)

Retrieves a pool belonging to a specific resource

Parameters:

  • Kind (String)

    resource kind: vdc, zone…

  • id (#to_i)

    resource id

  • Kind (String)

    of pool: image, vm, host, etc

Returns:



199
200
201
202
203
204
205
206
207
208
209
# File 'zona.rb', line 199

def get_resource_pool(kind, id, pool)
    url = URI.parse("#{@endpoint}/#{kind}/#{id}/#{pool}")
    req = Net::HTTP::Get.new(url.path)

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

    res = Client.http_start(url, @timeout) {|http|
        http.request(req)
    }
    return Client.parse_error(res, kind)
end

- (String, Zona::Error) post_resource(kind, tmpl_json)

Creates a resource

Parameters:

  • kind (String)

    resource kind: vdc, zone…

  • tmpl_json (String)

    JSON template

Returns:



133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'zona.rb', line 133

def post_resource(kind, tmpl_json)
    url = URI.parse("#{@endpoint}/#{kind}")

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

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

    res = Client.http_start(url, @timeout) do |http|
        http.request(req)
    end

    return Client.parse_error(res, kind)
end

- (String, Zona::Error) post_resource_file(kind, template)

Creates a resource from an opennebula template file

Parameters:

  • kind (String)

    resource kind: vdc,zone…

  • template (String)

    path to template file

Returns:



115
116
117
118
# File 'zona.rb', line 115

def post_resource_file(kind, template)
    tmpl_str = File.read(template)
    post_resource_str(kind, tmpl_str)
end

- (String, Zona::Error) post_resource_str(kind, tmpl_str)

Creates a resource from an OpenNebula template string

Parameters:

  • kind (String)

    resource kind: vdc,zone…

  • tmpl_str (String)

    OpenNebula template string

Returns:



124
125
126
127
# File 'zona.rb', line 124

def post_resource_str(kind, tmpl_str)
    tmpl_json = Zona.parse_template(kind, tmpl_str)
    post_resource(kind, tmpl_json)
end

- (String, Zona::Error) put_resource(kind, id, tmpl_json)

Modifies a resource

Parameters:

  • kind (String)

    resource kind: vdc, zone…

  • tmpl_json (String)

    JSON template

Returns:



161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'zona.rb', line 161

def put_resource(kind, id, tmpl_json)
    url = URI.parse("#{@endpoint}/#{kind}/#{id}")

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

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

    res = Client.http_start(url, @timeout) do |http|
        http.request(req)
    end

    return Client.parse_error(res, kind)
end

- (String, Zona::Error) put_resource_str(kind, id, tmpl_str)

Modifies a resource from an OpenNebula template string

Parameters:

  • kind (String)

    resource kind: vdc, zone…

  • tmpl_str (String)

    OpenNebula template string

Returns:



152
153
154
155
# File 'zona.rb', line 152

def put_resource_str(kind, id, tmpl_str)
    tmpl_json = Zona.parse_template(kind, tmpl_str)
    put_resource(kind, id, tmpl_json)
end