oceanic 0.0.0a

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'faraday'
4
+ gem 'faraday_middleware'
5
+
6
+ gemspec
@@ -0,0 +1,7 @@
1
+ Copyright (c) 2013 Vince Salinas
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,11 @@
1
+ )
2
+ ( /(
3
+ )\()) ( ) (
4
+ ((_)\ ( ))\ ( /( ( )\ (
5
+ ((_) )\ /((_))(_)) )\ )((_) )\
6
+ / _ \ ((_)(_)) ((_)_ _(_/( (_) ((_)
7
+ | (_) |/ _| / -_)/ _` || ' \))| |/ _|
8
+ \___/ \__| \___|\__,_||_||_| |_|\__|
9
+ -----
10
+ # Oceanic
11
+ Oceanic makes it simple to utilize the DigitalCloud API from Ruby. More documentation to follow.
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,82 @@
1
+ # Public: This is the main namespace for Oceanic. You can either use it to
2
+ # create Oceanic::Client objects, or access it directly.
3
+ #
4
+ # Examples
5
+ #
6
+ # Oceanic.client_id = 'TSQbHoqi771ivc17S1ksp'
7
+ # Oceanic.api_key = 'efdejo4W5BIGsWmqEOQbJmxQNis4tNoQ75jWZJMw1'
8
+ # Oceanic.droplets
9
+ #
10
+ # client = Oceanic.new 'TSQbHoqi771ivc17S1ksp', 'efdejo4W5BIGsWmqEOQbJmxQNis4tNoQ75jWZJMw1'
11
+ # client.droplets
12
+ #
13
+ module Oceanic
14
+ VERSION = "0.0.0a"
15
+
16
+ class << self
17
+ # Public: Gets or sets the API Key that Oceanic uses for requests.
18
+ # This is provided by DigitalOcean.
19
+ attr_accessor :api_key
20
+
21
+ # Public: Gets or sets the Client ID that Oceanic uses for requests.
22
+ # This is provided by DigitalOcean.
23
+ attr_accessor :client_id
24
+
25
+ # Public: Gets or sets the path that Oceanic libs are loaded from.
26
+ attr_accessor :lib_path
27
+
28
+ # Public: Sets the default Oceanic::Client for simple scripts that
29
+ # access the Oceanic constant directly.
30
+ #
31
+ # Oceanic.droplets
32
+ attr_writer :default_client
33
+
34
+ # Public: Initializes a new Oceanic::Client.
35
+ #
36
+ # client_id - Your Client ID provided by DigitalOcean.
37
+ # api_key - Your API Key provided by DigitalOcean.
38
+ #
39
+ # Example
40
+ #
41
+ # Oceanic.new 'TSQbHoqi771ivc17S1ksp', 'efdejo4W5BIGsWmqEOQbJmxQNis4tNoQ75jWZJMw1'
42
+ #
43
+ # Returns a Oceanic::Client.
44
+ def new(client_id = nil, api_key = nil)
45
+ client_id ||= @client_id
46
+ api_key ||= @api_key
47
+ Oceanic::Client.new(client_id, api_key)
48
+ end
49
+
50
+ # Internal: Requires internal Oceanic libraries.
51
+ #
52
+ # *libs - One or more relative String names to Oceanic classes.
53
+ #
54
+ # Returns nothing.
55
+ def require_libs(*libs)
56
+ libs.each do |lib|
57
+ require "#{lib_path}/#{lib}"
58
+ end
59
+ end
60
+
61
+ alias require_lib require_libs
62
+
63
+ private
64
+ # Internal: Proxies method calls on the Oceanic constant to
65
+ # #default_client.
66
+ def method_missing(name, *args, &block)
67
+ default_client.send(name, *args, &block)
68
+ end
69
+ end
70
+
71
+ self.lib_path = File.expand_path "../oceanic", __FILE__
72
+
73
+ # Gets the default client used for simple scripts.
74
+ #
75
+ # Returns an Oceanic::Client, configured with #client_id and
76
+ # #api_key
77
+ def self.default_client
78
+ @default_client ||= Connection.new(@client_id, @api_key)
79
+ end
80
+
81
+ require_libs 'image', 'size', 'region', 'ssh_key', 'droplet', 'client'
82
+ end
@@ -0,0 +1,210 @@
1
+ require 'faraday'
2
+ require 'faraday_middleware'
3
+
4
+ module Oceanic
5
+ # Public: Client objects manage the API requests made to DigitalOcean.
6
+ #
7
+ # Examples
8
+ #
9
+ # client = Oceanic.new 'TSQbHoqi771ivc17S1ksp', 'efdejo4W5BIGsWmqEOQbJmxQNis4tNoQ75jWZJMw1'
10
+ # # List droplets in account
11
+ # client.droplets
12
+ # # => {"status" => "OK",
13
+ # "droplets" => [{"backups_active" => nil,
14
+ # "id" => 100823,
15
+ # "image_id" => 420,
16
+ # "name" => "test222",
17
+ # "region_id" => 1,
18
+ # "size_id" => 33,
19
+ # "status" => "active"}]}
20
+ #
21
+ class Client
22
+ # Public: Gets the Client ID for this Client.
23
+ attr_reader :client_id
24
+
25
+ # Public: Gets the API Key for this Client.
26
+ attr_reader :api_key
27
+
28
+ def initialize(client_id, api_key)
29
+ raise ArgumentError, "An Oceanic::Client requires a Client ID." unless client_id
30
+ raise ArgumentError, "An Oceanic::Client requires an API Key." unless api_key
31
+
32
+ @client_id = client_id
33
+ @api_key = api_key
34
+ fd_options = {
35
+ headers: {'Accept' => 'application/json'},
36
+ ssl: {verify: false},
37
+ url: 'https://api.digitalocean.com/',
38
+ params: {client_id: @client_id, api_key: @api_key}
39
+ }
40
+ @responses = []
41
+ @connection = Faraday.new(fd_options) do |fd|
42
+ fd.request :url_encoded
43
+ fd.use FaradayMiddleware::ParseJson
44
+ fd.use FaradayMiddleware::FollowRedirects
45
+ fd.adapter :net_http
46
+ end
47
+ end
48
+
49
+ def droplets
50
+ res = request 'droplets'
51
+ if ok?
52
+ Droplets.new(self, @response['droplets'].map{|x| Droplet.new(self, x)})
53
+ end
54
+ end
55
+
56
+ def droplet(id)
57
+ request("droplets/#{id}")
58
+ if ok?
59
+ Droplet.new(self, @response['droplet'])
60
+ end
61
+ end
62
+
63
+ def droplet_create(name, size_id, image_id, region_id, ssh_key_ids = nil)
64
+ params = {name: name, size_id: size_id, image_id: image_id, region_id: region_id}
65
+ if ssh_key_ids
66
+ if ssh_key_ids.is_a?(String)
67
+ params[:ssh_key_ids] = ssh_key_ids
68
+ elsif ssh_key_ids.is_a?(Array)
69
+ params[:ssh_key_ids] = ssh_key_ids.join(',')
70
+ end
71
+ end
72
+ request 'droplets/new', params
73
+ end
74
+
75
+ def droplet_reboot(id)
76
+ request "droplets/#{id}/reboot"
77
+ end
78
+
79
+ def droplet_cycle(id)
80
+ request "droplets/#{id}/power_cycle"
81
+ end
82
+
83
+ def droplet_shutdown(id)
84
+ request "droplets/#{id}/shutdown"
85
+ end
86
+
87
+ def droplet_off(id)
88
+ request "droplets/#{id}/power_off"
89
+ end
90
+
91
+ def droplet_on(id)
92
+ request "droplets/#{id}/power_on"
93
+ end
94
+
95
+ def droplet_password_reset(id)
96
+ request "droplets/#{id}/password_reset"
97
+ end
98
+
99
+ def droplet_resize(id, size_id)
100
+ request "droplets/#{id}/reboot", {size_id: size_id}
101
+ end
102
+
103
+ def droplet_snapshot(id, snapshot_name)
104
+ request "droplets/#{id}/snapshot", {name: snapshot_name}
105
+ end
106
+
107
+ def droplet_restore(id, image_id)
108
+ request "droplets/#{id}/restore", {image_id: image_id}
109
+ end
110
+
111
+ def droplet_rebuild(id, image_id)
112
+ request "droplets/#{id}/rebuild", {image_id: image_id}
113
+ end
114
+
115
+ def droplet_enable_backups(id)
116
+ request "droplets/#{id}/enable_backups"
117
+ end
118
+
119
+ def droplet_disable_backups(id)
120
+ request "droplets/#{id}/disable_backups"
121
+ end
122
+
123
+ def droplet_destroy(id)
124
+ request "droplets/#{id}/destroy"
125
+ end
126
+
127
+ def regions
128
+ request 'regions'
129
+ if ok?
130
+ @response['regions'].map{|x| Region.new(self, x)}
131
+ end
132
+ end
133
+
134
+ def images
135
+ request 'images'
136
+ if ok?
137
+ @response['images'].map{|x| Image.new(self, x)}
138
+ end
139
+ end
140
+
141
+ def image(id)
142
+ request "images/#{id}"
143
+ if ok?
144
+ Image.new(self, @response['image'])
145
+ end
146
+ end
147
+
148
+ def image_destroy(id)
149
+ request "images/#{id}/destroy"
150
+ end
151
+
152
+ def ssh_keys
153
+ res = request 'ssh_keys'
154
+ if ok?
155
+ SshKeys.new(self, @response['ssh_keys'].map{|x| SshKey.new(self, x)})
156
+ end
157
+ end
158
+
159
+ def ssh_key(id)
160
+ request "ssh_keys/#{id}"
161
+ SshKey.new(self, @response['ssh_key'])
162
+ end
163
+
164
+ def ssh_key_create(name, public_key)
165
+ request 'ssh_keys/new', {name: name, ssh_pub_key: public_key}
166
+ end
167
+
168
+ def ssh_key_edit(id, new_public_key)
169
+ request "ssh_keys/#{id}/edit", {ssh_pub_key: new_public_key}
170
+ end
171
+
172
+ def ssh_key_destroy(id)
173
+ request "ssh_keys/#{id}/destroy"
174
+ end
175
+
176
+ def sizes
177
+ request 'sizes'
178
+ if ok?
179
+ @response['sizes'].map{|x| Size.new(self, x)}
180
+ end
181
+ end
182
+
183
+ def to_s
184
+ "(Oceanic::Client client_id: '#{@client_id}', api_key: '#{@api_key}')"
185
+ end
186
+
187
+ private
188
+ def ok?
189
+ @response || @response['status'] == 'OK'
190
+ end
191
+
192
+ def request(endpoint, params = {})
193
+ params[:client_id] = @client_id
194
+ params[:api_key] = @api_key
195
+ params.each do |k,v|
196
+ if v.is_a?(Image) or v.is_a?(Size) or v.is_a?(Region)
197
+ params[k] = v.id
198
+ end
199
+ end
200
+ resp = @connection.get "/#{endpoint}/"
201
+ if resp.body
202
+ @response = resp.body
203
+ @responses << @response
204
+ else
205
+ @response = nil
206
+ @responses << @response
207
+ end
208
+ end
209
+ end
210
+ end
@@ -0,0 +1,100 @@
1
+ module Oceanic
2
+ class Droplets < Array
3
+ def initialize(client, array)
4
+ super()
5
+ @client = client
6
+ array.each{|x| self << x}
7
+ end
8
+
9
+ def new(name, size_id, image_id, region_id, ssh_key_ids = nil)
10
+ @client.droplet_create(name, size_id, image_id, region_id, ssh_key_ids)
11
+ end
12
+ end
13
+
14
+ class Droplet
15
+ attr_reader :id
16
+ attr_reader :name
17
+ attr_reader :size_id
18
+ attr_reader :image_id
19
+ attr_reader :region_id
20
+ attr_reader :ip_address
21
+ attr_reader :status
22
+ attr_reader :ssh_key_ids
23
+
24
+ def initialize(client, hash)
25
+ @client = client
26
+ @id = hash['id']
27
+ @name = hash['name']
28
+ @size_id = hash['size_id']
29
+ @image_id = hash['image_id']
30
+ @region_id = hash['region_id']
31
+ @ip_address = hash['ip_address']
32
+ @status = hash['status']
33
+ @ssh_key_ids = hash['ssh_key_ids']
34
+ end
35
+
36
+ def size_id= value
37
+ resize(value)
38
+ end
39
+
40
+ def image_id= value
41
+ rebuild(value)
42
+ end
43
+
44
+ def reboot
45
+ @client.droplet_reboot(@id)
46
+ end
47
+
48
+ def cycle
49
+ @client.droplet_cycle(@id)
50
+ end
51
+
52
+ def shutdown
53
+ @client.droplet_shutdown(@id)
54
+ end
55
+
56
+ def off
57
+ @client.droplet_off(@id)
58
+ end
59
+
60
+ def on
61
+ @client.droplet_on(@id)
62
+ end
63
+
64
+ def password_reset
65
+ @client.droplet_password_reset(@id)
66
+ end
67
+
68
+ def resize(size_id)
69
+ @client.droplet_resize(@id, size_id)
70
+ end
71
+
72
+ def snapshot(snapshot_name)
73
+ @client.droplet_snapshot(@id, snapshot_name)
74
+ end
75
+
76
+ def restore(image_id)
77
+ @client.droplet_restore(@id, image_id)
78
+ end
79
+
80
+ def rebuild(image_id)
81
+ @client.droplet_rebuild(@id, image_id)
82
+ end
83
+
84
+ def enable_backups
85
+ @client.droplet_enable_backups(@id)
86
+ end
87
+
88
+ def disable_backups
89
+ @client.droplet_disable_backups(@id)
90
+ end
91
+
92
+ def destroy
93
+ @client.droplet_destroy(@id)
94
+ end
95
+
96
+ def to_s
97
+ "(Oceanic::Droplet id: #{id}, name: '#{@name}', ip_address: '#{@ip_address}', status: '#{@status}', size_id: #{@size_id}, image_id: #{@image_id}, region_id: #{@region_id}, ssh_key_ids: #{@ssh_key_ids.to_s})"
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,22 @@
1
+ module Oceanic
2
+ class Image
3
+ attr_reader :id
4
+ attr_reader :name
5
+ attr_reader :distribution
6
+
7
+ def initialize(client, hash)
8
+ @client = client
9
+ @id = hash['id']
10
+ @name = hash['name']
11
+ @distribution = hash['distribution']
12
+ end
13
+
14
+ def destroy
15
+ @client.image_destroy(@id)
16
+ end
17
+
18
+ def to_s
19
+ "(Oceanic::Image id: #{id}, name: '#{@name}', distribution: '#{@distribution}')"
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,16 @@
1
+ module Oceanic
2
+ class Region
3
+ attr_reader :id
4
+ attr_reader :name
5
+
6
+ def initialize(client, hash)
7
+ @client = client
8
+ @id = hash['id']
9
+ @name = hash['name']
10
+ end
11
+
12
+ def to_s
13
+ "(Oceanic::Region id: #{id}, name: '#{@name}')"
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,16 @@
1
+ module Oceanic
2
+ class Size
3
+ attr_reader :id
4
+ attr_reader :name
5
+
6
+ def initialize(client, hash)
7
+ @client = client
8
+ @id = hash['id']
9
+ @name = hash['name']
10
+ end
11
+
12
+ def to_s
13
+ "(Oceanic::Size id: #{id}, name: '#{@name}')"
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,46 @@
1
+ module Oceanic
2
+ class SshKeys < Array
3
+ def initialize(client, array)
4
+ super()
5
+ @client = client
6
+ array.each{|x| self << x}
7
+ end
8
+
9
+ def new(name, public_key)
10
+ @client.ssh_key_create(name, public_key)
11
+ # check result...
12
+ # self << SshKey.new(@client, result['id'], result['name'], result['public_key']
13
+ end
14
+ end
15
+
16
+ class SshKey
17
+ attr_reader :id
18
+ attr_reader :name
19
+ attr_reader :public_key
20
+
21
+ def initialize(client, hash)
22
+ @client = client
23
+ @id = hash['id']
24
+ @name = hash['name']
25
+ @public_key = hash['public_key']
26
+ end
27
+
28
+ def public_key= value
29
+ edit(value)
30
+ end
31
+
32
+ def edit(new_public_key)
33
+ @client.ssh_key_edit(@id, new_public_key)
34
+ # check result...
35
+ # @public_key = new_public_key
36
+ end
37
+
38
+ def destroy
39
+ @client.droplet_destroy(@id)
40
+ end
41
+
42
+ def to_s
43
+ "(Oceanic::SshKey id: #{id}, name: '#{@name}', public_key: '#{@public_key.slice(0,20)}...')"
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,32 @@
1
+ lib = 'oceanic'
2
+ lib_file = File.expand_path("../lib/#{lib}.rb", __FILE__)
3
+ File.read(lib_file) =~ /\bVERSION\s*=\s*["'](.+?)["']/
4
+ version = $1
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.specification_version = 1 if spec.respond_to? :specification_version=
8
+ spec.required_rubygems_version = '>= 1.3.6'
9
+
10
+ spec.name = lib
11
+ spec.version = version
12
+
13
+ spec.summary = %q{DigitalOcean API library}
14
+ spec.description = %q{Library for interaction with the DigitalOcean API.}
15
+
16
+ spec.authors = ["Vince Salinas"]
17
+ spec.email = ["kojul@kojul.com"]
18
+ spec.homepage = "https://github.com/kojul/oceanic"
19
+ spec.licenses = ['MIT']
20
+
21
+ spec.add_dependency "faraday", '>= 0.8.6'
22
+ spec.add_dependency "faraday_middleware", '>= 0.9.0'
23
+
24
+ spec.files = %w(README.md LICENSE.md Gemfile Rakefile)
25
+ spec.files << "#{lib}.gemspec"
26
+ spec.files += Dir.glob("lib/**/*.rb")
27
+ spec.files += Dir.glob("test/**/*.rb")
28
+
29
+ dev_null = File.exist?('/dev/null') ? '/dev/null' : 'NUL'
30
+ git_files = `git ls-files -z 2>#{dev_null}`
31
+ spec.files &= git_files.split("\0") if $?.success?
32
+ end
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: oceanic
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.0a
5
+ prerelease: 5
6
+ platform: ruby
7
+ authors:
8
+ - Vince Salinas
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-03-22 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: faraday
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 0.8.6
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 0.8.6
30
+ - !ruby/object:Gem::Dependency
31
+ name: faraday_middleware
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: 0.9.0
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: 0.9.0
46
+ description: Library for interaction with the DigitalOcean API.
47
+ email:
48
+ - kojul@kojul.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - README.md
54
+ - LICENSE.md
55
+ - Gemfile
56
+ - Rakefile
57
+ - oceanic.gemspec
58
+ - lib/oceanic/client.rb
59
+ - lib/oceanic/droplet.rb
60
+ - lib/oceanic/image.rb
61
+ - lib/oceanic/region.rb
62
+ - lib/oceanic/size.rb
63
+ - lib/oceanic/ssh_key.rb
64
+ - lib/oceanic.rb
65
+ homepage: https://github.com/kojul/oceanic
66
+ licenses:
67
+ - MIT
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ! '>'
82
+ - !ruby/object:Gem::Version
83
+ version: 1.3.1
84
+ requirements: []
85
+ rubyforge_project:
86
+ rubygems_version: 1.8.25
87
+ signing_key:
88
+ specification_version: 1
89
+ summary: DigitalOcean API library
90
+ test_files: []