Fix ensureStaticIP if name for existed address was changed

It might happend when we change external IP address from ephemeral to
static and give it a custom name.
In this case GetRegionAddress with given name will return Not Found
This commit is contained in:
Elena Morozova 2019-12-18 16:14:24 -08:00
parent 0387ee4244
commit 0995ef573f
2 changed files with 19 additions and 0 deletions

View File

@ -1060,6 +1060,18 @@ func ensureStaticIP(s CloudAddressService, name, serviceName, region, existingIP
existed = true
}
// If address exists, get it by IP, because name might be different.
// This can specifically happen if the IP was changed from ephemeral to static,
// which results in a new name for the IP.
if existingIP != "" {
addr, err := s.GetRegionAddressByIP(region, existingIP)
if err != nil {
return "", false, fmt.Errorf("error getting static IP address: %v", err)
}
return addr.Address, existed, nil
}
// Otherwise, get address by name
addr, err := s.GetRegionAddress(name, region)
if err != nil {
return "", false, fmt.Errorf("error getting static IP address: %v", err)

View File

@ -63,6 +63,13 @@ func TestEnsureStaticIP(t *testing.T) {
if err != nil || !existed || ip != ipPrime {
t.Fatalf(`ensureStaticIP(%v, %v, %v, %v, %v) = %v, %v, %v; want %v, true, nil`, gce, ipName, serviceName, gce.region, ip, ipPrime, existed, err, ip)
}
// Ensure call with different name
ipName = "another-name-for-static-ip"
ipPrime, existed, err = ensureStaticIP(gce, ipName, serviceName, gce.region, ip, cloud.NetworkTierDefault)
if err != nil || !existed || ip != ipPrime {
t.Fatalf(`ensureStaticIP(%v, %v, %v, %v, %v) = %v, %v, %v; want %v, true, nil`, gce, ipName, serviceName, gce.region, ip, ipPrime, existed, err, ip)
}
}
func TestEnsureStaticIPWithTier(t *testing.T) {