Skip to content

Commit

Permalink
fix: allow getting resources with number as name
Browse files Browse the repository at this point in the history
  • Loading branch information
phm07 committed Dec 19, 2024
1 parent 51baf8d commit d4d8657
Show file tree
Hide file tree
Showing 11 changed files with 48 additions and 12 deletions.
5 changes: 4 additions & 1 deletion hcloud/certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,10 @@ func (c *CertificateClient) GetByName(ctx context.Context, name string) (*Certif
// retrieves a Certificate by its name. If the Certificate does not exist, nil is returned.
func (c *CertificateClient) Get(ctx context.Context, idOrName string) (*Certificate, *Response, error) {
if id, err := strconv.ParseInt(idOrName, 10, 64); err == nil {
return c.GetByID(ctx, id)
cert, res, err := c.GetByID(ctx, id)
if cert != nil {
return cert, res, err
}
}
return c.GetByName(ctx, idOrName)
}
Expand Down
5 changes: 4 additions & 1 deletion hcloud/firewall.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,10 @@ func (c *FirewallClient) GetByName(ctx context.Context, name string) (*Firewall,
// retrieves a Firewall by its name. If the Firewall does not exist, nil is returned.
func (c *FirewallClient) Get(ctx context.Context, idOrName string) (*Firewall, *Response, error) {
if id, err := strconv.ParseInt(idOrName, 10, 64); err == nil {
return c.GetByID(ctx, id)
fw, res, err := c.GetByID(ctx, id)
if fw != nil {
return fw, res, err
}
}
return c.GetByName(ctx, idOrName)
}
Expand Down
5 changes: 4 additions & 1 deletion hcloud/floating_ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,10 @@ func (c *FloatingIPClient) GetByName(ctx context.Context, name string) (*Floatin
// retrieves a Floating IP by its name. If the Floating IP does not exist, nil is returned.
func (c *FloatingIPClient) Get(ctx context.Context, idOrName string) (*FloatingIP, *Response, error) {
if id, err := strconv.ParseInt(idOrName, 10, 64); err == nil {
return c.GetByID(ctx, id)
ip, res, err := c.GetByID(ctx, id)
if ip != nil {
return ip, res, err
}
}
return c.GetByName(ctx, idOrName)
}
Expand Down
10 changes: 8 additions & 2 deletions hcloud/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,10 @@ func (c *ImageClient) GetByNameAndArchitecture(ctx context.Context, name string,
// Deprecated: Use [ImageClient.GetForArchitecture] instead.
func (c *ImageClient) Get(ctx context.Context, idOrName string) (*Image, *Response, error) {
if id, err := strconv.ParseInt(idOrName, 10, 64); err == nil {
return c.GetByID(ctx, id)
img, res, err := c.GetByID(ctx, id)
if img != nil {
return img, res, err
}
}
return c.GetByName(ctx, idOrName)
}
Expand All @@ -146,7 +149,10 @@ func (c *ImageClient) Get(ctx context.Context, idOrName string) (*Image, *Respon
// check for this in your calling method.
func (c *ImageClient) GetForArchitecture(ctx context.Context, idOrName string, architecture Architecture) (*Image, *Response, error) {
if id, err := strconv.ParseInt(idOrName, 10, 64); err == nil {
return c.GetByID(ctx, id)
img, res, err := c.GetByID(ctx, id)
if img != nil {
return img, res, err
}
}
return c.GetByNameAndArchitecture(ctx, idOrName, architecture)
}
Expand Down
5 changes: 4 additions & 1 deletion hcloud/load_balancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,10 @@ func (c *LoadBalancerClient) GetByName(ctx context.Context, name string) (*LoadB
// retrieves a Load Balancer by its name. If the Load Balancer does not exist, nil is returned.
func (c *LoadBalancerClient) Get(ctx context.Context, idOrName string) (*LoadBalancer, *Response, error) {
if id, err := strconv.ParseInt(idOrName, 10, 64); err == nil {
return c.GetByID(ctx, id)
lb, res, err := c.GetByID(ctx, id)
if lb != nil {
return lb, res, err
}
}
return c.GetByName(ctx, idOrName)
}
Expand Down
5 changes: 4 additions & 1 deletion hcloud/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,10 @@ func (c *NetworkClient) GetByName(ctx context.Context, name string) (*Network, *
// retrieves a network by its name. If the network does not exist, nil is returned.
func (c *NetworkClient) Get(ctx context.Context, idOrName string) (*Network, *Response, error) {
if id, err := strconv.ParseInt(idOrName, 10, 64); err == nil {
return c.GetByID(ctx, id)
n, res, err := c.GetByID(ctx, id)
if n != nil {
return n, res, err
}
}
return c.GetByName(ctx, idOrName)
}
Expand Down
5 changes: 4 additions & 1 deletion hcloud/placement_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ func (c *PlacementGroupClient) GetByName(ctx context.Context, name string) (*Pla
// retrieves a PlacementGroup by its name. If the PlacementGroup does not exist, nil is returned.
func (c *PlacementGroupClient) Get(ctx context.Context, idOrName string) (*PlacementGroup, *Response, error) {
if id, err := strconv.ParseInt(idOrName, 10, 64); err == nil {
return c.GetByID(ctx, id)
pg, res, err := c.GetByID(ctx, id)
if pg != nil {
return pg, res, err
}
}
return c.GetByName(ctx, idOrName)
}
Expand Down
5 changes: 4 additions & 1 deletion hcloud/primary_ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,10 @@ func (c *PrimaryIPClient) GetByName(ctx context.Context, name string) (*PrimaryI
// retrieves a Primary IP by its name. If the Primary IP does not exist, nil is returned.
func (c *PrimaryIPClient) Get(ctx context.Context, idOrName string) (*PrimaryIP, *Response, error) {
if id, err := strconv.ParseInt(idOrName, 10, 64); err == nil {
return c.GetByID(ctx, id)
ip, res, err := c.GetByID(ctx, id)
if ip != nil {
return ip, res, err
}
}
return c.GetByName(ctx, idOrName)
}
Expand Down
5 changes: 4 additions & 1 deletion hcloud/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,10 @@ func (c *ServerClient) GetByName(ctx context.Context, name string) (*Server, *Re
// retrieves a server by its name. If the server does not exist, nil is returned.
func (c *ServerClient) Get(ctx context.Context, idOrName string) (*Server, *Response, error) {
if id, err := strconv.ParseInt(idOrName, 10, 64); err == nil {
return c.GetByID(ctx, id)
srv, res, err := c.GetByID(ctx, id)
if srv != nil {
return srv, res, err
}
}
return c.GetByName(ctx, idOrName)
}
Expand Down
5 changes: 4 additions & 1 deletion hcloud/ssh_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ func (c *SSHKeyClient) GetByFingerprint(ctx context.Context, fingerprint string)
// retrieves a SSH key by its name. If the SSH key does not exist, nil is returned.
func (c *SSHKeyClient) Get(ctx context.Context, idOrName string) (*SSHKey, *Response, error) {
if id, err := strconv.ParseInt(idOrName, 10, 64); err == nil {
return c.GetByID(ctx, id)
sshKey, res, err := c.GetByID(ctx, id)
if sshKey != nil {
return sshKey, res, err
}
}
return c.GetByName(ctx, idOrName)
}
Expand Down
5 changes: 4 additions & 1 deletion hcloud/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,10 @@ func (c *VolumeClient) GetByName(ctx context.Context, name string) (*Volume, *Re
// retrieves a volume by its name. If the volume does not exist, nil is returned.
func (c *VolumeClient) Get(ctx context.Context, idOrName string) (*Volume, *Response, error) {
if id, err := strconv.ParseInt(idOrName, 10, 64); err == nil {
return c.GetByID(ctx, id)
vol, res, err := c.GetByID(ctx, id)
if vol != nil {
return vol, res, err
}
}
return c.GetByName(ctx, idOrName)
}
Expand Down

0 comments on commit d4d8657

Please sign in to comment.