forked from juju/juju
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rest.go
148 lines (131 loc) · 4.28 KB
/
rest.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Copyright 2017 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package apiserver
import (
"bytes"
"io"
"mime"
"net/http"
"strconv"
"github.com/juju/errors"
"github.com/juju/juju/apiserver/common"
"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/state"
"github.com/juju/juju/state/storage"
)
// RestHTTPHandler creates is a http.Handler which serves ReST requests.
type RestHTTPHandler struct {
GetHandler FailableHandlerFunc
}
// ServeHTTP is defined on handler.Handler.
func (h *RestHTTPHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
var err error
switch r.Method {
case "GET":
err = errors.Annotate(h.GetHandler(w, r), "cannot retrieve model data")
default:
err = emitUnsupportedMethodErr(r.Method)
}
if err != nil {
if err := sendJSONError(w, r, errors.Trace(err)); err != nil {
logger.Errorf("%v", errors.Annotate(err, "cannot return error to user"))
}
}
}
// modelRestHandler handles ReST requests through HTTPS in the API server.
type modelRestHandler struct {
ctxt httpContext
dataDir string
stateAuthFunc func(*http.Request) (*state.State, state.StatePoolReleaser, error)
}
// ServeGet handles http GET requests.
func (h *modelRestHandler) ServeGet(w http.ResponseWriter, r *http.Request) error {
if r.Method != "GET" {
return errors.Trace(emitUnsupportedMethodErr(r.Method))
}
st, releaser, _, err := h.ctxt.stateForRequestAuthenticated(r)
if err != nil {
return errors.Trace(err)
}
defer releaser()
return errors.Trace(h.processGet(r, w, st))
}
// processGet handles a ReST GET request after authentication.
func (h *modelRestHandler) processGet(r *http.Request, w http.ResponseWriter, st *state.State) error {
query := r.URL.Query()
entity := query.Get(":entity")
// TODO(wallyworld) - support more than just "remote-application"
switch entity {
case "remote-application":
return h.processRemoteApplication(r, w, st)
default:
return errors.NotSupportedf("entity %v", entity)
}
}
// processRemoteApplication handles a request for attributes on remote applications.
func (h *modelRestHandler) processRemoteApplication(r *http.Request, w http.ResponseWriter, st *state.State) error {
query := r.URL.Query()
name := query.Get(":name")
remoteApp, err := st.RemoteApplication(name)
if err != nil {
return errors.Trace(err)
}
attribute := query.Get(":attribute")
// TODO(wallyworld) - support more than just "icon"
if attribute != "icon" {
return errors.NotSupportedf("attribute %v on entity %v", attribute, name)
}
// Get the backend state for the source model so we can lookup the app in that model to get the charm details.
offerUUID := remoteApp.OfferUUID()
sourceModelUUID := remoteApp.SourceModel().Id()
sourceSt, releaser, err := h.ctxt.srv.statePool.Get(sourceModelUUID)
if err != nil {
return errors.Trace(err)
}
defer releaser()
offers := state.NewApplicationOffers(sourceSt)
offer, err := offers.ApplicationOfferForUUID(offerUUID)
if err != nil {
return errors.Trace(err)
}
app, err := sourceSt.Application(offer.ApplicationName)
if err != nil {
return errors.Trace(err)
}
ch, _, err := app.Charm()
if err != nil {
return errors.Trace(err)
}
store := storage.NewStorage(sourceSt.ModelUUID(), sourceSt.MongoSession())
// Use the storage to retrieve and save the charm archive.
charmPath, err := common.ReadCharmFromStorage(store, h.dataDir, ch.StoragePath())
if errors.IsNotFound(err) {
return h.byteSender(w, ".svg", []byte(common.DefaultCharmIcon))
}
if err != nil {
return errors.Trace(err)
}
iconContents, err := common.CharmArchiveEntry(charmPath, "icon.svg", true)
if errors.IsNotFound(err) {
return h.byteSender(w, ".svg", []byte(common.DefaultCharmIcon))
}
if err != nil {
return errors.Trace(err)
}
return h.byteSender(w, ".svg", iconContents)
}
func (h *modelRestHandler) byteSender(w http.ResponseWriter, ext string, contents []byte) error {
ctype := mime.TypeByExtension(ext)
if ctype != "" {
// Older mime.types may map .js to x-javascript.
// Map it to javascript for consistency.
if ctype == params.ContentTypeXJS {
ctype = params.ContentTypeJS
}
w.Header().Set("Content-Type", ctype)
}
w.Header().Set("Content-Length", strconv.Itoa(len(contents)))
w.WriteHeader(http.StatusOK)
io.Copy(w, bytes.NewReader(contents))
return nil
}