forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
actionhandler.go
48 lines (38 loc) · 1.42 KB
/
actionhandler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package kontainerdriver
import (
"github.com/rancher/norman/httperror"
"github.com/rancher/norman/types"
v3 "github.com/rancher/types/apis/management.cattle.io/v3"
)
type ActionHandler struct {
KontainerDrivers v3.KontainerDriverInterface
KontainerDriverLister v3.KontainerDriverLister
}
func (a ActionHandler) ActionHandler(actionName string, action *types.Action, apiContext *types.APIContext) error {
// passing nil as the resource only works because just namespace is grabbed from it and nodedriver is not namespaced
if err := apiContext.AccessControl.CanDo(v3.KontainerDriverGroupVersionKind.Group, v3.KontainerDriverResource.Name, "update", apiContext, nil, apiContext.Schema); err != nil {
return err
}
switch actionName {
case "activate":
return a.activate(apiContext)
case "deactivate":
return a.deactivate(apiContext)
}
return httperror.NewAPIError(httperror.NotFound, "not found")
}
func (a ActionHandler) activate(apiContext *types.APIContext) error {
return a.setDriverActiveStatus(apiContext, true)
}
func (a ActionHandler) deactivate(apiContext *types.APIContext) error {
return a.setDriverActiveStatus(apiContext, false)
}
func (a ActionHandler) setDriverActiveStatus(apiContext *types.APIContext, status bool) error {
driver, err := a.KontainerDriverLister.Get("", apiContext.ID)
if err != nil {
return err
}
driver.Spec.Active = status
_, err = a.KontainerDrivers.Update(driver)
return err
}