GoogleCloudPlatform/cloud-sql-go-connector

Creating a database instance with the setting Ipv4Enabled set to false is ineffective.

Closed this issue · 6 comments

Bug Description

When creating or updating a database instance with Ipv4Enabled set to false, it is ineffective; regardless of setting it to true or false, the final outcome is always true.

During the debugging process of our code, we found that when Ipv4Enabled is set to false, the field is ignored after serialization. Additionally, after the instance is created, the value of this field is true when retrieved using a get method. This is also observed from the console.

截屏2024-07-23 17 08 34 截屏2024-07-23 17 09 19

Example code (or command)

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()

Stacktrace

No response

Steps to reproduce?

  1. ?
  2. ?
  3. ?
    ...

Environment

  1. OS type and version: macOS
  2. Go version: 1.22.1
  3. Cloud SQL Go Connector version: v1.11.1

cloud.google.com/go/cloudsqlconn v1.11.1

Additional Details

No response

Hi @wujiuye, thanks for raising an issue on the Cloud SQL Go Connector 😄

Firstly, I believe you may have opened this issue on the wrong library.

This Github repository is for the cloudsqlconn Go library.

From your description and code shared it seems you are having issues with API calls via the sqladmin library.

The proper Github repo to open an issue on would instead be https://github.com/googleapis/google-api-go-client as it holds the code for generating/serialization of the sqladmin library.

I'd recommend open an issue on the proper repo as this may very well be a bug since the docs say that ipv4Enabled should be the toggle for enabling Public IP on an instance. (reference, rest API docs)

image

As Jack said, this is a structural issue with the Golang JSON library and the generated API code. After discussing with @codyoss, The best solution is to use the ForceSend field as described in the google golang api doc. This is the only way to make Go JSON library render the zero-value fields like Ipv4Enabled: False.

Closing this out as answered. @wujiuye let us know if you have any issues with the ForceSendFields 😄

As Jack said, this is a structural issue with the Golang JSON library and the generated API code. After discussing with @codyoss, The best solution is to use the ForceSend field as described in the google golang api doc. This is the only way to make Go JSON library render the zero-value fields like Ipv4Enabled: False.

Thank you for providing the solution. The issue has been resolved using ForceSendFields: []string{"Ipv4Enabled"}.

Awesome! Glad to hear it :)