Class: Opsmgr::Api::HttpClient
- Inherits:
-
Object
- Object
- Opsmgr::Api::HttpClient
- Includes:
- Loggable
- Defined in:
- lib/opsmgr/api/http_client.rb
Defined Under Namespace
Classes: FakeResponse
Instance Attribute Summary collapse
-
#endpoints ⇒ Object
readonly
Returns the value of attribute endpoints.
-
#environment ⇒ Object
readonly
Returns the value of attribute environment.
-
#uri ⇒ Object
readonly
Returns the value of attribute uri.
Class Method Summary collapse
Instance Method Summary collapse
- #add_product(product_name, version) ⇒ Object
- #auth_redirect_location ⇒ Object
- #create_env_admin_user(token) ⇒ Object
- #create_the_first_user ⇒ Object
- #delete_unused_products ⇒ Object
- #download_staged_manifest(product_guid) ⇒ Object
- #ensure_env_admin_user_exists ⇒ Object
- #import_installation(installation_file, password) ⇒ Object
- #import_secret_parameter_name ⇒ Object
- #import_stemcell(stemcell_file) ⇒ Object
-
#initialize(environment, endpoints, om_version) ⇒ HttpClient
constructor
A new instance of HttpClient.
- #install_log(install_id) ⇒ Object
- #install_status(install_id) ⇒ Object
- #installation_delete ⇒ Object
- #installation_deletion_status ⇒ Object
- #installation_settings ⇒ Object
-
#installed_products ⇒ Object
products that have been uploaded, configured, and installed.
-
#list_components ⇒ Object
components are both uploaded AND configured AND deployed.
-
#list_products ⇒ Object
products are uploaded but not necessarily configured or deployed.
- #mark_product_for_deletion(product_guid) ⇒ Object
- #product_manifest(product_guid) ⇒ Object
- #read_timeout ⇒ Object
- #root_ca_certificate ⇒ Object
- #trigger_install ⇒ Object
- #upgrade_product(product_guid, to_version) ⇒ Object
- #upload_component(product_path) ⇒ Object
- #upload_product_installation_settings(settings_file_path) ⇒ Object
- #verify_if_env_admin_user_exists?(token) ⇒ Boolean
- #version ⇒ Object
- #wait_for_uaa_to_be_available(sleep_interval = 10, max_retry_count = 24) ⇒ Object
Methods included from Loggable
Constructor Details
#initialize(environment, endpoints, om_version) ⇒ HttpClient
Returns a new instance of HttpClient.
24 25 26 27 28 29 |
# File 'lib/opsmgr/api/http_client.rb', line 24 def initialize(environment, endpoints, om_version) @environment = environment @uri = URI.parse(environment.settings.dig('ops_manager', 'url')) @endpoints = endpoints @om_version = om_version end |
Instance Attribute Details
#endpoints ⇒ Object (readonly)
Returns the value of attribute endpoints.
18 19 20 |
# File 'lib/opsmgr/api/http_client.rb', line 18 def endpoints @endpoints end |
#environment ⇒ Object (readonly)
Returns the value of attribute environment.
18 19 20 |
# File 'lib/opsmgr/api/http_client.rb', line 18 def environment @environment end |
#uri ⇒ Object (readonly)
Returns the value of attribute uri.
18 19 20 |
# File 'lib/opsmgr/api/http_client.rb', line 18 def uri @uri end |
Class Method Details
Instance Method Details
#add_product(product_name, version) ⇒ Object
96 97 98 99 100 101 102 |
# File 'lib/opsmgr/api/http_client.rb', line 96 def add_product(product_name, version) req = Net::HTTP::Post.new(endpoints.add_product_path) req.set_form_data('name' => product_name, 'product_version' => version) add_auth_header(req) http.request(req) end |
#auth_redirect_location ⇒ Object
335 336 337 338 339 340 341 |
# File 'lib/opsmgr/api/http_client.rb', line 335 def auth_redirect_location host = environment.settings.dig('ops_manager', 'url') host.concat('/') unless host.end_with? '/' host+'auth/cloudfoundry' end |
#create_env_admin_user(token) ⇒ Object
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 |
# File 'lib/opsmgr/api/http_client.rb', line 290 def create_env_admin_user(token) request = Net::HTTP::Post.new( endpoints.uaa_create_user ) request['Content-Type'] = 'application/json' request['Authorization'] = token request.body = { userName: web_auth_user, password: web_auth_password, emails: [ { value: web_auth_user } ] }.to_json response = http.request(request) if response.code != '201' raise("Unexpected response from UAA: code #{response.code}, body: #{response.body}") end end |
#create_the_first_user ⇒ Object
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/opsmgr/api/http_client.rb', line 161 def create_the_first_user if Gem::Version.new(@om_version) >= Gem::Version.new('1.6') req = Net::HTTP::Post.new(endpoints.api_setup_path) req.set_form_data( 'setup[user_name]' => web_auth_user, 'setup[password]' => web_auth_password, 'setup[password_confirmation]' => web_auth_password, 'setup[eula_accepted]' => true ) else req = Net::HTTP::Post.new(endpoints.api_user_path) req.set_form_data( 'user[user_name]' => web_auth_user, 'user[password]' => web_auth_password, 'user[password_confirmation]' => web_auth_password, ) end http.request(req) end |
#delete_unused_products ⇒ Object
197 198 199 200 201 202 |
# File 'lib/opsmgr/api/http_client.rb', line 197 def delete_unused_products req = Net::HTTP::Delete.new(endpoints.list_products_path) add_auth_header(req) http.request(req) end |
#download_staged_manifest(product_guid) ⇒ Object
112 113 114 115 116 117 |
# File 'lib/opsmgr/api/http_client.rb', line 112 def download_staged_manifest(product_guid) req = Net::HTTP::Get.new(endpoints.download_staged_manifest(product_guid)) add_auth_header(req) http.request(req) end |
#ensure_env_admin_user_exists ⇒ Object
263 264 265 266 267 |
# File 'lib/opsmgr/api/http_client.rb', line 263 def ensure_env_admin_user_exists token = get_oauth_token('admin') create_env_admin_user(token) unless verify_if_env_admin_user_exists?(token) end |
#import_installation(installation_file, password) ⇒ Object
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/opsmgr/api/http_client.rb', line 208 def import_installation(installation_file, password) create_the_first_user unless Gem::Version.new(@om_version) >= Gem::Version.new('1.7') log.info('uploading the installation file') req = Net::HTTP::Post::Multipart.new( endpoints.import_installation_path, 'installation[file]' => UploadIO.new( installation_file, 'application/octet-stream', File.basename(installation_file) ), import_secret_parameter_name => password ) # The post-1.7 import happens before a user has been created so we cannot authenticate. add_auth_header(req) unless Gem::Version.new(@om_version) >= Gem::Version.new('1.7') response = http.request(req) if Gem::Version.new(@om_version) == Gem::Version.new('1.7') wait_for_uaa_to_be_available ensure_env_admin_user_exists end response end |
#import_secret_parameter_name ⇒ Object
311 312 313 |
# File 'lib/opsmgr/api/http_client.rb', line 311 def import_secret_parameter_name Gem::Version.new(@om_version) >= Gem::Version.new('1.7') ? 'passphrase' : 'password' end |
#import_stemcell(stemcell_file) ⇒ Object
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 |
# File 'lib/opsmgr/api/http_client.rb', line 315 def import_stemcell(stemcell_file) req = Net::HTTP::Post::Multipart.new( endpoints.import_stemcell_path, 'stemcell[file]' => UploadIO.new( stemcell_file, 'application/octet-stream', File.basename(stemcell_file) ) ) add_auth_header(req) http.request(req) end |
#install_log(install_id) ⇒ Object
186 187 188 |
# File 'lib/opsmgr/api/http_client.rb', line 186 def install_log(install_id) get_with_basic_auth(endpoints.installation_log_path(install_id)) end |
#install_status(install_id) ⇒ Object
182 183 184 |
# File 'lib/opsmgr/api/http_client.rb', line 182 def install_status(install_id) get_with_basic_auth(endpoints.show_installation_status(install_id)) end |
#installation_delete ⇒ Object
150 151 152 153 154 155 |
# File 'lib/opsmgr/api/http_client.rb', line 150 def installation_delete req = Net::HTTP::Delete.new(endpoints.installation_path) add_auth_header(req) http.request(req) end |
#installation_deletion_status ⇒ Object
157 158 159 |
# File 'lib/opsmgr/api/http_client.rb', line 157 def installation_deletion_status get_with_basic_auth(endpoints.installation_deletion_status_path) end |
#installation_settings ⇒ Object
146 147 148 |
# File 'lib/opsmgr/api/http_client.rb', line 146 def installation_settings get_with_basic_auth(endpoints.installation_settings_get_path) end |
#installed_products ⇒ Object
products that have been uploaded, configured, and installed
138 139 140 |
# File 'lib/opsmgr/api/http_client.rb', line 138 def installed_products get_with_basic_auth(endpoints.installed_products_path) end |
#list_components ⇒ Object
components are both uploaded AND configured AND deployed
128 129 130 |
# File 'lib/opsmgr/api/http_client.rb', line 128 def list_components get_with_basic_auth(endpoints.list_components_path) end |
#list_products ⇒ Object
products are uploaded but not necessarily configured or deployed
133 134 135 |
# File 'lib/opsmgr/api/http_client.rb', line 133 def list_products get_with_basic_auth(endpoints.list_products_path) end |
#mark_product_for_deletion(product_guid) ⇒ Object
190 191 192 193 194 195 |
# File 'lib/opsmgr/api/http_client.rb', line 190 def mark_product_for_deletion(product_guid) req = Net::HTTP::Delete.new(endpoints.mark_for_deletion_path(product_guid)) add_auth_header(req) http.request(req) end |
#product_manifest(product_guid) ⇒ Object
142 143 144 |
# File 'lib/opsmgr/api/http_client.rb', line 142 def product_manifest(product_guid) get_with_basic_auth(endpoints.product_manifest_path(product_guid)) end |
#read_timeout ⇒ Object
331 332 333 |
# File 'lib/opsmgr/api/http_client.rb', line 331 def read_timeout environment.settings.dig('iaas_type') != 'vcloud' ? 600 : 3600 end |
#root_ca_certificate ⇒ Object
89 90 91 92 93 94 |
# File 'lib/opsmgr/api/http_client.rb', line 89 def root_ca_certificate req = Net::HTTP::Get.new(endpoints.root_ca_certificate_path) add_auth_header(req) http.request(req) end |
#trigger_install ⇒ Object
119 120 121 122 123 124 125 |
# File 'lib/opsmgr/api/http_client.rb', line 119 def trigger_install req = Net::HTTP::Post.new(endpoints.install_post_path) req.body = '' add_auth_header(req) http.request(req) end |
#upgrade_product(product_guid, to_version) ⇒ Object
104 105 106 107 108 109 110 |
# File 'lib/opsmgr/api/http_client.rb', line 104 def upgrade_product(product_guid, to_version) req = Net::HTTP::Put.new(endpoints.upgrade_product_path(product_guid)) req.set_form_data('to_version' => to_version) add_auth_header(req) http.request(req) end |
#upload_component(product_path) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/opsmgr/api/http_client.rb', line 47 def upload_component(product_path) # We ran into a possible Net::HTTP bug where Ruby would always drop the connection while waiting for the # uploaded zip file to unzip. Curl behaved correctly, but curb (Ruby bindings for libcurl) did not behave # well with WebMock, so here we are. upload_component_command = %W( curl -k --silent --fail #{uri}#{endpoints.upload_product_path} -F #{endpoints.upload_product_form_key}[file]=@#{product_path} -X POST ) if Gem::Version.new(@om_version) >= Gem::Version.new('1.7') log.info('using oauth') token = get_oauth_token(web_auth_user) upload_component_command.push("-H") upload_component_command.push("Authorization: #{token}") else log.info('using basic auth') upload_component_command.push("-u") upload_component_command.push("#{web_auth_user}:#{web_auth_password}") end log.info('uploading product') error = nil status = Open4.popen4(*upload_component_command) do |_, _, stdout, stderr| log.info(stdout.read) error = stderr.read end if status.success? FakeResponse.new('200', '{}') else error_code = (error.match(/^\< HTTP\/.* (\d+) /) || [nil, '???'])[1] FakeResponse.new(error_code, error) end end |
#upload_product_installation_settings(settings_file_path) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/opsmgr/api/http_client.rb', line 31 def upload_product_installation_settings(settings_file_path) req = Net::HTTP::Post::Multipart.new( endpoints.installation_settings_post_path, 'installation[file]' => UploadIO.new( settings_file_path, 'application/octet-stream', File.basename(settings_file_path) ) ) add_auth_header(req) http.request(req) end |
#verify_if_env_admin_user_exists?(token) ⇒ Boolean
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
# File 'lib/opsmgr/api/http_client.rb', line 269 def verify_if_env_admin_user_exists?(token) request = Net::HTTP::Get.new( endpoints.uaa_get_user_by_username_path(web_auth_user) ) request['Authorization'] = token response = http.request(request) if response.code != '200' raise("Invalid response from UAA: #{response.code}") end reply = JSON.parse(response.body) if reply['resources'].nil? raise("Invalid response body from UAA: #{response.body}") end !reply['resources'].empty? end |
#version ⇒ Object
204 205 206 |
# File 'lib/opsmgr/api/http_client.rb', line 204 def version endpoints.version end |
#wait_for_uaa_to_be_available(sleep_interval = 10, max_retry_count = 24) ⇒ Object
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
# File 'lib/opsmgr/api/http_client.rb', line 237 def wait_for_uaa_to_be_available(sleep_interval = 10, max_retry_count = 24) retry_count = 0 loop do request = Net::HTTP::Get.new( endpoints.login_ensure_availability ) response = http.request(request) http_location = response['location'] break if response.code == '302' && http_location == auth_redirect_location retry_count += 1 if retry_count >= max_retry_count retry_wait_time = sleep_interval * retry_count raise("Timed out waiting for UAA to be available after #{retry_wait_time} seconds") end Kernel.sleep(sleep_interval) end end |