@@ -528,6 +528,8 @@ func expandSqlDatabaseInstanceSettings(configured []interface{}, secondGen bool)
528
528
529
529
_settings := configured [0 ].(map [string ]interface {})
530
530
settings := & sqladmin.Settings {
531
+ // Version is unset in Create but is set during update
532
+ SettingsVersion : int64 (_settings ["version" ].(int )),
531
533
Tier : _settings ["tier" ].(string ),
532
534
ForceSendFields : []string {"StorageAutoResize" },
533
535
ActivationPolicy : _settings ["activation_policy" ].(string ),
@@ -701,10 +703,7 @@ func resourceSqlDatabaseInstanceRead(d *schema.ResourceData, meta interface{}) e
701
703
}
702
704
703
705
if len (ipAddresses ) > 0 {
704
- firstIpAddress := ipAddresses [0 ]["ip_address" ]
705
- if err := d .Set ("first_ip_address" , firstIpAddress ); err != nil {
706
- log .Printf ("[WARN] Failed to set SQL Database Instance First IP Address" )
707
- }
706
+ d .Set ("first_ip_address" , ipAddresses [0 ]["ip_address" ])
708
707
}
709
708
710
709
if err := d .Set ("server_ca_cert" , flattenServerCaCert (instance .ServerCaCert )); err != nil {
@@ -727,271 +726,21 @@ func resourceSqlDatabaseInstanceUpdate(d *schema.ResourceData, meta interface{})
727
726
return err
728
727
}
729
728
730
- d .Partial (true )
731
-
732
- instance , err := config .clientSqlAdmin .Instances .Get (project ,
733
- d .Get ("name" ).(string )).Do ()
734
-
735
- if err != nil {
736
- return fmt .Errorf ("Error retrieving instance %s: %s" ,
737
- d .Get ("name" ).(string ), err )
738
- }
739
-
740
- if d .HasChange ("settings" ) {
741
- _oListCast , _settingsListCast := d .GetChange ("settings" )
742
- _oList := _oListCast .([]interface {})
743
- _o := _oList [0 ].(map [string ]interface {})
744
- _settingsList := _settingsListCast .([]interface {})
745
-
746
- _settings := _settingsList [0 ].(map [string ]interface {})
747
-
748
- settings := & sqladmin.Settings {
749
- Tier : _settings ["tier" ].(string ),
750
- SettingsVersion : instance .Settings .SettingsVersion ,
751
- ForceSendFields : []string {"StorageAutoResize" },
752
- }
753
-
754
- if ! isFirstGen (d ) {
755
- autoResize := _settings ["disk_autoresize" ].(bool )
756
- settings .StorageAutoResize = & autoResize
757
- }
758
-
759
- if v , ok := _settings ["activation_policy" ]; ok {
760
- settings .ActivationPolicy = v .(string )
761
- }
762
-
763
- if v , ok := _settings ["authorized_gae_applications" ]; ok {
764
- settings .AuthorizedGaeApplications = make ([]string , 0 )
765
- for _ , app := range v .([]interface {}) {
766
- settings .AuthorizedGaeApplications = append (settings .AuthorizedGaeApplications ,
767
- app .(string ))
768
- }
769
- }
770
-
771
- if v , ok := _settings ["availability_type" ]; ok {
772
- settings .AvailabilityType = v .(string )
773
- }
774
-
775
- if v , ok := _settings ["backup_configuration" ]; ok {
776
- _backupConfigurationList := v .([]interface {})
777
-
778
- settings .BackupConfiguration = & sqladmin.BackupConfiguration {}
779
- if len (_backupConfigurationList ) == 1 && _backupConfigurationList [0 ] != nil {
780
- _backupConfiguration := _backupConfigurationList [0 ].(map [string ]interface {})
781
-
782
- if vp , okp := _backupConfiguration ["binary_log_enabled" ]; okp {
783
- settings .BackupConfiguration .BinaryLogEnabled = vp .(bool )
784
- }
785
-
786
- if vp , okp := _backupConfiguration ["enabled" ]; okp {
787
- settings .BackupConfiguration .Enabled = vp .(bool )
788
- }
789
-
790
- if vp , okp := _backupConfiguration ["start_time" ]; okp {
791
- settings .BackupConfiguration .StartTime = vp .(string )
792
- }
793
- }
794
- }
795
-
796
- if v , ok := _settings ["crash_safe_replication" ]; ok {
797
- settings .CrashSafeReplicationEnabled = v .(bool )
798
- }
799
-
800
- if v , ok := _settings ["disk_size" ]; ok {
801
- if v .(int ) > 0 && int64 (v .(int )) > instance .Settings .DataDiskSizeGb {
802
- settings .DataDiskSizeGb = int64 (v .(int ))
803
- }
804
- }
805
-
806
- if v , ok := _settings ["disk_type" ]; ok && len (v .(string )) > 0 {
807
- settings .DataDiskType = v .(string )
808
- }
809
-
810
- _oldDatabaseFlags := make ([]interface {}, 0 )
811
- if ov , ook := _o ["database_flags" ]; ook {
812
- _oldDatabaseFlags = ov .([]interface {})
813
- }
814
-
815
- if v , ok := _settings ["database_flags" ]; ok || len (_oldDatabaseFlags ) > 0 {
816
- oldDatabaseFlags := settings .DatabaseFlags
817
- settings .DatabaseFlags = make ([]* sqladmin.DatabaseFlags , 0 )
818
- _databaseFlagsList := make ([]interface {}, 0 )
819
- if v != nil {
820
- _databaseFlagsList = v .([]interface {})
821
- }
822
-
823
- _odbf_map := make (map [string ]interface {})
824
- for _ , _dbf := range _oldDatabaseFlags {
825
- _entry := _dbf .(map [string ]interface {})
826
- _odbf_map [_entry ["name" ].(string )] = true
827
- }
828
-
829
- // First read the flags from the server, and reinsert those that
830
- // were not previously defined
831
- for _ , entry := range oldDatabaseFlags {
832
- _ , ok_old := _odbf_map [entry .Name ]
833
- if ! ok_old {
834
- settings .DatabaseFlags = append (
835
- settings .DatabaseFlags , entry )
836
- }
837
- }
838
- // finally, insert only those that were previously defined
839
- // and are still defined.
840
- for _ , _flag := range _databaseFlagsList {
841
- _entry := _flag .(map [string ]interface {})
842
- flag := & sqladmin.DatabaseFlags {}
843
- if vp , okp := _entry ["name" ]; okp {
844
- flag .Name = vp .(string )
845
- }
846
-
847
- if vp , okp := _entry ["value" ]; okp {
848
- flag .Value = vp .(string )
849
- }
850
-
851
- settings .DatabaseFlags = append (settings .DatabaseFlags , flag )
852
- }
853
- }
854
-
855
- if v , ok := _settings ["ip_configuration" ]; ok {
856
- _ipConfigurationList := v .([]interface {})
857
-
858
- settings .IpConfiguration = & sqladmin.IpConfiguration {}
859
- if len (_ipConfigurationList ) == 1 && _ipConfigurationList [0 ] != nil {
860
- _ipConfiguration := _ipConfigurationList [0 ].(map [string ]interface {})
861
-
862
- if vp , okp := _ipConfiguration ["ipv4_enabled" ]; okp {
863
- settings .IpConfiguration .Ipv4Enabled = vp .(bool )
864
- }
865
-
866
- if vp , okp := _ipConfiguration ["require_ssl" ]; okp {
867
- settings .IpConfiguration .RequireSsl = vp .(bool )
868
- }
869
-
870
- _oldAuthorizedNetworkList := make ([]interface {}, 0 )
871
- if ov , ook := _o ["ip_configuration" ]; ook {
872
- _oldIpConfList := ov .([]interface {})
873
- if len (_oldIpConfList ) > 0 {
874
- _oldIpConf := _oldIpConfList [0 ].(map [string ]interface {})
875
- if ovp , ookp := _oldIpConf ["authorized_networks" ]; ookp {
876
- _oldAuthorizedNetworkList = ovp .(* schema.Set ).List ()
877
- }
878
- }
879
- }
880
-
881
- if vp , okp := _ipConfiguration ["authorized_networks" ]; okp || len (_oldAuthorizedNetworkList ) > 0 {
882
- oldAuthorizedNetworks := instance .Settings .IpConfiguration .AuthorizedNetworks
883
- settings .IpConfiguration .AuthorizedNetworks = make ([]* sqladmin.AclEntry , 0 )
884
-
885
- _authorizedNetworksList := make ([]interface {}, 0 )
886
- if vp != nil {
887
- _authorizedNetworksList = vp .(* schema.Set ).List ()
888
- }
889
- _oipc_map := make (map [string ]interface {})
890
- for _ , _ipc := range _oldAuthorizedNetworkList {
891
- _entry := _ipc .(map [string ]interface {})
892
- _oipc_map [_entry ["value" ].(string )] = true
893
- }
894
- // Next read the network tuples from the server, and reinsert those that
895
- // were not previously defined
896
- for _ , entry := range oldAuthorizedNetworks {
897
- _ , ok_old := _oipc_map [entry .Value ]
898
- if ! ok_old {
899
- settings .IpConfiguration .AuthorizedNetworks = append (
900
- settings .IpConfiguration .AuthorizedNetworks , entry )
901
- }
902
- }
903
- // finally, update old entries and insert new ones
904
- // and are still defined.
905
- for _ , _ipc := range _authorizedNetworksList {
906
- _entry := _ipc .(map [string ]interface {})
907
- entry := & sqladmin.AclEntry {}
908
-
909
- if vpp , okpp := _entry ["expiration_time" ]; okpp {
910
- entry .ExpirationTime = vpp .(string )
911
- }
912
-
913
- if vpp , okpp := _entry ["name" ]; okpp {
914
- entry .Name = vpp .(string )
915
- }
916
-
917
- if vpp , okpp := _entry ["value" ]; okpp {
918
- entry .Value = vpp .(string )
919
- }
920
-
921
- settings .IpConfiguration .AuthorizedNetworks = append (
922
- settings .IpConfiguration .AuthorizedNetworks , entry )
923
- }
924
- }
925
- }
926
- }
927
-
928
- if v , ok := _settings ["location_preference" ]; ok {
929
- _locationPreferenceList := v .([]interface {})
930
-
931
- settings .LocationPreference = & sqladmin.LocationPreference {}
932
- if len (_locationPreferenceList ) == 1 && _locationPreferenceList [0 ] != nil {
933
- _locationPreference := _locationPreferenceList [0 ].(map [string ]interface {})
934
-
935
- if vp , okp := _locationPreference ["follow_gae_application" ]; okp {
936
- settings .LocationPreference .FollowGaeApplication = vp .(string )
937
- }
938
-
939
- if vp , okp := _locationPreference ["zone" ]; okp {
940
- settings .LocationPreference .Zone = vp .(string )
941
- }
942
- }
943
- }
944
-
945
- if v , ok := _settings ["maintenance_window" ]; ok && len (v .([]interface {})) > 0 {
946
- _maintenanceWindowList := v .([]interface {})
947
-
948
- settings .MaintenanceWindow = & sqladmin.MaintenanceWindow {}
949
- if len (_maintenanceWindowList ) == 1 && _maintenanceWindowList [0 ] != nil {
950
- _maintenanceWindow := _maintenanceWindowList [0 ].(map [string ]interface {})
951
-
952
- if vp , okp := _maintenanceWindow ["day" ]; okp {
953
- settings .MaintenanceWindow .Day = int64 (vp .(int ))
954
- }
955
-
956
- if vp , okp := _maintenanceWindow ["hour" ]; okp {
957
- settings .MaintenanceWindow .Hour = int64 (vp .(int ))
958
- }
959
-
960
- if vp , ok := _maintenanceWindow ["update_track" ]; ok {
961
- if len (vp .(string )) > 0 {
962
- settings .MaintenanceWindow .UpdateTrack = vp .(string )
963
- }
964
- }
965
- }
966
- }
967
-
968
- if v , ok := _settings ["pricing_plan" ]; ok {
969
- settings .PricingPlan = v .(string )
970
- }
971
-
972
- if v , ok := _settings ["replication_type" ]; ok {
973
- settings .ReplicationType = v .(string )
974
- }
975
-
976
- if v , ok := _settings ["user_labels" ]; ok {
977
- settings .UserLabels = convertStringMap (v .(map [string ]interface {}))
978
- }
979
-
980
- instance .Settings = settings
729
+ // Update only updates the settings, so they are all we need to set.
730
+ instance := & sqladmin.DatabaseInstance {
731
+ Settings : expandSqlDatabaseInstanceSettings (d .Get ("settings" ).([]interface {}), ! isFirstGen (d )),
981
732
}
982
733
983
- d .Partial (false )
984
-
985
734
// Lock on the master_instance_name just in case updating any replica
986
735
// settings causes operations on the master.
987
736
if v , ok := d .GetOk ("master_instance_name" ); ok {
988
737
mutexKV .Lock (instanceMutexKey (project , v .(string )))
989
738
defer mutexKV .Unlock (instanceMutexKey (project , v .(string )))
990
739
}
991
740
992
- op , err := config .clientSqlAdmin .Instances .Update (project , instance . Name , instance ).Do ()
741
+ op , err := config .clientSqlAdmin .Instances .Update (project , d . Get ( "name" ).( string ) , instance ).Do ()
993
742
if err != nil {
994
- return fmt .Errorf ("Error, failed to update instance %s: %s" , instance .Name , err )
743
+ return fmt .Errorf ("Error, failed to update instance settings for %s: %s" , instance .Name , err )
995
744
}
996
745
997
746
err = sqladminOperationWaitTime (config , op , project , "Update Instance" , int (d .Timeout (schema .TimeoutUpdate ).Minutes ()))
0 commit comments