-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresources_mig.go
167 lines (149 loc) · 4.51 KB
/
resources_mig.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Copyright 2017 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package apiserver
import (
"io"
"net/http"
"net/url"
"strconv"
charmresource "github.com/juju/charm/v10/resource"
"github.com/juju/errors"
"github.com/juju/juju/core/resources"
"github.com/juju/juju/rpc/params"
"github.com/juju/juju/state"
)
// resourcesMigrationUploadHandler handles resources uploads for model migrations.
type resourcesMigrationUploadHandler struct {
ctxt httpContext
stateAuthFunc func(*http.Request) (*state.PooledState, error)
}
func (h *resourcesMigrationUploadHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
// Validate before authenticate because the authentication is dependent
// on the state connection that is determined during the validation.
st, err := h.stateAuthFunc(r)
if err != nil {
if err := sendError(w, err); err != nil {
logger.Errorf("%v", err)
}
return
}
defer st.Release()
switch r.Method {
case "POST":
res, err := h.processPost(r, st.State)
if err != nil {
if err := sendError(w, err); err != nil {
logger.Errorf("%v", err)
}
return
}
if err := sendStatusAndJSON(w, http.StatusOK, ¶ms.ResourceUploadResult{
ID: res.ID,
Timestamp: res.Timestamp,
}); err != nil {
logger.Errorf("%v", err)
}
default:
if err := sendError(w, errors.MethodNotAllowedf("unsupported method: %q", r.Method)); err != nil {
logger.Errorf("%v", err)
}
}
}
// processPost handles resources upload POST request after
// authentication.
func (h *resourcesMigrationUploadHandler) processPost(r *http.Request, st *state.State) (resources.Resource, error) {
var empty resources.Resource
query := r.URL.Query()
target, isUnit, err := getUploadTarget(query)
if err != nil {
return empty, errors.Trace(err)
}
userID := query.Get("user") // Is allowed to be blank
res, err := queryToResource(query)
if err != nil {
return empty, errors.Trace(err)
}
rSt := st.Resources()
reader := r.Body
// Don't associate content with a placeholder resource.
if isPlaceholder(query) {
reader = nil
}
outRes, err := setResource(isUnit, target, userID, res, reader, rSt)
if err != nil {
return empty, errors.Annotate(err, "resource upload failed")
}
return outRes, nil
}
func setResource(isUnit bool, target, user string, res charmresource.Resource, r io.Reader, rSt state.Resources) (
resources.Resource, error,
) {
if isUnit {
return rSt.SetUnitResource(target, user, res)
}
return rSt.SetResource(target, user, res, r, state.DoNotIncrementCharmModifiedVersion)
}
func isPlaceholder(query url.Values) bool {
return query.Get("timestamp") == ""
}
func getUploadTarget(query url.Values) (string, bool, error) {
appName := query.Get("application")
unitName := query.Get("unit")
switch {
case appName == "" && unitName == "":
return "", false, errors.BadRequestf("missing application/unit")
case appName != "" && unitName != "":
return "", false, errors.BadRequestf("application and unit can't be set at the same time")
case appName != "":
return appName, false, nil
default:
return unitName, true, nil
}
}
func queryToResource(query url.Values) (charmresource.Resource, error) {
var err error
empty := charmresource.Resource{}
res := charmresource.Resource{
Meta: charmresource.Meta{
Name: query.Get("name"),
Path: query.Get("path"),
Description: query.Get("description"),
},
}
if res.Name == "" {
return empty, errors.BadRequestf("missing name")
}
res.Type, err = charmresource.ParseType(query.Get("type"))
if err != nil {
return empty, errors.BadRequestf("invalid type")
}
res.Origin, err = charmresource.ParseOrigin(query.Get("origin"))
if err != nil {
return empty, errors.BadRequestf("invalid origin")
}
res.Revision, err = strconv.Atoi(query.Get("revision"))
if err != nil {
return empty, errors.BadRequestf("invalid revision")
}
res.Size, err = strconv.ParseInt(query.Get("size"), 10, 64)
if err != nil {
return empty, errors.BadRequestf("invalid size")
}
switch res.Type {
case charmresource.TypeFile:
if res.Path == "" {
return empty, errors.BadRequestf("missing path")
}
res.Fingerprint, err = charmresource.ParseFingerprint(query.Get("fingerprint"))
if err != nil {
return empty, errors.BadRequestf("invalid fingerprint")
}
case charmresource.TypeContainerImage:
res.Fingerprint, err = charmresource.ParseFingerprint(query.Get("fingerprint"))
if err != nil {
// Old resources do not have fingerprints.
res.Fingerprint = charmresource.Fingerprint{}
}
}
return res, nil
}