From 1396a7eeec186c7ab76da28bd92ca71654c2c4f5 Mon Sep 17 00:00:00 2001 From: Daishan Peng Date: Fri, 15 Dec 2017 16:48:36 -0700 Subject: [PATCH 1/3] multiple fixes Fixed link to catalog, versionLinks, category, refreshAction and hiding versions from templates --- api/catalog/filter.go | 47 +++++++++++++++++++++++++++++++++++++++++++ api/setup/setup.go | 14 +++++++++++-- 2 files changed, 59 insertions(+), 2 deletions(-) diff --git a/api/catalog/filter.go b/api/catalog/filter.go index 19d298b..6f972b9 100644 --- a/api/catalog/filter.go +++ b/api/catalog/filter.go @@ -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 CatalogFormatter(apiContext *types.APIContext, resource *types.RawResource) { + resource.Actions["refresh"] = apiContext.URLBuilder.Action("refresh", resource) +} + +func CataLogRefreshActionHandler(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 { diff --git a/api/setup/setup.go b/api/setup/setup.go index bd23e89..3154db7 100644 --- a/api/setup/setup.go +++ b/api/setup/setup.go @@ -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 { @@ -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.CatalogFormatter + schema.ActionHandler = catalog.CataLogRefreshActionHandler +} + func ClusterRegistrationTokens(schemas *types.Schemas) { schema := schemas.Schema(&managementschema.Version, client.ClusterRegistrationTokenType) schema.Formatter = func(request *types.APIContext, resource *types.RawResource) { @@ -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{ From d7721502099a0004cef02a8a0d9a317ef03ffb9c Mon Sep 17 00:00:00 2001 From: Daishan Peng Date: Sat, 16 Dec 2017 01:21:12 -0700 Subject: [PATCH 2/3] update vendor(have hack) --- vendor.conf | 4 ++-- vendor/github.com/rancher/norman/types/server_types.go | 1 + vendor/github.com/rancher/norman/urlbuilder/url.go | 4 ++++ .../types/apis/management.cattle.io/v3/catalog_types.go | 3 ++- .../client/management/v3/zz_generated_catalog_status.go | 8 +++++--- 5 files changed, 14 insertions(+), 6 deletions(-) diff --git a/vendor.conf b/vendor.conf index c26aea9..94d1049 100644 --- a/vendor.conf +++ b/vendor.conf @@ -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 diff --git a/vendor/github.com/rancher/norman/types/server_types.go b/vendor/github.com/rancher/norman/types/server_types.go index b52dd5f..8859a8d 100644 --- a/vendor/github.com/rancher/norman/types/server_types.go +++ b/vendor/github.com/rancher/norman/types/server_types.go @@ -149,6 +149,7 @@ type URLBuilder interface { SetSubContext(subContext string) FilterLink(schema *Schema, fieldName string, value string) string Action(action string, resource *RawResource) string + ResourceLinkByID(schema *Schema, id string) string } type Store interface { diff --git a/vendor/github.com/rancher/norman/urlbuilder/url.go b/vendor/github.com/rancher/norman/urlbuilder/url.go index 824de81..7645155 100644 --- a/vendor/github.com/rancher/norman/urlbuilder/url.go +++ b/vendor/github.com/rancher/norman/urlbuilder/url.go @@ -134,6 +134,10 @@ func (u *urlBuilder) FilterLink(schema *types.Schema, fieldName string, value st url.QueryEscape(fieldName) + "=" + url.QueryEscape(value) } +func (u *urlBuilder) ResourceLinkByID(schema *types.Schema, id string) string { + return u.constructBasicURL(schema.Version, schema.PluralName, id) +} + func (u *urlBuilder) constructBasicURL(version types.APIVersion, parts ...string) string { buffer := bytes.Buffer{} diff --git a/vendor/github.com/rancher/types/apis/management.cattle.io/v3/catalog_types.go b/vendor/github.com/rancher/types/apis/management.cattle.io/v3/catalog_types.go index 9d16d36..98a3972 100644 --- a/vendor/github.com/rancher/types/apis/management.cattle.io/v3/catalog_types.go +++ b/vendor/github.com/rancher/types/apis/management.cattle.io/v3/catalog_types.go @@ -22,7 +22,8 @@ type CatalogSpec struct { } type CatalogStatus struct { - Commit string `json:"commit,omitempty"` + LastRefreshTimestamp string `json:"lastRefreshTimestamp,omitempty"` + Commit string `json:"commit,omitempty"` } type Template struct { diff --git a/vendor/github.com/rancher/types/client/management/v3/zz_generated_catalog_status.go b/vendor/github.com/rancher/types/client/management/v3/zz_generated_catalog_status.go index a14dda6..6d043e4 100644 --- a/vendor/github.com/rancher/types/client/management/v3/zz_generated_catalog_status.go +++ b/vendor/github.com/rancher/types/client/management/v3/zz_generated_catalog_status.go @@ -1,10 +1,12 @@ package client const ( - CatalogStatusType = "catalogStatus" - CatalogStatusFieldCommit = "commit" + CatalogStatusType = "catalogStatus" + CatalogStatusFieldCommit = "commit" + CatalogStatusFieldLastRefreshTimestamp = "lastRefreshTimestamp" ) type CatalogStatus struct { - Commit string `json:"commit,omitempty"` + Commit string `json:"commit,omitempty"` + LastRefreshTimestamp string `json:"lastRefreshTimestamp,omitempty"` } From 3fe400210b67548ac57213eac356f80ae1e1c1e6 Mon Sep 17 00:00:00 2001 From: Daishan Peng Date: Sat, 16 Dec 2017 01:24:17 -0700 Subject: [PATCH 3/3] resolve golint --- api/catalog/filter.go | 4 ++-- api/setup/setup.go | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/api/catalog/filter.go b/api/catalog/filter.go index 6f972b9..e73b521 100644 --- a/api/catalog/filter.go +++ b/api/catalog/filter.go @@ -27,11 +27,11 @@ func TemplateFormatter(apiContext *types.APIContext, resource *types.RawResource resource.Links["catalog"] = apiContext.URLBuilder.ResourceLinkByID(catalogSchema, catalogName) } -func CatalogFormatter(apiContext *types.APIContext, resource *types.RawResource) { +func Formatter(apiContext *types.APIContext, resource *types.RawResource) { resource.Actions["refresh"] = apiContext.URLBuilder.Action("refresh", resource) } -func CataLogRefreshActionHandler(actionName string, action *types.Action, apiContext *types.APIContext) error { +func RefreshActionHandler(actionName string, action *types.Action, apiContext *types.APIContext) error { if actionName != "refresh" { return httperror.NewAPIError(httperror.NotFound, "not found") } diff --git a/api/setup/setup.go b/api/setup/setup.go index 3154db7..07b008b 100644 --- a/api/setup/setup.go +++ b/api/setup/setup.go @@ -65,8 +65,8 @@ func Catalog(schemas *types.Schemas) { schema.ResourceActions = map[string]types.Action{ "refresh": {}, } - schema.Formatter = catalog.CatalogFormatter - schema.ActionHandler = catalog.CataLogRefreshActionHandler + schema.Formatter = catalog.Formatter + schema.ActionHandler = catalog.RefreshActionHandler } func ClusterRegistrationTokens(schemas *types.Schemas) {