Skip to content

Commit 0b7122f

Browse files
author
Uwe Schwaeke
committed
feat(glance): serve metadefs schema documents for Horizon
Horizon's Angular "Create Image" modal loads GET /v2/schemas/metadefs/namespace before it renders. Lightstack had no such route, so the request 404'd, the modal's response handler read `.data` off undefined (TypeError), and the create failed with "Unable to retrieve namespace / project / create image". Add the static metadefs schema documents upstream Glance serves (namespace[s], resource_type[s], object[s], propert{y,ies}, tag[s]). They are fixed JSON, so they need no database and no auth-specific state. Assisted-by: Claude:claude-opus-4-8 Signed-off-by: Uwe Schwaeke <uwe.schwaeke@sap.com>
1 parent faece7b commit 0b7122f

3 files changed

Lines changed: 164 additions & 0 deletions

File tree

‎internal/glance/images.go‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,17 @@ func (svc *Service) RegisterRoutes(r *gin.RouterGroup) {
114114
r.GET("/schemas/images", svc.GetImagesSchema)
115115
r.GET("/schemas/member", svc.GetMemberSchema)
116116
r.GET("/schemas/members", svc.GetMembersSchema)
117+
// Metadefs schema documents (Horizon Create Image loads /schemas/metadefs/namespace).
118+
r.GET("/schemas/metadefs/namespace", svc.GetMetadefNamespaceSchema)
119+
r.GET("/schemas/metadefs/namespaces", svc.GetMetadefNamespacesSchema)
120+
r.GET("/schemas/metadefs/resource_type", svc.GetMetadefResourceTypeSchema)
121+
r.GET("/schemas/metadefs/resource_types", svc.GetMetadefResourceTypeSchema)
122+
r.GET("/schemas/metadefs/object", svc.GetMetadefObjectSchema)
123+
r.GET("/schemas/metadefs/objects", svc.GetMetadefObjectSchema)
124+
r.GET("/schemas/metadefs/property", svc.GetMetadefPropertySchema)
125+
r.GET("/schemas/metadefs/properties", svc.GetMetadefPropertySchema)
126+
r.GET("/schemas/metadefs/tag", svc.GetMetadefTagSchema)
127+
r.GET("/schemas/metadefs/tags", svc.GetMetadefTagSchema)
117128

118129
// Tasks and import
119130
r.POST("/tasks", svc.CreateTask)

‎internal/glance/metadefs.go‎

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,3 +319,106 @@ func (svc *Service) ListMetadefResourceTypes(c *gin.Context) {
319319

320320
c.JSON(http.StatusOK, gin.H{"resource_types": resourceTypes})
321321
}
322+
323+
// --- Metadefs schema documents (GET /v2/schemas/metadefs/*) ---
324+
//
325+
// Upstream Glance serves these static JSON schemas; Horizon's Angular "Create Image"
326+
// modal loads /v2/schemas/metadefs/namespace before it renders. Without the route it
327+
// 404s, the modal's response handler reads `.data` off undefined (TypeError), and the
328+
// create fails with "Unable to retrieve namespace / project / create image". These are
329+
// fixed documents, so they need no database and no auth-specific state.
330+
331+
// GetMetadefNamespaceSchema handles GET /v2/schemas/metadefs/namespace.
332+
func (svc *Service) GetMetadefNamespaceSchema(c *gin.Context) {
333+
c.JSON(http.StatusOK, gin.H{
334+
"name": "namespace",
335+
"additionalProperties": false,
336+
"properties": gin.H{
337+
"namespace": gin.H{"type": "string", "description": "The unique namespace text."},
338+
"display_name": gin.H{"type": "string", "description": "The user friendly name for the namespace."},
339+
"description": gin.H{"type": "string", "description": "Provides a user friendly description of the namespace."},
340+
"visibility": gin.H{"type": "string", "enum": []string{"public", "private"}, "description": "Scope of namespace accessibility."},
341+
"protected": gin.H{"type": "boolean", "description": "If true, namespace will not be deletable."},
342+
"owner": gin.H{"type": "string", "description": "Owner of the namespace."},
343+
"created_at": gin.H{"type": "string", "format": "date-time"},
344+
"updated_at": gin.H{"type": "string", "format": "date-time"},
345+
"self": gin.H{"type": "string"},
346+
"schema": gin.H{"type": "string"},
347+
"resource_type_associations": gin.H{"type": "array"},
348+
"properties": gin.H{"type": "object"},
349+
"objects": gin.H{"type": "array"},
350+
"tags": gin.H{"type": "array"},
351+
},
352+
})
353+
}
354+
355+
// GetMetadefNamespacesSchema handles GET /v2/schemas/metadefs/namespaces.
356+
func (svc *Service) GetMetadefNamespacesSchema(c *gin.Context) {
357+
c.JSON(http.StatusOK, gin.H{
358+
"name": "namespaces",
359+
"properties": gin.H{
360+
"namespaces": gin.H{"type": "array", "items": gin.H{"type": "object", "name": "namespace"}},
361+
"first": gin.H{"type": "string"},
362+
"next": gin.H{"type": "string"},
363+
"schema": gin.H{"type": "string"},
364+
},
365+
})
366+
}
367+
368+
// GetMetadefResourceTypeSchema handles GET /v2/schemas/metadefs/resource_type[s].
369+
func (svc *Service) GetMetadefResourceTypeSchema(c *gin.Context) {
370+
c.JSON(http.StatusOK, gin.H{
371+
"name": "resource_type_association",
372+
"properties": gin.H{
373+
"name": gin.H{"type": "string"},
374+
"prefix": gin.H{"type": "string"},
375+
"properties_target": gin.H{"type": "string"},
376+
"created_at": gin.H{"type": "string", "format": "date-time"},
377+
"updated_at": gin.H{"type": "string", "format": "date-time"},
378+
},
379+
})
380+
}
381+
382+
// GetMetadefObjectSchema handles GET /v2/schemas/metadefs/object[s].
383+
func (svc *Service) GetMetadefObjectSchema(c *gin.Context) {
384+
c.JSON(http.StatusOK, gin.H{
385+
"name": "object",
386+
"properties": gin.H{
387+
"name": gin.H{"type": "string"},
388+
"description": gin.H{"type": "string"},
389+
"required": gin.H{"type": "array"},
390+
"properties": gin.H{"type": "object"},
391+
"schema": gin.H{"type": "string"},
392+
"self": gin.H{"type": "string"},
393+
"created_at": gin.H{"type": "string", "format": "date-time"},
394+
"updated_at": gin.H{"type": "string", "format": "date-time"},
395+
},
396+
})
397+
}
398+
399+
// GetMetadefPropertySchema handles GET /v2/schemas/metadefs/property|properties.
400+
func (svc *Service) GetMetadefPropertySchema(c *gin.Context) {
401+
c.JSON(http.StatusOK, gin.H{
402+
"name": "property",
403+
"properties": gin.H{
404+
"name": gin.H{"type": "string"},
405+
"title": gin.H{"type": "string"},
406+
"type": gin.H{"type": "string"},
407+
"description": gin.H{"type": "string"},
408+
"default": gin.H{"type": "string"},
409+
"enum": gin.H{"type": "array"},
410+
},
411+
})
412+
}
413+
414+
// GetMetadefTagSchema handles GET /v2/schemas/metadefs/tag[s].
415+
func (svc *Service) GetMetadefTagSchema(c *gin.Context) {
416+
c.JSON(http.StatusOK, gin.H{
417+
"name": "tag",
418+
"properties": gin.H{
419+
"name": gin.H{"type": "string"},
420+
"created_at": gin.H{"type": "string", "format": "date-time"},
421+
"updated_at": gin.H{"type": "string", "format": "date-time"},
422+
},
423+
})
424+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package glance
2+
3+
import (
4+
"encoding/json"
5+
"net/http"
6+
"net/http/httptest"
7+
"testing"
8+
9+
"github.com/gin-gonic/gin"
10+
)
11+
12+
// The Glance metadefs schema routes must answer 200 with a schema object. Horizon's
13+
// Create Image modal loads /v2/schemas/metadefs/namespace and reads `.data` off the
14+
// response; a 404 there makes the modal fail ("Unable to retrieve namespace"). Guard
15+
// each registered path against regressing to a 404.
16+
func TestMetadefsSchemaRoutes(t *testing.T) {
17+
gin.SetMode(gin.TestMode)
18+
svc := &Service{}
19+
r := gin.New()
20+
r.GET("/v2/schemas/metadefs/namespace", svc.GetMetadefNamespaceSchema)
21+
r.GET("/v2/schemas/metadefs/namespaces", svc.GetMetadefNamespacesSchema)
22+
r.GET("/v2/schemas/metadefs/resource_type", svc.GetMetadefResourceTypeSchema)
23+
r.GET("/v2/schemas/metadefs/object", svc.GetMetadefObjectSchema)
24+
r.GET("/v2/schemas/metadefs/property", svc.GetMetadefPropertySchema)
25+
r.GET("/v2/schemas/metadefs/tag", svc.GetMetadefTagSchema)
26+
27+
for _, path := range []string{
28+
"/v2/schemas/metadefs/namespace",
29+
"/v2/schemas/metadefs/namespaces",
30+
"/v2/schemas/metadefs/resource_type",
31+
"/v2/schemas/metadefs/object",
32+
"/v2/schemas/metadefs/property",
33+
"/v2/schemas/metadefs/tag",
34+
} {
35+
w := httptest.NewRecorder()
36+
r.ServeHTTP(w, httptest.NewRequest(http.MethodGet, path, nil))
37+
if w.Code != http.StatusOK {
38+
t.Errorf("GET %s = %d, want 200", path, w.Code)
39+
continue
40+
}
41+
var body map[string]any
42+
if err := json.Unmarshal(w.Body.Bytes(), &body); err != nil {
43+
t.Errorf("GET %s: body not JSON: %v", path, err)
44+
continue
45+
}
46+
if _, ok := body["properties"]; !ok {
47+
t.Errorf("GET %s: schema missing 'properties': %s", path, w.Body.String())
48+
}
49+
}
50+
}

0 commit comments

Comments
 (0)