-
Notifications
You must be signed in to change notification settings - Fork 0
/
template.go
126 lines (108 loc) · 3.87 KB
/
template.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
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"
"github.com/rancher/types/apis/management.cattle.io/v3"
managementschema "github.com/rancher/types/apis/management.cattle.io/v3/schema"
"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")
}
}