Skip to content

Commit

Permalink
Merge pull request #491 from kube-tarian/plugin-store-db-find
Browse files Browse the repository at this point in the history
error handling for postgres table record not exists cases
  • Loading branch information
vramk23 authored May 19, 2024
2 parents ee19af2 + 9b0418d commit b4b5a5f
Show file tree
Hide file tree
Showing 13 changed files with 53 additions and 76 deletions.
9 changes: 4 additions & 5 deletions capten/common-pkg/capten-store/app_config_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func (a *Store) UpsertAppConfig(appData *agentpb.SyncAppData) error {

appConfig := &ClusterAppConfig{}
recordFound := true
err := a.dbClient.Find(appConfig, ClusterAppConfig{ReleaseName: appData.Config.ReleaseName})
err := a.dbClient.FindFirst(appConfig, ClusterAppConfig{ReleaseName: appData.Config.ReleaseName})
if err != nil {
if gerrors.GetErrorType(err) != postgresdb.ObjectNotExist {
return prepareError(err, appData.Config.ReleaseName, "Fetch")
Expand Down Expand Up @@ -59,9 +59,8 @@ func (a *Store) UpsertAppConfig(appData *agentpb.SyncAppData) error {

func (a *Store) GetAppConfig(appReleaseName string) (*agentpb.SyncAppData, error) {
appConfig := &ClusterAppConfig{}
err := a.dbClient.Find(appConfig, ClusterAppConfig{ReleaseName: appReleaseName})
err := a.dbClient.FindFirst(appConfig, ClusterAppConfig{ReleaseName: appReleaseName})
if err != nil {
err = prepareError(err, appReleaseName, "Fetch")
return nil, err
}

Expand Down Expand Up @@ -109,8 +108,8 @@ func (a *Store) GetAppConfig(appReleaseName string) (*agentpb.SyncAppData, error
func (a *Store) GetAllApps() ([]*agentpb.SyncAppData, error) {
var appConfigs []ClusterAppConfig
err := a.dbClient.Find(&appConfigs, nil)
if err != nil && gerrors.GetErrorType(err) != postgresdb.ObjectNotExist {
return nil, fmt.Errorf("Unable to fetch apps: %v", err.Error())
if err != nil {
return nil, fmt.Errorf("failed to fetch apps: %v", err.Error())
}

var appData []*agentpb.SyncAppData
Expand Down
16 changes: 5 additions & 11 deletions capten/common-pkg/capten-store/cloud_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ import (
"time"

"github.com/google/uuid"
"github.com/kube-tarian/kad/capten/common-pkg/gerrors"
"github.com/kube-tarian/kad/capten/common-pkg/pb/captenpluginspb"
postgresdb "github.com/kube-tarian/kad/capten/common-pkg/postgres"
"gorm.io/gorm"
)

func (a *Store) UpsertCloudProvider(config *captenpluginspb.CloudProvider) error {
Expand All @@ -33,7 +30,7 @@ func (a *Store) UpsertCloudProvider(config *captenpluginspb.CloudProvider) error

func (a *Store) GetCloudProviderForID(id string) (*captenpluginspb.CloudProvider, error) {
provider := CloudProvider{}
err := a.dbClient.Find(&provider, CloudProvider{ID: uuid.MustParse(id)})
err := a.dbClient.FindFirst(&provider, CloudProvider{ID: uuid.MustParse(id)})
if err != nil {
return nil, err
}
Expand All @@ -51,7 +48,7 @@ func (a *Store) GetCloudProviderForID(id string) (*captenpluginspb.CloudProvider
func (a *Store) GetCloudProviders() ([]*captenpluginspb.CloudProvider, error) {
providers := []CloudProvider{}
err := a.dbClient.Find(&providers, nil)
if err != nil && gerrors.GetErrorType(err) != postgresdb.ObjectNotExist {
if err != nil {
return nil, fmt.Errorf("failed to fetch providers: %v", err.Error())
}

Expand All @@ -70,11 +67,8 @@ func (a *Store) GetCloudProviders() ([]*captenpluginspb.CloudProvider, error) {
func (a *Store) GetCloudProvidersByLabelsAndCloudType(searchLabels []string, cloudType string) ([]*captenpluginspb.CloudProvider, error) {
providers := []CloudProvider{}
err := a.dbClient.Session().Where("cloud_type = ?", cloudType).Where("labels @> ?", fmt.Sprintf("{%s}", searchLabels[0])).Find(&providers).Error
if err != nil && gerrors.GetErrorType(err) != postgresdb.ObjectNotExist {
if gorm.ErrRecordNotFound != err {
return nil, fmt.Errorf("failed to fetch providers: %v", err.Error())
}
err = nil
if err != nil {
return nil, fmt.Errorf("failed to fetch providers: %v", err.Error())
}

cloudProviders := make([]*captenpluginspb.CloudProvider, 0)
Expand All @@ -92,7 +86,7 @@ func (a *Store) GetCloudProvidersByLabelsAndCloudType(searchLabels []string, clo
func (a *Store) GetCloudProvidersByLabels(searchLabels []string) ([]*captenpluginspb.CloudProvider, error) {
providers := []CloudProvider{}
err := a.dbClient.Find(&providers, "labels @> ?", fmt.Sprintf("{%s}", searchLabels[0]))
if err != nil && gerrors.GetErrorType(err) != postgresdb.ObjectNotExist {
if err != nil {
return nil, fmt.Errorf("failed to fetch providers: %v", err.Error())
}

Expand Down
9 changes: 2 additions & 7 deletions capten/common-pkg/capten-store/cluster_plugin_config_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"github.com/kube-tarian/kad/capten/common-pkg/gerrors"
"github.com/kube-tarian/kad/capten/common-pkg/pb/clusterpluginspb"
postgresdb "github.com/kube-tarian/kad/capten/common-pkg/postgres"
"github.com/pkg/errors"
"gorm.io/gorm"
)

func (a *Store) UpsertClusterPluginConfig(pluginConfig *clusterpluginspb.Plugin) error {
Expand Down Expand Up @@ -67,11 +65,8 @@ func (a *Store) DeleteClusterPluginConfig(pluginName string) error {

func (a *Store) GetClusterPluginConfig(pluginName string) (*clusterpluginspb.Plugin, error) {
var pluginConfig ClusterPluginConfig
err := a.dbClient.Find(&pluginConfig, "plugin_name = ?", pluginName)
err := a.dbClient.FindFirst(&pluginConfig, ClusterPluginConfig{PluginName: pluginName})
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
}
return nil, err
}

Expand Down Expand Up @@ -103,7 +98,7 @@ func (a *Store) GetClusterPluginConfig(pluginName string) (*clusterpluginspb.Plu
func (a *Store) GetAllClusterPluginConfigs() ([]*clusterpluginspb.Plugin, error) {
var plugins []ClusterPluginConfig
err := a.dbClient.Find(&plugins, nil)
if err != nil && gerrors.GetErrorType(err) != postgresdb.ObjectNotExist {
if err != nil {
return nil, fmt.Errorf("failed to fetch plugins: %v", err.Error())
}

Expand Down
8 changes: 3 additions & 5 deletions capten/common-pkg/capten-store/container_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import (
"time"

"github.com/google/uuid"
"github.com/kube-tarian/kad/capten/common-pkg/gerrors"
"github.com/kube-tarian/kad/capten/common-pkg/pb/captenpluginspb"
postgresdb "github.com/kube-tarian/kad/capten/common-pkg/postgres"
)

func (a *Store) UpsertContainerRegistry(config *captenpluginspb.ContainerRegistry) error {
Expand All @@ -32,7 +30,7 @@ func (a *Store) UpsertContainerRegistry(config *captenpluginspb.ContainerRegistr

func (a *Store) GetContainerRegistryForID(id string) (*captenpluginspb.ContainerRegistry, error) {
registry := ContainerRegistry{}
err := a.dbClient.Find(&registry, ContainerRegistry{ID: uuid.MustParse(id)})
err := a.dbClient.FindFirst(&registry, ContainerRegistry{ID: uuid.MustParse(id)})
if err != nil {
return nil, err
}
Expand All @@ -50,7 +48,7 @@ func (a *Store) GetContainerRegistryForID(id string) (*captenpluginspb.Container
func (a *Store) GetContainerRegistries() ([]*captenpluginspb.ContainerRegistry, error) {
registries := []ContainerRegistry{}
err := a.dbClient.Find(&registries, nil)
if err != nil && gerrors.GetErrorType(err) != postgresdb.ObjectNotExist {
if err != nil {
return nil, fmt.Errorf("failed to fetch registries: %v", err.Error())
}

Expand All @@ -70,7 +68,7 @@ func (a *Store) GetContainerRegistries() ([]*captenpluginspb.ContainerRegistry,
func (a *Store) GetContainerRegistriesByLabels(searchLabels []string) ([]*captenpluginspb.ContainerRegistry, error) {
registries := []ContainerRegistry{}
err := a.dbClient.Find(&registries, "labels @> ?", fmt.Sprintf("{%s}", searchLabels[0]))
if err != nil && gerrors.GetErrorType(err) != postgresdb.ObjectNotExist {
if err != nil {
return nil, fmt.Errorf("failed to fetch registries: %v", err.Error())
}

Expand Down
10 changes: 3 additions & 7 deletions capten/common-pkg/capten-store/crossplane_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"github.com/kube-tarian/kad/capten/common-pkg/gerrors"
postgresdb "github.com/kube-tarian/kad/capten/common-pkg/postgres"
"github.com/kube-tarian/kad/capten/model"
"github.com/pkg/errors"
"gorm.io/gorm"
)

func (a *Store) UpsertCrossplaneProject(crossplaneProject *model.CrossplaneProject) error {
Expand Down Expand Up @@ -47,11 +45,9 @@ func (a *Store) DeleteCrossplaneProject(id string) error {

func (a *Store) GetCrossplaneProjectForID(id string) (*model.CrossplaneProject, error) {
project := CrossplaneProject{}
err := a.dbClient.Find(&project, CrossplaneProject{ID: 1})
err := a.dbClient.FindFirst(&project, CrossplaneProject{ID: 1})
if err != nil {
return nil, err
} else if project.ID == 0 {
return nil, gorm.ErrRecordNotFound
}

crossplaneProject := &model.CrossplaneProject{
Expand All @@ -70,7 +66,7 @@ func (a *Store) GetCrossplaneProject() (*model.CrossplaneProject, error) {

func (a *Store) updateCrossplaneProject() (*model.CrossplaneProject, error) {
allCrossplaneGitProjects, err := a.GetGitProjectsByLabels([]string{"crossplane"})
if err != nil && gerrors.GetErrorType(err) != postgresdb.ObjectNotExist {
if err != nil {
return nil, fmt.Errorf("failed to fetch projects: %v", err.Error())
}

Expand All @@ -85,7 +81,7 @@ func (a *Store) updateCrossplaneProject() (*model.CrossplaneProject, error) {

crossplaneProject, err := a.GetCrossplaneProjectForID("0")
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
if gerrors.GetErrorType(err) == postgresdb.ObjectNotExist {
project := CrossplaneProject{
ID: 1,
GitProjectID: gitProjectUUID,
Expand Down
6 changes: 3 additions & 3 deletions capten/common-pkg/capten-store/crossplane_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (a *Store) DeleteCrossplaneProviderById(id string) error {
func (a *Store) GetCrossplaneProviders() ([]*captenpluginspb.CrossplaneProvider, error) {
providers := []CrossplaneProvider{}
err := a.dbClient.Find(&providers, nil)
if err != nil && gerrors.GetErrorType(err) != postgresdb.ObjectNotExist {
if err != nil {
return nil, fmt.Errorf("failed to fetch providers: %v", err.Error())
}

Expand Down Expand Up @@ -68,7 +68,7 @@ func (a *Store) UpdateCrossplaneProvider(provider *model.CrossplaneProvider) err
func (a *Store) GetCrossplanProviderByCloudType(cloudType string) (*captenpluginspb.CrossplaneProvider, error) {
providers := []CrossplaneProvider{}
err := a.dbClient.Find(&providers, "cloud_type = ?", cloudType)
if err != nil && gerrors.GetErrorType(err) != postgresdb.ObjectNotExist {
if err != nil {
return nil, fmt.Errorf("failed to fetch providers: %v", err.Error())
}

Expand All @@ -88,7 +88,7 @@ func (a *Store) GetCrossplanProviderByCloudType(cloudType string) (*captenplugin

func (a *Store) GetCrossplanProviderById(id string) (*captenpluginspb.CrossplaneProvider, error) {
provider := CrossplaneProvider{}
err := a.dbClient.Find(&provider, CrossplaneProvider{ID: uuid.MustParse(id)})
err := a.dbClient.FindFirst(&provider, CrossplaneProvider{ID: uuid.MustParse(id)})
if err != nil {
return nil, err
}
Expand Down
8 changes: 3 additions & 5 deletions capten/common-pkg/capten-store/git_projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import (
"time"

"github.com/google/uuid"
"github.com/kube-tarian/kad/capten/common-pkg/gerrors"
"github.com/kube-tarian/kad/capten/common-pkg/pb/captenpluginspb"
postgresdb "github.com/kube-tarian/kad/capten/common-pkg/postgres"
)

func (a *Store) UpsertGitProject(config *captenpluginspb.GitProject) error {
Expand Down Expand Up @@ -38,7 +36,7 @@ func (a *Store) DeleteGitProjectById(id string) error {

func (a *Store) GetGitProjectForID(id string) (*captenpluginspb.GitProject, error) {
project := GitProject{}
err := a.dbClient.Find(&project, GitProject{ID: uuid.MustParse(id)})
err := a.dbClient.FindFirst(&project, GitProject{ID: uuid.MustParse(id)})
if err != nil {
return nil, err
}
Expand All @@ -55,7 +53,7 @@ func (a *Store) GetGitProjectForID(id string) (*captenpluginspb.GitProject, erro
func (a *Store) GetGitProjects() ([]*captenpluginspb.GitProject, error) {
projects := []GitProject{}
err := a.dbClient.Find(&projects, nil)
if err != nil && gerrors.GetErrorType(err) != postgresdb.ObjectNotExist {
if err != nil {
return nil, fmt.Errorf("failed to fetch projects: %v", err.Error())
}

Expand All @@ -74,7 +72,7 @@ func (a *Store) GetGitProjects() ([]*captenpluginspb.GitProject, error) {
func (a *Store) GetGitProjectsByLabels(searchLabels []string) ([]*captenpluginspb.GitProject, error) {
projects := []GitProject{}
err := a.dbClient.Find(&projects, "labels @> ?", fmt.Sprintf("{%s}", searchLabels[0]))
if err != nil && gerrors.GetErrorType(err) != postgresdb.ObjectNotExist {
if err != nil {
return nil, fmt.Errorf("failed to fetch projects: %v", err.Error())
}

Expand Down
8 changes: 2 additions & 6 deletions capten/common-pkg/capten-store/integration_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -815,12 +815,8 @@ func TestPluginStoreConfig(t *testing.T) {
return
}

config, err = store.GetPluginStoreConfig(pluginstorepb.StoreType_CENTRAL_STORE)
if !assert.Nil(t, err) {
t.Logf("get plugin store config error, %v", err)
return
}
assert.Equal(t, config.GitProjectURL, "")
_, err = store.GetPluginStoreConfig(pluginstorepb.StoreType_CENTRAL_STORE)
assert.Error(t, err)
}

func TestPluginStoreData(t *testing.T) {
Expand Down
9 changes: 2 additions & 7 deletions capten/common-pkg/capten-store/managed_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ import (
"time"

"github.com/google/uuid"
"github.com/kube-tarian/kad/capten/common-pkg/gerrors"
"github.com/kube-tarian/kad/capten/common-pkg/pb/captenpluginspb"
postgresdb "github.com/kube-tarian/kad/capten/common-pkg/postgres"
"gorm.io/gorm"
)

func (a *Store) UpsertManagedCluster(managedCluster *captenpluginspb.ManagedCluster) error {
Expand Down Expand Up @@ -42,11 +39,9 @@ func (a *Store) DeleteManagedClusterById(id string) error {

func (a *Store) GetManagedClusterForID(id string) (*captenpluginspb.ManagedCluster, error) {
cluster := ManagedCluster{}
err := a.dbClient.Find(&cluster, ManagedCluster{ID: uuid.MustParse(id)})
err := a.dbClient.FindFirst(&cluster, ManagedCluster{ID: uuid.MustParse(id)})
if err != nil {
return nil, err
} else if cluster.ID == uuid.Nil {
return nil, gorm.ErrRecordNotFound
}

result := &captenpluginspb.ManagedCluster{
Expand All @@ -63,7 +58,7 @@ func (a *Store) GetManagedClusterForID(id string) (*captenpluginspb.ManagedClust
func (a *Store) GetManagedClusters() ([]*captenpluginspb.ManagedCluster, error) {
clusters := []ManagedCluster{}
err := a.dbClient.Find(&clusters, nil)
if err != nil && gerrors.GetErrorType(err) != postgresdb.ObjectNotExist {
if err != nil {
return nil, fmt.Errorf("failed to fetch clusters: %v", err.Error())
}

Expand Down
8 changes: 3 additions & 5 deletions capten/common-pkg/capten-store/plugin_store_config_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,13 @@ import (
func (a *Store) UpsertPluginStoreConfig(config *pluginstorepb.PluginStoreConfig) error {
pluginStoreConfig := &PluginStoreConfig{}
recordFound := true
err := a.dbClient.Find(pluginStoreConfig, PluginStoreConfig{StoreType: int(config.StoreType)})
err := a.dbClient.FindFirst(pluginStoreConfig, PluginStoreConfig{StoreType: int(config.StoreType)})
if err != nil {
if gerrors.GetErrorType(err) != postgresdb.ObjectNotExist {
return prepareError(err, fmt.Sprintf("%d", config.StoreType), "Fetch")
}
err = nil
recordFound = false
} else if pluginStoreConfig.StoreType == 0 {
recordFound = false
}

pluginStoreConfig.StoreType = int(config.StoreType)
Expand All @@ -39,9 +37,9 @@ func (a *Store) UpsertPluginStoreConfig(config *pluginstorepb.PluginStoreConfig)

func (a *Store) GetPluginStoreConfig(storeType pluginstorepb.StoreType) (*pluginstorepb.PluginStoreConfig, error) {
pluginStoreConfig := &PluginStoreConfig{}
err := a.dbClient.Find(pluginStoreConfig, PluginStoreConfig{StoreType: int(storeType)})
err := a.dbClient.FindFirst(pluginStoreConfig, PluginStoreConfig{StoreType: int(storeType)})
if err != nil {
return nil, prepareError(err, fmt.Sprintf("%d", storeType), "Fetch")
return nil, err
}

return &pluginstorepb.PluginStoreConfig{
Expand Down
11 changes: 3 additions & 8 deletions capten/common-pkg/capten-store/plugin_store_date_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@ import (
"github.com/kube-tarian/kad/capten/common-pkg/gerrors"
"github.com/kube-tarian/kad/capten/common-pkg/pb/pluginstorepb"
postgresdb "github.com/kube-tarian/kad/capten/common-pkg/postgres"
"gorm.io/gorm"
)

func (a *Store) UpsertPluginStoreData(gitProjectID string, pluginData *pluginstorepb.PluginData) error {
pluginStoreData := &PluginStoreData{}
recordFound := true
err := a.dbClient.Find(pluginStoreData, PluginStoreData{
err := a.dbClient.FindFirst(pluginStoreData, PluginStoreData{
StoreType: int(pluginData.StoreType),
GitProjectID: uuid.MustParse(gitProjectID),
PluginName: pluginData.PluginName})
Expand All @@ -25,8 +24,6 @@ func (a *Store) UpsertPluginStoreData(gitProjectID string, pluginData *pluginsto
}
err = nil
recordFound = false
} else if pluginStoreData.StoreType == 0 {
recordFound = false
}

pluginStoreData.StoreType = int(pluginData.StoreType)
Expand All @@ -51,15 +48,13 @@ func (a *Store) UpsertPluginStoreData(gitProjectID string, pluginData *pluginsto

func (a *Store) GetPluginStoreData(storeType pluginstorepb.StoreType, gitProjectId, pluginName string) (*pluginstorepb.PluginData, error) {
pluginStoreData := &PluginStoreData{}
err := a.dbClient.Find(pluginStoreData, PluginStoreData{
err := a.dbClient.FindFirst(pluginStoreData, PluginStoreData{
StoreType: int(storeType),
GitProjectID: uuid.MustParse(gitProjectId),
PluginName: pluginName,
})
if err != nil {
return nil, prepareError(err, fmt.Sprintf("%s/%s/%s", gitProjectId, gitProjectId, pluginName), "Fetch")
} else if pluginStoreData.StoreType == 0 {
return nil, gorm.ErrRecordNotFound
return nil, err
}

pluginData := &pluginstorepb.PluginData{
Expand Down
Loading

0 comments on commit b4b5a5f

Please sign in to comment.