Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature. add api to stack-templates for checking name #280

Merged
merged 1 commit into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions api/swagger/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -1018,6 +1018,43 @@ const docTemplate = `{
}
}
},
"/admin/stack-templates/name/{name}/existence": {
"get": {
"security": [
{
"JWT": []
}
],
"description": "Check name for stackTemplate",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"StackTemplates"
],
"summary": "Check name for stackTemplate",
"parameters": [
{
"type": "string",
"description": "name",
"name": "name",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/github_com_openinfradev_tks-api_pkg_domain.CheckStackTemplateNameResponse"
}
}
}
}
},
"/admin/stack-templates/services": {
"get": {
"security": [
Expand Down Expand Up @@ -7412,6 +7449,14 @@ const docTemplate = `{
}
}
},
"github_com_openinfradev_tks-api_pkg_domain.CheckStackTemplateNameResponse": {
"type": "object",
"properties": {
"existed": {
"type": "boolean"
}
}
},
"github_com_openinfradev_tks-api_pkg_domain.CloudAccountResponse": {
"type": "object",
"properties": {
Expand Down
45 changes: 45 additions & 0 deletions api/swagger/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -1012,6 +1012,43 @@
}
}
},
"/admin/stack-templates/name/{name}/existence": {
"get": {
"security": [
{
"JWT": []
}
],
"description": "Check name for stackTemplate",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"StackTemplates"
],
"summary": "Check name for stackTemplate",
"parameters": [
{
"type": "string",
"description": "name",
"name": "name",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/github_com_openinfradev_tks-api_pkg_domain.CheckStackTemplateNameResponse"
}
}
}
}
},
"/admin/stack-templates/services": {
"get": {
"security": [
Expand Down Expand Up @@ -7406,6 +7443,14 @@
}
}
},
"github_com_openinfradev_tks-api_pkg_domain.CheckStackTemplateNameResponse": {
"type": "object",
"properties": {
"existed": {
"type": "boolean"
}
}
},
"github_com_openinfradev_tks-api_pkg_domain.CloudAccountResponse": {
"type": "object",
"properties": {
Expand Down
28 changes: 28 additions & 0 deletions api/swagger/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,11 @@ definitions:
existed:
type: boolean
type: object
github_com_openinfradev_tks-api_pkg_domain.CheckStackTemplateNameResponse:
properties:
existed:
type: boolean
type: object
github_com_openinfradev_tks-api_pkg_domain.CloudAccountResponse:
properties:
awsAccountId:
Expand Down Expand Up @@ -3644,6 +3649,29 @@ paths:
summary: Update StackTemplate organizations
tags:
- StackTemplates
/admin/stack-templates/name/{name}/existence:
get:
consumes:
- application/json
description: Check name for stackTemplate
parameters:
- description: name
in: path
name: name
required: true
type: string
produces:
- application/json
responses:
"200":
description: OK
schema:
$ref: '#/definitions/github_com_openinfradev_tks-api_pkg_domain.CheckStackTemplateNameResponse'
security:
- JWT: []
summary: Check name for stackTemplate
tags:
- StackTemplates
/admin/stack-templates/services:
get:
consumes:
Expand Down
1 change: 1 addition & 0 deletions internal/delivery/api/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ const (
Admin_UpdateStackTemplate
Admin_DeleteStackTemplate
Admin_UpdateStackTemplateOrganizations
Admin_CheckStackTemplateName
GetOrganizationStackTemplates
GetOrganizationStackTemplate

Expand Down
8 changes: 8 additions & 0 deletions internal/delivery/api/generated_endpoints.go.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,10 @@ var ApiMap = map[Endpoint]EndpointInfo{
Name: "Admin_UpdateStackTemplateOrganizations",
Group: "StackTemplate",
},
Admin_CheckStackTemplateName: {
Name: "Admin_CheckStackTemplateName",
Group: "StackTemplate",
},
GetOrganizationStackTemplates: {
Name: "GetOrganizationStackTemplates",
Group: "StackTemplate",
Expand Down Expand Up @@ -888,6 +892,8 @@ func (e Endpoint) String() string {
return "Admin_DeleteStackTemplate"
case Admin_UpdateStackTemplateOrganizations:
return "Admin_UpdateStackTemplateOrganizations"
case Admin_CheckStackTemplateName:
return "Admin_CheckStackTemplateName"
case GetOrganizationStackTemplates:
return "GetOrganizationStackTemplates"
case GetOrganizationStackTemplate:
Expand Down Expand Up @@ -1260,6 +1266,8 @@ func GetEndpoint(name string) Endpoint {
return Admin_DeleteStackTemplate
case "Admin_UpdateStackTemplateOrganizations":
return Admin_UpdateStackTemplateOrganizations
case "Admin_CheckStackTemplateName":
return Admin_CheckStackTemplateName
case "GetOrganizationStackTemplates":
return GetOrganizationStackTemplates
case "GetOrganizationStackTemplate":
Expand Down
36 changes: 36 additions & 0 deletions internal/delivery/http/stack-template.go
Original file line number Diff line number Diff line change
Expand Up @@ -415,3 +415,39 @@ func (h *StackTemplateHandler) GetOrganizationStackTemplate(w http.ResponseWrite

ResponseJSON(w, r, http.StatusOK, out)
}

// CheckStackTemplateName godoc
//
// @Tags StackTemplates
// @Summary Check name for stackTemplate
// @Description Check name for stackTemplate
// @Accept json
// @Produce json
// @Param name path string true "name"
// @Success 200 {object} domain.CheckStackTemplateNameResponse
// @Router /admin/stack-templates/name/{name}/existence [GET]
// @Security JWT
func (h *StackTemplateHandler) CheckStackTemplateName(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
name, ok := vars["name"]
if !ok {
ErrorJSON(w, r, httpErrors.NewBadRequestError(fmt.Errorf("Invalid name"), "ST_INVALID_STACK_TEMAPLTE_NAME", ""))
return
}

exist := true
_, err := h.usecase.GetByName(r.Context(), name)
if err != nil {
if _, code := httpErrors.ErrorResponse(err); code == http.StatusNotFound {
exist = false
} else {
ErrorJSON(w, r, err)
return
}
}

var out domain.CheckStackTemplateNameResponse
out.Existed = exist

ResponseJSON(w, r, http.StatusOK, out)
}
9 changes: 9 additions & 0 deletions internal/repository/stack-template.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
// Interfaces
type IStackTemplateRepository interface {
Get(stackTemplateId uuid.UUID) (model.StackTemplate, error)
GetByName(name string) (model.StackTemplate, error)
Fetch(pg *pagination.Pagination) ([]model.StackTemplate, error)
FetchWithOrganization(organizationId string, pg *pagination.Pagination) (out []model.StackTemplate, err error)
Create(dto model.StackTemplate) (stackTemplateId uuid.UUID, err error)
Expand Down Expand Up @@ -39,6 +40,14 @@ func (r *StackTemplateRepository) Get(stackTemplateId uuid.UUID) (out model.Stac
return
}

func (r *StackTemplateRepository) GetByName(name string) (out model.StackTemplate, err error) {
res := r.db.First(&out, "name = ?", name)
if res.Error != nil {
return out, res.Error
}
return
}

func (r *StackTemplateRepository) Fetch(pg *pagination.Pagination) (out []model.StackTemplate, err error) {
if pg == nil {
pg = pagination.NewPagination(nil)
Expand Down
1 change: 1 addition & 0 deletions internal/route/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ func SetupRouter(db *gorm.DB, argoClient argowf.ArgoClient, kc keycloak.IKeycloa
stackTemplateHandler := delivery.NewStackTemplateHandler(usecaseFactory)
r.Handle(API_PREFIX+API_VERSION+ADMINAPI_PREFIX+"/stack-templates", customMiddleware.Handle(internalApi.Admin_GetStackTemplates, http.HandlerFunc(stackTemplateHandler.GetStackTemplates))).Methods(http.MethodGet)
r.Handle(API_PREFIX+API_VERSION+ADMINAPI_PREFIX+"/stack-templates/services", customMiddleware.Handle(internalApi.Admin_GetStackTemplateServices, http.HandlerFunc(stackTemplateHandler.GetStackTemplateServices))).Methods(http.MethodGet)
r.Handle(API_PREFIX+API_VERSION+ADMINAPI_PREFIX+"/stack-templates/name/{name}/existence", customMiddleware.Handle(internalApi.Admin_CheckStackTemplateName, http.HandlerFunc(stackTemplateHandler.CheckStackTemplateName))).Methods(http.MethodGet)
r.Handle(API_PREFIX+API_VERSION+ADMINAPI_PREFIX+"/stack-templates/{stackTemplateId}", customMiddleware.Handle(internalApi.Admin_GetStackTemplates, http.HandlerFunc(stackTemplateHandler.GetStackTemplate))).Methods(http.MethodGet)
r.Handle(API_PREFIX+API_VERSION+ADMINAPI_PREFIX+"/stack-templates", customMiddleware.Handle(internalApi.Admin_CreateStackTemplate, http.HandlerFunc(stackTemplateHandler.CreateStackTemplate))).Methods(http.MethodPost)
r.Handle(API_PREFIX+API_VERSION+ADMINAPI_PREFIX+"/stack-templates/{stackTemplateId}/organizations", customMiddleware.Handle(internalApi.Admin_UpdateStackTemplateOrganizations, http.HandlerFunc(stackTemplateHandler.UpdateStackTemplateOrganizations))).Methods(http.MethodPut)
Expand Down
17 changes: 16 additions & 1 deletion internal/usecase/stack-template.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
"github.com/openinfradev/tks-api/internal/repository"
"github.com/openinfradev/tks-api/pkg/httpErrors"
"github.com/openinfradev/tks-api/pkg/log"
"github.com/pkg/errors"
"gorm.io/gorm"
)

type IStackTemplateUsecase interface {
Expand All @@ -22,6 +24,7 @@ type IStackTemplateUsecase interface {
Update(ctx context.Context, dto model.StackTemplate) error
Delete(ctx context.Context, dto model.StackTemplate) error
UpdateOrganizations(ctx context.Context, dto model.StackTemplate) error
GetByName(ctx context.Context, name string) (model.StackTemplate, error)
}

type StackTemplateUsecase struct {
Expand Down Expand Up @@ -94,11 +97,23 @@ func (u *StackTemplateUsecase) Update(ctx context.Context, dto model.StackTempla
func (u *StackTemplateUsecase) Get(ctx context.Context, stackTemplateId uuid.UUID) (res model.StackTemplate, err error) {
res, err = u.repo.Get(stackTemplateId)
if err != nil {
return model.StackTemplate{}, err
return res, err
}
return
}

func (u *StackTemplateUsecase) GetByName(ctx context.Context, name string) (out model.StackTemplate, err error) {
out, err = u.repo.GetByName(name)
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return out, httpErrors.NewNotFoundError(err, "ST_FAILED_FETCH_STACK_TEMPLATE", "")
}
return out, err
}

return
}

func (u *StackTemplateUsecase) Fetch(ctx context.Context, pg *pagination.Pagination) (res []model.StackTemplate, err error) {
res, err = u.repo.Fetch(pg)
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions pkg/domain/stack-template.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,7 @@ type GetStackTemplateServicesResponse struct {
type UpdateStackTemplateOrganizationsRequest struct {
OrganizationIds []string `json:"organizationIds" validate:"required"`
}

type CheckStackTemplateNameResponse struct {
Existed bool `json:"existed"`
}
2 changes: 2 additions & 0 deletions pkg/httpErrors/errorCode.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ var errorMap = map[ErrorCode]string{
"ST_CREATE_ALREADY_EXISTED_NAME": "스택 템플릿에 이미 존재하는 이름입니다.",
"ST_FAILED_UPDATE_ORGANIZATION": "스택 템플릿에 조직을 설정하는데 실패했습니다.",
"ST_NOT_EXISTED_STACK_TEMPLATE": "업데이트할 스택템플릿이 존재하지 않습니다.",
"ST_INVALID_STACK_TEMAPLTE_NAME": "유효하지 않은 스택템플릿 이름입니다. 스택템플릿 이름을 확인하세요.",
"ST_FAILED_FETCH_STACK_TEMPLATE": "스택템플릿을 가져오는데 실패했습니다.",

// PolicyTemplate
"PT_CREATE_ALREADY_EXISTED_NAME": "정첵 템플릿에 이미 존재하는 이름입니다.",
Expand Down
Loading