Skip to content

Commit

Permalink
Merge pull request #485 from gscho/set-machine-type
Browse files Browse the repository at this point in the history
Created set machine type request
  • Loading branch information
Temikus authored Feb 24, 2020
2 parents 0cdfd9d + 0e85063 commit a82c2c5
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/fog/compute/google.rb
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ class Google < Fog::Service
request :set_forwarding_rule_target
request :set_global_forwarding_rule_target
request :set_server_disk_auto_delete
request :set_server_machine_type
request :set_server_metadata
request :set_server_scheduling
request :set_server_tags
Expand Down
15 changes: 15 additions & 0 deletions lib/fog/compute/google/models/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,21 @@ def set_metadata(new_metadata = {}, async = true)
reload
end

def set_machine_type(new_machine_type, async = true)
requires :identity, :zone

raise Fog::Errors::Error.new("Instance must be stopped to change machine type") unless stopped?

data = service.set_server_machine_type(
identity, zone_name, new_machine_type
)
operation = Fog::Compute::Google::Operations
.new(:service => service)
.get(data.name, data.zone)
operation.wait_for { ready? } unless async
reload
end

def set_tags(new_tags = [], async = true)
requires :identity, :zone

Expand Down
23 changes: 23 additions & 0 deletions lib/fog/compute/google/requests/set_server_machine_type.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module Fog
module Compute
class Google
class Mock
def set_server_machine_type(_instance, _zone, _machine_type)
# :no-coverage:
Fog::Mock.not_implemented
# :no-coverage:
end
end

class Real
def set_server_machine_type(instance, zone, machine_type)
request = ::Google::Apis::ComputeV1::InstancesSetMachineTypeRequest.new
zone = zone.split("/")[-1]
machine_type = machine_type.split("/")[-1]
request.machine_type = "zones/#{zone}/machineTypes/#{machine_type}"
@compute.set_instance_machine_type(@project, zone, instance, request)
end
end
end
end
end
16 changes: 16 additions & 0 deletions test/integration/compute/core_compute/test_servers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,22 @@ def teardown
super
end

def test_set_machine_type
server = @factory.create
server.stop
server.wait_for { stopped? }
server.set_machine_type("n1-standard-2", false)
assert_equal "n1-standard-2", server.machine_type.split("/")[-1]
end

def test_set_machine_type_fail
server = @factory.create
server.wait_for { ready? }
assert_raises Fog::Errors::Error do
server.set_machine_type("n1-standard-2", false)
end
end

def test_set_metadata
server = @factory.create
server.wait_for { ready? }
Expand Down

0 comments on commit a82c2c5

Please sign in to comment.