github.com/cs3org/reva/v2@v2.27.7/pkg/mentix/meshdata/service.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 meshdata 20 21 import ( 22 "fmt" 23 "net/url" 24 "strings" 25 26 "github.com/cs3org/reva/v2/pkg/mentix/utils/network" 27 ) 28 29 // Service represents a service managed by Mentix. 30 type Service struct { 31 *ServiceEndpoint 32 33 Host string 34 AdditionalEndpoints []*ServiceEndpoint 35 } 36 37 // FindEndpoint searches for an additional endpoint with the given name. 38 func (service *Service) FindEndpoint(name string) *ServiceEndpoint { 39 for _, endpoint := range service.AdditionalEndpoints { 40 if strings.EqualFold(endpoint.Name, name) { 41 return endpoint 42 } 43 } 44 45 return nil 46 } 47 48 // InferMissingData infers missing data from other data where possible. 49 func (service *Service) InferMissingData() { 50 service.ServiceEndpoint.InferMissingData() 51 52 // Infer missing data 53 if service.Host == "" { 54 if serviceURL, err := url.Parse(service.URL); err == nil { 55 service.Host = network.ExtractDomainFromURL(serviceURL, true) 56 } 57 } 58 } 59 60 // Verify checks if the service data is valid. 61 func (service *Service) Verify() error { 62 if err := service.ServiceEndpoint.Verify(); err != nil { 63 return err 64 } 65 66 return nil 67 } 68 69 // ServiceType represents a service type managed by Mentix. 70 type ServiceType struct { 71 Name string 72 Description string 73 } 74 75 // InferMissingData infers missing data from other data where possible. 76 func (serviceType *ServiceType) InferMissingData() { 77 // Infer missing data 78 if serviceType.Description == "" { 79 serviceType.Description = serviceType.Name 80 } 81 } 82 83 // Verify checks if the service type data is valid. 84 func (serviceType *ServiceType) Verify() error { 85 // Verify data 86 if serviceType.Name == "" { 87 return fmt.Errorf("service type name missing") 88 } 89 90 return nil 91 } 92 93 // ServiceEndpoint represents a service endpoint managed by Mentix. 94 type ServiceEndpoint struct { 95 Type *ServiceType 96 Name string 97 RawURL string 98 URL string 99 IsMonitored bool 100 Properties map[string]string 101 } 102 103 // InferMissingData infers missing data from other data where possible. 104 func (serviceEndpoint *ServiceEndpoint) InferMissingData() { 105 } 106 107 // Verify checks if the service endpoint data is valid. 108 func (serviceEndpoint *ServiceEndpoint) Verify() error { 109 if serviceEndpoint.Type == nil { 110 return fmt.Errorf("service endpoint type missing") 111 } 112 if serviceEndpoint.Name == "" { 113 return fmt.Errorf("service endpoint name missing") 114 } 115 if serviceEndpoint.URL == "" { 116 return fmt.Errorf("service endpoint URL missing") 117 } 118 119 return nil 120 }