-
Notifications
You must be signed in to change notification settings - Fork 5
Add debug API endpoint for mail template rendering #830
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| // SPDX-FileCopyrightText: 2026 SAP SE or an SAP affiliate company | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package api | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "errors" | ||
| "fmt" | ||
| "net/http" | ||
| "strings" | ||
| "time" | ||
|
|
||
| . "github.com/majewsky/gg/option" | ||
| limesresources "github.com/sapcc/go-api-declarations/limes/resources" | ||
| "github.com/sapcc/go-api-declarations/liquid" | ||
| "github.com/sapcc/go-bits/httpapi" | ||
| "github.com/sapcc/go-bits/logg" | ||
| "github.com/sapcc/go-bits/must" | ||
| "github.com/sapcc/go-bits/respondwith" | ||
| "golang.org/x/net/html" | ||
|
|
||
| "github.com/sapcc/limes/internal/core" | ||
| "github.com/sapcc/limes/internal/db" | ||
| ) | ||
|
|
||
| // everythingCommitment provides a dummy commitment where all available fields | ||
| // are filled with data to cover all potential cases in the mail template rendering process. | ||
| var everythingCommitment db.ProjectCommitment = db.ProjectCommitment{ | ||
| ID: 42, | ||
| UUID: "commitment-uuid", | ||
| ProjectID: 1, | ||
| AZResourceID: 7, | ||
| Amount: 500, | ||
| Duration: must.Return(limesresources.ParseCommitmentDuration("1 year")), | ||
| CreatedAt: time.Now(), | ||
| CreatorUUID: "creator-uuid", | ||
| CreatorName: "Foo User", | ||
| ConfirmBy: Some(time.Now()), | ||
| ConfirmedAt: Some(time.Now()), | ||
| ExpiresAt: time.Now(), | ||
|
|
||
| SupersededAt: Some(time.Now()), | ||
| CreationContextJSON: json.RawMessage(`{"reason": "create"}`), | ||
| SupersedeContextJSON: Some(json.RawMessage(`{"reason": "merge", "related_uuids": ["other-commitment-uuid"]}`)), | ||
| RenewContextJSON: Some(json.RawMessage(`{"reason": "renew"}`)), | ||
|
|
||
| TransferStatus: limesresources.CommitmentTransferStatusPublic, | ||
| TransferToken: Some("transfer-token"), | ||
| TransferStartedAt: Some(time.Now()), | ||
|
|
||
| Status: liquid.CommitmentStatusConfirmed, | ||
| NotifyOnConfirm: true, | ||
| NotifiedForExpiration: true, | ||
| } | ||
|
|
||
| // RenderMailTemplate handles GET /admin/mail/render?template_type=:type | ||
| func (p *v1Provider) RenderMailTemplate(w http.ResponseWriter, r *http.Request) { | ||
| httpapi.IdentifyEndpoint(r, "/admin/mail/render") | ||
| token := p.CheckToken(r) | ||
| if !token.Require(w, "cluster:show") { | ||
| return | ||
| } | ||
|
|
||
| template_type := r.URL.Query().Get("template_type") | ||
| if template_type == "" { | ||
| http.Error(w, "missing required parameter: template_type", http.StatusBadRequest) | ||
| return | ||
| } | ||
|
|
||
| mailConfig, ok := p.Cluster.Config.MailNotifications.Unpack() | ||
| if !ok { | ||
| respondwith.ErrorText(w, errors.New("could not get mail configuration")) | ||
| return | ||
| } | ||
|
|
||
| var template core.MailTemplate | ||
| switch template_type { | ||
| case "confirmed_commitments": | ||
| template = mailConfig.Templates.ConfirmedCommitments | ||
| case "expiring_commitments": | ||
| template = mailConfig.Templates.ExpiringCommitments | ||
| case "transferred_commitments": | ||
| template = mailConfig.Templates.TransferredCommitments | ||
| default: | ||
| http.Error(w, "invalid template type", http.StatusBadRequest) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we list available templates here? The user needs admin anyhow, so we don't leak anything, right? |
||
| return | ||
| } | ||
|
|
||
| dummyResource := core.AZResourceLocation{ | ||
| ServiceType: "foo-service", | ||
| ResourceName: "bar-resource", | ||
| AvailabilityZone: "eu-de-1a", | ||
| } | ||
|
|
||
| notification := core.CommitmentGroupNotification{ | ||
| DomainName: "example-domain", | ||
| ProjectName: "test-project", | ||
| Commitments: []core.CommitmentNotification{ | ||
| { | ||
| Commitment: everythingCommitment, | ||
| DateString: time.Now().Format("2000-01-01"), | ||
| Resource: dummyResource, | ||
| LeftoverAmount: 100, | ||
| }, | ||
| { | ||
| Commitment: everythingCommitment, | ||
| DateString: time.Now().Format("2000-01-01"), | ||
| Resource: dummyResource, | ||
| LeftoverAmount: 200, | ||
| }, | ||
| { | ||
| Commitment: everythingCommitment, | ||
| DateString: time.Now().Format("2000-01-01"), | ||
| Resource: dummyResource, | ||
| LeftoverAmount: 300, | ||
| }, | ||
| }, | ||
| } | ||
| projectID := db.ProjectID(42) | ||
| mailNotification, err := template.Render(notification, projectID, time.Now()) | ||
| if respondwith.ErrorText(w, err) { | ||
| return | ||
| } | ||
|
|
||
| _, err = html.Parse(strings.NewReader(mailNotification.Body)) | ||
| if err != nil { | ||
| respondwith.ErrorText(w, fmt.Errorf("mail template rendering returned invalid HTML: %w", err)) | ||
| return | ||
| } | ||
|
|
||
| w.WriteHeader(http.StatusOK) | ||
| _, err = w.Write([]byte(mailNotification.Body)) | ||
| if err != nil { | ||
| logg.Error("cannot write response for %s %s: %s", r.Method, r.URL.Path, err.Error()) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| // SPDX-FileCopyrightText: 2026 SAP SE or an SAP affiliate company | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| package api_test | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "testing" | ||
|
|
||
| "github.com/sapcc/go-bits/assert" | ||
|
|
||
| "github.com/sapcc/limes/internal/test" | ||
| ) | ||
|
|
||
| func TestRenderMailTemplate(t *testing.T) { | ||
| s := test.NewSetup(t, | ||
| test.WithConfig(`{ | ||
| "availability_zones": ["az-one", "az-two"], | ||
| "discovery": { | ||
| "method": "static", | ||
| "static_config": { | ||
| "domains": [ | ||
| {"name": "germany", "id": "uuid-for-germany"} | ||
| ], | ||
| "projects": { | ||
| "uuid-for-germany": [{"name": "dresden", "id": "uuid-for-dresden", "parent_id": "uuid-for-germany"}] | ||
| } | ||
| } | ||
| }, | ||
| "liquids": { | ||
| "shared": {"area": "shared"} | ||
| } | ||
| }`), | ||
| test.WithMailTemplates, | ||
| ) | ||
|
|
||
| // endpoint requires cluster show permissions | ||
| s.TokenValidator.Enforcer.AllowView = false | ||
| assert.HTTPRequest{ | ||
| Method: "GET", | ||
| Path: "/admin/mail/render?template_type=confirmed_commitments", | ||
| ExpectStatus: http.StatusForbidden, | ||
| }.Check(t, s.Handler) | ||
| s.TokenValidator.Enforcer.AllowView = true | ||
|
|
||
| // expect error when template type is missing | ||
| assert.HTTPRequest{ | ||
| Method: "GET", | ||
| Path: "/admin/mail/render", | ||
| ExpectStatus: http.StatusBadRequest, | ||
| ExpectBody: assert.StringData("missing required parameter: template_type\n"), | ||
| }.Check(t, s.Handler) | ||
|
|
||
| // expect error for invalid template type | ||
| assert.HTTPRequest{ | ||
| Method: "GET", | ||
| Path: "/admin/mail/render?template_type=unknown", | ||
| ExpectStatus: http.StatusBadRequest, | ||
| ExpectBody: assert.StringData("invalid template type\n"), | ||
| }.Check(t, s.Handler) | ||
|
|
||
| // happy path | ||
| assert.HTTPRequest{ | ||
| Method: "GET", | ||
| Path: "/admin/mail/render?template_type=confirmed_commitments", | ||
| ExpectStatus: http.StatusOK, | ||
| }.Check(t, s.Handler) | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
?was added to be consistent with the existing admin endpoints documentation