From 2e58b109c72853854d9e2ab072d34405b9dbb3ac Mon Sep 17 00:00:00 2001 From: VoigtS Date: Wed, 13 Nov 2024 17:50:04 +0100 Subject: [PATCH] Add resource API handling. In this implementation the API will return the minimal value set by an local or outside admin --- internal/api/api_test.go | 12 ++++++ .../datamodel/apply_computed_project_quota.go | 1 + internal/reports/project.go | 42 ++++++++++++------- 3 files changed, 39 insertions(+), 16 deletions(-) diff --git a/internal/api/api_test.go b/internal/api/api_test.go index 5bb90a01..05c6939b 100644 --- a/internal/api/api_test.go +++ b/internal/api/api_test.go @@ -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", diff --git a/internal/datamodel/apply_computed_project_quota.go b/internal/datamodel/apply_computed_project_quota.go index 56eca63e..0b316ac8 100644 --- a/internal/datamodel/apply_computed_project_quota.go +++ b/internal/datamodel/apply_computed_project_quota.go @@ -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) `) diff --git a/internal/reports/project.go b/internal/reports/project.go index 0673d704..19294554 100644 --- a/internal/reports/project.go +++ b/internal/reports/project.go @@ -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}} @@ -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, - "a, &maxQuotaFromAdmin, + "a, &maxQuotaFromAdmin, &maxQuotaFromProject, &az, &azQuota, &azUsage, &azPhysicalUsage, &azHistoricalUsage, &backendQuota, &azSubresources, ) if err != nil { @@ -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 }