37 Star 411 Fork 76

GVPrancher/rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
node_driver.go 3.17 KB
一键复制 编辑 原始数据 按行查看 历史
Dan Ramich 提交于 2018-06-20 15:26 . Update node access pieces
package node
import (
"bytes"
"net/http"
"time"
"encoding/json"
"github.com/ghodss/yaml"
"github.com/rancher/norman/api/access"
"github.com/rancher/norman/types"
"github.com/rancher/norman/types/convert"
"github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/rancher/types/client/management/v3"
"github.com/rancher/types/compose"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
const (
configKey = "extractedConfig"
)
type DriverHandlers struct {
NodeDriverClient v3.NodeDriverInterface
}
func (h *DriverHandlers) ActionHandler(actionName string, action *types.Action, apiContext *types.APIContext) error {
m, err := h.NodeDriverClient.GetNamespaced("", apiContext.ID, metav1.GetOptions{})
if err != nil {
return err
}
// 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.NodeDriverGroupVersionKind.Group, v3.NodeDriverResource.Name, "update", apiContext, nil, apiContext.Schema); err != nil {
return err
}
switch actionName {
case "activate":
m.Spec.Active = true
v3.NodeDriverConditionActive.Unknown(m)
case "deactivate":
m.Spec.Active = false
v3.NodeDriverConditionInactive.Unknown(m)
}
_, err = h.NodeDriverClient.Update(m)
if err != nil {
return err
}
data := map[string]interface{}{}
if err := access.ByID(apiContext, apiContext.Version, apiContext.Type, apiContext.ID, &data); err != nil {
return err
}
apiContext.WriteResponse(http.StatusOK, data)
return nil
}
// Formatter for NodeDriver
func (h *DriverHandlers) Formatter(apiContext *types.APIContext, resource *types.RawResource) {
if err := apiContext.AccessControl.CanDo(v3.NodeDriverGroupVersionKind.Group, v3.NodeDriverResource.Name, "update", apiContext, resource.Values, apiContext.Schema); err == nil {
resource.AddAction(apiContext, "activate")
resource.AddAction(apiContext, "deactivate")
}
resource.Links["exportYaml"] = apiContext.URLBuilder.Link("exportYaml", resource)
}
func (h DriverHandlers) ExportYamlHandler(apiContext *types.APIContext, next types.RequestHandler) error {
switch apiContext.Link {
case "exportyaml":
nodeDriver, err := h.NodeDriverClient.Get(apiContext.ID, metav1.GetOptions{})
if err != nil {
return err
}
topkey := compose.Config{}
topkey.Version = "v3"
nd := client.NodeDriver{}
if err := convert.ToObj(nodeDriver.Spec, &nd); err != nil {
return err
}
topkey.NodeDrivers = map[string]client.NodeDriver{}
topkey.NodeDrivers[nodeDriver.Spec.DisplayName] = nd
m, err := convert.EncodeToMap(topkey)
if err != nil {
return err
}
delete(m["nodeDrivers"].(map[string]interface{})[nodeDriver.Spec.DisplayName].(map[string]interface{}), "actions")
delete(m["nodeDrivers"].(map[string]interface{})[nodeDriver.Spec.DisplayName].(map[string]interface{}), "links")
data, err := json.Marshal(m)
if err != nil {
return err
}
buf, err := yaml.JSONToYAML(data)
if err != nil {
return err
}
reader := bytes.NewReader(buf)
apiContext.Response.Header().Set("Content-Type", "text/yaml")
http.ServeContent(apiContext.Response, apiContext.Request, "exportYaml", time.Now(), reader)
return nil
}
return nil
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.1.2-rc3

搜索帮助

0d507c66 1850385 C8b1a773 1850385