-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* init account service * rebase * add properties query # Conflicts: # service/go.work.sum * add GetCostAmount query api * format && add account service workflow * add swagger api docment for account service * rebase upstream
- Loading branch information
Showing
36 changed files
with
2,458 additions
and
213 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Copyright © 2023 sealos. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package common | ||
|
||
type Type int |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
FROM gcr.io/distroless/static:nonroot | ||
ARG TARGETARCH | ||
COPY bin/service-account-$TARGETARCH /manager | ||
EXPOSE 9090 | ||
USER 65532:65532 | ||
|
||
ENTRYPOINT ["/manager"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
IMG ?= ghcr.io/labring/sealos-account-service:latest | ||
|
||
# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set) | ||
ifeq (,$(shell go env GOBIN)) | ||
GOBIN=$(shell go env GOPATH)/bin | ||
else | ||
GOBIN=$(shell go env GOBIN) | ||
endif | ||
|
||
# only support linux, non cgo | ||
PLATFORMS ?= linux_arm64 linux_amd64 | ||
GOOS=linux | ||
CGO_ENABLED=0 | ||
GOARCH=$(shell go env GOARCH) | ||
|
||
GO_BUILD_FLAGS=-trimpath -ldflags "-s -w" | ||
|
||
.PHONY: all | ||
all: build | ||
|
||
##@ General | ||
|
||
# The help target prints out all targets with their descriptions organized | ||
# beneath their categories. The categories are represented by '##@' and the | ||
# target descriptions by '##'. The awk commands is responsible for reading the | ||
# entire set of makefiles included in this invocation, looking for lines of the | ||
# file as xyz: ## something, and then pretty-format the target and help. Then, | ||
# if there's a line with ##@ something, that gets pretty-printed as a category. | ||
# More info on the usage of ANSI control characters for terminal formatting: | ||
# https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters | ||
# More info on the awk command: | ||
# http://linuxcommand.org/lc3_adv_awk.php | ||
|
||
.PHONY: help | ||
help: ## Display this help. | ||
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST) | ||
|
||
##@ Build | ||
|
||
.PHONY: clean | ||
clean: | ||
rm -f $(SERVICE_NAME) | ||
|
||
.PHONY: build | ||
build: clean ## Build service-hub binary. | ||
CGO_ENABLED=$(CGO_ENABLED) GOOS=$(GOOS) go build $(GO_BUILD_FLAGS) -o bin/manager main.go | ||
|
||
.PHONY: docker-build | ||
docker-build: build | ||
mv bin/manager bin/service-database-${TARGETARCH} | ||
docker build -t $(IMG) . | ||
|
||
.PHONY: docker-push | ||
docker-push: | ||
docker push $(IMG) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package api | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/labring/sealos/service/account/common" | ||
|
||
"github.com/labring/sealos/service/account/dao" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/labring/sealos/service/account/helper" | ||
) | ||
|
||
var _ = helper.NamespaceBillingHistoryReq{} | ||
|
||
// @Summary Get namespace billing history list | ||
// @Description Get the billing history namespace list from the database | ||
// @Tags BillingHistory | ||
// @Accept json | ||
// @Produce json | ||
// @Param request body helper.NamespaceBillingHistoryReq true "Namespace billing history request" | ||
// @Success 200 {object} helper.NamespaceBillingHistoryRespData "successfully retrieved namespace billing history list" | ||
// @Failure 400 {object} helper.ErrorMessage "failed to parse namespace billing history request" | ||
// @Failure 401 {object} helper.ErrorMessage "authenticate error" | ||
// @Failure 500 {object} helper.ErrorMessage "failed to get namespace billing history list" | ||
// @Router /account/v1alpha1/namespaces [post] | ||
func GetBillingHistoryNamespaceList(c *gin.Context) { | ||
// Parse the namespace billing history request | ||
req, err := helper.ParseNamespaceBillingHistoryReq(c) | ||
if err != nil { | ||
c.JSON(http.StatusBadRequest, helper.ErrorMessage{Error: fmt.Sprintf("failed to parse namespace billing history request: %v", err)}) | ||
return | ||
} | ||
if err := helper.Authenticate(req.Auth); err != nil { | ||
c.JSON(http.StatusUnauthorized, helper.ErrorMessage{Error: fmt.Sprintf("authenticate error : %v", err)}) | ||
return | ||
} | ||
|
||
// Get the billing history namespace list from the database | ||
nsList, err := dao.DBClient.GetBillingHistoryNamespaceList(req) | ||
if err != nil { | ||
c.JSON(http.StatusInternalServerError, helper.ErrorMessage{Error: fmt.Sprintf("failed to get namespace billing history list: %v", err)}) | ||
return | ||
} | ||
c.JSON(http.StatusOK, helper.NamespaceBillingHistoryResp{ | ||
Data: helper.NamespaceBillingHistoryRespData{ | ||
List: nsList, | ||
}, | ||
Message: "successfully retrieved namespace billing history list", | ||
}) | ||
} | ||
|
||
// @Summary Get properties | ||
// @Description Get properties from the database | ||
// @Tags Properties | ||
// @Accept json | ||
// @Produce json | ||
// @Param request body helper.Auth true "auth request" | ||
// @Success 200 {object} helper.GetPropertiesResp "successfully retrieved properties" | ||
// @Failure 401 {object} helper.ErrorMessage "authenticate error" | ||
// @Failure 500 {object} helper.ErrorMessage "failed to get properties" | ||
// @Router /account/v1alpha1/properties [post] | ||
func GetProperties(c *gin.Context) { | ||
if err := helper.AuthenticateWithBind(c); err != nil { | ||
c.JSON(http.StatusUnauthorized, helper.ErrorMessage{Error: fmt.Sprintf("authenticate error : %v", err)}) | ||
return | ||
} | ||
// Get the properties from the database | ||
properties, err := dao.DBClient.GetProperties() | ||
if err != nil { | ||
c.JSON(http.StatusInternalServerError, fmt.Errorf(fmt.Sprintf("failed to get properties: %v", err))) | ||
return | ||
} | ||
c.JSON(http.StatusOK, helper.GetPropertiesResp{ | ||
Data: helper.GetPropertiesRespData{ | ||
Properties: properties, | ||
}, | ||
Message: "successfully retrieved properties", | ||
}) | ||
} | ||
|
||
type CostsResult struct { | ||
Data CostsResultData `json:"data" bson:"data"` | ||
Message string `json:"message" bson:"message"` | ||
} | ||
|
||
type CostsResultData struct { | ||
Costs common.TimeCostsMap `json:"costs" bson:"costs"` | ||
} | ||
|
||
// @Summary Get user costs | ||
// @Description Get user costs within a specified time range | ||
// @Tags Costs | ||
// @Accept json | ||
// @Produce json | ||
// @Param request body helper.UserCostsAmountReq true "User costs amount request" | ||
// @Success 200 {object} map[string]interface{} "successfully retrieved user costs" | ||
// @Failure 400 {object} map[string]interface{} "failed to parse user hour costs amount request" | ||
// @Failure 401 {object} map[string]interface{} "authenticate error" | ||
// @Failure 500 {object} map[string]interface{} "failed to get user costs" | ||
// @Router /account/v1alpha1/costs [post] | ||
func GetCosts(c *gin.Context) { | ||
req, err := helper.ParseUserCostsAmountReq(c) | ||
if err != nil { | ||
c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("failed to parse user hour costs amount request: %v", err)}) | ||
return | ||
} | ||
if err := helper.Authenticate(req.Auth); err != nil { | ||
c.JSON(http.StatusUnauthorized, gin.H{"error": fmt.Sprintf("authenticate error : %v", err)}) | ||
return | ||
} | ||
costs, err := dao.DBClient.GetCostAmount(req.Auth.Owner, req.TimeRange.StartTime, req.TimeRange.EndTime) | ||
if err != nil { | ||
c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("failed to get cost : %v", err)}) | ||
} | ||
c.JSON(http.StatusOK, CostsResult{ | ||
Data: CostsResultData{Costs: costs}, | ||
Message: "successfully retrieved user costs", | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package common | ||
|
||
type PropertyQuery struct { | ||
Name string `json:"name,omitempty" bson:"name,omitempty" example:"cpu"` | ||
Alias string `json:"alias,omitempty" bson:"alias,omitempty" example:"gpu-tesla-v100"` | ||
UnitPrice float64 `json:"unit_price,omitempty" bson:"unit_price,omitempty" example:"10000"` | ||
Unit string `json:"unit,omitempty" bson:"unit,omitempty" example:"1m"` | ||
} | ||
|
||
type TimeCostsMap [][]interface{} |
Oops, something went wrong.