Class: AtlanticNet

Inherits:
Object
  • Object
show all
Defined in:
lib/atlantic_net.rb

Defined Under Namespace

Classes: HttpTransport

Constant Summary collapse

VERSION =
"2010-12-30"
FORMAT =
"json"

Instance Method Summary collapse

Constructor Details

#initialize(access_key, private_key, options = {}) ⇒ AtlanticNet

Returns a new instance of AtlanticNet.

Parameters:

  • access_key (String)

    The Access Key for your Account. This is the “API Public Key” from the Account Settings page.

  • private_key (String)

    The Private Key for your Account. This is the “API Private Key” from the Account Settings page.



53
54
55
56
57
# File 'lib/atlantic_net.rb', line 53

def initialize(access_key, private_key, options = {})
  @access_key = access_key
  @private_key = private_key
  @transport = transport_from_options(options)
end

Instance Method Details

#describe_images(options = {}) ⇒ Array<Hash>

Retrieve the description of all available cloud images or the description of a specific cloud image by providing the image id

Parameters:

  • options (Hash) (defaults to: {})

    A hash of the options

Options Hash (options):

  • :image_id (String)

    ID indicating the flavor and version number of the operating system image

Returns:

  • (Array<Hash>)

    A array of image descriptions



173
174
175
176
177
178
179
180
181
182
183
# File 'lib/atlantic_net.rb', line 173

def describe_images(options={})
  args = {}

  if options.has_key? :image_id
    response = api_call('describe-image', {imageid: options[:image_id]})
  else
    response = api_call('describe-image')      
  end

  response["describe-imageresponse"]["imagesset"].values
end

#describe_instance(instance_id) ⇒ Hash

Describe a specific cloud server.

Parameters:

  • instance_id (String)

    The instance ID of the server you want to describe.

Returns:

  • (Hash)

    The hash of the instance



108
109
110
111
# File 'lib/atlantic_net.rb', line 108

def describe_instance(instance_id)
  response = api_call('describe-instance', {instanceid: instance_id})
  response["describe-instanceresponse"]["instanceSet"]["item"]
end

#describe_plans(options = {}) ⇒ Array<Hash>

Retrieve a list of available cloud server plans, narrow the listing down optionally by server platform, or get information about just one specific plan

Parameters:

  • options (Hash) (defaults to: {})

    A hash of the options

Options Hash (options):

  • plan_name (String)

    The name of the plan to describe

  • platform (String)

    The platform to filter plans by (windows, linux)

Returns:

  • (Array<Hash>)

    An array of plan descriptions



194
195
196
197
198
199
200
201
202
# File 'lib/atlantic_net.rb', line 194

def describe_plans(options={})
  if options.empty?
    response = api_call('describe-plan')
  else
    response = api_call('describe-plan',options)
  end

  response["describe-planresponse"]["plans"].values
end

#list_instancesArray<Hash>

Retrieve the list of currently active cloud servers.

Returns:

  • (Array<Hash>)

    The list of servers



80
81
82
83
# File 'lib/atlantic_net.rb', line 80

def list_instances
  response = api_call('list-instances')
  response['list-instancesresponse']['instancesSet'].values
end

#list_ssh_keysArray<Hash>

Retrieve the details of all SSH Keys associated with the account

Returns:

  • (Array<Hash>)

    An array of SSH keys



208
209
210
211
# File 'lib/atlantic_net.rb', line 208

def list_ssh_keys
  response = api_call('list-sshkeys')
  response["list-sshkeysresponse"]["KeysSet"].values
end

#reboot_instance(instance_id, options = {}) ⇒ Hash

Restart a specific cloud server.

Parameters:

  • instance_id (String)

    The instance ID of the server you want to reboot.

  • options (Hash) (defaults to: {})

    The options to include with restart.

Options Hash (options):

  • :reboot_type (String) — default: soft

    Whether you want to perform a soft or hard reboot. soft is the equivalent of performing a graceful shutdown. hard is the equivalent of disconnecting the power and reconnecting it.

Returns:

  • (Hash)

    The return value of the request



96
97
98
99
100
# File 'lib/atlantic_net.rb', line 96

def reboot_instance(instance_id, options={})
  reboot_type = options[:reboot_type] || "soft"
  response = api_call('reboot-instance', {instanceid: instance_id, reboottype: reboot_type})
  response["reboot-instanceresponse"]["return"]
end

#run_instance(server_name, plan_name, vm_location, options = {}) ⇒ Hash

Run a cloud server with the provided configuration options.

Parameters:

  • server_name (String)

    The name/description that will be used for the instance

  • plan_name (String)

    The plan that should be used for this instance

  • vm_location (String)

    The data center location you want this instance launched

  • args (Hash)

    A hash of the options

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :image_id (String)

    The VM image to use for this instance. This or :clone_image are required.

  • :clone_image (String)

    The server to clone for this instance. This or :image_id are required.

  • :enable_backup (Boolean)

    Whether backups should be enabled for this instance.

  • :server_quantity (Int)

    How many instances of this server should be launched.

  • :key_id (String)

    The ID of the key to deploy for accessing this server.

Returns:

  • (Hash)

    The run instance result



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/atlantic_net.rb', line 141

def run_instance(server_name, plan_name, vm_location, options={})
  option_mapping = {
    image_id: :imageid,
    clone_image: :cloneimage,
    enable_backup: :enablebackup,
    server_quantity: :serverqty,
    key_id: :key_id
  }

  request_options = {servername: server_name, planname: plan_name, vm_location: vm_location}

  unless options.has_key? :image_id or options.has_key? :clone_image
    fail ArgumentError.new("Missing argument: image_id or clone_image are required")
  end

  option_mapping.each do | key, value |
    if options.has_key? key
      request_options[value] = options[key]
    end
  end

  response = api_call('run-instance', request_options)
  response["run-instanceresponse"]["instancesSet"]["item"]
end

#signature(timestamp, request_id) ⇒ String

Generates a Base64 encoded Sha256 HMAC given a timestamp and a request_id

Parameters:

  • timestamp (String, Int)

    The timestamp of the requesting call. This should be the time the the request was originally made.

  • request_id (String)

    The UUID representing the request. This should be unique for each request.

Returns:

  • (String)

    A Base64 encoded HMAC of the timestamp and request_id



69
70
71
72
73
74
# File 'lib/atlantic_net.rb', line 69

def signature(timestamp, request_id)
  string_to_sign = "#{timestamp}#{request_id}"

  digest = OpenSSL::Digest.new('sha256')
  Base64.encode64(OpenSSL::HMAC.digest(digest, @private_key, string_to_sign)).strip()
end

#terminate_instance(instance_id) ⇒ Hash

Terminate a specific cloud server.

Parameters:

  • instance_id (String)

    The instance ID of the server you want to terminate.

Returns:

  • (Hash)

    The termination response result.



119
120
121
122
# File 'lib/atlantic_net.rb', line 119

def terminate_instance(instance_id)
  response = api_call('terminate-instance', {instanceid: instance_id})
  response["terminate-instanceresponse"]["instancesSet"]["item"]
end