Skip to content

Commit

Permalink
Add function to get valid template versions (#430)
Browse files Browse the repository at this point in the history
  • Loading branch information
bfoley13 authored Nov 21, 2024
1 parent e896b21 commit a5607c6
Show file tree
Hide file tree
Showing 30 changed files with 50 additions and 51 deletions.
19 changes: 10 additions & 9 deletions pkg/config/draftconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"io/fs"
"slices"

"github.com/Azure/draft/pkg/config/transformers"
"github.com/Azure/draft/pkg/config/validators"
Expand Down Expand Up @@ -34,7 +35,7 @@ type DraftConfig struct {
DisplayName string `yaml:"displayName"`
Description string `yaml:"description"`
Type string `yaml:"type"`
Versions string `yaml:"versions"`
Versions []string `yaml:"versions"`
DefaultVersion string `yaml:"defaultVersion"`
Variables []*BuilderVar `yaml:"variables"`
FileNameOverrideMap map[string]string `yaml:"filenameOverrideMap"`
Expand Down Expand Up @@ -232,13 +233,8 @@ func (d *DraftConfig) ApplyDefaultVariablesForVersion(version string) error {
return fmt.Errorf("invalid version: %w", err)
}

expectedConfigVersionRange, err := semver.ParseRange(d.Versions)
if err != nil {
return fmt.Errorf("invalid config version range: %w", err)
}

if !expectedConfigVersionRange(v) {
return fmt.Errorf("version %s is outside of config version range %s", version, d.Versions)
if !slices.Contains(d.Versions, version) {
return fmt.Errorf("requested version outside of valid versions: %s", version)
}

for _, variable := range d.Variables {
Expand Down Expand Up @@ -376,11 +372,16 @@ func (d *DraftConfig) DeepCopy() *DraftConfig {
DisplayName: d.DisplayName,
Description: d.Description,
Type: d.Type,
Versions: d.Versions,
Versions: make([]string, len(d.Versions)),
DefaultVersion: d.DefaultVersion,
Variables: make([]*BuilderVar, len(d.Variables)),
FileNameOverrideMap: make(map[string]string),
}

for i, version := range d.Versions {
newConfig.Versions[i] = version
}

for i, variable := range d.Variables {
newConfig.Variables[i] = variable.DeepCopy()
}
Expand Down
6 changes: 4 additions & 2 deletions pkg/config/draftconfig_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,10 @@ func loadTemplatesWithValidation() error {
return fmt.Errorf("template %s has an invalid type: %s", path, currTemplate.Type)
}

if _, err := semver.ParseRange(currTemplate.Versions); err != nil {
return fmt.Errorf("template %s has an invalid version range: %s", path, currTemplate.Versions)
for _, version := range currTemplate.Versions {
if _, err := semver.Parse(version); err != nil {
return fmt.Errorf("template %s has an invalid version: %s", path, version)
}
}

referenceVarMap := map[string]*BuilderVar{}
Expand Down
12 changes: 6 additions & 6 deletions pkg/config/draftconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ func TestApplyDefaultVariablesForVersion(t *testing.T) {
testName: "excludeOutOfVersionRangeVariables",
version: "0.0.1",
draftConfig: DraftConfig{
Versions: ">=0.0.1 <=0.0.2",
Versions: []string{"0.0.1", "0.0.2"},
Variables: []*BuilderVar{
{
Name: "var1",
Expand Down Expand Up @@ -239,7 +239,7 @@ func TestApplyDefaultVariablesForVersion(t *testing.T) {
testName: "emptyInputVersion",
version: "",
draftConfig: DraftConfig{
Versions: ">=0.0.1 <=0.0.2",
Versions: []string{"0.0.1", "0.0.2"},
Variables: []*BuilderVar{
{
Name: "var1",
Expand All @@ -265,7 +265,7 @@ func TestApplyDefaultVariablesForVersion(t *testing.T) {
testName: "inputVersionOutOfRange",
version: "0.0.3",
draftConfig: DraftConfig{
Versions: ">=0.0.1 <=0.0.2",
Versions: []string{"0.0.1", "0.0.2"},
Variables: []*BuilderVar{
{
Name: "var1",
Expand All @@ -285,13 +285,13 @@ func TestApplyDefaultVariablesForVersion(t *testing.T) {
},
},
},
wantErrMsg: "version 0.0.3 is outside of config version range >=0.0.1 <=0.0.2",
wantErrMsg: "requested version outside of valid versions: 0.0.3",
},
{
testName: "overwriteDevfaultValue",
version: "0.0.2",
draftConfig: DraftConfig{
Versions: ">=0.0.1 <=0.0.2",
Versions: []string{"0.0.1", "0.0.2"},
Variables: []*BuilderVar{
{
Name: "var1",
Expand Down Expand Up @@ -320,7 +320,7 @@ func TestApplyDefaultVariablesForVersion(t *testing.T) {
testName: "referenceVarOverwrite",
version: "0.0.2",
draftConfig: DraftConfig{
Versions: ">=0.0.1 <=0.0.2",
Versions: []string{"0.0.1", "0.0.2"},
Variables: []*BuilderVar{
{
Name: "var1",
Expand Down
12 changes: 4 additions & 8 deletions pkg/handlers/template_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io/fs"
"path/filepath"
"runtime"
"slices"
"strings"

"github.com/Azure/draft/pkg/config"
Expand Down Expand Up @@ -90,18 +91,13 @@ func loadTemplates() error {
}

// IsValidVersion checks if a version is valid for a given version range
func IsValidVersion(versionRange, version string) bool {
v, err := semver.Parse(version)
func IsValidVersion(versions []string, version string) bool {
_, err := semver.Parse(version)
if err != nil {
return false
}

expectedRange, err := semver.ParseRange(versionRange)
if err != nil {
return false
}

return expectedRange(v)
return slices.Contains(versions, version)
}

func sanatizeTemplateSrcDir(src string) string {
Expand Down
2 changes: 1 addition & 1 deletion template/addons/azure/webapp_routing/draft.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
templateName: "app-routing-ingress"
description: "This template is used to create an ingress resource for use with the app-routing addon in AKS"
versions: "0.0.1"
versions: ["0.0.1"]
defaultVersion: "0.0.1"
type: "manifest"
variables:
Expand Down
2 changes: 1 addition & 1 deletion template/azurePipelines/kustomize/draft.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
templateName: "azure-pipeline-kustomize"
description: "This template is used to create an Azure Pipeline for deploying an app to AKS using Kustomize"
versions: "0.0.1"
versions: ["0.0.1"]
defaultVersion: "0.0.1"
type: "workflow"
variables:
Expand Down
2 changes: 1 addition & 1 deletion template/azurePipelines/manifests/draft.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
templateName: "azure-pipeline-manifests"
description: "Azure Pipeline for deploying a containerized application to AKS using kubernetes manifests"
versions: "0.0.1"
versions: ["0.0.1"]
defaultVersion: "0.0.1"
type: "workflow"
variables:
Expand Down
2 changes: 1 addition & 1 deletion template/deployments/helm/draft.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
templateName: "deployment-helm"
description: "This template is used to create a Helm deployment for an application"
type: "deployment"
versions: "0.0.1"
versions: ["0.0.1"]
defaultVersion: "0.0.1"
variables:
- name: "PORT"
Expand Down
2 changes: 1 addition & 1 deletion template/deployments/kustomize/draft.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
templateName: "deployment-kustomize"
versions: "0.0.1"
versions: ["0.0.1"]
defaultVersion: "0.0.1"
description: "This template is used to create a Kustomize deployment for an application"
type: "deployment"
Expand Down
2 changes: 1 addition & 1 deletion template/deployments/manifests/draft.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
templateName: "deployment-manifests"
description: "This template is used to create a Kubernetes manifest deployment for an application"
versions: "0.0.1"
versions: ["0.0.1"]
defaultVersion: "0.0.1"
type: "deployment"
variables:
Expand Down
2 changes: 1 addition & 1 deletion template/dockerfiles/clojure/draft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ language: clojure
displayName: Clojure
templateName: "dockerfile-clojure"
description: "This template is used to create a Dockerfile for a Clojure application"
versions: "0.0.1"
versions: ["0.0.1"]
defaultVersion: "0.0.1"
type: "dockerfile"
variables:
Expand Down
2 changes: 1 addition & 1 deletion template/dockerfiles/csharp/draft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ language: csharp
displayName: C#
templateName: "dockerfile-csharp"
description: "This template is used to create a Dockerfile for a C# application"
versions: "0.0.1"
versions: ["0.0.1"]
defaultVersion: "0.0.1"
type: "dockerfile"
variables:
Expand Down
2 changes: 1 addition & 1 deletion template/dockerfiles/erlang/draft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ language: erlang
displayName: Erlang
templateName: "dockerfile-erlang"
description: "This template is used to create a Dockerfile for an Erlang application"
versions: "0.0.1"
versions: ["0.0.1"]
defaultVersion: "0.0.1"
type: "dockerfile"
variables:
Expand Down
2 changes: 1 addition & 1 deletion template/dockerfiles/go/draft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ language: go
displayName: Go
templateName: "dockerfile-go"
description: "This template is used to create a Dockerfile for a Go application"
versions: "0.0.1"
versions: ["0.0.1"]
defaultVersion: "0.0.1"
type: "dockerfile"
variables:
Expand Down
2 changes: 1 addition & 1 deletion template/dockerfiles/gomodule/draft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ language: gomodule
displayName: Go Module
templateName: "dockerfile-gomodule"
description: "This template is used to create a Dockerfile for a Go Module application"
versions: "0.0.1"
versions: ["0.0.1"]
defaultVersion: "0.0.1"
type: "dockerfile"
variables:
Expand Down
2 changes: 1 addition & 1 deletion template/dockerfiles/gradle/draft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ language: gradle
displayName: Gradle
templateName: "dockerfile-gradle"
description: "This template is used to create a Dockerfile for a Gradle application"
versions: "0.0.1"
versions: ["0.0.1"]
defaultVersion: "0.0.1"
type: "dockerfile"
variables:
Expand Down
2 changes: 1 addition & 1 deletion template/dockerfiles/gradlew/draft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ language: gradle
displayName: Gradle
templateName: "dockerfile-gradlew"
description: "This template is used to create a Dockerfile for a Gradle application"
versions: "0.0.1"
versions: ["0.0.1"]
defaultVersion: "0.0.1"
type: "dockerfile"
variables:
Expand Down
2 changes: 1 addition & 1 deletion template/dockerfiles/java/draft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ language: java
displayName: Java
templateName: "dockerfile-java"
description: "This template is used to create a Dockerfile for a Java application"
versions: "0.0.1"
versions: ["0.0.1"]
defaultVersion: "0.0.1"
type: "dockerfile"
variables:
Expand Down
2 changes: 1 addition & 1 deletion template/dockerfiles/javascript/draft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ language: javascript
displayName: JavaScript
templateName: "dockerfile-javascript"
description: "This template is used to create a Dockerfile for a JavaScript application"
versions: "0.0.1"
versions: ["0.0.1"]
defaultVersion: "0.0.1"
type: "dockerfile"
variables:
Expand Down
2 changes: 1 addition & 1 deletion template/dockerfiles/php/draft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ language: php
displayName: PHP
templateName: "dockerfile-php"
description: "This template is used to create a Dockerfile for a PHP application"
versions: "0.0.1"
versions: ["0.0.1"]
defaultVersion: "0.0.1"
type: "dockerfile"
variables:
Expand Down
2 changes: 1 addition & 1 deletion template/dockerfiles/python/draft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ language: python
displayName: Python
templateName: "dockerfile-python"
description: "This template is used to create a Dockerfile for a Python application"
versions: "0.0.1"
versions: ["0.0.1"]
defaultVersion: "0.0.1"
type: "dockerfile"
variables:
Expand Down
2 changes: 1 addition & 1 deletion template/dockerfiles/ruby/draft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ language: ruby
displayName: Ruby
templateName: "dockerfile-ruby"
description: "This template is used to create a Dockerfile for a Ruby application"
versions: "0.0.1"
versions: ["0.0.1"]
defaultVersion: "0.0.1"
type: "dockerfile"
variables:
Expand Down
2 changes: 1 addition & 1 deletion template/dockerfiles/rust/draft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ language: rust
displayName: Rust
templateName: "dockerfile-rust"
description: "This template is used to create a Dockerfile for a Rust application"
versions: "0.0.1"
versions: ["0.0.1"]
defaultVersion: "0.0.1"
type: "dockerfile"
variables:
Expand Down
2 changes: 1 addition & 1 deletion template/dockerfiles/swift/draft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ language: swift
displayName: Swift
templateName: "dockerfile-swift"
description: "This template is used to create a Dockerfile for a Swift application"
versions: "0.0.1"
versions: ["0.0.1"]
defaultVersion: "0.0.1"
type: "dockerfile"
variables:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
templateName: "horizontalPodAutoscaler-manifests"
description: "This template is used to create a horizontalPodAutoscaling for an application"
versions: "0.0.1"
versions: ["0.0.1"]
defaultVersion: "0.0.1"
type: "manifest"
variables:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
templateName: "podDisruptionBudget-manifests"
description: "This template is used to create a PodDisruptionBudget for an application"
versions: "0.0.1"
versions: ["0.0.1"]
defaultVersions: "0.0.1"
type: "manifest"
variables:
Expand Down
2 changes: 1 addition & 1 deletion template/manifests/Service/manifests/draft.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
templateName: "service-manifests"
description: "This template is used to create a generic Service for an application"
versions: "0.0.1"
versions: ["0.0.1"]
defaultVersion: "0.0.1"
type: "manifest"
variables:
Expand Down
2 changes: 1 addition & 1 deletion template/workflows/helm/draft.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
templateName: "github-workflow-helm"
description: "This template is used to create a GitHub workflow for building and deploying an app to AKS with Helm"
type: "workflow"
versions: "0.0.1"
versions: ["0.0.1"]
defaultVersion: "0.0.1"
variables:
- name: "WORKFLOWNAME"
Expand Down
2 changes: 1 addition & 1 deletion template/workflows/kustomize/draft.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
templateName: "github-workflow-kustomize"
description: "This template is used to create a GitHub workflow for building and deploying an app to AKS with Kustomize"
type: "workflow"
versions: "0.0.1"
versions: ["0.0.1"]
defaultVersion: "0.0.1"
variables:
- name: "WORKFLOWNAME"
Expand Down
2 changes: 1 addition & 1 deletion template/workflows/manifests/draft.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
templateName: "github-workflow-manifests"
description: "This template is used to create a GitHub workflow for building and deploying an app to AKS with kubernetes manifests"
type: "workflow"
versions: "0.0.1"
versions: ["0.0.1"]
defaultVersion: "0.0.1"
variables:
- name: "WORKFLOWNAME"
Expand Down

0 comments on commit a5607c6

Please sign in to comment.