Class: NSXDriver::NSXTClient

Inherits:
NSXClient show all
Defined in:
lib/nsxt_client.rb

Overview

Class NSXTClient

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from NSXClient

#check_response, new_child, new_from_host, new_from_id

Constructor Details

#initialize(nsxmgr, nsx_user, nsx_password) ⇒ NSXTClient

CONSTRUCTORS



66
67
68
69
# File 'lib/nsxt_client.rb', line 66

def initialize(nsxmgr, nsx_user, nsx_password)
    super(nsxmgr, nsx_user, nsx_password)
    @nsx_type = NSXConstants::NSXT
end

Instance Attribute Details

#nsx_passwordObject

Returns the value of attribute nsx_password.



62
63
64
# File 'lib/nsxt_client.rb', line 62

def nsx_password
  @nsx_password
end

#nsx_typeObject

Returns the value of attribute nsx_type.



63
64
65
# File 'lib/nsxt_client.rb', line 63

def nsx_type
  @nsx_type
end

#nsx_userObject

Returns the value of attribute nsx_user.



61
62
63
# File 'lib/nsxt_client.rb', line 61

def nsx_user
  @nsx_user
end

#nsxmgrObject

ATTIBUTES



60
61
62
# File 'lib/nsxt_client.rb', line 60

def nsxmgr
  @nsxmgr
end

Instance Method Details

#add_headers(aditional_headers = []) ⇒ Object

Prepare headers



72
73
74
75
76
77
78
79
80
# File 'lib/nsxt_client.rb', line 72

def add_headers(aditional_headers = [])
    headers = NSXConstants::HEADER_JSON.clone
    unless aditional_headers.empty?
        aditional_headers.each do |header|
            headers[header.keys[0]] = header.values[0]
        end
    end
    headers
end

#delete(url, aditional_headers = [], valid_codes = []) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/nsxt_client.rb', line 155

def delete(url, aditional_headers = [], valid_codes = [])
    if valid_codes.empty?
        valid_codes = [NSXConstants::CODE_OK,
                       NSXConstants::CODE_NO_CONTENT]
    end
    uri = URI.parse(@nsxmgr + url)
    headers = add_headers(aditional_headers)
    request = Net::HTTP::Delete.new(uri.request_uri, headers)
    request.basic_auth(@nsx_user, @nsx_password)
    response = Net::HTTP.start(uri.host, uri.port, :use_ssl => true,
        :verify_mode => OpenSSL::SSL::VERIFY_NONE) do |https|
            https.request(request)
        end
    check_response(response, valid_codes)
end

#get(url, aditional_headers = [], valid_codes = []) ⇒ Object

METHODS



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/nsxt_client.rb', line 83

def get(url, aditional_headers = [], valid_codes = [])
    if valid_codes.empty?
        valid_codes = [NSXConstants::CODE_OK,
                       NSXConstants::CODE_NO_CONTENT]
    end
    uri = URI.parse(@nsxmgr + url)
    headers = add_headers(aditional_headers)
    request = Net::HTTP::Get.new(uri.request_uri, headers)
    request.basic_auth(@nsx_user, @nsx_password)
    response = Net::HTTP.start(uri.host, uri.port, :use_ssl => true,
        :verify_mode => OpenSSL::SSL::VERIFY_NONE) do |https|
            https.request(request)
        end
    response = check_response(response, valid_codes)
    JSON.parse(response.body)
end

#get_full_response(url, aditional_headers = [], valid_codes = []) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/nsxt_client.rb', line 100

def get_full_response(url, aditional_headers = [], valid_codes = [])
    if valid_codes.empty?
        valid_codes = [NSXConstants::CODE_OK,
                       NSXConstants::CODE_NO_CONTENT]
    end
    uri = URI.parse(@nsxmgr + url)
    headers = add_headers(aditional_headers)
    request = Net::HTTP::Get.new(uri.request_uri, headers)
    request.basic_auth(@nsx_user, @nsx_password)
    response = Net::HTTP.start(uri.host, uri.port, :use_ssl => true,
        :verify_mode => OpenSSL::SSL::VERIFY_NONE) do |https|
            https.request(request)
        end
    check_response(response, valid_codes)
end

#get_token(url, aditional_headers = [], valid_codes = []) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/nsxt_client.rb', line 171

def get_token(url, aditional_headers = [], valid_codes = [])
    if valid_codes.empty?
        valid_codes = [NSXConstants::CODE_OK]
    end
    uri = URI.parse(@nsxmgr + url)
    headers = add_headers(aditional_headers)
    request = Net::HTTP::Post.new(uri.request_uri, headers)
    request.basic_auth(@nsx_user, @nsx_password)
    response = Net::HTTP.start(uri.host, uri.port, :use_ssl => true,
        :verify_mode => OpenSSL::SSL::VERIFY_NONE) do |https|
            https.request(request)
        end
    response = check_response(response, valid_codes)
    response.body
end

#post(url, data, aditional_headers = [], valid_codes = []) ⇒ Object

Return: id of the created object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/nsxt_client.rb', line 117

def post(url, data, aditional_headers = [], valid_codes = [])
    if valid_codes.empty?
        valid_codes = [NSXConstants::CODE_CREATED,
                       NSXConstants::CODE_OK]
    end
    uri = URI.parse(@nsxmgr + url)
    headers = add_headers(aditional_headers)
    request = Net::HTTP::Post.new(uri.request_uri, headers)
    request.body = data
    request.basic_auth(@nsx_user, @nsx_password)
    response = Net::HTTP.start(uri.host, uri.port, :use_ssl => true,
        :verify_mode => OpenSSL::SSL::VERIFY_NONE) do |https|
            https.request(request)
        end
    response = check_response(response, valid_codes)
    response_json = JSON.parse(response.body)
    response_json['id']
end

#put(url, data, aditional_headers = [], valid_codes = []) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/nsxt_client.rb', line 136

def put(url, data, aditional_headers = [], valid_codes = [])
    if valid_codes.empty?
        valid_codes = [NSXConstants::CODE_CREATED,
                       NSXConstants::CODE_OK]
    end
    uri = URI.parse(@nsxmgr + url)
    headers = add_headers(aditional_headers)
    request = Net::HTTP::Put.new(uri.request_uri, headers)
    request.body = data
    request.basic_auth(@nsx_user, @nsx_password)
    response = Net::HTTP.start(uri.host, uri.port, :use_ssl => true,
        :verify_mode => OpenSSL::SSL::VERIFY_NONE) do |https|
            https.request(request)
        end
    response = check_response(response, valid_codes)
    response_json = JSON.parse(response.body)
    response_json['id']
end