charms HTTPS API: allow for providing the default charm icon. #6352

Merged
merged 2 commits into from Sep 30, 2016

Conversation

Projects
None yet
4 participants
Member

frankban commented Sep 29, 2016

This is done so that the GUI can safely user the Juju URL to the icon even in the case of local charms without an icon.
The API change here is backward compatible.

LGTM

LGTM modulo few suggestions, thanks!

apiserver/charms.go
@@ -78,7 +78,7 @@ func (h *charmsHandler) serveGet(w http.ResponseWriter, r *http.Request) error {
// Retrieve or list charm files.
// Requires "url" (charm URL) and an optional "file" (the path to the
// charm file) to be included in the query.
@rogpeppe

rogpeppe Sep 30, 2016

Owner

Please document the new "icon" query value here.

apiserver/charms.go
@@ -78,7 +78,7 @@ func (h *charmsHandler) serveGet(w http.ResponseWriter, r *http.Request) error {
// Retrieve or list charm files.
// Requires "url" (charm URL) and an optional "file" (the path to the
// charm file) to be included in the query.
- charmArchivePath, fileArg, err := h.processGet(r, st)
+ charmArchivePath, fileArg, serveDefaultIcon, err := h.processGet(r, st)
@rogpeppe

rogpeppe Sep 30, 2016

Owner

Perhaps "serveIcon" would be a more appropriate name, as that's the name of the
query value and what we're really doing is asking for the icon, which a fallback to
the default icon only when needed.

apiserver/charms.go
@@ -191,7 +193,15 @@ func (h *charmsHandler) archiveEntrySender(filePath string) bundleContentSenderF
io.Copy(w, contents)
return nil
}
- return errors.NotFoundf("charm")
+ // Serve the default icon (if the charm icon was requested), or return
+ // a 404 not found response.
@rogpeppe

rogpeppe Sep 30, 2016

Owner

I think this comment would read better inside the if statement:

if serveDefaultIcon {
    // An icon was requested but none was found in the archive so
    // return the default icon instead.
    ....
}
// No file found
apiserver/charms.go
-func (h *charmsHandler) processGet(r *http.Request, st *state.State) (string, string, error) {
+// It returns the bundle path, the requested file path (if any), whether the
+// default charm icon has been requested and an error.
+func (h *charmsHandler) processGet(r *http.Request, st *state.State) (string, string, bool, error) {
@rogpeppe

rogpeppe Sep 30, 2016

Owner

Perhaps name the return parameters to make it more obvious?

apiserver/charms.go
query := r.URL.Query()
// Retrieve and validate query parameters.
curlString := query.Get("url")
if curlString == "" {
- return "", "", fmt.Errorf("expected url=CharmURL query argument")
+ return "", "", false, fmt.Errorf("expected url=CharmURL query argument")
@rogpeppe

rogpeppe Sep 30, 2016

Owner

I saw a nice idiom recently that can reduce the boilerplate in this kind
of case. Define a local variable:

errRet := func(err error) (string, string, bool, error) {
     return "", "", false, err
}

then all your returns can just be:

return errRet(fmt.Errorf(....))

and if you want to change the return parameters, you just need to change it one
place. Useful for large value-type names too.

@frankban

frankban Sep 30, 2016

Member

Nice, done.

apiserver/charms_test.go
+ expectBody: "1",
+ }}
+
+ // Run the tests.
@rogpeppe

rogpeppe Sep 30, 2016

Owner

This isn't a hugely useful comment :)

@frankban

frankban Sep 30, 2016

Member

Haha right, removed.

+package apiserver
+
+// defaultIcon holds the default charm icon SVG content.
+// Keep this in sync with the default icon returned by the charm store.
@rogpeppe

rogpeppe Sep 30, 2016

Owner

What's the plan for that? A CI 'bot test somewhere?

@frankban

frankban Sep 30, 2016

Member

Perhaps, that would be interesting to implement. Although the default icon never changed, so it could be overkill.

@@ -0,0 +1,335 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
@rogpeppe

rogpeppe Sep 30, 2016

Owner

Can't we use an in-memory test charm rather than adding to the global charm repo charms?

Also, does the icon have to be the actual icon SVG - just a token amount of text would probably do.

@frankban

frankban Sep 30, 2016

Member

Given we have some charms there already I've found adding an icon simpler. We could convert all these tests to use in memory charms, I'd prefer not to do that in this charm. Also, I just aded the real mysql icon.

frankban added some commits Sep 30, 2016

charms HTTPS API: allow for providing the default charm icon.
This is done so that the GUI can safely user the Juju URL to the icon even in the case of local charms without an icon.
The API change here is backward compatible.
Member

frankban commented Sep 30, 2016

Thank you for the reviews!
$$merge$$

Contributor

jujubot commented Sep 30, 2016

Status: merge request accepted. Url: http://juju-ci.vapour.ws:8080/job/github-merge-juju

@jujubot jujubot merged commit 4c592f1 into juju:master Sep 30, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment