2024-02-28 09:54:50 +05:30
|
|
|
package integrations
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
|
|
|
|
|
2025-03-20 21:01:41 +05:30
|
|
|
"github.com/SigNoz/signoz/pkg/query-service/model"
|
2025-04-15 21:05:36 +05:30
|
|
|
"github.com/SigNoz/signoz/pkg/sqlstore"
|
|
|
|
|
"github.com/SigNoz/signoz/pkg/types"
|
|
|
|
|
"github.com/SigNoz/signoz/pkg/valuer"
|
|
|
|
|
"github.com/uptrace/bun"
|
2024-02-28 09:54:50 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type InstalledIntegrationsSqliteRepo struct {
|
2025-04-15 21:05:36 +05:30
|
|
|
store sqlstore.SQLStore
|
2024-02-28 09:54:50 +05:30
|
|
|
}
|
|
|
|
|
|
2025-04-15 21:05:36 +05:30
|
|
|
func NewInstalledIntegrationsSqliteRepo(store sqlstore.SQLStore) (
|
2024-02-28 09:54:50 +05:30
|
|
|
*InstalledIntegrationsSqliteRepo, error,
|
|
|
|
|
) {
|
|
|
|
|
return &InstalledIntegrationsSqliteRepo{
|
2025-04-15 21:05:36 +05:30
|
|
|
store: store,
|
2024-02-28 09:54:50 +05:30
|
|
|
}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *InstalledIntegrationsSqliteRepo) list(
|
|
|
|
|
ctx context.Context,
|
2025-04-15 21:05:36 +05:30
|
|
|
orgId string,
|
|
|
|
|
) ([]types.InstalledIntegration, *model.ApiError) {
|
|
|
|
|
integrations := []types.InstalledIntegration{}
|
|
|
|
|
|
|
|
|
|
err := r.store.BunDB().NewSelect().
|
|
|
|
|
Model(&integrations).
|
|
|
|
|
Where("org_id = ?", orgId).
|
|
|
|
|
Order("installed_at").
|
|
|
|
|
Scan(ctx)
|
2024-02-28 09:54:50 +05:30
|
|
|
if err != nil {
|
|
|
|
|
return nil, model.InternalError(fmt.Errorf(
|
|
|
|
|
"could not query installed integrations: %w", err,
|
|
|
|
|
))
|
|
|
|
|
}
|
|
|
|
|
return integrations, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *InstalledIntegrationsSqliteRepo) get(
|
2025-04-15 21:05:36 +05:30
|
|
|
ctx context.Context, orgId string, integrationTypes []string,
|
|
|
|
|
) (map[string]types.InstalledIntegration, *model.ApiError) {
|
|
|
|
|
integrations := []types.InstalledIntegration{}
|
|
|
|
|
|
|
|
|
|
typeValues := []interface{}{}
|
|
|
|
|
for _, integrationType := range integrationTypes {
|
|
|
|
|
typeValues = append(typeValues, integrationType)
|
2024-02-28 09:54:50 +05:30
|
|
|
}
|
|
|
|
|
|
2025-04-15 21:05:36 +05:30
|
|
|
err := r.store.BunDB().NewSelect().Model(&integrations).
|
|
|
|
|
Where("org_id = ?", orgId).
|
|
|
|
|
Where("type IN (?)", bun.In(typeValues)).
|
|
|
|
|
Scan(ctx)
|
2024-02-28 09:54:50 +05:30
|
|
|
if err != nil {
|
|
|
|
|
return nil, model.InternalError(fmt.Errorf(
|
|
|
|
|
"could not query installed integrations: %w", err,
|
|
|
|
|
))
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-15 21:05:36 +05:30
|
|
|
result := map[string]types.InstalledIntegration{}
|
2024-02-28 09:54:50 +05:30
|
|
|
for _, ii := range integrations {
|
2025-04-15 21:05:36 +05:30
|
|
|
result[ii.Type] = ii
|
2024-02-28 09:54:50 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *InstalledIntegrationsSqliteRepo) upsert(
|
|
|
|
|
ctx context.Context,
|
2025-04-15 21:05:36 +05:30
|
|
|
orgId string,
|
|
|
|
|
integrationType string,
|
|
|
|
|
config types.InstalledIntegrationConfig,
|
|
|
|
|
) (*types.InstalledIntegration, *model.ApiError) {
|
|
|
|
|
|
|
|
|
|
integration := types.InstalledIntegration{
|
|
|
|
|
Identifiable: types.Identifiable{
|
|
|
|
|
ID: valuer.GenerateUUID(),
|
|
|
|
|
},
|
|
|
|
|
OrgID: orgId,
|
|
|
|
|
Type: integrationType,
|
|
|
|
|
Config: config,
|
2024-02-28 09:54:50 +05:30
|
|
|
}
|
|
|
|
|
|
2025-04-15 21:05:36 +05:30
|
|
|
_, dbErr := r.store.BunDB().NewInsert().
|
|
|
|
|
Model(&integration).
|
|
|
|
|
On("conflict (type, org_id) DO UPDATE").
|
|
|
|
|
Set("config = EXCLUDED.config").
|
|
|
|
|
Exec(ctx)
|
|
|
|
|
|
2024-02-28 09:54:50 +05:30
|
|
|
if dbErr != nil {
|
|
|
|
|
return nil, model.InternalError(fmt.Errorf(
|
|
|
|
|
"could not insert record for integration installation: %w", dbErr,
|
|
|
|
|
))
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-15 21:05:36 +05:30
|
|
|
res, apiErr := r.get(ctx, orgId, []string{integrationType})
|
2024-02-28 09:54:50 +05:30
|
|
|
if apiErr != nil || len(res) < 1 {
|
|
|
|
|
return nil, model.WrapApiError(
|
|
|
|
|
apiErr, "could not fetch installed integration",
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-15 21:05:36 +05:30
|
|
|
installed := res[integrationType]
|
2024-02-28 09:54:50 +05:30
|
|
|
|
|
|
|
|
return &installed, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (r *InstalledIntegrationsSqliteRepo) delete(
|
2025-04-15 21:05:36 +05:30
|
|
|
ctx context.Context, orgId string, integrationType string,
|
2024-02-28 09:54:50 +05:30
|
|
|
) *model.ApiError {
|
2025-04-15 21:05:36 +05:30
|
|
|
_, dbErr := r.store.BunDB().NewDelete().
|
|
|
|
|
Model(&types.InstalledIntegration{}).
|
|
|
|
|
Where("type = ?", integrationType).
|
|
|
|
|
Where("org_id = ?", orgId).
|
|
|
|
|
Exec(ctx)
|
2024-02-28 09:54:50 +05:30
|
|
|
|
|
|
|
|
if dbErr != nil {
|
|
|
|
|
return model.InternalError(fmt.Errorf(
|
|
|
|
|
"could not delete installed integration record for %s: %w",
|
2025-04-15 21:05:36 +05:30
|
|
|
integrationType, dbErr,
|
2024-02-28 09:54:50 +05:30
|
|
|
))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|