Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore: dual write to instance API till UI fully migrates to datasource API (#767)

Danny Xu116c2aa54698
diff --git a/api/instance.go b/api/instance.go
index 8085e11fd3..b363442980 100644
--- a/api/instance.go
+++ b/api/instance.go
@@ -34,6 +34,9 @@ type Instance struct {
ExternalLink string `jsonapi:"attr,externalLink"`
Host string `jsonapi:"attr,host"`
Port string `jsonapi:"attr,port"`
+ Username string `jsonapi:"attr,username"`
+ // Password is not returned to the client
+ Password string
}
// InstanceCreate is the API message for creating an instance.
@@ -81,11 +84,14 @@ type InstancePatch struct {
UpdaterID int
// Domain specific fields
- Name *string `jsonapi:"attr,name"`
- EngineVersion *string
- ExternalLink *string `jsonapi:"attr,externalLink"`
- Host *string `jsonapi:"attr,host"`
- Port *string `jsonapi:"attr,port"`
+ Name *string `jsonapi:"attr,name"`
+ EngineVersion *string
+ ExternalLink *string `jsonapi:"attr,externalLink"`
+ Host *string `jsonapi:"attr,host"`
+ Port *string `jsonapi:"attr,port"`
+ Username *string `jsonapi:"attr,username"`
+ Password *string `jsonapi:"attr,password"`
+ UseEmptyPassword bool `jsonapi:"attr,useEmptyPassword"`
}
// DataSourceFromInstanceWithType gets a typed data source from a instance.
diff --git a/frontend/src/store/modules/instance.ts b/frontend/src/store/modules/instance.ts
index 5504cf0718..cd4e358428 100644
--- a/frontend/src/store/modules/instance.ts
+++ b/frontend/src/store/modules/instance.ts
@@ -34,7 +34,7 @@ function convert(
let environment: Environment = unknown("ENVIRONMENT") as Environment;
environment.id = parseInt(environmentId);
- const anomalyIdList = instance.relationships!.anomaly
+ const anomalyIdList = instance.relationships!.anomalyList
.data as ResourceIdentifier[];
const anomalyList: Anomaly[] = [];
for (const item of anomalyIdList) {
diff --git a/server/instance.go b/server/instance.go
index a74cc82ee1..51b0219813 100644
--- a/server/instance.go
+++ b/server/instance.go
@@ -144,6 +144,50 @@ func (s *Server) registerInstanceRoutes(g *echo.Group) {
}
}
+ // TODO(d): remove this once UI fully switched to data source API.
+ if instancePatch.Username != nil || instancePatch.Password != nil || instancePatch.UseEmptyPassword {
+ instanceFind := &api.InstanceFind{
+ ID: &id,
+ }
+ instance, err = s.InstanceService.FindInstance(ctx, instanceFind)
+ if err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch instance ID: %v", id)).SetInternal(err)
+ }
+ if instance == nil {
+ return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Instance ID not found: %d", id))
+ }
+
+ dataSourceType := api.Admin
+ dataSourceFind := &api.DataSourceFind{
+ InstanceID: &instance.ID,
+ Type: &dataSourceType,
+ }
+ adminDataSource, err := s.DataSourceService.FindDataSource(ctx, dataSourceFind)
+ if err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to fetch data source for instance: %v", instance.Name)).SetInternal(err)
+ }
+ if adminDataSource == nil {
+ err := fmt.Errorf("data source not found for instance ID %v, name %q and type %q", instance.ID, instance.Name, dataSourceType)
+ return echo.NewHTTPError(http.StatusInternalServerError, err.Error()).SetInternal(err)
+ }
+
+ dataSourcePatch := &api.DataSourcePatch{
+ ID: adminDataSource.ID,
+ UpdaterID: c.Get(getPrincipalIDContextKey()).(int),
+ Username: instancePatch.Username,
+ }
+ if instancePatch.Password != nil {
+ dataSourcePatch.Password = instancePatch.Password
+ } else if instancePatch.UseEmptyPassword {
+ password := ""
+ dataSourcePatch.Password = &password
+ }
+ _, err = s.DataSourceService.PatchDataSource(ctx, dataSourcePatch)
+ if err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, fmt.Sprintf("Failed to patch data source for instance: %v", instance.Name)).SetInternal(err)
+ }
+ }
+
if err := s.composeInstanceRelationship(ctx, instance); err != nil {
return err
}
@@ -468,6 +512,12 @@ func (s *Server) composeInstanceRelationship(ctx context.Context, instance *api.
if dataSource.Updater, err = s.composePrincipalByID(ctx, dataSource.UpdaterID); err != nil {
return err
}
+
+ // TODO(d): remove this when UI is fully switched to data source API.
+ if dataSource.Type == api.Admin {
+ instance.Username = dataSource.Username
+ instance.Password = dataSource.Password
+ }
}
return nil
diff --git a/store/seed/test/10000__reset.sql b/store/seed/test/10000__reset.sql
index 1eddbcc46f..6e74825595 100644
--- a/store/seed/test/10000__reset.sql
+++ b/store/seed/test/10000__reset.sql
@@ -23,6 +23,9 @@ DELETE FROM
DELETE FROM
activity;
+DELETE FROM
+ sheet;
+
DELETE FROM
issue_subscriber;
@@ -99,8 +102,6 @@ DELETE FROM
DELETE FROM
deployment_config;
-DELETE FROM
- sheet;
-- Project 1 refers to DEFAULT project which is considered as part of schema
DELETE FROM
project