digital_ocean 0.0.1alpha1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (67) hide show
  1. data/.gitignore +18 -0
  2. data/.rspec +2 -0
  3. data/.travis.yml +9 -0
  4. data/Gemfile +4 -0
  5. data/Guardfile +11 -0
  6. data/LICENSE.txt +22 -0
  7. data/README.md +32 -0
  8. data/Rakefile +17 -0
  9. data/digital_ocean.gemspec +38 -0
  10. data/lib/digital_ocean/api.rb +46 -0
  11. data/lib/digital_ocean/authentication_middleware.rb +29 -0
  12. data/lib/digital_ocean/resource/base.rb +22 -0
  13. data/lib/digital_ocean/resource/droplet.rb +84 -0
  14. data/lib/digital_ocean/resource/image.rb +17 -0
  15. data/lib/digital_ocean/resource/region.rb +9 -0
  16. data/lib/digital_ocean/resource/size.rb +9 -0
  17. data/lib/digital_ocean/resource/ssh_key.rb +27 -0
  18. data/lib/digital_ocean/resource.rb +12 -0
  19. data/lib/digital_ocean/version.rb +3 -0
  20. data/lib/digital_ocean.rb +10 -0
  21. data/spec/api_spec.rb +421 -0
  22. data/spec/digital_ocean_spec.rb +7 -0
  23. data/spec/spec_helper.rb +21 -0
  24. data/spec/support/vcr.rb +21 -0
  25. data/spec/vcr/cassettes/DigitalOcean_API/_droplets/_create/should_be_successful.yml +48 -0
  26. data/spec/vcr/cassettes/DigitalOcean_API/_droplets/_delete/should_be_successful.yml +48 -0
  27. data/spec/vcr/cassettes/DigitalOcean_API/_droplets/_disable_backups/should_be_successful.yml +48 -0
  28. data/spec/vcr/cassettes/DigitalOcean_API/_droplets/_enable_backups/should_be_successful.yml +48 -0
  29. data/spec/vcr/cassettes/DigitalOcean_API/_droplets/_list/should_be_successful.yml +48 -0
  30. data/spec/vcr/cassettes/DigitalOcean_API/_droplets/_list/should_return_a_list_of_all_droplets.yml +48 -0
  31. data/spec/vcr/cassettes/DigitalOcean_API/_droplets/_power_cycle/should_be_successful.yml +48 -0
  32. data/spec/vcr/cassettes/DigitalOcean_API/_droplets/_power_off/should_be_successful.yml +48 -0
  33. data/spec/vcr/cassettes/DigitalOcean_API/_droplets/_power_on/should_be_successful.yml +48 -0
  34. data/spec/vcr/cassettes/DigitalOcean_API/_droplets/_reboot/should_be_successful.yml +48 -0
  35. data/spec/vcr/cassettes/DigitalOcean_API/_droplets/_rebuild/should_be_successful.yml +48 -0
  36. data/spec/vcr/cassettes/DigitalOcean_API/_droplets/_reset_root_password/should_be_successful.yml +49 -0
  37. data/spec/vcr/cassettes/DigitalOcean_API/_droplets/_resize/should_be_successful.yml +48 -0
  38. data/spec/vcr/cassettes/DigitalOcean_API/_droplets/_restore/should_be_successful.yml +48 -0
  39. data/spec/vcr/cassettes/DigitalOcean_API/_droplets/_show/should_be_successful.yml +48 -0
  40. data/spec/vcr/cassettes/DigitalOcean_API/_droplets/_shutdown/should_be_successful.yml +48 -0
  41. data/spec/vcr/cassettes/DigitalOcean_API/_droplets/_snapshot/should_be_successful.yml +48 -0
  42. data/spec/vcr/cassettes/DigitalOcean_API/_images/_delete/invalid/should_not_be_successful.yml +48 -0
  43. data/spec/vcr/cassettes/DigitalOcean_API/_images/_delete/valid/should_be_successful.yml +49 -0
  44. data/spec/vcr/cassettes/DigitalOcean_API/_images/_list/with_filter_global/should_be_successful.yml +69 -0
  45. data/spec/vcr/cassettes/DigitalOcean_API/_images/_list/with_filter_global/should_return_global_images.yml +69 -0
  46. data/spec/vcr/cassettes/DigitalOcean_API/_images/_list/with_filter_my_images/should_be_successful.yml +48 -0
  47. data/spec/vcr/cassettes/DigitalOcean_API/_images/_list/with_filter_my_images/should_return_my_images.yml +48 -0
  48. data/spec/vcr/cassettes/DigitalOcean_API/_images/_list/without_filter/should_be_successful.yml +70 -0
  49. data/spec/vcr/cassettes/DigitalOcean_API/_images/_list/without_filter/should_return_a_list_of_all_images.yml +70 -0
  50. data/spec/vcr/cassettes/DigitalOcean_API/_images/_show/invalid/should_not_be_successful.yml +48 -0
  51. data/spec/vcr/cassettes/DigitalOcean_API/_images/_show/valid/should_be_successful.yml +48 -0
  52. data/spec/vcr/cassettes/DigitalOcean_API/_images/_show/valid/should_return_the_image.yml +48 -0
  53. data/spec/vcr/cassettes/DigitalOcean_API/_regions/_list/should_be_successful.yml +49 -0
  54. data/spec/vcr/cassettes/DigitalOcean_API/_regions/_list/should_return_a_list_of_all_available_regions.yml +49 -0
  55. data/spec/vcr/cassettes/DigitalOcean_API/_regions/_list/should_return_a_list_of_all_regions.yml +49 -0
  56. data/spec/vcr/cassettes/DigitalOcean_API/_sizes/_list/should_be_successful.yml +48 -0
  57. data/spec/vcr/cassettes/DigitalOcean_API/_sizes/_list/should_return_a_list_of_all_droplet_sizes.yml +48 -0
  58. data/spec/vcr/cassettes/DigitalOcean_API/_sizes/_list/should_return_the_correct_ID_for_the_96GB_size.yml +48 -0
  59. data/spec/vcr/cassettes/DigitalOcean_API/_ssh_keys/_add/should_be_successful.yml +48 -0
  60. data/spec/vcr/cassettes/DigitalOcean_API/_ssh_keys/_delete/should_be_successful.yml +48 -0
  61. data/spec/vcr/cassettes/DigitalOcean_API/_ssh_keys/_list/should_be_successful.yml +48 -0
  62. data/spec/vcr/cassettes/DigitalOcean_API/_ssh_keys/_list/should_return_a_list_of_all_SSH_keys.yml +48 -0
  63. data/spec/vcr/cassettes/DigitalOcean_API/_ssh_keys/_show/invalid/should_not_be_successful.yml +48 -0
  64. data/spec/vcr/cassettes/DigitalOcean_API/_ssh_keys/_show/valid/should_be_successful.yml +50 -0
  65. data/spec/vcr/cassettes/DigitalOcean_API/_ssh_keys/_show/valid/should_return_a_list_of_all_SSH_keys.yml +50 -0
  66. data/spec/vcr/cassettes/DigitalOcean_API/_ssh_keys/_show/valid/should_return_the_public_keykey.yml +50 -0
  67. metadata +353 -0
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .rbenv-vars
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,9 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.8.7
4
+ - 1.9.2
5
+ - 1.9.3
6
+ - jruby-19mode
7
+ - ruby-head
8
+ - jruby-head
9
+ script: bundle exec rake test
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in digital_ocean.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,11 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard 'rspec' do
5
+ watch(%r{^spec/.+_spec\.rb$})
6
+ watch(%r{^lib/(.+)/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}/#{m[2]}_spec.rb" }
7
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
8
+
9
+ watch('spec/spec_helper.rb') { "spec" }
10
+ end
11
+
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Roland Moriz
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,32 @@
1
+ # DigitalOcean
2
+
3
+ TODO: Write a gem description
4
+
5
+ [![Build Status](https://travis-ci.org/rmoriz/digital_ocean.png)](https://travis-ci.org/rmoriz/digital_ocean)
6
+
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ gem 'digital_ocean'
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install digital_ocean
21
+
22
+ ## Usage
23
+
24
+ TODO: Write usage instructions here
25
+
26
+ ## Contributing
27
+
28
+ 1. Fork it
29
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
30
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
31
+ 4. Push to the branch (`git push origin my-new-feature`)
32
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,17 @@
1
+ #encoding: utf-8
2
+ require 'rubygems'
3
+ require 'bundler'
4
+ Bundler.setup
5
+ Bundler::GemHelper.install_tasks
6
+
7
+ require 'rspec/core/rake_task'
8
+ desc 'Run RSpec'
9
+ RSpec::Core::RakeTask.new do |spec|
10
+ spec.pattern = 'spec/**/*_spec.rb'
11
+ spec.rspec_opts = ['--color', '--format nested']
12
+ end
13
+
14
+ desc 'Run rspec tests'
15
+ task :test => [:spec]
16
+
17
+ task :default => :test
@@ -0,0 +1,38 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'digital_ocean/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = 'digital_ocean'
8
+ gem.version = DigitalOcean::VERSION
9
+ gem.authors = ['Roland Moriz']
10
+ gem.email = ['roland@moriz.de']
11
+ gem.description = %q{A Ruby gem to interact with DigitalOcean, a cloud hosting provider}
12
+ gem.summary = %q{This gem wraps the DigitalOcean API documented at https://api.digitalocean.com/}
13
+ gem.homepage = 'https://github.com/rmoriz/digital_ocean'
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ['lib']
19
+
20
+ if RUBY_PLATFORM == 'java'
21
+ gem.add_runtime_dependency 'jruby-openssl'
22
+ end
23
+
24
+ gem.add_dependency 'faraday', '~> 0.8.4'
25
+ gem.add_dependency 'faraday_middleware'
26
+ gem.add_dependency 'json'
27
+ gem.add_dependency 'rash'
28
+
29
+ gem.add_development_dependency 'rspec', '~> 2.0'
30
+ gem.add_development_dependency 'guard'
31
+ gem.add_development_dependency 'guard-rspec'
32
+ gem.add_development_dependency 'rb-fsevent'
33
+
34
+ gem.add_development_dependency 'vcr', '~> 2.4'
35
+ gem.add_development_dependency 'fakeweb', '~> 1.3.0'
36
+ gem.add_development_dependency 'rake'
37
+ gem.add_development_dependency 'yard'
38
+ end
@@ -0,0 +1,46 @@
1
+ module DigitalOcean
2
+ class API
3
+ def initialize(params)
4
+ @client_id = params[:client_id]
5
+ @api_key = params[:api_key]
6
+ @debug = params[:debug]
7
+ @base_url = params[:base_url] || 'https://api.digitalocean.com/'
8
+ @faraday_adapter = params[:faraday_adapter] || Faraday.default_adapter
9
+ @faraday = params[:faraday] || default_faraday
10
+
11
+ raise ArgumentError, ':client_id missing' unless @client_id
12
+ raise ArgumentError, ':api_key missing' unless @api_key
13
+ end
14
+
15
+ def droplets
16
+ DigitalOcean::Resource::Droplet.new(@faraday)
17
+ end
18
+
19
+ def sizes
20
+ DigitalOcean::Resource::Size.new(@faraday)
21
+ end
22
+
23
+ def regions
24
+ DigitalOcean::Resource::Region.new(@faraday)
25
+ end
26
+
27
+ def ssh_keys
28
+ DigitalOcean::Resource::SSHKey.new(@faraday)
29
+ end
30
+
31
+ def images
32
+ DigitalOcean::Resource::Image.new(@faraday)
33
+ end
34
+
35
+ def default_faraday
36
+ Faraday.new(:url => @base_url) do |faraday|
37
+ faraday.use AuthenticationMiddleware, @client_id, @api_key
38
+ faraday.request :url_encoded
39
+ faraday.response :rashify
40
+ faraday.response :json
41
+ faraday.response(:logger) if @debug
42
+ faraday.adapter @faraday_adapter
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,29 @@
1
+ module DigitalOcean
2
+ class AuthenticationMiddleware < Faraday::Middleware
3
+ extend Forwardable
4
+ def_delegators :'Faraday::Utils', :parse_query, :build_query
5
+
6
+ def initialize(app, client_id, api_key)
7
+ @client_id = client_id
8
+ @api_key = api_key
9
+
10
+ super(app)
11
+ end
12
+
13
+ def call(env)
14
+ params = { 'client_id' => @client_id, 'api_key' => @api_key }.update query_params(env[:url])
15
+
16
+ env[:url].query = build_query params
17
+
18
+ @app.call(env)
19
+ end
20
+
21
+ def query_params(url)
22
+ if url.query.nil? or url.query.empty?
23
+ {}
24
+ else
25
+ parse_query url.query
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,22 @@
1
+ module DigitalOcean
2
+ module Resource
3
+ class Base
4
+ def initialize(connection, options = {})
5
+ @connection = connection
6
+ end
7
+
8
+ private
9
+
10
+ def apply_params(query, opts)
11
+ apply_default_params(query)
12
+
13
+ opts.each_pair do |key, value|
14
+ query.params[key] = value
15
+ end
16
+ end
17
+
18
+ def apply_default_params(query)
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,84 @@
1
+ module DigitalOcean
2
+ module Resource
3
+ class Droplet < Base
4
+
5
+ def list(opts = {})
6
+ @connection.get '/droplets' do |query|
7
+ apply_params(query, opts)
8
+ end.body
9
+ end
10
+
11
+ def show(id, opts = {})
12
+ @connection.get "/droplets/#{id}" do |query|
13
+ apply_params(query, opts)
14
+ end.body
15
+ end
16
+
17
+ def create(params)
18
+ @connection.get '/droplets/new' do |query|
19
+ apply_params(query, params)
20
+ end.body
21
+ end
22
+
23
+ def reboot(id)
24
+ @connection.put("/droplets/#{id}/reboot").body
25
+ end
26
+
27
+ def power_cycle(id)
28
+ @connection.put("/droplets/#{id}/power_cycle").body
29
+ end
30
+
31
+ def shutdown(id)
32
+ @connection.put("/droplets/#{id}/shutdown").body
33
+ end
34
+
35
+ def power_on(id)
36
+ @connection.put("/droplets/#{id}/power_on").body
37
+ end
38
+
39
+ def power_off(id)
40
+ @connection.put("/droplets/#{id}/power_off").body
41
+ end
42
+
43
+ def reset_root_password(id)
44
+ @connection.post("/droplets/#{id}/reset_root_password").body
45
+ end
46
+
47
+ def resize(id, params)
48
+ @connection.get "/droplets/#{id}/resize" do |query|
49
+ apply_params(query, params)
50
+ end.body
51
+ end
52
+
53
+ def snapshot(id, params)
54
+ @connection.get "/droplets/#{id}/snapshot" do |query|
55
+ apply_params(query, params)
56
+ end.body
57
+ end
58
+
59
+ def restore(id, params)
60
+ @connection.post "/droplets/#{id}/restore" do |query|
61
+ apply_params(query, params)
62
+ end.body
63
+ end
64
+
65
+ def rebuild(id, params)
66
+ @connection.post "/droplets/#{id}/rebuild" do |query|
67
+ apply_params(query, params)
68
+ end.body
69
+ end
70
+
71
+ def enable_backups(id)
72
+ @connection.post("/droplets/#{id}/enable_backups").body
73
+ end
74
+
75
+ def disable_backups(id)
76
+ @connection.post("/droplets/#{id}/disable_backups").body
77
+ end
78
+
79
+ def delete(id)
80
+ @connection.delete("/droplets/#{id}/destroy").body
81
+ end
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,17 @@
1
+ module DigitalOcean
2
+ module Resource
3
+ class Image < Base
4
+ def list(opts = {})
5
+ @connection.get('/images', opts).body
6
+ end
7
+
8
+ def show(id)
9
+ @connection.get("/images/#{id}").body
10
+ end
11
+
12
+ def delete(id)
13
+ @connection.get("/images/#{id}/destroy").body
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,9 @@
1
+ module DigitalOcean
2
+ module Resource
3
+ class Region < Base
4
+ def list
5
+ @connection.get('/regions').body
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ module DigitalOcean
2
+ module Resource
3
+ class Size < Base
4
+ def list
5
+ @connection.get('/sizes').body
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,27 @@
1
+ module DigitalOcean
2
+ module Resource
3
+ class SSHKey < Base
4
+ def list
5
+ @connection.get('/ssh_keys').body
6
+ end
7
+
8
+ def show(id)
9
+ @connection.get("/ssh_keys/#{id}").body
10
+ end
11
+
12
+ def delete(id)
13
+ @connection.delete("/ssh_keys/#{id}").body
14
+ end
15
+
16
+ #def add(args)
17
+ # @connection.get('/ssh_keys.json') do |query|
18
+ # apply_params(query, args)
19
+ # end.body
20
+ #end
21
+ #
22
+ #def edit
23
+ #end
24
+ #
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,12 @@
1
+ require 'digital_ocean/resource/base'
2
+ require 'digital_ocean/resource/droplet'
3
+ require 'digital_ocean/resource/size'
4
+ require 'digital_ocean/resource/region'
5
+ require 'digital_ocean/resource/ssh_key'
6
+ require 'digital_ocean/resource/image'
7
+
8
+ module DigitalOcean
9
+ module Resource
10
+
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ module DigitalOcean
2
+ VERSION = '0.0.1alpha1'
3
+ end
@@ -0,0 +1,10 @@
1
+ require 'faraday'
2
+ require 'faraday_middleware'
3
+
4
+ require 'digital_ocean/version'
5
+ require 'digital_ocean/authentication_middleware'
6
+ require 'digital_ocean/api'
7
+ require 'digital_ocean/resource'
8
+
9
+ module DigitalOcean
10
+ end