Skip to content

Commit

Permalink
remove non-used StencilGroups
Browse files Browse the repository at this point in the history
add support for filters
  • Loading branch information
lvangool committed Dec 11, 2019
1 parent 44d387f commit 9c6121e
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 45 deletions.
58 changes: 36 additions & 22 deletions bundle/bundle_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func CreateSkycapFiles(outputDir, templateRepository, branch, packName, githubUR
//Create .bundle directory structure if it doesn't exist
tempFolder := os.TempDir()
bundleFolder := filepath.Join(tempFolder, "bundle")
//bundleFolder := "/tmp/bundle"

// cleanup the bundle folder
defer func() {
Expand Down Expand Up @@ -91,11 +92,6 @@ func GenerateBundleFiles(bundleFolder, templateRepository, branch, packName, git
return err
}

err = handleConfigStoreRecords(packName, databases, bundle, bundleFolder, isGenericBTR)
if err != nil {
return err
}

// find components with min-usage 1
minUsageComponents, err := getMinUsageComponents(template)
if err != nil {
Expand All @@ -108,6 +104,11 @@ func GenerateBundleFiles(bundleFolder, templateRepository, branch, packName, git
return err
}

err = handleConfigStoreRecords(packName, databases, bundle, bundleFolder, isGenericBTR)
if err != nil {
return err
}

// add stencils to the bundle
bundle, err = addStencils(template, templateRepository, branch, services, bundleFolder, bundle, githubURL, requiredComponents)
if err != nil {
Expand Down Expand Up @@ -232,20 +233,18 @@ func getConfigStoreRecords(databases []common.Database, includeDatabases bool) (
}

func setConfigStoreRecords(configStoreRecords []cloud66.BundledConfigStoreRecord, prefix string, bundle *bundles.Bundle, bundleFolder string) error {
unmarshalledOutput := cloud66.BundledConfigStoreRecords{Records: configStoreRecords}
marshalledOutput, err := yaml.Marshal(&unmarshalledOutput)
bundledConfigStoreRecords := cloud66.BundledConfigStoreRecords{Records: configStoreRecords}
outputs, err := yaml.Marshal(&bundledConfigStoreRecords)
if err != nil {
return err
}

fileName := prefix + "-configstore.yml"
filePath := filepath.Join(filepath.Join(bundleFolder, "configstore"), fileName)

err = ioutil.WriteFile(filePath, marshalledOutput, 0600)
err = ioutil.WriteFile(filePath, outputs, 0600)
if err != nil {

return err
}

bundle.ConfigStore = append(bundle.ConfigStore, fileName)
return nil
}
Expand Down Expand Up @@ -363,22 +362,19 @@ func addWorkflows(bundle *bundles.Bundle, template *templates.Template, template
}

func loadBundle(bundleFolder string) (*bundles.Bundle, error) {
// TODO: if manifest file present, pick that up instead
var bundle *bundles.Bundle
manifestPath := filepath.Join(bundleFolder, "manifest.json")
if common.FileExists(manifestPath) {
//open manifest.json file and cast it into the struct
// open the template.json file and start downloading the stencils
manifest, err := os.Open(manifestPath)
manifestFile, err := os.Open(manifestPath)
if err != nil {
return nil, err
}
manifestData, err := ioutil.ReadAll(manifest)
manifestData, err := ioutil.ReadAll(manifestFile)
if err != nil {
return nil, err
}

err = json.Unmarshal(manifestData, &manifest)
err = json.Unmarshal(manifestData, &bundle)
if err != nil {
return nil, err
}
Expand All @@ -388,13 +384,13 @@ func loadBundle(bundleFolder string) (*bundles.Bundle, error) {
Metadata: nil,
UID: "",
Name: "",
StencilGroups: make([]*bundles.StencilGroup, 0),
BaseTemplates: make([]*bundles.BaseTemplate, 0),
Policies: make([]*bundles.Policy, 0),
Transformations: make([]*bundles.Transformation, 0),
Workflows: make([]*bundles.Workflow, 0),
Tags: make([]string, 0),
HelmReleases: make([]*bundles.HelmRelease, 0),
Filters: make([]*bundles.Filter, 0),
Tags: make([]string, 0),
Configurations: make([]string, 0),
ConfigStore: make([]string, 0),
}
Expand Down Expand Up @@ -469,7 +465,7 @@ func downloadStencil(context string, templateStencil *templates.Stencil, btrShor
return &bundleStencil, nil
}

func downloadPolicy(templatePolicy *templates.Policy, btrShortName string, bundleFolder string, templateRepository string, branch string) (*bundles.Policy, error) {
func downloadPolicy(templatePolicy *templates.Policy, btrShortName, bundleFolder, templateRepository, branch string) (*bundles.Policy, error) {
remoteFilename := templatePolicy.Filename
localFilename := templatePolicy.Filename
filename, err := downloadComponent(remoteFilename, localFilename, btrShortName, templateRepository, "policies", bundleFolder, branch)
Expand All @@ -484,7 +480,7 @@ func downloadPolicy(templatePolicy *templates.Policy, btrShortName string, bundl
return bundlePolicy, nil
}

func downloadTransformation(templateTransformation *templates.Transformation, btrShortName string, bundleFolder string, templateRepository string, branch string) (*bundles.Transformation, error) {
func downloadTransformation(templateTransformation *templates.Transformation, btrShortName, bundleFolder, templateRepository, branch string) (*bundles.Transformation, error) {
remoteFilename := templateTransformation.Filename
localFilename := templateTransformation.Filename
filename, err := downloadComponent(remoteFilename, localFilename, btrShortName, templateRepository, "transformations", bundleFolder, branch)
Expand Down Expand Up @@ -714,6 +710,16 @@ func getMinUsageComponents(template *templates.Template) ([]string, error) {
result = append(result, fullyQualifiedTransformationName)
}
}
// filters
for _, templateFilter := range template.Templates.Filters {
if templateFilter.MinUsage > 0 {
fullyQualifiedFilterName, err := generateFullyQualifiedName(templateFilter)
if err != nil {
return nil, err
}
result = append(result, fullyQualifiedFilterName)
}
}
return result, nil
}

Expand Down Expand Up @@ -770,7 +776,7 @@ func getDependencyComponentsInternal(template *templates.Template, rootName stri
func getTemplateDependencies(template *templates.Template, name string) ([]string, error) {
nameParts := strings.Split(name, "/")
if len(nameParts) != 2 {
return nil, fmt.Errorf("dependency name '%s' should be 'TEMPLATE_TYPE/TEMPLATE_NAME', where TEMPLATE_TYPE is one of 'stencils', 'policies', 'transformations', or 'helm_charts'", name)
return nil, fmt.Errorf("dependency name '%s' should be 'TEMPLATE_TYPE/TEMPLATE_NAME', where TEMPLATE_TYPE is one of 'stencils', 'policies', 'transformations', 'filters' or 'helm_charts'", name)
}

templateType := nameParts[0]
Expand Down Expand Up @@ -800,6 +806,12 @@ func getTemplateDependencies(template *templates.Template, name string) ([]strin
return v.Dependencies, nil
}
}
case "filters":
for _, v := range template.Templates.Filters {
if v.Name == templateName {
return v.Dependencies, nil
}
}
default:
return nil, fmt.Errorf("dependency name '%s' should be 'TEMPLATE_TYPE/TEMPLATE_NAME', where TEMPLATE_TYPE is one of 'stencils', 'policies', 'transformations', or 'helm_charts'", name)
}
Expand Down Expand Up @@ -900,6 +912,8 @@ func generateFullyQualifiedName(v templates.TemplateInterface) (string, error) {
return "helm_releases" + "/" + name, nil
case templates.Workflow, *templates.Workflow:
return "workflows" + "/" + name, nil
case templates.Filter, *templates.Filter:
return "filters" + "/" + name, nil
default:
return "", fmt.Errorf("generateFullyQualifiedName missing definition for %T", vt)
}
Expand Down
1 change: 0 additions & 1 deletion bundle/bundles/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ type Bundle struct {
Metadata *Metadata `json:"metadata"`
UID string `json:"uid"`
Name string `json:"name"`
StencilGroups []*StencilGroup `json:"stencil_groups"`
BaseTemplates []*BaseTemplate `json:"base_templates"`
Policies []*Policy `json:"policies"`
Transformations []*Transformation `json:"transformations"`
Expand Down
7 changes: 0 additions & 7 deletions bundle/bundles/stencil-group.go

This file was deleted.

13 changes: 7 additions & 6 deletions bundle/templates/filter.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package templates

type Filter struct {
UID string `json:"UID"`
Name string `json:"name"`
Filename string `json:"filename"`
Tags []string `json:"tags"`
MinUsage int `json:"min_usage"`
UID string `json:"UID"`
Name string `json:"name"`
Filename string `json:"filename"`
Tags []string `json:"tags"`
MinUsage int `json:"min_usage"`
Dependencies []string `json:"dependencies"`
}

func (v Filter) GetName() string {
return v.Name
}

func (v Filter) GetDependencies() []string {
return []string{}
return v.Dependencies
}
7 changes: 1 addition & 6 deletions common/utils-template.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@ package common

import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"time"
"errors"
)


type DownloadFile struct {
URL string `json:"url"`
Name string `json:"name"`
Expand All @@ -26,10 +25,8 @@ type TemplateDefinition struct {
BundleManifest []DownloadFile `json:"bundle-manifest-jsons"`
}


func Fetch(url string, mod *time.Time) (io.ReadCloser, error) {
PrintlnL2("Downloading from %s", url)

req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
Expand Down Expand Up @@ -67,7 +64,6 @@ func FetchJSON(url string, mod *time.Time, v interface{}) error {
return json.NewDecoder(r).Decode(v)
}


func DownloadTemplates(tempDir string, td TemplateDefinition, templatePath string, flagBranch string) error {
err := DownloadSingleFile(tempDir, DownloadFile{URL: strings.Replace(templatePath, "{{.branch}}", flagBranch, -1), Name: "templates.json"}, flagBranch)
if err != nil {
Expand Down Expand Up @@ -125,4 +121,3 @@ func DownloadSingleFile(tempDir string, temp DownloadFile, flagBranch string) er

return nil
}

5 changes: 2 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,12 +432,11 @@ func analyze(
if strings.Contains(generator, "skycap") {
_, err = os.Stat("starter.bundle")
if err == nil && !overwrite {
return nil, fmt.Errorf("Starter bundle file already exist. Use flag to overwrite.")
return nil, fmt.Errorf("starter bundle file already exist. Use flag to overwrite.")
}
err = pack.CreateSkycapFiles(path, templates, flagBTRBranch)

if err != nil {
return nil, fmt.Errorf("Failed to write Starter bundle file due to: %s", err.Error())
return nil, fmt.Errorf("failed to write Starter bundle file due to: %s", err.Error())
}
}

Expand Down

0 comments on commit 9c6121e

Please sign in to comment.