Class: OneForm::Client

Inherits:
Object
  • Object
show all
Includes:
Drivers, Providers, Provisions
Defined in:
lib/opennebula/oneform_client.rb

Overview

OneForm API client

Constant Summary collapse

DEFAULT_OPTIONS =
[
    ENDPOINT = {
        :name => 'server',
        :short => '-s url',
        :large => '--server url',
        :format => String,
        :description => 'OneForm endpoint'
    },
    USERNAME={
        :name => 'username',
        :short => '-u name',
        :large => '--username name',
        :format => String,
        :description => 'User name'
    },
    PASSWORD={
        :name => 'password',
        :short => '-p pass',
        :large => '--password pass',
        :format => String,
        :description => 'User password'
    },
    API_VERSION={
        :name => 'api_version',
        :large => '--api-version version',
        :format => String,
        :description => 'OneForm API Version to use'
    }
]

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Client

Returns a new instance of Client.



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
# File 'lib/opennebula/oneform_client.rb', line 68

def initialize(opts = {})
    endpoint  = '/.one/oneform_endpoint'
    @username = opts[:username] || ENV['ONEFORM_USER']
    @password = opts[:password] || ENV['ONEFORM_PASSWORD']

    # By default oneform uses JSON as content type
    @content_type = opts[:content_type] || 'application/json'

    # Set API version
    @version = opts[:api_version] || ENV['ONEFORM_VERSION'] || 'v1'

    if opts[:url]
        url = opts[:url]
    elsif ENV['ONEFORM_URL']
        url = ENV['ONEFORM_URL']
    elsif Dir.home && File.exist?(Dir.home + endpoint)
        url = File.read(Dir.home + endpoint).strip
    elsif File.exist?('/var/lib/one/.one/oneform_endpoint')
        url = File.read('/var/lib/one/.one/oneform_endpoint').strip
    else
        url = 'http://localhost:13013'
    end

    url = url.chomp('/') + "/api/#{@version}"

    if @username.nil? && @password.nil?
        if Dir.home && !Dir.home.empty? && ENV['ONE_AUTH'] && File.file?(ENV['ONE_AUTH'])
            one_auth = File.read(ENV['ONE_AUTH'])
        elsif Dir.home && File.file?(Dir.home + '/.one/one_auth')
            one_auth = File.read(Dir.home + '/.one/one_auth')
        elsif File.file?('/var/lib/one/.one/one_auth')
            one_auth = File.read('/var/lib/one/.one/one_auth')
        else
            raise 'ONE_AUTH file not present'
        end

        one_auth = one_auth.rstrip
        @username, @password = one_auth.split(':')
    end

    @uri = URI.parse(url)

    @user_agent = "OpenNebula #{CloudClient::VERSION} " <<
        "(#{opts[:user_agent]||'Ruby'})"

    @host = nil
    @port = nil

    return unless ENV['http_proxy']

    uri_proxy = URI.parse(ENV['http_proxy'])
    flag = false

    #  Check if we need to bypass the proxy
    if ENV['no_proxy']
        ENV['no_proxy'].split(',').each do |item|
            item = item.strip

            if (IPAddress @uri.host rescue nil).nil?
                if (IPAddress(item) rescue nil).nil?
                    flag |= (item == @uri.host)
                end
            else
                unless (IPAddress item rescue nil).nil?
                    flag |= IPAddress(item).include? IPAddress(@uri.host)
                end
            end
        end
    end

    return if flag

    @host = uri_proxy.host
    @port = uri_proxy.port
end