Repository: osbuild/osbuild-composer
Pull request #144 (2019-12-05 13:59:38)

api: implement /compose/info route
Not everything is implemented in this route but enough to pass the tests.


Patch:
diff --git a/internal/weldr/api.go b/internal/weldr/api.go
index 821826a7c..742c44a55 100644
--- a/internal/weldr/api.go
+++ b/internal/weldr/api.go
@@ -1350,8 +1350,54 @@ func (api *API) composeInfoHandler(writer http.ResponseWriter, request *http.Req
 		return
 	}
 
-	// TODO: implement this route for v0 and v1 (they differ)
-	notImplementedHandler(writer, request, params)
+	uuidString := params.ByName("uuid")
+	id, err := uuid.Parse(uuidString)
+	if err != nil {
+		errors := responseError{
+			ID:  "UnknownUUID",
+			Msg: fmt.Sprintf("%s is not a valid build uuid", uuidString),
+		}
+		statusResponseError(writer, http.StatusBadRequest, errors)
+		return
+	}
+
+	compose, exists := api.store.GetCompose(id)
+
+	if !exists {
+		errors := responseError{
+			ID:  "UnknownUUID",
+			Msg: fmt.Sprintf("%s is not a valid build uuid", uuidString),
+		}
+		statusResponseError(writer, http.StatusBadRequest, errors)
+		return
+	}
+
+	var reply struct {
+		ID          uuid.UUID            `json:"id"`
+		Config      string               `json:"config"`    // anaconda config, let's ignore this field
+		Blueprint   *blueprint.Blueprint `json:"blueprint"` // blueprint not frozen!
+		Commit      string               `json:"commit"`    // empty for now
+		Deps        []string             `json:"deps"`      // empty for now
+		ComposeType string               `json:"compose_type"`
+		QueueStatus string               `json:"queue_status"`
+		ImageSize   int64                `json:"image_size"`
+		Uploads     []UploadResponse     `json:"uploads,omitempty"`
+	}
+
+	reply.ID = id
+	reply.Blueprint = compose.Blueprint
+	reply.Deps = []string{}
+	reply.ComposeType = compose.OutputType
+	reply.QueueStatus = compose.QueueStatus
+	if compose.Image != nil {
+		reply.ImageSize = compose.Image.Size
+	}
+
+	if isRequestVersionAtLeast(params, 1) {
+		reply.Uploads = TargetsToUploadResponses(compose.Targets)
+	}
+
+	json.NewEncoder(writer).Encode(reply)
 }
 
 func (api *API) composeImageHandler(writer http.ResponseWriter, request *http.Request, params httprouter.Params) {


Test patch:
diff --git a/internal/weldr/api_test.go b/internal/weldr/api_test.go
index 998ef7f85..893be40e6 100644
--- a/internal/weldr/api_test.go
+++ b/internal/weldr/api_test.go
@@ -364,6 +364,31 @@ func TestComposeStatus(t *testing.T) {
 	}
 }
 
+func TestComposeInfo(t *testing.T) {
+	var cases = []struct {
+		Fixture        rpmmd_mock.FixtureGenerator
+		Method         string
+		Path           string
+		Body           string
+		ExpectedStatus int
+		ExpectedJSON   string
+	}{
+		{rpmmd_mock.BaseFixture, "GET", "/api/v0/compose/info/30000000-0000-0000-0000-000000000000", ``, http.StatusOK, `{"id":"30000000-0000-0000-0000-000000000000","config":"","blueprint":{"name":"test","description":"","version":"0.0.0","packages":[],"modules":[],"groups":[]},"commit":"","deps":[],"compose_type":"tar","queue_status":"WAITING","image_size":0}`},
+		{rpmmd_mock.BaseFixture, "GET", "/api/v1/compose/info/30000000-0000-0000-0000-000000000000", ``, http.StatusOK, `{"id":"30000000-0000-0000-0000-000000000000","config":"","blueprint":{"name":"test","description":"","version":"0.0.0","packages":[],"modules":[],"groups":[]},"commit":"","deps":[],"compose_type":"tar","queue_status":"WAITING","image_size":0,"uploads":[{"uuid":"10000000-0000-0000-0000-000000000000","status":"WAITING","provider_name":"aws","image_name":"awsimage","creation_time":1574857140,"settings":{"region":"frankfurt","accessKeyID":"accesskey","secretAccessKey":"secretkey","bucket":"clay","key":"imagekey"}}]}`},
+		{rpmmd_mock.BaseFixture, "GET", "/api/v1/compose/info/30000000-0000-0000-0000", ``, http.StatusBadRequest, `{"status":false,"errors":[{"id":"UnknownUUID","msg":"30000000-0000-0000-0000 is not a valid build uuid"}]}`},
+		{rpmmd_mock.BaseFixture, "GET", "/api/v1/compose/info/42000000-0000-0000-0000-000000000000", ``, http.StatusBadRequest, `{"status":false,"errors":[{"id":"UnknownUUID","msg":"42000000-0000-0000-0000-000000000000 is not a valid build uuid"}]}`},
+	}
+
+	if len(os.Getenv("OSBUILD_COMPOSER_TEST_EXTERNAL")) > 0 {
+		t.Skip("This test is for internal testing only")
+	}
+
+	for _, c := range cases {
+		api, _ := createWeldrAPI(rpmmd_mock.BaseFixture)
+		test.TestRoute(t, api, false, c.Method, c.Path, c.Body, c.ExpectedStatus, c.ExpectedJSON)
+	}
+}
+
 func TestComposeQueue(t *testing.T) {
 	var cases = []struct {
 		Fixture        rpmmd_mock.FixtureGenerator