Skip to content

Commit 5f14cb0

Browse files
committed
add argument error handling when getting path id
1 parent 06abfbb commit 5f14cb0

File tree

13 files changed

+68
-27
lines changed

13 files changed

+68
-27
lines changed

hcloud/action.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ func (a *Action) Error() error {
8686

8787
type nilResource struct{}
8888

89-
func (nilResource) pathID() string {
90-
return ""
89+
func (nilResource) pathID() (string, error) {
90+
return "", nil
9191
}
9292

9393
// ActionClient is a client for the actions API.
@@ -209,7 +209,7 @@ func (c *ResourceActionClient[R]) All(ctx context.Context, opts ActionListOpts)
209209
}
210210

211211
type actionSupporter interface {
212-
pathID() string
212+
pathID() (string, error)
213213
}
214214

215215
// ListFor returns a paginated list of actions for the given Resource.
@@ -220,7 +220,10 @@ func (c *ResourceActionClient[R]) ListFor(ctx context.Context, resource R, opts
220220
opPath := c.getBaseURL() + "/%s/actions?%s"
221221
ctx = ctxutil.SetOpPath(ctx, opPath)
222222

223-
id := resource.pathID()
223+
id, err := resource.pathID()
224+
if err != nil {
225+
return nil, nil, invalidArgument("resource", resource, err)
226+
}
224227

225228
reqPath := fmt.Sprintf(opPath, id, opts.values().Encode())
226229

hcloud/action_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,15 @@ func TestResourceActionClientListFor(t *testing.T) {
407407
require.Len(t, result, 1)
408408
require.Equal(t, int64(1509772237), result[0].ID)
409409
})
410+
411+
t.Run("invalid argument", func(t *testing.T) {
412+
ctx, _, client := makeTestUtils(t)
413+
414+
result, resp, err := client.PrimaryIP.Action.ListFor(ctx, &PrimaryIP{ID: 0}, ActionListOpts{})
415+
require.EqualError(t, err, "invalid argument 'resource' [*hcloud.PrimaryIP]: missing field [ID] in [*hcloud.PrimaryIP]")
416+
require.Nil(t, result)
417+
require.Nil(t, resp)
418+
})
410419
}
411420

412421
func TestResourceActionClientAllFor(t *testing.T) {

hcloud/certificate.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,11 @@ type Certificate struct {
8282
UsedBy []CertificateUsedByRef
8383
}
8484

85-
func (c *Certificate) pathID() string {
86-
return strconv.FormatInt(c.ID, 10)
85+
func (c *Certificate) pathID() (string, error) {
86+
if c.ID == 0 {
87+
return "", missingField(c, "ID")
88+
}
89+
return strconv.FormatInt(c.ID, 10), nil
8790
}
8891

8992
// CertificateCreateResult is the result of creating a certificate.

hcloud/firewall.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,11 @@ type Firewall struct {
2222
AppliedTo []FirewallResource
2323
}
2424

25-
func (f *Firewall) pathID() string {
26-
return strconv.FormatInt(f.ID, 10)
25+
func (f *Firewall) pathID() (string, error) {
26+
if f.ID == 0 {
27+
return "", missingField(f, "ID")
28+
}
29+
return strconv.FormatInt(f.ID, 10), nil
2730
}
2831

2932
// FirewallRule represents a Firewall's rules.

hcloud/floating_ip.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,11 @@ type FloatingIP struct {
2929
Name string
3030
}
3131

32-
func (f *FloatingIP) pathID() string {
33-
return strconv.FormatInt(f.ID, 10)
32+
func (f *FloatingIP) pathID() (string, error) {
33+
if f.ID == 0 {
34+
return "", missingField(f, "ID")
35+
}
36+
return strconv.FormatInt(f.ID, 10), nil
3437
}
3538

3639
// DNSPtrForIP returns the reverse DNS pointer of the IP address.

hcloud/image.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,11 @@ type Image struct {
3535
Deleted time.Time
3636
}
3737

38-
func (image *Image) pathID() string {
39-
return strconv.FormatInt(image.ID, 10)
38+
func (image *Image) pathID() (string, error) {
39+
if image.ID == 0 {
40+
return "", missingField(image, "ID")
41+
}
42+
return strconv.FormatInt(image.ID, 10), nil
4043
}
4144

4245
// IsDeprecated returns whether the image is deprecated.

hcloud/load_balancer.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,11 @@ type LoadBalancer struct {
3232
IngoingTraffic uint64
3333
}
3434

35-
func (lb *LoadBalancer) pathID() string {
36-
return strconv.FormatInt(lb.ID, 10)
35+
func (lb *LoadBalancer) pathID() (string, error) {
36+
if lb.ID == 0 {
37+
return "", missingField(lb, "ID")
38+
}
39+
return strconv.FormatInt(lb.ID, 10), nil
3740
}
3841

3942
// LoadBalancerPublicNet represents a Load Balancer's public network.

hcloud/network.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,11 @@ type Network struct {
5757
ExposeRoutesToVSwitch bool
5858
}
5959

60-
func (n *Network) pathID() string {
61-
return strconv.FormatInt(n.ID, 10)
60+
func (n *Network) pathID() (string, error) {
61+
if n.ID == 0 {
62+
return "", missingField(n, "ID")
63+
}
64+
return strconv.FormatInt(n.ID, 10), nil
6265
}
6366

6467
// NetworkSubnet represents a subnet of a network in the Hetzner Cloud.

hcloud/primary_ip.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,11 @@ type PrimaryIP struct {
3535
Datacenter *Datacenter
3636
}
3737

38-
func (p *PrimaryIP) pathID() string {
39-
return strconv.FormatInt(p.ID, 10)
38+
func (p *PrimaryIP) pathID() (string, error) {
39+
if p.ID == 0 {
40+
return "", missingField(p, "ID")
41+
}
42+
return strconv.FormatInt(p.ID, 10), nil
4043
}
4144

4245
// PrimaryIPProtection represents the protection level of a Primary IP.

hcloud/server.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,11 @@ type Server struct {
4444
Datacenter *Datacenter
4545
}
4646

47-
func (s *Server) pathID() string {
48-
return strconv.FormatInt(s.ID, 10)
47+
func (s *Server) pathID() (string, error) {
48+
if s.ID == 0 {
49+
return "", missingField(s, "ID")
50+
}
51+
return strconv.FormatInt(s.ID, 10), nil
4952
}
5053

5154
// ServerProtection represents the protection level of a server.

0 commit comments

Comments
 (0)