Skip to content

Commit

Permalink
Add resource API handling.
Browse files Browse the repository at this point in the history
In this implementation the API will return the minimal value set by an local or outside admin
  • Loading branch information
VoigtS committed Nov 13, 2024
1 parent c1f182f commit 2e58b10
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 16 deletions.
12 changes: 12 additions & 0 deletions internal/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,18 @@ func Test_ProjectOperations(t *testing.T) {
ExpectBody: assert.JSONFixtureFile("./fixtures/project-get-paris.json"),
}.Check(t, s.Handler)

// paris handles local max quota setting
_, dberr := s.DB.Exec("UPDATE project_resources SET max_quota_from_admin=300, max_quota_from_project=200 where id=17")
if dberr != nil {
t.Fatal(dberr)
}
assert.HTTPRequest{
Method: "GET",
Path: "/v1/domains/uuid-for-france/projects/uuid-for-paris",
ExpectStatus: 200,
ExpectBody: assert.JSONFixtureFile("./fixtures/project-get-paris.json"),
}.Check(t, s.Handler)

// check GetProjectRates
assert.HTTPRequest{
Method: "GET",
Expand Down
1 change: 1 addition & 0 deletions internal/datamodel/apply_computed_project_quota.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ var (
WHERE ps.type = $1 AND pr.name = $2 AND (pr.min_quota_from_backend IS NOT NULL
OR pr.max_quota_from_backend IS NOT NULL
OR pr.max_quota_from_admin IS NOT NULL
OR pr.max_quota_from_project IS NOT NULL
OR pr.override_quota_from_config IS NOT NULL)
`)

Expand Down
42 changes: 26 additions & 16 deletions internal/reports/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ var (
`)

projectReportResourcesQuery = sqlext.SimplifyWhitespace(`
SELECT p.id, ps.type, ps.scraped_at, pr.name, pr.quota, pr.max_quota_from_admin, par.az, par.quota, par.usage, par.physical_usage, par.historical_usage, pr.backend_quota, par.subresources
SELECT p.id, ps.type, ps.scraped_at, pr.name, pr.quota, pr.max_quota_from_admin, pr.max_quota_from_project, par.az, par.quota, par.usage, par.physical_usage, par.historical_usage, pr.backend_quota, par.subresources
FROM projects p
JOIN project_services ps ON ps.project_id = p.id {{AND ps.type = $service_type}}
JOIN project_resources pr ON pr.service_id = ps.id {{AND pr.name = $resource_name}}
Expand Down Expand Up @@ -128,23 +128,24 @@ func GetProjectResources(cluster *core.Cluster, domain db.Domain, project *db.Pr
)
err = sqlext.ForeachRow(dbi, fmt.Sprintf(queryStr, whereStr), append(joinArgs, whereArgs...), func(rows *sql.Rows) error {
var (
projectID db.ProjectID
dbServiceType db.ServiceType
scrapedAt *time.Time
dbResourceName liquid.ResourceName
quota *uint64
maxQuotaFromAdmin *uint64
az *limes.AvailabilityZone
azQuota *uint64
azUsage *uint64
azPhysicalUsage *uint64
azHistoricalUsage *string
backendQuota *int64
azSubresources *string
projectID db.ProjectID
dbServiceType db.ServiceType
scrapedAt *time.Time
dbResourceName liquid.ResourceName
quota *uint64
maxQuotaFromAdmin *uint64
maxQuotaFromProject *uint64
az *limes.AvailabilityZone
azQuota *uint64
azUsage *uint64
azPhysicalUsage *uint64
azHistoricalUsage *string
backendQuota *int64
azSubresources *string
)
err := rows.Scan(
&projectID, &dbServiceType, &scrapedAt, &dbResourceName,
&quota, &maxQuotaFromAdmin,
&quota, &maxQuotaFromAdmin, &maxQuotaFromProject,
&az, &azQuota, &azUsage, &azPhysicalUsage, &azHistoricalUsage, &backendQuota, &azSubresources,
)
if err != nil {
Expand Down Expand Up @@ -221,7 +222,16 @@ func GetProjectResources(cluster *core.Cluster, domain db.Domain, project *db.Pr
if quota != nil {
resReport.Quota = quota
resReport.UsableQuota = quota
resReport.MaxQuota = maxQuotaFromAdmin
if maxQuotaFromAdmin != nil && maxQuotaFromProject == nil {
resReport.MaxQuota = maxQuotaFromAdmin
}
if maxQuotaFromProject != nil && maxQuotaFromAdmin == nil {
resReport.MaxQuota = maxQuotaFromProject
}
if maxQuotaFromProject != nil && maxQuotaFromAdmin != nil {
maxQuota := min(*maxQuotaFromAdmin, *maxQuotaFromProject)
resReport.MaxQuota = &maxQuota
}
if backendQuota != nil && (*backendQuota < 0 || uint64(*backendQuota) != *quota) { //nolint:gosec // negative backend quota is explicitly checked
resReport.BackendQuota = backendQuota
}
Expand Down

0 comments on commit 2e58b10

Please sign in to comment.