feat: refactor api mount
This commit is contained in:
@ -48,7 +48,7 @@ func (r *AgentRepository) Attach(ctx context.Context, tenantID datastore.TenantI
|
||||
now := time.Now().UTC()
|
||||
|
||||
query = `
|
||||
UPDATE agents SET tenant_id = $1, updated_at = $2
|
||||
UPDATE agents SET tenant_id = $1, updated_at = $2 WHERE id = $3
|
||||
RETURNING "id", "thumbprint", "keyset", "metadata", "status", "created_at", "updated_at", "tenant_id"
|
||||
`
|
||||
|
||||
@ -56,6 +56,7 @@ func (r *AgentRepository) Attach(ctx context.Context, tenantID datastore.TenantI
|
||||
ctx, query,
|
||||
tenantID,
|
||||
now,
|
||||
agentID,
|
||||
)
|
||||
|
||||
metadata := JSONMap{}
|
||||
@ -85,8 +86,47 @@ func (r *AgentRepository) Attach(ctx context.Context, tenantID datastore.TenantI
|
||||
}
|
||||
|
||||
// Detach implements datastore.AgentRepository.
|
||||
func (*AgentRepository) Detach(ctx context.Context, agentID datastore.AgentID) (*datastore.Agent, error) {
|
||||
panic("unimplemented")
|
||||
func (r *AgentRepository) Detach(ctx context.Context, agentID datastore.AgentID) (*datastore.Agent, error) {
|
||||
var agent datastore.Agent
|
||||
|
||||
err := r.withTxRetry(ctx, func(tx *sql.Tx) error {
|
||||
now := time.Now().UTC()
|
||||
|
||||
query := `
|
||||
UPDATE agents SET tenant_id = null, updated_at = $1 WHERE id = $2
|
||||
RETURNING "id", "thumbprint", "keyset", "metadata", "status", "created_at", "updated_at", "tenant_id"
|
||||
`
|
||||
|
||||
row := tx.QueryRowContext(
|
||||
ctx, query,
|
||||
now,
|
||||
agentID,
|
||||
)
|
||||
|
||||
metadata := JSONMap{}
|
||||
var rawKeySet []byte
|
||||
|
||||
err := row.Scan(&agent.ID, &agent.Thumbprint, &rawKeySet, &metadata, &agent.Status, &agent.CreatedAt, &agent.UpdatedAt, &agent.TenantID)
|
||||
if err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
|
||||
agent.Metadata = metadata
|
||||
|
||||
keySet, err := jwk.Parse(rawKeySet)
|
||||
if err != nil {
|
||||
return errors.WithStack(err)
|
||||
}
|
||||
|
||||
agent.KeySet = &datastore.SerializableKeySet{keySet}
|
||||
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, errors.WithStack(err)
|
||||
}
|
||||
|
||||
return &agent, nil
|
||||
}
|
||||
|
||||
// DeleteSpec implements datastore.AgentRepository.
|
||||
|
Reference in New Issue
Block a user