forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
link.go
68 lines (53 loc) · 1.7 KB
/
link.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
package yaml
import (
"fmt"
"net/http"
"github.com/rancher/norman/httperror"
"github.com/rancher/norman/types"
"github.com/rancher/rancher/pkg/clustermanager"
)
func NewLinkHandler(proxy http.Handler, manager *clustermanager.Manager, next types.RequestHandler) types.RequestHandler {
lh := &yamlLinkHandler{
Proxy: proxy,
ClusterManager: manager,
next: next,
}
return lh.LinkHandler
}
type yamlLinkHandler struct {
Proxy http.Handler
ClusterManager *clustermanager.Manager
next types.RequestHandler
}
func (s *yamlLinkHandler) callNext(apiContext *types.APIContext, next types.RequestHandler) error {
if s.next != nil {
return s.next(apiContext, next)
} else if next != nil {
return next(apiContext, nil)
}
return httperror.NewAPIError(httperror.NotFound, "link not found")
}
func (s *yamlLinkHandler) LinkHandler(apiContext *types.APIContext, next types.RequestHandler) error {
if apiContext.Link != "yaml" {
return s.callNext(apiContext, next)
}
clusterName := s.ClusterManager.ClusterName(apiContext)
if clusterName == "" {
return httperror.NewAPIError(httperror.NotFound, "cluster not found")
}
schema := apiContext.Schemas.Schema(apiContext.Version, apiContext.Type)
if schema == nil {
return fmt.Errorf("failed to find schema " + apiContext.Type)
}
data, err := schema.Store.ByID(apiContext, schema, apiContext.ID)
if err != nil {
return err
}
link, _ := data[".selfLink"].(string)
if link == "" {
return httperror.NewAPIError(httperror.NotFound, "self link not found")
}
apiContext.Request.URL.Path = fmt.Sprintf("/k8s/clusters/%s%s", clusterName, link)
s.Proxy.ServeHTTP(apiContext.Response, apiContext.Request)
return nil
}