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: , api_key: ) ⇒ API

Instantiate an API object

Overloads:

  • #initialize(credentials_file) ⇒ API

    Instantiate an API object from a credentials file.

    Parameters:

    • credentials_file (String, File)

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

    Raises:

    • (JSON::ParserError)

      if the JSON file could not be read properly.

    • (KeyError)

      if any required key could not be found.

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

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

    Parameters:

    • api_id (String)

      A LunaNode API ID

    • api_key (String)

      A LunaNode API Key

    Raises:

    • (KeyError)

      if any required key could not be found.


78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/lunanode/api.rb', line 78

def initialize(*args, **options)
  credentials_file = args.compact.first.to_s
  if File.exist?(credentials_file)
    credentials_data = File.read(credentials_file)
    options = JSON.parse(credentials_data, symbolize_names: true)
  end
  @api_id = options.fetch(:api_id).to_s.dup.freeze
  @api_key = options.fetch(:api_key).to_s.dup.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


57
58
59
# File 'lib/lunanode/api.rb', line 57

def api_id
  @api_id
end

Class Method Details

.params_for(method_name) ⇒ Hash

Show parameter info for any Lunanode::API instance method.

The keys of the hash results denote the type of parameter:

  • :keyreq => Required keyword argument

  • :key => Optional keyword argument

  • :keyrest => Arbitrary additional keyword arguments

  • :req => Required positional argument

  • :opt => Optional positional argument

  • :rest => Arbitrary additional positional arguments

Parameters:

  • method_name (#to_sym)

    The name of the API method

Returns:

  • (Hash)

    information about the method parameters.


43
44
45
46
47
48
49
50
# File 'lib/lunanode/api.rb', line 43

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 = param_groups.map { |k, v| [k, v.map(&:last)] }.to_h
    out.each_value(&:freeze).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.


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

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.


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

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