-
Notifications
You must be signed in to change notification settings - Fork 0
/
catalog.go
101 lines (91 loc) · 2.84 KB
/
catalog.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
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
}