Class: Opsmgr::Api::HttpClient

Inherits:
Object
  • Object
show all
Includes:
Loggable
Defined in:
lib/opsmgr/api/http_client.rb

Defined Under Namespace

Classes: FakeResponse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Loggable

included, #log

Constructor Details

#initialize(environment, endpoints) ⇒ HttpClient

Returns a new instance of HttpClient.



19
20
21
22
23
# File 'lib/opsmgr/api/http_client.rb', line 19

def initialize(environment, endpoints)
  @environment = environment
  @uri = URI.parse(environment.settings.ops_manager.url)
  @endpoints = endpoints
end

Instance Attribute Details

#endpointsObject (readonly)

Returns the value of attribute endpoints.



13
14
15
# File 'lib/opsmgr/api/http_client.rb', line 13

def endpoints
  @endpoints
end

#environmentObject (readonly)

Returns the value of attribute environment.



13
14
15
# File 'lib/opsmgr/api/http_client.rb', line 13

def environment
  @environment
end

#uriObject (readonly)

Returns the value of attribute uri.



13
14
15
# File 'lib/opsmgr/api/http_client.rb', line 13

def uri
  @uri
end

Class Method Details

.build(environment) ⇒ Object



15
16
17
# File 'lib/opsmgr/api/http_client.rb', line 15

def self.build(environment)
  new(environment, Opsmgr::Api::EndpointsFactory.create(environment))
end

Instance Method Details

#add_product(product_name, version) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/opsmgr/api/http_client.rb', line 72

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)
  req.basic_auth(web_auth_user, web_auth_password)

  http.request(req)
end

#create_the_first_userObject



130
131
132
133
134
135
136
137
# File 'lib/opsmgr/api/http_client.rb', line 130

def create_the_first_user
  req = Net::HTTP::Post.new(endpoints.configure_user_path)
  req.set_form_data(
    'user[user_name]' => web_auth_user,
    'user[password]' => web_auth_password,
    'user[password_confirmation]' => web_auth_password)
  http.request(req)
end

#delete_unused_productsObject



154
155
156
157
158
159
# File 'lib/opsmgr/api/http_client.rb', line 154

def delete_unused_products
  req = Net::HTTP::Delete.new(endpoints.list_products_path)
  req.basic_auth(web_auth_user, web_auth_password)

  http.request(req)
end

#install_log(install_id) ⇒ Object



143
144
145
# File 'lib/opsmgr/api/http_client.rb', line 143

def install_log(install_id)
  get_with_basic_auth(endpoints.installation_log_path(install_id))
end

#install_status(install_id) ⇒ Object



139
140
141
# File 'lib/opsmgr/api/http_client.rb', line 139

def install_status(install_id)
  get_with_basic_auth(endpoints.show_installation_status(install_id))
end

#installation_deleteObject



119
120
121
122
123
124
# File 'lib/opsmgr/api/http_client.rb', line 119

def installation_delete
  req = Net::HTTP::Delete.new(endpoints.installation_path)
  req.basic_auth(web_auth_user, web_auth_password)

  http.request(req)
end

#installation_deletion_statusObject



126
127
128
# File 'lib/opsmgr/api/http_client.rb', line 126

def installation_deletion_status
  get_with_basic_auth(endpoints.installation_deletion_status_path)
end

#installation_settingsObject



115
116
117
# File 'lib/opsmgr/api/http_client.rb', line 115

def installation_settings
  get_with_basic_auth(endpoints.installation_settings_get_path)
end

#installed_productsObject

products that have been uploaded, configured, and installed



107
108
109
# File 'lib/opsmgr/api/http_client.rb', line 107

def installed_products
  get_with_basic_auth(endpoints.installed_products_path)
end

#list_componentsObject

components are both uploaded AND configured AND deployed



97
98
99
# File 'lib/opsmgr/api/http_client.rb', line 97

def list_components
  get_with_basic_auth(endpoints.list_components_path)
end

#list_productsObject

products are uploaded but not necessarily configured or deployed



102
103
104
# File 'lib/opsmgr/api/http_client.rb', line 102

def list_products
  get_with_basic_auth(endpoints.list_products_path)
end

#mark_product_for_deletion(product_guid) ⇒ Object



147
148
149
150
151
152
# File 'lib/opsmgr/api/http_client.rb', line 147

def mark_product_for_deletion(product_guid)
  req = Net::HTTP::Delete.new(endpoints.mark_for_deletion_path(product_guid))
  req.basic_auth(web_auth_user, web_auth_password)

  http.request(req)
end

#product_manifest(product_guid) ⇒ Object



111
112
113
# File 'lib/opsmgr/api/http_client.rb', line 111

def product_manifest(product_guid)
  get_with_basic_auth(endpoints.product_manifest_path(product_guid))
end

#trigger_installObject



88
89
90
91
92
93
94
# File 'lib/opsmgr/api/http_client.rb', line 88

def trigger_install
  req = Net::HTTP::Post.new(endpoints.install_post_path)
  req.body = ''
  req.basic_auth(web_auth_user, web_auth_password)

  http.request(req)
end

#upgrade_product(product_guid, to_version) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/opsmgr/api/http_client.rb', line 80

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)
  req.basic_auth(web_auth_user, web_auth_password)

  http.request(req)
end

#upload_component(product_path) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/opsmgr/api/http_client.rb', line 41

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
      -u #{web_auth_user}:#{web_auth_password}
    )
  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



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/opsmgr/api/http_client.rb', line 25

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)
      )
  )

  req.basic_auth(web_auth_user, web_auth_password)

  http.request(req)
end

#versionObject



161
162
163
# File 'lib/opsmgr/api/http_client.rb', line 161

def version
  endpoints.version
end