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
|
|
@ -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) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue