-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a CS3API compliant data exporter to Mentix (#955)
- Loading branch information
1 parent
a1e5641
commit 64d6830
Showing
17 changed files
with
254 additions
and
9 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
19 changes: 19 additions & 0 deletions
19
docs/content/en/docs/config/http/services/mentix/cs3api/_index.md
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,19 @@ | ||
--- | ||
title: "cs3api" | ||
linkTitle: "cs3api" | ||
weight: 10 | ||
description: > | ||
Configuration for the CS3API of the Mentix service | ||
--- | ||
|
||
{{% pageinfo %}} | ||
The CS3API exporter exposes Mentix data in a format that is compliant with the CS3API `ProviderInfo` structure via an HTTP endpoint. | ||
{{% /pageinfo %}} | ||
|
||
{{% dir name="endpoint" type="string" default="/" %}} | ||
The endpoint where the mesh data can be queried. | ||
{{< highlight toml >}} | ||
[http.services.mentix.cs3api] | ||
endpoint = "/data" | ||
{{< /highlight >}} | ||
{{% /dir %}} |
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
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,73 @@ | ||
// Copyright 2018-2020 CERN | ||
// | ||
// 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. | ||
// | ||
// In applying this license, CERN does not waive the privileges and immunities | ||
// granted to it by virtue of its status as an Intergovernmental Organization | ||
// or submit itself to any jurisdiction. | ||
|
||
package exporters | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/rs/zerolog" | ||
|
||
"github.com/cs3org/reva/pkg/mentix/config" | ||
"github.com/cs3org/reva/pkg/mentix/exporters/cs3api" | ||
) | ||
|
||
// CS3APIExporter implements the CS3API exporter. | ||
type CS3APIExporter struct { | ||
BaseRequestExporter | ||
} | ||
|
||
// Activate activates the exporter. | ||
func (exporter *CS3APIExporter) Activate(conf *config.Configuration, log *zerolog.Logger) error { | ||
if err := exporter.BaseExporter.Activate(conf, log); err != nil { | ||
return err | ||
} | ||
|
||
// Store CS3API specific settings | ||
exporter.endpoint = conf.CS3API.Endpoint | ||
|
||
return nil | ||
} | ||
|
||
// HandleRequest handles the actual HTTP request. | ||
func (exporter *CS3APIExporter) HandleRequest(resp http.ResponseWriter, req *http.Request) error { | ||
// Data is read, so acquire a read lock | ||
exporter.locker.RLock() | ||
defer exporter.locker.RUnlock() | ||
|
||
data, err := cs3api.HandleQuery(exporter.meshData, req.URL.Query()) | ||
if err == nil { | ||
if _, err := resp.Write(data); err != nil { | ||
return fmt.Errorf("error writing the API request response: %v", err) | ||
} | ||
} else { | ||
return fmt.Errorf("error while serving API request: %v", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// GetName returns the display name of the exporter. | ||
func (exporter *CS3APIExporter) GetName() string { | ||
return "CS3API" | ||
} | ||
|
||
func init() { | ||
registerExporter(config.ExporterIDCS3API, &CS3APIExporter{}) | ||
} |
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,113 @@ | ||
// Copyright 2018-2020 CERN | ||
// | ||
// 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. | ||
// | ||
// In applying this license, CERN does not waive the privileges and immunities | ||
// granted to it by virtue of its status as an Intergovernmental Organization | ||
// or submit itself to any jurisdiction. | ||
|
||
package cs3api | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/url" | ||
"strings" | ||
|
||
ocmprovider "github.com/cs3org/go-cs3apis/cs3/ocm/provider/v1beta1" | ||
|
||
"github.com/cs3org/reva/pkg/mentix/meshdata" | ||
) | ||
|
||
const ( | ||
queryMethodDefault = "" | ||
) | ||
|
||
// HandleQuery handles an HTTP request based on the provided 'method' parameter. | ||
func HandleQuery(meshData *meshdata.MeshData, params url.Values) ([]byte, error) { | ||
method := params.Get("method") | ||
switch strings.ToLower(method) { | ||
case queryMethodDefault: | ||
return handleDefaultQuery(meshData, params) | ||
|
||
default: | ||
return []byte{}, fmt.Errorf("unknown API method '%v'", method) | ||
} | ||
} | ||
|
||
func handleDefaultQuery(meshData *meshdata.MeshData, params url.Values) ([]byte, error) { | ||
// Convert the mesh data | ||
ocmData, err := convertMeshDataToOCMData(meshData) | ||
if err != nil { | ||
return []byte{}, fmt.Errorf("unable to convert the mesh data to OCM data structures: %v", err) | ||
} | ||
|
||
// Marshal the OCM data as JSON | ||
data, err := json.MarshalIndent(ocmData, "", "\t") | ||
if err != nil { | ||
return []byte{}, fmt.Errorf("unable to marshal the mesh data: %v", err) | ||
} | ||
|
||
return data, nil | ||
} | ||
|
||
func convertMeshDataToOCMData(meshData *meshdata.MeshData) ([]*ocmprovider.ProviderInfo, error) { | ||
// Convert the mesh data into the corresponding OCM data structures | ||
providers := make([]*ocmprovider.ProviderInfo, 0, len(meshData.Sites)) | ||
for _, site := range meshData.Sites { | ||
// Gather all services from the site | ||
services := make([]*ocmprovider.Service, 0, len(site.Services)) | ||
for _, service := range site.Services { | ||
// Gather all additional endpoints of the service | ||
addEndpoints := make([]*ocmprovider.ServiceEndpoint, 0, len(service.AdditionalEndpoints)) | ||
for _, endpoint := range service.AdditionalEndpoints { | ||
addEndpoints = append(addEndpoints, convertServiceEndpointToOCMData(endpoint)) | ||
} | ||
|
||
services = append(services, &ocmprovider.Service{ | ||
Host: service.Host, | ||
Endpoint: convertServiceEndpointToOCMData(service.ServiceEndpoint), | ||
AdditionalEndpoints: addEndpoints, | ||
ApiVersion: meshdata.GetPropertyValue(service.Properties, meshdata.PropertyAPIVersion, ""), | ||
}) | ||
} | ||
|
||
// Copy the site info into a ProviderInfo | ||
providers = append(providers, &ocmprovider.ProviderInfo{ | ||
Name: site.Name, | ||
FullName: site.FullName, | ||
Description: site.Description, | ||
Organization: site.Organization, | ||
Domain: site.Domain, | ||
Homepage: site.Homepage, | ||
Email: site.Email, | ||
Services: services, | ||
Properties: site.Properties, | ||
}) | ||
} | ||
|
||
return providers, nil | ||
} | ||
|
||
func convertServiceEndpointToOCMData(endpoint *meshdata.ServiceEndpoint) *ocmprovider.ServiceEndpoint { | ||
return &ocmprovider.ServiceEndpoint{ | ||
Type: &ocmprovider.ServiceType{ | ||
Name: endpoint.Type.Name, | ||
Description: endpoint.Type.Description, | ||
}, | ||
Name: endpoint.Name, | ||
Path: endpoint.URL, | ||
IsMonitored: endpoint.IsMonitored, | ||
Properties: endpoint.Properties, | ||
} | ||
} |
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.