Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change authorized_networks field in sql instance to be a Set. #564

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
194 changes: 55 additions & 139 deletions google/resource_sql_database_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,23 @@ import (
"google.golang.org/api/sqladmin/v1beta4"
)

var sqlDatabaseAuthorizedNetWorkSchemaElem *schema.Resource = &schema.Resource{
Schema: map[string]*schema.Schema{
"expiration_time": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"value": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
},
}

func resourceSqlDatabaseInstance() *schema.Resource {
return &schema.Resource{
Create: resourceSqlDatabaseInstanceCreate,
Expand Down Expand Up @@ -128,24 +145,10 @@ func resourceSqlDatabaseInstance() *schema.Resource {
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"authorized_networks": &schema.Schema{
Type: schema.TypeList,
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"expiration_time": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"value": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
},
},
Set: schema.HashResource(sqlDatabaseAuthorizedNetWorkSchemaElem),
Elem: sqlDatabaseAuthorizedNetWorkSchemaElem,
},
"ipv4_enabled": &schema.Schema{
Type: schema.TypeBool,
Expand Down Expand Up @@ -441,46 +444,7 @@ func resourceSqlDatabaseInstanceCreate(d *schema.ResourceData, meta interface{})
}
}

if v, ok := _settings["ip_configuration"]; ok {
_ipConfigurationList := v.([]interface{})

if len(_ipConfigurationList) == 1 && _ipConfigurationList[0] != nil {
settings.IpConfiguration = &sqladmin.IpConfiguration{}
_ipConfiguration := _ipConfigurationList[0].(map[string]interface{})

if vp, okp := _ipConfiguration["ipv4_enabled"]; okp {
settings.IpConfiguration.Ipv4Enabled = vp.(bool)
}

if vp, okp := _ipConfiguration["require_ssl"]; okp {
settings.IpConfiguration.RequireSsl = vp.(bool)
}

if vp, okp := _ipConfiguration["authorized_networks"]; okp {
settings.IpConfiguration.AuthorizedNetworks = make([]*sqladmin.AclEntry, 0)
_authorizedNetworksList := vp.([]interface{})
for _, _acl := range _authorizedNetworksList {
_entry := _acl.(map[string]interface{})
entry := &sqladmin.AclEntry{}

if vpp, okpp := _entry["expiration_time"]; okpp {
entry.ExpirationTime = vpp.(string)
}

if vpp, okpp := _entry["name"]; okpp {
entry.Name = vpp.(string)
}

if vpp, okpp := _entry["value"]; okpp {
entry.Value = vpp.(string)
}

settings.IpConfiguration.AuthorizedNetworks = append(
settings.IpConfiguration.AuthorizedNetworks, entry)
}
}
}
}
settings.IpConfiguration = expandIpConfiguration(_settings["ip_configuration"].([]interface{}))

if v, ok := _settings["location_preference"]; ok {
_locationPreferenceList := v.([]interface{})
Expand Down Expand Up @@ -673,13 +637,13 @@ func resourceSqlDatabaseInstanceRead(d *schema.ResourceData, meta interface{}) e
d.Set("connection_name", instance.ConnectionName)

if err := d.Set("settings", flattenSettings(instance.Settings)); err != nil {
log.Printf("[WARN] Failed to set SQL Database Instance Settings")
log.Printf("[WARN] Failed to set SQL Database Instance Settings: %s", err)
}
if err := d.Set("replica_configuration", flattenReplicaConfiguration(instance.ReplicaConfiguration)); err != nil {
log.Printf("[WARN] Failed to set SQL Database Instance Replica Configuration")
log.Printf("[WARN] Failed to set SQL Database Instance Replica Configuration: %s", err)
}
if err := d.Set("ip_address", flattenIpAddresses(instance.IpAddresses)); err != nil {
log.Printf("[WARN] Failed to set SQL Database Instance IP Addresses")
log.Printf("[WARN] Failed to set SQL Database Instance IP Addresses: %s", err)
}

d.Set("master_instance_name", strings.TrimPrefix(instance.MasterInstanceName, project+":"))
Expand All @@ -698,8 +662,6 @@ func resourceSqlDatabaseInstanceUpdate(d *schema.ResourceData, meta interface{})
return err
}

d.Partial(true)

instance, err := config.clientSqlAdmin.Instances.Get(project,
d.Get("name").(string)).Do()

Expand Down Expand Up @@ -814,78 +776,7 @@ func resourceSqlDatabaseInstanceUpdate(d *schema.ResourceData, meta interface{})
}
}

if v, ok := _settings["ip_configuration"]; ok {
_ipConfigurationList := v.([]interface{})

settings.IpConfiguration = &sqladmin.IpConfiguration{}
if len(_ipConfigurationList) == 1 && _ipConfigurationList[0] != nil {
_ipConfiguration := _ipConfigurationList[0].(map[string]interface{})

if vp, okp := _ipConfiguration["ipv4_enabled"]; okp {
settings.IpConfiguration.Ipv4Enabled = vp.(bool)
}

if vp, okp := _ipConfiguration["require_ssl"]; okp {
settings.IpConfiguration.RequireSsl = vp.(bool)
}

_oldAuthorizedNetworkList := make([]interface{}, 0)
if ov, ook := _o["ip_configuration"]; ook {
_oldIpConfList := ov.([]interface{})
if len(_oldIpConfList) > 0 {
_oldIpConf := _oldIpConfList[0].(map[string]interface{})
if ovp, ookp := _oldIpConf["authorized_networks"]; ookp {
_oldAuthorizedNetworkList = ovp.([]interface{})
}
}
}

if vp, okp := _ipConfiguration["authorized_networks"]; okp || len(_oldAuthorizedNetworkList) > 0 {
oldAuthorizedNetworks := instance.Settings.IpConfiguration.AuthorizedNetworks
settings.IpConfiguration.AuthorizedNetworks = make([]*sqladmin.AclEntry, 0)

_authorizedNetworksList := make([]interface{}, 0)
if vp != nil {
_authorizedNetworksList = vp.([]interface{})
}
_oipc_map := make(map[string]interface{})
for _, _ipc := range _oldAuthorizedNetworkList {
_entry := _ipc.(map[string]interface{})
_oipc_map[_entry["value"].(string)] = true
}
// Next read the network tuples from the server, and reinsert those that
// were not previously defined
for _, entry := range oldAuthorizedNetworks {
_, ok_old := _oipc_map[entry.Value]
if !ok_old {
settings.IpConfiguration.AuthorizedNetworks = append(
settings.IpConfiguration.AuthorizedNetworks, entry)
}
}
// finally, update old entries and insert new ones
// and are still defined.
for _, _ipc := range _authorizedNetworksList {
_entry := _ipc.(map[string]interface{})
entry := &sqladmin.AclEntry{}

if vpp, okpp := _entry["expiration_time"]; okpp {
entry.ExpirationTime = vpp.(string)
}

if vpp, okpp := _entry["name"]; okpp {
entry.Name = vpp.(string)
}

if vpp, okpp := _entry["value"]; okpp {
entry.Value = vpp.(string)
}

settings.IpConfiguration.AuthorizedNetworks = append(
settings.IpConfiguration.AuthorizedNetworks, entry)
}
}
}
}
settings.IpConfiguration = expandIpConfiguration(_settings["ip_configuration"].([]interface{}))

if v, ok := _settings["location_preference"]; ok {
_locationPreferenceList := v.([]interface{})
Expand Down Expand Up @@ -938,8 +829,6 @@ func resourceSqlDatabaseInstanceUpdate(d *schema.ResourceData, meta interface{})
instance.Settings = settings
}

d.Partial(false)

op, err := config.clientSqlAdmin.Instances.Update(project, instance.Name, instance).Do()
if err != nil {
return fmt.Errorf("Error, failed to update instance %s: %s", instance.Name, err)
Expand Down Expand Up @@ -1049,8 +938,21 @@ func flattenIpConfiguration(ipConfiguration *sqladmin.IpConfiguration) interface
return []map[string]interface{}{data}
}

func flattenAuthorizedNetworks(entries []*sqladmin.AclEntry) interface{} {
networks := make([]map[string]interface{}, 0, len(entries))
func expandIpConfiguration(configured []interface{}) *sqladmin.IpConfiguration {
if len(configured) == 0 {
return &sqladmin.IpConfiguration{}
}

ipConfig := configured[0].(map[string]interface{})
return &sqladmin.IpConfiguration{
Ipv4Enabled: ipConfig["ipv4_enabled"].(bool),
RequireSsl: ipConfig["require_ssl"].(bool),
AuthorizedNetworks: expandAuthorizedNetworks(ipConfig["authorized_networks"].(*schema.Set)),
}
}

func flattenAuthorizedNetworks(entries []*sqladmin.AclEntry) *schema.Set {
networks := make([]interface{}, 0, len(entries))

for _, entry := range entries {
data := map[string]interface{}{
Expand All @@ -1062,7 +964,21 @@ func flattenAuthorizedNetworks(entries []*sqladmin.AclEntry) interface{} {
networks = append(networks, data)
}

return networks
return schema.NewSet(schema.HashResource(sqlDatabaseAuthorizedNetWorkSchemaElem), networks)
}

func expandAuthorizedNetworks(configured *schema.Set) []*sqladmin.AclEntry {
entries := make([]*sqladmin.AclEntry, 0)
for _, authNetworkConfig := range configured.List() {
entry := authNetworkConfig.(map[string]interface{})

entries = append(entries, &sqladmin.AclEntry{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this resource were ForceNew I think this (putting everything in the struct instead of checking if it's set first) would work, but since it's updatable you'll need ForceSendFields in order to be able to change values from non-empty to empty. But then by supplying ForceSendFields, you're insisting that each value has a default of empty, and I haven't checked whether that's actually true. If it is, can you add a comment as to why this is ok? (likewise with expandIpConfiguration)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok the actual more important comment I meant to make here: If any of these aren't set this will panic because you can't type-assert nil to a string.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, check me on that. I'm not sure whether it'll be nil or the zero value.

ExpirationTime: entry["expiration_time"].(string),
Name: entry["name"].(string),
Value: entry["value"].(string),
})
}
return entries
}

func flattenLocationPreference(locationPreference *sqladmin.LocationPreference) interface{} {
Expand Down