Skip to content

Commit

Permalink
Fixing Server.set_metadata
Browse files Browse the repository at this point in the history
- Fixes the set_metadata method and adopts a better format for it.
- This should not be a breaking change since the method appears to have never worked since 2.0 :(
- Adding a test for it so it gets caught by CI in the future

Fixes #329
  • Loading branch information
Temikus committed May 19, 2018
1 parent db3e01f commit 2554ba0
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
16 changes: 13 additions & 3 deletions lib/fog/compute/google/models/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -350,15 +350,25 @@ def set_scheduling(async = true,
reload
end

# Set an instance metadata
#
# @param [Bool] async Perform the operation asyncronously
# @param [Hash] new_metadata A new metadata object
# Format: {'foo' => 'bar', 'baz'=>'foo'}
#
# @returns [Fog::Compute::Google::Server] server object
def set_metadata(new_metadata = {}, async = true)
requires :identity, :zone

if new_metadata[:items] && new_metadata[:items].is_a?(Hash)
new_metadata[:items] = new_metadata[:items].map { |k, v| { :key => k, :value => v } }
unless new_metadata.is_a?(Hash)
raise Fog::Errors::Error.new("Instance metadata should be a hash")
end

# If metadata is presented in {'foo' => 'bar', 'baz'=>'foo'}
new_metadata_items = new_metadata.each.map { |k, v| { :key => k.to_s, :value => v.to_s } }

data = service.set_server_metadata(
identity, zone_name, metadata[:fingerprint], new_metadata
identity, zone_name, metadata[:fingerprint], new_metadata_items
)
operation = Fog::Compute::Google::Operations
.new(:service => service)
Expand Down
2 changes: 2 additions & 0 deletions lib/fog/compute/google/requests/set_server_metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ class Real
# instance.reload
# fingerprint = instance.metadata['fingerprint']
# @param [Hash] metadata A new metadata object
# Should have the following structure:
# {:items=>[{:key=>"foo", :value=>"bar"}, {:key=>"baz", :value=>"foo"}]}
#
# @returns [::Google::Apis::ComputeV1::Operation] set operation
def set_server_metadata(instance, zone, fingerprint, metadata_items = [])
Expand Down
9 changes: 9 additions & 0 deletions test/integration/compute/test_servers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,17 @@
class TestServers < FogIntegrationTest
include TestCollection

# Cleanup is handled by TestCollection
def setup
@subject = Fog::Compute[:google].servers
@factory = ServersFactory.new(namespaced_name)
end

def test_set_metadata
server = @factory.create
server.wait_for { ready? }
server.set_metadata({ "foo" => "bar", "baz" => "foo" }, false)
assert_equal [{ :key => "foo", :value => "bar" },
{ :key=>"baz", :value=>"foo" }], server.metadata[:items]
end
end

0 comments on commit 2554ba0

Please sign in to comment.