Skip to content

Commit

Permalink
PMM-11206 Remove limit for custom templates upload (#2166)
Browse files Browse the repository at this point in the history
* PMM-11206 Refactoring

* PMM-11206 Regenerate mocks

* PMM-11206 Refactoring

* PMM-11206 Update comment

* PMM-11206 Allow to upload multiple templates at once, improve error messages

* PMM-11206 Fix

* PMM-11206 Add API test

* PMM-11206 Fix linter warning

* PMM-11206 Cleanup

* PMM-11206 Fix API tests

* PMM-11206 Update saas dependency

* PMM-11206 Fix

* PMM-11206 Fix API test

* PMM-11206 Update saas dependency

* PMM-11206 Fix
  • Loading branch information
artemgavrilov authored Jul 28, 2023
1 parent 146167a commit feaf7af
Show file tree
Hide file tree
Showing 21 changed files with 316 additions and 267 deletions.
40 changes: 38 additions & 2 deletions api-tests/management/alerting/alerting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ func assertTemplate(t *testing.T, expectedTemplate alert.Template, listTemplates
assert.Equal(t, expectedTemplate.Labels, tmpl.Labels)
assert.Equal(t, expectedTemplate.Annotations, tmpl.Annotations)

expectedYAML, err := alert.ToYAML([]alert.Template{expectedTemplate})
require.NoError(t, err)
assert.Equal(t, expectedYAML, tmpl.Yaml)

assert.NotEmpty(t, tmpl.CreatedAt)
}

Expand All @@ -124,6 +128,9 @@ func TestTemplatesAPI(t *testing.T) {
templateData, err := os.ReadFile("../../testdata/ia/template.yaml")
require.NoError(t, err)

multipleTemplatesData, err := os.ReadFile("../../testdata/ia/multiple-templates.yaml")
require.NoError(t, err)

invalidTemplateData, err := os.ReadFile("../../testdata/ia/invalid-template.yaml")
require.NoError(t, err)

Expand Down Expand Up @@ -156,6 +163,35 @@ func TestTemplatesAPI(t *testing.T) {
assertTemplate(t, alertTemplates[0], resp.Payload.Templates)
})

t.Run("multiple templates at once", func(t *testing.T) {
t.Parallel()

alertTemplates, yml := formatTemplateYaml(t, string(multipleTemplatesData))
_, err := client.CreateTemplate(&alerting.CreateTemplateParams{
Body: alerting.CreateTemplateBody{
Yaml: yml,
},
Context: pmmapitests.Context,
})
require.NoError(t, err)
require.Len(t, alertTemplates, 2)
t.Cleanup(func() {
deleteTemplate(t, client, alertTemplates[0].Name)
deleteTemplate(t, client, alertTemplates[1].Name)
})

resp, err := client.ListTemplates(&alerting.ListTemplatesParams{
Body: alerting.ListTemplatesBody{
Reload: true,
},
Context: pmmapitests.Context,
})
require.NoError(t, err)

assertTemplate(t, alertTemplates[0], resp.Payload.Templates)
assertTemplate(t, alertTemplates[1], resp.Payload.Templates)
})

t.Run("duplicate", func(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -188,7 +224,7 @@ func TestTemplatesAPI(t *testing.T) {
},
Context: pmmapitests.Context,
})
pmmapitests.AssertAPIErrorf(t, err, 400, codes.InvalidArgument, "Failed to parse rule template.")
pmmapitests.AssertAPIErrorf(t, err, 400, codes.InvalidArgument, "Failed to parse rule template")
})

t.Run("invalid template", func(t *testing.T) {
Expand All @@ -201,7 +237,7 @@ func TestTemplatesAPI(t *testing.T) {
Context: pmmapitests.Context,
})

pmmapitests.AssertAPIErrorf(t, err, 400, codes.InvalidArgument, "Failed to parse rule template.")
pmmapitests.AssertAPIErrorf(t, err, 400, codes.InvalidArgument, "Failed to parse rule template")
})
})

Expand Down
Empty file.
49 changes: 49 additions & 0 deletions api-tests/testdata/ia/multiple-templates.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
---
templates:
- name: test_template_1
version: 1
summary: Test summary 1
tiers: [ anonymous, registered ]
expr: "test expression"
params:
- name: param1
summary: first parameter with default value and defined range
unit: s
type: float
range: [ 0, 100 ]
value: 80
- name: param2
summary: second parameter without default value and defined range
unit: s
type: float
for: 300s
severity: warning
labels:
foo: bar
annotations:
description: test description
summary: test summary

- name: test_template_2
version: 1
summary: Test summary 2
tiers: [ anonymous, registered ]
expr: "test expression"
params:
- name: param1
summary: first parameter with default value and defined range
unit: s
type: float
range: [ 0, 100 ]
value: 80
- name: param2
summary: second parameter without default value and defined range
unit: s
type: float
for: 300s
severity: warning
labels:
foo: bar
annotations:
description: test description
summary: test summary
6 changes: 3 additions & 3 deletions api/managementpb/alerting/alerting.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions api/managementpb/alerting/alerting.proto
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ message Template {
TemplateSource source = 9;
// Template creation time. Empty for built-in and SaaS templates.
google.protobuf.Timestamp created_at = 10;
// YAML (or JSON) template file content. Empty for built-in and SaaS templates.
// YAML template file content. Empty for built-in and SaaS templates.
string yaml = 11;
}

Expand All @@ -116,7 +116,7 @@ message ListTemplatesResponse {
}

message CreateTemplateRequest {
// YAML (or JSON) template file content.
// YAML template file content.
string yaml = 1 [(validate.rules).string.min_len = 1];
}

Expand All @@ -125,7 +125,7 @@ message CreateTemplateResponse {}
message UpdateTemplateRequest {
// Machine-readable name (ID).
string name = 1 [(validate.rules).string.min_len = 1];
// YAML (or JSON) template file content.
// YAML template file content.
string yaml = 2 [(validate.rules).string.min_len = 1];
}

Expand Down
6 changes: 3 additions & 3 deletions api/managementpb/alerting/json/alerting.json
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@
"type": "object",
"properties": {
"yaml": {
"description": "YAML (or JSON) template file content.",
"description": "YAML template file content.",
"type": "string",
"x-order": 0
}
Expand Down Expand Up @@ -575,7 +575,7 @@
"x-order": 1
},
"yaml": {
"description": "YAML (or JSON) template file content. Empty for built-in and SaaS templates.",
"description": "YAML template file content. Empty for built-in and SaaS templates.",
"type": "string",
"x-order": 10
}
Expand Down Expand Up @@ -660,7 +660,7 @@
"x-order": 0
},
"yaml": {
"description": "YAML (or JSON) template file content.",
"description": "YAML template file content.",
"type": "string",
"x-order": 1
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions api/swagger/swagger-dev.json
Original file line number Diff line number Diff line change
Expand Up @@ -31110,7 +31110,7 @@
"type": "object",
"properties": {
"yaml": {
"description": "YAML (or JSON) template file content.",
"description": "YAML template file content.",
"type": "string",
"x-order": 0
}
Expand Down Expand Up @@ -31470,7 +31470,7 @@
"x-order": 9
},
"yaml": {
"description": "YAML (or JSON) template file content. Empty for built-in and SaaS templates.",
"description": "YAML template file content. Empty for built-in and SaaS templates.",
"type": "string",
"x-order": 10
}
Expand Down Expand Up @@ -31555,7 +31555,7 @@
"x-order": 0
},
"yaml": {
"description": "YAML (or JSON) template file content.",
"description": "YAML template file content.",
"type": "string",
"x-order": 1
}
Expand Down
6 changes: 3 additions & 3 deletions api/swagger/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -21220,7 +21220,7 @@
"type": "object",
"properties": {
"yaml": {
"description": "YAML (or JSON) template file content.",
"description": "YAML template file content.",
"type": "string",
"x-order": 0
}
Expand Down Expand Up @@ -21580,7 +21580,7 @@
"x-order": 9
},
"yaml": {
"description": "YAML (or JSON) template file content. Empty for built-in and SaaS templates.",
"description": "YAML template file content. Empty for built-in and SaaS templates.",
"type": "string",
"x-order": 10
}
Expand Down Expand Up @@ -21665,7 +21665,7 @@
"x-order": 0
},
"yaml": {
"description": "YAML (or JSON) template file content.",
"description": "YAML template file content.",
"type": "string",
"x-order": 1
}
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ require (
github.com/operator-framework/api v0.17.6
github.com/operator-framework/operator-lifecycle-manager v0.24.0
github.com/percona-platform/dbaas-api v0.0.0-20230103182808-d79c449a9f4c
github.com/percona-platform/saas v0.0.0-20230713134421-bb403194c5f7
github.com/percona-platform/saas v0.0.0-20230728161159-ad6bdeb8a3d9
github.com/percona/dbaas-operator v0.1.6
github.com/percona/exporter_shared v0.7.4
github.com/percona/go-mysql v0.0.0-20210427141028-73d29c6da78c
Expand Down Expand Up @@ -133,7 +133,7 @@ require (
github.com/spf13/pflag v1.0.5 // indirect
github.com/xlab/treeprint v1.1.0 // indirect
go.opentelemetry.io/otel/metric v1.16.0 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/atomic v1.11.0 // indirect
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1 // indirect
golang.org/x/time v0.3.0 // indirect
google.golang.org/genproto v0.0.0-20230526203410-71b5a4ffd15e // indirect
Expand Down Expand Up @@ -172,7 +172,7 @@ require (
github.com/andybalholm/brotli v1.0.5 // indirect
github.com/armon/go-metrics v0.4.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cenkalti/backoff/v4 v4.2.0 // indirect
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/charmbracelet/harmonica v0.2.0 // indirect
github.com/cloudflare/golz4 v0.0.0-20150217214814-ef862a3cdc58 // indirect
Expand Down
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ github.com/brianvoe/gofakeit v3.18.0+incompatible/go.mod h1:kfwdRA90vvNhPutZWfH7
github.com/brianvoe/gofakeit/v6 v6.23.0 h1:pgVhyWpYq4e0GEVCh2gdZnS/nBX+8SnyTBliHg5xjks=
github.com/brianvoe/gofakeit/v6 v6.23.0/go.mod h1:Ow6qC71xtwm79anlwKRlWZW6zVq9D2XHE4QSSMP/rU8=
github.com/buger/jsonparser v1.1.1/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx27UK13J/0=
github.com/cenkalti/backoff/v4 v4.2.0 h1:HN5dHm3WBOgndBH6E8V0q2jIYIR3s9yglV8k/+MN3u4=
github.com/cenkalti/backoff/v4 v4.2.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
github.com/cenkalti/backoff/v4 v4.2.1 h1:y4OZtCnogmCPw98Zjyt5a6+QwPLGkiQsYW5oUqylYbM=
github.com/cenkalti/backoff/v4 v4.2.1/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
Expand Down Expand Up @@ -645,8 +645,8 @@ github.com/percona-lab/crypto v0.0.0-20220811043533-d164de3c7f08 h1:NprWeXddFZJS
github.com/percona-lab/crypto v0.0.0-20220811043533-d164de3c7f08/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
github.com/percona-platform/dbaas-api v0.0.0-20230103182808-d79c449a9f4c h1:1JySfwdjVfc9ahl0466OX7nSQ7Z4SjQkLe3ZdLkMOJI=
github.com/percona-platform/dbaas-api v0.0.0-20230103182808-d79c449a9f4c/go.mod h1:/jgle33awfHq1va/T6NnNS5wWAETSnl6wUZ1bew+CJ0=
github.com/percona-platform/saas v0.0.0-20230713134421-bb403194c5f7 h1:9XwfsWsQjWLWZpm9ouuAMZGZ3g4bT4pt0E/fr0Tc/Vo=
github.com/percona-platform/saas v0.0.0-20230713134421-bb403194c5f7/go.mod h1:lZuFcqj0EoQWx28SYkTcdhJOCQEbRcAyahYPfRMY7tc=
github.com/percona-platform/saas v0.0.0-20230728161159-ad6bdeb8a3d9 h1:KkOH+Y4sVRP7qvRtTDmfPFNjjQcwU2054/jNl9DZhEo=
github.com/percona-platform/saas v0.0.0-20230728161159-ad6bdeb8a3d9/go.mod h1:lZuFcqj0EoQWx28SYkTcdhJOCQEbRcAyahYPfRMY7tc=
github.com/percona/dbaas-operator v0.1.6 h1:NsZXDKcPXk38kET+X6r8Es+3Supyu5XJZMS0gqPejKs=
github.com/percona/dbaas-operator v0.1.6/go.mod h1:52B/kh+Jmtfv0JiZgDcc34qgbwwEi9U4A3311JBxIZg=
github.com/percona/exporter_shared v0.7.4 h1:S+xnfK/CySiYqr4XqLiLAfO3rxgEOUFK+m6lCBi3mgc=
Expand Down Expand Up @@ -832,8 +832,8 @@ go.starlark.net v0.0.0-20230717150657-8a3343210976 h1:7ljYNcZU84T2N0tZdDgvL7U3M4
go.starlark.net v0.0.0-20230717150657-8a3343210976/go.mod h1:jxU+3+j+71eXOW14274+SmmuW82qJzl6iZSeqEtTGds=
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/atomic v1.10.0 h1:9qC72Qh0+3MqyJbAn8YU5xVq1frD8bn3JtD2oXtafVQ=
go.uber.org/atomic v1.10.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
go.uber.org/atomic v1.11.0 h1:ZvwS0R+56ePWxUNi+Atn9dWONBPp/AUETXlHW0DxSjE=
go.uber.org/atomic v1.11.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
go.uber.org/goleak v1.1.10/go.mod h1:8a7PlsEVH3e/a/GLqe5IIrQx6GzcnRmZEufDUTk4A7A=
go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=
go.uber.org/multierr v1.9.0 h1:7fIwc/ZtS0q++VgcfqFDxSBZVv/Xo49/SYnDFupUwlI=
Expand Down
7 changes: 7 additions & 0 deletions managed/models/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,3 +230,10 @@ const (
Bool = ParamType("bool")
String = ParamType("string")
)

type ParamUnit string

const (
Percent = ParamUnit("%")
Seconds = ParamUnit("s")
)
Loading

0 comments on commit feaf7af

Please sign in to comment.