Skip to content

Commit

Permalink
Merge pull request openinfradev#269 from openinfradev/stack_template_…
Browse files Browse the repository at this point in the history
…organization

feature. add api for stackTemplates. CUR
  • Loading branch information
ktkfree authored Mar 7, 2024
2 parents 8938cf1 + 07a6413 commit 3e1e5fb
Show file tree
Hide file tree
Showing 12 changed files with 2,159 additions and 388 deletions.
880 changes: 747 additions & 133 deletions api/swagger/docs.go

Large diffs are not rendered by default.

880 changes: 747 additions & 133 deletions api/swagger/swagger.json

Large diffs are not rendered by default.

454 changes: 424 additions & 30 deletions api/swagger/swagger.yaml

Large diffs are not rendered by default.

12 changes: 7 additions & 5 deletions internal/delivery/api/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,13 @@ const (
GetResourceQuota

// StackTemplate
GetStackTemplates
CreateStackTemplate
GetStackTemplate
UpdateStackTemplate
DeleteStackTemplate
Admin_GetStackTemplates
Admin_GetStackTemplate
Admin_GetStackTemplateServices
Admin_CreateStackTemplate
Admin_UpdateStackTemplate
Admin_DeleteStackTemplate
Admin_UpdateStackTemplateOrganizations

// Dashboard
GetChartsDashboard // 대시보드/대시보드/조회
Expand Down
76 changes: 46 additions & 30 deletions internal/delivery/api/generated_endpoints.go.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,24 +275,32 @@ var ApiMap = map[Endpoint]EndpointInfo{
Name: "GetResourceQuota",
Group: "CloudAccount",
},
GetStackTemplates: {
Name: "GetStackTemplates",
Admin_GetStackTemplates: {
Name: "Admin_GetStackTemplates",
Group: "StackTemplate",
},
CreateStackTemplate: {
Name: "CreateStackTemplate",
Admin_GetStackTemplate: {
Name: "Admin_GetStackTemplate",
Group: "StackTemplate",
},
GetStackTemplate: {
Name: "GetStackTemplate",
Admin_GetStackTemplateServices: {
Name: "Admin_GetStackTemplateServices",
Group: "StackTemplate",
},
UpdateStackTemplate: {
Name: "UpdateStackTemplate",
Admin_CreateStackTemplate: {
Name: "Admin_CreateStackTemplate",
Group: "StackTemplate",
},
DeleteStackTemplate: {
Name: "DeleteStackTemplate",
Admin_UpdateStackTemplate: {
Name: "Admin_UpdateStackTemplate",
Group: "StackTemplate",
},
Admin_DeleteStackTemplate: {
Name: "Admin_DeleteStackTemplate",
Group: "StackTemplate",
},
Admin_UpdateStackTemplateOrganizations: {
Name: "Admin_UpdateStackTemplateOrganizations",
Group: "StackTemplate",
},
GetChartsDashboard: {
Expand Down Expand Up @@ -686,16 +694,20 @@ func (e Endpoint) String() string {
return "DeleteForceCloudAccount"
case GetResourceQuota:
return "GetResourceQuota"
case GetStackTemplates:
return "GetStackTemplates"
case CreateStackTemplate:
return "CreateStackTemplate"
case GetStackTemplate:
return "GetStackTemplate"
case UpdateStackTemplate:
return "UpdateStackTemplate"
case DeleteStackTemplate:
return "DeleteStackTemplate"
case Admin_GetStackTemplates:
return "Admin_GetStackTemplates"
case Admin_GetStackTemplate:
return "Admin_GetStackTemplate"
case Admin_GetStackTemplateServices:
return "Admin_GetStackTemplateServices"
case Admin_CreateStackTemplate:
return "Admin_CreateStackTemplate"
case Admin_UpdateStackTemplate:
return "Admin_UpdateStackTemplate"
case Admin_DeleteStackTemplate:
return "Admin_DeleteStackTemplate"
case Admin_UpdateStackTemplateOrganizations:
return "Admin_UpdateStackTemplateOrganizations"
case GetChartsDashboard:
return "GetChartsDashboard"
case GetChartDashboard:
Expand Down Expand Up @@ -964,16 +976,20 @@ func GetEndpoint(name string) Endpoint {
return DeleteForceCloudAccount
case "GetResourceQuota":
return GetResourceQuota
case "GetStackTemplates":
return GetStackTemplates
case "CreateStackTemplate":
return CreateStackTemplate
case "GetStackTemplate":
return GetStackTemplate
case "UpdateStackTemplate":
return UpdateStackTemplate
case "DeleteStackTemplate":
return DeleteStackTemplate
case "Admin_GetStackTemplates":
return Admin_GetStackTemplates
case "Admin_GetStackTemplate":
return Admin_GetStackTemplate
case "Admin_GetStackTemplateServices":
return Admin_GetStackTemplateServices
case "Admin_CreateStackTemplate":
return Admin_CreateStackTemplate
case "Admin_UpdateStackTemplate":
return Admin_UpdateStackTemplate
case "Admin_DeleteStackTemplate":
return Admin_DeleteStackTemplate
case "Admin_UpdateStackTemplateOrganizations":
return Admin_UpdateStackTemplateOrganizations
case "GetChartsDashboard":
return GetChartsDashboard
case "GetChartDashboard":
Expand Down
103 changes: 77 additions & 26 deletions internal/delivery/http/stack-template.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (h *StackTemplateHandler) CreateStackTemplate(w http.ResponseWriter, r *htt
// @Param sortOrder query string false "sortOrder"
// @Param filters query []string false "filters"
// @Success 200 {object} domain.GetStackTemplatesResponse
// @Router /stack-templates [get]
// @Router /admin/stack-templates [get]
// @Security JWT
func (h *StackTemplateHandler) GetStackTemplates(w http.ResponseWriter, r *http.Request) {
urlParams := r.URL.Query()
Expand All @@ -94,6 +94,14 @@ func (h *StackTemplateHandler) GetStackTemplates(w http.ResponseWriter, r *http.
log.InfoWithContext(r.Context(), err)
}

out.StackTemplates[i].Organizations = make([]domain.SimpleOrganizationResponse, len(stackTemplate.Organizations))
for j, organization := range stackTemplate.Organizations {
if err := serializer.Map(organization, &out.StackTemplates[i].Organizations[j]); err != nil {
log.InfoWithContext(r.Context(), err)
continue
}
}

err := json.Unmarshal(stackTemplate.Services, &out.StackTemplates[i].Services)
if err != nil {
log.ErrorWithContext(r.Context(), err)
Expand Down Expand Up @@ -171,34 +179,31 @@ func (h *StackTemplateHandler) GetStackTemplate(w http.ResponseWriter, r *http.R
// @Router /admin/stack-templates/{stackTemplateId} [put]
// @Security JWT
func (h *StackTemplateHandler) UpdateStackTemplate(w http.ResponseWriter, r *http.Request) {
/*
vars := mux.Vars(r)
strId, ok := vars["stackTemplateId"]
if !ok {
ErrorJSON(w, r, httpErrors.NewBadRequestError(fmt.Errorf("Invalid stackTemplateId")))
return
}
stackTemplateId, err := uuid.Parse(strId)
if err != nil {
ErrorJSON(w, r, httpErrors.NewBadRequestError(errors.Wrap(err, "Failed to parse uuid %s")))
return
}
vars := mux.Vars(r)
strId, ok := vars["stackTemplateId"]
if !ok {
ErrorJSON(w, r, httpErrors.NewBadRequestError(fmt.Errorf("invalid stackTemplateId"), "C_INVALID_STACK_TEMPLATE_ID", ""))
return
}

var dto domain.StackTemplate
if err := serializer.Map(r, &dto); err != nil {
log.InfoWithContext(r.Context(),err)
}
dto.ID = stackTemplateId
stackTemplateId, err := uuid.Parse(strId)
if err != nil {
ErrorJSON(w, r, httpErrors.NewBadRequestError(errors.Wrap(err, "Failed to parse uuid %s"), "C_INVALID_STACK_TEMPLATE_ID", ""))
return
}

err = h.usecase.Update(r.Context(), dto)
if err != nil {
ErrorJSON(w, r, err)
return
}
*/
var dto domain.StackTemplate
if err := serializer.Map(r, &dto); err != nil {
log.InfoWithContext(r.Context(), err)
}
dto.ID = stackTemplateId

ErrorJSON(w, r, fmt.Errorf("need implementation"))
err = h.usecase.Update(r.Context(), dto)
if err != nil {
ErrorJSON(w, r, err)
return
}
ResponseJSON(w, r, http.StatusOK, nil)
}

// DeleteStackTemplate godoc
Expand Down Expand Up @@ -249,3 +254,49 @@ func (h *StackTemplateHandler) GetStackTemplateServices(w http.ResponseWriter, r

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

// UpdateStackTemplateOrganizations godoc
//
// @Tags StackTemplates
// @Summary Update StackTemplate organizations
// @Description Update StackTemplate organizations
// @Accept json
// @Produce json
// @Param body body domain.UpdateStackTemplateOrganizationsRequest true "Update stack template organizations request"
// @Success 200 {object} nil
// @Router /admin/stack-templates/{stackTemplateId}/organizations [put]
// @Security JWT
func (h *StackTemplateHandler) UpdateStackTemplateOrganizations(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
strId, ok := vars["stackTemplateId"]
if !ok {
ErrorJSON(w, r, httpErrors.NewBadRequestError(fmt.Errorf("invalid stackTemplateId"), "C_INVALID_STACK_TEMPLATE_ID", ""))
return
}

stackTemplateId, err := uuid.Parse(strId)
if err != nil {
ErrorJSON(w, r, httpErrors.NewBadRequestError(errors.Wrap(err, "Failed to parse uuid %s"), "C_INVALID_STACK_TEMPLATE_ID", ""))
return
}

input := domain.UpdateStackTemplateOrganizationsRequest{}
err = UnmarshalRequestInput(r, &input)
if err != nil {
ErrorJSON(w, r, err)
return
}

var dto domain.StackTemplate
if err := serializer.Map(input, &dto); err != nil {
log.InfoWithContext(r.Context(), err)
}
dto.ID = stackTemplateId

err = h.usecase.UpdateOrganizations(r.Context(), dto)
if err != nil {
ErrorJSON(w, r, err)
return
}
ResponseJSON(w, r, http.StatusOK, nil)
}
15 changes: 15 additions & 0 deletions internal/pagination/pagination.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,21 @@ func (p *Pagination) Response() (out domain.PaginationResponse, err error) {
return out, err
}

func NewPaginationWithFilter(column string, releation string, op string, values []string) *Pagination {
pg := newDefaultPagination()

pg.Filters = append(pg.Filters, Filter{
Column: helper.ToSnakeCase(strings.Replace(column, "[]", "", -1)),
Relation: releation,
Operator: op,
Values: values,
Or: false,
})
pg.MakePaginationRequest()

return pg
}

func NewPagination(urlParams *url.Values) *Pagination {
pg := newDefaultPagination()

Expand Down
23 changes: 18 additions & 5 deletions internal/repository/stack-template.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (r *StackTemplateRepository) Fetch(pg *pagination.Pagination) (out []domain
pg = pagination.NewPagination(nil)
}

_, res := pg.Fetch(r.db, &stackTemplates)
_, res := pg.Fetch(r.db.Preload(clause.Associations), &stackTemplates)
if res.Error != nil {
return nil, res.Error
}
Expand All @@ -94,10 +94,14 @@ func (r *StackTemplateRepository) Create(dto domain.StackTemplate) (stackTemplat
stackTemplate := StackTemplate{
Name: dto.Name,
Description: dto.Description,
CloudService: dto.CloudService,
Platform: dto.Platform,
Template: dto.Template,
TemplateType: dto.TemplateType,
Version: dto.Version,
CloudService: dto.CloudService,
Platform: dto.Platform,
KubeVersion: dto.KubeVersion,
KubeType: dto.KubeType,
Services: dto.Services,
CreatorId: &dto.CreatorId,
UpdatorId: &dto.CreatorId}
res := r.db.Create(&stackTemplate)
Expand All @@ -111,8 +115,16 @@ func (r *StackTemplateRepository) Update(dto domain.StackTemplate) (err error) {
res := r.db.Model(&StackTemplate{}).
Where("id = ?", dto.ID).
Updates(map[string]interface{}{
"Description": dto.Description,
"UpdatorId": dto.UpdatorId})
"Template": dto.Template,
"TemplateType": dto.TemplateType,
"Version": dto.Version,
"CloudService": dto.CloudService,
"Platform": dto.Platform,
"KubeVersion": dto.KubeVersion,
"KubeType": dto.KubeType,
"Services": dto.Services,
"Description": dto.Description,
"UpdatorId": dto.UpdatorId})
if res.Error != nil {
return res.Error
}
Expand Down Expand Up @@ -148,6 +160,7 @@ func reflectStackTemplate(stackTemplate StackTemplate) (out domain.StackTemplate
if err := serializer.Map(stackTemplate, &out); err != nil {
log.Error(err)
}

out.Services = stackTemplate.Services
return
}
18 changes: 7 additions & 11 deletions internal/route/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,17 +178,13 @@ func SetupRouter(db *gorm.DB, argoClient argowf.ArgoClient, kc keycloak.IKeycloa
r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/cloud-accounts/{cloudAccountId}/quotas", customMiddleware.Handle(internalApi.GetResourceQuota, http.HandlerFunc(cloudAccountHandler.GetResourceQuota))).Methods(http.MethodGet)

stackTemplateHandler := delivery.NewStackTemplateHandler(usecaseFactory)
/* REMOVE START */
r.Handle(API_PREFIX+API_VERSION+"/stack-templates", customMiddleware.Handle(internalApi.GetStackTemplates, http.HandlerFunc(stackTemplateHandler.GetStackTemplates))).Methods(http.MethodGet)
r.Handle(API_PREFIX+API_VERSION+"/stack-templates", customMiddleware.Handle(internalApi.CreateStackTemplate, http.HandlerFunc(stackTemplateHandler.CreateStackTemplate))).Methods(http.MethodPost)
r.Handle(API_PREFIX+API_VERSION+"/stack-templates/{stackTemplateId}", customMiddleware.Handle(internalApi.GetStackTemplate, http.HandlerFunc(stackTemplateHandler.GetStackTemplate))).Methods(http.MethodGet)
r.Handle(API_PREFIX+API_VERSION+"/stack-templates/{stackTemplateId}", customMiddleware.Handle(internalApi.UpdateStackTemplate, http.HandlerFunc(stackTemplateHandler.UpdateStackTemplate))).Methods(http.MethodPut)
r.Handle(API_PREFIX+API_VERSION+"/stack-templates/{stackTemplateId}", customMiddleware.Handle(internalApi.DeleteStackTemplate, http.HandlerFunc(stackTemplateHandler.DeleteStackTemplate))).Methods(http.MethodDelete)
/* REMOVE END */

r.Handle(API_PREFIX+API_VERSION+ADMINAPI_PREFIX+"/stack-templates", customMiddleware.Handle(internalApi.GetStackTemplates, http.HandlerFunc(stackTemplateHandler.GetStackTemplates))).Methods(http.MethodGet)
r.Handle(API_PREFIX+API_VERSION+ADMINAPI_PREFIX+"/stack-templates", customMiddleware.Handle(internalApi.CreateStackTemplate, http.HandlerFunc(stackTemplateHandler.CreateStackTemplate))).Methods(http.MethodPost)
r.Handle(API_PREFIX+API_VERSION+ADMINAPI_PREFIX+"/stack-templates/services", customMiddleware.Handle(internalApi.GetStackTemplates, http.HandlerFunc(stackTemplateHandler.GetStackTemplateServices))).Methods(http.MethodGet)
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/{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/services", customMiddleware.Handle(internalApi.Admin_GetStackTemplateServices, http.HandlerFunc(stackTemplateHandler.GetStackTemplateServices))).Methods(http.MethodGet)
r.Handle(API_PREFIX+API_VERSION+ADMINAPI_PREFIX+"/stack-templates/{stackTemplateId}/organizations", customMiddleware.Handle(internalApi.Admin_UpdateStackTemplateOrganizations, http.HandlerFunc(stackTemplateHandler.UpdateStackTemplateOrganizations))).Methods(http.MethodPut)
r.Handle(API_PREFIX+API_VERSION+ADMINAPI_PREFIX+"/stack-templates/{stackTemplateId}", customMiddleware.Handle(internalApi.Admin_UpdateStackTemplate, http.HandlerFunc(stackTemplateHandler.UpdateStackTemplate))).Methods(http.MethodPut)
r.Handle(API_PREFIX+API_VERSION+ADMINAPI_PREFIX+"/stack-templates/{stackTemplateId}", customMiddleware.Handle(internalApi.Admin_DeleteStackTemplate, http.HandlerFunc(stackTemplateHandler.DeleteStackTemplate))).Methods(http.MethodDelete)

dashboardHandler := delivery.NewDashboardHandler(usecaseFactory)
r.Handle(API_PREFIX+API_VERSION+"/organizations/{organizationId}/dashboard/charts", customMiddleware.Handle(internalApi.GetChartsDashboard, http.HandlerFunc(dashboardHandler.GetCharts))).Methods(http.MethodGet)
Expand Down
Loading

0 comments on commit 3e1e5fb

Please sign in to comment.