github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/fastly/service_v1.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 fastly 16 17 import ( 18 "github.com/GoogleCloudPlatform/terraformer/terraformutils" 19 "github.com/fastly/go-fastly/v3/fastly" 20 ) 21 22 type ServiceV1Generator struct { 23 FastlyService 24 } 25 26 func (g *ServiceV1Generator) loadServices(client *fastly.Client) ([]*fastly.Service, error) { 27 services, err := client.ListServices(&fastly.ListServicesInput{}) 28 if err != nil { 29 return nil, err 30 } 31 for _, service := range services { 32 g.Resources = append(g.Resources, terraformutils.NewSimpleResource( 33 service.ID, 34 service.ID, 35 "fastly_service_v1", 36 "fastly", 37 []string{})) 38 } 39 return services, nil 40 } 41 42 func (g *ServiceV1Generator) loadDictionaryItems(client *fastly.Client, serviceID string) error { 43 latest, err := client.LatestVersion(&fastly.LatestVersionInput{ 44 ServiceID: serviceID, 45 }) 46 if err != nil { 47 return err 48 } 49 dictionaries, err := client.ListDictionaries(&fastly.ListDictionariesInput{ 50 ServiceID: serviceID, 51 ServiceVersion: latest.Number, 52 }) 53 if err != nil { 54 return err 55 } 56 for _, dictionary := range dictionaries { 57 g.Resources = append(g.Resources, terraformutils.NewResource( 58 dictionary.ID, 59 dictionary.ID, 60 "fastly_service_dictionary_items_v1", 61 "fastly", 62 map[string]string{ 63 "service_id": serviceID, 64 "dictionary_id": dictionary.ID, 65 }, 66 []string{}, 67 map[string]interface{}{})) 68 } 69 return nil 70 } 71 72 func (g *ServiceV1Generator) loadACLEntries(client *fastly.Client, serviceID string) error { 73 latest, err := client.LatestVersion(&fastly.LatestVersionInput{ 74 ServiceID: serviceID, 75 }) 76 if err != nil { 77 return err 78 } 79 acls, err := client.ListACLs(&fastly.ListACLsInput{ 80 ServiceID: serviceID, 81 ServiceVersion: latest.Number, 82 }) 83 if err != nil { 84 return err 85 } 86 for _, acl := range acls { 87 g.Resources = append(g.Resources, terraformutils.NewResource( 88 acl.ID, 89 acl.ID, 90 "fastly_service_acl_entries_v1", 91 "fastly", 92 map[string]string{ 93 "service_id": serviceID, 94 "acl_id": acl.ID, 95 }, 96 []string{}, 97 map[string]interface{}{})) 98 } 99 return nil 100 } 101 102 func (g *ServiceV1Generator) loadDynamicSnippetContent(client *fastly.Client, serviceID string) error { 103 latest, err := client.LatestVersion(&fastly.LatestVersionInput{ 104 ServiceID: serviceID, 105 }) 106 if err != nil { 107 return err 108 } 109 snippets, err := client.ListSnippets(&fastly.ListSnippetsInput{ 110 ServiceID: serviceID, 111 ServiceVersion: latest.Number, 112 }) 113 if err != nil { 114 return err 115 } 116 for _, snippet := range snippets { 117 // check if dynamic 118 if snippet.Dynamic == 1 { 119 g.Resources = append(g.Resources, terraformutils.NewResource( 120 snippet.ID, 121 snippet.ID, 122 "fastly_service_dynamic_snippet_content_v1", 123 "fastly", 124 map[string]string{ 125 "service_id": serviceID, 126 "snippet_id": snippet.ID, 127 }, 128 []string{}, 129 map[string]interface{}{})) 130 } 131 } 132 return nil 133 } 134 135 func (g *ServiceV1Generator) InitResources() error { 136 client, err := fastly.NewClient(g.Args["api_key"].(string)) 137 if err != nil { 138 return err 139 } 140 services, err := g.loadServices(client) 141 if err != nil { 142 return err 143 } 144 for _, service := range services { 145 err := g.loadDictionaryItems(client, service.ID) 146 if err != nil { 147 return err 148 } 149 err = g.loadACLEntries(client, service.ID) 150 if err != nil { 151 return err 152 } 153 err = g.loadDynamicSnippetContent(client, service.ID) 154 if err != nil { 155 return err 156 } 157 } 158 return nil 159 }