Add more watcher tests
Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
This commit is contained in:
parent
37f6434ed8
commit
8a79d9e8f9
5 changed files with 366 additions and 23 deletions
|
|
@ -16,6 +16,7 @@ package sql
|
|||
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/pkg/errors"
|
||||
|
|
@ -23,10 +24,11 @@ import (
|
|||
|
||||
runnerErrors "github.com/cloudbase/garm-provider-common/errors"
|
||||
"github.com/cloudbase/garm-provider-common/util"
|
||||
"github.com/cloudbase/garm/database/common"
|
||||
"github.com/cloudbase/garm/params"
|
||||
)
|
||||
|
||||
func (s *sqlDatabase) CreateEnterprise(ctx context.Context, name, credentialsName, webhookSecret string, poolBalancerType params.PoolBalancerType) (params.Enterprise, error) {
|
||||
func (s *sqlDatabase) CreateEnterprise(ctx context.Context, name, credentialsName, webhookSecret string, poolBalancerType params.PoolBalancerType) (paramEnt params.Enterprise, err error) {
|
||||
if webhookSecret == "" {
|
||||
return params.Enterprise{}, errors.New("creating enterprise: missing secret")
|
||||
}
|
||||
|
|
@ -34,6 +36,12 @@ func (s *sqlDatabase) CreateEnterprise(ctx context.Context, name, credentialsNam
|
|||
if err != nil {
|
||||
return params.Enterprise{}, errors.Wrap(err, "encoding secret")
|
||||
}
|
||||
|
||||
defer func() {
|
||||
if err == nil {
|
||||
s.sendNotify(common.EnterpriseEntityType, common.CreateOperation, paramEnt)
|
||||
}
|
||||
}()
|
||||
newEnterprise := Enterprise{
|
||||
Name: name,
|
||||
WebhookSecret: secret,
|
||||
|
|
@ -66,12 +74,12 @@ func (s *sqlDatabase) CreateEnterprise(ctx context.Context, name, credentialsNam
|
|||
return params.Enterprise{}, errors.Wrap(err, "creating enterprise")
|
||||
}
|
||||
|
||||
param, err := s.sqlToCommonEnterprise(newEnterprise, true)
|
||||
paramEnt, err = s.sqlToCommonEnterprise(newEnterprise, true)
|
||||
if err != nil {
|
||||
return params.Enterprise{}, errors.Wrap(err, "creating enterprise")
|
||||
}
|
||||
|
||||
return param, nil
|
||||
return paramEnt, nil
|
||||
}
|
||||
|
||||
func (s *sqlDatabase) GetEnterprise(ctx context.Context, name string) (params.Enterprise, error) {
|
||||
|
|
@ -124,11 +132,22 @@ func (s *sqlDatabase) ListEnterprises(_ context.Context) ([]params.Enterprise, e
|
|||
}
|
||||
|
||||
func (s *sqlDatabase) DeleteEnterprise(ctx context.Context, enterpriseID string) error {
|
||||
enterprise, err := s.getEnterpriseByID(ctx, s.conn, enterpriseID)
|
||||
enterprise, err := s.getEnterpriseByID(ctx, s.conn, enterpriseID, "Endpoint", "Credentials")
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "fetching enterprise")
|
||||
}
|
||||
|
||||
defer func(ent Enterprise) {
|
||||
if err == nil {
|
||||
asParams, innerErr := s.sqlToCommonEnterprise(ent, true)
|
||||
if innerErr == nil {
|
||||
s.sendNotify(common.EnterpriseEntityType, common.DeleteOperation, asParams)
|
||||
} else {
|
||||
slog.With(slog.Any("error", innerErr)).ErrorContext(ctx, "error sending delete notification", "enterprise", enterpriseID)
|
||||
}
|
||||
}
|
||||
}(enterprise)
|
||||
|
||||
q := s.conn.Unscoped().Delete(&enterprise)
|
||||
if q.Error != nil && !errors.Is(q.Error, gorm.ErrRecordNotFound) {
|
||||
return errors.Wrap(q.Error, "deleting enterprise")
|
||||
|
|
@ -137,10 +156,15 @@ func (s *sqlDatabase) DeleteEnterprise(ctx context.Context, enterpriseID string)
|
|||
return nil
|
||||
}
|
||||
|
||||
func (s *sqlDatabase) UpdateEnterprise(ctx context.Context, enterpriseID string, param params.UpdateEntityParams) (params.Enterprise, error) {
|
||||
func (s *sqlDatabase) UpdateEnterprise(ctx context.Context, enterpriseID string, param params.UpdateEntityParams) (newParams params.Enterprise, err error) {
|
||||
defer func() {
|
||||
if err == nil {
|
||||
s.sendNotify(common.EnterpriseEntityType, common.UpdateOperation, newParams)
|
||||
}
|
||||
}()
|
||||
var enterprise Enterprise
|
||||
var creds GithubCredentials
|
||||
err := s.conn.Transaction(func(tx *gorm.DB) error {
|
||||
err = s.conn.Transaction(func(tx *gorm.DB) error {
|
||||
var err error
|
||||
enterprise, err = s.getEnterpriseByID(ctx, tx, enterpriseID)
|
||||
if err != nil {
|
||||
|
|
@ -196,7 +220,7 @@ func (s *sqlDatabase) UpdateEnterprise(ctx context.Context, enterpriseID string,
|
|||
if err != nil {
|
||||
return params.Enterprise{}, errors.Wrap(err, "updating enterprise")
|
||||
}
|
||||
newParams, err := s.sqlToCommonEnterprise(enterprise, true)
|
||||
newParams, err = s.sqlToCommonEnterprise(enterprise, true)
|
||||
if err != nil {
|
||||
return params.Enterprise{}, errors.Wrap(err, "updating enterprise")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@ package sql
|
|||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/pkg/errors"
|
||||
|
|
@ -24,10 +25,11 @@ import (
|
|||
|
||||
runnerErrors "github.com/cloudbase/garm-provider-common/errors"
|
||||
"github.com/cloudbase/garm-provider-common/util"
|
||||
"github.com/cloudbase/garm/database/common"
|
||||
"github.com/cloudbase/garm/params"
|
||||
)
|
||||
|
||||
func (s *sqlDatabase) CreateOrganization(ctx context.Context, name, credentialsName, webhookSecret string, poolBalancerType params.PoolBalancerType) (params.Organization, error) {
|
||||
func (s *sqlDatabase) CreateOrganization(ctx context.Context, name, credentialsName, webhookSecret string, poolBalancerType params.PoolBalancerType) (org params.Organization, err error) {
|
||||
if webhookSecret == "" {
|
||||
return params.Organization{}, errors.New("creating org: missing secret")
|
||||
}
|
||||
|
|
@ -35,6 +37,12 @@ func (s *sqlDatabase) CreateOrganization(ctx context.Context, name, credentialsN
|
|||
if err != nil {
|
||||
return params.Organization{}, errors.Wrap(err, "encoding secret")
|
||||
}
|
||||
|
||||
defer func() {
|
||||
if err == nil {
|
||||
s.sendNotify(common.OrganizationEntityType, common.CreateOperation, org)
|
||||
}
|
||||
}()
|
||||
newOrg := Organization{
|
||||
Name: name,
|
||||
WebhookSecret: secret,
|
||||
|
|
@ -68,13 +76,13 @@ func (s *sqlDatabase) CreateOrganization(ctx context.Context, name, credentialsN
|
|||
return params.Organization{}, errors.Wrap(err, "creating org")
|
||||
}
|
||||
|
||||
param, err := s.sqlToCommonOrganization(newOrg, true)
|
||||
org, err = s.sqlToCommonOrganization(newOrg, true)
|
||||
if err != nil {
|
||||
return params.Organization{}, errors.Wrap(err, "creating org")
|
||||
}
|
||||
param.WebhookSecret = webhookSecret
|
||||
org.WebhookSecret = webhookSecret
|
||||
|
||||
return param, nil
|
||||
return org, nil
|
||||
}
|
||||
|
||||
func (s *sqlDatabase) GetOrganization(ctx context.Context, name string) (params.Organization, error) {
|
||||
|
|
@ -114,12 +122,23 @@ func (s *sqlDatabase) ListOrganizations(_ context.Context) ([]params.Organizatio
|
|||
return ret, nil
|
||||
}
|
||||
|
||||
func (s *sqlDatabase) DeleteOrganization(ctx context.Context, orgID string) error {
|
||||
org, err := s.getOrgByID(ctx, s.conn, orgID)
|
||||
func (s *sqlDatabase) DeleteOrganization(ctx context.Context, orgID string) (err error) {
|
||||
org, err := s.getOrgByID(ctx, s.conn, orgID, "Endpoint", "Credentials")
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "fetching org")
|
||||
}
|
||||
|
||||
defer func(org Organization) {
|
||||
if err == nil {
|
||||
asParam, innerErr := s.sqlToCommonOrganization(org, true)
|
||||
if innerErr == nil {
|
||||
s.sendNotify(common.OrganizationEntityType, common.DeleteOperation, asParam)
|
||||
} else {
|
||||
slog.With(slog.Any("error", innerErr)).ErrorContext(ctx, "error sending delete notification", "org", orgID)
|
||||
}
|
||||
}
|
||||
}(org)
|
||||
|
||||
q := s.conn.Unscoped().Delete(&org)
|
||||
if q.Error != nil && !errors.Is(q.Error, gorm.ErrRecordNotFound) {
|
||||
return errors.Wrap(q.Error, "deleting org")
|
||||
|
|
@ -128,10 +147,15 @@ func (s *sqlDatabase) DeleteOrganization(ctx context.Context, orgID string) erro
|
|||
return nil
|
||||
}
|
||||
|
||||
func (s *sqlDatabase) UpdateOrganization(ctx context.Context, orgID string, param params.UpdateEntityParams) (params.Organization, error) {
|
||||
func (s *sqlDatabase) UpdateOrganization(ctx context.Context, orgID string, param params.UpdateEntityParams) (paramOrg params.Organization, err error) {
|
||||
defer func() {
|
||||
if err == nil {
|
||||
s.sendNotify(common.OrganizationEntityType, common.UpdateOperation, paramOrg)
|
||||
}
|
||||
}()
|
||||
var org Organization
|
||||
var creds GithubCredentials
|
||||
err := s.conn.Transaction(func(tx *gorm.DB) error {
|
||||
err = s.conn.Transaction(func(tx *gorm.DB) error {
|
||||
var err error
|
||||
org, err = s.getOrgByID(ctx, tx, orgID)
|
||||
if err != nil {
|
||||
|
|
@ -188,11 +212,11 @@ func (s *sqlDatabase) UpdateOrganization(ctx context.Context, orgID string, para
|
|||
if err != nil {
|
||||
return params.Organization{}, errors.Wrap(err, "updating enterprise")
|
||||
}
|
||||
newParams, err := s.sqlToCommonOrganization(org, true)
|
||||
paramOrg, err = s.sqlToCommonOrganization(org, true)
|
||||
if err != nil {
|
||||
return params.Organization{}, errors.Wrap(err, "saving org")
|
||||
}
|
||||
return newParams, nil
|
||||
return paramOrg, nil
|
||||
}
|
||||
|
||||
func (s *sqlDatabase) GetOrganizationByID(ctx context.Context, orgID string) (params.Organization, error) {
|
||||
|
|
|
|||
|
|
@ -17,6 +17,7 @@ package sql
|
|||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/pkg/errors"
|
||||
|
|
@ -121,7 +122,7 @@ func (s *sqlDatabase) ListRepositories(_ context.Context) ([]params.Repository,
|
|||
}
|
||||
|
||||
func (s *sqlDatabase) DeleteRepository(ctx context.Context, repoID string) (err error) {
|
||||
repo, err := s.getRepoByID(ctx, s.conn, repoID)
|
||||
repo, err := s.getRepoByID(ctx, s.conn, repoID, "Endpoint", "Credentials")
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "fetching repo")
|
||||
}
|
||||
|
|
@ -131,6 +132,8 @@ func (s *sqlDatabase) DeleteRepository(ctx context.Context, repoID string) (err
|
|||
asParam, innerErr := s.sqlToCommonRepository(repo, true)
|
||||
if innerErr == nil {
|
||||
s.sendNotify(common.RepositoryEntityType, common.DeleteOperation, asParam)
|
||||
} else {
|
||||
slog.With(slog.Any("error", innerErr)).ErrorContext(ctx, "error sending delete notification", "repo", repoID)
|
||||
}
|
||||
}
|
||||
}(repo)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue