Skip to content

Commit

Permalink
feat: cloud integrations: some more cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
raj-k-singh committed Jan 9, 2025
1 parent 016bce2 commit 7d576aa
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 29 deletions.
1 change: 1 addition & 0 deletions pkg/query-service/app/cloudintegrations/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ type GenerateConnectionUrlRequest struct {
}

type SigNozAgentConfig struct {
// The region in which SigNoz agent should be installed.
Region string `json:"region"`
}

Expand Down
2 changes: 2 additions & 0 deletions pkg/query-service/app/cloudintegrations/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ func (a *AccountRecord) account() Account {

if a.Config != nil {
ca.Config = *a.Config
} else {
ca.Config = DefaultAccountConfig()
}

return ca
Expand Down
53 changes: 24 additions & 29 deletions pkg/query-service/app/cloudintegrations/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ import (
)

type cloudProviderAccountsRepository interface {
// list connected cloud provider accounts.
listConnected(ctx context.Context, cloudProvider string) ([]AccountRecord, *model.ApiError)

getByIds(ctx context.Context, cloudProvider string, ids []string) (map[string]*AccountRecord, *model.ApiError)

get(ctx context.Context, cloudProvider string, id string) (*AccountRecord, *model.ApiError)

// Insert an account or update it by ID for specified non-empty fields
// Insert an account or update it by (cloudProvider, id)
// for specified non-empty fields
upsert(
ctx context.Context,
cloudProvider string,
Expand Down Expand Up @@ -90,11 +90,11 @@ func (r *cloudProviderAccountsSQLRepository) listConnected(
created_at,
removed_at
from cloud_integrations_accounts
where
cloud_provider=$1
and removed_at is NULL
and cloud_account_id is not NULL
and last_agent_report_json is not NULL
where
cloud_provider=$1
and removed_at is NULL
and cloud_account_id is not NULL
and last_agent_report_json is not NULL
order by created_at
`, cloudProvider,
)
Expand All @@ -110,7 +110,8 @@ func (r *cloudProviderAccountsSQLRepository) listConnected(
func (r *cloudProviderAccountsSQLRepository) getByIds(
ctx context.Context, cloudProvider string, ids []string,
) (map[string]*AccountRecord, *model.ApiError) {
accounts := []AccountRecord{}

records := []AccountRecord{}

idPlaceholders := []string{}
queryArgs := []any{cloudProvider}
Expand All @@ -121,7 +122,7 @@ func (r *cloudProviderAccountsSQLRepository) getByIds(
}

err := r.db.SelectContext(
ctx, &accounts, fmt.Sprintf(`
ctx, &records, fmt.Sprintf(`
select
cloud_provider,
id,
Expand All @@ -145,13 +146,7 @@ func (r *cloudProviderAccountsSQLRepository) getByIds(
}

result := map[string]*AccountRecord{}
for _, a := range accounts {

if a.Config == nil {
config := DefaultAccountConfig()
a.Config = &config
}

for _, a := range records {
result[a.Id] = &a
}

Expand Down Expand Up @@ -193,31 +188,31 @@ func (r *cloudProviderAccountsSQLRepository) upsert(

// Prepare clause for setting values in `on conflict do update`
onConflictSetStmts := []string{}
updateColStmt := func(col string) string {
setColStatement := func(col string) string {
return fmt.Sprintf("%s=excluded.%s", col, col)
}

if config != nil {
onConflictSetStmts = append(
onConflictSetStmts, updateColStmt("config_json"),
onConflictSetStmts, setColStatement("config_json"),
)
}

if cloudAccountId != nil {
onConflictSetStmts = append(
onConflictSetStmts, updateColStmt("cloud_account_id"),
onConflictSetStmts, setColStatement("cloud_account_id"),
)
}

if agentReport != nil {
onConflictSetStmts = append(
onConflictSetStmts, updateColStmt("last_agent_report_json"),
onConflictSetStmts, setColStatement("last_agent_report_json"),
)
}

if removedAt != nil {
onConflictSetStmts = append(
onConflictSetStmts, updateColStmt("removed_at"),
onConflictSetStmts, setColStatement("removed_at"),
)
}

Expand All @@ -230,14 +225,14 @@ func (r *cloudProviderAccountsSQLRepository) upsert(
}

insertQuery := fmt.Sprintf(`
INSERT INTO cloud_integrations_accounts (
cloud_provider,
id,
config_json,
cloud_account_id,
last_agent_report_json,
removed_at
) values ($1, $2, $3, $4, $5, $6)
INSERT INTO cloud_integrations_accounts (
cloud_provider,
id,
config_json,
cloud_account_id,
last_agent_report_json,
removed_at
) values ($1, $2, $3, $4, $5, $6)
%s`, onConflictClause,
)

Expand Down

0 comments on commit 7d576aa

Please sign in to comment.