forked from openshift-kni/oran-o2ims
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request openshift-kni#6 from jhernand/add_metadata_server
Add metadata server
- Loading branch information
Showing
11 changed files
with
449 additions
and
23 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
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,20 @@ | ||
/* | ||
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 server | ||
|
||
// Names of command line flags: | ||
const ( | ||
cloudIDFlagName = "cloud-id" | ||
) |
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,171 @@ | ||
/* | ||
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 server | ||
|
||
import ( | ||
"log/slog" | ||
"net/http" | ||
|
||
"github.com/gorilla/mux" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/openshift-kni/oran-o2ims/internal" | ||
"github.com/openshift-kni/oran-o2ims/internal/exit" | ||
"github.com/openshift-kni/oran-o2ims/internal/service" | ||
) | ||
|
||
// MetadataServer creates and returns the `start metadata-server` command. | ||
func MetadataServer() *cobra.Command { | ||
c := NewMetadataServer() | ||
result := &cobra.Command{ | ||
Use: "metadata-server", | ||
Short: "Starts the metadata server", | ||
Args: cobra.NoArgs, | ||
RunE: c.run, | ||
} | ||
flags := result.Flags() | ||
_ = flags.String( | ||
cloudIDFlagName, | ||
"", | ||
"O-Cloud identifier.", | ||
) | ||
return result | ||
} | ||
|
||
// MetadataServerCommand contains the data and logic needed to run the `start | ||
// deployment-manager-server` command. | ||
type MetadataServerCommand struct { | ||
} | ||
|
||
// NewMetadataServer creates a new runner that knows how to execute the `start | ||
// deployment-manager-server` command. | ||
func NewMetadataServer() *MetadataServerCommand { | ||
return &MetadataServerCommand{} | ||
} | ||
|
||
// run executes the `start deployment-manager-server` command. | ||
func (c *MetadataServerCommand) run(cmd *cobra.Command, argv []string) error { | ||
// Get the context: | ||
ctx := cmd.Context() | ||
|
||
// Get the dependencies from the context: | ||
logger := internal.LoggerFromContext(ctx) | ||
|
||
// Get the flags: | ||
flags := cmd.Flags() | ||
|
||
// Get the cloud identifier: | ||
cloudID, err := flags.GetString(cloudIDFlagName) | ||
if err != nil { | ||
logger.Error( | ||
"Failed to get cloud identifier flag", | ||
"flag", cloudIDFlagName, | ||
"error", err.Error(), | ||
) | ||
return exit.Error(1) | ||
} | ||
if cloudID == "" { | ||
logger.Error( | ||
"Cloud identifier is empty", | ||
"flag", cloudIDFlagName, | ||
) | ||
return exit.Error(1) | ||
} | ||
logger.Info( | ||
"Cloud identifier", | ||
"value", cloudID, | ||
) | ||
|
||
// Create the router: | ||
router := mux.NewRouter() | ||
router.NotFoundHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
service.SendError(w, http.StatusNotFound, "Not found") | ||
}) | ||
router.MethodNotAllowedHandler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
service.SendError(w, http.StatusMethodNotAllowed, "Method not allowed") | ||
}) | ||
|
||
// Create the handler that servers the information about the versions of the API: | ||
versionsHandler, err := service.NewVersionsHandler(). | ||
SetLogger(logger). | ||
Build() | ||
if err != nil { | ||
logger.Error( | ||
"Failed to create versions handler", | ||
slog.String("error", err.Error()), | ||
) | ||
return exit.Error(1) | ||
} | ||
versionsAdapter, err := service.NewObjectAdapter(). | ||
SetLogger(logger). | ||
SetIDVariable("version"). | ||
SetHandler(versionsHandler). | ||
Build() | ||
if err != nil { | ||
logger.Error( | ||
"Failed to create versions adapter", | ||
slog.String("error", err.Error()), | ||
) | ||
return exit.Error(1) | ||
} | ||
router.Handle( | ||
"/O2ims_infrastructureInventory/api_versions", | ||
versionsAdapter, | ||
).Methods(http.MethodGet) | ||
router.Handle( | ||
"/O2ims_infrastructureInventory/{version}/api_versions", | ||
versionsAdapter, | ||
).Methods(http.MethodGet) | ||
|
||
// Create the handler that serves the information about the cloud: | ||
cloudInfoHandler, err := service.NewCloudInfoHandler(). | ||
SetLogger(logger). | ||
SetCloudID(cloudID). | ||
Build() | ||
if err != nil { | ||
logger.Error( | ||
"Failed to create cloud info handler", | ||
slog.String("error", err.Error()), | ||
) | ||
return exit.Error(1) | ||
} | ||
cloudInfoAdapter, err := service.NewObjectAdapter(). | ||
SetLogger(logger). | ||
SetHandler(cloudInfoHandler). | ||
Build() | ||
if err != nil { | ||
logger.Error( | ||
"Failed to create cloud info adapter", | ||
slog.String("error", err.Error()), | ||
) | ||
return exit.Error(1) | ||
} | ||
router.Handle( | ||
"/O2ims_infrastructureInventory/v1", | ||
cloudInfoAdapter, | ||
).Methods(http.MethodGet) | ||
|
||
// Start the server: | ||
err = http.ListenAndServe(":8080", router) | ||
if err != nil { | ||
logger.Error( | ||
"server finished with error", | ||
"error", err, | ||
) | ||
return exit.Error(1) | ||
} | ||
|
||
return nil | ||
} |
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,91 @@ | ||
/* | ||
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 service | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"log/slog" | ||
|
||
"github.com/openshift-kni/oran-o2ims/internal/data" | ||
) | ||
|
||
// CloudInfoHandlerBuilder contains the data and logic needed to create a new handler for the application | ||
// root. Don't create instances of this type directly, use the NewCloudInfoHandler function instead. | ||
type CloudInfoHandlerBuilder struct { | ||
logger *slog.Logger | ||
cloudID string | ||
} | ||
|
||
// RootHander knows how to respond to requests for the application root. Don't create instances of | ||
// this type directly, use the NewCloudInfoHandler function instead. | ||
type CloudInfoHandler struct { | ||
logger *slog.Logger | ||
cloudID string | ||
} | ||
|
||
// NewCloudInfoHandler creates a builder that can then be used to configure and create a handler for the | ||
// root of the application. | ||
func NewCloudInfoHandler() *CloudInfoHandlerBuilder { | ||
return &CloudInfoHandlerBuilder{} | ||
} | ||
|
||
// SetLogger sets the logger that the handler will use to write to the log. This is mandatory. | ||
func (b *CloudInfoHandlerBuilder) SetLogger(value *slog.Logger) *CloudInfoHandlerBuilder { | ||
b.logger = value | ||
return b | ||
} | ||
|
||
// SetCloudID sets the identifier of the O-Cloud of this handler. This is mandatory. | ||
func (b *CloudInfoHandlerBuilder) SetCloudID(value string) *CloudInfoHandlerBuilder { | ||
b.cloudID = value | ||
return b | ||
} | ||
|
||
// Build uses the data stored in the builder to create and configure a new handler. | ||
func (b *CloudInfoHandlerBuilder) Build() (result *CloudInfoHandler, err error) { | ||
// Check parameters: | ||
if b.logger == nil { | ||
err = errors.New("logger is mandatory") | ||
return | ||
} | ||
if b.cloudID == "" { | ||
err = errors.New("cloud identifier is mandatory") | ||
return | ||
} | ||
|
||
// Create and populate the object: | ||
result = &CloudInfoHandler{ | ||
logger: b.logger, | ||
cloudID: b.cloudID, | ||
} | ||
return | ||
} | ||
|
||
// Get is part of the implementation of the object handler interface. | ||
func (h *CloudInfoHandler) Get(ctx context.Context, request *ObjectRequest) (response *ObjectResponse, | ||
err error) { | ||
response = &ObjectResponse{ | ||
Object: data.Object{ | ||
"oCloudId": h.cloudID, | ||
"globalCloudId": h.cloudID, | ||
"name": "OpenShift O-Cloud", | ||
"description": "OpenShift O-Cloud", | ||
"serviceUri": "https://localhost:8080", | ||
"extensions": data.Object{}, | ||
}, | ||
} | ||
return | ||
} |
Oops, something went wrong.