Skip to content

Commit

Permalink
Support query param for desired provider and region
Browse files Browse the repository at this point in the history
  • Loading branch information
cb-github-robot authored Oct 8, 2024
2 parents b999f09 + 5fd0e20 commit 63c62cf
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 12 deletions.
24 changes: 21 additions & 3 deletions api/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ const docTemplate = `{
},
"/recommendation/mci": {
"post": {
"description": "Recommend an appropriate multi-cloud infrastructure (MCI) for cloud migration",
"description": "Recommend an appropriate multi-cloud infrastructure (MCI) for cloud migration\n\n[Note] ` + "`" + `desiredProvider` + "`" + ` and ` + "`" + `desiredRegion` + "`" + ` are required.\n- ` + "`" + `desiredProvider` + "`" + ` and ` + "`" + `desiredRegion` + "`" + ` can set on the query parameter or the request body.\n\n- If desiredProvider and desiredRegion are set on request body, the values in the query parameter will be ignored.",
"consumes": [
"application/json"
],
Expand All @@ -318,6 +318,26 @@ const docTemplate = `{
"$ref": "#/definitions/controller.RecommendInfraRequest"
}
},
{
"enum": [
"aws",
"azure",
"gcp",
"ncp"
],
"type": "string",
"default": "aws",
"description": "Provider (e.g., aws, azure, gcp)",
"name": "desiredProvider",
"in": "query"
},
{
"type": "string",
"default": "ap-northeast-2",
"description": "Region (e.g., ap-northeast-2)",
"name": "desiredRegion",
"in": "query"
},
{
"type": "string",
"description": "Custom request ID (NOTE: It will be used as a trace ID.)",
Expand Down Expand Up @@ -852,8 +872,6 @@ const docTemplate = `{
"controller.RecommendInfraRequest": {
"type": "object",
"required": [
"desiredProvider",
"desiredRegion",
"onpremiseInfraModel"
],
"properties": {
Expand Down
24 changes: 21 additions & 3 deletions api/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@
},
"/recommendation/mci": {
"post": {
"description": "Recommend an appropriate multi-cloud infrastructure (MCI) for cloud migration",
"description": "Recommend an appropriate multi-cloud infrastructure (MCI) for cloud migration\n\n[Note] `desiredProvider` and `desiredRegion` are required.\n- `desiredProvider` and `desiredRegion` can set on the query parameter or the request body.\n\n- If desiredProvider and desiredRegion are set on request body, the values in the query parameter will be ignored.",
"consumes": [
"application/json"
],
Expand All @@ -312,6 +312,26 @@
"$ref": "#/definitions/controller.RecommendInfraRequest"
}
},
{
"enum": [
"aws",
"azure",
"gcp",
"ncp"
],
"type": "string",
"default": "aws",
"description": "Provider (e.g., aws, azure, gcp)",
"name": "desiredProvider",
"in": "query"
},
{
"type": "string",
"default": "ap-northeast-2",
"description": "Region (e.g., ap-northeast-2)",
"name": "desiredRegion",
"in": "query"
},
{
"type": "string",
"description": "Custom request ID (NOTE: It will be used as a trace ID.)",
Expand Down Expand Up @@ -846,8 +866,6 @@
"controller.RecommendInfraRequest": {
"type": "object",
"required": [
"desiredProvider",
"desiredRegion",
"onpremiseInfraModel"
],
"properties": {
Expand Down
26 changes: 22 additions & 4 deletions api/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,6 @@ definitions:
onpremiseInfraModel:
$ref: '#/definitions/onprem.OnPremInfra'
required:
- desiredProvider
- desiredRegion
- onpremiseInfraModel
type: object
controller.RecommendInfraResponse:
Expand Down Expand Up @@ -696,8 +694,13 @@ paths:
post:
consumes:
- application/json
description: Recommend an appropriate multi-cloud infrastructure (MCI) for cloud
migration
description: |-
Recommend an appropriate multi-cloud infrastructure (MCI) for cloud migration
[Note] `desiredProvider` and `desiredRegion` are required.
- `desiredProvider` and `desiredRegion` can set on the query parameter or the request body.
- If desiredProvider and desiredRegion are set on request body, the values in the query parameter will be ignored.
operationId: RecommendInfra
parameters:
- description: Specify the your infrastructure to be migrated
Expand All @@ -706,6 +709,21 @@ paths:
required: true
schema:
$ref: '#/definitions/controller.RecommendInfraRequest'
- default: aws
description: Provider (e.g., aws, azure, gcp)
enum:
- aws
- azure
- gcp
- ncp
in: query
name: desiredProvider
type: string
- default: ap-northeast-2
description: Region (e.g., ap-northeast-2)
in: query
name: desiredRegion
type: string
- description: 'Custom request ID (NOTE: It will be used as a trace ID.)'
in: header
name: X-Request-Id
Expand Down
32 changes: 30 additions & 2 deletions pkg/api/rest/controller/recommendation.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ limitations under the License.
package controller

import (
"fmt"
"net/http"

// cloudmodel "github.com/cloud-barista/cm-beetle/pkg/api/rest/model/cloud/infra"
Expand All @@ -39,8 +40,8 @@ import (
// }

type RecommendInfraRequest struct {
DesiredProvider string `json:"desiredProvider" validate:"required" example:"aws"`
DesiredRegion string `json:"desiredRegion" validate:"required" example:"ap-northeast-2"`
DesiredProvider string `json:"desiredProvider" example:"aws"`
DesiredRegion string `json:"desiredRegion" example:"ap-northeast-2"`
OnpremiseInfraModel onprem.OnPremInfra `json:"onpremiseInfraModel" validate:"required"`
}

Expand All @@ -52,17 +53,27 @@ type RecommendInfraResponse struct {
// @ID RecommendInfra
// @Summary Recommend an appropriate multi-cloud infrastructure (MCI) for cloud migration
// @Description Recommend an appropriate multi-cloud infrastructure (MCI) for cloud migration
// @Description
// @Description [Note] `desiredProvider` and `desiredRegion` are required.
// @Description - `desiredProvider` and `desiredRegion` can set on the query parameter or the request body.
// @Description
// @Description - If desiredProvider and desiredRegion are set on request body, the values in the query parameter will be ignored.
// @Tags [Recommendation] Infrastructure
// @Accept json
// @Produce json
// @Param UserInfra body RecommendInfraRequest true "Specify the your infrastructure to be migrated"
// @Param desiredProvider query string false "Provider (e.g., aws, azure, gcp)" Enums(aws,azure,gcp,ncp) default(aws)
// @Param desiredRegion query string false "Region (e.g., ap-northeast-2)" default(ap-northeast-2)
// @Param X-Request-Id header string false "Custom request ID (NOTE: It will be used as a trace ID.)"
// @Success 200 {object} RecommendInfraResponse "The result of recommended infrastructure"
// @Failure 404 {object} common.SimpleMsg
// @Failure 500 {object} common.SimpleMsg
// @Router /recommendation/mci [post]
func RecommendInfra(c echo.Context) error {

desiredProvider := c.QueryParam("desiredProvider")
desiredRegion := c.QueryParam("desiredRegion")

// [Input]
reqt := &RecommendInfraRequest{}
if err := c.Bind(reqt); err != nil {
Expand All @@ -73,8 +84,25 @@ func RecommendInfra(c echo.Context) error {

log.Trace().Msgf("reqt: %v\n", reqt)

if reqt.DesiredProvider == "" && desiredProvider == "" {
err := fmt.Errorf("invalid request: 'desiredProvider' is required")
resp := common.SimpleMsg{Message: err.Error()}
return c.JSON(http.StatusBadRequest, resp)
}
if reqt.DesiredRegion == "" && desiredRegion == "" {
err := fmt.Errorf("invalid request: 'desiredRegion' is required")
resp := common.SimpleMsg{Message: err.Error()}
return c.JSON(http.StatusBadRequest, resp)
}

provider := reqt.DesiredProvider
if provider == "" {
provider = desiredProvider
}
region := reqt.DesiredRegion
if region == "" {
region = desiredRegion
}
sourceInfra := reqt.OnpremiseInfraModel

ok, err := recommendation.IsValidProviderAndRegion(provider, region)
Expand Down

0 comments on commit 63c62cf

Please sign in to comment.