-
Notifications
You must be signed in to change notification settings - Fork 0
/
secret_resource.go
45 lines (38 loc) · 1.13 KB
/
secret_resource.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package daemon
import (
"fmt"
"net/http"
"net/url"
"github.com/leanovate/microtools/logging"
"github.com/leanovate/microtools/rest"
"github.com/untoldwind/trustless/secrets"
)
// SecretResource is a REST resource representing a specific secret
type SecretResource struct {
rest.ResourceBase
logger logging.Logger
secrets secrets.Secrets
secretID string
}
// NewSecretResource create a SecretResource
func NewSecretResource(secrets secrets.Secrets, secretID string, logger logging.Logger) *SecretResource {
return &SecretResource{
logger: logger.WithField("resource", "secret").WithField("secretID", secretID),
secrets: secrets,
secretID: secretID,
}
}
// Self link of the resource
func (r *SecretResource) Self() rest.Link {
return rest.SimpleLink(fmt.Sprintf("/v1/secrets/%s", url.QueryEscape(r.secretID)))
}
// Get the secret the resource represents
func (r *SecretResource) Get(request *http.Request) (interface{}, error) {
secret, err := r.secrets.Get(request.Context(), r.secretID)
if err == secrets.ErrSecretNotFound {
return nil, rest.HTTPNotFound
} else if err != nil {
return nil, err
}
return secret, nil
}