github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/aws/rds.go (about) 1 // Copyright 2018 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 aws 16 17 import ( 18 "context" 19 "strings" 20 21 "github.com/GoogleCloudPlatform/terraformer/terraformutils" 22 23 "github.com/aws/aws-sdk-go-v2/service/rds" 24 ) 25 26 var RDSAllowEmptyValues = []string{"tags."} 27 28 type RDSGenerator struct { 29 AWSService 30 } 31 32 func (g *RDSGenerator) loadDBInstances(svc *rds.Client) error { 33 p := rds.NewDescribeDBInstancesPaginator(svc, &rds.DescribeDBInstancesInput{}) 34 for p.HasMorePages() { 35 page, err := p.NextPage(context.TODO()) 36 if err != nil { 37 return err 38 } 39 for _, db := range page.DBInstances { 40 resourceName := StringValue(db.DBInstanceIdentifier) 41 g.Resources = append(g.Resources, terraformutils.NewSimpleResource( 42 resourceName, 43 resourceName, 44 "aws_db_instance", 45 "aws", 46 RDSAllowEmptyValues, 47 )) 48 } 49 } 50 return nil 51 } 52 53 func (g *RDSGenerator) loadDBParameterGroups(svc *rds.Client) error { 54 p := rds.NewDescribeDBParameterGroupsPaginator(svc, &rds.DescribeDBParameterGroupsInput{}) 55 for p.HasMorePages() { 56 page, err := p.NextPage(context.TODO()) 57 if err != nil { 58 return err 59 } 60 for _, parameterGroup := range page.DBParameterGroups { 61 resourceName := StringValue(parameterGroup.DBParameterGroupName) 62 if strings.Contains(resourceName, ".") { 63 continue // skip default Default ParameterGroups like default.mysql5.6 64 } 65 g.Resources = append(g.Resources, terraformutils.NewSimpleResource( 66 resourceName, 67 resourceName, 68 "aws_db_parameter_group", 69 "aws", 70 RDSAllowEmptyValues, 71 )) 72 } 73 } 74 return nil 75 } 76 77 func (g *RDSGenerator) loadDBSubnetGroups(svc *rds.Client) error { 78 p := rds.NewDescribeDBSubnetGroupsPaginator(svc, &rds.DescribeDBSubnetGroupsInput{}) 79 for p.HasMorePages() { 80 page, err := p.NextPage(context.TODO()) 81 if err != nil { 82 return err 83 } 84 for _, subnet := range page.DBSubnetGroups { 85 resourceName := StringValue(subnet.DBSubnetGroupName) 86 g.Resources = append(g.Resources, terraformutils.NewSimpleResource( 87 resourceName, 88 resourceName, 89 "aws_db_subnet_group", 90 "aws", 91 RDSAllowEmptyValues, 92 )) 93 } 94 } 95 return nil 96 } 97 98 func (g *RDSGenerator) loadOptionGroups(svc *rds.Client) error { 99 p := rds.NewDescribeOptionGroupsPaginator(svc, &rds.DescribeOptionGroupsInput{}) 100 for p.HasMorePages() { 101 page, err := p.NextPage(context.TODO()) 102 if err != nil { 103 return err 104 } 105 for _, optionGroup := range page.OptionGroupsList { 106 resourceName := StringValue(optionGroup.OptionGroupName) 107 if strings.Contains(resourceName, ".") || strings.Contains(resourceName, ":") { 108 continue // skip default Default OptionGroups like default.mysql5.6 109 } 110 g.Resources = append(g.Resources, terraformutils.NewSimpleResource( 111 resourceName, 112 resourceName, 113 "aws_db_option_group", 114 "aws", 115 RDSAllowEmptyValues, 116 )) 117 } 118 } 119 return nil 120 } 121 122 func (g *RDSGenerator) loadEventSubscription(svc *rds.Client) error { 123 p := rds.NewDescribeEventSubscriptionsPaginator(svc, &rds.DescribeEventSubscriptionsInput{}) 124 for p.HasMorePages() { 125 page, err := p.NextPage(context.TODO()) 126 if err != nil { 127 return err 128 } 129 for _, eventSubscription := range page.EventSubscriptionsList { 130 resourceName := StringValue(eventSubscription.CustomerAwsId) 131 g.Resources = append(g.Resources, terraformutils.NewSimpleResource( 132 resourceName, 133 resourceName, 134 "aws_db_event_subscription", 135 "aws", 136 RDSAllowEmptyValues, 137 )) 138 } 139 } 140 return nil 141 } 142 143 // Generate TerraformResources from AWS API, 144 // from each database create 1 TerraformResource. 145 // Need only database name as ID for terraform resource 146 // AWS api support paging 147 func (g *RDSGenerator) InitResources() error { 148 config, e := g.generateConfig() 149 if e != nil { 150 return e 151 } 152 svc := rds.NewFromConfig(config) 153 154 if err := g.loadDBInstances(svc); err != nil { 155 return err 156 } 157 if err := g.loadDBParameterGroups(svc); err != nil { 158 return err 159 } 160 if err := g.loadDBSubnetGroups(svc); err != nil { 161 return err 162 } 163 if err := g.loadOptionGroups(svc); err != nil { 164 return err 165 } 166 167 if err := g.loadEventSubscription(svc); err != nil { 168 return err 169 } 170 171 return nil 172 } 173 174 func (g *RDSGenerator) PostConvertHook() error { 175 for i, r := range g.Resources { 176 if r.InstanceInfo.Type != "aws_db_instance" { 177 continue 178 } 179 for _, parameterGroup := range g.Resources { 180 if parameterGroup.InstanceInfo.Type != "aws_db_parameter_group" { 181 continue 182 } 183 if parameterGroup.InstanceState.Attributes["name"] == r.InstanceState.Attributes["parameter_group_name"] { 184 g.Resources[i].Item["parameter_group_name"] = "${aws_db_parameter_group." + parameterGroup.ResourceName + ".name}" 185 } 186 } 187 188 for _, subnet := range g.Resources { 189 if subnet.InstanceInfo.Type != "aws_db_subnet_group" { 190 continue 191 } 192 if subnet.InstanceState.Attributes["name"] == r.InstanceState.Attributes["db_subnet_group_name"] { 193 g.Resources[i].Item["db_subnet_group_name"] = "${aws_db_subnet_group." + subnet.ResourceName + ".name}" 194 } 195 } 196 197 for _, optionGroup := range g.Resources { 198 if optionGroup.InstanceInfo.Type != "aws_db_option_group" { 199 continue 200 } 201 if optionGroup.InstanceState.Attributes["name"] == r.InstanceState.Attributes["option_group_name"] { 202 g.Resources[i].Item["option_group_name"] = "${aws_db_option_group." + optionGroup.ResourceName + ".name}" 203 } 204 } 205 } 206 return nil 207 }