Skip to content

Commit

Permalink
Enable Get image API
Browse files Browse the repository at this point in the history
  • Loading branch information
cb-github-robot authored Oct 14, 2024
2 parents 9320309 + 76949f0 commit 116ee55
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 20 deletions.
4 changes: 2 additions & 2 deletions src/api/rest/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -6724,7 +6724,7 @@ const docTemplate = `{
},
"/ns/{nsId}/resources/image/{imageId}": {
"get": {
"description": "Get image",
"description": "GetImage returns an image object if there are matched images for the given namespace and imageKey(Id, CspResourceName, GuestOS,...)",
"consumes": [
"application/json"
],
Expand All @@ -6747,7 +6747,7 @@ const docTemplate = `{
},
{
"type": "string",
"description": "Image ID ({providerName}+{regionName}+{cspImageName})",
"description": "(Note: imageId param will be refined in next release, enabled for temporal support) This param accepts vaious input types as Image Key: [1. registerd ID: ({providerName}+{regionName}+{GuestOS}). 2. cspImageName. 3. GuestOS)]",
"name": "imageId",
"in": "path",
"required": true
Expand Down
4 changes: 2 additions & 2 deletions src/api/rest/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -6717,7 +6717,7 @@
},
"/ns/{nsId}/resources/image/{imageId}": {
"get": {
"description": "Get image",
"description": "GetImage returns an image object if there are matched images for the given namespace and imageKey(Id, CspResourceName, GuestOS,...)",
"consumes": [
"application/json"
],
Expand All @@ -6740,7 +6740,7 @@
},
{
"type": "string",
"description": "Image ID ({providerName}+{regionName}+{cspImageName})",
"description": "(Note: imageId param will be refined in next release, enabled for temporal support) This param accepts vaious input types as Image Key: [1. registerd ID: ({providerName}+{regionName}+{GuestOS}). 2. cspImageName. 3. GuestOS)]",
"name": "imageId",
"in": "path",
"required": true
Expand Down
8 changes: 6 additions & 2 deletions src/api/rest/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5097,7 +5097,8 @@ paths:
tags:
- "[Infra Resource] Image Management"
summary: Get image
description: Get image
description: "GetImage returns an image object if there are matched images for\
\ the given namespace and imageKey(Id, CspResourceName, GuestOS,...)"
operationId: GetImage
parameters:
- name: nsId
Expand All @@ -5109,7 +5110,10 @@ paths:
default: system
- name: imageId
in: path
description: "Image ID ({providerName}+{regionName}+{cspImageName})"
description: "(Note: imageId param will be refined in next release, enabled\
\ for temporal support) This param accepts vaious input types as Image Key:\
\ [1. registerd ID: ({providerName}+{regionName}+{GuestOS}). 2. cspImageName.\
\ 3. GuestOS)]"
required: true
schema:
type: string
Expand Down
13 changes: 9 additions & 4 deletions src/api/rest/server/resource/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,19 +215,24 @@ func RestFetchImages(c echo.Context) error {
// RestGetImage godoc
// @ID GetImage
// @Summary Get image
// @Description Get image
// @Description GetImage returns an image object if there are matched images for the given namespace and imageKey(Id, CspResourceName, GuestOS,...)
// @Tags [Infra Resource] Image Management
// @Accept json
// @Produce json
// @Param nsId path string true "Namespace ID" default(system)
// @Param imageId path string true "Image ID ({providerName}+{regionName}+{cspImageName})"
// @Param imageId path string true "(Note: imageId param will be refined in next release, enabled for temporal support) This param accepts vaious input types as Image Key: [1. registerd ID: ({providerName}+{regionName}+{GuestOS}). 2. cspImageName. 3. GuestOS)]"
// @Success 200 {object} model.TbImageInfo
// @Failure 404 {object} model.SimpleMsg
// @Failure 500 {object} model.SimpleMsg
// @Router /ns/{nsId}/resources/image/{imageId} [get]
func RestGetImage(c echo.Context) error {
// This is a dummy function for Swagger.
return nil
nsId := c.Param("nsId")
imageKey := c.Param("imageId")
imageKey = strings.ReplaceAll(imageKey, " ", "+")
imageKey = strings.ReplaceAll(imageKey, "%2B", "+")

content, err := resource.GetImage(nsId, imageKey)
return common.EndRequestWithLog(c, err, content)
}

// Response structure for RestGetAllImage
Expand Down
4 changes: 2 additions & 2 deletions src/api/rest/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,9 +423,9 @@ func RunServer() {
g.PUT("/:nsId/mci/:mciId/vm/:vmId/dataDisk", rest_resource.RestPutVmDataDisk)

g.POST("/:nsId/resources/image", rest_resource.RestPostImage)
g.GET("/:nsId/resources/image/:resourceId", rest_resource.RestGetResource)
g.GET("/:nsId/resources/image/:imageId", rest_resource.RestGetImage)
g.GET("/:nsId/resources/image", rest_resource.RestGetAllResources)
g.PUT("/:nsId/resources/image/:resourceId", rest_resource.RestPutImage)
g.PUT("/:nsId/resources/image/:imageId", rest_resource.RestPutImage)
g.DELETE("/:nsId/resources/image/:resourceId", rest_resource.RestDelResource)
g.DELETE("/:nsId/resources/image", rest_resource.RestDelAllResources)

Expand Down
8 changes: 4 additions & 4 deletions src/core/resource/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ func UpdateImage(nsId string, imageId string, fieldsToUpdate model.TbImageInfo,
return fieldsToUpdate, nil
}

// GetImage accepts namespace Id and imageKey(Id,CspResourceId,GuestOS,...), and returns the TB image object
// GetImage accepts namespace Id and imageKey(Id,CspResourceName,GuestOS,...), and returns the TB image object
func GetImage(nsId string, imageKey string) (model.TbImageInfo, error) {
if err := common.CheckString(nsId); err != nil {
log.Error().Err(err).Msg("Invalid namespace ID")
Expand All @@ -577,9 +577,9 @@ func GetImage(nsId string, imageKey string) (model.TbImageInfo, error) {

// ex: img-487zeit5
image = model.TbImageInfo{Namespace: nsId, CspImageName: imageKey}
has, err = model.ORM.Where("LOWER(Namespace) = ? AND LOWER(CspResourceId) = ?", nsId, imageKey).Get(&image)
has, err = model.ORM.Where("LOWER(Namespace) = ? AND LOWER(CspImageName) = ?", nsId, imageKey).Get(&image)
if err != nil {
log.Info().Err(err).Msgf("Failed to get image %s by CspResourceId", imageKey)
log.Info().Err(err).Msgf("Failed to get image %s by CspImageName", imageKey)
}
if has {
return image, nil
Expand All @@ -595,5 +595,5 @@ func GetImage(nsId string, imageKey string) (model.TbImageInfo, error) {
return image, nil
}

return model.TbImageInfo{}, fmt.Errorf("The imageKey %s not found by any of ID, CspResourceId, GuestOS", imageKey)
return model.TbImageInfo{}, fmt.Errorf("The imageKey %s not found by any of ID, CspImageName, GuestOS", imageKey)
}
8 changes: 4 additions & 4 deletions src/core/resource/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ type Range struct {
Max float32 `json:"max"`
}

// GetSpec accepts namespace Id and specKey(Id,CspResourceId,...), and returns the TB spec object
// GetSpec accepts namespace Id and specKey(Id,CspResourceName,...), and returns the TB spec object
func GetSpec(nsId string, specKey string) (model.TbSpecInfo, error) {
if err := common.CheckString(nsId); err != nil {
log.Error().Err(err).Msg("Invalid namespace ID")
Expand All @@ -395,15 +395,15 @@ func GetSpec(nsId string, specKey string) (model.TbSpecInfo, error) {

// ex: img-487zeit5
spec = model.TbSpecInfo{Namespace: nsId, CspSpecName: specKey}
has, err = model.ORM.Where("LOWER(Namespace) = ? AND LOWER(CspResourceId) = ?", nsId, specKey).Get(&spec)
has, err = model.ORM.Where("LOWER(Namespace) = ? AND LOWER(CspResourceName) = ?", nsId, specKey).Get(&spec)
if err != nil {
log.Info().Err(err).Msgf("Failed to get spec %s by CspResourceId", specKey)
log.Info().Err(err).Msgf("Failed to get spec %s by CspResourceName", specKey)
}
if has {
return spec, nil
}

return model.TbSpecInfo{}, fmt.Errorf("The specKey %s not found by any of ID, CspResourceId", specKey)
return model.TbSpecInfo{}, fmt.Errorf("The specKey %s not found by any of ID, CspResourceName", specKey)
}

// FilterSpecsByRange accepts criteria ranges for filtering, and returns the list of filtered TB spec objects
Expand Down

0 comments on commit 116ee55

Please sign in to comment.