Skip to content

Commit

Permalink
Merge pull request #71 from vshn/feat/dbaas-templates
Browse files Browse the repository at this point in the history
Add Templates for Exoscale DBaaS products
  • Loading branch information
glrf authored Dec 14, 2022
2 parents 087d77c + 64fd4cd commit a3953e1
Show file tree
Hide file tree
Showing 24 changed files with 162 additions and 9 deletions.
10 changes: 10 additions & 0 deletions description_templates/_dbaas_usage.gotmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Exoscale Kafka

{{- define "_dbaas_usage" -}}
{{ $keySeg := splitList ":" .Source -}}
{{ if ge (len $keySeg) 5 -}}
Plan: {{ index $keySeg 4 }}
{{ end -}}
Qty: {{.Quantity | printf "%.0f"}} Instance-Hours
Unit Price: CHF {{.PricePerUnit | printf "%.8f"}} / {{.Unit}} / Hour
{{ end -}}
4 changes: 4 additions & 0 deletions description_templates/golden/kafka:exoscale.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Exoscale Kafka

Qty: 87955674 Instance-Hours
Unit Price: CHF 0.00000075 / UNIT / Hour
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Exoscale Kafka

Plan: business-16
Qty: 87955674 Instance-Hours
Unit Price: CHF 0.00000075 / UNIT / Hour
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Exoscale Kafka

Plan: premium-30x-32
Qty: 87955674 Instance-Hours
Unit Price: CHF 0.00000075 / UNIT / Hour
4 changes: 4 additions & 0 deletions description_templates/golden/mysql:exoscale.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Exoscale MySQL

Qty: 87955674 Instance-Hours
Unit Price: CHF 0.00000075 / UNIT / Hour
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Exoscale MySQL

Plan: business-225
Qty: 87955674 Instance-Hours
Unit Price: CHF 0.00000075 / UNIT / Hour
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Exoscale MySQL

Plan: startup-16
Qty: 87955674 Instance-Hours
Unit Price: CHF 0.00000075 / UNIT / Hour
4 changes: 4 additions & 0 deletions description_templates/golden/opensearch:exoscale.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Exoscale OpenSearch

Qty: 87955674 Instance-Hours
Unit Price: CHF 0.00000075 / UNIT / Hour
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Exoscale OpenSearch

Plan: premium-30x-16
Qty: 87955674 Instance-Hours
Unit Price: CHF 0.00000075 / UNIT / Hour
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Exoscale OpenSearch

Plan: startup-8
Qty: 87955674 Instance-Hours
Unit Price: CHF 0.00000075 / UNIT / Hour
4 changes: 4 additions & 0 deletions description_templates/golden/postgres:exoscale.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Exoscale PostgreSQL

Qty: 87955674 Instance-Hours
Unit Price: CHF 0.00000075 / UNIT / Hour
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Exoscale PostgreSQL

Plan: premium-32
Qty: 87955674 Instance-Hours
Unit Price: CHF 0.00000075 / UNIT / Hour
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Exoscale PostgreSQL

Plan: startup-8
Qty: 87955674 Instance-Hours
Unit Price: CHF 0.00000075 / UNIT / Hour
4 changes: 4 additions & 0 deletions description_templates/golden/redis:exoscale.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Exoscale Redis

Qty: 87955674 Instance-Hours
Unit Price: CHF 0.00000075 / UNIT / Hour
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Exoscale Redis

Plan: hobbyist-2
Qty: 87955674 Instance-Hours
Unit Price: CHF 0.00000075 / UNIT / Hour
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Exoscale Redis

Plan: premium-16
Qty: 87955674 Instance-Hours
Unit Price: CHF 0.00000075 / UNIT / Hour
6 changes: 6 additions & 0 deletions description_templates/golden_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ func TestGenerateGolden(t *testing.T) {
sourceKeys = append(sourceKeys, strings.TrimSuffix(name, extension))
}

sourceKeys = append(sourceKeys, "kafka:exoscale:*:*:business-16", "kafka:exoscale:*:*:premium-30x-32")
sourceKeys = append(sourceKeys, "opensearch:exoscale:*:*:startup-8", "opensearch:exoscale:*:*:premium-30x-16")
sourceKeys = append(sourceKeys, "redis:exoscale:*:*:hobbyist-2", "redis:exoscale:*:*:premium-16")
sourceKeys = append(sourceKeys, "postgres:exoscale:*:*:startup-8", "postgres:exoscale:*:*:premium-32")
sourceKeys = append(sourceKeys, "mysql:exoscale:*:*:startup-16", "mysql:exoscale:*:*:business-225")

baseItem := invoice.Item{
Description: "Long form query description",
QueryName: "default_query",
Expand Down
3 changes: 3 additions & 0 deletions description_templates/kafka:exoscale.gotmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Exoscale Kafka

{{template "_dbaas_usage" . -}}
3 changes: 3 additions & 0 deletions description_templates/mysql:exoscale.gotmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Exoscale MySQL

{{template "_dbaas_usage" . -}}
3 changes: 3 additions & 0 deletions description_templates/opensearch:exoscale.gotmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Exoscale OpenSearch

{{template "_dbaas_usage" . -}}
3 changes: 3 additions & 0 deletions description_templates/postgres:exoscale.gotmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Exoscale PostgreSQL

{{template "_dbaas_usage" . -}}
3 changes: 3 additions & 0 deletions description_templates/redis:exoscale.gotmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Exoscale Redis

{{template "_dbaas_usage" . -}}
25 changes: 19 additions & 6 deletions invoice/desctmpl/renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,27 @@ func ItemDescriptionTemplateRendererFromFS(fs fs.FS, extension string) (*ItemDes
}

// RenderItemDescription renders an item description. Uses the `.ProductRef.Source` as the key to look which template to use.
// If there are no exact matches, it will try to find templates that match a substring of the key using longest prefix match.
// Example: Source "foo:bar:buzz" will match template "foo:bar" if template "foo:bar:buzz" does not exist.
func (r *ItemDescriptionTemplateRenderer) RenderItemDescription(_ context.Context, item invoice.Item) (string, error) {
key := item.ProductRef.Source
tmpl := r.root.Lookup(key + r.extension)
if tmpl == nil {
return "", fmt.Errorf("failed to find template for `ProductRef.Source=%q`%s", key, r.root.DefinedTemplates())
tmpl, err := r.lookup(item.ProductRef.Source)
if err != nil {
return "", err
}

b := &strings.Builder{}
err := r.root.ExecuteTemplate(b, item.ProductRef.Source+r.extension, item)
err = tmpl.Execute(b, item)
return b.String(), err
}

func (r *ItemDescriptionTemplateRenderer) lookup(key string) (*template.Template, error) {

segments := strings.Split(key, ":")
for i := len(segments); i > 0; i-- {
tmpl := r.root.Lookup(strings.Join(segments[:i], ":") + r.extension)
if tmpl != nil {
return tmpl, nil
}
}

return nil, fmt.Errorf("failed to find template for `ProductRef.Source=%q`%s", key, r.root.DefinedTemplates())
}
45 changes: 42 additions & 3 deletions invoice/desctmpl/renderer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ func TestRenderItemDescription(t *testing.T) {
"storage" + extension: &fstest.MapFile{
Data: []byte("so vieli bytesli: {{.Total}}"),
},
"kafka" + extension: &fstest.MapFile{
Data: []byte("so kafkaesque: {{.Total}}"),
},
"kafka:exoscale:*:*:premium-30x-33" + extension: &fstest.MapFile{
Data: []byte("not so kafkaesque: {{.Total}}"),
},
}

subject, err := desctmpl.ItemDescriptionTemplateRendererFromFS(templateFS, extension)
Expand All @@ -35,24 +41,57 @@ func TestRenderItemDescription(t *testing.T) {
invoice.Item{ProductRef: invoice.ProductRef{Source: "memory"}, Total: 77},
"memory: 77",
require.NoError,
}, {
},
{
"storage source",
invoice.Item{ProductRef: invoice.ProductRef{Source: "storage"}, Total: 99},
"so vieli bytesli: 99",
require.NoError,
}, {
},
{
"kafka source",
invoice.Item{ProductRef: invoice.ProductRef{Source: "kafka"}, Total: 11},
"so kafkaesque: 11",
require.NoError,
},
{
"specialized kafka source",
invoice.Item{ProductRef: invoice.ProductRef{Source: "kafka:exoscale:*:*:premium-30x-32"}, Total: 12},
"so kafkaesque: 12",
require.NoError,
},
{
"weird specialized kafka source",
invoice.Item{ProductRef: invoice.ProductRef{Source: "kafka:exoscale:::.a::*:*:premium-30x-32"}, Total: 15},
"so kafkaesque: 15",
require.NoError,
},

{
"specialized kafka source with special template",
invoice.Item{ProductRef: invoice.ProductRef{Source: "kafka:exoscale:*:*:premium-30x-33"}, Total: 12},
"not so kafkaesque: 12",
require.NoError,
},
{
"unknown source",
invoice.Item{ProductRef: invoice.ProductRef{Source: "unknown"}, Total: 77},
"",
require.Error,
},
{
"weird unknown source",
invoice.Item{ProductRef: invoice.ProductRef{Source: ":::plswhy?::unknown:::"}, Total: 77},
"",
require.Error,
},
}

for _, tc := range tests {
t.Run(tc.desc, func(t *testing.T) {
rendered, err := subject.RenderItemDescription(context.Background(), tc.item)
require.Equal(t, tc.expectedOut, rendered)
tc.expectedErr(t, err)
require.Equal(t, tc.expectedOut, rendered)
})
}
}

0 comments on commit a3953e1

Please sign in to comment.