vagrant_cloud 1.1.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE +1 -0
- data/README.md +12 -3
- data/lib/vagrant_cloud/account.rb +86 -5
- data/lib/vagrant_cloud/box.rb +106 -18
- data/lib/vagrant_cloud/client.rb +60 -0
- data/lib/vagrant_cloud/errors.rb +25 -0
- data/lib/vagrant_cloud/provider.rb +88 -13
- data/lib/vagrant_cloud/search.rb +31 -0
- data/lib/vagrant_cloud/version.rb +106 -15
- data/lib/vagrant_cloud.rb +10 -9
- metadata +29 -24
- data/lib/vagrant_cloud/cli.rb +0 -65
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e324926c5c32c84fd0c0506035b0dadc552e4f6c
|
4
|
+
data.tar.gz: 493f832cc1c117c5634dd674ec0a95100003b552
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 435e8819762b21c375438e577a2a315afc356936212eb7a87e80eb6eb8c019827b0dcbf7044f4fd1f5ceff42c3245b5947ed5c8a74146db4b913d9f195d6fbfc
|
7
|
+
data.tar.gz: 76e35098ecf224f7df999d235be1f0687e062916c163efd87b9264d473bf95b950e54704ca8e3072988e1c2282dc7f6ce9c8279305095c78a911376e080e3b3f
|
data/LICENSE
CHANGED
data/README.md
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
vagrant_cloud
|
2
2
|
=============
|
3
|
-
|
3
|
+
Ruby client for the [Vagrant Cloud API](https://www.vagrantup.com/docs/vagrant-cloud/api.html).
|
4
4
|
|
5
|
-
[![Build Status](https://img.shields.io/travis/
|
5
|
+
[![Build Status](https://img.shields.io/travis/hashicorp/vagrant_cloud/master.svg)](https://travis-ci.org/hashicorp/vagrant_cloud)
|
6
6
|
[![Gem Version](https://img.shields.io/gem/v/vagrant_cloud.svg)](https://rubygems.org/gems/vagrant_cloud)
|
7
7
|
|
8
8
|
|
@@ -22,8 +22,12 @@ version.release
|
|
22
22
|
puts provider.download_url
|
23
23
|
```
|
24
24
|
|
25
|
+
__NOTE:__ As of version 2.0.0, the CLI has been deprecated in favor of the `vagrant cloud`
|
26
|
+
command. More information about how to use the `vagrant cloud` command can be found
|
27
|
+
on the [Vagrant documentation](https://www.vagrantup.com/docs/cli/cloud.html).
|
28
|
+
|
25
29
|
Example CLI usage:
|
26
|
-
Create a version and provider within an existing Box, upload a file to be hosted by Vagrant
|
30
|
+
Create a version and provider within an existing Box, upload a file to be hosted by Vagrant Cloud, and release the version
|
27
31
|
```sh
|
28
32
|
vagrant_cloud create_version --username $USERNAME --token $VAGRANT_CLOUD_TOKEN --box $BOX_NAME --version $BOX_VERSION
|
29
33
|
vagrant_cloud create_provider --username $USERNAME --token $VAGRANT_CLOUD_TOKEN --box $BOX_NAME --version $BOX_VERSION
|
@@ -56,3 +60,8 @@ Release a new version:
|
|
56
60
|
1. Bump the version in `vagrant_cloud.gemspec`, merge to master.
|
57
61
|
2. Push a new tag to master.
|
58
62
|
3. Release to RubyGems with `bundle exec rake release`.
|
63
|
+
|
64
|
+
History
|
65
|
+
-------
|
66
|
+
This gem has been developed and maintained by [Cargo Media](https://www.cargomedia.ch) since April 2014.
|
67
|
+
HashiCorp became the official maintainer in October 2017.
|
@@ -5,13 +5,83 @@ module VagrantCloud
|
|
5
5
|
|
6
6
|
# @param [String] username
|
7
7
|
# @param [String] access_token
|
8
|
-
def initialize(username, access_token)
|
8
|
+
def initialize(username, access_token, custom_server = nil)
|
9
9
|
@username = username
|
10
10
|
@access_token = access_token
|
11
|
+
@client = Client.new(access_token, custom_server)
|
11
12
|
end
|
12
13
|
|
14
|
+
#---------------------------
|
15
|
+
# Authentication API Helpers
|
16
|
+
#---------------------------
|
17
|
+
|
18
|
+
# @param [String] password
|
19
|
+
# @param [String] description
|
20
|
+
# @param [String] 2FA code
|
21
|
+
# @return [Hash] response body
|
22
|
+
def create_token(password, description = nil, code = nil)
|
23
|
+
token_data_params = {
|
24
|
+
token: { description: description },
|
25
|
+
user: { login: @username, password: password },
|
26
|
+
two_factor: code
|
27
|
+
}.delete_if { |_, v| v.nil? }
|
28
|
+
|
29
|
+
token_response = @client.request('post', '/authenticate', token_data_params)
|
30
|
+
token_response
|
31
|
+
end
|
32
|
+
|
33
|
+
# @param [String] token
|
34
|
+
def delete_token(access_token = nil)
|
35
|
+
token_response = @client.request('delete', '/authenticate', nil, access_token)
|
36
|
+
token_response
|
37
|
+
end
|
38
|
+
|
39
|
+
# Validates a token on the account or a one-off validation token request.
|
40
|
+
# Will return nil if token is valid, otherwise will return Hash of response
|
41
|
+
# from Vagrant Cloud
|
42
|
+
#
|
43
|
+
# @param [String] access_token
|
44
|
+
# @return [Hash] response body
|
45
|
+
def validate_token(access_token = nil)
|
46
|
+
token_response = @client.request('get', '/authenticate', nil, access_token)
|
47
|
+
token_response
|
48
|
+
end
|
49
|
+
|
50
|
+
# @param [String] delivery_method
|
51
|
+
# @param [String] password
|
52
|
+
# @return [Hash] response body
|
53
|
+
def request_2fa_code(delivery_method, password)
|
54
|
+
twofa_code_params = {
|
55
|
+
two_factor: { delivery_method: delivery_method },
|
56
|
+
user: { login: @username, password: password }
|
57
|
+
}
|
58
|
+
|
59
|
+
code_response = @client.request('post', '/two-factor/request-code', twofa_code_params)
|
60
|
+
code_response
|
61
|
+
end
|
62
|
+
|
63
|
+
#---------------------------
|
64
|
+
# Organization API Helpers
|
65
|
+
#---------------------------
|
66
|
+
|
67
|
+
# @param [String] - organization
|
68
|
+
# @return [Hash]
|
69
|
+
def read_organization(org = nil)
|
70
|
+
if org
|
71
|
+
name = org
|
72
|
+
else
|
73
|
+
name = @username
|
74
|
+
end
|
75
|
+
|
76
|
+
@client.request('get', "/user/#{name}")
|
77
|
+
end
|
78
|
+
|
79
|
+
#--------------------
|
80
|
+
# Old Box API Helpers
|
81
|
+
#--------------------
|
82
|
+
|
13
83
|
# @param [String] name
|
14
|
-
# @param [Hash]
|
84
|
+
# @param [Hash] data
|
15
85
|
# @return [Box]
|
16
86
|
def get_box(name, data = nil)
|
17
87
|
Box.new(self, name, data)
|
@@ -24,7 +94,7 @@ module VagrantCloud
|
|
24
94
|
params = box_params(*args)
|
25
95
|
params[:name] = name
|
26
96
|
|
27
|
-
data = request('post', '/boxes', box: params)
|
97
|
+
data = @client.request('post', '/boxes', box: params)
|
28
98
|
get_box(name, data)
|
29
99
|
end
|
30
100
|
|
@@ -37,7 +107,7 @@ module VagrantCloud
|
|
37
107
|
begin
|
38
108
|
box = get_box(name)
|
39
109
|
box.data
|
40
|
-
rescue RestClient::ResourceNotFound
|
110
|
+
rescue RestClient::ResourceNotFound
|
41
111
|
box = create_box(name, params)
|
42
112
|
# If we've just created the box, we're done.
|
43
113
|
return box
|
@@ -55,21 +125,32 @@ module VagrantCloud
|
|
55
125
|
box
|
56
126
|
end
|
57
127
|
|
128
|
+
#--------------------
|
129
|
+
# Old Box API Helpers
|
130
|
+
#--------------------
|
131
|
+
|
132
|
+
# REMOVED IN FAVOR OF CLIENT CLASS, but still exists to support any old clients
|
133
|
+
#
|
58
134
|
# @param [String] method
|
59
135
|
# @param [String] path
|
60
136
|
# @param [Hash] params
|
61
137
|
# @return [Hash]
|
62
138
|
def request(method, path, params = {})
|
63
|
-
|
139
|
+
headers = {}
|
140
|
+
|
141
|
+
headers['Authorization'] = "Bearer #{access_token}" if access_token
|
142
|
+
|
64
143
|
result = RestClient::Request.execute(
|
65
144
|
method: method,
|
66
145
|
url: url_base + path,
|
67
146
|
payload: params,
|
147
|
+
headers: headers,
|
68
148
|
ssl_version: 'TLSv1'
|
69
149
|
)
|
70
150
|
result = JSON.parse(result)
|
71
151
|
errors = result['errors']
|
72
152
|
raise "Vagrant Cloud returned error: #{errors}" if errors
|
153
|
+
|
73
154
|
result
|
74
155
|
end
|
75
156
|
|
data/lib/vagrant_cloud/box.rb
CHANGED
@@ -2,17 +2,100 @@ module VagrantCloud
|
|
2
2
|
class Box
|
3
3
|
attr_accessor :account
|
4
4
|
attr_accessor :name
|
5
|
-
attr_accessor :data
|
6
5
|
|
7
6
|
# @param [String] account
|
8
7
|
# @param [String] name
|
9
8
|
# @param [Hash] data
|
10
|
-
|
9
|
+
# @param [String] description
|
10
|
+
# @param [String] short_description
|
11
|
+
# @param [String] access_token
|
12
|
+
def initialize(account, name = nil, data = nil, short_description = nil, description = nil, access_token = nil, custom_server = nil)
|
11
13
|
@account = account
|
12
14
|
@name = name
|
13
15
|
@data = data
|
16
|
+
@description = description
|
17
|
+
@short_description = short_description
|
18
|
+
@client = Client.new(access_token, custom_server)
|
19
|
+
end
|
20
|
+
|
21
|
+
#--------------------
|
22
|
+
# Box API Helpers
|
23
|
+
#--------------------
|
24
|
+
|
25
|
+
# Read this box
|
26
|
+
# @return [Hash]
|
27
|
+
def data
|
28
|
+
@data ||= @client.request('get', box_path)
|
29
|
+
end
|
30
|
+
|
31
|
+
# Update a box
|
32
|
+
#
|
33
|
+
# @param [Hash] args
|
34
|
+
# @param [String] org - organization of the box to read
|
35
|
+
# @param [String] box_name - name of the box to read
|
36
|
+
# @return [Hash]
|
37
|
+
def update(args = {})
|
38
|
+
# hash arguments kept for backwards compatibility
|
39
|
+
data = @client.request('put', box_path(args[:organization], args[:name]), box: args)
|
40
|
+
|
41
|
+
# Update was called on *this* object, so update
|
42
|
+
# objects data locally
|
43
|
+
@data = data if !args[:organization] && !args[:name]
|
44
|
+
data
|
45
|
+
end
|
46
|
+
|
47
|
+
# A generic function to read any box on Vagrant Cloud
|
48
|
+
# If org and box name is not supplied, it will default to
|
49
|
+
# reading the given Box object
|
50
|
+
#
|
51
|
+
# @param [String] org - organization of the box to read
|
52
|
+
# @param [String] box_name - name of the box to read
|
53
|
+
# @return [Hash]
|
54
|
+
def delete(org = nil, box_name = nil)
|
55
|
+
@client.request('delete', box_path(org, box_name))
|
14
56
|
end
|
15
57
|
|
58
|
+
# A generic function to read any box on Vagrant Cloud
|
59
|
+
#
|
60
|
+
# @param [String] org - organization of the box to read
|
61
|
+
# @param [String] box_name - name of the box to read
|
62
|
+
# @return [Hash]
|
63
|
+
def read(org = nil, box_name = nil)
|
64
|
+
@client.request('get', box_path(org, box_name))
|
65
|
+
end
|
66
|
+
|
67
|
+
# @param [String] short_description
|
68
|
+
# @param [String] description
|
69
|
+
# @param [Bool] is_private
|
70
|
+
# @return [Hash]
|
71
|
+
def create(short_description = nil, description = nil, org = nil, box_name = nil, is_private = false)
|
72
|
+
update_data = !(org && box_name)
|
73
|
+
|
74
|
+
org ||= account.username
|
75
|
+
box_name ||= @name
|
76
|
+
short_description ||= @short_description
|
77
|
+
description ||= @description
|
78
|
+
|
79
|
+
params = {
|
80
|
+
name: box_name,
|
81
|
+
username: org,
|
82
|
+
is_private: is_private,
|
83
|
+
short_description: short_description,
|
84
|
+
description: description
|
85
|
+
}.delete_if { |_, v| v.nil? }
|
86
|
+
|
87
|
+
data = @client.request('post', '/boxes', box: params)
|
88
|
+
|
89
|
+
# Create was called on *this* object, so update
|
90
|
+
# objects data locally
|
91
|
+
@data = data if update_data
|
92
|
+
data
|
93
|
+
end
|
94
|
+
|
95
|
+
#--------------------
|
96
|
+
# Metadata Helpers
|
97
|
+
#--------------------
|
98
|
+
|
16
99
|
# @return [String]
|
17
100
|
def description
|
18
101
|
data['description_markdown'].to_s
|
@@ -34,19 +117,9 @@ module VagrantCloud
|
|
34
117
|
version_list.sort_by { |version| Gem::Version.new(version.number) }
|
35
118
|
end
|
36
119
|
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
end
|
41
|
-
|
42
|
-
# @param [Hash] args
|
43
|
-
def update(args = {})
|
44
|
-
@data = account.request('put', "/box/#{account.username}/#{name}", box: args)
|
45
|
-
end
|
46
|
-
|
47
|
-
def delete
|
48
|
-
account.request('delete', "/box/#{account.username}/#{name}")
|
49
|
-
end
|
120
|
+
#------------------------
|
121
|
+
# Old Version API Helpers
|
122
|
+
#------------------------
|
50
123
|
|
51
124
|
# @param [Integer] number
|
52
125
|
# @param [Hash] data
|
@@ -61,7 +134,7 @@ module VagrantCloud
|
|
61
134
|
def create_version(name, description = nil)
|
62
135
|
params = { version: name }
|
63
136
|
params[:description] = description if description
|
64
|
-
data =
|
137
|
+
data = @client.request('post', "#{box_path}/versions", version: params)
|
65
138
|
get_version(data['number'], data)
|
66
139
|
end
|
67
140
|
|
@@ -69,8 +142,8 @@ module VagrantCloud
|
|
69
142
|
# @param [String] description
|
70
143
|
# @return [Version]
|
71
144
|
def ensure_version(name, description = nil)
|
72
|
-
version = versions.select { |
|
73
|
-
version
|
145
|
+
version = versions.select { |v| v.version == name }.first
|
146
|
+
version ||= create_version(name, description)
|
74
147
|
if description && (description != version.description)
|
75
148
|
version.update(description)
|
76
149
|
end
|
@@ -87,6 +160,21 @@ module VagrantCloud
|
|
87
160
|
|
88
161
|
private
|
89
162
|
|
163
|
+
# Constructs the box path based on an account and box name.
|
164
|
+
# If no params are given, it constructs a path for *this* Box object,
|
165
|
+
# but if both params are given it will construct a path for a one-off request
|
166
|
+
#
|
167
|
+
# @param [String] - username
|
168
|
+
# @param [String] - box_name
|
169
|
+
# @return [String] - API path to box
|
170
|
+
def box_path(username = nil, box_name = nil)
|
171
|
+
if username && box_name
|
172
|
+
"/box/#{username}/#{box_name}"
|
173
|
+
else
|
174
|
+
"/box/#{account.username}/#{name}"
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|
90
178
|
# Vagrant Cloud returns keys different from what you set for some params.
|
91
179
|
# Values in this map should be strings.
|
92
180
|
ATTR_MAP = {
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
module VagrantCloud
|
4
|
+
class Client
|
5
|
+
# Base Vagrant Cloud API URL
|
6
|
+
URL_BASE = 'https://vagrantcloud.com/api/v1'.freeze
|
7
|
+
attr_accessor :access_token
|
8
|
+
|
9
|
+
# @param [String] access_token - token used to authenticate API requests
|
10
|
+
# @param [String] url_base - URL used to make API requests
|
11
|
+
def initialize(access_token = nil, url_base = nil)
|
12
|
+
if url_base
|
13
|
+
@url_base = url_base
|
14
|
+
else
|
15
|
+
@url_base = URL_BASE
|
16
|
+
end
|
17
|
+
|
18
|
+
@access_token = access_token
|
19
|
+
end
|
20
|
+
|
21
|
+
# @param [String] method
|
22
|
+
# @param [String] path
|
23
|
+
# @param [Hash] params
|
24
|
+
# @param [String] token
|
25
|
+
# @return [Hash]
|
26
|
+
def request(method, path, params = {}, token = nil)
|
27
|
+
headers = {}
|
28
|
+
|
29
|
+
if token
|
30
|
+
headers['Authorization'] = "Bearer #{token}"
|
31
|
+
elsif @access_token
|
32
|
+
headers['Authorization'] = "Bearer #{@access_token}"
|
33
|
+
end
|
34
|
+
|
35
|
+
headers['Accept'] = 'application/json'
|
36
|
+
|
37
|
+
request_params = {
|
38
|
+
method: method,
|
39
|
+
url: @url_base + path,
|
40
|
+
headers: headers,
|
41
|
+
ssl_version: 'TLSv1'
|
42
|
+
}
|
43
|
+
|
44
|
+
if ['get', 'head', 'delete'].include?(method.downcase)
|
45
|
+
headers[:params] = params
|
46
|
+
else
|
47
|
+
request_params[:payload] = params
|
48
|
+
end
|
49
|
+
|
50
|
+
begin
|
51
|
+
result = RestClient::Request.execute(request_params)
|
52
|
+
|
53
|
+
parsed_result = JSON.parse(result)
|
54
|
+
parsed_result
|
55
|
+
rescue RestClient::ExceptionWithResponse => e
|
56
|
+
raise ClientError.new(e.message, e.http_body, e.http_code)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module VagrantCloud
|
2
|
+
class ClientError < StandardError
|
3
|
+
attr_accessor :error_arr
|
4
|
+
attr_accessor :error_code
|
5
|
+
|
6
|
+
def initialize(msg, http_body, http_code)
|
7
|
+
begin
|
8
|
+
errors = JSON.parse(http_body)
|
9
|
+
vagrant_cloud_msg = errors['errors']
|
10
|
+
|
11
|
+
if vagrant_cloud_msg.is_a?(Array)
|
12
|
+
message = msg + ' - ' + vagrant_cloud_msg.join(', ').to_s
|
13
|
+
else
|
14
|
+
message = msg + ' - ' + vagrant_cloud_msg
|
15
|
+
end
|
16
|
+
rescue JSON::ParserError
|
17
|
+
message = msg
|
18
|
+
end
|
19
|
+
|
20
|
+
@error_arr = vagrant_cloud_msg
|
21
|
+
@error_code = http_code
|
22
|
+
super(message)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -2,17 +2,25 @@ module VagrantCloud
|
|
2
2
|
class Provider
|
3
3
|
attr_accessor :version
|
4
4
|
attr_accessor :name
|
5
|
-
attr_accessor :data
|
6
5
|
|
7
6
|
# @param [Version] version
|
8
7
|
# @param [String] name
|
9
8
|
# @param [Hash] data
|
10
|
-
|
9
|
+
# @param [String] access_token
|
10
|
+
def initialize(version, name, data = nil, url = nil, username = nil, box_name = nil, access_token = nil, custom_server = nil)
|
11
11
|
@version = version
|
12
12
|
@name = name
|
13
13
|
@data = data
|
14
|
+
@username = username
|
15
|
+
@box_name = box_name
|
16
|
+
@url = url
|
17
|
+
@client = Client.new(access_token, custom_server)
|
14
18
|
end
|
15
19
|
|
20
|
+
#--------------------
|
21
|
+
# Metadata Helpers
|
22
|
+
#--------------------
|
23
|
+
|
16
24
|
# @return [String]
|
17
25
|
def url
|
18
26
|
data['original_url'].to_s
|
@@ -23,29 +31,84 @@ module VagrantCloud
|
|
23
31
|
data['download_url'].to_s
|
24
32
|
end
|
25
33
|
|
34
|
+
#--------------------
|
35
|
+
# Provider API Helpers
|
36
|
+
#--------------------
|
37
|
+
|
26
38
|
# @return [Hash]
|
27
39
|
def data
|
28
|
-
@data ||=
|
40
|
+
@data ||= @client.request('get', provider_path)
|
41
|
+
end
|
42
|
+
|
43
|
+
# Creates a provider for *this* Provider object, or if all params are given,
|
44
|
+
# will make a one-off request to create a Provider and return its data
|
45
|
+
#
|
46
|
+
# @param [String] name
|
47
|
+
# @param [String] url
|
48
|
+
# @param [String] username
|
49
|
+
# @param [String] box_name
|
50
|
+
# @param [String] version_number
|
51
|
+
def create_provider(name = nil, url = nil, username = nil, box_name = nil, version_number = nil)
|
52
|
+
update_data = !(username && version_number && provider_name && box_name)
|
53
|
+
name ||= @name
|
54
|
+
url ||= @url
|
55
|
+
username ||= @username
|
56
|
+
box_name ||= @box_name
|
57
|
+
version_number ||= @version
|
58
|
+
|
59
|
+
params = { name: name, url: url }.delete_if { |_, v| v.nil? }
|
60
|
+
data = @client.request('post', create_provider_path(username, box_name, version_number), provider: params)
|
61
|
+
|
62
|
+
@data = data if update_data
|
63
|
+
data
|
29
64
|
end
|
30
65
|
|
31
66
|
# @param [String] url
|
32
|
-
|
67
|
+
# @param [String] username
|
68
|
+
# @param [String] box_name
|
69
|
+
# @param [String] version_number
|
70
|
+
# @param [String] provider_name
|
71
|
+
def update(url = nil, username = nil, box_name = nil, version_number = nil, provider_name = nil)
|
72
|
+
update_data = !(username && version_number && provider_name && box_name)
|
73
|
+
provider_name ||= @name
|
74
|
+
url ||= @url
|
75
|
+
username ||= @username
|
76
|
+
box_name ||= @box_name
|
77
|
+
version_number ||= @version
|
78
|
+
|
33
79
|
params = { url: url }
|
34
|
-
|
80
|
+
data = @client.request('put',
|
81
|
+
provider_path(username, box_name, version_number, provider_name),
|
82
|
+
provider: params)
|
83
|
+
|
84
|
+
@data = data if update_data
|
85
|
+
data
|
35
86
|
end
|
36
87
|
|
37
|
-
|
38
|
-
|
88
|
+
# @param [String] username
|
89
|
+
# @param [String] box_name
|
90
|
+
# @param [String] version_number
|
91
|
+
# @param [String] provider_name
|
92
|
+
def delete(username = nil, box_name = nil, version_number = nil, provider_name = nil)
|
93
|
+
@client.request('delete', provider_path(username, box_name, version_number, provider_name))
|
39
94
|
end
|
40
95
|
|
96
|
+
# @param [String] username
|
97
|
+
# @param [String] box_name
|
98
|
+
# @param [String] version_number
|
99
|
+
# @param [String] provider_name
|
41
100
|
# @return [String]
|
42
|
-
def upload_url
|
43
|
-
|
101
|
+
def upload_url(username = nil, box_name = nil, version_number = nil, provider_name = nil)
|
102
|
+
@client.request('get', "#{provider_path(username, box_name, version_number, provider_name)}/upload")['upload_path']
|
44
103
|
end
|
45
104
|
|
105
|
+
# @param [String] username
|
106
|
+
# @param [String] box_name
|
107
|
+
# @param [String] version_number
|
108
|
+
# @param [String] provider_name
|
46
109
|
# @param [String] file_path
|
47
|
-
def upload_file(file_path)
|
48
|
-
url = upload_url
|
110
|
+
def upload_file(file_path, username = nil, box_name = nil, version_number = nil, provider_name = nil)
|
111
|
+
url = upload_url(username, box_name, version_number, provider_name)
|
49
112
|
payload = File.open(file_path, 'r')
|
50
113
|
RestClient::Request.execute(
|
51
114
|
method: :put,
|
@@ -67,8 +130,20 @@ module VagrantCloud
|
|
67
130
|
box.account
|
68
131
|
end
|
69
132
|
|
70
|
-
def
|
71
|
-
|
133
|
+
def create_provider_path(username = nil, box_name = nil, version_number = nil)
|
134
|
+
if username && box_name && version_number
|
135
|
+
"/box/#{username}/#{box_name}/version/#{version_number}/providers"
|
136
|
+
else
|
137
|
+
"/box/#{account.username}/#{box.name}/version/#{version.number}/providers"
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
def provider_path(username = nil, box_name = nil, version_number = nil, provider_name = nil)
|
142
|
+
if username && box_name && version_number && name
|
143
|
+
"/box/#{username}/#{box_name}/version/#{version_number}/provider/#{provider_name}"
|
144
|
+
else
|
145
|
+
"/box/#{account.username}/#{box.name}/version/#{version.number}/provider/#{name}"
|
146
|
+
end
|
72
147
|
end
|
73
148
|
end
|
74
149
|
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module VagrantCloud
|
2
|
+
class Search
|
3
|
+
attr_accessor :account
|
4
|
+
|
5
|
+
def initialize(access_token = nil, custom_server = nil)
|
6
|
+
@client = Client.new(access_token, custom_server)
|
7
|
+
end
|
8
|
+
|
9
|
+
# Requests a search based on the given parameters
|
10
|
+
#
|
11
|
+
# @param [String] query
|
12
|
+
# @param [String] provider
|
13
|
+
# @param [String] sort
|
14
|
+
# @param [String] order
|
15
|
+
# @param [String] limit
|
16
|
+
# @param [String] page
|
17
|
+
# @return [Hash]
|
18
|
+
def search(query = nil, provider = nil, sort = nil, order = nil, limit = nil, page = nil)
|
19
|
+
params = {
|
20
|
+
q: query,
|
21
|
+
provider: provider,
|
22
|
+
sort: sort,
|
23
|
+
order: order,
|
24
|
+
limit: limit,
|
25
|
+
page: page
|
26
|
+
}.delete_if { |_, v| v.nil? }
|
27
|
+
|
28
|
+
@client.request('get', '/search', params)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -2,17 +2,24 @@ module VagrantCloud
|
|
2
2
|
class Version
|
3
3
|
attr_accessor :box
|
4
4
|
attr_accessor :number
|
5
|
-
attr_accessor :data
|
6
5
|
|
7
6
|
# @param [Box] box
|
8
7
|
# @param [String] number
|
9
8
|
# @param [Hash] data
|
10
|
-
|
9
|
+
# @param [String] description
|
10
|
+
# @param [String] access_token
|
11
|
+
def initialize(box, number, data = nil, description = nil, access_token = nil, custom_server = nil)
|
11
12
|
@box = box
|
12
13
|
@number = number
|
13
14
|
@data = data
|
15
|
+
@description = description
|
16
|
+
@client = Client.new(access_token, custom_server)
|
14
17
|
end
|
15
18
|
|
19
|
+
#--------------------
|
20
|
+
# Metadata Helpers
|
21
|
+
#--------------------
|
22
|
+
|
16
23
|
# @return [String]
|
17
24
|
def version
|
18
25
|
data['version'].to_s
|
@@ -38,29 +45,88 @@ module VagrantCloud
|
|
38
45
|
version
|
39
46
|
end
|
40
47
|
|
48
|
+
#--------------------
|
49
|
+
# Version API Helpers
|
50
|
+
#--------------------
|
51
|
+
|
41
52
|
# @return [Hash]
|
42
53
|
def data
|
43
|
-
@data ||=
|
54
|
+
@data ||= @client.request('get', version_path)
|
55
|
+
end
|
56
|
+
|
57
|
+
# @param [String] username
|
58
|
+
# @param [String] box_name
|
59
|
+
# @param [String] version_number
|
60
|
+
# @return [Hash]
|
61
|
+
def read(username = nil, box_name = nil, version_number = nil)
|
62
|
+
@client.request('get', version_path(username, box_name, version_number))
|
44
63
|
end
|
45
64
|
|
65
|
+
# @param [String] username
|
66
|
+
# @param [String] box_name
|
67
|
+
# @param [String] version_number
|
46
68
|
# @param [String] description
|
47
|
-
def update(description)
|
69
|
+
def update(description = nil, username = nil, box_name = nil, version_number = nil)
|
70
|
+
update_data = !(username && box_name && version_number)
|
71
|
+
description ||= @description
|
48
72
|
version = { description: description }
|
49
|
-
|
73
|
+
data = @client.request('put',
|
74
|
+
version_path(username, box_name, version_number),
|
75
|
+
version: version)
|
76
|
+
|
77
|
+
@data = data if update_data
|
78
|
+
data
|
50
79
|
end
|
51
80
|
|
52
|
-
|
53
|
-
|
81
|
+
# @param [String] username
|
82
|
+
# @param [String] box_name
|
83
|
+
# @param [String] version_number
|
84
|
+
def delete(username = nil, box_name = nil, version_number = nil)
|
85
|
+
@client.request('delete', version_path(username, box_name, version_number))
|
54
86
|
end
|
55
87
|
|
56
|
-
|
57
|
-
|
88
|
+
# @param [String] username
|
89
|
+
# @param [String] box_name
|
90
|
+
# @param [String] version_number
|
91
|
+
def release(username = nil, box_name = nil, version_number = nil)
|
92
|
+
data = @client.request('put', "#{version_path(username, box_name, version_number)}/release")
|
93
|
+
|
94
|
+
@data = data if !(username && box_name && version_number)
|
95
|
+
data
|
58
96
|
end
|
59
97
|
|
60
|
-
|
61
|
-
|
98
|
+
# @param [String] username
|
99
|
+
# @param [String] box_name
|
100
|
+
# @param [String] version_number
|
101
|
+
def revoke(username = nil, box_name = nil, version_number = nil)
|
102
|
+
update_data = !(username && box_name && version_number)
|
103
|
+
data = @client.request('put', "#{version_path(username, box_name, version_number)}/revoke")
|
104
|
+
|
105
|
+
@data = data if update_data
|
106
|
+
data
|
62
107
|
end
|
63
108
|
|
109
|
+
# @param [String] number
|
110
|
+
# @param [String] description
|
111
|
+
# @param [String] org
|
112
|
+
# @param [String] box_name
|
113
|
+
# @return [Hash]
|
114
|
+
def create_version(number = nil, description = nil, org = nil, box_name = nil)
|
115
|
+
update_data = !(org && box_name && description && number)
|
116
|
+
number ||= @number
|
117
|
+
description ||= @description
|
118
|
+
|
119
|
+
params = { version: number, description: description }
|
120
|
+
data = @client.request('post', create_version_path(org, box_name).to_s, version: params)
|
121
|
+
|
122
|
+
@data = data if update_data
|
123
|
+
data
|
124
|
+
end
|
125
|
+
|
126
|
+
#--------------------
|
127
|
+
# Old Provider Helpers
|
128
|
+
#--------------------
|
129
|
+
|
64
130
|
# @param [String] name
|
65
131
|
# @param [Hash] data
|
66
132
|
# @return [Provider]
|
@@ -72,8 +138,8 @@ module VagrantCloud
|
|
72
138
|
# @param [String] url
|
73
139
|
# @return [Provider]
|
74
140
|
def create_provider(name, url = nil)
|
75
|
-
params = { name: name, url: url }.delete_if { |
|
76
|
-
data =
|
141
|
+
params = { name: name, url: url }.delete_if { |_, v| v.nil? }
|
142
|
+
data = @client.request('post', "#{version_path}/providers", provider: params)
|
77
143
|
get_provider(name, data)
|
78
144
|
end
|
79
145
|
|
@@ -81,8 +147,8 @@ module VagrantCloud
|
|
81
147
|
# @param [String] url
|
82
148
|
# @return [Provider]
|
83
149
|
def ensure_provider(name, url)
|
84
|
-
provider = providers.select { |
|
85
|
-
provider
|
150
|
+
provider = providers.select { |p| p.name == name }.first
|
151
|
+
provider ||= create_provider(name, url)
|
86
152
|
provider.update(url) if url != provider.url
|
87
153
|
provider
|
88
154
|
end
|
@@ -93,5 +159,30 @@ module VagrantCloud
|
|
93
159
|
def account
|
94
160
|
box.account
|
95
161
|
end
|
162
|
+
|
163
|
+
# Path used to generate a new version on a box
|
164
|
+
#
|
165
|
+
# @param [String] - username
|
166
|
+
# @param [String] - box_name
|
167
|
+
# @return [String]
|
168
|
+
def create_version_path(username = nil, box_name = nil)
|
169
|
+
if username && box_name
|
170
|
+
"/box/#{username}/#{box_name}/versions"
|
171
|
+
else
|
172
|
+
"/box/#{account.username}/#{box.name}/versions"
|
173
|
+
end
|
174
|
+
end
|
175
|
+
|
176
|
+
# @param [String] - username
|
177
|
+
# @param [String] - box_name
|
178
|
+
# @param [String] - version_number
|
179
|
+
# @return [String]
|
180
|
+
def version_path(username = nil, box_name = nil, version_number = nil)
|
181
|
+
if username && box_name && version_number
|
182
|
+
"/box/#{username}/#{box_name}/version/#{version_number}"
|
183
|
+
else
|
184
|
+
"/box/#{account.username}/#{box.name}/version/#{number}"
|
185
|
+
end
|
186
|
+
end
|
96
187
|
end
|
97
188
|
end
|
data/lib/vagrant_cloud.rb
CHANGED
@@ -1,10 +1,11 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require 'rest_client'
|
1
|
+
require 'json'
|
2
|
+
require 'rest_client'
|
4
3
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
4
|
+
require 'vagrant_cloud/errors'
|
5
|
+
|
6
|
+
require 'vagrant_cloud/account'
|
7
|
+
require 'vagrant_cloud/box'
|
8
|
+
require 'vagrant_cloud/version'
|
9
|
+
require 'vagrant_cloud/provider'
|
10
|
+
require 'vagrant_cloud/search'
|
11
|
+
require 'vagrant_cloud/client'
|
metadata
CHANGED
@@ -1,43 +1,44 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: vagrant_cloud
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
|
+
- HashiCorp
|
7
8
|
- Cargo Media
|
8
9
|
autorequire:
|
9
10
|
bindir: bin
|
10
11
|
cert_chain: []
|
11
|
-
date:
|
12
|
+
date: 2018-10-16 00:00:00.000000000 Z
|
12
13
|
dependencies:
|
13
14
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
15
|
+
name: json
|
15
16
|
requirement: !ruby/object:Gem::Requirement
|
16
17
|
requirements:
|
17
18
|
- - "~>"
|
18
19
|
- !ruby/object:Gem::Version
|
19
|
-
version: 2.0
|
20
|
+
version: 2.1.0
|
20
21
|
type: :runtime
|
21
22
|
prerelease: false
|
22
23
|
version_requirements: !ruby/object:Gem::Requirement
|
23
24
|
requirements:
|
24
25
|
- - "~>"
|
25
26
|
- !ruby/object:Gem::Version
|
26
|
-
version: 2.0
|
27
|
+
version: 2.1.0
|
27
28
|
- !ruby/object:Gem::Dependency
|
28
|
-
name:
|
29
|
+
name: rest-client
|
29
30
|
requirement: !ruby/object:Gem::Requirement
|
30
31
|
requirements:
|
31
32
|
- - "~>"
|
32
33
|
- !ruby/object:Gem::Version
|
33
|
-
version: 0.
|
34
|
+
version: 2.0.2
|
34
35
|
type: :runtime
|
35
36
|
prerelease: false
|
36
37
|
version_requirements: !ruby/object:Gem::Requirement
|
37
38
|
requirements:
|
38
39
|
- - "~>"
|
39
40
|
- !ruby/object:Gem::Version
|
40
|
-
version: 0.
|
41
|
+
version: 2.0.2
|
41
42
|
- !ruby/object:Gem::Dependency
|
42
43
|
name: rake
|
43
44
|
requirement: !ruby/object:Gem::Requirement
|
@@ -67,38 +68,36 @@ dependencies:
|
|
67
68
|
- !ruby/object:Gem::Version
|
68
69
|
version: '3.0'
|
69
70
|
- !ruby/object:Gem::Dependency
|
70
|
-
name:
|
71
|
+
name: rubocop
|
71
72
|
requirement: !ruby/object:Gem::Requirement
|
72
73
|
requirements:
|
73
74
|
- - "~>"
|
74
75
|
- !ruby/object:Gem::Version
|
75
|
-
version:
|
76
|
+
version: 0.59.1
|
76
77
|
type: :development
|
77
78
|
prerelease: false
|
78
79
|
version_requirements: !ruby/object:Gem::Requirement
|
79
80
|
requirements:
|
80
81
|
- - "~>"
|
81
82
|
- !ruby/object:Gem::Version
|
82
|
-
version:
|
83
|
+
version: 0.59.1
|
83
84
|
- !ruby/object:Gem::Dependency
|
84
|
-
name:
|
85
|
+
name: webmock
|
85
86
|
requirement: !ruby/object:Gem::Requirement
|
86
87
|
requirements:
|
87
88
|
- - "~>"
|
88
89
|
- !ruby/object:Gem::Version
|
89
|
-
version: 0
|
90
|
+
version: '3.0'
|
90
91
|
type: :development
|
91
92
|
prerelease: false
|
92
93
|
version_requirements: !ruby/object:Gem::Requirement
|
93
94
|
requirements:
|
94
95
|
- - "~>"
|
95
96
|
- !ruby/object:Gem::Version
|
96
|
-
version: 0
|
97
|
-
description:
|
98
|
-
|
99
|
-
|
100
|
-
executables:
|
101
|
-
- vagrant_cloud
|
97
|
+
version: '3.0'
|
98
|
+
description: Ruby library for the HashiCorp Vagrant Cloud API
|
99
|
+
email: vagrant@hashicorp.com
|
100
|
+
executables: []
|
102
101
|
extensions: []
|
103
102
|
extra_rdoc_files: []
|
104
103
|
files:
|
@@ -108,14 +107,20 @@ files:
|
|
108
107
|
- lib/vagrant_cloud.rb
|
109
108
|
- lib/vagrant_cloud/account.rb
|
110
109
|
- lib/vagrant_cloud/box.rb
|
111
|
-
- lib/vagrant_cloud/
|
110
|
+
- lib/vagrant_cloud/client.rb
|
111
|
+
- lib/vagrant_cloud/errors.rb
|
112
112
|
- lib/vagrant_cloud/provider.rb
|
113
|
+
- lib/vagrant_cloud/search.rb
|
113
114
|
- lib/vagrant_cloud/version.rb
|
114
|
-
homepage: https://github.com/
|
115
|
+
homepage: https://github.com/hashicorp/vagrant_cloud
|
115
116
|
licenses:
|
116
117
|
- MIT
|
117
118
|
metadata: {}
|
118
|
-
post_install_message:
|
119
|
+
post_install_message: |-
|
120
|
+
NOTICE: As of the 2.0.0 release, the vagrant_cloud gem provides library functionality
|
121
|
+
and no longer includes a command line client. For a command line client,
|
122
|
+
use the `vagrant cloud` subcommand from Vagrant. Vagrant can be downloaded
|
123
|
+
from: https://www.vagrantup.com/downloads.html
|
119
124
|
rdoc_options: []
|
120
125
|
require_paths:
|
121
126
|
- lib
|
@@ -131,8 +136,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
131
136
|
version: '0'
|
132
137
|
requirements: []
|
133
138
|
rubyforge_project:
|
134
|
-
rubygems_version: 2.
|
139
|
+
rubygems_version: 2.5.2.1
|
135
140
|
signing_key:
|
136
141
|
specification_version: 4
|
137
|
-
summary:
|
142
|
+
summary: Vagrant Cloud API Library
|
138
143
|
test_files: []
|
data/lib/vagrant_cloud/cli.rb
DELETED
@@ -1,65 +0,0 @@
|
|
1
|
-
require 'thor'
|
2
|
-
|
3
|
-
module VagrantCloud
|
4
|
-
class Cli < Thor
|
5
|
-
package_name 'VagrantCloud'
|
6
|
-
|
7
|
-
class_option :username, alias: '-u', required: true, desc: 'Vagrant Cloud username'
|
8
|
-
class_option :token, alias: '-t', required: true, desc: 'Vagrant Cloud access token'
|
9
|
-
class_option :box, alias: '-b', required: true, desc: 'name of the box'
|
10
|
-
class_option :verbose, type: :boolean
|
11
|
-
|
12
|
-
desc 'create_version', 'creates a new version within a given box'
|
13
|
-
method_option :version, alias: '-v', required: true, desc: 'version within the box'
|
14
|
-
def create_version
|
15
|
-
version = current_box.create_version(options[:version])
|
16
|
-
puts "created #{version.version} of box #{options[:box]}; current status is #{version.status}" if options[:verbose]
|
17
|
-
version
|
18
|
-
end
|
19
|
-
|
20
|
-
desc 'release_version', 'release the specified version within a given box'
|
21
|
-
method_option :version, alias: '-v', required: true, desc: 'version within the box'
|
22
|
-
def release_version
|
23
|
-
version = current_version
|
24
|
-
version.release
|
25
|
-
puts "Box #{options[:box]} / version: #{version.version}; current status is #{version.status}" if options[:verbose]
|
26
|
-
true
|
27
|
-
end
|
28
|
-
|
29
|
-
desc 'create_provider', 'creates a provider within a given box and version'
|
30
|
-
method_option :version, alias: '-v', required: true, desc: 'version within the box'
|
31
|
-
method_option :provider, alias: '-p', default: 'virtualbox', desc: 'the provider for the box; default: virtualbox'
|
32
|
-
method_option :provider_url, alias: '-pu', desc: 'URL to the file for remote hosting; do not include if you intend to upload a file subsequently'
|
33
|
-
def create_provider
|
34
|
-
provider = current_version.create_provider(options[:provider], options[:provider_url])
|
35
|
-
puts "created #{provider.data['name']} provider within version #{provider.version.version}" if options[:verbose]
|
36
|
-
provider
|
37
|
-
end
|
38
|
-
|
39
|
-
desc 'upload_file', 'upload a file for Atlas to host to an existing version and provider'
|
40
|
-
method_option :version, alias: '-v', required: true, desc: 'version within the box'
|
41
|
-
method_option :provider, alias: '-p', default: 'virtualbox', desc: 'the provider for the box; default: virtualbox'
|
42
|
-
method_option :provider_file_path, alias: '-pfp', required: true, desc: 'path to file to be uploaded for Atlast hosting'
|
43
|
-
def upload_file
|
44
|
-
get_provider(options[:provider]).upload_file(options[:provider_file_path])
|
45
|
-
end
|
46
|
-
|
47
|
-
private
|
48
|
-
|
49
|
-
def current_account
|
50
|
-
VagrantCloud::Account.new(options[:username], options[:token])
|
51
|
-
end
|
52
|
-
|
53
|
-
def current_box
|
54
|
-
current_account.get_box(options[:box])
|
55
|
-
end
|
56
|
-
|
57
|
-
def current_version
|
58
|
-
current_box.get_version(options[:version])
|
59
|
-
end
|
60
|
-
|
61
|
-
def get_provider(provider_str)
|
62
|
-
current_version.get_provider(provider_str)
|
63
|
-
end
|
64
|
-
end
|
65
|
-
end
|