Skip to content
This repository has been archived by the owner on Jan 27, 2018. It is now read-only.

catalog fix #2

Merged
merged 3 commits into from
Dec 16, 2017
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
47 changes: 47 additions & 0 deletions api/catalog/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,62 @@ import (
"net/http"
"time"

"fmt"

"strings"

"github.com/rancher/norman/api/access"
"github.com/rancher/norman/httperror"
"github.com/rancher/norman/types"
managementschema "github.com/rancher/types/apis/management.cattle.io/v3/schema"
"github.com/rancher/types/client/management/v3"
)

func TemplateFormatter(apiContext *types.APIContext, resource *types.RawResource) {
delete(resource.Values, "icon")
resource.Values["versionLinks"] = extractVersionLinks(apiContext, resource)
delete(resource.Values, "versions")
resource.Links["icon"] = apiContext.URLBuilder.Link("icon", resource)
catalogSchema := apiContext.Schemas.Schema(&managementschema.Version, client.CatalogType)
catalogName := strings.Split(resource.ID, "-")[0]
resource.Links["catalog"] = apiContext.URLBuilder.ResourceLinkByID(catalogSchema, catalogName)
}

func Formatter(apiContext *types.APIContext, resource *types.RawResource) {
resource.Actions["refresh"] = apiContext.URLBuilder.Action("refresh", resource)
}

func RefreshActionHandler(actionName string, action *types.Action, apiContext *types.APIContext) error {
if actionName != "refresh" {
return httperror.NewAPIError(httperror.NotFound, "not found")
}

store := apiContext.Schema.Store

data, err := store.ByID(apiContext, apiContext.Schema, apiContext.ID)
if err != nil {
return err
}
data["lastRefreshTimestamp"] = time.Now().Format(time.RFC3339)

_, err = store.Update(apiContext, apiContext.Schema, data, apiContext.ID)
if err != nil {
return err
}
return nil
}

func extractVersionLinks(apiContext *types.APIContext, resource *types.RawResource) map[string]string {
schema := apiContext.Schemas.Schema(&managementschema.Version, client.TemplateVersionType)
versionMap := resource.Values["versions"].([]interface{})
r := map[string]string{}
for _, version := range versionMap {
revision := version.(map[string]interface{})["revision"].(int64)
version := version.(map[string]interface{})["version"].(string)
versionID := fmt.Sprintf("%v-%v", resource.ID, revision)
r[version] = apiContext.URLBuilder.ResourceLinkByID(schema, versionID)
}
return r
}

func TemplateIconHandler(apiContext *types.APIContext) error {
Expand Down
14 changes: 12 additions & 2 deletions api/setup/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ func Schemas(ctx context.Context, management *config.ManagementContext, schemas
ProjectLinks(schemas, management)
Templates(schemas)
ClusterRegistrationTokens(schemas)
addUserAction(schemas)
User(schemas)
Catalog(schemas)

crdStore, err := crd.NewCRDStoreFromConfig(management.RESTConfig)
if err != nil {
Expand Down Expand Up @@ -59,6 +60,15 @@ func Templates(schemas *types.Schemas) {
schema.LinkHandler = catalog.TemplateIconHandler
}

func Catalog(schemas *types.Schemas) {
schema := schemas.Schema(&managementschema.Version, client.CatalogType)
schema.ResourceActions = map[string]types.Action{
"refresh": {},
}
schema.Formatter = catalog.Formatter
schema.ActionHandler = catalog.RefreshActionHandler
}

func ClusterRegistrationTokens(schemas *types.Schemas) {
schema := schemas.Schema(&managementschema.Version, client.ClusterRegistrationTokenType)
schema.Formatter = func(request *types.APIContext, resource *types.RawResource) {
Expand Down Expand Up @@ -92,7 +102,7 @@ func Subscribe(schemas *types.Schemas) {
})
}

func addUserAction(schemas *types.Schemas) {
func User(schemas *types.Schemas) {
schemas.MustImport(&managementschema.Version, authn.ChangePasswordInput{})
schema := schemas.Schema(&managementschema.Version, client.UserType)
schema.ResourceActions = map[string]types.Action{
Expand Down
4 changes: 2 additions & 2 deletions vendor.conf
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ golang.org/x/sync/errgroup 450f422ab23cf9881c94e2db30cac0eb1b7cf80c
golang.org/x/crypto 94eea52f7b742c7cbe0b03b22f0c4c8631ece122

github.com/rancher/cluster-api d13b10e2485cbfb12336640be3945232efe341d7
github.com/rancher/norman f26502f7539957cbf8b8b5321a1fe0953d818e31
github.com/rancher/types dd15a686608d403f270ed6fba9201a04da0686e0
github.com/rancher/norman c7742ff2b440f47b609c392ecc03b8a0efcda20f https://github.com/StrongMonkey/norman.git
github.com/rancher/types dde4d8be3608507ae27c79e1393e193c8d77e3ad https://github.com/StrongMonkey/types-1.git

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions vendor/github.com/rancher/norman/urlbuilder/url.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.