onecloud 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 04ea9ba83cd619761bf2e607eeb820a1b29ac1e0
4
+ data.tar.gz: 0c50eb05622226654ee221843d7258f12523703c
5
+ SHA512:
6
+ metadata.gz: 906ca0dacd3001e0937eb98d2e070bf8214404fdf5e539f7482effb8185f985a332cfeaa8a58e091fa222816abc4c0ecfd548e92bd324f26de6772fb89c7f48d
7
+ data.tar.gz: b6420bbdad88b0ffba4993208e474fc523b26545b13ca798e66f93eabd7949611c492edc7ad8bd68d65aa48cedb49f29fb6ec05a3f0d1252a841b1c39b28e19e
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.3
4
+ before_install: gem install bundler -v 1.10.6
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in onecloud.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 IlyaV
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,65 @@
1
+ # Onecloud
2
+
3
+ Onecloud - API wrapper for Russian cloud provider 1Cloud.ru
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'onecloud'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install onecloud
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ require 'onecloud'
25
+
26
+ api = Onecloud::Api.new('API_SECRET_TOKEN')
27
+
28
+ # Get account balance
29
+ api.balance
30
+
31
+ # Create new server
32
+ api.add_server({
33
+ Name: "testAPI",
34
+ CPU: 1,
35
+ RAM: 1024,
36
+ HDD: 40,
37
+ imageID: 1,
38
+ HDDType: "SSD",
39
+ IsHighPerformance: true
40
+ })
41
+
42
+ # List all created servers
43
+ api.servers
44
+
45
+ # Update the server parameters
46
+ api.update_server_by_id('SERVER_ID', {
47
+ isHigtPerformance: false,
48
+ CPU: 2,
49
+ RAM: 2048,
50
+ HDD: 80
51
+ })
52
+ ```
53
+
54
+ ## Development
55
+
56
+ Pull requests are welcome!
57
+
58
+ ## Contributing
59
+
60
+ Bug reports and pull requests are welcome on GitHub at https://github.com/wimnorder/onecloud.
61
+
62
+ ## License
63
+
64
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
65
+
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "onecloud"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/lib/onecloud.rb ADDED
@@ -0,0 +1,83 @@
1
+ require 'net/http'
2
+ require 'openssl'
3
+ require 'json'
4
+ require 'uri'
5
+
6
+ require 'onecloud/customer'
7
+ require 'onecloud/dc'
8
+ require 'onecloud/dns'
9
+ require 'onecloud/errors'
10
+ require 'onecloud/image'
11
+ require 'onecloud/network'
12
+ require 'onecloud/server'
13
+ require 'onecloud/request'
14
+ require 'onecloud/version'
15
+
16
+ # Onecloud API Gem
17
+ module Onecloud
18
+ # Onecloud API Authorize
19
+ # Params:
20
+ # +token+:: Client access token
21
+ class Api
22
+ include Request
23
+ include Customer
24
+ include DC
25
+ include DNS # TODO: Rename to Domains
26
+ include Image
27
+ include Network
28
+ include Server
29
+ include Errors
30
+
31
+ API_ENDPOINT = 'https://api.1cloud.ru/'.freeze
32
+
33
+ HTTP_METHODS = {
34
+ get: Net::HTTP::Get,
35
+ put: Net::HTTP::Put,
36
+ post: Net::HTTP::Post,
37
+ delete: Net::HTTP::Delete
38
+ }
39
+
40
+ def initialize(token)
41
+ @token = token
42
+ uri = URI.parse(API_ENDPOINT)
43
+
44
+ @http = Net::HTTP.start(uri.host, uri.port, use_ssl: true)
45
+ @http.verify_mode = OpenSSL::SSL::VERIFY_PEER
46
+ @http.ssl_version = :TLSv1_2
47
+ end
48
+
49
+ private
50
+
51
+ def json_request(meth, path, params = {})
52
+ response = request(meth, path, params)
53
+
54
+ result = Struct.new(:code, :status, :body)
55
+
56
+ if !response.body.empty? # DELETE requests has empty body
57
+ json_body = JSON.parse(response.body, quirks_mode: true)
58
+ result.new(response.code, RESPONSE_CODES[response.code.to_s.to_sym], json_body)
59
+ else
60
+ result.new(response.code, nil)
61
+ end
62
+ end
63
+
64
+ def request(meth, path, params = {})
65
+ full_path = encode_path_params([API_ENDPOINT, path].join, params)
66
+
67
+ request = HTTP_METHODS[meth.to_sym].new(full_path)
68
+
69
+ request['Content-Type'] = 'application/json'
70
+ request['Authorization'] = ['Bearer ', @token].join
71
+
72
+ if ['post', 'put'].include?(meth.to_s)
73
+ request.body = params.to_json
74
+ return @http.request(request)
75
+ end
76
+ @http.request(request)
77
+ end
78
+
79
+ def encode_path_params(path, params)
80
+ [path, URI.encode_www_form(params)].join('?')
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,8 @@
1
+ module Onecloud
2
+ # Get current balance
3
+ module Customer
4
+ def balance
5
+ get('customer/balance')
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,8 @@
1
+ module Onecloud
2
+ # Datacenter
3
+ module DC
4
+ def datacenters
5
+ get('dcLocation')
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,64 @@
1
+ module Onecloud
2
+ # Operations with DNS
3
+ module DNS # TODO: Rename to 'Domains'
4
+ # List of all domains
5
+ def domains
6
+ get("Dns") # TODO: alias: all_dns, dns, dns_list
7
+ end
8
+
9
+ # Domain by ID
10
+ def domain_by_id(domain_id)
11
+ get("Dns/#{domain_id}")
12
+ end
13
+
14
+ # Create new domain
15
+ def add_domain(params)
16
+ post('dns', params)
17
+ end
18
+
19
+ # Delete domain by ID
20
+ def remove_domain(domain_id)
21
+ delete("dns/#{domain_id}")
22
+ end
23
+
24
+ # Create A type record
25
+ def add_domain_record_A(params)
26
+ post('dns/recorda', params)
27
+ end
28
+
29
+ # Create AAAA type record
30
+ def add_domain_record_AAAA(params)
31
+ post('dns/recordaaaa', params)
32
+ end
33
+
34
+ # Create CNAME type record
35
+ def add_domain_record_CNAME(params)
36
+ post('dns/recordcname', params)
37
+ end
38
+
39
+ # Create MX type record
40
+ def add_domain_record_MX(params)
41
+ post('dns/recordmx', params)
42
+ end
43
+
44
+ # Create NS type record
45
+ def add_domain_record_NS(params)
46
+ post('dns/recordns', params)
47
+ end
48
+
49
+ # Create TXT type record
50
+ def add_domain_record_TXT(params)
51
+ post('dns/recordtxt', params)
52
+ end
53
+
54
+ # Remove record for domain by his ID
55
+ def remove_domain_record(domain_id, record_id)
56
+ delete("dns/#{domain_id}/#{record_id}")
57
+ end
58
+
59
+ # Create SRV type record
60
+ def add_domain_record_SRV(params)
61
+ post('dns/recordsrv', params)
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,13 @@
1
+ module Onecloud
2
+ # API response codes
3
+ module Errors
4
+ RESPONSE_CODES = {
5
+ "200": 'Request successfully complete',
6
+ "401": 'Request is not authorized',
7
+ "403": 'Forbidden',
8
+ "404": 'Object not found',
9
+ "400": 'Bad request parameters',
10
+ "500": 'Unknown error has occurred - contact 1Cloud.ru support'
11
+ }
12
+ end
13
+ end
@@ -0,0 +1,22 @@
1
+ module Onecloud
2
+ # Server Images (templates)
3
+ module Image
4
+ # Get list of all available images
5
+ def images
6
+ get('Image')
7
+ end
8
+
9
+ # Create new Image
10
+ # +Name+::string
11
+ # +TechName+::number
12
+ # +ServerID+::number
13
+ def add_image(params)
14
+ post('image', params)
15
+ end
16
+
17
+ # Delete Image
18
+ def remove_image(id)
19
+ delete("image/#{id}")
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,24 @@
1
+ module Onecloud
2
+ # Private networks
3
+ module Network
4
+ # List of all networks
5
+ def networks
6
+ get('network')
7
+ end
8
+
9
+ # Private network by ID
10
+ def network_by_id(id)
11
+ get("network/#{id}")
12
+ end
13
+
14
+ # Create new network with params
15
+ def add_network(params)
16
+ post('network', params)
17
+ end
18
+
19
+ # Remove network by ID
20
+ def remove_network(id)
21
+ delete("network/#{id}")
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,25 @@
1
+
2
+ module Onecloud
3
+ # Requests data from API
4
+ module Request
5
+ def get(path, params = {})
6
+ json_request :get, path, params
7
+ end
8
+
9
+ def put(path, params = {})
10
+ json_request :put, path, params
11
+ end
12
+
13
+ def post(path, params = {})
14
+ json_request :post, path, params
15
+ end
16
+
17
+ def patch(path, params = {})
18
+ json_request :patch, path, params
19
+ end
20
+
21
+ def delete(path)
22
+ json_request :delete, path
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,50 @@
1
+ module Onecloud
2
+ # Server methods
3
+ module Server
4
+ # List of all servers
5
+ def servers
6
+ get('Server')
7
+ end
8
+
9
+ # Get server by ID
10
+ def server_by_id(server_id)
11
+ get("Server/#{server_id}")
12
+ end
13
+
14
+ # Create new server
15
+ def add_server(params)
16
+ post('Server', params)
17
+ end
18
+
19
+ # Update server configuration
20
+ def update_server_by_id(server_id, params)
21
+ put("Server/#{server_id}", params)
22
+ end
23
+
24
+ # Remove server by ID
25
+ def remove_server(server_id)
26
+ delete("Server/#{server_id}")
27
+ end
28
+
29
+ # System actions with server by ID
30
+ # TODO: separate actions by name (eg. server_reload(id), server_poweroff)
31
+ def server_do_action(server_id, params)
32
+ post("Server/#{server_id}/Action", params)
33
+ end
34
+
35
+ # All previous actions on the server
36
+ def server_actions(server_id)
37
+ get("Server/#{server_id}/Action")
38
+ end
39
+
40
+ # The action on the server
41
+ def server_apply_action(server_id, action_id)
42
+ get("Server/#{server_id}/Action/#{action_id}")
43
+ end
44
+
45
+ # Rebuild server
46
+ def server_rebuild(server_id, params)
47
+ post("Server/#{server_id}/rebuild", params)
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,3 @@
1
+ module Onecloud
2
+ VERSION = "0.1.0"
3
+ end
data/onecloud.gemspec ADDED
@@ -0,0 +1,32 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'onecloud/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'onecloud'
8
+ spec.version = Onecloud::VERSION
9
+ spec.authors = ['IlyaV']
10
+ spec.email = ['ilya@codeplay.ru']
11
+
12
+ spec.summary = 'API wrapper for 1Cloud'
13
+ spec.description = 'Onecloud - API wrapper for Russian cloud provider 1Cloud.ru'
14
+ spec.homepage = 'https://github.com/wimnorder/onecloud'
15
+ spec.license = 'MIT'
16
+
17
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
+ # delete this section to allow pushing this gem to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata['allowed_push_host'] = 'https://rubygems.org'
21
+ else
22
+ raise 'RubyGems 2.0 or newer is required to protect against public gem pushes.'
23
+ end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ spec.bindir = 'exe'
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ['lib']
29
+
30
+ spec.add_development_dependency 'bundler', '~> 1.10'
31
+ spec.add_development_dependency 'rake', '~> 10.0'
32
+ end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: onecloud
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - IlyaV
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-09-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.10'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: Onecloud - API wrapper for Russian cloud provider 1Cloud.ru
42
+ email:
43
+ - ilya@codeplay.ru
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - ".travis.yml"
50
+ - CODE_OF_CONDUCT.md
51
+ - Gemfile
52
+ - LICENSE.txt
53
+ - README.md
54
+ - Rakefile
55
+ - bin/console
56
+ - bin/setup
57
+ - lib/onecloud.rb
58
+ - lib/onecloud/customer.rb
59
+ - lib/onecloud/dc.rb
60
+ - lib/onecloud/dns.rb
61
+ - lib/onecloud/errors.rb
62
+ - lib/onecloud/image.rb
63
+ - lib/onecloud/network.rb
64
+ - lib/onecloud/request.rb
65
+ - lib/onecloud/server.rb
66
+ - lib/onecloud/version.rb
67
+ - onecloud.gemspec
68
+ homepage: https://github.com/wimnorder/onecloud
69
+ licenses:
70
+ - MIT
71
+ metadata:
72
+ allowed_push_host: https://rubygems.org
73
+ post_install_message:
74
+ rdoc_options: []
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements: []
88
+ rubyforge_project:
89
+ rubygems_version: 2.4.5.1
90
+ signing_key:
91
+ specification_version: 4
92
+ summary: API wrapper for 1Cloud
93
+ test_files: []