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

Add test case for SQLInstance location preference #2658

Merged
Show file tree
Hide file tree
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
25 changes: 10 additions & 15 deletions mockgcp/mocksql/sqlinstance.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,9 @@ func (s *sqlInstancesService) Insert(ctx context.Context, req *pb.SqlInstancesIn
fqn := name.String()
now := time.Now()

region := "us-central1"
zone := "us-central1-a"

obj := proto.Clone(req.GetBody()).(*pb.DatabaseInstance)
obj.Name = name.InstanceName
obj.Project = name.Project.ID
obj.Region = region

obj.SelfLink = fmt.Sprintf("https://sqladmin.googleapis.com/sql/v1beta4/projects/%s/instances/%s",
name.Project.ID, name.InstanceName)
Expand All @@ -112,8 +108,6 @@ func (s *sqlInstancesService) Insert(ctx context.Context, req *pb.SqlInstancesIn
return nil, err
}

obj.GceZone = zone

// By default, allocate a public IP for the instance.
shouldAllocatePublicIP := true
// By default, do not allocate a private IP for the instance.
Expand Down Expand Up @@ -172,7 +166,9 @@ func (s *sqlInstancesService) Insert(ctx context.Context, req *pb.SqlInstancesIn

obj.ServiceAccountEmailAddress = fmt.Sprintf("p%[email protected]", name.Project.Number)

populateDefaults(obj, zone)
populateDefaults(obj)

obj.GceZone = obj.Settings.LocationPreference.Zone

obj.Settings.SettingsVersion = wrapperspb.Int64(1)

Expand Down Expand Up @@ -510,7 +506,7 @@ func setDatabaseVersionDefaults(obj *pb.DatabaseInstance) error {
return nil
}

func populateDefaults(obj *pb.DatabaseInstance, zone string) {
func populateDefaults(obj *pb.DatabaseInstance) {
if obj.InstanceType == pb.SqlInstanceType_SQL_INSTANCE_TYPE_UNSPECIFIED {
obj.InstanceType = pb.SqlInstanceType_CLOUD_SQL_INSTANCE
}
Expand Down Expand Up @@ -575,13 +571,12 @@ func populateDefaults(obj *pb.DatabaseInstance, zone string) {
}
}

locationPreference := settings.LocationPreference
if locationPreference == nil {
locationPreference = &pb.LocationPreference{}
settings.LocationPreference = locationPreference
if settings.LocationPreference == nil {
settings.LocationPreference = &pb.LocationPreference{
Kind: "sql#locationPreference",
Zone: obj.Region + "-a",
}
}
locationPreference.Kind = "sql#locationPreference"
locationPreference.Zone = zone

backupConfiguration := settings.BackupConfiguration
if backupConfiguration == nil {
Expand Down Expand Up @@ -739,7 +734,7 @@ func (s *sqlInstancesService) Update(ctx context.Context, req *pb.SqlInstancesUp
obj.State = existing.State
obj.UpgradableDatabaseVersions = existing.UpgradableDatabaseVersions

populateDefaults(obj, existing.GetSettings().GetLocationPreference().GetZone())
populateDefaults(obj)

obj.Settings.SettingsVersion = wrapperspb.Int64(existing.GetSettings().GetSettingsVersion().GetValue() + 1)

Expand Down
151 changes: 83 additions & 68 deletions pkg/controller/direct/sql/mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,20 +309,7 @@ func SQLInstanceKRMToGCP(in *krm.SQLInstance, refs *SQLInstanceInternalRefs) (*a
}

out.Settings.IpConfiguration = InstanceIpConfigurationKRMToGCP(in.Spec.Settings.IpConfiguration, refs)

if in.Spec.Settings.LocationPreference != nil {
out.Settings.LocationPreference = &api.LocationPreference{}
if in.Spec.Settings.LocationPreference.FollowGaeApplication != nil {
// todo: deprecated
out.Settings.LocationPreference.FollowGaeApplication = *in.Spec.Settings.LocationPreference.FollowGaeApplication
}
if in.Spec.Settings.LocationPreference.SecondaryZone != nil {
out.Settings.LocationPreference.SecondaryZone = *in.Spec.Settings.LocationPreference.SecondaryZone
}
if in.Spec.Settings.LocationPreference.Zone != nil {
out.Settings.LocationPreference.Zone = *in.Spec.Settings.LocationPreference.Zone
}
}
out.Settings.LocationPreference = InstanceLocationPreferenceKRMToGCP(in.Spec.Settings.LocationPreference)

if in.Spec.Settings.MaintenanceWindow != nil {
out.Settings.MaintenanceWindow = &api.MaintenanceWindow{}
Expand Down Expand Up @@ -479,6 +466,21 @@ func InstancePscConfigKRMToGCP(in []krm.InstancePscConfig) *api.PscConfig {
return out
}

func InstanceLocationPreferenceKRMToGCP(in *krm.InstanceLocationPreference) *api.LocationPreference {
if in == nil {
return nil
}

out := &api.LocationPreference{
Kind: "sql#locationPreference",
FollowGaeApplication: direct.ValueOf(in.FollowGaeApplication),
SecondaryZone: direct.ValueOf(in.SecondaryZone),
Zone: direct.ValueOf(in.Zone),
}

return out
}

func SQLInstanceGCPToKRM(in *api.DatabaseInstance) (*krm.SQLInstance, error) {
out := &krm.SQLInstance{}

Expand Down Expand Up @@ -644,60 +646,8 @@ func SQLInstanceGCPToKRM(in *api.DatabaseInstance) (*krm.SQLInstance, error) {
}
}

if in.Settings.IpConfiguration != nil {
ic := &krm.InstanceIpConfiguration{}

if in.Settings.IpConfiguration.AllocatedIpRange != "" {
ic.AllocatedIpRange = &in.Settings.IpConfiguration.AllocatedIpRange
}

if in.Settings.IpConfiguration.AuthorizedNetworks != nil {
ans := []krm.InstanceAuthorizedNetworks{}
for _, an := range in.Settings.IpConfiguration.AuthorizedNetworks {
ans = append(ans, krm.InstanceAuthorizedNetworks{
ExpirationTime: &an.ExpirationTime,
Name: &an.Name,
Value: an.Value,
})
}
ic.AuthorizedNetworks = ans
}

ic.EnablePrivatePathForGoogleCloudServices = &in.Settings.IpConfiguration.EnablePrivatePathForGoogleCloudServices

ic.Ipv4Enabled = &in.Settings.IpConfiguration.Ipv4Enabled

if in.Settings.IpConfiguration.PrivateNetwork != "" {
ic.PrivateNetworkRef = &refs.ComputeNetworkRef{
External: in.Settings.IpConfiguration.PrivateNetwork,
}
}

if in.Settings.IpConfiguration.PscConfig != nil {
out.Spec.Settings.IpConfiguration.PscConfig = []krm.InstancePscConfig{
{
AllowedConsumerProjects: in.Settings.IpConfiguration.PscConfig.AllowedConsumerProjects,
PscEnabled: &in.Settings.IpConfiguration.PscConfig.PscEnabled,
},
}
}

ic.RequireSsl = &in.Settings.IpConfiguration.RequireSsl

if in.Settings.IpConfiguration.SslMode != "" {
ic.SslMode = &in.Settings.IpConfiguration.SslMode
}

out.Spec.Settings.IpConfiguration = ic
}

if in.Settings.LocationPreference != nil {
out.Spec.Settings.LocationPreference = &krm.InstanceLocationPreference{
FollowGaeApplication: &in.Settings.LocationPreference.FollowGaeApplication,
SecondaryZone: &in.Settings.LocationPreference.SecondaryZone,
Zone: &in.Settings.LocationPreference.Zone,
}
}
out.Spec.Settings.IpConfiguration = InstanceIpConfigurationGCPToKRM(in.Settings.IpConfiguration)
out.Spec.Settings.LocationPreference = InstanceLocationPreferenceGCPToKRM(in.Settings.LocationPreference)

if in.Settings.MaintenanceWindow != nil {
out.Spec.Settings.MaintenanceWindow = &krm.InstanceMaintenanceWindow{
Expand Down Expand Up @@ -749,6 +699,71 @@ func SQLInstanceGCPToKRM(in *api.DatabaseInstance) (*krm.SQLInstance, error) {
return out, nil
}

func InstanceIpConfigurationGCPToKRM(in *api.IpConfiguration) *krm.InstanceIpConfiguration {
if in == nil {
return nil
}

out := &krm.InstanceIpConfiguration{
AllocatedIpRange: direct.LazyPtr(in.AllocatedIpRange),
AuthorizedNetworks: InstanceAuthorizedNetworksGCPToKRM(in.AuthorizedNetworks),
EnablePrivatePathForGoogleCloudServices: direct.LazyPtr(in.EnablePrivatePathForGoogleCloudServices),
Ipv4Enabled: direct.LazyPtr(in.Ipv4Enabled),
PscConfig: InstancePscConfigGCPToKRM(in.PscConfig),
RequireSsl: direct.LazyPtr(in.RequireSsl),
SslMode: direct.LazyPtr(in.SslMode),
}

if in.PrivateNetwork != "" {
out.PrivateNetworkRef = &refs.ComputeNetworkRef{
External: in.PrivateNetwork,
}
}

return out
}

func InstanceAuthorizedNetworksGCPToKRM(in []*api.AclEntry) []krm.InstanceAuthorizedNetworks {
out := []krm.InstanceAuthorizedNetworks{}
for _, net := range in {
out = append(out, krm.InstanceAuthorizedNetworks{
ExpirationTime: direct.LazyPtr(net.ExpirationTime),
Name: direct.LazyPtr(net.Name),
Value: net.Value,
})
}
return out
}

func InstancePscConfigGCPToKRM(in *api.PscConfig) []krm.InstancePscConfig {
if in == nil {
return nil
}

out := []krm.InstancePscConfig{
{
AllowedConsumerProjects: in.AllowedConsumerProjects,
PscEnabled: direct.LazyPtr(in.PscEnabled),
},
}

return out
}

func InstanceLocationPreferenceGCPToKRM(in *api.LocationPreference) *krm.InstanceLocationPreference {
if in == nil {
return nil
}

out := &krm.InstanceLocationPreference{
FollowGaeApplication: direct.LazyPtr(in.FollowGaeApplication),
SecondaryZone: direct.LazyPtr(in.SecondaryZone),
Zone: direct.LazyPtr(in.Zone),
}

return out
}

func Convert_SQLInstance_API_v1_To_KRM_status(in *api.DatabaseInstance, out *krm.SQLInstanceStatus) error {
if in == nil {
return fmt.Errorf("cannot convert nil DatabaseInstance")
Expand Down
Loading
Loading