2022-05-05 13:25:50 +00:00
|
|
|
// Copyright 2022 Cloudbase Solutions SRL
|
|
|
|
|
//
|
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License"); you may
|
|
|
|
|
// not use this file except in compliance with the License. You may obtain
|
|
|
|
|
// a copy of the License at
|
|
|
|
|
//
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
//
|
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
|
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
|
// License for the specific language governing permissions and limitations
|
|
|
|
|
// under the License.
|
|
|
|
|
|
2022-05-04 13:15:27 +00:00
|
|
|
package sql
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2023-06-24 00:22:51 +00:00
|
|
|
"encoding/json"
|
2025-08-16 19:31:58 +00:00
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
2024-06-19 12:40:56 +00:00
|
|
|
"log/slog"
|
2023-03-12 16:01:49 +02:00
|
|
|
|
2023-04-10 00:03:49 +00:00
|
|
|
"github.com/google/uuid"
|
2023-06-24 00:22:51 +00:00
|
|
|
"gorm.io/datatypes"
|
2022-05-04 13:15:27 +00:00
|
|
|
"gorm.io/gorm"
|
|
|
|
|
"gorm.io/gorm/clause"
|
2024-02-22 16:54:38 +01:00
|
|
|
|
|
|
|
|
runnerErrors "github.com/cloudbase/garm-provider-common/errors"
|
2024-06-19 12:19:58 +00:00
|
|
|
"github.com/cloudbase/garm/database/common"
|
2024-02-22 16:54:38 +01:00
|
|
|
"github.com/cloudbase/garm/params"
|
2022-05-04 13:15:27 +00:00
|
|
|
)
|
|
|
|
|
|
2024-06-19 12:19:58 +00:00
|
|
|
func (s *sqlDatabase) CreateInstance(_ context.Context, poolID string, param params.CreateInstanceParams) (instance params.Instance, err error) {
|
2024-03-28 10:08:19 +00:00
|
|
|
pool, err := s.getPoolByID(s.conn, poolID)
|
2022-05-04 13:15:27 +00:00
|
|
|
if err != nil {
|
2025-08-16 19:31:58 +00:00
|
|
|
return params.Instance{}, fmt.Errorf("error fetching pool: %w", err)
|
2022-05-04 13:15:27 +00:00
|
|
|
}
|
2022-12-06 19:48:00 +00:00
|
|
|
|
2024-06-19 12:19:58 +00:00
|
|
|
defer func() {
|
|
|
|
|
if err == nil {
|
|
|
|
|
s.sendNotify(common.InstanceEntityType, common.CreateOperation, instance)
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
|
2023-06-24 00:22:51 +00:00
|
|
|
var labels datatypes.JSON
|
|
|
|
|
if len(param.AditionalLabels) > 0 {
|
|
|
|
|
labels, err = json.Marshal(param.AditionalLabels)
|
|
|
|
|
if err != nil {
|
2025-08-16 19:31:58 +00:00
|
|
|
return params.Instance{}, fmt.Errorf("error marshalling labels: %w", err)
|
2023-06-24 00:22:51 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-18 06:09:44 +00:00
|
|
|
var secret []byte
|
|
|
|
|
if len(param.JitConfiguration) > 0 {
|
2023-08-19 16:31:02 +00:00
|
|
|
secret, err = s.marshalAndSeal(param.JitConfiguration)
|
2023-08-18 06:09:44 +00:00
|
|
|
if err != nil {
|
2025-08-16 19:31:58 +00:00
|
|
|
return params.Instance{}, fmt.Errorf("error marshalling jit config: %w", err)
|
2023-08-18 06:09:44 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-04 13:15:27 +00:00
|
|
|
newInstance := Instance{
|
2023-03-27 08:40:22 +00:00
|
|
|
Pool: pool,
|
|
|
|
|
Name: param.Name,
|
|
|
|
|
Status: param.Status,
|
|
|
|
|
RunnerStatus: param.RunnerStatus,
|
|
|
|
|
OSType: param.OSType,
|
|
|
|
|
OSArch: param.OSArch,
|
|
|
|
|
CallbackURL: param.CallbackURL,
|
|
|
|
|
MetadataURL: param.MetadataURL,
|
|
|
|
|
GitHubRunnerGroup: param.GitHubRunnerGroup,
|
2023-08-18 06:09:44 +00:00
|
|
|
JitConfiguration: secret,
|
2023-06-24 00:22:51 +00:00
|
|
|
AditionalLabels: labels,
|
2023-08-26 19:40:01 +00:00
|
|
|
AgentID: param.AgentID,
|
2022-05-04 13:15:27 +00:00
|
|
|
}
|
|
|
|
|
q := s.conn.Create(&newInstance)
|
|
|
|
|
if q.Error != nil {
|
2025-08-16 19:31:58 +00:00
|
|
|
return params.Instance{}, fmt.Errorf("error creating instance: %w", q.Error)
|
2022-05-04 13:15:27 +00:00
|
|
|
}
|
|
|
|
|
|
2023-08-28 08:11:44 +00:00
|
|
|
return s.sqlToParamsInstance(newInstance)
|
2022-05-04 13:15:27 +00:00
|
|
|
}
|
|
|
|
|
|
2024-03-28 10:08:19 +00:00
|
|
|
func (s *sqlDatabase) getPoolInstanceByName(poolID string, instanceName string) (Instance, error) {
|
|
|
|
|
pool, err := s.getPoolByID(s.conn, poolID)
|
2022-05-04 13:15:27 +00:00
|
|
|
if err != nil {
|
2025-08-16 19:31:58 +00:00
|
|
|
return Instance{}, fmt.Errorf("error fetching pool: %w", err)
|
2022-05-04 13:15:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var instance Instance
|
|
|
|
|
q := s.conn.Model(&Instance{}).
|
|
|
|
|
Preload(clause.Associations).
|
|
|
|
|
Where("name = ? and pool_id = ?", instanceName, pool.ID).
|
|
|
|
|
First(&instance)
|
|
|
|
|
if q.Error != nil {
|
2022-05-11 14:50:19 +00:00
|
|
|
if errors.Is(q.Error, gorm.ErrRecordNotFound) {
|
2025-08-16 19:31:58 +00:00
|
|
|
return Instance{}, fmt.Errorf("error fetching pool instance by name: %w", runnerErrors.ErrNotFound)
|
2022-05-11 14:50:19 +00:00
|
|
|
}
|
2025-08-16 19:31:58 +00:00
|
|
|
return Instance{}, fmt.Errorf("error fetching pool instance by name: %w", q.Error)
|
2022-05-04 13:15:27 +00:00
|
|
|
}
|
2025-04-11 10:42:31 +00:00
|
|
|
|
|
|
|
|
instance.Pool = pool
|
2022-05-04 13:15:27 +00:00
|
|
|
return instance, nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-22 16:54:38 +01:00
|
|
|
func (s *sqlDatabase) getInstanceByName(_ context.Context, instanceName string, preload ...string) (Instance, error) {
|
2022-05-04 13:15:27 +00:00
|
|
|
var instance Instance
|
|
|
|
|
|
|
|
|
|
q := s.conn
|
|
|
|
|
|
|
|
|
|
if len(preload) > 0 {
|
|
|
|
|
for _, item := range preload {
|
|
|
|
|
q = q.Preload(item)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
q = q.Model(&Instance{}).
|
|
|
|
|
Preload(clause.Associations).
|
|
|
|
|
Where("name = ?", instanceName).
|
|
|
|
|
First(&instance)
|
|
|
|
|
if q.Error != nil {
|
2022-05-11 14:50:19 +00:00
|
|
|
if errors.Is(q.Error, gorm.ErrRecordNotFound) {
|
2025-08-16 19:31:58 +00:00
|
|
|
return Instance{}, fmt.Errorf("error fetching instance by name: %w", runnerErrors.ErrNotFound)
|
2022-05-11 14:50:19 +00:00
|
|
|
}
|
2025-08-16 19:31:58 +00:00
|
|
|
return Instance{}, fmt.Errorf("error fetching instance by name: %w", q.Error)
|
2022-05-04 13:15:27 +00:00
|
|
|
}
|
|
|
|
|
return instance, nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-28 18:23:49 +00:00
|
|
|
func (s *sqlDatabase) GetPoolInstanceByName(_ context.Context, poolID string, instanceName string) (params.Instance, error) {
|
2024-03-28 10:08:19 +00:00
|
|
|
instance, err := s.getPoolInstanceByName(poolID, instanceName)
|
2022-05-04 13:15:27 +00:00
|
|
|
if err != nil {
|
2025-08-16 19:31:58 +00:00
|
|
|
return params.Instance{}, fmt.Errorf("error fetching instance: %w", err)
|
2022-05-04 13:15:27 +00:00
|
|
|
}
|
2022-12-01 18:00:22 +02:00
|
|
|
|
2023-08-28 08:11:44 +00:00
|
|
|
return s.sqlToParamsInstance(instance)
|
2022-05-04 13:15:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *sqlDatabase) GetInstanceByName(ctx context.Context, instanceName string) (params.Instance, error) {
|
2025-04-17 15:13:24 +00:00
|
|
|
instance, err := s.getInstanceByName(ctx, instanceName, "StatusMessages", "Pool", "ScaleSet")
|
2022-05-04 13:15:27 +00:00
|
|
|
if err != nil {
|
2025-08-16 19:31:58 +00:00
|
|
|
return params.Instance{}, fmt.Errorf("error fetching instance: %w", err)
|
2022-05-04 13:15:27 +00:00
|
|
|
}
|
2022-12-01 18:00:22 +02:00
|
|
|
|
2023-08-28 08:11:44 +00:00
|
|
|
return s.sqlToParamsInstance(instance)
|
2022-05-04 13:15:27 +00:00
|
|
|
}
|
|
|
|
|
|
2024-06-19 12:19:58 +00:00
|
|
|
func (s *sqlDatabase) DeleteInstance(_ context.Context, poolID string, instanceName string) (err error) {
|
2024-03-28 10:08:19 +00:00
|
|
|
instance, err := s.getPoolInstanceByName(poolID, instanceName)
|
2022-05-04 13:15:27 +00:00
|
|
|
if err != nil {
|
2025-04-27 21:03:37 +00:00
|
|
|
if errors.Is(err, runnerErrors.ErrNotFound) {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2025-08-16 19:31:58 +00:00
|
|
|
return fmt.Errorf("error deleting instance: %w", err)
|
2022-05-04 13:15:27 +00:00
|
|
|
}
|
2024-06-19 12:19:58 +00:00
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
|
if err == nil {
|
|
|
|
|
var providerID string
|
|
|
|
|
if instance.ProviderID != nil {
|
|
|
|
|
providerID = *instance.ProviderID
|
|
|
|
|
}
|
2025-04-27 20:42:42 +00:00
|
|
|
instanceNotif := params.Instance{
|
2024-06-19 12:19:58 +00:00
|
|
|
ID: instance.ID.String(),
|
|
|
|
|
Name: instance.Name,
|
|
|
|
|
ProviderID: providerID,
|
|
|
|
|
AgentID: instance.AgentID,
|
2025-04-27 20:42:42 +00:00
|
|
|
}
|
|
|
|
|
switch {
|
|
|
|
|
case instance.PoolID != nil:
|
|
|
|
|
instanceNotif.PoolID = instance.PoolID.String()
|
|
|
|
|
case instance.ScaleSetFkID != nil:
|
|
|
|
|
instanceNotif.ScaleSetID = *instance.ScaleSetFkID
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if notifyErr := s.sendNotify(common.InstanceEntityType, common.DeleteOperation, instanceNotif); notifyErr != nil {
|
2024-06-19 12:40:56 +00:00
|
|
|
slog.With(slog.Any("error", notifyErr)).Error("failed to send notify")
|
|
|
|
|
}
|
2024-06-19 12:19:58 +00:00
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
|
2022-05-04 13:15:27 +00:00
|
|
|
if q := s.conn.Unscoped().Delete(&instance); q.Error != nil {
|
|
|
|
|
if errors.Is(q.Error, gorm.ErrRecordNotFound) {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2025-08-16 19:31:58 +00:00
|
|
|
return fmt.Errorf("error deleting instance: %w", q.Error)
|
2022-05-04 13:15:27 +00:00
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-20 17:39:52 +00:00
|
|
|
func (s *sqlDatabase) DeleteInstanceByName(ctx context.Context, instanceName string) error {
|
2025-05-02 17:38:29 +00:00
|
|
|
instance, err := s.getInstanceByName(ctx, instanceName, "Pool", "ScaleSet")
|
2025-04-20 17:39:52 +00:00
|
|
|
if err != nil {
|
2025-04-27 21:03:37 +00:00
|
|
|
if errors.Is(err, runnerErrors.ErrNotFound) {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2025-08-16 19:31:58 +00:00
|
|
|
return fmt.Errorf("error deleting instance: %w", err)
|
2025-04-20 17:39:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
|
if err == nil {
|
|
|
|
|
var providerID string
|
|
|
|
|
if instance.ProviderID != nil {
|
|
|
|
|
providerID = *instance.ProviderID
|
|
|
|
|
}
|
2025-04-24 23:29:40 +00:00
|
|
|
payload := params.Instance{
|
2025-04-20 17:39:52 +00:00
|
|
|
ID: instance.ID.String(),
|
|
|
|
|
Name: instance.Name,
|
|
|
|
|
ProviderID: providerID,
|
|
|
|
|
AgentID: instance.AgentID,
|
2025-04-24 23:29:40 +00:00
|
|
|
}
|
|
|
|
|
if instance.PoolID != nil {
|
|
|
|
|
payload.PoolID = instance.PoolID.String()
|
|
|
|
|
}
|
|
|
|
|
if instance.ScaleSetFkID != nil {
|
|
|
|
|
payload.ScaleSetID = *instance.ScaleSetFkID
|
|
|
|
|
}
|
|
|
|
|
if notifyErr := s.sendNotify(common.InstanceEntityType, common.DeleteOperation, payload); notifyErr != nil {
|
2025-04-20 17:39:52 +00:00
|
|
|
slog.With(slog.Any("error", notifyErr)).Error("failed to send notify")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}()
|
|
|
|
|
|
|
|
|
|
if q := s.conn.Unscoped().Delete(&instance); q.Error != nil {
|
|
|
|
|
if errors.Is(q.Error, gorm.ErrRecordNotFound) {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2025-08-16 19:31:58 +00:00
|
|
|
return fmt.Errorf("error deleting instance: %w", q.Error)
|
2025-04-20 17:39:52 +00:00
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-30 18:22:06 +00:00
|
|
|
func (s *sqlDatabase) AddInstanceEvent(ctx context.Context, instanceName string, event params.EventType, eventLevel params.EventLevel, statusMessage string) error {
|
|
|
|
|
instance, err := s.getInstanceByName(ctx, instanceName)
|
2022-05-04 13:15:27 +00:00
|
|
|
if err != nil {
|
2025-08-16 19:31:58 +00:00
|
|
|
return fmt.Errorf("error updating instance: %w", err)
|
2022-05-04 13:15:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
msg := InstanceStatusUpdate{
|
2022-12-29 16:49:50 +00:00
|
|
|
Message: statusMessage,
|
|
|
|
|
EventType: event,
|
|
|
|
|
EventLevel: eventLevel,
|
2022-05-04 13:15:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err := s.conn.Model(&instance).Association("StatusMessages").Append(&msg); err != nil {
|
2025-08-16 19:31:58 +00:00
|
|
|
return fmt.Errorf("error adding status message: %w", err)
|
2022-05-04 13:15:27 +00:00
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-30 18:22:06 +00:00
|
|
|
func (s *sqlDatabase) UpdateInstance(ctx context.Context, instanceName string, param params.UpdateInstanceParams) (params.Instance, error) {
|
2025-04-17 15:13:24 +00:00
|
|
|
instance, err := s.getInstanceByName(ctx, instanceName, "Pool", "ScaleSet")
|
2022-05-04 13:15:27 +00:00
|
|
|
if err != nil {
|
2025-08-16 19:31:58 +00:00
|
|
|
return params.Instance{}, fmt.Errorf("error updating instance: %w", err)
|
2022-05-04 13:15:27 +00:00
|
|
|
}
|
|
|
|
|
|
2022-06-29 16:23:01 +00:00
|
|
|
if param.AgentID != 0 {
|
|
|
|
|
instance.AgentID = param.AgentID
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-04 13:15:27 +00:00
|
|
|
if param.ProviderID != "" {
|
|
|
|
|
instance.ProviderID = ¶m.ProviderID
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if param.OSName != "" {
|
|
|
|
|
instance.OSName = param.OSName
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if param.OSVersion != "" {
|
|
|
|
|
instance.OSVersion = param.OSVersion
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if string(param.RunnerStatus) != "" {
|
|
|
|
|
instance.RunnerStatus = param.RunnerStatus
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if string(param.Status) != "" {
|
|
|
|
|
instance.Status = param.Status
|
|
|
|
|
}
|
2022-05-10 12:28:39 +00:00
|
|
|
if param.CreateAttempt != 0 {
|
|
|
|
|
instance.CreateAttempt = param.CreateAttempt
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-29 22:57:10 +00:00
|
|
|
if param.TokenFetched != nil {
|
|
|
|
|
instance.TokenFetched = *param.TokenFetched
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-19 16:31:02 +00:00
|
|
|
if param.JitConfiguration != nil {
|
|
|
|
|
secret, err := s.marshalAndSeal(param.JitConfiguration)
|
|
|
|
|
if err != nil {
|
2025-08-16 19:31:58 +00:00
|
|
|
return params.Instance{}, fmt.Errorf("error marshalling jit config: %w", err)
|
2023-08-19 16:31:02 +00:00
|
|
|
}
|
|
|
|
|
instance.JitConfiguration = secret
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-10 12:28:39 +00:00
|
|
|
instance.ProviderFault = param.ProviderFault
|
2022-05-04 13:15:27 +00:00
|
|
|
|
|
|
|
|
q := s.conn.Save(&instance)
|
|
|
|
|
if q.Error != nil {
|
2025-08-16 19:31:58 +00:00
|
|
|
return params.Instance{}, fmt.Errorf("error updating instance: %w", q.Error)
|
2022-05-04 13:15:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(param.Addresses) > 0 {
|
|
|
|
|
addrs := []Address{}
|
|
|
|
|
for _, addr := range param.Addresses {
|
|
|
|
|
addrs = append(addrs, Address{
|
|
|
|
|
Address: addr.Address,
|
|
|
|
|
Type: string(addr.Type),
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
if err := s.conn.Model(&instance).Association("Addresses").Replace(addrs); err != nil {
|
2025-08-16 19:31:58 +00:00
|
|
|
return params.Instance{}, fmt.Errorf("error updating addresses: %w", err)
|
2022-05-04 13:15:27 +00:00
|
|
|
}
|
|
|
|
|
}
|
2024-06-19 12:19:58 +00:00
|
|
|
inst, err := s.sqlToParamsInstance(instance)
|
|
|
|
|
if err != nil {
|
2025-08-16 19:31:58 +00:00
|
|
|
return params.Instance{}, fmt.Errorf("error converting instance: %w", err)
|
2024-06-19 12:19:58 +00:00
|
|
|
}
|
|
|
|
|
s.sendNotify(common.InstanceEntityType, common.UpdateOperation, inst)
|
|
|
|
|
return inst, nil
|
2022-05-04 13:15:27 +00:00
|
|
|
}
|
|
|
|
|
|
2024-02-22 16:54:38 +01:00
|
|
|
func (s *sqlDatabase) ListPoolInstances(_ context.Context, poolID string) ([]params.Instance, error) {
|
2023-04-10 00:03:49 +00:00
|
|
|
u, err := uuid.Parse(poolID)
|
2022-05-04 13:15:27 +00:00
|
|
|
if err != nil {
|
2025-08-16 19:31:58 +00:00
|
|
|
return nil, fmt.Errorf("error parsing id: %w", runnerErrors.ErrBadRequest)
|
2022-05-04 13:15:27 +00:00
|
|
|
}
|
|
|
|
|
|
2022-12-29 16:49:50 +00:00
|
|
|
var instances []Instance
|
2025-08-12 09:15:58 +00:00
|
|
|
query := s.conn.
|
|
|
|
|
Preload("Pool").
|
|
|
|
|
Preload("Job").
|
|
|
|
|
Where("pool_id = ?", u)
|
2022-12-29 16:49:50 +00:00
|
|
|
|
|
|
|
|
if err := query.Find(&instances); err.Error != nil {
|
2025-08-16 19:31:58 +00:00
|
|
|
return nil, fmt.Errorf("error fetching instances: %w", err.Error)
|
2022-12-29 16:49:50 +00:00
|
|
|
}
|
|
|
|
|
|
2025-04-08 09:15:54 +00:00
|
|
|
ret := make([]params.Instance, len(instances))
|
|
|
|
|
for idx, inst := range instances {
|
|
|
|
|
ret[idx], err = s.sqlToParamsInstance(inst)
|
2023-08-28 08:11:44 +00:00
|
|
|
if err != nil {
|
2025-08-16 19:31:58 +00:00
|
|
|
return nil, fmt.Errorf("error converting instance: %w", err)
|
2023-08-28 08:11:44 +00:00
|
|
|
}
|
2022-05-04 13:15:27 +00:00
|
|
|
}
|
|
|
|
|
return ret, nil
|
|
|
|
|
}
|
2022-05-05 13:07:06 +00:00
|
|
|
|
2024-02-22 16:54:38 +01:00
|
|
|
func (s *sqlDatabase) ListAllInstances(_ context.Context) ([]params.Instance, error) {
|
2022-05-05 13:07:06 +00:00
|
|
|
var instances []Instance
|
|
|
|
|
|
2025-08-12 09:15:58 +00:00
|
|
|
q := s.conn.
|
|
|
|
|
Preload("Pool").
|
|
|
|
|
Preload("ScaleSet").
|
|
|
|
|
Preload("Job").
|
|
|
|
|
Find(&instances)
|
2022-05-05 13:07:06 +00:00
|
|
|
if q.Error != nil {
|
2025-08-16 19:31:58 +00:00
|
|
|
return nil, fmt.Errorf("error fetching instances: %w", q.Error)
|
2022-05-05 13:07:06 +00:00
|
|
|
}
|
|
|
|
|
ret := make([]params.Instance, len(instances))
|
2023-08-28 08:11:44 +00:00
|
|
|
var err error
|
2022-05-05 13:07:06 +00:00
|
|
|
for idx, instance := range instances {
|
2023-08-28 08:11:44 +00:00
|
|
|
ret[idx], err = s.sqlToParamsInstance(instance)
|
|
|
|
|
if err != nil {
|
2025-08-16 19:31:58 +00:00
|
|
|
return nil, fmt.Errorf("error converting instance: %w", err)
|
2023-08-28 08:11:44 +00:00
|
|
|
}
|
2022-05-05 13:07:06 +00:00
|
|
|
}
|
|
|
|
|
return ret, nil
|
|
|
|
|
}
|
2022-05-13 23:34:16 +00:00
|
|
|
|
2024-03-28 18:23:49 +00:00
|
|
|
func (s *sqlDatabase) PoolInstanceCount(_ context.Context, poolID string) (int64, error) {
|
2024-03-28 10:08:19 +00:00
|
|
|
pool, err := s.getPoolByID(s.conn, poolID)
|
2022-05-13 23:34:16 +00:00
|
|
|
if err != nil {
|
2025-08-16 19:31:58 +00:00
|
|
|
return 0, fmt.Errorf("error fetching pool: %w", err)
|
2022-05-13 23:34:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var cnt int64
|
|
|
|
|
q := s.conn.Model(&Instance{}).Where("pool_id = ?", pool.ID).Count(&cnt)
|
|
|
|
|
if q.Error != nil {
|
2025-08-16 19:31:58 +00:00
|
|
|
return 0, fmt.Errorf("error fetching instance count: %w", q.Error)
|
2022-05-13 23:34:16 +00:00
|
|
|
}
|
|
|
|
|
return cnt, nil
|
|
|
|
|
}
|