Skip to content

Commit

Permalink
Added GetByName api in agent and get_by_id action api in kube_object …
Browse files Browse the repository at this point in the history
…router
  • Loading branch information
niloydeb1 committed Jul 26, 2022
1 parent d0c6ed4 commit a83c029
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 3 deletions.
25 changes: 24 additions & 1 deletion api/v1/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type agentApi struct {
// @Tags Agent
// @Produce json
// @Success 200 {object} common.ResponseDTO
// @Router /api/v1/agents [GET]
// @Router /api/v1/agents/{agent} [GET]
func (a agentApi) Get(context echo.Context) error {
var companyId string
if config.EnableAuthentication {
Expand All @@ -43,6 +43,29 @@ func (a agentApi) Get(context echo.Context) error {
return common.GenerateErrorResponse(context, "Agents Query Failed", "Operation Failed")
}

func (a agentApi) GetByName(context echo.Context) error {
var companyId string
if config.EnableAuthentication {
userResourcePermission, err := GetUserResourcePermissionFromBearerToken(context, a.jwtService)
if err != nil {
return common.GenerateUnauthorizedResponse(context, err, err.Error())
}
if err := checkAuthority(userResourcePermission, string(enums.PROCESS), "", string(enums.READ)); err != nil {
return common.GenerateUnauthorizedResponse(context, err, err.Error())
}
companyId = userResourcePermission.Metadata.CompanyId
}
if companyId == "" {
return common.GenerateErrorResponse(context, "[ERROR]: Company Id is not found.", "Operation failed.")
}
agent := context.Param("agent")
code, data := a.agentService.GetByName(agent, companyId)
if code == 200 {
return context.JSON(200, data)
}
return common.GenerateErrorResponse(context, "Agents Query Failed", "Operation Failed")
}

// Get... Get K8sObjs Api
// @Summary Get K8sObjs api
// @Description Api for getting all K8sObjs short info by agent name and process id
Expand Down
1 change: 1 addition & 0 deletions api/v1/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ func KubeObjectRouter(g *echo.Group) {
func AgentRouter(g *echo.Group) {
agentApi := NewAgentApi(dependency.GetV1Agent(), dependency.GetV1JwtService())
g.GET("", agentApi.Get)
g.GET("/:agent", agentApi.GetByName)
g.GET("/:agent/k8sobjs", agentApi.GetK8sObjs)
g.GET("/:agent/daemonSets/:daemonSetId/pods", agentApi.GetPodsByDaemonSet)
g.GET("/:agent/deployments/:deploymentId/pods", agentApi.GetPodsByDeployment)
Expand Down
25 changes: 23 additions & 2 deletions api/v1/kube_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ type kubeObject struct {
// @Tags KubeObject
// @Produce json
// @Param owner-reference query string false "Owner Reference"
// @Param action query string true "action [dashboard_data]"
// @Param action query string true "action [dashboard_data/get_by_id]"
// @Param object query string true "object [certificate/cluster-role/cluster-role-binding/config-map/daemon-set/deployment/ingress/namespace/network-policy/node/pod/persistent-volume/persistent-volume-claim/replica-set/role/role-binding/secret/service/service-account/stateful-set]"
// @Param id query string false "Kube Object ID to get by id"
// @Param processId query string true "Process Id"
// @Param agent query string true "Agent Name"
// @Param page query int64 false "Page Number"
Expand Down Expand Up @@ -50,10 +51,30 @@ func (k kubeObject) Get(context echo.Context) error {
}
object := context.QueryParam("object")
object = reformatObjectName(object)
if object == "" {
return common.GenerateErrorResponse(context, "[ERROR]: K8S Object name is not given", "Operation Failed")
}
agent := context.QueryParam("agent")
ownerReference := context.QueryParam("owner-reference")
if agent == "" {
return common.GenerateErrorResponse(context, "[ERROR]: Agent name is not given", "Operation Failed")
}
processId := context.QueryParam("processId")
if processId == "" {
return common.GenerateErrorResponse(context, "[ERROR]: Process ID is not given", "Operation Failed")
}
ownerReference := context.QueryParam("owner-reference")
option := getK8sObjectQueryOption(context)
if action == "get_by_id" {
id := context.QueryParam("id")
if id == "" {
return common.GenerateErrorResponse(context, "[ERROR]: K8S Object ID is not given", "Operation Failed")
}
code, data := k.kubeObjectService.GetByID(object, id, agent, processId)
if code == 200 {
return context.JSON(code, data)
}
return common.GenerateErrorResponse(context, "[ERROR]: k8s Object Query Failed", "Operation Failed")
}
code, data := k.kubeObjectService.Get(action, companyId, object, agent, ownerReference, processId, option)
if code == 200 {
return context.JSON(code, data)
Expand Down
1 change: 1 addition & 0 deletions core/v1/api/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import "github.com/labstack/echo/v4"
// Agent operations
type Agent interface {
Get(context echo.Context) error
GetByName(context echo.Context) error
GetK8sObjs(context echo.Context) error
GetPodsByDaemonSet(context echo.Context) error
GetPodsByDeployment(context echo.Context) error
Expand Down
15 changes: 15 additions & 0 deletions core/v1/logic/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,21 @@ func (a agentService) Get(companyId string) (httpCode int, body interface{}) {
return code, response
}

func (a agentService) GetByName(agent, companyId string) (httpCode int, body interface{}) {
var response interface{}
header := make(map[string]string)
header["token"] = config.Token
code, b, err := a.httpClient.Get(config.LighthouseQueryServerUrl+"/agents/"+agent+"?companyId="+companyId, header)
if err != nil {
return code, err
}
err = json.Unmarshal(b, &response)
if err != nil {
return http.StatusBadRequest, err
}
return code, response
}

func (a agentService) GetK8sObjs(agent, processId string) (httpCode int, body interface{}) {
var response interface{}
header := make(map[string]string)
Expand Down
15 changes: 15 additions & 0 deletions core/v1/logic/kube_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,21 @@ func (k kubeObjectService) Get(action, companyId, object, agent, ownerReference,
return code, response
}

func (k kubeObjectService) GetByID(object, id, agent, processId string) (httpCode int, body interface{}) {
var response interface{}
header := make(map[string]string)
header["token"] = config.Token
code, b, err := k.httpPublisher.Get(config.LighthouseQueryServerUrl+"/"+object+"/"+id+"?agent="+agent+"&processId="+processId, header)
if err != nil {
return code, err
}
err = json.Unmarshal(b, &response)
if err != nil {
return http.StatusBadRequest, err
}
return code, response
}

// NewKubeObjectService returns KubeObject type service
func NewKubeObjectService(publisher service.HttpClient) service.KubeObject {
return kubeObjectService{
Expand Down
1 change: 1 addition & 0 deletions core/v1/service/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package service
// Agent operations
type Agent interface {
Get(companyId string) (httpCode int, body interface{})
GetByName(agent, companyId string) (httpCode int, body interface{})
GetK8sObjs(agent, processId string) (httpCode int, body interface{})
GetPodsByDaemonSet(agent, processId, daemonSetId string) (httpCode int, body interface{})
GetPodsByDeployment(agent, processId, deploymentId string) (httpCode int, body interface{})
Expand Down
1 change: 1 addition & 0 deletions core/v1/service/kube_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ import v1 "github.com/klovercloud-ci-cd/api-service/core/v1"
// KubeObject k8s Object operations.
type KubeObject interface {
Get(action, companyId, object, agent, ownerReference, processId string, option v1.ResourceQueryOption) (httpCode int, body interface{})
GetByID(object, id, agent, processId string) (httpCode int, body interface{})
}

0 comments on commit a83c029

Please sign in to comment.