Skip to content

Commit 9abbd79

Browse files
authored
refactor: use generic request for Delete calls (#620)
1 parent 3ee94ef commit 9abbd79

File tree

11 files changed

+38
-60
lines changed

11 files changed

+38
-60
lines changed

hcloud/certificate.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -313,11 +313,9 @@ func (c *CertificateClient) Update(ctx context.Context, certificate *Certificate
313313

314314
// Delete deletes a certificate.
315315
func (c *CertificateClient) Delete(ctx context.Context, certificate *Certificate) (*Response, error) {
316-
req, err := c.client.NewRequest(ctx, "DELETE", fmt.Sprintf("/certificates/%d", certificate.ID), nil)
317-
if err != nil {
318-
return nil, err
319-
}
320-
return c.client.Do(req, nil)
316+
reqPath := fmt.Sprintf("/certificates/%d", certificate.ID)
317+
318+
return deleteRequestNoResult(ctx, c.client, reqPath)
321319
}
322320

323321
// RetryIssuance retries the issuance of a failed managed certificate.

hcloud/firewall.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -252,11 +252,9 @@ func (c *FirewallClient) Update(ctx context.Context, firewall *Firewall, opts Fi
252252

253253
// Delete deletes a Firewall.
254254
func (c *FirewallClient) Delete(ctx context.Context, firewall *Firewall) (*Response, error) {
255-
req, err := c.client.NewRequest(ctx, "DELETE", fmt.Sprintf("/firewalls/%d", firewall.ID), nil)
256-
if err != nil {
257-
return nil, err
258-
}
259-
return c.client.Do(req, nil)
255+
reqPath := fmt.Sprintf("/firewalls/%d", firewall.ID)
256+
257+
return deleteRequestNoResult(ctx, c.client, reqPath)
260258
}
261259

262260
// FirewallSetRulesOpts specifies options for setting rules of a Firewall.

hcloud/floating_ip.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -245,11 +245,9 @@ func (c *FloatingIPClient) Create(ctx context.Context, opts FloatingIPCreateOpts
245245

246246
// Delete deletes a Floating IP.
247247
func (c *FloatingIPClient) Delete(ctx context.Context, floatingIP *FloatingIP) (*Response, error) {
248-
req, err := c.client.NewRequest(ctx, "DELETE", fmt.Sprintf("/floating_ips/%d", floatingIP.ID), nil)
249-
if err != nil {
250-
return nil, err
251-
}
252-
return c.client.Do(req, nil)
248+
reqPath := fmt.Sprintf("/floating_ips/%d", floatingIP.ID)
249+
250+
return deleteRequestNoResult(ctx, c.client, reqPath)
253251
}
254252

255253
// FloatingIPUpdateOpts specifies options for updating a Floating IP.

hcloud/image.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -206,11 +206,9 @@ func (c *ImageClient) AllWithOpts(ctx context.Context, opts ImageListOpts) ([]*I
206206

207207
// Delete deletes an image.
208208
func (c *ImageClient) Delete(ctx context.Context, image *Image) (*Response, error) {
209-
req, err := c.client.NewRequest(ctx, "DELETE", fmt.Sprintf("/images/%d", image.ID), nil)
210-
if err != nil {
211-
return nil, err
212-
}
213-
return c.client.Do(req, nil)
209+
reqPath := fmt.Sprintf("/images/%d", image.ID)
210+
211+
return deleteRequestNoResult(ctx, c.client, reqPath)
214212
}
215213

216214
// ImageUpdateOpts specifies options for updating an image.

hcloud/load_balancer.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -464,11 +464,9 @@ func (c *LoadBalancerClient) Create(ctx context.Context, opts LoadBalancerCreate
464464

465465
// Delete deletes a Load Balancer.
466466
func (c *LoadBalancerClient) Delete(ctx context.Context, loadBalancer *LoadBalancer) (*Response, error) {
467-
req, err := c.client.NewRequest(ctx, "DELETE", fmt.Sprintf("/load_balancers/%d", loadBalancer.ID), nil)
468-
if err != nil {
469-
return nil, err
470-
}
471-
return c.client.Do(req, nil)
467+
reqPath := fmt.Sprintf("/load_balancers/%d", loadBalancer.ID)
468+
469+
return deleteRequestNoResult(ctx, c.client, reqPath)
472470
}
473471

474472
func (c *LoadBalancerClient) addTarget(ctx context.Context, loadBalancer *LoadBalancer, reqBody schema.LoadBalancerActionAddTargetRequest) (*Action, *Response, error) {

hcloud/network.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,11 +160,9 @@ func (c *NetworkClient) AllWithOpts(ctx context.Context, opts NetworkListOpts) (
160160

161161
// Delete deletes a network.
162162
func (c *NetworkClient) Delete(ctx context.Context, network *Network) (*Response, error) {
163-
req, err := c.client.NewRequest(ctx, "DELETE", fmt.Sprintf("/networks/%d", network.ID), nil)
164-
if err != nil {
165-
return nil, err
166-
}
167-
return c.client.Do(req, nil)
163+
reqPath := fmt.Sprintf("/networks/%d", network.ID)
164+
165+
return deleteRequestNoResult(ctx, c.client, reqPath)
168166
}
169167

170168
// NetworkUpdateOpts specifies options for updating a network.

hcloud/placement_group.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -207,9 +207,7 @@ func (c *PlacementGroupClient) Update(ctx context.Context, placementGroup *Place
207207

208208
// Delete deletes a PlacementGroup.
209209
func (c *PlacementGroupClient) Delete(ctx context.Context, placementGroup *PlacementGroup) (*Response, error) {
210-
req, err := c.client.NewRequest(ctx, "DELETE", fmt.Sprintf("/placement_groups/%d", placementGroup.ID), nil)
211-
if err != nil {
212-
return nil, err
213-
}
214-
return c.client.Do(req, nil)
210+
reqPath := fmt.Sprintf("/placement_groups/%d", placementGroup.ID)
211+
212+
return deleteRequestNoResult(ctx, c.client, reqPath)
215213
}

hcloud/primary_ip.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -282,11 +282,9 @@ func (c *PrimaryIPClient) Create(ctx context.Context, reqBody PrimaryIPCreateOpt
282282

283283
// Delete deletes a Primary IP.
284284
func (c *PrimaryIPClient) Delete(ctx context.Context, primaryIP *PrimaryIP) (*Response, error) {
285-
req, err := c.client.NewRequest(ctx, "DELETE", fmt.Sprintf("/primary_ips/%d", primaryIP.ID), nil)
286-
if err != nil {
287-
return nil, err
288-
}
289-
return c.client.Do(req, nil)
285+
reqPath := fmt.Sprintf("/primary_ips/%d", primaryIP.ID)
286+
287+
return deleteRequestNoResult(ctx, c.client, reqPath)
290288
}
291289

292290
// Update updates a Primary IP.

hcloud/server.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -441,20 +441,18 @@ func (c *ServerClient) Delete(ctx context.Context, server *Server) (*Response, e
441441

442442
// DeleteWithResult deletes a server and returns the parsed response containing the action.
443443
func (c *ServerClient) DeleteWithResult(ctx context.Context, server *Server) (*ServerDeleteResult, *Response, error) {
444-
req, err := c.client.NewRequest(ctx, "DELETE", fmt.Sprintf("/servers/%d", server.ID), nil)
445-
if err != nil {
446-
return &ServerDeleteResult{}, nil, err
447-
}
444+
result := &ServerDeleteResult{}
448445

449-
var respBody schema.ServerDeleteResponse
450-
resp, err := c.client.Do(req, &respBody)
446+
reqPath := fmt.Sprintf("/servers/%d", server.ID)
447+
448+
respBody, resp, err := deleteRequest[schema.ServerDeleteResponse](ctx, c.client, reqPath)
451449
if err != nil {
452-
return &ServerDeleteResult{}, resp, err
450+
return result, resp, err
453451
}
454452

455-
return &ServerDeleteResult{
456-
Action: ActionFromSchema(respBody.Action),
457-
}, resp, nil
453+
result.Action = ActionFromSchema(respBody.Action)
454+
455+
return result, resp, nil
458456
}
459457

460458
// ServerUpdateOpts specifies options for updating a server.

hcloud/ssh_key.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,9 @@ func (c *SSHKeyClient) Create(ctx context.Context, opts SSHKeyCreateOpts) (*SSHK
162162

163163
// Delete deletes a SSH key.
164164
func (c *SSHKeyClient) Delete(ctx context.Context, sshKey *SSHKey) (*Response, error) {
165-
req, err := c.client.NewRequest(ctx, "DELETE", fmt.Sprintf("/ssh_keys/%d", sshKey.ID), nil)
166-
if err != nil {
167-
return nil, err
168-
}
169-
return c.client.Do(req, nil)
165+
reqPath := fmt.Sprintf("/ssh_keys/%d", sshKey.ID)
166+
167+
return deleteRequestNoResult(ctx, c.client, reqPath)
170168
}
171169

172170
// SSHKeyUpdateOpts specifies options for updating a SSH key.

0 commit comments

Comments
 (0)