sigs.k8s.io/external-dns@v0.14.1/source/cloudfoundry.go (about) 1 /* 2 Copyright 2019 The Kubernetes Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package source 18 19 import ( 20 "context" 21 "net/url" 22 23 cfclient "github.com/cloudfoundry-community/go-cfclient" 24 25 "sigs.k8s.io/external-dns/endpoint" 26 ) 27 28 type cloudfoundrySource struct { 29 client *cfclient.Client 30 } 31 32 // NewCloudFoundrySource creates a new cloudfoundrySource with the given config 33 func NewCloudFoundrySource(cfClient *cfclient.Client) (Source, error) { 34 return &cloudfoundrySource{ 35 client: cfClient, 36 }, nil 37 } 38 39 func (rs *cloudfoundrySource) AddEventHandler(ctx context.Context, handler func()) { 40 } 41 42 // Endpoints returns endpoint objects 43 func (rs *cloudfoundrySource) Endpoints(ctx context.Context) ([]*endpoint.Endpoint, error) { 44 endpoints := []*endpoint.Endpoint{} 45 46 u, err := url.Parse(rs.client.Config.ApiAddress) 47 if err != nil { 48 panic(err) 49 } 50 51 domains, _ := rs.client.ListDomains() 52 for _, domain := range domains { 53 q := url.Values{} 54 q.Set("q", "domain_guid:"+domain.Guid) 55 routes, _ := rs.client.ListRoutesByQuery(q) 56 for _, element := range routes { 57 endpoints = append(endpoints, 58 endpoint.NewEndpointWithTTL(element.Host+"."+domain.Name, endpoint.RecordTypeCNAME, 300, u.Host)) 59 } 60 } 61 62 return endpoints, nil 63 }