Skip to content

Commit b7a8fcf

Browse files
authored
Merge pull request #464 from Tinyblargon/ha-delete
Feat: HaResource delete
2 parents fe036ac + 2b57066 commit b7a8fcf

File tree

8 files changed

+81
-32
lines changed

8 files changed

+81
-32
lines changed

proxmox/client__api.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
// in the future we might put the interface even lower, but for now this is sufficient
1010
type clientApiInterface interface {
1111
createHaRule(ctx context.Context, params map[string]any) error
12+
deleteHaResource(ctx context.Context, id GuestID) error
1213
deleteHaRule(ctx context.Context, id HaRuleID) error
1314
getGuestConfig(ctx context.Context, vmr *VmRef) (map[string]any, error)
1415
getGuestPendingChanges(ctx context.Context, vmr *VmRef) ([]any, error)
@@ -33,6 +34,10 @@ func (c *clientAPI) createHaRule(ctx context.Context, params map[string]any) err
3334
return c.post(ctx, "/cluster/ha/rules", params)
3435
}
3536

37+
func (c *clientAPI) deleteHaResource(ctx context.Context, id GuestID) error {
38+
return c.delete(ctx, "/cluster/ha/resources/"+id.String())
39+
}
40+
3641
func (c *clientAPI) deleteHaRule(ctx context.Context, id HaRuleID) error {
3742
return c.delete(ctx, "/cluster/ha/rules/"+id.String())
3843
}

proxmox/client__api__mock.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
type mockClientAPI struct {
88
createHaRuleFunc func(ctx context.Context, params map[string]any) error
9+
deleteHaResourceFunc func(ctx context.Context, id GuestID) error
910
deleteHaRuleFunc func(ctx context.Context, id HaRuleID) error
1011
getGuestConfigFunc func(ctx context.Context, vmr *VmRef) (map[string]any, error)
1112
getGuestPendingChangesFunc func(ctx context.Context, vmr *VmRef) ([]any, error)
@@ -30,6 +31,13 @@ func (m *mockClientAPI) createHaRule(ctx context.Context, params map[string]any)
3031
return m.createHaRuleFunc(ctx, params)
3132
}
3233

34+
func (m *mockClientAPI) deleteHaResource(ctx context.Context, id GuestID) error {
35+
if m.deleteHaResourceFunc == nil {
36+
m.panic("deleteHaResourceFunc")
37+
}
38+
return m.deleteHaResourceFunc(ctx, id)
39+
}
40+
3341
func (m *mockClientAPI) deleteHaRule(ctx context.Context, id HaRuleID) error {
3442
if m.deleteHaRuleFunc == nil {
3543
m.panic("deleteHaRuleFunc")

proxmox/client__new.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ type ClientNew interface {
2727
haCreateNodeAffinityRuleNoCheck(ctx context.Context, ha HaNodeAffinityRule) error
2828
haCreateResourceAffinityRule(ctx context.Context, ha HaResourceAffinityRule) error
2929
haCreateResourceAffinityRuleNoCheck(ctx context.Context, ha HaResourceAffinityRule) error
30+
haDeleteResource(ctx context.Context, id GuestID) error
3031
haDeleteRule(ctx context.Context, id HaRuleID) error
3132
haDeleteRuleNoCheck(ctx context.Context, id HaRuleID) error
3233
haGetRule(ctx context.Context, id HaRuleID) (HaRule, error)

proxmox/client__new__mock.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ type MockClient struct {
1616
HaCreateNodeAffinityRuleNoCheckFunc func(ctx context.Context, ha HaNodeAffinityRule) error
1717
HaCreateResourceAffinityRuleFunc func(ctx context.Context, ha HaResourceAffinityRule) error
1818
HaCreateResourceAffinityRuleNoCheckFunc func(ctx context.Context, ha HaResourceAffinityRule) error
19+
HaDeleteResourceFunc func(ctx context.Context, id GuestID) error
1920
HaDeleteRuleFunc func(ctx context.Context, id HaRuleID) error
2021
HaDeleteRuleNoCheckFunc func(ctx context.Context, id HaRuleID) error
2122
HaGetRuleFunc func(ctx context.Context, id HaRuleID) (HaRule, error)
@@ -117,6 +118,13 @@ func (m *MockClient) haCreateResourceAffinityRuleNoCheck(ctx context.Context, ha
117118
return m.HaCreateResourceAffinityRuleNoCheckFunc(ctx, ha)
118119
}
119120

121+
func (m *MockClient) haDeleteResource(ctx context.Context, id GuestID) error {
122+
if m.HaDeleteResourceFunc == nil {
123+
m.panic("HaDeleteResourceFunc")
124+
}
125+
return m.HaDeleteResourceFunc(ctx, id)
126+
}
127+
120128
func (m *MockClient) haDeleteRule(ctx context.Context, id HaRuleID) error {
121129
if m.HaDeleteRuleFunc == nil {
122130
m.panic("HaDeleteRuleFunc")

proxmox/config__guest.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,29 @@ const (
233233
GuestIdMinimum = 100
234234
)
235235

236+
func (id GuestID) DeleteHaResource(ctx context.Context, c *Client) error {
237+
err := id.Validate()
238+
if err != nil {
239+
return err
240+
}
241+
return c.new().haDeleteResource(ctx, id)
242+
}
243+
244+
func (c *clientNew) haDeleteResource(ctx context.Context, id GuestID) error {
245+
return id.deleteHaResource(ctx, c.apiGet())
246+
}
247+
248+
func (id GuestID) deleteHaResource(ctx context.Context, c clientApiInterface) error {
249+
err := c.deleteHaResource(ctx, id)
250+
if err != nil {
251+
if strings.HasPrefix(err.Error(), "500 cannot delete service") {
252+
return Error.haResourceDoesNotExist(id)
253+
}
254+
return err
255+
}
256+
return nil
257+
}
258+
236259
func (id GuestID) errorContext() string {
237260
return "ID " + id.String()
238261
}

proxmox/config__ha__rules.go

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -211,26 +211,24 @@ func (r *rawHaNodeAffinityRule) GetStrict() bool {
211211

212212
func (r *rawHaNodeAffinityRule) get() *HaNodeAffinityRule {
213213
return &HaNodeAffinityRule{
214-
Comment: util.Pointer(r.GetComment()),
215-
Enabled: util.Pointer(r.GetEnabled()),
216-
Guests: util.Pointer(r.GetGuests()),
217-
ID: r.GetID(),
218-
Nodes: util.Pointer(r.GetNodes()),
219-
Strict: util.Pointer(r.GetStrict()),
220-
rawDigest: r.getDigest()}
214+
Comment: util.Pointer(r.GetComment()),
215+
Enabled: util.Pointer(r.GetEnabled()),
216+
Guests: util.Pointer(r.GetGuests()),
217+
ID: r.GetID(),
218+
Nodes: util.Pointer(r.GetNodes()),
219+
Strict: util.Pointer(r.GetStrict())}
221220
}
222221

223222
func (r *rawHaNodeAffinityRule) getDigest() digest { return haGetDigest(r.a) }
224223

225224
type HaNodeAffinityRule struct {
226-
Comment *string `json:"comment,omitempty"` // Never nil when returned
227-
Digest [sha1.Size]byte `json:"digest,omitempty"` // only returned.
228-
Enabled *bool `json:"enabled,omitempty"` // Never nil when returned
229-
Guests *[]VmRef `json:"guests,omitempty"` // Never nil when returned
230-
ID HaRuleID `json:"id"`
231-
Nodes *[]HaNode `json:"nodes,omitempty"` // Never nil when returned
232-
Strict *bool `json:"strict,omitempty"` // Never nil when returned
233-
rawDigest digest `json:"-"`
225+
Comment *string `json:"comment,omitempty"` // Never nil when returned
226+
Digest [sha1.Size]byte `json:"digest,omitempty"` // only returned.
227+
Enabled *bool `json:"enabled,omitempty"` // Never nil when returned
228+
Guests *[]VmRef `json:"guests,omitempty"` // Never nil when returned
229+
ID HaRuleID `json:"id"`
230+
Nodes *[]HaNode `json:"nodes,omitempty"` // Never nil when returned
231+
Strict *bool `json:"strict,omitempty"` // Never nil when returned
234232
}
235233

236234
const (
@@ -477,12 +475,11 @@ func (r *rawHaResourceAffinityRule) GetID() HaRuleID { return haGetID(r.a) }
477475

478476
func (r *rawHaResourceAffinityRule) get() *HaResourceAffinityRule {
479477
return &HaResourceAffinityRule{
480-
Affinity: util.Pointer(r.GetAffinity()),
481-
Comment: util.Pointer(r.GetComment()),
482-
Enabled: util.Pointer(r.GetEnabled()),
483-
Guests: util.Pointer(r.GetGuests()),
484-
ID: r.GetID(),
485-
rawDigest: r.getDigest()}
478+
Affinity: util.Pointer(r.GetAffinity()),
479+
Comment: util.Pointer(r.GetComment()),
480+
Enabled: util.Pointer(r.GetEnabled()),
481+
Guests: util.Pointer(r.GetGuests()),
482+
ID: r.GetID()}
486483
}
487484

488485
func (r *rawHaResourceAffinityRule) getDigest() digest { return haGetDigest(r.a) }
@@ -492,13 +489,12 @@ const (
492489
)
493490

494491
type HaResourceAffinityRule struct {
495-
Affinity *HaAffinity `json:"affinity,omitempty"` // Never nil when returned
496-
Comment *string `json:"comment,omitempty"` // Never nil when returned
497-
Digest [sha1.Size]byte `json:"digest,omitempty"` // only returned.
498-
Enabled *bool `json:"enabled,omitempty"` // Never nil when returned
499-
Guests *[]VmRef `json:"guests,omitempty"` // Never nil when returned
500-
ID HaRuleID `json:"id"`
501-
rawDigest digest `json:"-"`
492+
Affinity *HaAffinity `json:"affinity,omitempty"` // Never nil when returned
493+
Comment *string `json:"comment,omitempty"` // Never nil when returned
494+
Digest [sha1.Size]byte `json:"digest,omitempty"` // only returned.
495+
Enabled *bool `json:"enabled,omitempty"` // Never nil when returned
496+
Guests *[]VmRef `json:"guests,omitempty"` // Never nil when returned
497+
ID HaRuleID `json:"id"`
502498
}
503499

504500
const (

proxmox/config__ha__rules_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -574,8 +574,7 @@ func Test_HaNodeAffinityRule_Get(t *testing.T) {
574574
"digest": string("ebefeede3059417444308d2e58d2a5e504fe6151")},
575575
outputPublic: baseRule(HaNodeAffinityRule{
576576
Digest: [20]byte{0xeb, 0xef, 0xee, 0xde, 0x30, 0x59, 0x41, 0x74, 0x44, 0x30, 0x8d, 0x2e, 0x58, 0xd2, 0xa5, 0xe5, 0x04, 0xfe, 0x61, 0x51}}),
577-
outputPrivate: util.Pointer(baseRule(HaNodeAffinityRule{
578-
rawDigest: "ebefeede3059417444308d2e58d2a5e504fe6151"}))}}},
577+
outputPrivate: util.Pointer(baseRule(HaNodeAffinityRule{}))}}},
579578
{category: `Enabled`,
580579
tests: []test{
581580
{name: "false float",
@@ -1246,8 +1245,7 @@ func Test_HaResourceAffinityRule_Get(t *testing.T) {
12461245
"digest": string("ebefeede3059417444308d2e58d2a5e504fe6151")},
12471246
outputPublic: baseRule(HaResourceAffinityRule{
12481247
Digest: [20]byte{0xeb, 0xef, 0xee, 0xde, 0x30, 0x59, 0x41, 0x74, 0x44, 0x30, 0x8d, 0x2e, 0x58, 0xd2, 0xa5, 0xe5, 0x04, 0xfe, 0x61, 0x51}}),
1249-
outputPrivate: util.Pointer(baseRule(HaResourceAffinityRule{
1250-
rawDigest: "ebefeede3059417444308d2e58d2a5e504fe6151"}))}}},
1248+
outputPrivate: util.Pointer(baseRule(HaResourceAffinityRule{}))}}},
12511249
{category: `Enabled`,
12521250
tests: []test{
12531251
{name: "false float",

proxmox/error.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,13 @@ func (errorMsg) guestIsProtectedCantDelete(id GuestID) error {
4040
err: Error.GuestIsProtectedCantDelete(),
4141
id: id}
4242
}
43+
44+
var errGuestNotHaManaged = errors.New("guest is not ha managed")
45+
46+
func (msg errorMsg) HaResourceDoesNotExist() error { return errGuestNotHaManaged }
47+
48+
func (errorMsg) haResourceDoesNotExist(id GuestID) error {
49+
return &errorWrapper[GuestID]{
50+
err: Error.HaResourceDoesNotExist(),
51+
id: id}
52+
}

0 commit comments

Comments
 (0)