Skip to content

Commit 664883c

Browse files
committed
minor changes
1 parent 6b4b748 commit 664883c

File tree

6 files changed

+34
-19
lines changed

6 files changed

+34
-19
lines changed

api/cluster/EnvironmentRestHandler.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,15 +170,30 @@ func (impl EnvironmentRestHandlerImpl) Get(w http.ResponseWriter, r *http.Reques
170170

171171
func (impl EnvironmentRestHandlerImpl) GetDataSourceName(w http.ResponseWriter, r *http.Request) {
172172
vars := mux.Vars(r)
173-
environment := vars["environment"]
173+
environmentName := vars["environmentName"]
174+
175+
bean, err := impl.environmentClusterMappingsService.FindOne(environmentName)
176+
if err != nil {
177+
impl.logger.Errorw("service err, Get", "err", err, "payload", bean)
178+
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
179+
return
180+
}
181+
182+
// RBAC enforcer applying
183+
token := r.Header.Get("token")
184+
if ok := impl.enforcer.Enforce(token, casbin.ResourceGlobalEnvironment, casbin.ActionGet, bean.EnvironmentIdentifier); !ok {
185+
common.WriteJsonResp(w, errors.New("unauthorized"), nil, http.StatusForbidden)
186+
return
187+
}
188+
//RBAC enforcer Ends
174189

175-
name, err := impl.environmentClusterMappingsService.GetDataSourceName(environment)
190+
resp, err := impl.environmentClusterMappingsService.GetDataSourceName(bean)
176191
if err != nil {
177-
impl.logger.Errorw("service err, Get", "err", err, "env", environment)
192+
impl.logger.Errorw("service err, Get", "err", err, "env", environmentName)
178193
common.WriteJsonResp(w, err, nil, http.StatusInternalServerError)
179194
return
180195
}
181-
common.WriteJsonResp(w, err, name, http.StatusOK)
196+
common.WriteJsonResp(w, err, resp, http.StatusOK)
182197
}
183198

184199
func (impl EnvironmentRestHandlerImpl) GetAll(w http.ResponseWriter, r *http.Request) {

api/cluster/EnvironmentRouter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func (impl EnvironmentRouterImpl) InitEnvironmentClusterMappingsRouter(environme
4343
HandlerFunc(impl.environmentClusterMappingsRestHandler.Get)
4444
environmentClusterMappingsRouter.Path("/data-source-name").
4545
Methods("GET").
46-
Queries("environment", "{environment}").
46+
Queries("environmentName", "{environmentName}").
4747
HandlerFunc(impl.environmentClusterMappingsRestHandler.GetDataSourceName)
4848
environmentClusterMappingsRouter.Path("").
4949
Methods("GET").

client/grafana/GrafanaClient.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ func (impl *GrafanaClientImpl) CreateDatasource(createDatasourceRequest CreateDa
380380
url = fmt.Sprintf(url, impl.config.GrafanaUsername, impl.config.GrafanaPassword)
381381
req, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer(reqBody))
382382
if err != nil {
383-
// do not log url or req body as they contains sensitive data
383+
// do not log url or req body as they contain sensitive data
384384
impl.logger.Errorw("error while creating http request", "destinationURL", impl.config.DestinationURL, "err", err)
385385
return nil, err
386386
}

pkg/cluster/ClusterServiceExtended.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ import (
3636
repository2 "github.com/devtron-labs/devtron/pkg/appStore/installedApp/repository"
3737
)
3838

39+
const DataSourceNameFormat = "Prometheus-%s-EnvId-%d"
40+
3941
// extends ClusterServiceImpl and enhances method of ClusterService with full mode specific errors
4042
type ClusterServiceImplExtended struct {
4143
environmentRepository repository.EnvironmentRepository
@@ -268,7 +270,8 @@ func (impl *ClusterServiceImplExtended) CreateGrafanaDataSource(clusterBean *bea
268270
//starts grafana creation
269271
// appending envId to ensure unique datasource name for each environment (ex- env got deleted and created with same name)
270272
// reverting to old name will be done in next release
271-
DataSourceName := "Prometheus-" + env.Name + "-EnvId-" + fmt.Sprint(env.Id)
273+
274+
DataSourceName := fmt.Sprintf(DataSourceNameFormat, env.Name, env.Id)
272275
createDatasourceReq := grafana.CreateDatasourceRequest{
273276
Name: DataSourceName,
274277
Type: "prometheus",

pkg/cluster/environment/EnvironmentService.go

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ import (
4646

4747
type EnvironmentService interface {
4848
FindOne(environment string) (*bean2.EnvironmentBean, error)
49-
GetDataSourceName(environment string) (dataSourceData bean2.DataSourceMetaData, err error)
49+
GetDataSourceName(*bean2.EnvironmentBean) (dataSourceData *bean2.DataSourceMetaData, err error)
5050
Create(mappings *bean2.EnvironmentBean, userId int32) (*bean2.EnvironmentBean, error)
5151
Update(mappings *bean2.EnvironmentBean, userId int32) (*bean2.EnvironmentBean, error)
5252
GetAllActive() ([]bean2.EnvironmentBean, error)
@@ -106,24 +106,19 @@ func NewEnvironmentServiceImpl(environmentRepository repository.EnvironmentRepos
106106
}
107107
}
108108

109-
func (impl EnvironmentServiceImpl) GetDataSourceName(environment string) (bean2.DataSourceMetaData, error) {
110-
datasource := bean2.DataSourceMetaData{}
111-
model, err := impl.environmentRepository.FindOne(environment)
112-
if err != nil {
113-
impl.logger.Errorw("error in fetching environment", "err", err)
114-
return datasource, err
115-
}
116-
if model.GrafanaDatasourceId == 0 {
109+
func (impl EnvironmentServiceImpl) GetDataSourceName(bean *bean2.EnvironmentBean) (*bean2.DataSourceMetaData, error) {
110+
datasource := &bean2.DataSourceMetaData{}
111+
if bean.DataSourceId == 0 || bean.PrometheusEndpoint == "" {
117112
return datasource, fmt.Errorf("prometheus endpoint not found")
118113
} else {
119-
impl.logger.Debugw("environment datasource name", "datasource", model.GrafanaDatasourceId)
120-
data, err := impl.grafanaClient.GetDatasource(model.GrafanaDatasourceId)
114+
impl.logger.Debugw("environment datasource name", "datasource", bean.DataSourceId)
115+
data, err := impl.grafanaClient.GetDatasource(bean.DataSourceId)
121116
if err != nil {
122117
impl.logger.Errorw("error in fetching datasource", "err", err)
123118
return datasource, err
124119
}
125120
datasource.Name = data.Name
126-
datasource.Id = model.GrafanaDatasourceId
121+
datasource.Id = bean.DataSourceId
127122
return datasource, nil
128123
}
129124
}
@@ -210,6 +205,7 @@ func (impl EnvironmentServiceImpl) FindOne(environment string) (*bean2.Environme
210205
Default: model.Default,
211206
EnvironmentIdentifier: model.EnvironmentIdentifier,
212207
Description: model.Description,
208+
DataSourceId: model.GrafanaDatasourceId,
213209
}
214210
return bean, nil
215211
}

pkg/cluster/environment/bean/bean.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ type EnvironmentBean struct {
3939
ClusterCAData string `json:"-"`
4040
ClusterKeyData string `json:"-"`
4141
ClusterCertData string `json:"-"`
42+
DataSourceId int `json:"-"`
4243
}
4344

4445
type EnvDto struct {

0 commit comments

Comments
 (0)