Skip to content

Commit 89a91bc

Browse files
authored
Merge pull request #435 from Temikus/non_boot_disk
JRuby fixes + disk tests/docs
2 parents 0713914 + 9815ecb commit 89a91bc

File tree

9 files changed

+193
-10
lines changed

9 files changed

+193
-10
lines changed

.travis.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@ dist: trusty
55
matrix:
66
fast_finish: true
77
include:
8-
- rvm: 2.1
9-
- rvm: 2.2
108
- rvm: 2.3
119
- rvm: 2.4
1210
- rvm: 2.5
11+
- rvm: jruby-9.1
1312
- rvm: jruby-head
1413
allow_failures:
1514
- rvm: jruby-head

CHANGELOG.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,27 @@ The format is loosely based on [Keep a Changelog](http://keepachangelog.com/en/1
44

55
This project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
66

7+
## Next
8+
9+
### User-facing
10+
11+
- \#435 Added additional examples for attached disks usage. [temikus]
12+
13+
### Development changes
14+
15+
#### Fixed
16+
17+
- \#425 Miscellaneous dev improvements around JRuby and disk handling [temikus]
18+
- Fix bundling in development environment on JRuby
19+
- Adding JRuby 9.1 into Travis
20+
- Remove EOL versions of ruby from Travis
21+
- Consolidated logic of `Disk.get_as_boot_disk` and increase doc coverage of
22+
disk-associated methods.
23+
- Added integration tests for disk snapshots + a guard method for
24+
`Snapshot.add_labels`
25+
26+
- \#435
27+
728
## 1.8.1
829

930
### User-facing

Gemfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,8 @@ gem "inch", :require => false
66
gem "osrcry", :require => false
77
gem "rubocop", :require => false
88

9+
# Debugger is specified here as it's not compatible with jRuby
10+
gem "pry-byebug", :platforms => :ruby
11+
912
# Specify your gem's dependencies in fog-google.gemspec
1013
gemspec
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# All examples presume that you have a ~/.fog credentials file set up.
2+
# More info on it can be found here: http://fog.io/about/getting_started.html
3+
4+
require "bundler"
5+
Bundler.require(:default, :development)
6+
7+
def example
8+
p "Connecting to Google API"
9+
connection = Fog::Compute.new(:provider => "Google")
10+
11+
p "Creating disk"
12+
disk = connection.disks.create(
13+
:name => "fog-smoke-test-#{Time.now.to_i}",
14+
:size_gb => 10,
15+
:zone => "us-central1-f",
16+
:source_image => "debian-9-stretch-v20180611"
17+
)
18+
19+
p "Creating a second disk"
20+
attached_disk = connection.disks.create(
21+
:name => "fog-smoke-test-#{Time.now.to_i}",
22+
:size_gb => 10,
23+
:zone => "us-central1-f"
24+
)
25+
26+
p "Waiting for disks to be ready"
27+
disk.wait_for { ready? }
28+
attached_disk.wait_for { ready? }
29+
30+
p "Creating a server"
31+
server = connection.servers.create(
32+
:name => "fog-smoke-test-#{Time.now.to_i}",
33+
:disks => [disk.attached_disk_obj(boot: true),
34+
attached_disk.attached_disk_obj(boot: false,
35+
auto_delete: true)],
36+
:machine_type => "n1-standard-1",
37+
:private_key_path => File.expand_path("~/.ssh/id_rsa"),
38+
:public_key_path => File.expand_path("~/.ssh/id_rsa.pub"),
39+
:zone => "us-central1-f",
40+
# Will be simplified, see https://github.com/fog/fog-google/issues/360
41+
:network_interfaces => [{ :network => "global/networks/default",
42+
:access_configs => [{
43+
:name => "External NAT",
44+
:type => "ONE_TO_ONE_NAT"
45+
}] }],
46+
:username => ENV["USER"]
47+
)
48+
49+
p "Deleting server"
50+
raise "Could not delete server." unless server.destroy
51+
end
52+
53+
example

fog-google.gemspec

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ Gem::Specification.new do |spec|
3030

3131
# Debugger
3232
spec.add_development_dependency "pry"
33-
spec.add_development_dependency "pry-byebug"
3433

3534
# Testing gems
3635
spec.add_development_dependency "retriable"

lib/fog/compute/google/models/disk.rb

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,19 +68,51 @@ def zone_name
6868
zone.nil? ? nil : zone.split("/")[-1]
6969
end
7070

71+
# Returns an attached disk configuration hash.
72+
#
73+
# Compute API needs attached disks to be specified in a custom format.
74+
# This provides a handy shortcut for generating a preformatted config.
75+
#
76+
# Example output:
77+
# {:auto_delete=>false,
78+
# :boot=>true,
79+
# :mode=>"READ_WRITE",
80+
# :source=>"https://www.googleapis.com/compute/v1/projects/myproj/zones/us-central1-f/disks/mydisk",
81+
# :type=>"PERSISTENT"}
82+
#
83+
# See Instances.insert API docs for more info:
84+
# https://cloud.google.com/compute/docs/reference/rest/v1/instances/insert
85+
#
86+
# @param [Hash] opts options to attach the disk with.
87+
# @option opts [Boolean] :writable The mode in which to attach this
88+
# disk. (defaults to READ_WRITE)
89+
# @option opts [Boolean] :boot Indicates whether this is a boot disk.
90+
# (defaults to false)
91+
# @option opts [String] :device_name Specifies a unique device name
92+
# of your choice that is reflected into the /dev/disk/by-id/google-*
93+
# tree of a Linux operating system running within the instance.
94+
# @option opts [Object] :encryption_key Encrypts or decrypts a disk
95+
# using a customer-supplied encryption key.
96+
# @option opts [Object] :auto_delete Specifies whether the disk will
97+
# be auto-deleted when the instance is deleted. (defaults to false)
98+
#
99+
# @return [Hash] Attached disk configuration hash
71100
def attached_disk_obj(opts = {})
72101
requires :self_link
73102
collection.attached_disk_obj(self_link, opts)
74103
end
75104

105+
# A legacy shorthand for attached_disk_obj
106+
#
107+
# @param [Object] writable The mode in which to attach this disk.
108+
# (defaults to READ_WRITE)
109+
# @param [Object] auto_delete Specifies whether the disk will be
110+
# auto-deleted when the instance is deleted. (defaults to false)
111+
# @return [Hash]
76112
def get_as_boot_disk(writable = true, auto_delete = false)
77-
{
78-
:auto_delete => auto_delete,
79-
:boot => true,
80-
:source => self_link,
81-
:mode => writable ? "READ_WRITE" : "READ_ONLY",
82-
:type => "PERSISTENT"
83-
}
113+
attached_disk_obj(boot: true,
114+
writable: writable,
115+
auto_delete: auto_delete)
84116
end
85117

86118
def ready?

lib/fog/compute/google/models/disks.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,34 @@ def get(identity, zone = nil)
3838
nil
3939
end
4040

41+
# Returns an attached disk configuration hash.
42+
#
43+
# Compute API needs attached disks to be specified in a custom format.
44+
# This provides a handy shortcut for generating a preformatted config.
45+
#
46+
# Example output:
47+
# {:auto_delete=>false,
48+
# :boot=>true,
49+
# :mode=>"READ_WRITE",
50+
# :source=>"https://www.googleapis.com/compute/v1/projects/myproj/zones/us-central1-f/disks/mydisk",
51+
# :type=>"PERSISTENT"}
52+
#
53+
# See Instances.insert API docs for more info:
54+
# https://cloud.google.com/compute/docs/reference/rest/v1/instances/insert
55+
#
56+
# @param [String] source self_link of an existing disk resource
57+
# @param [Boolean] writable The mode in which to attach this disk.
58+
# (defaults to READ_WRITE)
59+
# @param [Boolean] boot Indicates whether this is a boot disk.
60+
# (defaults to false)
61+
# @param [String] device_name Specifies a unique device name of your
62+
# choice that is reflected into the /dev/disk/by-id/google-* tree of
63+
# a Linux operating system running within the instance.
64+
# @param [Object] encryption_key Encrypts or decrypts a disk using
65+
# a customer-supplied encryption key.
66+
# @param [Object] auto_delete Specifies whether the disk will be
67+
# auto-deleted when the instance is deleted. (defaults to false)
68+
# @return [Hash]
4169
def attached_disk_obj(source,
4270
writable: true,
4371
boot: false,

lib/fog/compute/google/models/snapshot.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ def destroy(async = true)
3939

4040
def set_labels(new_labels)
4141
requires :identity, :label_fingerprint
42+
43+
unless new_labels.is_a? Hash
44+
raise ArgumentError,
45+
"Labels should be a hash, e.g. {foo: \"bar\",fog: \"test\"}"
46+
end
47+
4248
service.set_snapshot_labels(identity, label_fingerprint, new_labels)
4349
reload
4450
end

test/integration/compute/core_compute/test_disks.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,46 @@ def setup
88
@subject = Fog::Compute[:google].disks
99
@factory = DisksFactory.new(namespaced_name)
1010
end
11+
12+
def test_get_as_configs
13+
disk = @factory.create
14+
disk.wait_for { ready? }
15+
16+
example = disk.get_as_boot_disk
17+
config = { :auto_delete => false,
18+
:boot => true,
19+
:source => disk.self_link,
20+
:mode => "READ_WRITE",
21+
:type => "PERSISTENT" }
22+
assert_equal(example, config)
23+
24+
example_with_params = disk.get_as_boot_disk(false, true)
25+
config_with_params = { :auto_delete => true,
26+
:boot => true,
27+
:source => disk.self_link,
28+
:mode => "READ_ONLY",
29+
:type => "PERSISTENT" }
30+
assert_equal(example_with_params, config_with_params)
31+
end
32+
33+
def test_create_snapshot
34+
disk = @factory.create
35+
disk.wait_for { ready? }
36+
37+
snapshot = disk.create_snapshot("fog-test-snapshot")
38+
39+
assert(snapshot.is_a?(Fog::Compute::Google::Snapshot),
40+
"Resulting snapshot should be a snapshot object.")
41+
42+
assert_raises(ArgumentError) { snapshot.set_labels(["bar", "test"]) }
43+
44+
snapshot.set_labels(foo: "bar", fog: "test")
45+
46+
assert_equal(snapshot.labels[:foo], "bar")
47+
assert_equal(snapshot.labels[:fog], "test")
48+
49+
# Clean up the snapshot
50+
operation = snapshot.destroy
51+
operation.wait_for { ready? }
52+
end
1153
end

0 commit comments

Comments
 (0)