-
Notifications
You must be signed in to change notification settings - Fork 0
/
cluster_template.go
59 lines (54 loc) · 2.18 KB
/
cluster_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
package clustertemplate
import (
"net/http"
"sort"
"time"
"github.com/rancher/norman/api/access"
"github.com/rancher/norman/types"
"github.com/rancher/norman/types/convert"
"github.com/rancher/norman/types/values"
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"
"github.com/sirupsen/logrus"
)
type Wrapper struct {
ClusterTemplates v3.ClusterTemplateInterface
ClusterTemplateLister v3.ClusterTemplateLister
ClusterTemplateRevisionLister v3.ClusterTemplateRevisionLister
ClusterTemplateRevisions v3.ClusterTemplateRevisionInterface
}
func (w Wrapper) Formatter(apiContext *types.APIContext, resource *types.RawResource) {
resource.Links["revisions"] = apiContext.URLBuilder.Link("revisions", resource)
}
func (w Wrapper) LinkHandler(apiContext *types.APIContext, next types.RequestHandler) error {
switch apiContext.Link {
case "revisions":
var template client.ClusterTemplate
if err := access.ByID(apiContext, &managementschema.Version, client.ClusterTemplateType, apiContext.ID, &template); err != nil {
return err
}
conditions := []*types.QueryCondition{
types.NewConditionFromString(client.ClusterTemplateRevisionFieldClusterTemplateID, types.ModifierEQ, []string{template.ID}...),
}
var templateVersions []map[string]interface{}
if err := access.List(apiContext, &managementschema.Version, client.ClusterTemplateRevisionType, &types.QueryOptions{Conditions: conditions}, &templateVersions); err != nil {
return err
}
sort.SliceStable(templateVersions, func(i, j int) bool {
val1, err := time.Parse(time.RFC3339, convert.ToString(values.GetValueN(templateVersions[i], "created")))
if err != nil {
logrus.Infof("error parsing time %v", err)
}
val2, err := time.Parse(time.RFC3339, convert.ToString(values.GetValueN(templateVersions[j], "created")))
if err != nil {
logrus.Infof("error parsing time %v", err)
}
return val1.After(val2)
})
apiContext.Type = client.ClusterTemplateRevisionType
apiContext.WriteResponse(http.StatusOK, templateVersions)
return nil
}
return nil
}