diff --git a/pkg/resources/formatter.go b/pkg/resources/formatter.go new file mode 100644 index 0000000..96768f2 --- /dev/null +++ b/pkg/resources/formatter.go @@ -0,0 +1,21 @@ +package resources + +import ( + "github.com/rancher/norman/pkg/types" + "github.com/rancher/norman/pkg/types/convert" + "github.com/rancher/norman/pkg/types/values" +) + +func Formatter(request *types.APIRequest, resource *types.RawResource) { + selfLink := convert.ToString(values.GetValueN(resource.Values, "metadata", "selfLink")) + if selfLink == "" { + return + } + + u := request.URLBuilder.RelativeToRoot(selfLink) + resource.Links["view"] = u + + if _, ok := resource.Links["update"]; ok { + resource.Links["update"] = u + } +} diff --git a/pkg/schemas/rbac.go b/pkg/schemas/rbac.go index 0caca2e..b3396d8 100644 --- a/pkg/schemas/rbac.go +++ b/pkg/schemas/rbac.go @@ -3,9 +3,8 @@ package schemas import ( "net/http" - "github.com/rancher/naok/pkg/attributes" - "github.com/rancher/naok/pkg/accesscontrol" + "github.com/rancher/naok/pkg/attributes" "github.com/rancher/norman/pkg/api/builtin" "github.com/rancher/norman/pkg/types" ) diff --git a/pkg/schemas/schemas.go b/pkg/schemas/schemas.go index 906c0fd..225b62e 100644 --- a/pkg/schemas/schemas.go +++ b/pkg/schemas/schemas.go @@ -11,6 +11,7 @@ import ( openapi_v2 "github.com/googleapis/gnostic/OpenAPIv2" "github.com/rancher/naok/pkg/accesscontrol" "github.com/rancher/naok/pkg/attributes" + "github.com/rancher/naok/pkg/resources" "github.com/rancher/norman/pkg/store/proxy" "github.com/rancher/norman/pkg/types" "github.com/rancher/norman/pkg/types/convert" @@ -32,6 +33,7 @@ type SchemaFactory interface { } type handler struct { + formatter types.Formatter schemas *schemas schemaStore types.Store client discovery.DiscoveryInterface @@ -69,6 +71,7 @@ func Register(ctx context.Context, clientGetter proxy.ClientGetter, discovery di apiService v1.APIServiceController) SchemaFactory { h := &handler{ + formatter: resources.Formatter, client: discovery, schemas: &schemas{}, schemaStore: proxy.NewProxyStore(clientGetter), @@ -182,6 +185,7 @@ func (h *handler) refresh(gv schema.GroupVersion, resources *metav1.APIResourceL schema.PluralName = resource.Name attributes.SetAPIResource(schema, resource) schema.Store = h.schemaStore + schema.Formatter = h.formatter h.schemas.schemas[schema.ID] = schema h.schemas.gvkToName[gvk] = schema.ID