Skip to content

Commit c3340fa

Browse files
authored
feat(deleter): count live resources that block a project or org delete (#2005)
1 parent 32d339a commit c3340fa

3 files changed

Lines changed: 299 additions & 8 deletions

File tree

‎core/deleter/deleter.go‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,38 @@ package deleter
22

33
import "strings"
44

5-
// Reasons an organization delete can be blocked. The API error carries
5+
// Reasons a delete can be blocked. The API error carries
66
// them as violation types, so a client can tell the reasons apart
77
// without reading the message text.
88
const (
99
BlockerActiveSubscription = "ACTIVE_SUBSCRIPTION"
1010
BlockerUnpaidInvoice = "UNPAID_INVOICE"
1111
BlockerNegativeTokenBalance = "NEGATIVE_TOKEN_BALANCE"
12+
BlockerResourcesExist = "RESOURCES_EXIST"
1213
)
1314

1415
// Kinds of entity a Blocker's Subject id refers to.
1516
const (
16-
SubjectSubscription = "billing_subscription"
17-
SubjectInvoice = "billing_invoice"
18-
SubjectBillingAccount = "billing_account"
17+
SubjectSubscription = "billing_subscription"
18+
SubjectInvoice = "billing_invoice"
19+
SubjectBillingAccount = "billing_account"
20+
SubjectResourceNamespace = "resource_namespace"
1921
)
2022

21-
// Blocker is one reason an organization cannot be deleted right now.
23+
// Blocker is one reason a project or an organization cannot be deleted right now.
2224
type Blocker struct {
2325
// Type is one of the Blocker* constants.
2426
Type string
25-
// Subject is the id of the entity behind the reason.
27+
// Subject is the id of the entity behind the reason, or the namespace
28+
// for BlockerResourcesExist.
2629
Subject string
2730
// SubjectType is one of the Subject* constants and says what kind of
2831
// entity the Subject id refers to.
2932
SubjectType string
30-
// Message says what blocks the delete and how to clear it.
33+
// Count is the number of blocking resources, for BlockerResourcesExist.
34+
Count int
35+
// Message says what blocks the delete and how to clear it. Empty for
36+
// BlockerResourcesExist.
3137
Message string
3238
}
3339

‎core/deleter/resource_blockers.go‎

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package deleter
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"maps"
7+
"slices"
8+
9+
"github.com/raystack/frontier/core/resource"
10+
)
11+
12+
const metadataDeletable = "deletable"
13+
14+
func (d Service) ProjectResourceBlockers(ctx context.Context, projectID string) ([]Blocker, error) {
15+
if !d.deletionConfig.Enabled {
16+
return nil, nil
17+
}
18+
resources, err := d.resService.List(ctx, resource.Filter{ProjectID: projectID})
19+
if err != nil {
20+
return nil, fmt.Errorf("failed to list resources of project[%s]: %w", projectID, err)
21+
}
22+
return d.deletionConfig.blockers(ParentProject, resources), nil
23+
}
24+
25+
func (d Service) OrganizationResourceBlockers(ctx context.Context, orgID string) ([]Blocker, error) {
26+
if !d.deletionConfig.Enabled {
27+
return nil, nil
28+
}
29+
resources, err := d.resService.List(ctx, resource.Filter{OrgID: orgID})
30+
if err != nil {
31+
return nil, fmt.Errorf("failed to list resources of organization[%s]: %w", orgID, err)
32+
}
33+
projects, err := d.listOrgProjects(ctx, orgID)
34+
if err != nil {
35+
return nil, fmt.Errorf("failed to list projects of organization[%s]: %w", orgID, err)
36+
}
37+
for _, p := range projects {
38+
projectResources, err := d.resService.List(ctx, resource.Filter{ProjectID: p.ID})
39+
if err != nil {
40+
return nil, fmt.Errorf("failed to list resources of project[%s]: %w", p.ID, err)
41+
}
42+
resources = append(resources, projectResources...)
43+
}
44+
return d.deletionConfig.blockers(ParentOrganization, resources), nil
45+
}
46+
47+
func (c Config) blockers(parent Parent, resources []resource.Resource) []Blocker {
48+
counts := map[string]int{}
49+
for _, r := range resources {
50+
if c.blocks(parent, r) {
51+
counts[r.NamespaceID]++
52+
}
53+
}
54+
var blockers []Blocker
55+
for _, namespace := range slices.Sorted(maps.Keys(counts)) {
56+
blockers = append(blockers, Blocker{
57+
Type: BlockerResourcesExist,
58+
Subject: namespace,
59+
SubjectType: SubjectResourceNamespace,
60+
Count: counts[namespace],
61+
})
62+
}
63+
return blockers
64+
}
65+
66+
func (c Config) blocks(parent Parent, r resource.Resource) bool {
67+
switch c.Resources[r.NamespaceID][parent] {
68+
case RuleAllow:
69+
return false
70+
case RuleBlockUnlessDeletable:
71+
deletable, _ := r.Metadata[metadataDeletable].(bool)
72+
return !deletable
73+
default:
74+
return true
75+
}
76+
}

‎core/deleter/service_test.go‎

Lines changed: 210 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ type deleterMocks struct {
6060
kycSvc *mocks.KycService
6161
planSvc *mocks.PlanService
6262
dialer *mailermocks.Dialer
63+
deletion deleter.Config
6364
}
6465

6566
func newMocks(t *testing.T) deleterMocks {
@@ -107,7 +108,7 @@ func (m deleterMocks) build() *deleter.Service {
107108
return deleter.NewCascadeDeleter(m.orgSvc, m.projSvc, m.resSvc, m.grpSvc, m.mbrSvc,
108109
m.polSvc, m.roleSvc, m.invSvc, m.usrSvc, m.patSvc, m.domainSvc, m.suSvc,
109110
m.custSvc, m.subSvc, m.invocSvc, m.checkoutSvc, m.creditSvc, m.kycSvc,
110-
m.planSvc, m.dialer, billing.OrgDeleteNoticeConfig{}, deleter.Config{})
111+
m.planSvc, m.dialer, billing.OrgDeleteNoticeConfig{}, m.deletion)
111112
}
112113

113114
func TestDeleteProject(t *testing.T) {
@@ -1514,3 +1515,211 @@ func TestDeleteUser(t *testing.T) {
15141515
assert.ErrorContains(t, err, "pat cleanup boom")
15151516
})
15161517
}
1518+
1519+
func blockerConfig() deleter.Config {
1520+
return deleter.Config{
1521+
Enabled: true,
1522+
Resources: map[string]deleter.Rules{
1523+
"compute/machine": {
1524+
deleter.ParentProject: deleter.RuleBlock,
1525+
deleter.ParentOrganization: deleter.RuleAllow,
1526+
},
1527+
"compute/order": {
1528+
deleter.ParentProject: deleter.RuleBlockUnlessDeletable,
1529+
deleter.ParentOrganization: deleter.RuleBlockUnlessDeletable,
1530+
},
1531+
"storage/file": {
1532+
deleter.ParentProject: deleter.RuleAllow,
1533+
deleter.ParentOrganization: deleter.RuleAllow,
1534+
},
1535+
"storage/transfer": {
1536+
deleter.ParentOrganization: deleter.RuleBlock,
1537+
},
1538+
},
1539+
}
1540+
}
1541+
1542+
func resourcesExist(namespace string, count int) deleter.Blocker {
1543+
return deleter.Blocker{
1544+
Type: deleter.BlockerResourcesExist,
1545+
Subject: namespace,
1546+
SubjectType: deleter.SubjectResourceNamespace,
1547+
Count: count,
1548+
}
1549+
}
1550+
1551+
func TestProjectResourceBlockers(t *testing.T) {
1552+
t.Run("returns no blockers and reads nothing while disabled", func(t *testing.T) {
1553+
m := newMocks(t)
1554+
1555+
blockers, err := m.build().ProjectResourceBlockers(context.Background(), "proj-1")
1556+
1557+
assert.NoError(t, err)
1558+
assert.Empty(t, blockers)
1559+
})
1560+
1561+
t.Run("counts blocking resources per namespace", func(t *testing.T) {
1562+
m := newMocks(t)
1563+
m.deletion = blockerConfig()
1564+
m.resSvc.EXPECT().List(mock.Anything, resource.Filter{ProjectID: "proj-1"}).
1565+
Return([]resource.Resource{
1566+
{ID: "m1", NamespaceID: "compute/machine"},
1567+
{ID: "m2", NamespaceID: "compute/machine"},
1568+
{ID: "o1", NamespaceID: "compute/order", Metadata: map[string]any{"deletable": true}},
1569+
{ID: "o2", NamespaceID: "compute/order", Metadata: map[string]any{"deletable": false}},
1570+
{ID: "o3", NamespaceID: "compute/order", Metadata: map[string]any{"deletable": "true"}},
1571+
{ID: "o4", NamespaceID: "compute/order"},
1572+
{ID: "f1", NamespaceID: "storage/file"},
1573+
{ID: "t1", NamespaceID: "storage/transfer"},
1574+
{ID: "r1", NamespaceID: "compute/report"},
1575+
}, nil)
1576+
1577+
blockers, err := m.build().ProjectResourceBlockers(context.Background(), "proj-1")
1578+
1579+
assert.NoError(t, err)
1580+
assert.Equal(t, []deleter.Blocker{
1581+
resourcesExist("compute/machine", 2),
1582+
resourcesExist("compute/order", 3),
1583+
resourcesExist("compute/report", 1),
1584+
resourcesExist("storage/transfer", 1),
1585+
}, blockers)
1586+
})
1587+
1588+
t.Run("returns no blockers when nothing blocks", func(t *testing.T) {
1589+
m := newMocks(t)
1590+
m.deletion = blockerConfig()
1591+
m.resSvc.EXPECT().List(mock.Anything, resource.Filter{ProjectID: "proj-1"}).
1592+
Return([]resource.Resource{
1593+
{ID: "o1", NamespaceID: "compute/order", Metadata: map[string]any{"deletable": true}},
1594+
{ID: "f1", NamespaceID: "storage/file"},
1595+
}, nil)
1596+
1597+
blockers, err := m.build().ProjectResourceBlockers(context.Background(), "proj-1")
1598+
1599+
assert.NoError(t, err)
1600+
assert.Empty(t, blockers)
1601+
})
1602+
1603+
t.Run("returns no blockers for a project with no resources", func(t *testing.T) {
1604+
m := newMocks(t)
1605+
m.deletion = blockerConfig()
1606+
m.resSvc.EXPECT().List(mock.Anything, resource.Filter{ProjectID: "proj-1"}).
1607+
Return([]resource.Resource{}, nil)
1608+
1609+
blockers, err := m.build().ProjectResourceBlockers(context.Background(), "proj-1")
1610+
1611+
assert.NoError(t, err)
1612+
assert.Empty(t, blockers)
1613+
})
1614+
1615+
t.Run("returns error when resource list fails", func(t *testing.T) {
1616+
m := newMocks(t)
1617+
m.deletion = blockerConfig()
1618+
m.resSvc.EXPECT().List(mock.Anything, resource.Filter{ProjectID: "proj-1"}).
1619+
Return(nil, errors.New("db error"))
1620+
1621+
_, err := m.build().ProjectResourceBlockers(context.Background(), "proj-1")
1622+
1623+
assert.ErrorContains(t, err, "failed to list resources of project[proj-1]: db error")
1624+
})
1625+
}
1626+
1627+
func TestOrganizationResourceBlockers(t *testing.T) {
1628+
t.Run("returns no blockers and reads nothing while disabled", func(t *testing.T) {
1629+
m := newMocks(t)
1630+
1631+
blockers, err := m.build().OrganizationResourceBlockers(context.Background(), "org-1")
1632+
1633+
assert.NoError(t, err)
1634+
assert.Empty(t, blockers)
1635+
})
1636+
1637+
t.Run("judges the org's own and its projects' resources by the organization rules", func(t *testing.T) {
1638+
m := newMocks(t)
1639+
m.deletion = blockerConfig()
1640+
m.resSvc.EXPECT().List(mock.Anything, resource.Filter{OrgID: "org-1"}).
1641+
Return([]resource.Resource{
1642+
{ID: "t1", NamespaceID: "storage/transfer"},
1643+
{ID: "f1", NamespaceID: "storage/file"},
1644+
}, nil)
1645+
m.projSvc.EXPECT().List(mock.Anything, project.Filter{OrgID: "org-1", IncludeDisabled: true}).
1646+
Return([]project.Project{{ID: "proj-1"}, {ID: "proj-2"}}, nil)
1647+
m.resSvc.EXPECT().List(mock.Anything, resource.Filter{ProjectID: "proj-1"}).
1648+
Return([]resource.Resource{
1649+
{ID: "m1", NamespaceID: "compute/machine"},
1650+
{ID: "m2", NamespaceID: "compute/machine"},
1651+
{ID: "o1", NamespaceID: "compute/order", Metadata: map[string]any{"deletable": false}},
1652+
}, nil)
1653+
m.resSvc.EXPECT().List(mock.Anything, resource.Filter{ProjectID: "proj-2"}).
1654+
Return([]resource.Resource{
1655+
{ID: "o2", NamespaceID: "compute/order"},
1656+
{ID: "o3", NamespaceID: "compute/order", Metadata: map[string]any{"deletable": true}},
1657+
{ID: "r1", NamespaceID: "compute/report"},
1658+
}, nil)
1659+
1660+
blockers, err := m.build().OrganizationResourceBlockers(context.Background(), "org-1")
1661+
1662+
assert.NoError(t, err)
1663+
assert.Equal(t, []deleter.Blocker{
1664+
resourcesExist("compute/order", 2),
1665+
resourcesExist("compute/report", 1),
1666+
resourcesExist("storage/transfer", 1),
1667+
}, blockers)
1668+
})
1669+
1670+
t.Run("judges the org's own resources when it has no projects", func(t *testing.T) {
1671+
m := newMocks(t)
1672+
m.deletion = blockerConfig()
1673+
m.resSvc.EXPECT().List(mock.Anything, resource.Filter{OrgID: "org-1"}).
1674+
Return([]resource.Resource{
1675+
{ID: "t1", NamespaceID: "storage/transfer"},
1676+
{ID: "f1", NamespaceID: "storage/file"},
1677+
}, nil)
1678+
m.projSvc.EXPECT().List(mock.Anything, project.Filter{OrgID: "org-1", IncludeDisabled: true}).
1679+
Return([]project.Project{}, nil)
1680+
1681+
blockers, err := m.build().OrganizationResourceBlockers(context.Background(), "org-1")
1682+
1683+
assert.NoError(t, err)
1684+
assert.Equal(t, []deleter.Blocker{resourcesExist("storage/transfer", 1)}, blockers)
1685+
})
1686+
1687+
t.Run("returns error when the org's own resource list fails", func(t *testing.T) {
1688+
m := newMocks(t)
1689+
m.deletion = blockerConfig()
1690+
m.resSvc.EXPECT().List(mock.Anything, resource.Filter{OrgID: "org-1"}).
1691+
Return(nil, errors.New("db error"))
1692+
1693+
_, err := m.build().OrganizationResourceBlockers(context.Background(), "org-1")
1694+
1695+
assert.ErrorContains(t, err, "failed to list resources of organization[org-1]: db error")
1696+
})
1697+
1698+
t.Run("returns error when project list fails", func(t *testing.T) {
1699+
m := newMocks(t)
1700+
m.deletion = blockerConfig()
1701+
m.resSvc.EXPECT().List(mock.Anything, resource.Filter{OrgID: "org-1"}).
1702+
Return([]resource.Resource{}, nil)
1703+
m.projSvc.EXPECT().List(mock.Anything, project.Filter{OrgID: "org-1", IncludeDisabled: true}).
1704+
Return(nil, errors.New("db error"))
1705+
1706+
_, err := m.build().OrganizationResourceBlockers(context.Background(), "org-1")
1707+
1708+
assert.ErrorContains(t, err, "failed to list projects of organization[org-1]: db error")
1709+
})
1710+
1711+
t.Run("returns error when a project's resource list fails", func(t *testing.T) {
1712+
m := newMocks(t)
1713+
m.deletion = blockerConfig()
1714+
m.resSvc.EXPECT().List(mock.Anything, resource.Filter{OrgID: "org-1"}).
1715+
Return([]resource.Resource{}, nil)
1716+
m.projSvc.EXPECT().List(mock.Anything, project.Filter{OrgID: "org-1", IncludeDisabled: true}).
1717+
Return([]project.Project{{ID: "proj-1"}}, nil)
1718+
m.resSvc.EXPECT().List(mock.Anything, resource.Filter{ProjectID: "proj-1"}).
1719+
Return(nil, errors.New("db error"))
1720+
1721+
_, err := m.build().OrganizationResourceBlockers(context.Background(), "org-1")
1722+
1723+
assert.ErrorContains(t, err, "failed to list resources of project[proj-1]: db error")
1724+
})
1725+
}

0 commit comments

Comments
 (0)