-
Notifications
You must be signed in to change notification settings - Fork 0
/
resources_unit.go
71 lines (62 loc) · 1.88 KB
/
resources_unit.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Copyright 2017 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package apiserver
import (
"fmt"
"io"
"net/http"
"github.com/juju/errors"
"github.com/juju/names/v4"
"github.com/juju/juju/core/resources"
"github.com/juju/juju/rpc/params"
"github.com/juju/juju/state"
)
// UnitResourcesHandler is the HTTP handler for unit agent downloads of
// resources.
type UnitResourcesHandler struct {
NewOpener func(*http.Request, ...string) (resources.Opener, state.PoolHelper, error)
}
// ServeHTTP implements http.Handler.
func (h *UnitResourcesHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
switch req.Method {
case "GET":
opener, ph, err := h.NewOpener(req, names.UnitTagKind, names.ApplicationTagKind)
if err != nil {
if err := sendError(resp, err); err != nil {
logger.Errorf("%v", err)
}
return
}
defer ph.Release()
name := req.URL.Query().Get(":resource")
opened, err := opener.OpenResource(name)
if err != nil {
if errors.IsNotFound(err) {
// non internal errors is not real errors.
logger.Warningf("cannot fetch resource reader: %v", err)
} else {
logger.Errorf("cannot fetch resource reader: %v", err)
}
if err := sendError(resp, err); err != nil {
logger.Errorf("%v", err)
}
return
}
defer opened.Close()
hdr := resp.Header()
hdr.Set("Content-Type", params.ContentTypeRaw)
hdr.Set("Content-Length", fmt.Sprint(opened.Size))
hdr.Set("Content-Sha384", opened.Fingerprint.String())
resp.WriteHeader(http.StatusOK)
if _, err := io.Copy(resp, opened); err != nil {
// We cannot use SendHTTPError here, so we log the error
// and move on.
logger.Errorf("unable to complete stream for resource: %v", err)
return
}
default:
if err := sendError(resp, errors.MethodNotAllowedf("unsupported method: %q", req.Method)); err != nil {
logger.Errorf("%v", err)
}
}
}