Skip to content

Commit e26c2a3

Browse files
committed
update: refactor
1 parent 0564fb7 commit e26c2a3

1 file changed

Lines changed: 22 additions & 17 deletions

File tree

integration/apply.go

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ func (e *IPVSApplyError) Error() string {
1919
return fmt.Sprintf("Unable to apply new config: %s\nReason: %s", e.what, e.origErr)
2020
}
2121

22+
func isActionAllowed(actions ApplyActions, action ApplyActionType) bool {
23+
allowed, found := actions[action]
24+
return found && allowed
25+
}
26+
2227
// Apply compares new config to current config, builds a changeset and
2328
// applies the change set items within.
2429
func (ipvsconfig *IPVSConfig) Apply(newconfig *IPVSConfig, opts ApplyOpts) error {
@@ -34,7 +39,7 @@ func (ipvsconfig *IPVSConfig) Apply(newconfig *IPVSConfig, opts ApplyOpts) error
3439
return ipvsconfig.ApplyChangeSet(newconfig, cs, opts)
3540
}
3641

37-
// ApplyChangeSet takes a chhange set and applies all change items to
42+
// ApplyChangeSet takes a change set and applies all change items to
3843
// the given IPVSConfig
3944
func (ipvsconfig *IPVSConfig) ApplyChangeSet(newconfig *IPVSConfig, cs *ChangeSet, opts ApplyOpts) error {
4045

@@ -48,44 +53,40 @@ func (ipvsconfig *IPVSConfig) ApplyChangeSet(newconfig *IPVSConfig, cs *ChangeSe
4853

4954
// check before hand wether all change set items are covered within allowedActions
5055
for _, csiIntf := range cs.Items {
51-
csi := csiIntf.(ChangeSetItem)
56+
csi, ok := csiIntf.(ChangeSetItem)
57+
if !ok {
58+
return fmt.Errorf("invalid item in change set: %v", csiIntf)
59+
}
5260

5361
switch csi.Type {
5462
case DeleteService:
55-
allowed, found := allowedActions[ApplyActionDeleteService]
56-
if !found || !allowed {
63+
if !isActionAllowed(allowedActions, ApplyActionDeleteService) {
5764
return &IPVSApplyError{what: "not allowed to delete a service"}
5865
}
5966
case AddService:
60-
allowed, found := allowedActions[ApplyActionAddService]
61-
if !found || !allowed {
67+
if !isActionAllowed(allowedActions, ApplyActionAddService) {
6268
return &IPVSApplyError{what: "not allowed to add a service"}
6369
}
6470
// if service has destinations, check as well if allowed
6571
if len(csi.Service.Destinations) > 0 {
66-
allowed, found = allowedActions[ApplyActionAddDestination]
67-
if !found || !allowed {
72+
if !isActionAllowed(allowedActions, ApplyActionAddDestination) {
6873
return &IPVSApplyError{what: "not allowed to add a destinations"}
6974
}
7075
}
7176
case UpdateService:
72-
allowed, found := allowedActions[ApplyActionUpdateService]
73-
if !found || !allowed {
77+
if !isActionAllowed(allowedActions, ApplyActionUpdateService) {
7478
return &IPVSApplyError{what: "not allowed to update a service"}
7579
}
7680
case AddDestination:
77-
allowed, found := allowedActions[ApplyActionAddDestination]
78-
if !found || !allowed {
81+
if !isActionAllowed(allowedActions, ApplyActionAddDestination) {
7982
return &IPVSApplyError{what: "not allowed to add a destination"}
8083
}
8184
case DeleteDestination:
82-
allowed, found := allowedActions[ApplyActionDeleteDestination]
83-
if !found || !allowed {
85+
if !isActionAllowed(allowedActions, ApplyActionDeleteDestination) {
8486
return &IPVSApplyError{what: "not allowed to delete a destination"}
8587
}
8688
case UpdateDestination:
87-
allowed, found := allowedActions[ApplyActionUpdateDestination]
88-
if !found || !allowed {
89+
if !isActionAllowed(allowedActions, ApplyActionUpdateDestination) {
8990
return &IPVSApplyError{what: "not allowed to update a destination"}
9091
}
9192
default:
@@ -94,7 +95,11 @@ func (ipvsconfig *IPVSConfig) ApplyChangeSet(newconfig *IPVSConfig, cs *ChangeSe
9495
}
9596

9697
for _, csiIntf := range cs.Items {
97-
csi := csiIntf.(ChangeSetItem)
98+
csi, ok := csiIntf.(ChangeSetItem)
99+
if !ok {
100+
return fmt.Errorf("invalid item in change set: %v", csiIntf)
101+
}
102+
98103
ipvsconfig.log.Printf("Applying change set item %#v\n", csi)
99104

100105
switch csi.Type {

0 commit comments

Comments
 (0)