-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add api endpoint to render request payloads for querying the LIQUID api
- Loading branch information
Showing
15 changed files
with
371 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
/****************************************************************************** | ||
* | ||
* Copyright 2024 SAP SE | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
******************************************************************************/ | ||
|
||
package api | ||
|
||
import ( | ||
"database/sql" | ||
"errors" | ||
"net/http" | ||
|
||
"github.com/sapcc/go-bits/httpapi" | ||
"github.com/sapcc/go-bits/respondwith" | ||
|
||
"github.com/sapcc/limes/internal/core" | ||
"github.com/sapcc/limes/internal/datamodel" | ||
"github.com/sapcc/limes/internal/db" | ||
) | ||
|
||
// GetServiceCapacityRequest handles GET /admin/liquid/service-capacity-request?service_type=:type. | ||
func (p *v1Provider) GetServiceCapacityRequest(w http.ResponseWriter, r *http.Request) { | ||
httpapi.IdentifyEndpoint(r, "/admin/liquid/service-capacity-request") | ||
token := p.CheckToken(r) | ||
if !token.Require(w, "cluster:show") { | ||
return | ||
} | ||
|
||
serviceType := r.URL.Query().Get("service_type") | ||
if serviceType == "" { | ||
http.Error(w, "missing required parameter: service_type", http.StatusBadRequest) | ||
return | ||
} | ||
|
||
plugin, ok := p.Cluster.CapacityPlugins[serviceType] | ||
if !ok { | ||
http.Error(w, "invalid service type", http.StatusBadRequest) | ||
return | ||
} | ||
|
||
backchannel := datamodel.NewCapacityPluginBackchannel(p.Cluster, p.DB) | ||
serviceCapacityRequest, err := plugin.BuildServiceCapacityRequest(backchannel, p.Cluster.Config.AvailabilityZones) | ||
if respondwith.ErrorText(w, err) { | ||
return | ||
} | ||
if serviceCapacityRequest == nil { | ||
http.Error(w, "capacity plugin does not support LIQUID requests", http.StatusNotImplemented) | ||
return | ||
} | ||
|
||
respondwith.JSON(w, http.StatusOK, serviceCapacityRequest) | ||
} | ||
|
||
// p.GetServiceUsageRequest handles GET /admin/liquid/service-usage-request?service_type=:type&project_id=:id. | ||
func (p *v1Provider) GetServiceUsageRequest(w http.ResponseWriter, r *http.Request) { | ||
httpapi.IdentifyEndpoint(r, "/admin/liquid/service-usage-request") | ||
token := p.CheckToken(r) | ||
if !token.Require(w, "cluster:show") { | ||
return | ||
} | ||
|
||
serviceType := r.URL.Query().Get("service_type") | ||
if serviceType == "" { | ||
http.Error(w, "missing required parameter: service_type", http.StatusBadRequest) | ||
return | ||
} | ||
|
||
plugin, ok := p.Cluster.QuotaPlugins[db.ServiceType(serviceType)] | ||
if !ok { | ||
http.Error(w, "invalid service type", http.StatusBadRequest) | ||
return | ||
} | ||
|
||
projectID := r.URL.Query().Get("project_id") | ||
if projectID == "" { | ||
http.Error(w, "missing required parameter: project_id", http.StatusBadRequest) | ||
return | ||
} | ||
|
||
var dbProject db.Project | ||
err := p.DB.SelectOne(&dbProject, `SELECT * FROM projects WHERE id = $1`, projectID) | ||
if errors.Is(err, sql.ErrNoRows) { | ||
http.Error(w, "project not found", http.StatusNotFound) | ||
return | ||
} else if respondwith.ErrorText(w, err) { | ||
return | ||
} | ||
|
||
var dbDomain db.Domain | ||
err = p.DB.SelectOne(&dbDomain, `SELECT * FROM domains WHERE id = $1`, dbProject.DomainID) | ||
if respondwith.ErrorText(w, err) { | ||
return | ||
} | ||
|
||
domain := core.KeystoneDomainFromDB(dbDomain) | ||
project := core.KeystoneProjectFromDB(dbProject, domain) | ||
|
||
serviceUsageRequest, err := plugin.BuildServiceUsageRequest(project, p.Cluster.Config.AvailabilityZones) | ||
if respondwith.ErrorText(w, err) { | ||
return | ||
} | ||
if serviceUsageRequest == nil { | ||
http.Error(w, "quota plugin does not support LIQUID requests", http.StatusNotImplemented) | ||
return | ||
} | ||
|
||
respondwith.JSON(w, http.StatusOK, serviceUsageRequest) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
/******************************************************************************* | ||
* | ||
* Copyright 2024 SAP SE | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You should have received a copy of the License along with this | ||
* program. If not, you may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*******************************************************************************/ | ||
|
||
package api | ||
|
||
import ( | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/sapcc/go-bits/assert" | ||
|
||
"github.com/sapcc/limes/internal/test" | ||
) | ||
|
||
const ( | ||
liquidQuotaTestConfigYAML = ` | ||
availability_zones: [ az-one, az-two ] | ||
discovery: | ||
method: --test-static | ||
params: | ||
domains: | ||
- { name: germany, id: uuid-for-germany } | ||
projects: | ||
uuid-for-germany: | ||
- { name: berlin, id: uuid-for-berlin, parent_id: uuid-for-germany } | ||
services: | ||
- service_type: unittest | ||
type: --test-generic | ||
` | ||
liquidCapacityTestConfigYAML = ` | ||
availability_zones: [ az-one, az-two ] | ||
discovery: | ||
method: --test-static | ||
services: | ||
- service_type: unittest | ||
type: --test-generic | ||
capacitors: | ||
- id: unittest | ||
type: --test-static | ||
` | ||
) | ||
|
||
func TestGetServiceCapacityRequest(t *testing.T) { | ||
t.Helper() | ||
s := test.NewSetup(t, | ||
test.WithConfig(liquidCapacityTestConfigYAML), | ||
test.WithAPIHandler(NewV1API), | ||
) | ||
|
||
// endpoint requires cluster show permissions | ||
s.TokenValidator.Enforcer.AllowView = false | ||
assert.HTTPRequest{ | ||
Method: "GET", | ||
Path: "/admin/liquid/service-capacity-request?service_type=unittest", | ||
ExpectStatus: http.StatusForbidden, | ||
}.Check(t, s.Handler) | ||
s.TokenValidator.Enforcer.AllowView = true | ||
|
||
// expect error when service type is missing | ||
assert.HTTPRequest{ | ||
Method: "GET", | ||
Path: "/admin/liquid/service-capacity-request", | ||
ExpectStatus: http.StatusBadRequest, | ||
ExpectBody: assert.StringData("missing required parameter: service_type\n"), | ||
}.Check(t, s.Handler) | ||
|
||
// expect error for invalid service type | ||
assert.HTTPRequest{ | ||
Method: "GET", | ||
Path: "/admin/liquid/service-capacity-request?service_type=invalid_service_type", | ||
ExpectStatus: http.StatusBadRequest, | ||
ExpectBody: assert.StringData("invalid service type\n"), | ||
}.Check(t, s.Handler) | ||
|
||
// TODO: Implement happy path test for liquid plugins | ||
// Expect not implemented error for now | ||
assert.HTTPRequest{ | ||
Method: "GET", | ||
Path: "/admin/liquid/service-capacity-request?service_type=unittest", | ||
ExpectStatus: 501, | ||
ExpectBody: assert.StringData("capacity plugin does not support LIQUID requests\n"), | ||
}.Check(t, s.Handler) | ||
} | ||
|
||
func TestServiceUsageRequest(t *testing.T) { | ||
t.Helper() | ||
s := test.NewSetup(t, | ||
test.WithConfig(liquidQuotaTestConfigYAML), | ||
test.WithAPIHandler(NewV1API), | ||
test.WithDBFixtureFile("fixtures/start-data.sql"), | ||
) | ||
|
||
// endpoint requires cluster show permissions | ||
s.TokenValidator.Enforcer.AllowView = false | ||
assert.HTTPRequest{ | ||
Method: "GET", | ||
Path: "/admin/liquid/service-usage-request?service_type=unittest&project_id=1", | ||
ExpectStatus: http.StatusForbidden, | ||
}.Check(t, s.Handler) | ||
s.TokenValidator.Enforcer.AllowView = true | ||
|
||
// expect error when service type is missing | ||
assert.HTTPRequest{ | ||
Method: "GET", | ||
Path: "/admin/liquid/service-usage-request?project_id=1", | ||
ExpectStatus: http.StatusBadRequest, | ||
ExpectBody: assert.StringData("missing required parameter: service_type\n"), | ||
}.Check(t, s.Handler) | ||
|
||
// expect error when project_id is missing | ||
assert.HTTPRequest{ | ||
Method: "GET", | ||
Path: "/admin/liquid/service-usage-request?service_type=unittest", | ||
ExpectStatus: http.StatusBadRequest, | ||
ExpectBody: assert.StringData("missing required parameter: project_id\n"), | ||
}.Check(t, s.Handler) | ||
|
||
// expect error for invalid service type | ||
assert.HTTPRequest{ | ||
Method: "GET", | ||
Path: "/admin/liquid/service-usage-request?service_type=invalid_service_type&project_id=1", | ||
ExpectStatus: http.StatusBadRequest, | ||
ExpectBody: assert.StringData("invalid service type\n"), | ||
}.Check(t, s.Handler) | ||
|
||
// expect error for invalid project_id | ||
assert.HTTPRequest{ | ||
Method: "GET", | ||
Path: "/admin/liquid/service-usage-request?service_type=unittest&project_id=-1", | ||
ExpectStatus: http.StatusNotFound, | ||
ExpectBody: assert.StringData("project not found\n"), | ||
}.Check(t, s.Handler) | ||
|
||
// TODO: Implement happy path test for liquid plugins | ||
// Expect not implemented error for now | ||
assert.HTTPRequest{ | ||
Method: "GET", | ||
Path: "/admin/liquid/service-usage-request?service_type=unittest&project_id=1", | ||
ExpectStatus: 501, | ||
ExpectBody: assert.StringData("quota plugin does not support LIQUID requests\n"), | ||
}.Check(t, s.Handler) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.