Skip to content

Commit f0ebd31

Browse files
committed
Introduce service_factory.go
1 parent c4cd6d4 commit f0ebd31

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

pkg/schema/v1/service_factory.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package v1
2+
3+
import (
4+
"context"
5+
"database/sql"
6+
"fmt"
7+
"github.com/icinga/icinga-kubernetes/pkg/database"
8+
"k8s.io/client-go/kubernetes"
9+
10+
"github.com/icinga/icinga-go-library/types"
11+
)
12+
13+
type ServiceFactory struct {
14+
clientset *kubernetes.Clientset
15+
db *database.Database
16+
ctx context.Context
17+
}
18+
19+
func NewServiceFactory(clientset *kubernetes.Clientset, db *database.Database, ctx context.Context) *ServiceFactory {
20+
return &ServiceFactory{
21+
clientset: clientset,
22+
db: db,
23+
ctx: ctx,
24+
}
25+
}
26+
27+
// GetServiceUUID fetches the UUID of a service by its namespace and name
28+
func (f *ServiceFactory) GetServiceUUID(ctx context.Context, namespace, name string) (types.UUID, error) {
29+
var serviceUuid types.UUID
30+
31+
query := `SELECT uuid FROM service WHERE namespace = ? AND name = ?`
32+
33+
err := f.db.QueryRowContext(ctx, query, namespace, name).Scan(&serviceUuid)
34+
if err != nil {
35+
if err == sql.ErrNoRows {
36+
return types.UUID{}, fmt.Errorf("service '%s' not found in namespace '%s'", name, namespace)
37+
}
38+
return types.UUID{}, fmt.Errorf("error fetching service UUID: %w", err)
39+
}
40+
41+
return serviceUuid, nil
42+
}

0 commit comments

Comments
 (0)