← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] lp:~jtv/gomaasapi/file-api into lp:gomaasapi

 

Jeroen T. Vermeulen has proposed merging lp:~jtv/gomaasapi/file-api into lp:gomaasapi.

Commit message:
Support file-information retrieval (/maas/api/1.0/files/<filename>) in the test service.

Requested reviews:
  MAAS Maintainers (maas-maintainers)

For more details, see:
https://code.launchpad.net/~jtv/gomaasapi/file-api/+merge/150790

There is one very unfortunate thing about this branch.  Given the latest changes in MAAS, an uploaded file in the test service should have an anon_resource_uri.  But the caller is not given the opportunity to supply it, because that would add a lot of weight to all calls to NewFile().  So we get a bit of a mix of caller-supplied and callee-supplied data.

So instead, the service computes an at least somewhat realistic anon_resource_uri by itself.  Callers shouldn't notice what's in there, really, because they shouldn't care.  The URI is meant for passing on unmodified.  But in a next step we should perhaps also support retrieval of the file through this URL.  I'm not entirely sure that that is needed for real-world testing, however: if you're in a position to obtain the anon_resource_uri, you've actually got the file's contents already.


Jeroen
-- 
https://code.launchpad.net/~jtv/gomaasapi/file-api/+merge/150790
Your team MAAS Maintainers is requested to review the proposed merge of lp:~jtv/gomaasapi/file-api into lp:gomaasapi.
=== modified file 'testservice.go'
--- testservice.go	2013-02-27 12:01:39 +0000
+++ testservice.go	2013-02-27 13:16:22 +0000
@@ -125,6 +125,12 @@
 	base64Content := base64.StdEncoding.EncodeToString(filecontent)
 	attrs["content"] = base64Content
 	attrs["filename"] = filename
+
+	// Allocate an arbitrary URL here.  It would be nice if the caller
+	// could do this, but that would change the API and require many
+	// changes.
+	attrs["anon_resource_uri"] = "/maas/1.0/files/?op=get_by_key&key=" + filename + "_key"
+
 	obj := newJSONMAASObject(attrs, server.client)
 	server.files[filename] = obj
 	return obj
@@ -350,7 +356,20 @@
 	case r.Method == "DELETE":
 		delete(server.files, filename)
 		w.WriteHeader(http.StatusOK)
-		return
+	case r.Method == "GET":
+		// Retrieve a file's information (including content) as a JSON
+		// object.
+		file, ok := server.files[filename]
+		if !ok {
+			http.NotFoundHandler().ServeHTTP(w, r)
+			return
+		}
+		jsonText, err := json.Marshal(file)
+		if err != nil {
+			panic(err)
+		}
+		w.WriteHeader(http.StatusOK)
+		fmt.Fprint(w, string(jsonText))
 	default:
 		// Default handler: not found.
 		http.NotFoundHandler().ServeHTTP(w, r)

=== modified file 'testservice_test.go'
--- testservice_test.go	2013-02-27 09:21:07 +0000
+++ testservice_test.go	2013-02-27 13:16:22 +0000
@@ -191,6 +191,32 @@
 	c.Check(field, Equals, base64.StdEncoding.EncodeToString(fileContent))
 }
 
+func (suite *TestServerSuite) TestHandlesFile(c *C) {
+	const filename = "my-file"
+	const fileContent = "test file content"
+	file := suite.server.NewFile(filename, []byte(fileContent))
+	getURI := fmt.Sprintf("/api/%s/files/%s/", suite.server.version, filename)
+	fileURI, err := file.GetField("anon_resource_uri")
+
+	resp, err := http.Get(suite.server.Server.URL + getURI)
+	c.Check(err, IsNil)
+
+	c.Check(resp.StatusCode, Equals, http.StatusOK)
+	content, err := ioutil.ReadAll(resp.Body)
+	c.Assert(err, IsNil)
+	var obj map[string]interface{}
+	err = json.Unmarshal(content, &obj)
+	c.Assert(err, IsNil)
+	anon_url, ok := obj["anon_resource_uri"]
+	c.Check(ok, Equals, true)
+	c.Check(anon_url.(string), Equals, fileURI)
+	base64Content, ok := obj["content"]
+	c.Check(ok, Equals, true)
+	decodedContent, err := base64.StdEncoding.DecodeString(base64Content.(string))
+	c.Assert(err, IsNil)
+	c.Check(string(decodedContent), Equals, fileContent)
+}
+
 func (suite *TestServerSuite) TestHandlesGetFile(c *C) {
 	fileContent := []byte("test file content")
 	fileName := "filename"