37 Star 411 Fork 76

GVPrancher/rancher

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
template.go 3.88 KB
一键复制 编辑 原始数据 按行查看 历史
Dan Ramich 提交于 2019-06-12 13:38 . goimport linting changes
package catalog
import (
"bytes"
"net/http"
"strings"
"time"
"github.com/rancher/norman/api/access"
"github.com/rancher/norman/httperror"
"github.com/rancher/norman/types"
helmlib "github.com/rancher/rancher/pkg/catalog/helm"
v3 "github.com/rancher/types/apis/management.cattle.io/v3"
managementschema "github.com/rancher/types/apis/management.cattle.io/v3/schema"
client "github.com/rancher/types/client/management/v3"
)
func TemplateFormatter(apiContext *types.APIContext, resource *types.RawResource) {
var prjCatalogName, clusterCatalogName string
// version links
resource.Values["versionLinks"] = extractVersionLinks(apiContext, resource)
//icon
delete(resource.Values, "icon")
resource.Links["icon"] = apiContext.URLBuilder.Link("icon", resource)
val := resource.Values
if val[client.CatalogTemplateFieldCatalogID] != nil {
//catalog link
catalogSchema := apiContext.Schemas.Schema(&managementschema.Version, client.CatalogType)
catalogName := strings.Split(resource.ID, "-")[0]
resource.Links["catalog"] = apiContext.URLBuilder.ResourceLinkByID(catalogSchema, catalogName)
}
if val[client.CatalogTemplateFieldProjectCatalogID] != nil {
prjCatID, ok := val[client.CatalogTemplateFieldProjectCatalogID].(string)
if ok {
prjCatalogName = prjCatID
}
//project catalog link
prjCatalogSchema := apiContext.Schemas.Schema(&managementschema.Version, client.ProjectCatalogType)
resource.Links["projectCatalog"] = apiContext.URLBuilder.ResourceLinkByID(prjCatalogSchema, prjCatalogName)
}
if val[client.CatalogTemplateFieldClusterCatalogID] != nil {
clusterCatID, ok := val[client.CatalogTemplateFieldClusterCatalogID].(string)
if ok {
clusterCatalogName = clusterCatID
}
//cluster catalog link
clCatalogSchema := apiContext.Schemas.Schema(&managementschema.Version, client.ClusterCatalogType)
resource.Links["clusterCatalog"] = apiContext.URLBuilder.ResourceLinkByID(clCatalogSchema, clusterCatalogName)
}
// delete category
delete(resource.Values, "category")
// delete versions
delete(resource.Values, "versions")
}
type TemplateWrapper struct {
CatalogLister v3.CatalogLister
ClusterCatalogLister v3.ClusterCatalogLister
ProjectCatalogLister v3.ProjectCatalogLister
}
func (t TemplateWrapper) TemplateIconHandler(apiContext *types.APIContext, next types.RequestHandler) error {
switch apiContext.Link {
case "icon":
template := &client.Template{}
if err := access.ByID(apiContext, apiContext.Version, apiContext.Type, apiContext.ID, template); err != nil {
return err
}
if template.Icon == "" {
http.Error(apiContext.Response, "", http.StatusNoContent)
return nil
}
var (
catalogType string
catalogName string
iconBytes []byte
err error
)
if template.CatalogID != "" {
catalogType = client.CatalogType
catalogName = template.CatalogID
} else if template.ClusterCatalogID != "" {
catalogType = client.ClusterCatalogType
catalogName = template.ClusterCatalogID
} else if template.ProjectCatalogID != "" {
catalogType = client.ProjectCatalogType
catalogName = template.ProjectCatalogID
}
namespace, name := helmlib.SplitNamespaceAndName(catalogName)
catalog, err := helmlib.GetCatalog(catalogType, namespace, name, t.CatalogLister, t.ClusterCatalogLister, t.ProjectCatalogLister)
if err != nil {
return err
}
helm, err := helmlib.New(catalog)
if err != nil {
return err
}
iconBytes, err = helm.LoadIcon(template.IconFilename, template.Icon)
if err != nil {
return err
}
t, err := time.Parse(time.RFC3339, template.Created)
if err != nil {
return err
}
iconReader := bytes.NewReader(iconBytes)
apiContext.Response.Header().Set("Cache-Control", "private, max-age=604800")
http.ServeContent(apiContext.Response, apiContext.Request, template.IconFilename, t, iconReader)
return nil
default:
return httperror.NewAPIError(httperror.NotFound, "not found")
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/rancher/rancher.git
git@gitee.com:rancher/rancher.git
rancher
rancher
rancher
v2.2.5

搜索帮助

0d507c66 1850385 C8b1a773 1850385