Skip to content

Commit

Permalink
always show latest versions of releases on init-* doc pages
Browse files Browse the repository at this point in the history
  • Loading branch information
cppforlife committed Aug 13, 2015
1 parent 10a7f6a commit affda5d
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 13 deletions.
60 changes: 57 additions & 3 deletions controllers/docs_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -20,7 +23,9 @@ var (
)

type DocsController struct {
boshInitBinsRepo bhbibrepo.Repository
releasesRepo bhrelsrepo.ReleasesRepository
releaseVersionsRepo bhrelsrepo.ReleaseVersionsRepository
boshInitBinsRepo bhbibrepo.Repository

defaultTmpl string
errorTmpl string
Expand All @@ -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",
Expand All @@ -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")

Expand Down Expand Up @@ -86,5 +103,42 @@ func (c DocsController) findPage(tmpl string) (interface{}, error) {
return installBoshInitPage{docPage: page, LatestBoshInitBinGroups: binGroups}, nil
}

// init-<cpi> 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
}
8 changes: 7 additions & 1 deletion controllers/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
58 changes: 58 additions & 0 deletions ui/release/known_releases.go
Original file line number Diff line number Diff line change
@@ -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("<a href='%s'>Initializing a BOSH environment on %s</a>", 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
}
12 changes: 3 additions & 9 deletions ui/release/release.go
Original file line number Diff line number Diff line change
Expand Up @@ -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("<a href='%s'>Initializing a BOSH environment on %s</a>", link[1], link[0]))
cpi, found := KnownCPIs.FindByShortName(r.Source.ShortName())
if found {
return template.HTML(cpi.DocPageLink())
}

return template.HTML("")
Expand Down

0 comments on commit affda5d

Please sign in to comment.