Skip to content

Commit 6a168ba

Browse files
Enable orgs
Signed-off-by: Gabriel Adrian Samfira <[email protected]>
1 parent 5dfcfc5 commit 6a168ba

File tree

22 files changed

+125
-77
lines changed

22 files changed

+125
-77
lines changed

cmd/garm-cli/cmd/organization.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ var orgAddCmd = &cobra.Command{
167167
Name: orgName,
168168
WebhookSecret: orgWebhookSecret,
169169
CredentialsName: orgCreds,
170+
ForgeType: params.EndpointType(forgeType),
170171
PoolBalancerType: params.PoolBalancerType(poolBalancerType),
171172
}
172173
response, err := apiCli.Organizations.CreateOrg(newOrgReq, authToken)
@@ -306,6 +307,7 @@ func init() {
306307
orgAddCmd.Flags().StringVar(&orgName, "name", "", "The name of the organization")
307308
orgAddCmd.Flags().StringVar(&poolBalancerType, "pool-balancer-type", string(params.PoolBalancerTypeRoundRobin), "The balancing strategy to use when creating runners in pools matching requested labels.")
308309
orgAddCmd.Flags().StringVar(&orgWebhookSecret, "webhook-secret", "", "The webhook secret for this organization")
310+
orgAddCmd.Flags().StringVar(&forgeType, "forge-type", string(params.GithubEndpointType), "The forge type of the organization. Supported values: github, gitea.")
309311
orgAddCmd.Flags().StringVar(&orgCreds, "credentials", "", "Credentials name. See credentials list.")
310312
orgAddCmd.Flags().BoolVar(&orgRandomWebhookSecret, "random-webhook-secret", false, "Generate a random webhook secret for this organization.")
311313
orgAddCmd.Flags().BoolVar(&installOrgWebhook, "install-webhook", false, "Install the webhook as part of the add operation.")
@@ -347,13 +349,17 @@ func formatOrganizations(orgs []params.Organization) {
347349
return
348350
}
349351
t := table.NewWriter()
350-
header := table.Row{"ID", "Name", "Endpoint", "Credentials name", "Pool Balancer Type", "Pool mgr running"}
352+
header := table.Row{"ID", "Name", "Endpoint", "Credentials name", "Pool Balancer Type", "Forge type", "Pool mgr running"}
351353
if long {
352354
header = append(header, "Created At", "Updated At")
353355
}
354356
t.AppendHeader(header)
355357
for _, val := range orgs {
356-
row := table.Row{val.ID, val.Name, val.Endpoint.Name, val.CredentialsName, val.GetBalancerType(), val.PoolManagerStatus.IsRunning}
358+
forgeType := val.Endpoint.EndpointType
359+
if forgeType == "" {
360+
forgeType = params.GithubEndpointType
361+
}
362+
row := table.Row{val.ID, val.Name, val.Endpoint.Name, val.CredentialsName, val.GetBalancerType(), forgeType, val.PoolManagerStatus.IsRunning}
357363
if long {
358364
row = append(row, val.CreatedAt, val.UpdatedAt)
359365
}

cmd/garm/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,10 +240,10 @@ func main() {
240240

241241
cacheWorker := cache.NewWorker(ctx, db)
242242
if err != nil {
243-
log.Fatalf("failed to create credentials worker: %+v", err)
243+
log.Fatalf("failed to create cache worker: %+v", err)
244244
}
245245
if err := cacheWorker.Start(); err != nil {
246-
log.Fatalf("failed to start credentials worker: %+v", err)
246+
log.Fatalf("failed to start cache worker: %+v", err)
247247
}
248248

249249
providers, err := providers.LoadProvidersFromConfig(ctx, *cfg, controllerInfo.ControllerID.String())

database/common/mocks/Store.go

Lines changed: 9 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

database/common/store.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ type RepoStore interface {
4747
}
4848

4949
type OrgStore interface {
50-
CreateOrganization(ctx context.Context, name, credentialsName, webhookSecret string, poolBalancerType params.PoolBalancerType) (params.Organization, error)
50+
CreateOrganization(ctx context.Context, name string, credentials params.ForgeCredentials, webhookSecret string, poolBalancerType params.PoolBalancerType) (org params.Organization, err error)
5151
GetOrganization(ctx context.Context, name, endpointName string) (params.Organization, error)
5252
GetOrganizationByID(ctx context.Context, orgID string) (params.Organization, error)
5353
ListOrganizations(ctx context.Context) ([]params.Organization, error)

database/sql/gitea.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,11 @@ func (s *sqlDatabase) getGiteaCredentialsByName(ctx context.Context, tx *gorm.DB
268268
if detailed {
269269
q = q.
270270
Preload("Repositories").
271-
Preload("Organizations")
271+
Preload("Organizations").
272+
Preload("Repositories.GiteaCredentials").
273+
Preload("Organizations.GiteaCredentials").
274+
Preload("Repositories.Credentials").
275+
Preload("Organizations.Credentials")
272276
}
273277

274278
userID, err := getUIDFromContext(ctx)
@@ -304,7 +308,11 @@ func (s *sqlDatabase) GetGiteaCredentials(ctx context.Context, id uint, detailed
304308
if detailed {
305309
q = q.
306310
Preload("Repositories").
307-
Preload("Organizations")
311+
Preload("Organizations").
312+
Preload("Repositories.GiteaCredentials").
313+
Preload("Organizations.GiteaCredentials").
314+
Preload("Repositories.Credentials").
315+
Preload("Organizations.Credentials")
308316
}
309317

310318
if !auth.IsAdmin(ctx) {

database/sql/github_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,7 @@ func (s *GithubTestSuite) TestDeleteCredentialsFailsIfReposOrgsOrEntitiesUseIt()
544544
err = s.db.DeleteRepository(ctx, repo.ID)
545545
s.Require().NoError(err)
546546

547-
org, err := s.db.CreateOrganization(ctx, "test-org", creds.Name, "superSecret@123BlaBla", params.PoolBalancerTypeRoundRobin)
547+
org, err := s.db.CreateOrganization(ctx, "test-org", creds, "superSecret@123BlaBla", params.PoolBalancerTypeRoundRobin)
548548
s.Require().NoError(err)
549549
s.Require().NotNil(org)
550550

database/sql/instances_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func (s *InstancesTestSuite) SetupTest() {
8484
creds := garmTesting.CreateTestGithubCredentials(adminCtx, "new-creds", db, s.T(), githubEndpoint)
8585

8686
// create an organization for testing purposes
87-
org, err := s.Store.CreateOrganization(s.adminCtx, "test-org", creds.Name, "test-webhookSecret", params.PoolBalancerTypeRoundRobin)
87+
org, err := s.Store.CreateOrganization(s.adminCtx, "test-org", creds, "test-webhookSecret", params.PoolBalancerTypeRoundRobin)
8888
if err != nil {
8989
s.FailNow(fmt.Sprintf("failed to create org: %s", err))
9090
}

database/sql/organizations.go

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import (
2929
"github.com/cloudbase/garm/params"
3030
)
3131

32-
func (s *sqlDatabase) CreateOrganization(ctx context.Context, name, credentialsName, webhookSecret string, poolBalancerType params.PoolBalancerType) (org params.Organization, err error) {
32+
func (s *sqlDatabase) CreateOrganization(ctx context.Context, name string, credentials params.ForgeCredentials, webhookSecret string, poolBalancerType params.PoolBalancerType) (param params.Organization, err error) {
3333
if webhookSecret == "" {
3434
return params.Organization{}, errors.New("creating org: missing secret")
3535
}
@@ -40,7 +40,7 @@ func (s *sqlDatabase) CreateOrganization(ctx context.Context, name, credentialsN
4040

4141
defer func() {
4242
if err == nil {
43-
s.sendNotify(common.OrganizationEntityType, common.CreateOperation, org)
43+
s.sendNotify(common.OrganizationEntityType, common.CreateOperation, param)
4444
}
4545
}()
4646
newOrg := Organization{
@@ -50,37 +50,37 @@ func (s *sqlDatabase) CreateOrganization(ctx context.Context, name, credentialsN
5050
}
5151

5252
err = s.conn.Transaction(func(tx *gorm.DB) error {
53-
creds, err := s.getGithubCredentialsByName(ctx, tx, credentialsName, false)
54-
if err != nil {
55-
return errors.Wrap(err, "creating org")
56-
}
57-
if creds.EndpointName == nil {
58-
return errors.Wrap(runnerErrors.ErrUnprocessable, "credentials have no endpoint")
53+
switch credentials.ForgeType {
54+
case params.GithubEndpointType:
55+
newOrg.CredentialsID = &credentials.ID
56+
case params.GiteaEndpointType:
57+
newOrg.GiteaCredentialsID = &credentials.ID
58+
default:
59+
return errors.Wrap(runnerErrors.ErrBadRequest, "unsupported credentials type")
5960
}
60-
newOrg.CredentialsID = &creds.ID
61-
newOrg.EndpointName = creds.EndpointName
6261

62+
newOrg.EndpointName = &credentials.Endpoint.Name
6363
q := tx.Create(&newOrg)
6464
if q.Error != nil {
6565
return errors.Wrap(q.Error, "creating org")
6666
}
67-
68-
newOrg.Credentials = creds
69-
newOrg.Endpoint = creds.Endpoint
70-
7167
return nil
7268
})
7369
if err != nil {
7470
return params.Organization{}, errors.Wrap(err, "creating org")
7571
}
7672

77-
org, err = s.sqlToCommonOrganization(newOrg, true)
73+
org, err := s.getOrgByID(ctx, s.conn, newOrg.ID.String(), "Pools", "Endpoint", "Credentials", "GiteaCredentials", "Credentials.Endpoint", "GiteaCredentials.Endpoint")
7874
if err != nil {
7975
return params.Organization{}, errors.Wrap(err, "creating org")
8076
}
81-
org.WebhookSecret = webhookSecret
8277

83-
return org, nil
78+
param, err = s.sqlToCommonOrganization(org, true)
79+
if err != nil {
80+
return params.Organization{}, errors.Wrap(err, "creating org")
81+
}
82+
83+
return param, nil
8484
}
8585

8686
func (s *sqlDatabase) GetOrganization(ctx context.Context, name, endpointName string) (params.Organization, error) {
@@ -101,7 +101,9 @@ func (s *sqlDatabase) ListOrganizations(_ context.Context) ([]params.Organizatio
101101
var orgs []Organization
102102
q := s.conn.
103103
Preload("Credentials").
104+
Preload("GiteaCredentials").
104105
Preload("Credentials.Endpoint").
106+
Preload("GiteaCredentials.Endpoint").
105107
Preload("Endpoint").
106108
Find(&orgs)
107109
if q.Error != nil {
@@ -121,7 +123,7 @@ func (s *sqlDatabase) ListOrganizations(_ context.Context) ([]params.Organizatio
121123
}
122124

123125
func (s *sqlDatabase) DeleteOrganization(ctx context.Context, orgID string) (err error) {
124-
org, err := s.getOrgByID(ctx, s.conn, orgID, "Endpoint", "Credentials", "Credentials.Endpoint")
126+
org, err := s.getOrgByID(ctx, s.conn, orgID, "Endpoint", "Credentials", "Credentials.Endpoint", "GiteaCredentials", "GiteaCredentials.Endpoint")
125127
if err != nil {
126128
return errors.Wrap(err, "fetching org")
127129
}
@@ -201,7 +203,7 @@ func (s *sqlDatabase) UpdateOrganization(ctx context.Context, orgID string, para
201203
return params.Organization{}, errors.Wrap(err, "saving org")
202204
}
203205

204-
org, err = s.getOrgByID(ctx, s.conn, orgID, "Endpoint", "Credentials", "Credentials.Endpoint")
206+
org, err = s.getOrgByID(ctx, s.conn, orgID, "Endpoint", "Credentials", "Credentials.Endpoint", "GiteaCredentials", "GiteaCredentials.Endpoint")
205207
if err != nil {
206208
return params.Organization{}, errors.Wrap(err, "updating enterprise")
207209
}
@@ -213,7 +215,7 @@ func (s *sqlDatabase) UpdateOrganization(ctx context.Context, orgID string, para
213215
}
214216

215217
func (s *sqlDatabase) GetOrganizationByID(ctx context.Context, orgID string) (params.Organization, error) {
216-
org, err := s.getOrgByID(ctx, s.conn, orgID, "Pools", "Credentials", "Endpoint", "Credentials.Endpoint")
218+
org, err := s.getOrgByID(ctx, s.conn, orgID, "Pools", "Credentials", "Endpoint", "Credentials.Endpoint", "GiteaCredentials", "GiteaCredentials.Endpoint")
217219
if err != nil {
218220
return params.Organization{}, errors.Wrap(err, "fetching org")
219221
}
@@ -254,7 +256,9 @@ func (s *sqlDatabase) getOrg(_ context.Context, name, endpointName string) (Orga
254256

255257
q := s.conn.Where("name = ? COLLATE NOCASE and endpoint_name = ? COLLATE NOCASE", name, endpointName).
256258
Preload("Credentials").
259+
Preload("GiteaCredentials").
257260
Preload("Credentials.Endpoint").
261+
Preload("GiteaCredentials.Endpoint").
258262
Preload("Endpoint").
259263
First(&org)
260264
if q.Error != nil {

database/sql/organizations_test.go

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ func (s *OrgTestSuite) SetupTest() {
100100
org, err := db.CreateOrganization(
101101
s.adminCtx,
102102
fmt.Sprintf("test-org-%d", i),
103-
s.testCreds.Name,
103+
s.testCreds,
104104
fmt.Sprintf("test-webhook-secret-%d", i),
105105
params.PoolBalancerTypeRoundRobin,
106106
)
@@ -179,7 +179,7 @@ func (s *OrgTestSuite) TestCreateOrganization() {
179179
org, err := s.Store.CreateOrganization(
180180
s.adminCtx,
181181
s.Fixtures.CreateOrgParams.Name,
182-
s.Fixtures.CreateOrgParams.CredentialsName,
182+
s.testCreds,
183183
s.Fixtures.CreateOrgParams.WebhookSecret,
184184
params.PoolBalancerTypeRoundRobin)
185185

@@ -210,7 +210,7 @@ func (s *OrgTestSuite) TestCreateOrganizationInvalidDBPassphrase() {
210210
_, err = sqlDB.CreateOrganization(
211211
s.adminCtx,
212212
s.Fixtures.CreateOrgParams.Name,
213-
s.Fixtures.CreateOrgParams.CredentialsName,
213+
s.testCreds,
214214
s.Fixtures.CreateOrgParams.WebhookSecret,
215215
params.PoolBalancerTypeRoundRobin)
216216

@@ -220,15 +220,6 @@ func (s *OrgTestSuite) TestCreateOrganizationInvalidDBPassphrase() {
220220

221221
func (s *OrgTestSuite) TestCreateOrganizationDBCreateErr() {
222222
s.Fixtures.SQLMock.ExpectBegin()
223-
s.Fixtures.SQLMock.
224-
ExpectQuery(regexp.QuoteMeta("SELECT * FROM `github_credentials` WHERE user_id = ? AND name = ? AND `github_credentials`.`deleted_at` IS NULL ORDER BY `github_credentials`.`id` LIMIT ?")).
225-
WithArgs(s.adminUserID, s.Fixtures.Orgs[0].CredentialsName, 1).
226-
WillReturnRows(sqlmock.NewRows([]string{"id", "endpoint_name"}).
227-
AddRow(s.testCreds.ID, s.githubEndpoint.Name))
228-
s.Fixtures.SQLMock.ExpectQuery(regexp.QuoteMeta("SELECT * FROM `github_endpoints` WHERE `github_endpoints`.`name` = ? AND `github_endpoints`.`deleted_at` IS NULL")).
229-
WithArgs(s.testCreds.Endpoint.Name).
230-
WillReturnRows(sqlmock.NewRows([]string{"name"}).
231-
AddRow(s.githubEndpoint.Name))
232223
s.Fixtures.SQLMock.
233224
ExpectExec(regexp.QuoteMeta("INSERT INTO `organizations`")).
234225
WillReturnError(fmt.Errorf("creating org mock error"))
@@ -237,7 +228,7 @@ func (s *OrgTestSuite) TestCreateOrganizationDBCreateErr() {
237228
_, err := s.StoreSQLMocked.CreateOrganization(
238229
s.adminCtx,
239230
s.Fixtures.CreateOrgParams.Name,
240-
s.Fixtures.CreateOrgParams.CredentialsName,
231+
s.testCreds,
241232
s.Fixtures.CreateOrgParams.WebhookSecret,
242233
params.PoolBalancerTypeRoundRobin)
243234

database/sql/pools_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func (s *PoolsTestSuite) SetupTest() {
8181
creds := garmTesting.CreateTestGithubCredentials(adminCtx, "new-creds", db, s.T(), githubEndpoint)
8282

8383
// create an organization for testing purposes
84-
org, err := s.Store.CreateOrganization(s.adminCtx, "test-org", creds.Name, "test-webhookSecret", params.PoolBalancerTypeRoundRobin)
84+
org, err := s.Store.CreateOrganization(s.adminCtx, "test-org", creds, "test-webhookSecret", params.PoolBalancerTypeRoundRobin)
8585
if err != nil {
8686
s.FailNow(fmt.Sprintf("failed to create org: %s", err))
8787
}

0 commit comments

Comments
 (0)