vagrant_cloud 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: dcdd9dd3b5ac00bf1add77d8a837f727c61c0041
4
- data.tar.gz: 2bd31b671ba202ca7b2cbba4a06e767949732b95
3
+ metadata.gz: 6a617453ddbb1ae2c011a4bfefc491b5d4bbb9ae
4
+ data.tar.gz: 9c29257f990145ffa7d0dcae07b4eef07d9e105b
5
5
  SHA512:
6
- metadata.gz: 72b0c2dbc26b450f46df0eb47111ea80742e4890a1ff143cb7e9ad03c724e499368c69eb5d4c4378a851261bd948801d2d928f9ccd97633a40e4c04de5ef7d8a
7
- data.tar.gz: 9b11a23883516758c7da2f4f0f851ea270030a9c4b128d62260f7503005835bbcf8b06cbf693b065197331a6d84d9713785da6a4d178e2d7e1bc0f849e1ba6a5
6
+ metadata.gz: f24a18bc5a0bbdc4d15ea65971ae3cc938f4edb248866c03fc07381b5856e4db516d687d423f9f62312b61278afc8e20ef505d8924c030516100f070d7d44d44
7
+ data.tar.gz: a431af85399f329ce7d30289d3140f7d58b53339b3db6dd3f466b3dddb889e376275eec9ab5af8d69814cced872c488d2265ea844066c0bb21c59ef8093f233d
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
- vagrant_cloud
1
+ vagrant_cloud [![Build Status](https://travis-ci.org/cargomedia/vagrant_cloud.svg)](https://travis-ci.org/cargomedia/vagrant_cloud)
2
2
  =============
3
3
 
4
- *Very* minimalistic ruby wrapper for the [Vagrant Cloud API](https://vagrantcloud.com/api).
4
+ *Very* minimalistic ruby wrapper for the [Vagrant Cloud API](https://atlas.hashicorp.com/docs).
5
5
 
6
6
  Consisting of four basic classes for your *account*, *boxes*, *versions* and *providers*.
7
7
 
@@ -5,62 +5,108 @@ module VagrantCloud
5
5
  attr_accessor :username
6
6
  attr_accessor :access_token
7
7
 
8
+ # @param [String] username
9
+ # @param [String] access_token
8
10
  def initialize(username, access_token)
9
11
  @username = username
10
12
  @access_token = access_token
11
13
  end
12
14
 
15
+ # @param [String] name
16
+ # @param [Hash]
17
+ # @return [Box]
13
18
  def get_box(name, data = nil)
14
19
  Box.new(self, name, data)
15
20
  end
16
21
 
17
- def create_box(name, description = nil, is_private = false)
18
- params = {:name => name}
19
- params[:description] = description if description
20
- params[:short_description] = description if description
21
- params[:is_private] = is_private ? 1 : 0
22
+ # @param [String] name
23
+ # @param [Hash] args
24
+ # @return [Box]
25
+ def create_box(name, *args)
26
+ params = box_params(*args)
27
+ params[:name] = name
28
+
22
29
  data = request('post', '/boxes', {:box => params})
23
30
  get_box(name, data)
24
31
  end
25
32
 
26
- def ensure_box(name, description = nil, is_private = nil)
33
+ # @param [String] name
34
+ # @param [Hash] args
35
+ # @return [Box]
36
+ def ensure_box(name, *args)
37
+ params = box_params(*args)
38
+
27
39
  begin
28
40
  box = get_box(name)
29
41
  box.data
30
42
  rescue RestClient::ResourceNotFound => e
31
- box = create_box(name, description, is_private)
43
+ box = create_box(name, params)
44
+ # If we've just created the box, we're done.
45
+ return box
32
46
  end
33
47
 
34
- updated_description = (!description.nil? && (description != box.description || description != box.description_short))
35
- updated_private = (!is_private.nil? && (is_private != box.private))
36
- if updated_description || updated_private
37
- box.update(description, is_private)
38
- end
48
+ # Select elements from params that don't match what we have in the box
49
+ # data. These are changed parameters and should be updated.
50
+ update_params = params.select { |k,v|
51
+ box.data[box.param_name(k)] != v
52
+ }
53
+
54
+ # Update the box with any params that had changed.
55
+ box.update(update_params) unless update_params.empty?
39
56
 
40
57
  box
41
58
  end
42
59
 
60
+ # @param [String] method
61
+ # @param [String] path
62
+ # @param [Hash] params
63
+ # @return [Hash]
43
64
  def request(method, path, params = {})
44
65
  params[:access_token] = access_token
45
- headers = {:access_token => access_token}
46
66
  result = RestClient::Request.execute(
47
- :method => method,
48
- :url => url_base + path,
49
- :payload => params,
50
- :headers => headers,
51
- :ssl_version => 'TLSv1'
67
+ :method => method,
68
+ :url => url_base + path,
69
+ :payload => params,
70
+ :ssl_version => 'TLSv1'
52
71
  )
53
72
  result = JSON.parse(result)
54
73
  errors = result['errors']
55
- raise "Vagrant Cloud returned error: #{errors}" if errors
74
+ raise(RuntimeError, "Vagrant Cloud returned error: #{errors}") if errors
56
75
  result
57
76
  end
58
77
 
59
78
  private
60
79
 
80
+ # @return [String]
61
81
  def url_base
62
82
  'https://vagrantcloud.com/api/v1'
63
83
  end
64
84
 
85
+ # @param [Array] args
86
+ # @return [Hash]
87
+ def box_params(*args)
88
+ # Prepares a hash based on the *args array passed in.
89
+ # Acceptable parameters are those documented by Hashicorp for the v1 API
90
+ # at https://atlas.hashicorp.com/docs
91
+
92
+ # This dance is to simulate what we could have accomplished with **args
93
+ # in Ruby 2.0+
94
+ # This will silently discard any options that are not passed in as a
95
+ # hash.
96
+ # Find and remove the first hash we find in *args. Set params to an
97
+ # empty hash if we weren't passed one.
98
+ params = args.select { |v| v.is_a?(Hash) }.first
99
+ if params.nil?
100
+ params = {}
101
+ else
102
+ args.delete_if { |v| v == params }
103
+ end
104
+
105
+ # Default boxes to public can be overridden by providing :is_private
106
+ params[:is_private] = false unless defined? params[:is_private]
107
+
108
+ params
109
+ end
110
+
65
111
  end
66
112
  end
@@ -6,50 +6,60 @@ module VagrantCloud
6
6
  attr_accessor :name
7
7
  attr_accessor :data
8
8
 
9
+ # @param [String] account
10
+ # @param [String] name
11
+ # @param [Hash] data
9
12
  def initialize(account, name, data = nil)
10
13
  @account = account
11
14
  @name = name
12
15
  @data = data
13
16
  end
14
17
 
18
+ # @return [String]
15
19
  def description
16
- data['description_markdown']
20
+ data['description_markdown'].to_s
17
21
  end
18
22
 
23
+ # @return [String]
19
24
  def description_short
20
- data['short_description']
25
+ data['short_description'].to_s
21
26
  end
22
27
 
28
+ # @return [TrueClass, FalseClass]
23
29
  def private
24
- data['private']
30
+ !!data['private']
25
31
  end
26
32
 
33
+ # @return [Array<Version>]
27
34
  def versions
28
- version_list = data['versions'].map { |data| Version.new(self, data['number'], data) }
35
+ version_list = data['versions'].map { |data| VagrantCloud::Version.new(self, data['number'], data) }
29
36
  version_list.sort_by { |version| Gem::Version.new(version.number) }
30
37
  end
31
38
 
39
+ # @return [Hash]
32
40
  def data
33
41
  @data ||= account.request('get', "/box/#{account.username}/#{name}")
34
42
  end
35
43
 
36
- def update(description, is_private)
37
- box = {
38
- :short_description => description,
39
- :description => description,
40
- :is_private => is_private,
41
- }
42
- @data = account.request('put', "/box/#{account.username}/#{name}", {:box => box})
44
+ # @param [Hash] args
45
+ def update(args = {})
46
+ @data = account.request('put', "/box/#{account.username}/#{name}", {:box => args})
43
47
  end
44
48
 
45
49
  def delete
46
50
  account.request('delete', "/box/#{account.username}/#{name}")
47
51
  end
48
52
 
53
+ # @param [Integer] number
54
+ # @param [Hash] data
55
+ # @return [Version]
49
56
  def get_version(number, data = nil)
50
- Version.new(self, number, data)
57
+ VagrantCloud::Version.new(self, number, data)
51
58
  end
52
59
 
60
+ # @param [String] name
61
+ # @param [String] description
62
+ # @return [Version]
53
63
  def create_version(name, description = nil)
54
64
  params = {:version => name}
55
65
  params[:description] = description if description
@@ -57,6 +67,9 @@ module VagrantCloud
57
67
  get_version(data['number'], data)
58
68
  end
59
69
 
70
+ # @param [String] name
71
+ # @param [String] description
72
+ # @return [Version]
60
73
  def ensure_version(name, description = nil)
61
74
  version = versions.select { |version| version.version == name }.first
62
75
  unless version
@@ -68,5 +81,21 @@ module VagrantCloud
68
81
  version
69
82
  end
70
83
 
84
+ # @param [Symbol]
85
+ # @return [String]
86
+ def param_name(param)
87
+ # This needs to return strings, otherwise it won't match the JSON that
88
+ # Vagrant Cloud returns.
89
+ ATTR_MAP.fetch(param, param.to_s)
90
+ end
91
+
92
+ private
93
+
94
+ # Vagrant Cloud returns keys different from what you set for some params.
95
+ # Values in this map should be strings.
96
+ ATTR_MAP = {
97
+ :is_private => 'private',
98
+ :description => 'description_markdown',
99
+ }
71
100
  end
72
101
  end
@@ -6,24 +6,31 @@ module VagrantCloud
6
6
  attr_accessor :name
7
7
  attr_accessor :data
8
8
 
9
+ # @param [Version] version
10
+ # @param [String] name
11
+ # @param [Hash] data
9
12
  def initialize(version, name, data = nil)
10
13
  @version = version
11
14
  @name = name
12
15
  @data = data
13
16
  end
14
17
 
18
+ # @return [String]
15
19
  def url
16
- data['original_url']
20
+ data['original_url'].to_s
17
21
  end
18
22
 
23
+ # @return [String]
19
24
  def download_url
20
- data['download_url']
25
+ data['download_url'].to_s
21
26
  end
22
27
 
28
+ # @return [Hash]
23
29
  def data
24
30
  @data ||= account.request('get', "/box/#{account.username}/#{box.name}/version/#{version.number}/provider/#{name}")
25
31
  end
26
32
 
33
+ # @param [String] url
27
34
  def update(url)
28
35
  params = {:url => url}
29
36
  @data = account.request('put', "/box/#{account.username}/#{box.name}/version/#{version.number}/provider/#{name}", {:provider => params})
@@ -35,10 +42,12 @@ module VagrantCloud
35
42
 
36
43
  private
37
44
 
45
+ # @return [Box]
38
46
  def box
39
47
  version.box
40
48
  end
41
49
 
50
+ # @return [Account]
42
51
  def account
43
52
  box.account
44
53
  end
@@ -6,36 +6,46 @@ module VagrantCloud
6
6
  attr_accessor :number
7
7
  attr_accessor :data
8
8
 
9
+ # @param [Box] box
10
+ # @param [String] number
11
+ # @param [Hash] data
9
12
  def initialize(box, number, data = nil)
10
13
  @box = box
11
14
  @number = number
12
15
  @data = data
13
16
  end
14
17
 
18
+ # @return [String]
15
19
  def version
16
- data['version']
20
+ data['version'].to_s
17
21
  end
18
22
 
23
+ # @return [String]
19
24
  def description
20
- data['description_markdown']
25
+ data['description_markdown'].to_s
21
26
  end
22
27
 
28
+ # @return [String]
23
29
  def status
24
- data['status']
30
+ data['status'].to_s
25
31
  end
26
32
 
33
+ # @return [Array<Provider>]
27
34
  def providers
28
35
  data['providers'].map { |data| Provider.new(self, data['name'], data) }
29
36
  end
30
37
 
38
+ # @return [String]
31
39
  def to_s
32
40
  version
33
41
  end
34
42
 
43
+ # @return [Hash]
35
44
  def data
36
45
  @data ||= account.request('get', "/box/#{account.username}/#{box.name}/version/#{number}")
37
46
  end
38
47
 
48
+ # @param [String] description
39
49
  def update(description)
40
50
  version = {:description => description}
41
51
  @data = account.request('put', "/box/#{account.username}/#{box.name}/version/#{number}", {:version => version})
@@ -53,16 +63,25 @@ module VagrantCloud
53
63
  @data = account.request('put', "/box/#{account.username}/#{box.name}/version/#{number}/revoke")
54
64
  end
55
65
 
66
+ # @param [String] name
67
+ # @param [Hash] data
68
+ # @return [Provider]
56
69
  def get_provider(name, data = nil)
57
70
  Provider.new(self, name, data)
58
71
  end
59
72
 
73
+ # @param [String] name
74
+ # @param [String] url
75
+ # @return [Provider]
60
76
  def create_provider(name, url)
61
77
  params = {:name => name, :url => url}
62
78
  data = account.request('post', "/box/#{account.username}/#{box.name}/version/#{self.number}/providers", {:provider => params})
63
79
  get_provider(name, data)
64
80
  end
65
81
 
82
+ # @param [String] name
83
+ # @param [String] url
84
+ # @return [Provider]
66
85
  def ensure_provider(name, url)
67
86
  provider = providers.select{ |provider| provider.name == name }.first
68
87
  unless provider
@@ -76,6 +95,7 @@ module VagrantCloud
76
95
 
77
96
  private
78
97
 
98
+ # @return [Account]
79
99
  def account
80
100
  box.account
81
101
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vagrant_cloud
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cargo Media
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-06-30 00:00:00.000000000 Z
11
+ date: 2015-07-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rest-client
@@ -38,6 +38,34 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '10.4'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: webmock
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.21'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.21'
41
69
  description: Very minimalistic ruby wrapper for the Vagrant Cloud API
42
70
  email: hello@cargomedia.ch
43
71
  executables: []
@@ -71,7 +99,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
71
99
  version: '0'
72
100
  requirements: []
73
101
  rubyforge_project:
74
- rubygems_version: 2.4.6
102
+ rubygems_version: 2.4.5
75
103
  signing_key:
76
104
  specification_version: 4
77
105
  summary: Vagrant Cloud API wrapper