rancher.rb 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5b860f7f5900ba770de094e61604b608433f9eab
4
+ data.tar.gz: d268271db2cdba069fa2a8eb8550d6a5424c1127
5
+ SHA512:
6
+ metadata.gz: 31a5c788aeda80da43b96117545864871144dda6bba8e05f0f3c9140d5c1416537fc7139ad007542a02b6f170d7bcf98dbd10a6313ff747474e3db42da4138ca
7
+ data.tar.gz: 0bab89127990234de5a4d1a7ea1d04f4aef8dacceee45d020ea6585a4f040bff6c58d1a07cd1d9de9cf89120ab479a5813fde5d4b8a0d3ba63d3dc8939e57ddd
data/.codeclimate.yml ADDED
@@ -0,0 +1,42 @@
1
+ # This is a sample .codeclimate.yml configured for Engine analysis on Code
2
+ # Climate Platform. For an overview of the Code Climate Platform, see here:
3
+ # http://docs.codeclimate.com/article/300-the-codeclimate-platform
4
+
5
+ # Under the engines key, you can configure which engines will analyze your repo.
6
+ # Each key is an engine name. For each value, you need to specify enabled: true
7
+ # to enable the engine as well as any other engines-specific configuration.
8
+
9
+ # For more details, see here:
10
+ # http://docs.codeclimate.com/article/289-configuring-your-repository-via-codeclimate-yml#platform
11
+
12
+ # For a list of all available engines, see here:
13
+ # http://docs.codeclimate.com/article/296-engines-available-engines
14
+
15
+ engines:
16
+ # to turn on an engine, add it here and set enabled to `true`
17
+ # to turn off an engine, set enabled to `false` or remove it
18
+ rubocop:
19
+ enabled: true
20
+ bundler-audit:
21
+ enabled: true
22
+
23
+ # Engines can analyze files and report issues on them, but you can separately
24
+ # decide which files will receive ratings based on those issues. This is
25
+ # specified by path patterns under the ratings key.
26
+
27
+ # For more details see here:
28
+ # http://docs.codeclimate.com/article/289-configuring-your-repository-via-codeclimate-yml#platform
29
+
30
+ # Note: If the ratings key is not specified, this will result in a 0.0 GPA on your dashboard.
31
+
32
+ ratings:
33
+ paths:
34
+ - lib/**/*
35
+
36
+ # You can globally exclude files from being analyzed by any engine using the
37
+ # exclude_paths key.
38
+
39
+ exclude_paths:
40
+ - bin/*
41
+ - spec/**/*
42
+ #- vendor/**/*
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.2.2
data/.travis.yml ADDED
@@ -0,0 +1,16 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
5
+ - 2.1.4
6
+ - 2.2.2
7
+ notifications:
8
+ email: false
9
+ deploy:
10
+ provider: rubygems
11
+ api_key:
12
+ secure: UQmEbIJ3z2ZJ1WOuGPQEtIHeDEEhCxol3kdL7CNFDzYge9gU2jqWfVbgBgB2fmhlOCsREy7VHettGpPoPQ0chYtOIAZcoL/8HBjnkcab6ucllCI90NDjQ8wTja2WTAPJQ3f6MeY3bu9bHlI+Wwn8dmPYnGu0NbDva5YdVZmA0jD2f8vOWBLr2RSg7MmP+cvAUMyBf+eNH/IcYdP9mz1/fney37Dy7fUu0mUuLTqP8/gO9uc2wom9GkFcMHZyfnUQwQVun4raimJX7R9i/3zVxPxrL6/IJ9qQ1NxwRRDhN12gmkviJ7vW+zTbjCQc/fXghoaeivJWtMSR3sfYRPBTjSU6YbNZjD0uFLuyHHLwhBbTZyY9vxaYbY9RFtbg5Fmc0MA4xrRNFzi25wUMm3XDDfvMmV6XCRsWFxej/c95AlKzGxaps0uDjnEoETUnz5Dg+pX4WqV788rajDcBFv3Obc5WyDVnelIzoZQ6MCh51ZtDSw3t1t2Go1XaKmjZxLtp0/UmaB4N4qlfN5pMaiVg3lhwuPBWifDSMnyIEmWHLzobIJTlCyEdavDXwFjBBXF1gktHSc+MNvdr4eECGUaxa4T1jMEXQ5IF4POFUFxQlVdp0h0dWFvCHMLlLWNv3ZlMoBa7tV6yl/gOkHH+kyRsEmZZTBgIT9o/YcgTzgd5VO8=
13
+ gem: dockercompose-generator
14
+ on:
15
+ tags: true
16
+ repo: jwhitcraft/rancher.rb
data/Gemfile ADDED
@@ -0,0 +1,22 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'rake'
4
+
5
+ group :development do
6
+ gem 'awesome_print', require: 'ap'
7
+ gem 'guard-rspec', '~> 4.5'
8
+ gem 'net-http-spy', github: 'masterkain/net-http-spy', branch: 'master'
9
+ end
10
+
11
+ group :test do
12
+ gem 'coveralls', require: false
13
+ gem 'multi_json', '~> 1.11.0'
14
+ gem 'mime-types', '< 2.0.0'
15
+ gem 'rspec', '~> 3.0.0'
16
+ gem 'simplecov', require: false
17
+ gem 'vcr', '~> 2.9.2'
18
+ gem 'webmock', '>= 1.9'
19
+ end
20
+
21
+ # Specify your gem's dependencies in rancher.gemspec
22
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Jon Whitcraft
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,70 @@
1
+ # Rancher.rb
2
+
3
+ Talk with [Rancher](http://rancher.io) from Ruby.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'rancher.rb'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install rancher.rb
20
+
21
+ ## Configuration
22
+
23
+ You can globally configure Rancher-rb to alawys use the same project by using the `.configure` command.
24
+ ```ruby
25
+ Rancher.configure do |c|
26
+ c.api_endpoint = 'http://localhost:8080/v1/projects/1a5/'
27
+ c.access_key = 'my_access_key_here'
28
+ c.secret_key = 'my_secret_key_here'
29
+ end
30
+ ```
31
+
32
+ Finding Resources
33
+ --------
34
+ Each resource type in the API is available as a member of the Client.
35
+
36
+ ### Listing all resources in a collection
37
+ ```ruby
38
+ hosts = Rancher.host.query();
39
+ puts "There are #{hosts.length} hosts:\n";
40
+ hosts.each { |host|
41
+ puts host.getName
42
+ }
43
+ ```
44
+
45
+ ### Filtering
46
+ Filters allow you to search a collection for resources matching a set of conditions.
47
+ ```ruby
48
+ http_balancers = Rancher.loadbalancers.query({
49
+ :publicStartPort => 80
50
+ })
51
+ ```
52
+
53
+ ### Getting a single Resource by ID
54
+ If you know the ID of the resource you are looking for already, you can also get it directly.
55
+ ```ruby
56
+ host = Rancher.host.by_id('your-host-id');
57
+ ```
58
+
59
+ ## ToDo:
60
+
61
+ - Write More Tests
62
+ - Ability To Define Custom Classes for specific types
63
+
64
+ ## Contributing
65
+
66
+ 1. Fork it ( https://github.com/jwhitcraft/rancher.rb/fork )
67
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
68
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
69
+ 4. Push to the branch (`git push origin my-new-feature`)
70
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'bundler/gem_tasks'
2
+
3
+ require 'rspec/core/rake_task'
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task(default: [:spec])
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require 'net-http-spy'
5
+ require "rancher"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ 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/rancher.rb ADDED
@@ -0,0 +1,32 @@
1
+ require 'json'
2
+ require 'rancher/client'
3
+ require 'rancher/default'
4
+ # Ruby Toolkit for the Rancher API
5
+ module Rancher
6
+ class << self
7
+ include Rancher::Configurable
8
+ # API client based on configured options {Configurable}
9
+ #
10
+ # @return [Rancher::Client] API Wrapper
11
+ def client
12
+ return @client if defined?(@client) && @client.same_options?(options)
13
+ @client = Rancher::Client.new(options)
14
+ end
15
+
16
+ private
17
+
18
+ def respond_to_missing?(method_name, include_private = false)
19
+ client.respond_to?(method_name, include_private)
20
+ end
21
+
22
+ def method_missing(method_name, *args, &block)
23
+ if client.respond_to?(method_name)
24
+ return client.send(method_name, *args, &block)
25
+ end
26
+
27
+ super
28
+ end
29
+ end
30
+ end
31
+
32
+ Rancher.setup
@@ -0,0 +1,12 @@
1
+ module Rancher
2
+ # Extracts options from method arguments
3
+ # @private
4
+ class Arguments < Array
5
+ attr_reader :options
6
+
7
+ def initialize(args)
8
+ @options = args.last.is_a?(::Hash) ? args.pop : {}
9
+ super(args)
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,8 @@
1
+ module Rancher
2
+ # Authentication methods for {Rancher::Client}
3
+ module Authentication
4
+ def basic_authenticated?
5
+ (@access_key && @secret_key)
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,66 @@
1
+ require 'rancher/collection'
2
+ require 'rancher/type'
3
+ require 'rancher/resource'
4
+
5
+ module Rancher
6
+ # Convert Results into a Ruby Class
7
+ module Classify
8
+ def classify(data)
9
+ classify_recursive(data)
10
+ end
11
+
12
+ private
13
+
14
+ def is_object?(object)
15
+ (object.is_a?(Sawyer::Resource) || object.is_a?(Hash))
16
+ end
17
+
18
+ def classify_recursive(data, depth = 0, as = 'auto')
19
+ as = classify_type(data) if as == 'auto'
20
+
21
+ return classify_object(data, depth) if as == 'object'
22
+ return classify_array(data, depth) if as == 'array'
23
+
24
+ data
25
+ end
26
+
27
+ def classify_type(data)
28
+ if is_object?(data)
29
+ return 'object'
30
+ elsif data.is_a?(Array)
31
+ return 'array'
32
+ end
33
+
34
+ 'scalar'
35
+ end
36
+
37
+ def classify_object(data, depth)
38
+ data.each do |key, element|
39
+ if element.is_a?(Array)
40
+ data[key] = classify_array(element, depth + 1)
41
+ elsif is_object?(element) && element[:type] && data.rels[key]
42
+ data[key] = classify_object(element, depth + 1)
43
+ elsif element.is_a?(Sawyer::Resource)
44
+ data[key] = element.attrs
45
+ end
46
+ end
47
+
48
+ get_class(data).new(data.attrs)
49
+ end
50
+
51
+ def classify_array(data, depth)
52
+ data.map { |element| classify_recursive(element, depth + 1) }
53
+ end
54
+
55
+ def get_class(data)
56
+ klass = Rancher::Resource
57
+
58
+ if data[:type]
59
+ klass = Rancher::Collection if data[:type] == 'collection'
60
+ klass = Rancher::Error if data[:type] == 'error'
61
+ end
62
+
63
+ klass
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,81 @@
1
+ require 'rancher/configurable'
2
+ require 'rancher/default'
3
+ require 'rancher/authentication'
4
+ require 'rancher/connection'
5
+ require 'rancher/classify'
6
+
7
+ module Rancher
8
+ # The Main Client for talking with Rancher
9
+ class Client
10
+ include Rancher::Authentication
11
+ include Rancher::Configurable
12
+ include Rancher::Connection
13
+ include Rancher::Classify
14
+
15
+ attr_reader :types
16
+
17
+ # Header keys that can be passed in options hash to {#get},{#head}
18
+ CONVENIENCE_HEADERS = Set.new([:accept, :content_type])
19
+
20
+ def initialize(options = {})
21
+ # Use options passed in, but fall back to module defaults
22
+ @types = {}
23
+ Rancher::Configurable.keys.each do |key|
24
+ instance_variable_set(
25
+ :"@#{key}",
26
+ options[key] || Rancher.instance_variable_get(:"@#{key}")
27
+ )
28
+ end
29
+
30
+ load_schema
31
+ end
32
+
33
+ def load_schema
34
+ response = get 'schema'
35
+
36
+ response.each do |res|
37
+ @types[res.get_id.to_sym] = Rancher::Type.new(res)
38
+ end if response.is_a?(Rancher::Collection)
39
+ end
40
+
41
+ # Text representation of the client, masking tokens and passwords
42
+ #
43
+ # @return [String]
44
+ def inspect
45
+ inspected = super
46
+
47
+ if @secret_key
48
+ inspected = inspected.gsub! @secret_key, "#{'*'*36}#{@secret_key[36..-1]}"
49
+ end
50
+
51
+ inspected
52
+ end
53
+
54
+ # Set Rancher access_key
55
+ #
56
+ # @param value [String] Rancher access_key
57
+ def access_key=(value)
58
+ reset_agent
59
+ @access_key = value
60
+ end
61
+
62
+ # Set Rancher secret_key
63
+ #
64
+ # @param value [String] Rancher secret_key
65
+ def secret_key=(value)
66
+ reset_agent
67
+ @secret_key = value
68
+ end
69
+
70
+ def respond_to_missing?(method_name, _include_private = false)
71
+ (@types.key?(method_name))
72
+ end
73
+
74
+ def method_missing(method_name, *args, &block)
75
+ return @types[method_name] if respond_to?(method_name)
76
+
77
+ super
78
+ end
79
+
80
+ end
81
+ end