github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/digitalocean/database_cluster.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 digitalocean 16 17 import ( 18 "context" 19 "fmt" 20 21 "github.com/GoogleCloudPlatform/terraformer/terraformutils" 22 "github.com/digitalocean/godo" 23 ) 24 25 type DatabaseClusterGenerator struct { 26 DigitalOceanService 27 } 28 29 func (g *DatabaseClusterGenerator) loadDatabaseClusters(ctx context.Context, client *godo.Client) ([]godo.Database, error) { 30 list := []godo.Database{} 31 32 // create options. initially, these will be blank 33 opt := &godo.ListOptions{} 34 for { 35 clusters, resp, err := client.Databases.List(ctx, opt) 36 if err != nil { 37 return nil, err 38 } 39 40 for _, cluster := range clusters { 41 g.Resources = append(g.Resources, terraformutils.NewSimpleResource( 42 cluster.ID, 43 cluster.Name, 44 "digitalocean_database_cluster", 45 "digitalocean", 46 []string{})) 47 list = append(list, cluster) 48 } 49 50 // if we are at the last page, break out the for loop 51 if resp.Links == nil || resp.Links.IsLastPage() { 52 break 53 } 54 55 page, err := resp.Links.CurrentPage() 56 if err != nil { 57 return nil, err 58 } 59 60 // set the page we want for the next request 61 opt.Page = page + 1 62 } 63 64 return list, nil 65 } 66 67 func (g *DatabaseClusterGenerator) loadDatabaseConnectionPools(ctx context.Context, client *godo.Client, clusterID string) error { 68 // create options. initially, these will be blank 69 opt := &godo.ListOptions{} 70 for { 71 pools, resp, err := client.Databases.ListPools(ctx, clusterID, opt) 72 if err != nil { 73 return err 74 } 75 76 for _, pool := range pools { 77 g.Resources = append(g.Resources, terraformutils.NewSimpleResource( 78 fmt.Sprintf("%s/%s", clusterID, pool.Name), 79 pool.Name, 80 "digitalocean_database_connection_pool", 81 "digitalocean", 82 []string{})) 83 } 84 85 // if we are at the last page, break out the for loop 86 if resp.Links == nil || resp.Links.IsLastPage() { 87 break 88 } 89 90 page, err := resp.Links.CurrentPage() 91 if err != nil { 92 return err 93 } 94 95 // set the page we want for the next request 96 opt.Page = page + 1 97 } 98 99 return nil 100 } 101 102 func (g *DatabaseClusterGenerator) loadDatabaseDBs(ctx context.Context, client *godo.Client, clusterID string) error { 103 // create options. initially, these will be blank 104 opt := &godo.ListOptions{} 105 for { 106 dbs, resp, err := client.Databases.ListDBs(ctx, clusterID, opt) 107 if err != nil { 108 return err 109 } 110 111 for _, db := range dbs { 112 // skip default database created by the digitalocean database cluster 113 if db.Name != "defaultdb" { 114 g.Resources = append(g.Resources, terraformutils.NewResource( 115 db.Name, 116 db.Name, 117 "digitalocean_database_db", 118 "digitalocean", 119 map[string]string{ 120 "cluster_id": clusterID, 121 "name": db.Name, 122 }, 123 []string{}, 124 map[string]interface{}{})) 125 } 126 } 127 128 // if we are at the last page, break out the for loop 129 if resp.Links == nil || resp.Links.IsLastPage() { 130 break 131 } 132 133 page, err := resp.Links.CurrentPage() 134 if err != nil { 135 return err 136 } 137 138 // set the page we want for the next request 139 opt.Page = page + 1 140 } 141 142 return nil 143 } 144 145 func (g *DatabaseClusterGenerator) loadDatabaseReplicas(ctx context.Context, client *godo.Client, clusterID string) error { 146 // create options. initially, these will be blank 147 opt := &godo.ListOptions{} 148 for { 149 replicas, resp, err := client.Databases.ListReplicas(ctx, clusterID, opt) 150 if err != nil { 151 return err 152 } 153 154 for _, replica := range replicas { 155 g.Resources = append(g.Resources, terraformutils.NewResource( 156 replica.Name, 157 replica.Name, 158 "digitalocean_database_replica", 159 "digitalocean", 160 map[string]string{ 161 "cluster_id": clusterID, 162 "name": replica.Name, 163 }, 164 []string{}, 165 map[string]interface{}{})) 166 } 167 168 // if we are at the last page, break out the for loop 169 if resp.Links == nil || resp.Links.IsLastPage() { 170 break 171 } 172 173 page, err := resp.Links.CurrentPage() 174 if err != nil { 175 return err 176 } 177 178 // set the page we want for the next request 179 opt.Page = page + 1 180 } 181 182 return nil 183 } 184 185 func (g *DatabaseClusterGenerator) loadDatabaseUsers(ctx context.Context, client *godo.Client, clusterID string) error { 186 // create options. initially, these will be blank 187 opt := &godo.ListOptions{} 188 for { 189 users, resp, err := client.Databases.ListUsers(ctx, clusterID, opt) 190 if err != nil { 191 return err 192 } 193 194 for _, user := range users { 195 // skip default user created by the digitalocean database cluster 196 if user.Name != "doadmin" { 197 g.Resources = append(g.Resources, terraformutils.NewResource( 198 user.Name, 199 user.Name, 200 "digitalocean_database_user", 201 "digitalocean", 202 map[string]string{ 203 "cluster_id": clusterID, 204 "name": user.Name, 205 }, 206 []string{}, 207 map[string]interface{}{})) 208 } 209 } 210 211 // if we are at the last page, break out the for loop 212 if resp.Links == nil || resp.Links.IsLastPage() { 213 break 214 } 215 216 page, err := resp.Links.CurrentPage() 217 if err != nil { 218 return err 219 } 220 221 // set the page we want for the next request 222 opt.Page = page + 1 223 } 224 225 return nil 226 } 227 228 func (g *DatabaseClusterGenerator) InitResources() error { 229 client := g.generateClient() 230 clusters, err := g.loadDatabaseClusters(context.TODO(), client) 231 if err != nil { 232 return err 233 } 234 for _, cluster := range clusters { 235 err := g.loadDatabaseConnectionPools(context.TODO(), client, cluster.ID) 236 if err != nil { 237 return err 238 } 239 err = g.loadDatabaseDBs(context.TODO(), client, cluster.ID) 240 if err != nil { 241 return err 242 } 243 err = g.loadDatabaseReplicas(context.TODO(), client, cluster.ID) 244 if err != nil { 245 return err 246 } 247 err = g.loadDatabaseUsers(context.TODO(), client, cluster.ID) 248 if err != nil { 249 return err 250 } 251 } 252 return nil 253 }