fog-joyent 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (110) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +9 -0
  3. data/.rubocop.yml +20 -0
  4. data/.rubocop_todo.yml +436 -0
  5. data/.ruby-gemset +1 -0
  6. data/.ruby-version +1 -0
  7. data/.travis.yml +26 -0
  8. data/CONTRIBUTING.md +18 -0
  9. data/CONTRIBUTORS.md +17 -0
  10. data/Gemfile +4 -0
  11. data/LICENSE.md +10 -0
  12. data/README.md +29 -0
  13. data/Rakefile +12 -0
  14. data/fog-joyent.gemspec +29 -0
  15. data/lib/fog/bin/joyent.rb +33 -0
  16. data/lib/fog/joyent.rb +16 -0
  17. data/lib/fog/joyent/analytics.rb +310 -0
  18. data/lib/fog/joyent/compute.rb +272 -0
  19. data/lib/fog/joyent/core.rb +10 -0
  20. data/lib/fog/joyent/errors.rb +91 -0
  21. data/lib/fog/joyent/models/analytics/field.rb +13 -0
  22. data/lib/fog/joyent/models/analytics/fields.rb +24 -0
  23. data/lib/fog/joyent/models/analytics/instrumentation.rb +82 -0
  24. data/lib/fog/joyent/models/analytics/instrumentations.rb +23 -0
  25. data/lib/fog/joyent/models/analytics/joyent_module.rb +13 -0
  26. data/lib/fog/joyent/models/analytics/joyent_modules.rb +24 -0
  27. data/lib/fog/joyent/models/analytics/metric.rb +17 -0
  28. data/lib/fog/joyent/models/analytics/metrics.rb +16 -0
  29. data/lib/fog/joyent/models/analytics/transformation.rb +13 -0
  30. data/lib/fog/joyent/models/analytics/transformations.rb +24 -0
  31. data/lib/fog/joyent/models/analytics/type.rb +16 -0
  32. data/lib/fog/joyent/models/analytics/types.rb +24 -0
  33. data/lib/fog/joyent/models/analytics/value.rb +20 -0
  34. data/lib/fog/joyent/models/compute/datacenter.rb +11 -0
  35. data/lib/fog/joyent/models/compute/datacenters.rb +21 -0
  36. data/lib/fog/joyent/models/compute/flavor.rb +19 -0
  37. data/lib/fog/joyent/models/compute/flavors.rb +21 -0
  38. data/lib/fog/joyent/models/compute/image.rb +26 -0
  39. data/lib/fog/joyent/models/compute/images.rb +30 -0
  40. data/lib/fog/joyent/models/compute/key.rb +19 -0
  41. data/lib/fog/joyent/models/compute/keys.rb +32 -0
  42. data/lib/fog/joyent/models/compute/network.rb +12 -0
  43. data/lib/fog/joyent/models/compute/networks.rb +14 -0
  44. data/lib/fog/joyent/models/compute/server.rb +124 -0
  45. data/lib/fog/joyent/models/compute/servers.rb +35 -0
  46. data/lib/fog/joyent/models/compute/snapshot.rb +44 -0
  47. data/lib/fog/joyent/models/compute/snapshots.rb +35 -0
  48. data/lib/fog/joyent/requests/analytics/create_instrumentation.rb +25 -0
  49. data/lib/fog/joyent/requests/analytics/delete_instrumentation.rb +23 -0
  50. data/lib/fog/joyent/requests/analytics/describe_analytics.rb +26 -0
  51. data/lib/fog/joyent/requests/analytics/get_instrumentation.rb +26 -0
  52. data/lib/fog/joyent/requests/analytics/get_instrumentation_value.rb +30 -0
  53. data/lib/fog/joyent/requests/analytics/list_instrumentations.rb +25 -0
  54. data/lib/fog/joyent/requests/compute/add_machine_tags.rb +19 -0
  55. data/lib/fog/joyent/requests/compute/create_key.rb +54 -0
  56. data/lib/fog/joyent/requests/compute/create_machine.rb +16 -0
  57. data/lib/fog/joyent/requests/compute/create_machine_snapshot.rb +16 -0
  58. data/lib/fog/joyent/requests/compute/delete_all_machine_metadata.rb +16 -0
  59. data/lib/fog/joyent/requests/compute/delete_all_machine_tags.rb +15 -0
  60. data/lib/fog/joyent/requests/compute/delete_key.rb +27 -0
  61. data/lib/fog/joyent/requests/compute/delete_machine.rb +15 -0
  62. data/lib/fog/joyent/requests/compute/delete_machine_metadata.rb +16 -0
  63. data/lib/fog/joyent/requests/compute/delete_machine_snapshot.rb +18 -0
  64. data/lib/fog/joyent/requests/compute/delete_machine_tag.rb +15 -0
  65. data/lib/fog/joyent/requests/compute/get_dataset.rb +27 -0
  66. data/lib/fog/joyent/requests/compute/get_image.rb +28 -0
  67. data/lib/fog/joyent/requests/compute/get_key.rb +29 -0
  68. data/lib/fog/joyent/requests/compute/get_machine.rb +29 -0
  69. data/lib/fog/joyent/requests/compute/get_machine_metadata.rb +24 -0
  70. data/lib/fog/joyent/requests/compute/get_machine_snapshot.rb +15 -0
  71. data/lib/fog/joyent/requests/compute/get_machine_tag.rb +18 -0
  72. data/lib/fog/joyent/requests/compute/get_package.rb +32 -0
  73. data/lib/fog/joyent/requests/compute/list_datacenters.rb +16 -0
  74. data/lib/fog/joyent/requests/compute/list_datasets.rb +24 -0
  75. data/lib/fog/joyent/requests/compute/list_images.rb +25 -0
  76. data/lib/fog/joyent/requests/compute/list_keys.rb +25 -0
  77. data/lib/fog/joyent/requests/compute/list_machine_snapshots.rb +15 -0
  78. data/lib/fog/joyent/requests/compute/list_machine_tags.rb +20 -0
  79. data/lib/fog/joyent/requests/compute/list_machines.rb +26 -0
  80. data/lib/fog/joyent/requests/compute/list_networks.rb +26 -0
  81. data/lib/fog/joyent/requests/compute/list_packages.rb +34 -0
  82. data/lib/fog/joyent/requests/compute/reboot_machine.rb +15 -0
  83. data/lib/fog/joyent/requests/compute/resize_machine.rb +16 -0
  84. data/lib/fog/joyent/requests/compute/start_machine.rb +16 -0
  85. data/lib/fog/joyent/requests/compute/start_machine_from_snapshot.rb +15 -0
  86. data/lib/fog/joyent/requests/compute/stop_machine.rb +16 -0
  87. data/lib/fog/joyent/requests/compute/update_machine_metadata.rb +15 -0
  88. data/lib/fog/joyent/version.rb +5 -0
  89. data/tests/helper.rb +18 -0
  90. data/tests/helpers/mock_helper.rb +16 -0
  91. data/tests/helpers/succeeds_helper.rb +9 -0
  92. data/tests/joyent/models/analytics/field_tests.rb +10 -0
  93. data/tests/joyent/models/analytics/fields_tests.rb +13 -0
  94. data/tests/joyent/models/analytics/instrumentation_tests.rb +13 -0
  95. data/tests/joyent/models/analytics/instrumentations_tests.rb +3 -0
  96. data/tests/joyent/models/analytics/joyent_module_tests.rb +10 -0
  97. data/tests/joyent/models/analytics/joyent_modules_tests.rb +13 -0
  98. data/tests/joyent/models/analytics/metric_tests.rb +10 -0
  99. data/tests/joyent/models/analytics/metrics_tests.rb +20 -0
  100. data/tests/joyent/models/analytics/transformation_tests.rb +10 -0
  101. data/tests/joyent/models/analytics/transformations_tests.rb +13 -0
  102. data/tests/joyent/models/analytics/type_tests.rb +10 -0
  103. data/tests/joyent/models/analytics/types_tests.rb +13 -0
  104. data/tests/joyent/requests/analytics/instrumentation_tests.rb +44 -0
  105. data/tests/joyent/requests/compute/datasets_tests.rb +58 -0
  106. data/tests/joyent/requests/compute/keys_tests.rb +47 -0
  107. data/tests/joyent/requests/compute/machines_tests.rb +66 -0
  108. data/tests/joyent/requests/compute/networks_tests.rb +39 -0
  109. data/tests/joyent/requests/compute/packages_tests.rb +68 -0
  110. metadata +235 -0
@@ -0,0 +1 @@
1
+ fog-joyent
@@ -0,0 +1 @@
1
+ 2.3.3
@@ -0,0 +1,26 @@
1
+ before_install:
2
+ - gem install bundler
3
+
4
+ matrix:
5
+ include:
6
+ - rvm: 1.8.7
7
+ gemfile: gemfiles/Gemfile.1.9.2-
8
+ - rvm: 1.9.2
9
+ gemfile: gemfiles/Gemfile.1.9.2-
10
+ - rvm: 1.9.3
11
+ gemfile: gemfiles/Gemfile.1.9.2+
12
+ - rvm: 2.0.0
13
+ gemfile: gemfiles/Gemfile.1.9.2+
14
+ - rvm: 2.1.10
15
+ gemfile: gemfiles/Gemfile.1.9.2+
16
+ - rvm: 2.2.6
17
+ gemfile: gemfiles/Gemfile.1.9.2+
18
+ - rvm: 2.3.3
19
+ gemfile: gemfiles/Gemfile.1.9.2+
20
+ - rvm: 2.4.0
21
+ gemfile: gemfiles/Gemfile.1.9.2+
22
+ env: COVERAGE=true
23
+ - rvm: ree
24
+ gemfile: gemfiles/Gemfile.1.9.2-
25
+ - rvm: jruby
26
+ gemfile: gemfiles/Gemfile.1.9.2+
@@ -0,0 +1,18 @@
1
+ ## Getting Involved
2
+
3
+ New contributors are always welcome, when it doubt please ask questions. We strive to be an open and welcoming community. Please be nice to one another.
4
+
5
+ ### Coding
6
+
7
+ * Pick a task:
8
+ * Offer feedback on open [pull requests](https://github.com/fog/fog/pulls).
9
+ * Review open [issues](https://github.com/fog/fog/issues) for things to help on.
10
+ * [Create an issue](https://github.com/fog/fog/issues/new) to start a discussion on additions or features.
11
+ * Fork the project, add your changes and tests to cover them in a topic branch.
12
+ * Commit your changes and rebase against `fog/fog` to ensure everything is up to date.
13
+ * [Submit a pull request](https://github.com/fog/fog/compare/).
14
+
15
+ ### Non-Coding
16
+
17
+ * Offer feedback on open [issues](https://github.com/fog/fog/issues).
18
+ * Organize or volunteer at events.
@@ -0,0 +1,17 @@
1
+ * Kevin Olbrich <kolbrich@6fusion.com>
2
+ * Kevin Chan <kevin@yinkei.com>
3
+ * Paul Thornthwaite <tokengeek@gmail.com>
4
+ * Carlos Sanchez <csanchez@maestrodev.com>
5
+ * Lance Ivy <lance@cainlevy.net>
6
+ * Wesley Beary <geemus@gmail.com>
7
+ * geemus <geemus@gmail.com>
8
+ * Blake Irvin and Eric Saxby <pair+blake+sax@wanelo.com>
9
+ * James Herdman <james.herdman@me.com>
10
+ * Jared Everett <jared.everett@tcnp3.com>
11
+ * Kevin Menard <nirvdrum@gmail.com>
12
+ * Manuel Franco <mfranco@flexiant.com>
13
+ * Pablo Baños López <pablo@besol.es>
14
+ * Paul Thornthwaite <paul@brightbox.co.uk>
15
+ * Steve Smith <github@scsworld.co.uk>
16
+ * angus <ags@sdx.com.au>
17
+ * Matt Darby <matt.darby@rackspace.com>
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in fog-joyent.gemspec
4
+ gemspec
@@ -0,0 +1,10 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014-2015 CONTRIBUTORS.md
4
+
5
+ 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:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ 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.
10
+
@@ -0,0 +1,29 @@
1
+ # Fog::Joyent
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'fog-joyent'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install fog-joyent
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( https://github.com/fog/fog-joyent/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create a new Pull Request
@@ -0,0 +1,12 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rubocop/rake_task'
3
+
4
+ RuboCop::RakeTask.new
5
+
6
+ task :default => :test
7
+
8
+ desc 'Run fog-joyent unit tests'
9
+ mock = ENV['FOG_MOCK'] || 'true'
10
+ task :test do
11
+ sh("export FOG_MOCK=#{mock} && bundle exec shindont")
12
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'fog/joyent/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "fog-joyent"
8
+ spec.version = Fog::Joyent::VERSION
9
+ spec.authors = %q(The Effeminate Batman)
10
+ spec.email = %q(eb@frosthawk.com)
11
+
12
+ spec.summary = %q{This library can be used as a module for 'fog'.}
13
+ spec.description = %q{Module for the 'fog' gem to support Joyent.}
14
+ spec.homepage = "https://github.com/fog/fog-joyent"
15
+ spec.license = "MIT"
16
+
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = %w(lib)
20
+ spec.files = `git ls-files -z`.split("\x0")
21
+
22
+ spec.add_dependency 'fog-core', '~> 1.42'
23
+ spec.add_dependency 'fog-json', '>= 1.0'
24
+
25
+ spec.add_development_dependency 'bundler', '~> 1.10'
26
+ spec.add_development_dependency "rake", '~> 10.0'
27
+ spec.add_development_dependency 'shindo', '~> 0.3'
28
+ spec.add_development_dependency 'rubocop', '~> 0.34'
29
+ end
@@ -0,0 +1,33 @@
1
+ class Joyent < Fog::Bin
2
+ class << self
3
+ def class_for(key)
4
+ case key
5
+ when :compute
6
+ Fog::Compute::Joyent
7
+ when :analytics
8
+ Fog::Joyent::Analytics
9
+ else
10
+ raise ArgumentError, "Unrecognized service: #{key}"
11
+ end
12
+ end
13
+
14
+ def [](service)
15
+ @@connections ||= Hash.new do |hash, key|
16
+ hash[key] = case key
17
+ when :compute
18
+ Fog::Logger.warning("Joyent[:compute] is not recommended, use Compute[:joyent] for portability")
19
+ Fog::Compute.new(:provider => 'Joyent')
20
+ when :analytics
21
+ Fog::Joyent::Analytics.new
22
+ else
23
+ raise ArgumentError, "Unrecognized service: #{key.inspect}"
24
+ end
25
+ end
26
+ @@connections[service]
27
+ end
28
+
29
+ def services
30
+ Fog::Joyent.services
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,16 @@
1
+ require 'pathname'
2
+ require 'fog/core'
3
+
4
+ module Fog
5
+ module Joyent
6
+ mod_path = Pathname.new(File.expand_path("../joyent", __FILE__))
7
+
8
+ autoload :Analytics, mod_path.join("analytics.rb")
9
+ autoload :Compute, mod_path.join("compute.rb")
10
+
11
+ extend Fog::Provider
12
+
13
+ service(:analytics, 'Analytics')
14
+ service(:compute, 'Compute')
15
+ end
16
+ end
@@ -0,0 +1,310 @@
1
+ require 'fog/joyent'
2
+ require 'fog/joyent/errors'
3
+ require 'thread'
4
+
5
+ module Fog
6
+ module Joyent
7
+ class Analytics < Fog::Service
8
+ requires :joyent_username
9
+
10
+ recognizes :joyent_password
11
+ recognizes :joyent_url
12
+
13
+ recognizes :joyent_keyname
14
+ recognizes :joyent_keyfile
15
+ recognizes :joyent_keydata
16
+ recognizes :joyent_keyphrase
17
+ recognizes :joyent_version
18
+ request_path 'fog/joyent/requests/analytics'
19
+
20
+ request :describe_analytics
21
+ request :list_instrumentations
22
+ request :get_instrumentation
23
+ request :create_instrumentation
24
+ request :delete_instrumentation
25
+ request :get_instrumentation_value
26
+
27
+ model_path 'fog/joyent/models/analytics'
28
+
29
+ collection :joyent_modules
30
+ model :joyent_module
31
+
32
+ collection :metrics
33
+ model :metric
34
+
35
+ collection :fields
36
+ model :field
37
+
38
+ collection :types
39
+ model :type
40
+
41
+ collection :transformations
42
+ model :transformation
43
+
44
+ collection :instrumentations
45
+ model :instrumentation
46
+
47
+ model :value
48
+
49
+ class Mock
50
+ def self.data
51
+ @data ||= Hash.new do |hash, key|
52
+ hash[key] =case key
53
+ when :instrumentation
54
+ { 'module' => "cpu",
55
+ 'stat' => "usage",
56
+ 'predicate' => {},
57
+ 'decomposition' => ["zonename"],
58
+ 'value-dimension' => 2,
59
+ 'value-arity' => "discrete-decomposition",
60
+ 'enabled' => true,
61
+ 'retention-time' => 86400,
62
+ 'idle-max' => 86400,
63
+ 'transformations' => [],
64
+ 'nsources' => 3,
65
+ 'granularity' => 30,
66
+ 'persist-data' => false,
67
+ 'crtime' => 1388690982000,
68
+ 'value-scope' => "point",
69
+ 'id' => "63",
70
+ 'uris' =>
71
+ [{ "uri" => "/#{@joyent_username}/analytics/instrumentations/63/value/raw",
72
+ "name" => "value_raw" }] }
73
+ when :values
74
+ {
75
+ 'value' => { 'zoneid' => 0 },
76
+ 'transformations' => {},
77
+ 'start_time' => Time.now.utc - 600,
78
+ 'duration' => 30,
79
+ 'end_time' => Time.now.utc - 570,
80
+ 'nsources' => 1,
81
+ 'minreporting' => 1,
82
+ 'requested_start_time' => Time.now.utc - 600,
83
+ 'requested_duration' => 30,
84
+ 'requested_end_time' => Time.now.utc - 570
85
+ }
86
+ when :describe_analytics
87
+ {
88
+ 'fields' => {
89
+ 'zonename' => {
90
+ 'label' => 'zone name',
91
+ 'type' => 'string'
92
+ },
93
+ 'pid' => {
94
+ 'label' => 'process identifier',
95
+ 'type' => 'string'
96
+ }
97
+ },
98
+ 'modules' => {
99
+ 'cpu' => {
100
+ 'label' => 'CPU'
101
+ }
102
+ },
103
+ 'transformations' => {
104
+ 'geolocate' => {
105
+ 'label' => 'geolocate IP addresses',
106
+ 'fields' => ['raddr'] }
107
+ },
108
+ 'metrics' => [{
109
+ "module" => "cpu",
110
+ "stat" => "thread_executions",
111
+ "label" => "thread executions",
112
+ "interval" => "interval",
113
+ "fields" => ["hostname", "zonename", "runtime"],
114
+ "unit" => "operations"
115
+ }],
116
+ 'types' => {
117
+ 'string' => {
118
+ 'arity' => "discrete",
119
+ 'unit' => ""
120
+ }
121
+ }
122
+ }
123
+ else
124
+ {}
125
+ end
126
+ end
127
+ end
128
+
129
+ def data
130
+ self.class.data
131
+ end
132
+
133
+ def initialize(options = {})
134
+ @joyent_username = options[:joyent_username]
135
+ @joyent_password = options[:joyent_password]
136
+ @joyent_url = 'https://us-sw-1.api.joyentcloud.com'
137
+ @joyent_version = '~7'
138
+ end
139
+
140
+ def request(opts)
141
+ raise "Not Implemented"
142
+ end
143
+ end # Mock
144
+
145
+ class Real
146
+ def initialize(options = {})
147
+ @mutex = Mutex.new
148
+ @connection_options = options[:connection_options] || {}
149
+ @persistent = options[:persistent] || false
150
+
151
+ @joyent_url = options[:joyent_url] || 'https://us-sw-1.api.joyentcloud.com'
152
+ @joyent_version = options[:joyent_version] || '~7'
153
+ @joyent_username = options[:joyent_username]
154
+
155
+ unless @joyent_username
156
+ raise ArgumentError, "options[:joyent_username] required"
157
+ end
158
+
159
+ if options[:joyent_keyname]
160
+ @joyent_keyname = options[:joyent_keyname]
161
+ @joyent_keyphrase = options[:joyent_keyphrase]
162
+ @key_manager = Net::SSH::Authentication::KeyManager.new(nil, {
163
+ :keys_only => true,
164
+ :passphrase => @joyent_keyphrase
165
+ })
166
+ @header_method = method(:header_for_signature_auth)
167
+
168
+ if options[:joyent_keyfile]
169
+ if File.exist?(options[:joyent_keyfile])
170
+ @joyent_keyfile = options[:joyent_keyfile]
171
+ @key_manager.add(@joyent_keyfile)
172
+ else
173
+ raise ArgumentError, "options[:joyent_keyfile] provided does not exist."
174
+ end
175
+ elsif options[:joyent_keydata]
176
+ if options[:joyent_keydata].to_s.empty?
177
+ raise ArgumentError, 'options[:joyent_keydata] must not be blank'
178
+ else
179
+ @joyent_keydata = options[:joyent_keydata]
180
+ @key_manager.add_key_data(@joyent_keydata)
181
+ end
182
+ end
183
+ elsif options[:joyent_password]
184
+ @joyent_password = options[:joyent_password]
185
+ @header_method = method(:header_for_basic_auth)
186
+ else
187
+ raise ArgumentError, "Must provide either a joyent_password or joyent_keyname and joyent_keyfile pair"
188
+ end
189
+
190
+ @connection = Fog::XML::Connection.new(
191
+ @joyent_url,
192
+ @persistent,
193
+ @connection_options
194
+ )
195
+ end
196
+
197
+ def request(opts = {})
198
+ opts[:headers] = {
199
+ "X-Api-Version" => @joyent_version,
200
+ "Content-Type" => "application/json",
201
+ "Accept" => "application/json"
202
+ }.merge(opts[:headers] || {}).merge(@header_method.call)
203
+
204
+ if opts[:body]
205
+ opts[:body] = Fog::JSON.encode(opts[:body])
206
+ end
207
+
208
+ response = @connection.request(opts)
209
+ if response.headers["Content-Type"] == "application/json"
210
+ response.body = json_decode(response.body)
211
+ end
212
+
213
+ response
214
+ rescue Excon::Errors::HTTPStatusError => e
215
+ if e.response.headers["Content-Type"] == "application/json"
216
+ e.response.body = json_decode(e.response.body)
217
+ end
218
+ raise_if_error!(e.request, e.response)
219
+ end
220
+
221
+ private
222
+
223
+ def json_decode(body)
224
+ parsed = Fog::JSON.decode(body)
225
+ decode_time_attrs(parsed)
226
+ end
227
+
228
+ def header_for_basic_auth
229
+ {
230
+ "Authorization" => "Basic #{Base64.encode64("#{@joyent_username}:#{@joyent_password}").delete("\r\n")}"
231
+ }
232
+ end
233
+
234
+ def header_for_signature_auth
235
+ date = Time.now.utc.httpdate
236
+
237
+ # Force KeyManager to load the key(s)
238
+ @mutex.synchronize do
239
+ @key_manager.each_identity {}
240
+ end
241
+
242
+ key = @key_manager.known_identities.keys.first
243
+
244
+ sig = if key.kind_of? OpenSSL::PKey::RSA
245
+ @key_manager.sign(key, date)[15..-1]
246
+ else
247
+ key = OpenSSL::PKey::DSA.new(File.read(@joyent_keyfile), @joyent_keyphrase)
248
+ key.sign('sha1', date)
249
+ end
250
+
251
+ key_id = "/#{@joyent_username}/keys/#{@joyent_keyname}"
252
+ key_type = key.class.to_s.split('::').last.downcase.to_sym
253
+
254
+ unless [:rsa, :dsa].include? key_type
255
+ raise Joyent::Errors::Unauthorized.new('Invalid key type -- only rsa or dsa key is supported')
256
+ end
257
+
258
+ signature = Base64.encode64(sig).delete("\r\n")
259
+
260
+ {
261
+ "Date" => date,
262
+ "Authorization" => "Signature keyId=\"#{key_id}\",algorithm=\"#{key_type}-sha1\" #{signature}"
263
+ }
264
+ rescue Net::SSH::Authentication::KeyManagerError => e
265
+ raise Joyent::Errors::Unauthorized.new('SSH Signing Error: :#{e.message}', e)
266
+ end
267
+
268
+ def decode_time_attrs(obj)
269
+ if obj.kind_of?(Hash)
270
+ obj["created"] = Time.parse(obj["created"]) unless obj["created"].nil? or obj["created"] == ''
271
+ obj["updated"] = Time.parse(obj["updated"]) unless obj["updated"].nil? or obj["updated"] == ''
272
+ elsif obj.kind_of?(Array)
273
+ obj.map do |o|
274
+ decode_time_attrs(o)
275
+ end
276
+ end
277
+
278
+ obj
279
+ end
280
+
281
+ def raise_if_error!(request, response)
282
+ case response.status
283
+ when 401 then
284
+ raise Fog::Compute::Joyent::Errors::Unauthorized.new('Invalid credentials were used', request, response)
285
+ when 403 then
286
+ raise Fog::Compute::Joyent::Errors::Forbidden.new('No permissions to the specified resource', request, response)
287
+ when 404 then
288
+ raise Fog::Compute::Joyent::Errors::NotFound.new('Requested resource was not found', request, response)
289
+ when 405 then
290
+ raise Fog::Compute::Joyent::Errors::MethodNotAllowed.new('Method not supported for the given resource', request, response)
291
+ when 406 then
292
+ raise Fog::Compute::Joyent::Errors::NotAcceptable.new('Try sending a different Accept header', request, response)
293
+ when 409 then
294
+ raise Fog::Compute::Joyent::Errors::Conflict.new('Most likely invalid or missing parameters', request, response)
295
+ when 414 then
296
+ raise Fog::Compute::Joyent::Errors::RequestEntityTooLarge.new('You sent too much data', request, response)
297
+ when 415 then
298
+ raise Fog::Compute::Joyent::Errors::UnsupportedMediaType.new('You encoded your request in a format we don\'t understand', request, response)
299
+ when 420 then
300
+ raise Fog::Compute::Joyent::Errors::PolicyNotForfilled.new('You are sending too many requests', request, response)
301
+ when 449 then
302
+ raise Fog::Compute::Joyent::Errors::RetryWith.new('Invalid API Version requested; try with a different API Version', request, response)
303
+ when 503 then
304
+ raise Fog::Compute::Joyent::Errors::ServiceUnavailable.new('Either there\'s no capacity in this datacenter, or we\'re in a maintenance window', request, response)
305
+ end
306
+ end
307
+ end # Real
308
+ end
309
+ end
310
+ end