37 Star 407 Fork 74

GVPrancher/rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
catalog.go 2.84 KB
一键复制 编辑 原始数据 按行查看 历史
Daishan Peng 提交于 2018-05-16 10:07 . rename catalog api files
package catalog
import (
"time"
"bytes"
"encoding/json"
"net/http"
"github.com/coreos/etcd/etcdserver/api/v3rpc/rpctypes"
"github.com/ghodss/yaml"
"github.com/rancher/norman/httperror"
"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"
)
func Formatter(apiContext *types.APIContext, resource *types.RawResource) {
resource.AddAction(apiContext, "refresh")
resource.Links["exportYaml"] = apiContext.URLBuilder.Link("exportYaml", resource)
}
func CollectionFormatter(request *types.APIContext, collection *types.GenericCollection) {
collection.AddAction(request, "refresh")
}
type ActionHandler struct {
CatalogClient v3.CatalogInterface
}
func (a ActionHandler) RefreshActionHandler(actionName string, action *types.Action, apiContext *types.APIContext) error {
if actionName != "refresh" {
return httperror.NewAPIError(httperror.NotFound, "not found")
}
catalogs := []v3.Catalog{}
if apiContext.ID != "" {
catalog, err := a.CatalogClient.Get(apiContext.ID, metav1.GetOptions{})
if err != nil {
return err
}
catalogs = append(catalogs, *catalog)
} else {
catalogList, err := a.CatalogClient.List(metav1.ListOptions{})
if err != nil {
return err
}
for _, catalog := range catalogList.Items {
catalogs = append(catalogs, catalog)
}
}
for _, catalog := range catalogs {
catalog.Status.LastRefreshTimestamp = time.Now().Format(time.RFC3339)
v3.CatalogConditionRefreshed.Unknown(&catalog)
if _, err := a.CatalogClient.Update(&catalog); err != nil {
return err
}
}
return nil
}
func (a ActionHandler) ExportYamlHandler(apiContext *types.APIContext, next types.RequestHandler) error {
switch apiContext.Link {
case "exportyaml":
catalog, err := a.CatalogClient.Get(apiContext.ID, metav1.GetOptions{})
if err != nil {
return rpctypes.ErrGRPCStopped
}
topkey := compose.Config{}
topkey.Version = "v3"
ca := client.Catalog{}
if err := convert.ToObj(catalog.Spec, &ca); err != nil {
return err
}
topkey.Catalogs = map[string]client.Catalog{}
topkey.Catalogs[catalog.Name] = ca
m, err := convert.EncodeToMap(topkey)
if err != nil {
return err
}
delete(m["catalogs"].(map[string]interface{})[catalog.Name].(map[string]interface{}), "actions")
delete(m["catalogs"].(map[string]interface{})[catalog.Name].(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
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.0.15-rc5

搜索帮助