sdk:cloud.google.com/go/cloudsqlconn v1.11.1
创建或更新数据库实例设置Ipv4Enabled为false时无效,不管设置true或false,最终的效果都是true。
我们debug代码的过程中发现,当Ipv4Enabled设置为false时,序列化后会忽略该字段,并且当实例创建出来后,我们使用get获取实例信息,该字段的值是true。从控制台看也是如此。
当Ipv4Enabled设置为true的时候:
序列化后:
当Ipv4Enabled设置为false的时候:
序列化后:
案例代码:
operation, err := sqlSvc.Instances.Insert(s.project, &sqladmin.DatabaseInstance{
Kind: "sql#instance",
DatabaseVersion: req.MysqlVersion,
InstanceType: "CLOUD_SQL_INSTANCE",
Name: req.InstanceName,
RootPassword: getDefaultRootPassword(session),
Region: "us-central1",
Settings: &sqladmin.Settings{
Kind: "sql#settings",
AvailabilityType: availabilityType,
DataDiskSizeGb: req.StorageCapacity,
DataDiskType: "PD_SSD",
Tier: req.MachineShapes,
IpConfiguration: &sqladmin.IpConfiguration{
Ipv4Enabled: req.EnablePublicIPAddress,
PrivateNetwork: s.vpc,
AuthorizedNetworks: authorizedNetworks,
EnablePrivatePathForGoogleCloudServices: true,
},
BackupConfiguration: &sqladmin.BackupConfiguration{
Kind: "sql#backupConfiguration",
BinaryLogEnabled: true,
Enabled: true,
StartTime: "12:00",
},
UserLabels: session.Extend,
},
}).Context(ctx).Do()
详情请看Issue:https://github.com/GoogleCloudPlatform/cloud-sql-go-connector/issues/847
解决方案:
使用ForceSendFields,例如ForceSendFields: []string{"Ipv4Enabled"}
,需要注意的是:指定的是go结构体的字段名,而不是json的字段名,所以是大写的Ipv4Enabled,而不是ipv4Enabled。
案例代码:
operation, err := sqlSvc.Instances.Insert(s.project, &sqladmin.DatabaseInstance{
Kind: "sql#instance",
DatabaseVersion: req.MysqlVersion,
InstanceType: "CLOUD_SQL_INSTANCE",
Name: req.InstanceName,
RootPassword: getDefaultRootPassword(session),
Region: "us-central1",
Settings: &sqladmin.Settings{
Kind: "sql#settings",
AvailabilityType: availabilityType,
DataDiskSizeGb: req.StorageCapacity,
DataDiskType: "PD_SSD",
Tier: req.MachineShapes,
IpConfiguration: &sqladmin.IpConfiguration{
Ipv4Enabled: req.EnablePublicIPAddress,
PrivateNetwork: s.vpc,
AuthorizedNetworks: authorizedNetworks,
EnablePrivatePathForGoogleCloudServices: true,
ForceSendFields: []string{"Ipv4Enabled"},
},
BackupConfiguration: &sqladmin.BackupConfiguration{
Kind: "sql#backupConfiguration",
BinaryLogEnabled: true,
Enabled: true,
StartTime: "12:00",
},
UserLabels: session.Extend,
},
}).Context(ctx).Do()
相关文档: