Skip to content

Commit

Permalink
QOL: Add ClusterIDs array to InstallationDTO object (#1088)
Browse files Browse the repository at this point in the history
* QOL: Add ClusterIDs array to InstallationDTO object

* Fix tests
  • Loading branch information
nickmisasi authored Dec 10, 2024
1 parent 09af48b commit 79eed1f
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 5 deletions.
2 changes: 1 addition & 1 deletion internal/api/installation.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ func handleCreateInstallation(c *Context, w http.ResponseWriter, r *http.Request

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusAccepted)
outputJSON(c, w, installation.ToDTO(annotations, dnsRecords))
outputJSON(c, w, installation.ToDTO(annotations, dnsRecords, nil))
}

func selectGroupForAnnotation(c *Context, annotations []string) (string, error) {
Expand Down
23 changes: 23 additions & 0 deletions internal/store/cluster_installation.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,29 @@ func (sqlStore *SQLStore) getClusterInstallations(db dbInterface, filter *model.
return clusterInstallations, nil
}

func (sqlStore *SQLStore) GetClusterInstallationsForInstallation(installationID string) ([]*model.ClusterInstallation, error) {
return sqlStore.getClusterInstallationsForInstallations(sqlStore.db, installationID)
}

func (sqlStore *SQLStore) GetClusterInstallationsForInstallations(installationIDs []string) ([]*model.ClusterInstallation, error) {
return sqlStore.getClusterInstallationsForInstallations(sqlStore.db, installationIDs...)
}

func (sqlStore *SQLStore) getClusterInstallationsForInstallations(db queryer, installationIDs ...string) ([]*model.ClusterInstallation, error) {
builder := clusterInstallationSelect.
Where(sq.Eq{"InstallationID": installationIDs}).
Where("DeleteAt = 0").
OrderBy("CreateAt ASC")

var clusterInstallations []*model.ClusterInstallation
err := sqlStore.selectBuilder(db, &clusterInstallations, builder)
if err != nil {
return nil, errors.Wrap(err, "failed to query for clusterInstallations")
}

return clusterInstallations, nil
}

// UpdateClusterInstallation updates the given cluster installation in the database.
func (sqlStore *SQLStore) UpdateClusterInstallation(clusterInstallation *model.ClusterInstallation) error {
_, err := sqlStore.execBuilder(sqlStore.db, sq.
Expand Down
24 changes: 22 additions & 2 deletions internal/store/installation_dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,17 @@ func (sqlStore *SQLStore) GetInstallationDTO(id string, includeGroupConfig, incl
return nil, errors.Wrap(err, "failed to fetch DNS records for installation")
}

return installation.ToDTO(annotations, dnsRecords), nil
clusterInstallations, err := sqlStore.GetClusterInstallationsForInstallation(id)
if err != nil {
return nil, errors.Wrap(err, "failed to get cluster installations for installation")
}

clusterIDs := make([]*string, 0, len(clusterInstallations))
for _, clusterInstallation := range clusterInstallations {
clusterIDs = append(clusterIDs, &clusterInstallation.ClusterID)
}

return installation.ToDTO(annotations, dnsRecords, clusterIDs), nil
}

// GetInstallationDTOs fetches the given page of installation with data from connected tables. The first page is 0.
Expand Down Expand Up @@ -57,9 +67,19 @@ func (sqlStore *SQLStore) GetInstallationDTOs(filter *model.InstallationFilter,
mapping[record.InstallationID] = append(mapping[record.InstallationID], record)
}

clusterInstallations, err := sqlStore.GetClusterInstallationsForInstallations(installationIDs)
if err != nil {
return nil, errors.Wrap(err, "failed to get cluster installations for installations")
}

clusterMapping := make(map[string][]*string, len(installations))
for _, clusterInstallation := range clusterInstallations {
clusterMapping[clusterInstallation.InstallationID] = append(clusterMapping[clusterInstallation.InstallationID], &clusterInstallation.ClusterID)
}

dtos := make([]*model.InstallationDTO, 0, len(installations))
for _, inst := range installations {
dtos = append(dtos, inst.ToDTO(annotations[inst.ID], mapping[inst.ID]))
dtos = append(dtos, inst.ToDTO(annotations[inst.ID], mapping[inst.ID], clusterMapping[inst.ID]))
}

return dtos, nil
Expand Down
20 changes: 19 additions & 1 deletion internal/store/installation_dto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,27 @@ func TestInstallationDTOs(t *testing.T) {
err = sqlStore.CreateInstallation(installation2, nil, dnsRecords2)
require.NoError(t, err)

clusterInstallation1 := &model.ClusterInstallation{
ClusterID: model.NewID(),
InstallationID: installation1.ID,
}
clusterInstallation2 := &model.ClusterInstallation{
ClusterID: model.NewID(),
InstallationID: installation2.ID,
}

err = sqlStore.CreateClusterInstallation(clusterInstallation1)
require.NoError(t, err)
err = sqlStore.CreateClusterInstallation(clusterInstallation2)
require.NoError(t, err)

t.Run("get installation DTO", func(t *testing.T) {
installationDTO, err := sqlStore.GetInstallationDTO(installation1.ID, false, false)
require.NoError(t, err)
assert.Equal(t, installation1, installationDTO.Installation)
assert.Equal(t, len(annotations), len(installationDTO.Annotations))
assert.Equal(t, annotations, model.SortAnnotations(installationDTO.Annotations))
assert.ElementsMatch(t, []*string{&clusterInstallation1.ClusterID}, installationDTO.ClusterIDs)
})

t.Run("get installation DTOs", func(t *testing.T) {
Expand All @@ -98,6 +113,9 @@ func TestInstallationDTOs(t *testing.T) {
for _, i := range installationDTOs {
model.SortAnnotations(i.Annotations)
}
assert.Equal(t, []*model.InstallationDTO{installation1.ToDTO(annotations, dnsRecords1), installation2.ToDTO(nil, dnsRecords2)}, installationDTOs)
assert.ElementsMatch(t, []*model.InstallationDTO{
installation1.ToDTO(annotations, dnsRecords1, []*string{&clusterInstallation1.ClusterID}),
installation2.ToDTO(nil, dnsRecords2, []*string{&clusterInstallation2.ClusterID}),
}, installationDTOs)
})
}
3 changes: 2 additions & 1 deletion model/installation.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ func (i *Installation) Clone() *Installation {
}

// ToDTO expands installation to InstallationDTO.
func (i *Installation) ToDTO(annotations []*Annotation, dnsRecords []*InstallationDNS) *InstallationDTO {
func (i *Installation) ToDTO(annotations []*Annotation, dnsRecords []*InstallationDNS, clusterIDs []*string) *InstallationDTO {
var dns string
for _, record := range dnsRecords {
if record.IsPrimary {
Expand All @@ -221,6 +221,7 @@ func (i *Installation) ToDTO(annotations []*Annotation, dnsRecords []*Installati
Annotations: annotations,
DNSRecords: dnsRecords,
DNS: dns,
ClusterIDs: clusterIDs,
}
}

Expand Down
1 change: 1 addition & 0 deletions model/installation_dto.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ type InstallationDTO struct {
// Deprecated: This is for backward compatibility until we switch all clients, as DNS was removed from Installation.
DNS string
DNSRecords []*InstallationDNS
ClusterIDs []*string `json:"ClusterIDs,omitempty"`
}

0 comments on commit 79eed1f

Please sign in to comment.