Class: Lunanode::API

Inherits:
Object
  • Object
show all
Includes:
APIActions
Defined in:
lib/lunanode/api.rb

Overview

Class for accessing the Lunanode API.

The class is instantiated by either passing it a JSON file containing the keys ‘api_id` and `api_key`, or specifying the ID and key directly:

api = Lunanode::API.new("credentials_file.json")
api = Lunanode::API.new(api_id: "ABCDEFG", api_key: "HIJKLMNOP")

Once instantiated, action methods follow the example:

api.vm_list # no parameters
api.vm_info(vm_id: "My-VM-ID") # required parameters
api.image_list(region: "Toronto") # optional parameters

Required and optional arguments are specified in the method definitions, but can can be queried via API.params_for. They follow the API specification atwiki.lunanode.com/index.php/API

Constant Summary collapse

API_ENDPOINT =

Class-level definitions

"https://dynamic.lunanode.com/api/".freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Lunanode::APIActions::DNS

#dns_dyn_add, #dns_dyn_list, #dns_dyn_remove, #dns_dyn_update, #dns_list, #dns_record_add, #dns_record_list, #dns_record_remove, #dns_set, #dns_zone_add, #dns_zone_list, #dns_zone_remove

Methods included from Lunanode::APIActions::Email

#email_alias_add, #email_alias_list, #email_alias_remove, #email_domain_add, #email_domain_dkim_set, #email_domain_dkim_unset, #email_domain_list, #email_domain_remove, #email_usage, #email_user_add, #email_user_list, #email_user_remove, #email_user_set_password

Methods included from Lunanode::APIActions::Floating

#floating_add, #floating_delete, #floating_list

Methods included from Lunanode::APIActions::Image

#image_delete, #image_details, #image_fetch, #image_list, #image_list_mine, #image_replicate, #image_retrieve

Methods included from Lunanode::APIActions::LB

#lb_associate, #lb_create, #lb_delete, #lb_info, #lb_list, #lb_member_add, #lb_member_remove

Methods included from Lunanode::APIActions::Monitor

#monitor_alert_add, #monitor_alert_list, #monitor_alert_remove, #monitor_check_add, #monitor_check_list, #monitor_check_remove, #monitor_check_types, #monitor_contact_add, #monitor_contact_list, #monitor_contact_remove

Methods included from Lunanode::APIActions::Network

#network_create, #network_delete, #network_list

Methods included from Lunanode::APIActions::Plan

#plan_list

Methods included from Lunanode::APIActions::Script

#script_list

Methods included from Lunanode::APIActions::Securitygroup

#securitygroup_create, #securitygroup_delete, #securitygroup_list, #securitygroup_rename, #securitygroup_rule_delete, #securitygroup_rule_insert, #securitygroup_rule_list

Methods included from Lunanode::APIActions::VM

#vm_create, #vm_delete, #vm_diskswap, #vm_floatingip_add, #vm_floatingip_delete, #vm_info, #vm_ip_add, #vm_ip_delete, #vm_iplist, #vm_list, #vm_reboot, #vm_reimage, #vm_rename, #vm_rescue, #vm_resize, #vm_securitygroup_add, #vm_securitygroup_remove, #vm_shelve, #vm_snapshot, #vm_start, #vm_stop, #vm_unshelve, #vm_vnc

Methods included from Lunanode::APIActions::Volume

#volume_attach, #volume_create, #volume_delete, #volume_detach, #volume_extend, #volume_info, #volume_list, #volume_snapshot_create, #volume_snapshot_delete, #volume_snapshot_list

Constructor Details

#initialize(credentials_file) ⇒ API #initialize(api_id: nil, api_key: nil) ⇒ API

Returns a new instance of API.

Overloads:

  • #initialize(credentials_file) ⇒ API

    Instantiate an API object from a credentials file.

    Parameters:

    • credentials_file (IO, String)

      a JSON credentials file (which contains the keys ‘api_id` and `api_key`)

  • #initialize(api_id: nil, api_key: nil) ⇒ API

    Instantiate an API object from an API ID and API Key.

    Parameters:

    • api_id (String) (defaults to: nil)

      A LunaNode API ID

    • api_key (String) (defaults to: nil)

      A LunaNode API Key



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/lunanode/api.rb', line 73

def initialize(credentials_file = nil, api_id: nil, api_key: nil)
  if !credentials_file.nil? && File.exist?(credentials_file)
    credentials_data = File.read(credentials_file)
    credentials = JSON.parse(credentials_data, symbolize_names: true)
    @api_id = credentials[:api_id]
    @api_key = credentials[:api_key]
  else
    @api_id = api_id
    @api_key = api_key
  end
  @api_id = @api_id.to_s.freeze
  @api_key = @api_key.to_s.freeze
  @rest_client = RestClient::Resource.new(
    API_ENDPOINT,
    verify_ssl: OpenSSL::SSL::VERIFY_PEER
  )
end

Instance Attribute Details

#api_idObject (readonly)

Returns the value of attribute api_id.



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

def api_id
  @api_id
end

Class Method Details

.params_for(method_name) ⇒ Hash

Show parameter info for any API method.

Parameters:

  • method_name (#to_sym)

    The name of the API method

Returns:

  • (Hash)

    information about the method parameters



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/lunanode/api.rb', line 36

def self.params_for(method_name)
  @params_for[method_name] ||= begin
    method_name = method_name.to_sym
    param_groups = instance_method(method_name).parameters.group_by(&:first)
    out = {
      required: param_groups[:keyreq] && param_group[:keyreq].map(&:last),
      optional: param_groups[:key] && param_groups[:key].map(&:last),
      additional: param_groups.key?(:keyrest),
    }
    out.each do |k, v|
      if v
        v.freeze
      else
        out.delete(k)
      end
    end
    out.freeze
  end
end

Instance Method Details

#action(category, action, **params) ⇒ Hash, ...

Send an arbitrary API action without any parameter checks.

Parameters:

  • category (#to_sym)

    The API action category (i.e. dns, image, vm…)

  • action (#to_sym)

    The API action name (i.e. create, list…)

  • params (Hash)

    Any parameters required for the action.

Returns:

  • (Hash, Array, String)

    Response data from the API action.

Raises:

  • (APIError)

    If there was a problem with the action.



106
107
108
109
110
111
112
113
114
115
116
# File 'lib/lunanode/api.rb', line 106

def action(category, action, **params)
  resp = call_api("#{category}/#{action}/", params)
  raise APIError, %("#{resp[:error]}") unless resp[:success] == "yes"
  resp.delete(:success)

  if resp.size == 1
    resp.values.first
  else
    resp
  end
end

#inspectObject Also known as: to_s

Display a string representation of the object without key information.



92
93
94
# File 'lib/lunanode/api.rb', line 92

def inspect
  "#<#{self.class.name}: api_id=#{api_id.inspect}>"
end