| 30 | ) |
| 31 | |
| 32 | func (s *sqlDatabase) CreateEnterprise(ctx context.Context, name string, credentials params.ForgeCredentials, webhookSecret string, poolBalancerType params.PoolBalancerType, agentMode bool) (paramEnt params.Enterprise, err error) { |
| 33 | if webhookSecret == "" { |
| 34 | return params.Enterprise{}, errors.New("creating enterprise: missing secret") |
| 35 | } |
| 36 | if credentials.ForgeType != params.GithubEndpointType { |
| 37 | return params.Enterprise{}, fmt.Errorf("enterprises are not supported on this forge type: %w", runnerErrors.ErrBadRequest) |
| 38 | } |
| 39 | |
| 40 | secret, err := util.Seal([]byte(webhookSecret), []byte(s.cfg.Passphrase)) |
| 41 | if err != nil { |
| 42 | return params.Enterprise{}, fmt.Errorf("error encoding secret: %w", err) |
| 43 | } |
| 44 | |
| 45 | defer func() { |
| 46 | if err == nil { |
| 47 | s.sendNotify(common.EnterpriseEntityType, common.CreateOperation, paramEnt) |
| 48 | } |
| 49 | }() |
| 50 | newEnterprise := Enterprise{ |
| 51 | Name: name, |
| 52 | WebhookSecret: secret, |
| 53 | PoolBalancerType: poolBalancerType, |
| 54 | AgentMode: agentMode, |
| 55 | } |
| 56 | err = s.conn.Transaction(func(tx *gorm.DB) error { |
| 57 | newEnterprise.CredentialsID = &credentials.ID |
| 58 | newEnterprise.EndpointName = &credentials.Endpoint.Name |
| 59 | |
| 60 | q := tx.Create(&newEnterprise) |
| 61 | if q.Error != nil { |
| 62 | return fmt.Errorf("error creating enterprise: %w", q.Error) |
| 63 | } |
| 64 | |
| 65 | newEnterprise, err = s.getEnterpriseByID(ctx, tx, newEnterprise.ID.String(), "Pools", "Credentials", "Endpoint", "Credentials.Endpoint") |
| 66 | if err != nil { |
| 67 | return fmt.Errorf("error creating enterprise: %w", err) |
| 68 | } |
| 69 | return nil |
| 70 | }) |
| 71 | if err != nil { |
| 72 | return params.Enterprise{}, fmt.Errorf("error creating enterprise: %w", err) |
| 73 | } |
| 74 | |
| 75 | ret, err := s.GetEnterpriseByID(ctx, newEnterprise.ID.String()) |
| 76 | if err != nil { |
| 77 | return params.Enterprise{}, fmt.Errorf("error creating enterprise: %w", err) |
| 78 | } |
| 79 | |
| 80 | return ret, nil |
| 81 | } |
| 82 | |
| 83 | func (s *sqlDatabase) GetEnterprise(ctx context.Context, name, endpointName string) (params.Enterprise, error) { |
| 84 | enterprise, err := s.getEnterprise(ctx, name, endpointName) |