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 to a Set #733

Merged
merged 1 commit into from
Nov 13, 2017
Merged
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
47 changes: 25 additions & 22 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),
Copy link
Contributor

Choose a reason for hiding this comment

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

This is actually the default behavior, so you don't need this line (I'm fine either way if you want to leave it in to be explicit).

I do wonder whether it actually makes more sense to hash on something a bit more like a primary key, like the network value. Even though our update logic will be the same no matter which part of it changes, it's possible that someone could get confused if they see in a plan that the whole thing is changing rather than just the part of it they changed. What do you think?

Elem: sqlDatabaseAuthorizedNetWorkSchemaElem,
},
"ipv4_enabled": &schema.Schema{
Type: schema.TypeBool,
Expand Down Expand Up @@ -458,7 +461,7 @@ func resourceSqlDatabaseInstanceCreate(d *schema.ResourceData, meta interface{})

if vp, okp := _ipConfiguration["authorized_networks"]; okp {
settings.IpConfiguration.AuthorizedNetworks = make([]*sqladmin.AclEntry, 0)
_authorizedNetworksList := vp.([]interface{})
_authorizedNetworksList := vp.(*schema.Set).List()
for _, _acl := range _authorizedNetworksList {
_entry := _acl.(map[string]interface{})
entry := &sqladmin.AclEntry{}
Expand Down Expand Up @@ -835,7 +838,7 @@ func resourceSqlDatabaseInstanceUpdate(d *schema.ResourceData, meta interface{})
if len(_oldIpConfList) > 0 {
_oldIpConf := _oldIpConfList[0].(map[string]interface{})
if ovp, ookp := _oldIpConf["authorized_networks"]; ookp {
_oldAuthorizedNetworkList = ovp.([]interface{})
_oldAuthorizedNetworkList = ovp.(*schema.Set).List()
}
}
}
Expand All @@ -846,7 +849,7 @@ func resourceSqlDatabaseInstanceUpdate(d *schema.ResourceData, meta interface{})

_authorizedNetworksList := make([]interface{}, 0)
if vp != nil {
_authorizedNetworksList = vp.([]interface{})
_authorizedNetworksList = vp.(*schema.Set).List()
}
_oipc_map := make(map[string]interface{})
for _, _ipc := range _oldAuthorizedNetworkList {
Expand Down Expand Up @@ -1050,7 +1053,7 @@ func flattenIpConfiguration(ipConfiguration *sqladmin.IpConfiguration) interface
}

func flattenAuthorizedNetworks(entries []*sqladmin.AclEntry) interface{} {
networks := make([]map[string]interface{}, 0, len(entries))
networks := schema.NewSet(schema.HashResource(sqlDatabaseAuthorizedNetWorkSchemaElem), []interface{}{})

for _, entry := range entries {
data := map[string]interface{}{
Expand All @@ -1059,7 +1062,7 @@ func flattenAuthorizedNetworks(entries []*sqladmin.AclEntry) interface{} {
"value": entry.Value,
}

networks = append(networks, data)
networks.Add(data)
}

return networks
Expand Down