Skip to content

Commit

Permalink
check if accessConfig is not nil before deleting
Browse files Browse the repository at this point in the history
  • Loading branch information
var77 committed Jun 27, 2024
1 parent 744a128 commit 37fafb7
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
12 changes: 6 additions & 6 deletions model/gcp_vm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,25 +72,25 @@ def swap_ip(vm)
gcp_client = Hosting::GcpApis.new
zone1 = "#{location}-a"
zone2 = "#{vm.location}-a"
vm_info1 = gcp_client.get_vm(name, zone1)
vm_info2 = gcp_client.get_vm(vm.name, zone2)
access_configs1 = gcp_client.get_vm(name, zone1)["networkInterfaces"][0]["accessConfigs"] || []
access_configs2 = gcp_client.get_vm(vm.name, zone2)["networkInterfaces"][0]["accessConfigs"] || []

if vm_info1["networkInterfaces"][0]["accessConfigs"].find { _1["name"] == "External NAT" }
if access_configs1.find { _1["name"] == "External NAT" }
gcp_client.delete_ephermal_ipv4(name, zone1)
end

if vm_info2["networkInterfaces"][0]["accessConfigs"].find { _1["name"] == "External NAT" }
if access_configs2.find { _1["name"] == "External NAT" }
gcp_client.delete_ephermal_ipv4(vm.name, zone2)
end

# we are explicitly checking if ip is already assigned
# because the operation can be terminated while running
# and on next retry we will have error that the external ip is already assigned
if !vm_info1["networkInterfaces"][0]["accessConfigs"].find { _1["natIP"] == vm.sshable.host }
if !access_configs1.find { _1["natIP"] == vm.sshable.host }
gcp_client.assign_static_ipv4(name, vm.sshable.host, zone1)
end

if !vm_info2["networkInterfaces"][0]["accessConfigs"].find { _1["natIP"] == sshable.host }
if !access_configs2.find { _1["natIP"] == sshable.host }
gcp_client.assign_static_ipv4(vm.name, sshable.host, zone2)
end

Expand Down
18 changes: 18 additions & 0 deletions spec/model/gcp_vm_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,24 @@
expect { gcp_vm.swap_ip(vm2) }.not_to raise_error
end

it "does not delete if no access config" do
api = instance_double(Hosting::GcpApis)
expect(api).to receive(:assign_static_ipv4).with("vm1", "ip2", "us-central1-a")
expect(api).to receive(:assign_static_ipv4).with("vm2", "ip1", "us-central1-a")
expect(api).to receive(:get_vm).and_return({"networkInterfaces" => [{"accessConfigs" => nil}]}).at_least(:once)
expect(Hosting::GcpApis).to receive(:new).and_return(api)
vm2 = instance_double(described_class, name: "vm2", address_name: "vm2-addr", location: "us-central1", sshable: instance_double(Sshable, host: "ip2"))
expect(gcp_vm).to receive(:sshable).and_return(instance_double(Sshable, host: "ip1")).at_least(:once)
expect(gcp_vm.sshable).to receive(:invalidate_cache_entry)
expect(vm2.sshable).to receive(:invalidate_cache_entry)
expect(gcp_vm.sshable).to receive(:update).with(host: "temp_vm1")
expect(gcp_vm.sshable).to receive(:update).with(host: "ip2")
expect(vm2.sshable).to receive(:update).with(host: "ip1")
expect(gcp_vm).to receive(:update).with(address_name: "vm2-addr")
expect(vm2).to receive(:update).with(address_name: "vm1-addr")
expect { gcp_vm.swap_ip(vm2) }.not_to raise_error
end

it "does not delete if no public ip" do
api = instance_double(Hosting::GcpApis)
expect(api).to receive(:assign_static_ipv4).with("vm1", "ip2", "us-central1-a")
Expand Down

0 comments on commit 37fafb7

Please sign in to comment.