github.com/cs3org/reva/v2@v2.27.7/pkg/mentix/exchangers/exporters/cs3api/query.go (about) 1 // Copyright 2018-2021 CERN 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 // 15 // In applying this license, CERN does not waive the privileges and immunities 16 // granted to it by virtue of its status as an Intergovernmental Organization 17 // or submit itself to any jurisdiction. 18 19 package cs3api 20 21 import ( 22 "encoding/json" 23 "fmt" 24 "net/http" 25 "net/url" 26 27 ocmprovider "github.com/cs3org/go-cs3apis/cs3/ocm/provider/v1beta1" 28 "github.com/cs3org/reva/v2/pkg/mentix/utils" 29 "github.com/rs/zerolog" 30 31 "github.com/cs3org/reva/v2/pkg/mentix/config" 32 "github.com/cs3org/reva/v2/pkg/mentix/meshdata" 33 ) 34 35 // HandleDefaultQuery processes a basic query. 36 func HandleDefaultQuery(meshData *meshdata.MeshData, params url.Values, conf *config.Configuration, _ *zerolog.Logger) (int, []byte, error) { 37 // Convert the mesh data 38 ocmData, err := convertMeshDataToOCMData(meshData, conf.Exporters.CS3API.ElevatedServiceTypes) 39 if err != nil { 40 return http.StatusBadRequest, []byte{}, fmt.Errorf("unable to convert the mesh data to OCM data structures: %v", err) 41 } 42 43 // Marshal the OCM data as JSON 44 data, err := json.MarshalIndent(ocmData, "", "\t") 45 if err != nil { 46 return http.StatusBadRequest, []byte{}, fmt.Errorf("unable to marshal the OCM data: %v", err) 47 } 48 49 return http.StatusOK, data, nil 50 } 51 52 func convertMeshDataToOCMData(meshData *meshdata.MeshData, elevatedServiceTypes []string) ([]*ocmprovider.ProviderInfo, error) { 53 // Convert the mesh data into the corresponding OCM data structures 54 providers := make([]*ocmprovider.ProviderInfo, 0, len(meshData.Sites)) 55 for _, site := range meshData.Sites { 56 // Gather all services from the site 57 services := make([]*ocmprovider.Service, 0, len(site.Services)) 58 59 addService := func(host string, endpoint *meshdata.ServiceEndpoint, addEndpoints []*ocmprovider.ServiceEndpoint, apiVersion string) { 60 services = append(services, &ocmprovider.Service{ 61 Host: host, 62 Endpoint: convertServiceEndpointToOCMData(endpoint), 63 AdditionalEndpoints: addEndpoints, 64 ApiVersion: apiVersion, 65 }) 66 } 67 68 for _, service := range site.Services { 69 apiVersion := meshdata.GetPropertyValue(service.Properties, meshdata.PropertyAPIVersion, "") 70 71 // Gather all additional endpoints of the service 72 addEndpoints := make([]*ocmprovider.ServiceEndpoint, 0, len(service.AdditionalEndpoints)) 73 for _, endpoint := range service.AdditionalEndpoints { 74 if utils.FindInStringArray(endpoint.Type.Name, elevatedServiceTypes, false) != -1 { 75 endpointURL, _ := url.Parse(endpoint.URL) 76 addService(endpointURL.Host, endpoint, nil, apiVersion) 77 } else { 78 addEndpoints = append(addEndpoints, convertServiceEndpointToOCMData(endpoint)) 79 } 80 } 81 82 addService(service.Host, service.ServiceEndpoint, addEndpoints, apiVersion) 83 } 84 85 // Copy the site info into a ProviderInfo 86 providers = append(providers, &ocmprovider.ProviderInfo{ 87 Name: site.Name, 88 FullName: site.FullName, 89 Description: site.Description, 90 Organization: site.Organization, 91 Domain: site.Domain, 92 Homepage: site.Homepage, 93 Email: site.Email, 94 Services: services, 95 Properties: site.Properties, 96 }) 97 } 98 99 return providers, nil 100 } 101 102 func convertServiceEndpointToOCMData(endpoint *meshdata.ServiceEndpoint) *ocmprovider.ServiceEndpoint { 103 return &ocmprovider.ServiceEndpoint{ 104 Type: &ocmprovider.ServiceType{ 105 Name: endpoint.Type.Name, 106 Description: endpoint.Type.Description, 107 }, 108 Name: endpoint.Name, 109 Path: endpoint.URL, 110 IsMonitored: endpoint.IsMonitored, 111 Properties: endpoint.Properties, 112 } 113 }