github.com/cs3org/reva/v2@v2.27.7/pkg/mentix/meshdata/site.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 // Site represents a single site managed by Mentix. 30 type Site struct { 31 // Internal settings 32 ID string 33 Name string 34 FullName string 35 Organization string 36 Domain string 37 Homepage string 38 Email string 39 Description string 40 Country string 41 CountryCode string 42 Location string 43 Latitude float32 44 Longitude float32 45 46 Services []*Service 47 Properties map[string]string 48 49 Downtimes Downtimes `json:"-"` 50 } 51 52 // AddService adds a new service; if a service with the same name already exists, the existing one is overwritten. 53 func (site *Site) AddService(service *Service) { 54 if serviceExisting := site.FindService(service.Name); serviceExisting != nil { 55 *service = *serviceExisting 56 } else { 57 site.Services = append(site.Services, service) 58 } 59 } 60 61 // RemoveService removes the provided service. 62 func (site *Site) RemoveService(name string) { 63 if service := site.FindService(name); service != nil { 64 for idx, serviceExisting := range site.Services { 65 if serviceExisting == service { 66 lastIdx := len(site.Services) - 1 67 site.Services[idx] = site.Services[lastIdx] 68 site.Services[lastIdx] = nil 69 site.Services = site.Services[:lastIdx] 70 break 71 } 72 } 73 } 74 } 75 76 // FindService searches for a service with the given name. 77 func (site *Site) FindService(name string) *Service { 78 for _, service := range site.Services { 79 if strings.EqualFold(service.Name, name) { 80 return service 81 } 82 } 83 return nil 84 } 85 86 // Verify checks if the site data is valid. 87 func (site *Site) Verify() error { 88 // Verify data 89 if site.Name == "" { 90 return fmt.Errorf("site name missing") 91 } 92 if site.Domain == "" && site.Homepage == "" { 93 return fmt.Errorf("site URL missing") 94 } 95 96 // Verify services 97 for _, service := range site.Services { 98 if err := service.Verify(); err != nil { 99 return err 100 } 101 } 102 103 return nil 104 } 105 106 // InferMissingData infers missing data from other data where possible. 107 func (site *Site) InferMissingData() { 108 // Infer missing data 109 if site.Homepage == "" { 110 site.Homepage = fmt.Sprintf("http://www.%v", site.Domain) 111 } else if site.Domain == "" { 112 if URL, err := url.Parse(site.Homepage); err == nil { 113 site.Domain = network.ExtractDomainFromURL(URL, false) 114 } 115 } 116 117 // Infer missing for services 118 for _, service := range site.Services { 119 service.InferMissingData() 120 } 121 }