Class: Morpheus::APIClient

Inherits:
Object
  • Object
show all
Defined in:
lib/morpheus/api/api_client.rb

Direct Known Subclasses

AccountGroupsInterface, AccountsInterface, AppsInterface, ArchiveBucketsInterface, ArchiveFilesInterface, AuthInterface, BlueprintsInterface, CloudDatastoresInterface, CloudPoliciesInterface, CloudsInterface, ContainersInterface, CustomInstanceTypesInterface, CypherInterface, DashboardInterface, DeployInterface, DeploymentsInterface, ExecuteSchedulesInterface, ExecutionRequestInterface, FileCopyRequestInterface, GroupPoliciesInterface, GroupsInterface, ImageBuilderBootScriptsInterface, ImageBuilderImageBuildsInterface, ImageBuilderInterface, ImageBuilderPreseedScriptsInterface, InstanceTypesInterface, InstancesInterface, KeyPairsInterface, LibraryContainerScriptsInterface, LibraryContainerTemplatesInterface, LibraryContainerTypesInterface, LibraryContainerUpgradesInterface, LibraryInstanceTypesInterface, LibraryLayoutsInterface, LicenseInterface, LoadBalancersInterface, LogsInterface, MonitoringAppsInterface, MonitoringChecksInterface, MonitoringContactsInterface, MonitoringGroupsInterface, MonitoringIncidentsInterface, MonitoringInterface, NetworkDomainsInterface, NetworkGroupsInterface, NetworkPoolServersInterface, NetworkPoolsInterface, NetworkProxiesInterface, NetworkServicesInterface, NetworksInterface, OldCypherInterface, OptionTypeListsInterface, OptionTypesInterface, OptionsInterface, PackagesInterface, PoliciesInterface, PowerSchedulesInterface, ProcessesInterface, ProvisionTypesInterface, RolesInterface, SecurityGroupRulesInterface, SecurityGroupsInterface, ServersInterface, SetupInterface, StorageProvidersInterface, TaskSetsInterface, TasksInterface, UserGroupsInterface, UserSettingsInterface, UserSourcesInterface, UsersInterface, VirtualImagesInterface, WhoamiInterface

Instance Method Summary collapse

Constructor Details

#initialize(access_token, refresh_token = nil, expires_in = nil, base_url = nil, verify_ssl = true, options = {}) ⇒ APIClient

Returns a new instance of APIClient.



6
7
8
9
10
11
12
13
14
15
# File 'lib/morpheus/api/api_client.rb', line 6

def initialize(access_token, refresh_token=nil,expires_in = nil, base_url=nil, verify_ssl=true, options={})
  @access_token = access_token
  @refresh_token = refresh_token
  @base_url = base_url
  if expires_in != nil
    @expires_at = DateTime.now + expires_in.seconds
  end
  set_ssl_verification_enabled(verify_ssl)
  setopts(options)
end

Instance Method Details

#account_groupsObject



207
208
209
# File 'lib/morpheus/api/api_client.rb', line 207

def 
  Morpheus::AccountGroupsInterface.new(@access_token, @refresh_token, @expires_at, @base_url).setopts(@options)
end

#accountsObject



279
280
281
# File 'lib/morpheus/api/api_client.rb', line 279

def accounts
  Morpheus::AccountsInterface.new(@access_token, @refresh_token, @expires_at, @base_url).setopts(@options)
end

#appsObject



255
256
257
# File 'lib/morpheus/api/api_client.rb', line 255

def apps
  Morpheus::AppsInterface.new(@access_token, @refresh_token, @expires_at, @base_url).setopts(@options)
end

#archive_bucketsObject



393
394
395
# File 'lib/morpheus/api/api_client.rb', line 393

def archive_buckets
  Morpheus::ArchiveBucketsInterface.new(@access_token, @refresh_token, @expires_at, @base_url).setopts(@options)
end

#archive_filesObject



397
398
399
# File 'lib/morpheus/api/api_client.rb', line 397

def archive_files
  Morpheus::ArchiveFilesInterface.new(@access_token, @refresh_token, @expires_at, @base_url).setopts(@options)
end

#authObject



187
188
189
# File 'lib/morpheus/api/api_client.rb', line 187

def auth
  Morpheus::AuthInterface.new(@base_url, @access_token).setopts(@options)
end

#blueprintsObject



259
260
261
# File 'lib/morpheus/api/api_client.rb', line 259

def blueprints
  Morpheus::BlueprintsInterface.new(@access_token, @refresh_token, @expires_at, @base_url).setopts(@options)
end

#cloud_datastoresObject



215
216
217
# File 'lib/morpheus/api/api_client.rb', line 215

def cloud_datastores
  Morpheus::CloudDatastoresInterface.new(@access_token, @refresh_token, @expires_at, @base_url).setopts(@options)
end

#cloud_policiesObject



361
362
363
# File 'lib/morpheus/api/api_client.rb', line 361

def cloud_policies
  Morpheus::CloudPoliciesInterface.new(@access_token, @refresh_token, @expires_at, @base_url).setopts(@options)
end

#cloudsObject



211
212
213
# File 'lib/morpheus/api/api_client.rb', line 211

def clouds
  Morpheus::CloudsInterface.new(@access_token, @refresh_token, @expires_at, @base_url).setopts(@options)
end

#containersObject



227
228
229
# File 'lib/morpheus/api/api_client.rb', line 227

def containers
  Morpheus::ContainersInterface.new(@access_token, @refresh_token, @expires_at, @base_url).setopts(@options)
end

#custom_instance_typesObject



311
312
313
# File 'lib/morpheus/api/api_client.rb', line 311

def custom_instance_types
  Morpheus::CustomInstanceTypesInterface.new(@access_token, @refresh_token, @expires_at, @base_url).setopts(@options)
end

#cypherObject



437
438
439
# File 'lib/morpheus/api/api_client.rb', line 437

def cypher
  Morpheus::CypherInterface.new(@access_token, @refresh_token, @expires_at, @base_url).setopts(@options)
end

#dashboardObject



323
324
325
# File 'lib/morpheus/api/api_client.rb', line 323

def dashboard
  Morpheus::DashboardInterface.new(@access_token, @refresh_token, @expires_at, @base_url).setopts(@options)
end

#default_content_typeObject

set this in your interface, eg. to ‘application/json’



35
36
37
# File 'lib/morpheus/api/api_client.rb', line 35

def default_content_type
  nil
end

#deployObject



263
264
265
# File 'lib/morpheus/api/api_client.rb', line 263

def deploy
  Morpheus::DeployInterface.new(@access_token, @refresh_token, @expires_at, @base_url).setopts(@options)
end

#deploymentsObject



267
268
269
# File 'lib/morpheus/api/api_client.rb', line 267

def deployments
  Morpheus::DeploymentsInterface.new(@access_token, @refresh_token, @expires_at, @base_url).setopts(@options)
end

#dryObject



22
23
24
# File 'lib/morpheus/api/api_client.rb', line 22

def dry()
  dry_run(true)
end

#dry_run(val = true) ⇒ Object



17
18
19
20
# File 'lib/morpheus/api/api_client.rb', line 17

def dry_run(val=true)
  @dry_run = !!val
  self
end

#execute(opts, options = {}) ⇒ Object

Execute an HTTP request with this client. opts - Hash of options for HTTP Request.

:url - The full url
:method - The default method is :get (GET)
:headers - Hash of headers to include in the request.
           eg. {'Content-Type' => 'application/json'}. :params is a special key for query parameters.
:params - query parameters
:payload - The body of the request.
:timeout - A custom timeout in seconds for api requests. The default is 30. todo: separate timeout options

options - Hash of common global options that commands parse. eg. :headers, :timeout, :dry_run, :curl, etc

:headers - Extra headers to add. This expects a Hash like {'Content-Type' => 'application/json'}.
:timeout - A custom timeout in seconds for api requests. The default is 30. todo: separate timeout options


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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/morpheus/api/api_client.rb', line 92

def execute(opts, options={})
  # merge in common global @options
  if @options
    options = options.merge(@options)
  else
    options = options.clone
  end

  # default HTTP method
  if opts[:method].nil?
    # why not a default? you will get an error from RestClient
    # opts[:method] = :get
  else
    # convert to lowercase Symbol like :get, :post, :put, or :delete
    opts[:method] = opts[:method].to_s.downcase.to_sym
  end

  # apply default headers
  opts[:headers] ||= {}
  # Authorization: apply our access token
  if @access_token
    if opts[:headers][:authorization].nil? && opts[:headers]['Authorization'].nil?
      opts[:headers][:authorization] = "Bearer #{@access_token}"
    else
      # authorization header has already been set.
    end
  end

  # Content-Type: apply interface default
  if opts[:headers]['Content-Type'].nil? && default_content_type
    opts[:headers]['Content-Type'] = default_content_type
  end

  # use custom timeout eg. from --timeout option
  if options[:timeout]
    opts[:timeout] = options[:timeout].to_f
  end
  
  # add extra headers, eg. from --header option
  # headers should be a Hash and not an Array, dont make me split you here!
  if options[:headers]
    opts[:headers] = opts[:headers].merge(options[:headers])
  end

  # this is confusing, but RestClient expects :params inside the headers...?
  # right?
  # move/copy params to headers.params for simplification.
  # remove this if issues arise
  if opts[:params] && (opts[:headers][:params].nil? || opts[:headers][:params].empty?)
    opts[:headers][:params] = opts[:params] # .delete(:params) maybe?
  end

  # :command_options for these
  # if options[:curl]
  #   opts[:curl] = options[:curl]
  # end
  # if options.key?(:pretty_json)
  #   opts[:pretty_json] = options[:pretty_json]
  # end
  # if options.key?(:scrub)
  #   opts[:scrub] = options[:scrub]
  # end

  # @verify_ssl is not used atm
  # todo: finish this and use it instead of the global variable RestClient.ssl_verification_enabled?
  # gotta clean up all APIClient subclasses new() methods to support this
  # the CliCommand subclasses should be changed to @users_interface = @api_client.users
  # also.. Credentials.new()
  if @verify_ssl == false
    opts[:verify_ssl] = OpenSSL::SSL::VERIFY_NONE
  end

  if @dry_run
    # JD: could return a Request object instead...
    # print_dry_run needs options somehow...
    opts[:command_options] = options # trash this..we got @options with setopts now
    return opts
  end

  # uhh can't use LIST at the moment
  # fix it!
  # if opts[:method] == :list
  #   opts[:method]
  # end

  # Morpheus::Logging::DarkPrinter.puts "Morpheus::RestClient.execute(#{opts})" if Morpheus::Logging.debug?
  # instead, using ::RestClient.log = STDOUT 
  response = Morpheus::RestClient.execute(opts)
  if opts[:parse_json] != false && options[:parse_json] != false
    return JSON.parse(response.to_s)
  else
    return response
  end
end

#execute_schedulesObject



331
332
333
# File 'lib/morpheus/api/api_client.rb', line 331

def execute_schedules
  Morpheus::ExecuteSchedulesInterface.new(@access_token, @refresh_token, @expires_at, @base_url).setopts(@options)
end

#execution_requestObject



445
446
447
# File 'lib/morpheus/api/api_client.rb', line 445

def execution_request
  Morpheus::ExecutionRequestInterface.new(@access_token, @refresh_token, @expires_at, @base_url).setopts(@options)
end

#file_copy_requestObject



449
450
451
# File 'lib/morpheus/api/api_client.rb', line 449

def file_copy_request
  Morpheus::FileCopyRequestInterface.new(@access_token, @refresh_token, @expires_at, @base_url).setopts(@options)
end

#group_policiesObject



357
358
359
# File 'lib/morpheus/api/api_client.rb', line 357

def group_policies
  Morpheus::GroupPoliciesInterface.new(@access_token, @refresh_token, @expires_at, @base_url).setopts(@options)
end

#groupsObject



203
204
205
# File 'lib/morpheus/api/api_client.rb', line 203

def groups
  Morpheus::GroupsInterface.new(@access_token, @refresh_token, @expires_at, @base_url).setopts(@options)
end

#image_builderObject



401
402
403
# File 'lib/morpheus/api/api_client.rb', line 401

def image_builder
  Morpheus::ImageBuilderInterface.new(@access_token, @refresh_token, @expires_at, @base_url).setopts(@options)
end

#instance_typesObject



231
232
233
# File 'lib/morpheus/api/api_client.rb', line 231

def instance_types
  Morpheus::InstanceTypesInterface.new(@access_token, @refresh_token, @expires_at, @base_url).setopts(@options)
end

#instancesObject



223
224
225
# File 'lib/morpheus/api/api_client.rb', line 223

def instances
  Morpheus::InstancesInterface.new(@access_token, @refresh_token, @expires_at, @base_url).setopts(@options)
end

#key_pairsObject



303
304
305
# File 'lib/morpheus/api/api_client.rb', line 303

def key_pairs
  Morpheus::KeyPairsInterface.new(@access_token, @refresh_token, @expires_at, @base_url).setopts(@options)
end

#library_container_scriptsObject



425
426
427
# File 'lib/morpheus/api/api_client.rb', line 425

def library_container_scripts
  Morpheus::LibraryContainerScriptsInterface.new(@access_token, @refresh_token, @expires_at, @base_url).setopts(@options)
end

#library_container_templatesObject



429
430
431
# File 'lib/morpheus/api/api_client.rb', line 429

def library_container_templates
  Morpheus::LibraryContainerTemplatesInterface.new(@access_token, @refresh_token, @expires_at, @base_url).setopts(@options)
end

#library_container_typesObject



421
422
423
# File 'lib/morpheus/api/api_client.rb', line 421

def library_container_types
  Morpheus::LibraryContainerTypesInterface.new(@access_token, @refresh_token, @expires_at, @base_url).setopts(@options)
end

#library_container_upgradesObject



417
418
419
# File 'lib/morpheus/api/api_client.rb', line 417

def library_container_upgrades
  Morpheus::LibraryContainerUpgradesInterface.new(@access_token, @refresh_token, @expires_at, @base_url).setopts(@options)
end

#library_instance_typesObject



409
410
411
# File 'lib/morpheus/api/api_client.rb', line 409

def library_instance_types
  Morpheus::LibraryInstanceTypesInterface.new(@access_token, @refresh_token, @expires_at, @base_url).setopts(@options)
end

#library_layoutsObject



413
414
415
# File 'lib/morpheus/api/api_client.rb', line 413

def library_layouts
  Morpheus::LibraryLayoutsInterface.new(@access_token, @refresh_token, @expires_at, @base_url).setopts(@options)
end

#licenseObject



307
308
309
# File 'lib/morpheus/api/api_client.rb', line 307

def license
  Morpheus::LicenseInterface.new(@access_token, @refresh_token, @expires_at, @base_url).setopts(@options)
end

#load_balancersObject



239
240
241
# File 'lib/morpheus/api/api_client.rb', line 239

def load_balancers
  Morpheus::LoadBalancersInterface.new(@access_token, @refresh_token, @expires_at, @base_url).setopts(@options)
end

#logsObject



295
296
297
# File 'lib/morpheus/api/api_client.rb', line 295

def logs
  Morpheus::LogsInterface.new(@access_token, @refresh_token, @expires_at, @base_url).setopts(@options)
end

#monitoringObject



339
340
341
# File 'lib/morpheus/api/api_client.rb', line 339

def monitoring
  Morpheus::MonitoringInterface.new(@access_token, @refresh_token, @expires_at, @base_url).setopts(@options)
end

#network_domainsObject



385
386
387
# File 'lib/morpheus/api/api_client.rb', line 385

def network_domains
  Morpheus::NetworkDomainsInterface.new(@access_token, @refresh_token, @expires_at, @base_url).setopts(@options)
end

#network_groupsObject



369
370
371
# File 'lib/morpheus/api/api_client.rb', line 369

def network_groups
  Morpheus::NetworkGroupsInterface.new(@access_token, @refresh_token, @expires_at, @base_url).setopts(@options)
end

#network_pool_serversObject



381
382
383
# File 'lib/morpheus/api/api_client.rb', line 381

def network_pool_servers
  Morpheus::NetworkPoolServersInterface.new(@access_token, @refresh_token, @expires_at, @base_url).setopts(@options)
end

#network_poolsObject



373
374
375
# File 'lib/morpheus/api/api_client.rb', line 373

def network_pools
  Morpheus::NetworkPoolsInterface.new(@access_token, @refresh_token, @expires_at, @base_url).setopts(@options)
end

#network_proxiesObject



389
390
391
# File 'lib/morpheus/api/api_client.rb', line 389

def network_proxies
  Morpheus::NetworkProxiesInterface.new(@access_token, @refresh_token, @expires_at, @base_url).setopts(@options)
end

#network_servicesObject



377
378
379
# File 'lib/morpheus/api/api_client.rb', line 377

def network_services
  Morpheus::NetworkServicesInterface.new(@access_token, @refresh_token, @expires_at, @base_url).setopts(@options)
end

#networksObject



365
366
367
# File 'lib/morpheus/api/api_client.rb', line 365

def networks
  Morpheus::NetworksInterface.new(@access_token, @refresh_token, @expires_at, @base_url).setopts(@options)
end

#old_cypherObject



441
442
443
# File 'lib/morpheus/api/api_client.rb', line 441

def old_cypher
  Morpheus::OldCypherInterface.new(@access_token, @refresh_token, @expires_at, @base_url).setopts(@options)
end

#option_type_listsObject



319
320
321
# File 'lib/morpheus/api/api_client.rb', line 319

def option_type_lists
  Morpheus::OptionTypeListsInterface.new(@access_token, @refresh_token, @expires_at, @base_url).setopts(@options)
end

#option_typesObject



315
316
317
# File 'lib/morpheus/api/api_client.rb', line 315

def option_types
  Morpheus::OptionTypesInterface.new(@access_token, @refresh_token, @expires_at, @base_url).setopts(@options)
end

#optionsObject



199
200
201
# File 'lib/morpheus/api/api_client.rb', line 199

def options
  Morpheus::OptionsInterface.new(@access_token, @refresh_token, @expires_at, @base_url).setopts(@options)
end

#packagesObject



433
434
435
# File 'lib/morpheus/api/api_client.rb', line 433

def packages
  Morpheus::PackagesInterface.new(@access_token, @refresh_token, @expires_at, @base_url).setopts(@options)
end

#policiesObject

def incidents

# Morpheus::MonitoringIncidentsInterface.new(@access_token, @refresh_token, @expires_at, @base_url).setopts(@options)
monitoring.incidents

end



353
354
355
# File 'lib/morpheus/api/api_client.rb', line 353

def policies
  Morpheus::PoliciesInterface.new(@access_token, @refresh_token, @expires_at, @base_url).setopts(@options)
end

#power_schedulesObject



327
328
329
# File 'lib/morpheus/api/api_client.rb', line 327

def power_schedules
  Morpheus::PowerSchedulesInterface.new(@access_token, @refresh_token, @expires_at, @base_url).setopts(@options)
end

#processesObject



453
454
455
# File 'lib/morpheus/api/api_client.rb', line 453

def processes
  Morpheus::ProcessesInterface.new(@access_token, @refresh_token, @expires_at, @base_url).setopts(@options)
end

#provision_typesObject



235
236
237
# File 'lib/morpheus/api/api_client.rb', line 235

def provision_types
  Morpheus::ProvisionTypesInterface.new(@access_token, @refresh_token, @expires_at, @base_url).setopts(@options)
end

#rolesObject



299
300
301
# File 'lib/morpheus/api/api_client.rb', line 299

def roles
  Morpheus::RolesInterface.new(@access_token, @refresh_token, @expires_at, @base_url).setopts(@options)
end

#security_group_rulesObject



275
276
277
# File 'lib/morpheus/api/api_client.rb', line 275

def security_group_rules
  Morpheus::SecurityGroupRulesInterface.new(@access_token, @refresh_token, @expires_at, @base_url).setopts(@options)
end

#security_groupsObject



271
272
273
# File 'lib/morpheus/api/api_client.rb', line 271

def security_groups
  Morpheus::SecurityGroupsInterface.new(@access_token, @refresh_token, @expires_at, @base_url).setopts(@options)
end

#serversObject



219
220
221
# File 'lib/morpheus/api/api_client.rb', line 219

def servers
  Morpheus::ServersInterface.new(@access_token, @refresh_token, @expires_at, @base_url).setopts(@options)
end

#set_ssl_verification_enabled(val) ⇒ Object



30
31
32
# File 'lib/morpheus/api/api_client.rb', line 30

def set_ssl_verification_enabled(val)
  @verify_ssl = !!val
end

#setopts(new_options) ⇒ Object Also known as: set_options

set common global @options for use with all requests meant for inline use just like dry(), set_options(dry_run:true) can be used in place of dry() Example: Prints curl -XGET …/whoami -H “Bearer” instead of actually request APIClient.new(token).whoami.setopts(curl:true).get({})

Parameters:

  • opts (Hash)

    globally supported options like :dry_run, :json, :curl, :headers, :timeout, etc



55
56
57
58
59
60
61
# File 'lib/morpheus/api/api_client.rb', line 55

def setopts(new_options)
  @options = new_options
  if @options[:dry_run]
    dry_run(true)
  end
  self
end

#setupObject



335
336
337
# File 'lib/morpheus/api/api_client.rb', line 335

def setup
  Morpheus::SetupInterface.new(@base_url).setopts(@options).setopts(@options)
end

#ssl_verification_enabled?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/morpheus/api/api_client.rb', line 26

def ssl_verification_enabled?
  @verify_ssl
end

#storage_providersObject



405
406
407
# File 'lib/morpheus/api/api_client.rb', line 405

def storage_providers
  Morpheus::StorageProvidersInterface.new(@access_token, @refresh_token, @expires_at, @base_url).setopts(@options)
end

#task_setsObject



247
248
249
# File 'lib/morpheus/api/api_client.rb', line 247

def task_sets
  Morpheus::TaskSetsInterface.new(@access_token, @refresh_token, @expires_at, @base_url).setopts(@options)
end

#tasksObject



243
244
245
# File 'lib/morpheus/api/api_client.rb', line 243

def tasks
  Morpheus::TasksInterface.new(@access_token, @refresh_token, @expires_at, @base_url).setopts(@options)
end

#user_groupsObject



287
288
289
# File 'lib/morpheus/api/api_client.rb', line 287

def user_groups
  Morpheus::UserGroupsInterface.new(@access_token, @refresh_token, @expires_at, @base_url).setopts(@options)
end

#user_settingsObject



195
196
197
# File 'lib/morpheus/api/api_client.rb', line 195

def 
  Morpheus::UserSettingsInterface.new(@access_token, @refresh_token, @expires_at, @base_url).setopts(@options)
end

#user_sourcesObject



291
292
293
# File 'lib/morpheus/api/api_client.rb', line 291

def user_sources
  Morpheus::UserSourcesInterface.new(@access_token, @refresh_token, @expires_at, @base_url).setopts(@options)
end

#usersObject



283
284
285
# File 'lib/morpheus/api/api_client.rb', line 283

def users
  Morpheus::UsersInterface.new(@access_token, @refresh_token, @expires_at, @base_url).setopts(@options)
end

#virtual_imagesObject



251
252
253
# File 'lib/morpheus/api/api_client.rb', line 251

def virtual_images
  Morpheus::VirtualImagesInterface.new(@access_token, @refresh_token, @expires_at, @base_url).setopts(@options)
end

#whoamiObject



191
192
193
# File 'lib/morpheus/api/api_client.rb', line 191

def whoami
  Morpheus::WhoamiInterface.new(@access_token, @refresh_token, @expires_at, @base_url).setopts(@options)
end

#withopts(tmp_options, &block) ⇒ Object Also known as: with_options

with_options sets common global @options for the duration of the block only then returns the options to their prior values

Parameters:

  • opts (Hash)

    globally supported options like :dry_run, :json, :curl, :headers, :timeout, etc

Returns:

  • result of block, usually the a result Hash from client.execute({})



67
68
69
70
71
72
73
74
75
76
# File 'lib/morpheus/api/api_client.rb', line 67

def withopts(tmp_options, &block)
  @_old_options = @options
  begin
    @options = tmp_options
    result = block.call()
  ensure
    @options = @_old_options
  end
  return result
end