Skip to content

Commit 6ed94b0

Browse files
committed
feat(tls): add events
1 parent 10676f2 commit 6ed94b0

2 files changed

Lines changed: 61 additions & 4 deletions

File tree

api/tls.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"fmt"
55
)
66

7+
type Event = map[string]string
8+
79
// TLS is the structure of an app's TLS settings.
810
type TLS struct {
911
// Owner is the app owner. It cannot be updated with TLS.Set(). See app.Transfer().
@@ -22,6 +24,7 @@ type TLS struct {
2224
//Use ACME to automatically generate certificates if CertsAuto enable
2325
CertsAutoEnabled *bool `json:"certs_auto_enabled,omitempty"`
2426
Issuer *Issuer `json:"issuer,omitempty"`
27+
Events []Event `json:"events,omitempty"`
2528
}
2629

2730
// Issuer is the structure of POST /v2/app/<app id>/tls/.

tls/tls_test.go

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package tls
22

33
import (
4+
"encoding/json"
45
"fmt"
56
"io"
67
"net/http"
@@ -10,6 +11,7 @@ import (
1011

1112
drycc "github.com/drycc/controller-sdk-go"
1213
"github.com/drycc/controller-sdk-go/api"
14+
"github.com/stretchr/testify/require"
1315
)
1416

1517
const (
@@ -48,9 +50,26 @@ const (
4850
}
4951
}`
5052

51-
tlsEnableExpected string = `{"https_enforced":true}`
52-
tlsDisableExpected string = `{"https_enforced":false}`
53-
issuerExpected string = `{"issuer":{"email":"anonymous@cert-manager.io","server":"https://acme-v02.api.letsencrypt.org/directory","key_id":"keyID","key_secret":"keySecret"}}`
53+
tlsEnableExpected string = `{"https_enforced":true}`
54+
tlsDisableExpected string = `{"https_enforced":false}`
55+
tlsCertsAutoEnabled string = `{"certs_auto_enabled":true}`
56+
tlsCertsAutoFixture string = `{
57+
"uuid": "c4aed81c-d1ca-4ff1-ab89-d2151264e1a3",
58+
"app": "foo",
59+
"owner": "test",
60+
"created": "2016-08-22T17:40:16Z",
61+
"updated": "2016-08-22T17:40:16Z",
62+
"https_enforced": null,
63+
"certs_auto_enabled": true,
64+
"issuer": {
65+
"email":"anonymous@cert-manager.io",
66+
"server":"https://acme-v02.api.letsencrypt.org/directory",
67+
"key_id":"keyID",
68+
"key_secret":"keySecret"
69+
},
70+
"events": [{"name": "foo", "kind": "Issuer", "time": "2024-04-08T01:14:49Z", "type": "Ready", "status": "True", "message": "ready message"}]
71+
}`
72+
issuerExpected string = `{"issuer":{"email":"anonymous@cert-manager.io","server":"https://acme-v02.api.letsencrypt.org/directory","key_id":"keyID","key_secret":"keySecret"}}`
5473
)
5574

5675
type fakeHTTPServer struct{}
@@ -84,6 +103,10 @@ func (fakeHTTPServer) ServeHTTP(res http.ResponseWriter, req *http.Request) {
84103
res.WriteHeader(http.StatusCreated)
85104
res.Write([]byte(issuerFixture))
86105
return
106+
} else if string(body) == tlsCertsAutoEnabled {
107+
res.WriteHeader(http.StatusCreated)
108+
res.Write([]byte(tlsCertsAutoFixture))
109+
return
87110
}
88111
fmt.Printf("Expected '%s', %s or '%s', Got '%s'\n",
89112
tlsEnableExpected,
@@ -288,6 +311,38 @@ func TestTLSDisable(t *testing.T) {
288311
}
289312
}
290313

314+
func TestEnableCertsAutoEnabled(t *testing.T) {
315+
t.Parallel()
316+
handler := fakeHTTPServer{}
317+
server := httptest.NewServer(handler)
318+
defer server.Close()
319+
320+
dClient, err := drycc.New(false, server.URL, "abc")
321+
if err != nil {
322+
t.Fatal(err)
323+
}
324+
325+
tls, err := EnableCertsAutoEnabled(dClient, "foo")
326+
if err != nil {
327+
t.Fatal(err)
328+
}
329+
330+
expected := []api.Event{
331+
{
332+
"name": "foo",
333+
"kind": "Issuer",
334+
"time": "2024-04-08T01:14:49Z",
335+
"type": "Ready",
336+
"status": "True",
337+
"message": "ready message",
338+
},
339+
}
340+
actual := tls.Events
341+
a, _ := json.Marshal(expected)
342+
b, _ := json.Marshal(actual)
343+
require.JSONEq(t, string(a), string(b), "Expected %v, Got %v", expected, actual)
344+
}
345+
291346
func TestAddIssuer(t *testing.T) {
292347
t.Parallel()
293348
handler := fakeHTTPServer{}
@@ -300,7 +355,6 @@ func TestAddIssuer(t *testing.T) {
300355
}
301356

302357
_, err = AddCertsIssuer(dClient, "foo", "anonymous@cert-manager.io", "https://acme-v02.api.letsencrypt.org/directory", "keyID", "keySecret")
303-
304358
if err != nil {
305359
t.Fatal(err)
306360
}

0 commit comments

Comments
 (0)