Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add self link mapper #392

Merged
merged 1 commit into from
Feb 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions types/mapper/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ func NewObject(mappers ...types.Mapper) Object {
Mappers: append([]types.Mapper{
&APIGroup{},
&Embed{Field: "metadata"},
&SelfLink{},
&Embed{Field: "spec", Optional: true},
&ReadOnly{Field: "status", Optional: true, SubFields: true},
Drop{Field: "kind"},
Expand Down
51 changes: 51 additions & 0 deletions types/mapper/selflink.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package mapper

import (
"strings"

"github.com/rancher/norman/types"
"github.com/rancher/wrangler/pkg/name"
)

type SelfLink struct {
resource string
}

func (s *SelfLink) FromInternal(data map[string]interface{}) {
sl, ok := data["selfLink"].(string)
if !ok || sl == "" {
data["selfLink"] = s.selflink(data)
}
}

func (s *SelfLink) ToInternal(data map[string]interface{}) error {
return nil
}

func (s *SelfLink) ModifySchema(schema *types.Schema, schemas *types.Schemas) error {
s.resource = name.GuessPluralName(schema.ID)
return nil
}

func (s *SelfLink) selflink(data map[string]interface{}) string {
buf := &strings.Builder{}
name := data["name"].(string)
apiVersion, ok := data["apiVersion"].(string)
if !ok || apiVersion == "v1" {
buf.WriteString("/api/v1/")
} else {
buf.WriteString("/apis/")
buf.WriteString(apiVersion)
buf.WriteString("/")
}
namespace, ok := data["namespace"].(string)
if !ok || namespace != "" {
buf.WriteString("namespaces/")
buf.WriteString(namespace)
buf.WriteString("/")
}
buf.WriteString(s.resource)
buf.WriteString("/")
buf.WriteString(name)
return buf.String()
}