refactor: update AS entry handling in API and database to support ASN-only entries. Remove prefix field from AS entry structure and adjust related functions and tests. Enhance OpenAPI specifications and documentation to reflect changes in AS entry representation.
This commit is contained in:
@@ -830,7 +830,7 @@ func (p *Postgres) CreateRenderRevision(revisionID, tenantID, moduleID string, p
|
||||
}
|
||||
_, err = tx.Exec(ctx, `
|
||||
INSERT INTO revision_materialized_prefix (revision_id, prefix, community_id, source)
|
||||
VALUES ($1::uuid, $2::cidr, $3::uuid, $4)`,
|
||||
VALUES ($1::uuid, $2, $3::uuid, $4)`,
|
||||
strings.TrimSpace(revisionID), strings.TrimSpace(pr.Prefix), comm, src)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
@@ -164,7 +164,7 @@ func (p *Postgres) ListASEntries(tenantID, moduleID string) ([]*store.ASEntry, e
|
||||
}
|
||||
ctx := context.Background()
|
||||
rows, err := p.pool.Query(ctx, `
|
||||
SELECT id::text, asn, prefix::text, community_id::text FROM module_as_entry WHERE module_id=$1`, moduleID)
|
||||
SELECT id::text, asn, community_id::text FROM module_as_entry WHERE module_id=$1`, moduleID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -173,13 +173,10 @@ func (p *Postgres) ListASEntries(tenantID, moduleID string) ([]*store.ASEntry, e
|
||||
for rows.Next() {
|
||||
var e store.ASEntry
|
||||
e.ModuleID = moduleID
|
||||
var asn *int64
|
||||
var pref, comm *string
|
||||
if err := rows.Scan(&e.ID, &asn, &pref, &comm); err != nil {
|
||||
var comm *string
|
||||
if err := rows.Scan(&e.ID, &e.ASN, &comm); err != nil {
|
||||
continue
|
||||
}
|
||||
e.ASN = asn
|
||||
e.Prefix = pref
|
||||
e.CommunityID = strOrNil(comm)
|
||||
out = append(out, &e)
|
||||
}
|
||||
@@ -194,19 +191,15 @@ func (p *Postgres) CreateASEntry(tenantID, moduleID string, in *store.ASEntry) (
|
||||
if mod.Type != "AS_PREFIXES" {
|
||||
return nil, store.ErrInvalidInput
|
||||
}
|
||||
if in == nil || (in.ASN == nil && (in.Prefix == nil || strings.TrimSpace(*in.Prefix) == "")) {
|
||||
if in == nil || !store.ValidASN(in.ASN) {
|
||||
return nil, store.ErrInvalidInput
|
||||
}
|
||||
ctx := context.Background()
|
||||
id := uuid.NewString()
|
||||
var pref any
|
||||
if in.Prefix != nil && strings.TrimSpace(*in.Prefix) != "" {
|
||||
pref = strings.TrimSpace(*in.Prefix)
|
||||
}
|
||||
_, err = p.pool.Exec(ctx, `
|
||||
INSERT INTO module_as_entry (id, module_id, asn, prefix, community_id)
|
||||
VALUES ($1,$2,$3,$4::cidr, NULLIF($5::uuid, '00000000-0000-0000-0000-000000000000'::uuid))`,
|
||||
id, moduleID, in.ASN, pref, uuidOrNilPtr(in.CommunityID))
|
||||
INSERT INTO module_as_entry (id, module_id, asn, community_id)
|
||||
VALUES ($1,$2,$3,NULLIF($4::uuid, '00000000-0000-0000-0000-000000000000'::uuid))`,
|
||||
id, moduleID, in.ASN, uuidOrNilPtr(in.CommunityID))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -216,16 +209,13 @@ func (p *Postgres) CreateASEntry(tenantID, moduleID string, in *store.ASEntry) (
|
||||
func (p *Postgres) getASEntry(ctx context.Context, moduleID, id string) (*store.ASEntry, error) {
|
||||
var e store.ASEntry
|
||||
e.ModuleID = moduleID
|
||||
var asn *int64
|
||||
var pref, comm *string
|
||||
var comm *string
|
||||
err := p.pool.QueryRow(ctx, `
|
||||
SELECT id::text, asn, prefix::text, community_id::text FROM module_as_entry WHERE id=$1 AND module_id=$2`, id, moduleID).Scan(
|
||||
&e.ID, &asn, &pref, &comm)
|
||||
SELECT id::text, asn, community_id::text FROM module_as_entry WHERE id=$1 AND module_id=$2`, id, moduleID).Scan(
|
||||
&e.ID, &e.ASN, &comm)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
e.ASN = asn
|
||||
e.Prefix = pref
|
||||
e.CommunityID = strOrNil(comm)
|
||||
return &e, nil
|
||||
}
|
||||
@@ -242,15 +232,7 @@ func (p *Postgres) UpdateASEntry(tenantID, moduleID, entryID string, patch *stor
|
||||
return nil, err
|
||||
}
|
||||
if patch.ASN != nil {
|
||||
cur.ASN = patch.ASN
|
||||
}
|
||||
if patch.Prefix != nil {
|
||||
p := strings.TrimSpace(*patch.Prefix)
|
||||
if p == "" {
|
||||
cur.Prefix = nil
|
||||
} else {
|
||||
cur.Prefix = &p
|
||||
}
|
||||
cur.ASN = *patch.ASN
|
||||
}
|
||||
if patch.CommunityID != nil {
|
||||
v := strings.TrimSpace(*patch.CommunityID)
|
||||
@@ -260,15 +242,14 @@ func (p *Postgres) UpdateASEntry(tenantID, moduleID, entryID string, patch *stor
|
||||
cur.CommunityID = &v
|
||||
}
|
||||
}
|
||||
ctx := context.Background()
|
||||
var pref any
|
||||
if cur.Prefix != nil {
|
||||
pref = *cur.Prefix
|
||||
if !store.ValidASN(cur.ASN) {
|
||||
return nil, store.ErrInvalidInput
|
||||
}
|
||||
ctx := context.Background()
|
||||
_, err = p.pool.Exec(ctx, `
|
||||
UPDATE module_as_entry SET asn=$3, prefix=$4::cidr, community_id=NULLIF($5::uuid, '00000000-0000-0000-0000-000000000000'::uuid), updated_at=now()
|
||||
UPDATE module_as_entry SET asn=$3, community_id=NULLIF($4::uuid, '00000000-0000-0000-0000-000000000000'::uuid), updated_at=now()
|
||||
WHERE id=$1 AND module_id=$2`,
|
||||
entryID, moduleID, cur.ASN, pref, uuidOrNilPtr(cur.CommunityID))
|
||||
entryID, moduleID, cur.ASN, uuidOrNilPtr(cur.CommunityID))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user