2019-08-13 23:36:03 +00:00
|
|
|
package common
|
2019-08-08 05:43:10 +00:00
|
|
|
|
|
|
|
import (
|
2020-02-14 23:20:04 +00:00
|
|
|
"strings"
|
|
|
|
|
2020-02-10 17:18:20 +00:00
|
|
|
"github.com/rancher/steve/pkg/accesscontrol"
|
2020-01-31 05:37:59 +00:00
|
|
|
"github.com/rancher/steve/pkg/schema"
|
|
|
|
"github.com/rancher/steve/pkg/schemaserver/types"
|
|
|
|
"github.com/rancher/steve/pkg/server/store/proxy"
|
2020-02-14 23:20:04 +00:00
|
|
|
"github.com/rancher/wrangler/pkg/data"
|
|
|
|
"github.com/rancher/wrangler/pkg/summary"
|
2020-01-31 05:37:59 +00:00
|
|
|
"k8s.io/apimachinery/pkg/api/meta"
|
2020-02-14 23:20:04 +00:00
|
|
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
2019-08-08 05:43:10 +00:00
|
|
|
)
|
|
|
|
|
2020-02-10 17:18:20 +00:00
|
|
|
func DefaultTemplate(clientGetter proxy.ClientGetter, asl accesscontrol.AccessSetLookup) schema.Template {
|
2020-01-31 05:37:59 +00:00
|
|
|
return schema.Template{
|
2020-02-10 17:18:20 +00:00
|
|
|
Store: proxy.NewProxyStore(clientGetter, asl),
|
2019-08-13 23:36:03 +00:00
|
|
|
Formatter: Formatter,
|
2020-01-31 05:37:59 +00:00
|
|
|
}
|
2019-08-13 23:36:03 +00:00
|
|
|
}
|
|
|
|
|
2020-06-11 04:18:22 +00:00
|
|
|
func DefaultFormatter(next types.Formatter) types.Formatter {
|
|
|
|
return types.FormatterChain(Formatter, next)
|
|
|
|
}
|
|
|
|
|
2019-08-08 05:43:10 +00:00
|
|
|
func Formatter(request *types.APIRequest, resource *types.RawResource) {
|
2020-01-31 05:37:59 +00:00
|
|
|
meta, err := meta.Accessor(resource.APIObject.Object)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
selfLink := meta.GetSelfLink()
|
2019-08-08 05:43:10 +00:00
|
|
|
if selfLink == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
u := request.URLBuilder.RelativeToRoot(selfLink)
|
|
|
|
resource.Links["view"] = u
|
|
|
|
|
2020-01-31 05:01:21 +00:00
|
|
|
if _, ok := resource.Links["update"]; !ok {
|
2019-08-08 05:43:10 +00:00
|
|
|
resource.Links["update"] = u
|
|
|
|
}
|
2020-02-14 23:20:04 +00:00
|
|
|
|
|
|
|
if unstr, ok := resource.APIObject.Object.(*unstructured.Unstructured); ok {
|
|
|
|
summary := summary.Summarize(unstr)
|
|
|
|
data.PutValue(unstr.Object, map[string]interface{}{
|
|
|
|
"name": summary.State,
|
|
|
|
"error": summary.Error,
|
|
|
|
"transitioning": summary.Transitioning,
|
|
|
|
"message": strings.Join(summary.Message, ":"),
|
|
|
|
}, "metadata", "state")
|
|
|
|
}
|
2019-08-08 05:43:10 +00:00
|
|
|
}
|