-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This patch adds a new `/o2ims-infrastructureInventory/v1/openapi` endpoint that returns the OpenAPI specification of the service. Currently this specification only covers the metadata and the deployment manager endpoints. Other endpoints will be added later. Related: https://issues.redhat.com/browse/MGMT-16112 Signed-off-by: Juan Hernandez <[email protected]>
- Loading branch information
Showing
9 changed files
with
642 additions
and
1 deletion.
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
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,103 @@ | ||
/* | ||
Copyright 2023 Red Hat Inc. | ||
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 openapi | ||
|
||
import ( | ||
"embed" | ||
"encoding/json" | ||
"errors" | ||
"log/slog" | ||
"net/http" | ||
|
||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
//go:embed spec.yaml | ||
var dataFS embed.FS | ||
|
||
// HandlerBuilder contains the data and logic needed to create a new handler for the OpenAPI | ||
// metadata. Don't create instances of this type directly, use the NewHandler function instead. | ||
type HandlerBuilder struct { | ||
logger *slog.Logger | ||
} | ||
|
||
// Handler knows how to respond to requests for the OpenAPI metadata. Don't create instances of | ||
// this type directly, use the NewHandler function instead. | ||
type Handler struct { | ||
logger *slog.Logger | ||
spec []byte | ||
} | ||
|
||
// NewHandler creates a builder that can then be used to configure and create a handler for the | ||
// OpenAPI metadata. | ||
func NewHandler() *HandlerBuilder { | ||
return &HandlerBuilder{} | ||
} | ||
|
||
// SetLogger sets the logger that the handler will use to write to the log. This is mandatory. | ||
func (b *HandlerBuilder) SetLogger(value *slog.Logger) *HandlerBuilder { | ||
b.logger = value | ||
return b | ||
} | ||
|
||
// Build uses the data stored in the builder to create and configure a new handler. | ||
func (b *HandlerBuilder) Build() (result *Handler, err error) { | ||
// Check parameters: | ||
if b.logger == nil { | ||
err = errors.New("logger is mandatory") | ||
return | ||
} | ||
|
||
// Load the specification: | ||
spec, err := b.loadSpec() | ||
if err != nil { | ||
return | ||
} | ||
|
||
// Create and populate the object: | ||
result = &Handler{ | ||
logger: b.logger, | ||
spec: spec, | ||
} | ||
return | ||
} | ||
|
||
func (b *HandlerBuilder) loadSpec() (result []byte, err error) { | ||
// Read the main file: | ||
data, err := dataFS.ReadFile("spec.yaml") | ||
if err != nil { | ||
return | ||
} | ||
var spec any | ||
err = yaml.Unmarshal(data, &spec) | ||
if err != nil { | ||
return | ||
} | ||
result, err = json.Marshal(spec) | ||
return | ||
} | ||
|
||
// ServeHTTP is the implementation of the object HTTP handler interface. | ||
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { | ||
w.Header().Set("Content-Type", "application/json") | ||
w.WriteHeader(http.StatusOK) | ||
_, err := w.Write(h.spec) | ||
if err != nil { | ||
h.logger.Error( | ||
"Failed to send data", | ||
slog.String("error", err.Error()), | ||
) | ||
} | ||
} |
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,104 @@ | ||
/* | ||
Copyright (c) 2023 Red Hat, Inc. | ||
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 openapi | ||
|
||
import ( | ||
"encoding/json" | ||
"mime" | ||
"net/http" | ||
"net/http/httptest" | ||
|
||
. "github.com/onsi/ginkgo/v2/dsl/core" | ||
. "github.com/onsi/ginkgo/v2/dsl/decorators" | ||
. "github.com/onsi/gomega" | ||
|
||
. "github.com/openshift-kni/oran-o2ims/internal/testing" | ||
) | ||
|
||
var _ = Describe("Handler", func() { | ||
It("Returns a valid JSON content type and document", func() { | ||
// Create the handler: | ||
handler, err := NewHandler(). | ||
SetLogger(logger). | ||
Build() | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(handler).ToNot(BeNil()) | ||
|
||
// Send the request: | ||
recorder := httptest.NewRecorder() | ||
request := httptest.NewRequest(http.MethodGet, "/", nil) | ||
handler.ServeHTTP(recorder, request) | ||
|
||
// Verify the content type: | ||
Expect(recorder.Code).To(Equal(http.StatusOK)) | ||
contentType := recorder.Header().Get("Content-Type") | ||
Expect(contentType).ToNot(BeEmpty()) | ||
mediaType, _, err := mime.ParseMediaType(contentType) | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(mediaType).To(Equal("application/json")) | ||
|
||
// Verify the content format: | ||
var spec any | ||
err = json.Unmarshal(recorder.Body.Bytes(), &spec) | ||
Expect(err).ToNot(HaveOccurred()) | ||
}) | ||
|
||
Describe("Content", Ordered, func() { | ||
var spec any | ||
|
||
BeforeAll(func() { | ||
// Create the handler: | ||
handler, err := NewHandler(). | ||
SetLogger(logger). | ||
Build() | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(handler).ToNot(BeNil()) | ||
|
||
// Send the request: | ||
recorder := httptest.NewRecorder() | ||
request := httptest.NewRequest(http.MethodGet, "/", nil) | ||
handler.ServeHTTP(recorder, request) | ||
|
||
// Parse the response: | ||
Expect(recorder.Code).To(Equal(http.StatusOK)) | ||
err = json.Unmarshal(recorder.Body.Bytes(), &spec) | ||
Expect(err).ToNot(HaveOccurred()) | ||
}) | ||
|
||
It("Contains the basic fields", func() { | ||
Expect(spec).To(MatchJQ(`.openapi`, "3.0.0")) | ||
Expect(spec).To(MatchJQ(`.info.title`, "O2 IMS")) | ||
Expect(spec).To(MatchJQ(`.info.version`, "1.0.0")) | ||
}) | ||
|
||
It("Contains at least one path", func() { | ||
Expect(spec).To(MatchJQ(`(.paths | length) > 0`, true)) | ||
}) | ||
|
||
It("Contains at least one schema", func() { | ||
Expect(spec).To(MatchJQ(`(.components.schemas | length) > 0`, true)) | ||
}) | ||
|
||
It("All paths start with the expected prefix", func() { | ||
Expect(spec).To(MatchJQ(`[.paths | keys[] | select(startswith("/o2ims-infrastructureInventory/") | not)] | length`, 0)) | ||
}) | ||
|
||
It("Contains the expected schemas", func() { | ||
Expect(spec).To(MatchJQ(`any(.components.schemas | keys[]; . == "APIVersion")`, true)) | ||
Expect(spec).To(MatchJQ(`any(.components.schemas | keys[]; . == "APIVersions")`, true)) | ||
Expect(spec).To(MatchJQ(`any(.components.schemas | keys[]; . == "DeploymentManager")`, true)) | ||
}) | ||
}) | ||
}) |
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,20 @@ | ||
# | ||
# Copyright (c) 2023 Red Hat, Inc. | ||
# | ||
# 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. | ||
# | ||
|
||
# This file contains the `spectral` configuration used to check the `spec.yaml` file. | ||
|
||
extends: [[spectral:oas, all]] | ||
|
||
rules: | ||
info-contact: off |
Oops, something went wrong.