forked from hashicorp/terraform-provider-google-beta
/
data_source_sql_database_instances.go
202 lines (178 loc) · 6.59 KB
/
data_source_sql_database_instances.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package sql
import (
"fmt"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/lorioux/google-beta/google-beta/tpgresource"
transport_tpg "github.com/lorioux/google-beta/google-beta/transport"
sqladmin "google.golang.org/api/sqladmin/v1beta4"
)
func DataSourceSqlDatabaseInstances() *schema.Resource {
return &schema.Resource{
Read: dataSourceSqlDatabaseInstancesRead,
Schema: map[string]*schema.Schema{
"project": {
Type: schema.TypeString,
Optional: true,
Description: `Project ID of the project that contains the instances.`,
},
"database_version": {
Type: schema.TypeString,
Optional: true,
Description: `To filter out the database instances which are of the specified database version.`,
},
"region": {
Type: schema.TypeString,
Optional: true,
Description: `To filter out the database instances which are located in this specified region.`,
},
"zone": {
Type: schema.TypeString,
Optional: true,
Description: `To filter out the database instances which are located in this specified zone.`,
},
"tier": {
Type: schema.TypeString,
Optional: true,
Description: `To filter out the database instances based on the machine type.`,
},
"state": {
Type: schema.TypeString,
Optional: true,
Description: `To filter out the database instances based on the current state of the database instance, valid values include : "SQL_INSTANCE_STATE_UNSPECIFIED", "RUNNABLE", "SUSPENDED", "PENDING_DELETE", "PENDING_CREATE", "MAINTENANCE" and "FAILED".`,
},
"instances": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: tpgresource.DatasourceSchemaFromResourceSchema(ResourceSqlDatabaseInstance().Schema),
},
},
},
}
}
func dataSourceSqlDatabaseInstancesRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*transport_tpg.Config)
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent)
if err != nil {
return err
}
project, err := tpgresource.GetProject(d, config)
if err != nil {
return err
}
filter := ""
if v, ok := d.GetOk("database_version"); ok {
filter += fmt.Sprintf("databaseVersion:%s", v.(string))
}
if v, ok := d.GetOk("region"); ok {
if filter != "" {
filter += " AND "
}
filter += fmt.Sprintf("region:%s", v.(string))
}
if v, ok := d.GetOk("zone"); ok {
if filter != "" {
filter += " AND "
}
filter += fmt.Sprintf("gceZone:%s", v.(string))
}
if v, ok := d.GetOk("tier"); ok {
if filter != "" {
filter += " AND "
}
filter += fmt.Sprintf("settings.tier:%s", v.(string))
}
if v, ok := d.GetOk("state"); ok {
if filter != "" {
filter += " AND "
}
filter += fmt.Sprintf("state:%s", v.(string))
}
pageToken := ""
databaseInstances := make([]map[string]interface{}, 0)
for {
var instances *sqladmin.InstancesListResponse
err = transport_tpg.Retry(transport_tpg.RetryOptions{
RetryFunc: func() (rerr error) {
instances, rerr = config.NewSqlAdminClient(userAgent).Instances.List(project).Filter(filter).PageToken(pageToken).Do()
return rerr
},
Timeout: d.Timeout(schema.TimeoutRead),
ErrorRetryPredicates: []transport_tpg.RetryErrorPredicateFunc{transport_tpg.IsSqlOperationInProgressError},
})
if err != nil {
return err
}
pageInstances := flattenDatasourceGoogleDatabaseInstancesList(instances.Items, project)
databaseInstances = append(databaseInstances, pageInstances...)
pageToken = instances.NextPageToken
if pageToken == "" {
break
}
}
if err := d.Set("instances", databaseInstances); err != nil {
return fmt.Errorf("Error retrieving instances: %s", err)
}
d.SetId(fmt.Sprintf("database_instances_ds/%s/%s/%s/%s/%s/%s", project, d.Get("database_version").(string), d.Get("region").(string), d.Get("zone").(string), d.Get("tier").(string), d.Get("state").(string)))
return nil
}
func flattenDatasourceGoogleDatabaseInstancesList(fetchedInstances []*sqladmin.DatabaseInstance, project string) []map[string]interface{} {
if fetchedInstances == nil {
return make([]map[string]interface{}, 0)
}
instances := make([]map[string]interface{}, 0, len(fetchedInstances))
for _, rawInstance := range fetchedInstances {
instance := make(map[string]interface{})
instance["name"] = rawInstance.Name
instance["region"] = rawInstance.Region
instance["database_version"] = rawInstance.DatabaseVersion
instance["connection_name"] = rawInstance.ConnectionName
instance["maintenance_version"] = rawInstance.MaintenanceVersion
instance["available_maintenance_versions"] = rawInstance.AvailableMaintenanceVersions
instance["instance_type"] = rawInstance.InstanceType
instance["service_account_email_address"] = rawInstance.ServiceAccountEmailAddress
instance["settings"] = flattenSettings(rawInstance.Settings)
if rawInstance.DiskEncryptionConfiguration != nil {
instance["encryption_key_name"] = rawInstance.DiskEncryptionConfiguration.KmsKeyName
}
instance["replica_configuration"] = flattenReplicaConfigurationforDataSource(rawInstance.ReplicaConfiguration)
ipAddresses := flattenIpAddresses(rawInstance.IpAddresses)
instance["ip_address"] = ipAddresses
if len(ipAddresses) > 0 {
instance["first_ip_address"] = ipAddresses[0]["ip_address"]
}
publicIpAddress := ""
privateIpAddress := ""
for _, ip := range rawInstance.IpAddresses {
if publicIpAddress == "" && ip.Type == "PRIMARY" {
publicIpAddress = ip.IpAddress
}
if privateIpAddress == "" && ip.Type == "PRIVATE" {
privateIpAddress = ip.IpAddress
}
}
instance["public_ip_address"] = publicIpAddress
instance["private_ip_address"] = privateIpAddress
instance["server_ca_cert"] = flattenServerCaCerts([]*sqladmin.SslCert{rawInstance.ServerCaCert})
instance["master_instance_name"] = strings.TrimPrefix(rawInstance.MasterInstanceName, project+":")
instance["project"] = project
instance["self_link"] = rawInstance.SelfLink
instances = append(instances, instance)
}
return instances
}
func flattenReplicaConfigurationforDataSource(replicaConfiguration *sqladmin.ReplicaConfiguration) []map[string]interface{} {
rc := []map[string]interface{}{}
if replicaConfiguration != nil {
data := map[string]interface{}{
"failover_target": replicaConfiguration.FailoverTarget,
// Don't attempt to assign anything from replicaConfiguration.MysqlReplicaConfiguration,
// since those fields are set on create and then not stored. Hence, those fields are not shown.
}
rc = append(rc, data)
}
return rc
}