From affda5d0486417cd43c75500eea200b506b089f7 Mon Sep 17 00:00:00 2001 From: dmitriy kalinin Date: Wed, 12 Aug 2015 20:17:27 -0700 Subject: [PATCH] always show latest versions of releases on init-* doc pages --- controllers/docs_controller.go | 60 ++++++++++++++++++++++++++++++++-- controllers/factory.go | 8 ++++- ui/release/known_releases.go | 58 ++++++++++++++++++++++++++++++++ ui/release/release.go | 12 ++----- 4 files changed, 125 insertions(+), 13 deletions(-) create mode 100644 ui/release/known_releases.go diff --git a/controllers/docs_controller.go b/controllers/docs_controller.go index 5d238163..14fa2851 100644 --- a/controllers/docs_controller.go +++ b/controllers/docs_controller.go @@ -6,11 +6,14 @@ import ( "regexp" "strings" + bosherr "github.com/cloudfoundry/bosh-agent/errors" boshlog "github.com/cloudfoundry/bosh-agent/logger" mart "github.com/go-martini/martini" martrend "github.com/martini-contrib/render" bhbibrepo "github.com/cppforlife/bosh-hub/bosh-init-bin/repo" + bhrelsrepo "github.com/cppforlife/bosh-hub/release/releasesrepo" + bhrelui "github.com/cppforlife/bosh-hub/ui/release" ) var ( @@ -20,7 +23,9 @@ var ( ) type DocsController struct { - boshInitBinsRepo bhbibrepo.Repository + releasesRepo bhrelsrepo.ReleasesRepository + releaseVersionsRepo bhrelsrepo.ReleaseVersionsRepository + boshInitBinsRepo bhbibrepo.Repository defaultTmpl string errorTmpl string @@ -29,9 +34,16 @@ type DocsController struct { logger boshlog.Logger } -func NewDocsController(boshInitBinsRepo bhbibrepo.Repository, logger boshlog.Logger) DocsController { +func NewDocsController( + releasesRepo bhrelsrepo.ReleasesRepository, + releaseVersionsRepo bhrelsrepo.ReleaseVersionsRepository, + boshInitBinsRepo bhbibrepo.Repository, + logger boshlog.Logger, +) DocsController { return DocsController{ - boshInitBinsRepo: boshInitBinsRepo, + releasesRepo: releasesRepo, + releaseVersionsRepo: releaseVersionsRepo, + boshInitBinsRepo: boshInitBinsRepo, defaultTmpl: "index", errorTmpl: "error", @@ -50,6 +62,11 @@ type installBoshInitPage struct { LatestBoshInitBinGroups []bhbibrepo.BinaryGroup } +type initManifestPage struct { + docPage + Releases []bhrelui.Release +} + func (c DocsController) Page(r martrend.Render, params mart.Params) { tmpl := strings.TrimSuffix(params["_1"], ".html") @@ -86,5 +103,42 @@ func (c DocsController) findPage(tmpl string) (interface{}, error) { return installBoshInitPage{docPage: page, LatestBoshInitBinGroups: binGroups}, nil } + // init- pages have an example manifest which lists latest releases + cpi, found := bhrelui.KnownCPIs.FindByDocPage(tmpl) + if found { + return newInitManifestPage(page, cpi, c.releasesRepo, c.releaseVersionsRepo) + } + + return page, nil +} + +func newInitManifestPage( + docPage docPage, + cpiRef bhrelui.ReleaseRef, + releasesRepo bhrelsrepo.ReleasesRepository, + releaseVersionsRepo bhrelsrepo.ReleaseVersionsRepository, +) (initManifestPage, error) { + page := initManifestPage{docPage: docPage} + + sources := []bhrelui.Source{bhrelui.BOSH.Source, cpiRef.Source} + + for _, source := range sources { + relVerRec, found, err := releasesRepo.FindLatest(source.Full()) + if err != nil { + return page, err + } else if !found { + return page, bosherr.New("Latest release '%s' is not found", source.Full()) + } + + rel, found, err := releaseVersionsRepo.Find(relVerRec) + if err != nil { + return page, err + } else if !found { + return page, bosherr.New("Release '%s' is not found", source.Full()) + } + + page.Releases = append(page.Releases, bhrelui.NewRelease(relVerRec, rel)) + } + return page, nil } diff --git a/controllers/factory.go b/controllers/factory.go index a11b0906..73909f32 100644 --- a/controllers/factory.go +++ b/controllers/factory.go @@ -48,7 +48,13 @@ type Factory struct { func NewFactory(apiKey string, r FactoryRepos, runner boshsys.CmdRunner, logger boshlog.Logger) (Factory, error) { factory := Factory{ HomeController: NewHomeController(r.ReleasesRepo(), r.StemcellsRepo(), logger), - DocsController: NewDocsController(r.BoshInitBinsRepo(), logger), + + DocsController: NewDocsController( + r.ReleasesRepo(), + r.ReleaseVersionsRepo(), + r.BoshInitBinsRepo(), + logger, + ), ReleasesController: NewReleasesController( r.ReleasesRepo(), diff --git a/ui/release/known_releases.go b/ui/release/known_releases.go new file mode 100644 index 00000000..c029c1d9 --- /dev/null +++ b/ui/release/known_releases.go @@ -0,0 +1,58 @@ +package release + +import ( + "fmt" + + bhrelsrepo "github.com/cppforlife/bosh-hub/release/releasesrepo" +) + +type ReleaseRef struct { + Source Source + PrettyName string + DocPage string +} + +type ReleaseRefs []ReleaseRef + +var BOSH = NewReleaseRef("github.com/cloudfoundry/bosh", "BOSH", "") + +var KnownCPIs = ReleaseRefs{ + NewReleaseRef("github.com/cloudfoundry-incubator/bosh-aws-cpi-release", "AWS", "init-aws"), + NewReleaseRef("github.com/cloudfoundry-incubator/bosh-openstack-cpi-release", "OpenStack", "init-openstack"), + NewReleaseRef("github.com/cloudfoundry-incubator/bosh-vsphere-cpi-release", "vSphere", "init-vsphere"), + NewReleaseRef("github.com/cloudfoundry-incubator/bosh-vcloud-cpi-release", "vCloud", "init-vcloud"), +} + +func NewReleaseRef(fullSource, prettyName, docPage string) ReleaseRef { + return ReleaseRef{ + Source: NewSource(bhrelsrepo.Source{Full: fullSource}), + PrettyName: prettyName, + DocPage: docPage, + } +} + +func (c ReleaseRef) DocPagePath() string { return fmt.Sprintf("/docs/%s.html", c.DocPage) } + +func (c ReleaseRef) DocPageLink() string { + return fmt.Sprintf("Initializing a BOSH environment on %s", c.DocPagePath(), c.PrettyName) +} + +func (c ReleaseRefs) FindByShortName(name string) (ReleaseRef, bool) { + for _, cpi := range c { + if cpi.Source.ShortName() == name { + return cpi, true + } + } + + return ReleaseRef{}, false +} + +func (c ReleaseRefs) FindByDocPage(docPage string) (ReleaseRef, bool) { + for _, cpi := range c { + if cpi.DocPage == docPage { + return cpi, true + } + } + + return ReleaseRef{}, false +} diff --git a/ui/release/release.go b/ui/release/release.go index 2922bd13..6c960d1c 100644 --- a/ui/release/release.go +++ b/ui/release/release.go @@ -134,15 +134,9 @@ func (r Release) GithubURLForPath(path, ref string) string { func (r Release) IsCPI() bool { return r.Source.IsCPI() } func (r Release) CPIDocsLink() template.HTML { - links := map[string][]string{ - "bosh-aws-cpi-release": []string{"AWS", "/docs/init-aws.html"}, - "bosh-openstack-cpi-release": []string{"OpenStack", "/docs/init-openstack.html"}, - "bosh-vsphere-cpi-release": []string{"vSphere", "/docs/init-vsphere.html"}, - "bosh-vcloud-cpi-release": []string{"vCloud", "/docs/init-vcloud.html"}, - } - - if link, found := links[r.Source.ShortName()]; found { - return template.HTML(fmt.Sprintf("Initializing a BOSH environment on %s", link[1], link[0])) + cpi, found := KnownCPIs.FindByShortName(r.Source.ShortName()) + if found { + return template.HTML(cpi.DocPageLink()) } return template.HTML("")