Skip to content

Commit 642fc86

Browse files
committed
fix: get data Source API
1 parent 583057a commit 642fc86

File tree

6 files changed

+60
-7
lines changed

6 files changed

+60
-7
lines changed

api/cluster/EnvironmentRestHandler.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ const ENV_DELETE_SUCCESS_RESP = "Environment deleted successfully."
5151
type EnvironmentRestHandler interface {
5252
Create(w http.ResponseWriter, r *http.Request)
5353
Get(w http.ResponseWriter, r *http.Request)
54+
GetDataSourceName(w http.ResponseWriter, r *http.Request)
5455
GetAll(w http.ResponseWriter, r *http.Request)
5556
GetAllActive(w http.ResponseWriter, r *http.Request)
5657
Update(w http.ResponseWriter, r *http.Request)
@@ -167,6 +168,19 @@ func (impl EnvironmentRestHandlerImpl) Get(w http.ResponseWriter, r *http.Reques
167168
common.WriteJsonResp(w, err, bean, http.StatusOK)
168169
}
169170

171+
func (impl EnvironmentRestHandlerImpl) GetDataSourceName(w http.ResponseWriter, r *http.Request) {
172+
vars := mux.Vars(r)
173+
environment := vars["environment"]
174+
175+
name, err := impl.environmentClusterMappingsService.GetDataSourceName(environment)
176+
if err != nil {
177+
impl.logger.Errorw("service err, Get", "err", err, "env", environment)
178+
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
179+
return
180+
}
181+
common.WriteJsonResp(w, err, name, http.StatusOK)
182+
}
183+
170184
func (impl EnvironmentRestHandlerImpl) GetAll(w http.ResponseWriter, r *http.Request) {
171185
environments, err := impl.environmentReadService.GetAll()
172186
if err != nil {

api/cluster/EnvironmentRouter.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ func (impl EnvironmentRouterImpl) InitEnvironmentClusterMappingsRouter(environme
4141
Methods("GET").
4242
Queries("environment", "{environment}").
4343
HandlerFunc(impl.environmentClusterMappingsRestHandler.Get)
44+
environmentClusterMappingsRouter.Path("/data-source-name").
45+
Methods("GET").
46+
Queries("environment", "{environment}").
47+
HandlerFunc(impl.environmentClusterMappingsRestHandler.GetDataSourceName)
4448
environmentClusterMappingsRouter.Path("").
4549
Methods("GET").
4650
Queries("id", "{id}").

cmd/external-app/wire.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ import (
5959
"github.com/devtron-labs/devtron/client/argocdServer/repoCredsK8sClient"
6060
"github.com/devtron-labs/devtron/client/argocdServer/session"
6161
"github.com/devtron-labs/devtron/client/dashboard"
62+
"github.com/devtron-labs/devtron/client/grafana"
6263
"github.com/devtron-labs/devtron/client/telemetry"
6364
"github.com/devtron-labs/devtron/internal/sql/repository"
6465
app2 "github.com/devtron-labs/devtron/internal/sql/repository/app"
@@ -145,6 +146,10 @@ func InitializeApp() (*App, error) {
145146
rbac.NewEnforcerUtilImpl,
146147
wire.Bind(new(rbac.EnforcerUtil), new(*rbac.EnforcerUtilImpl)),
147148

149+
grafana.GetGrafanaClientConfig,
150+
grafana.NewGrafanaClientImpl,
151+
wire.Bind(new(grafana.GrafanaClient), new(*grafana.GrafanaClientImpl)),
152+
148153
commonEnforcementFunctionsUtil.NewCommonEnforcementUtilImpl,
149154
wire.Bind(new(commonEnforcementFunctionsUtil.CommonEnforcementUtil), new(*commonEnforcementFunctionsUtil.CommonEnforcementUtilImpl)),
150155

cmd/external-app/wire_gen.go

Lines changed: 10 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/cluster/environment/EnvironmentService.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package environment
1919
import (
2020
"encoding/json"
2121
"fmt"
22+
"github.com/devtron-labs/devtron/client/grafana"
2223
bean3 "github.com/devtron-labs/devtron/pkg/attributes/bean"
2324
"github.com/devtron-labs/devtron/pkg/cluster"
2425
bean4 "github.com/devtron-labs/devtron/pkg/cluster/bean"
@@ -45,6 +46,7 @@ import (
4546

4647
type EnvironmentService interface {
4748
FindOne(environment string) (*bean2.EnvironmentBean, error)
49+
GetDataSourceName(environment string) (string, error)
4850
Create(mappings *bean2.EnvironmentBean, userId int32) (*bean2.EnvironmentBean, error)
4951
Update(mappings *bean2.EnvironmentBean, userId int32) (*bean2.EnvironmentBean, error)
5052
GetAllActive() ([]bean2.EnvironmentBean, error)
@@ -80,14 +82,16 @@ type EnvironmentServiceImpl struct {
8082
userAuthService user.UserAuthService
8183
attributesRepository repository2.AttributesRepository
8284
clusterReadService read.ClusterReadService
85+
grafanaClient grafana.GrafanaClient
8386
}
8487

8588
func NewEnvironmentServiceImpl(environmentRepository repository.EnvironmentRepository,
8689
clusterService cluster.ClusterService, logger *zap.SugaredLogger,
8790
K8sUtil *util2.K8sServiceImpl, k8sInformerFactory informer.K8sInformerFactory,
8891
// propertiesConfigService pipeline.PropertiesConfigService,
8992
userAuthService user.UserAuthService, attributesRepository repository2.AttributesRepository,
90-
clusterReadService read.ClusterReadService) *EnvironmentServiceImpl {
93+
clusterReadService read.ClusterReadService,
94+
grafanaClient grafana.GrafanaClient) *EnvironmentServiceImpl {
9195
return &EnvironmentServiceImpl{
9296
environmentRepository: environmentRepository,
9397
logger: logger,
@@ -98,6 +102,26 @@ func NewEnvironmentServiceImpl(environmentRepository repository.EnvironmentRepos
98102
userAuthService: userAuthService,
99103
attributesRepository: attributesRepository,
100104
clusterReadService: clusterReadService,
105+
grafanaClient: grafanaClient,
106+
}
107+
}
108+
109+
func (impl EnvironmentServiceImpl) GetDataSourceName(environment string) (string, error) {
110+
model, err := impl.environmentRepository.FindOne(environment)
111+
if err != nil {
112+
impl.logger.Errorw("error in fetching environment", "err", err)
113+
return "", err
114+
}
115+
if model.GrafanaDatasourceId == 0 {
116+
return "", fmt.Errorf("prometheus endpoint not found")
117+
} else {
118+
impl.logger.Debugw("environment datasource name", "datasource", model.GrafanaDatasourceId)
119+
datasource, err := impl.grafanaClient.GetDatasource(model.GrafanaDatasourceId)
120+
if err != nil {
121+
impl.logger.Errorw("error in fetching datasource", "err", err)
122+
return "", err
123+
}
124+
return datasource.Name, nil
101125
}
102126
}
103127

wire_gen.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)