Skip to content

Commit

Permalink
feat: cloud integrations: API response account structure
Browse files Browse the repository at this point in the history
  • Loading branch information
raj-k-singh committed Jan 8, 2025
1 parent ab72918 commit 016bce2
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 42 deletions.
28 changes: 12 additions & 16 deletions pkg/query-service/app/cloudintegrations/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"github.com/jmoiron/sqlx"
"go.signoz.io/signoz/pkg/query-service/model"
"go.uber.org/zap"
)

var SupportedCloudProviders = []string{
Expand Down Expand Up @@ -39,15 +38,15 @@ func NewController(db *sqlx.DB) (
}, nil
}

type ConnectedAccount struct {
type Account struct {
Id string `json:"id"`
CloudAccountId string `json:"cloud_account_id"`
Config AccountConfig `json:"config"`
Status AccountStatus `json:"status"`
}

type ConnectedAccountsListResponse struct {
Accounts []ConnectedAccount `json:"accounts"`
Accounts []Account `json:"accounts"`
}

func (c *Controller) ListConnectedAccounts(
Expand All @@ -59,19 +58,14 @@ func (c *Controller) ListConnectedAccounts(
return nil, apiErr
}

accounts, apiErr := c.repo.listConnected(ctx, cloudProvider)
accountRecords, apiErr := c.repo.listConnected(ctx, cloudProvider)
if apiErr != nil {
return nil, model.WrapApiError(apiErr, "couldn't list cloud accounts")
}

connectedAccounts := []ConnectedAccount{}
for _, a := range accounts {
ca, err := a.connectedAccount()
if err != nil {
zap.L().Error("invalid connected account record", zap.String("accountId", a.Id), zap.Error(err))
} else {
connectedAccounts = append(connectedAccounts, *ca)
}
connectedAccounts := []Account{}
for _, a := range accountRecords {
connectedAccounts = append(connectedAccounts, a.account())
}

return &ConnectedAccountsListResponse{
Expand Down Expand Up @@ -159,7 +153,7 @@ type AgentCheckInRequest struct {
}

type AgentCheckInResponse struct {
Account Account `json:"account"`
Account AccountRecord `json:"account"`
}

func (c *Controller) CheckInAsAgent(
Expand Down Expand Up @@ -208,19 +202,21 @@ func (c *Controller) UpdateAccountConfig(
return nil, apiErr
}

account, apiErr := c.repo.upsert(
accountRecord, apiErr := c.repo.upsert(
ctx, cloudProvider, &accountId, &req.Config, nil, nil, nil,
)
if apiErr != nil {
return nil, model.WrapApiError(apiErr, "couldn't upsert cloud account")
}

return account, nil
account := accountRecord.account()

return &account, nil
}

func (c *Controller) DisconnectAccount(
ctx context.Context, cloudProvider string, accountId string,
) (*Account, *model.ApiError) {
) (*AccountRecord, *model.ApiError) {
if apiErr := validateCloudProviderName(cloudProvider); apiErr != nil {
return nil, apiErr
}
Expand Down
18 changes: 7 additions & 11 deletions pkg/query-service/app/cloudintegrations/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

// Represents a cloud provider account for cloud integrations
type Account struct {
type AccountRecord struct {
CloudProvider string `json:"cloud_provider" db:"cloud_provider"`
Id string `json:"id" db:"id"`
Config *AccountConfig `json:"config" db:"config_json"`
Expand Down Expand Up @@ -91,7 +91,7 @@ type AccountIntegrationStatus struct {
LastHeartbeatTsMillis *int64 `json:"last_heartbeat_ts_ms"`
}

func (a *Account) status() AccountStatus {
func (a *AccountRecord) status() AccountStatus {
status := AccountStatus{}
if a.LastAgentReport != nil {
lastHeartbeat := a.LastAgentReport.TimestampMillis
Expand All @@ -100,20 +100,16 @@ func (a *Account) status() AccountStatus {
return status
}

func (a *Account) connectedAccount() (*ConnectedAccount, error) {
ca := ConnectedAccount{Id: a.Id, Status: a.status()}
func (a *AccountRecord) account() Account {
ca := Account{Id: a.Id, Status: a.status()}

if a.CloudAccountId == nil {
return nil, fmt.Errorf("account %s has nil cloud account id", a.Id)
} else {
if a.CloudAccountId != nil {
ca.CloudAccountId = *a.CloudAccountId
}

if a.Config == nil {
return nil, fmt.Errorf("account %s has nil config", a.Id)
} else {
if a.Config != nil {
ca.Config = *a.Config
}

return &ca, nil
return ca
}
22 changes: 11 additions & 11 deletions pkg/query-service/app/cloudintegrations/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ import (

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

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

get(ctx context.Context, cloudProvider string, id string) (*Account, *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
upsert(
Expand All @@ -28,7 +28,7 @@ type cloudProviderAccountsRepository interface {
cloudAccountId *string,
agentReport *AgentReport,
removedAt *time.Time,
) (*Account, *model.ApiError)
) (*AccountRecord, *model.ApiError)
}

func newCloudProviderAccountsRepository(db *sqlx.DB) (
Expand Down Expand Up @@ -76,8 +76,8 @@ type cloudProviderAccountsSQLRepository struct {

func (r *cloudProviderAccountsSQLRepository) listConnected(
ctx context.Context, cloudProvider string,
) ([]Account, *model.ApiError) {
accounts := []Account{}
) ([]AccountRecord, *model.ApiError) {
accounts := []AccountRecord{}

err := r.db.SelectContext(
ctx, &accounts, `
Expand Down Expand Up @@ -109,8 +109,8 @@ func (r *cloudProviderAccountsSQLRepository) listConnected(

func (r *cloudProviderAccountsSQLRepository) getByIds(
ctx context.Context, cloudProvider string, ids []string,
) (map[string]*Account, *model.ApiError) {
accounts := []Account{}
) (map[string]*AccountRecord, *model.ApiError) {
accounts := []AccountRecord{}

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

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

if a.Config == nil {
Expand All @@ -160,7 +160,7 @@ func (r *cloudProviderAccountsSQLRepository) getByIds(

func (r *cloudProviderAccountsSQLRepository) get(
ctx context.Context, cloudProvider string, id string,
) (*Account, *model.ApiError) {
) (*AccountRecord, *model.ApiError) {
res, apiErr := r.getByIds(ctx, cloudProvider, []string{id})
if apiErr != nil {
return nil, apiErr
Expand All @@ -184,7 +184,7 @@ func (r *cloudProviderAccountsSQLRepository) upsert(
cloudAccountId *string,
agentReport *AgentReport,
removedAt *time.Time,
) (*Account, *model.ApiError) {
) (*AccountRecord, *model.ApiError) {
// Insert
if id == nil {
newId := uuid.NewString()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ func (tb *CloudIntegrationsTestBed) CheckInAsAgentWithQS(

func (tb *CloudIntegrationsTestBed) UpdateAccountConfigWithQS(
cloudProvider string, accountId string, newConfig cloudintegrations.AccountConfig,
) *cloudintegrations.Account {
) *cloudintegrations.AccountRecord {
respDataJson := tb.RequestQS(
fmt.Sprintf(
"/api/v1/cloud-integrations/%s/accounts/%s/config",
Expand All @@ -247,7 +247,7 @@ func (tb *CloudIntegrationsTestBed) UpdateAccountConfigWithQS(
},
)

var resp cloudintegrations.Account
var resp cloudintegrations.AccountRecord
err := json.Unmarshal(respDataJson, &resp)
if err != nil {
tb.t.Fatalf("could not unmarshal apiResponse.Data json into Account")
Expand All @@ -258,15 +258,15 @@ func (tb *CloudIntegrationsTestBed) UpdateAccountConfigWithQS(

func (tb *CloudIntegrationsTestBed) DisconnectAccountWithQS(
cloudProvider string, accountId string,
) *cloudintegrations.Account {
) *cloudintegrations.AccountRecord {
respDataJson := tb.RequestQS(
fmt.Sprintf(
"/api/v1/cloud-integrations/%s/accounts/%s/disconnect",
cloudProvider, accountId,
), map[string]any{},
)

var resp cloudintegrations.Account
var resp cloudintegrations.AccountRecord
err := json.Unmarshal(respDataJson, &resp)
if err != nil {
tb.t.Fatalf("could not unmarshal apiResponse.Data json into Account")
Expand Down

0 comments on commit 016bce2

Please sign in to comment.