github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/cloudflare/access.go (about) 1 // Copyright 2019 The Terraformer Authors. 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 package cloudflare 16 17 import ( 18 "fmt" 19 20 "github.com/GoogleCloudPlatform/terraformer/terraformutils" 21 cf "github.com/cloudflare/cloudflare-go" 22 ) 23 24 type AccessGenerator struct { 25 CloudflareService 26 } 27 28 func (g *AccessGenerator) createAccessApplications(api *cf.API, zoneID string) ([]terraformutils.Resource, error) { 29 resources := []terraformutils.Resource{} 30 accessApplications, _, err := api.AccessApplications(zoneID, cf.PaginationOptions{}) 31 if err != nil { 32 return []terraformutils.Resource{}, err 33 } 34 35 for _, app := range accessApplications { 36 resources = append(resources, terraformutils.NewResource( 37 app.ID, 38 fmt.Sprintf("%s_%s", app.Name, app.ID), 39 "cloudflare_access_application", 40 "cloudflare", 41 map[string]string{ 42 "zone_id": zoneID, 43 "name": app.Name, 44 }, 45 []string{}, 46 map[string]interface{}{}, 47 )) 48 } 49 50 return resources, nil 51 } 52 53 func (g *AccessGenerator) InitResources() error { 54 api, err := g.initializeAPI() 55 if err != nil { 56 return err 57 } 58 59 zones, err := api.ListZones() 60 if err != nil { 61 return err 62 } 63 64 for _, zone := range zones { 65 tmpRes, err := g.createAccessApplications(api, zone.ID) 66 if err != nil { 67 return err 68 } 69 70 g.Resources = append(g.Resources, tmpRes...) 71 } 72 73 return nil 74 }