Skip to content

Commit

Permalink
Merge pull request #301 from mcbenjemaa/feature/mysql-public-net
Browse files Browse the repository at this point in the history
[MySQLServer] Make forProvider.publicNetworkAccess effective
  • Loading branch information
ulucinar authored Nov 18, 2021
2 parents a50aa1a + 58695f7 commit bd13ec7
Show file tree
Hide file tree
Showing 2 changed files with 169 additions and 19 deletions.
48 changes: 29 additions & 19 deletions pkg/clients/database/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,13 @@ func toMySQLProperties(s v1beta1.SQLServerParameters, adminPassword string) mysq
switch createMode {
case azuredbv1beta1.CreateModePointInTimeRestore:
return &mysql.ServerPropertiesForRestore{
MinimalTLSVersion: mysql.MinimalTLSVersionEnum(s.MinimalTLSVersion),
Version: mysql.ServerVersion(s.Version),
SslEnforcement: mysql.SslEnforcementEnum(s.SSLEnforcement),
CreateMode: mysql.CreateModePointInTimeRestore,
RestorePointInTime: safeDate(s.RestorePointInTime),
SourceServerID: s.SourceServerID,
MinimalTLSVersion: mysql.MinimalTLSVersionEnum(s.MinimalTLSVersion),
Version: mysql.ServerVersion(s.Version),
SslEnforcement: mysql.SslEnforcementEnum(s.SSLEnforcement),
CreateMode: mysql.CreateModePointInTimeRestore,
RestorePointInTime: safeDate(s.RestorePointInTime),
SourceServerID: s.SourceServerID,
PublicNetworkAccess: mysql.PublicNetworkAccessEnum(azure.ToString(s.PublicNetworkAccess)),
StorageProfile: &mysql.StorageProfile{
BackupRetentionDays: azure.ToInt32PtrFromIntPtr(s.StorageProfile.BackupRetentionDays),
GeoRedundantBackup: mysql.GeoRedundantBackup(azure.ToString(s.StorageProfile.GeoRedundantBackup)),
Expand All @@ -104,11 +105,12 @@ func toMySQLProperties(s v1beta1.SQLServerParameters, adminPassword string) mysq
}
case azuredbv1beta1.CreateModeGeoRestore:
return &mysql.ServerPropertiesForGeoRestore{
MinimalTLSVersion: mysql.MinimalTLSVersionEnum(s.MinimalTLSVersion),
Version: mysql.ServerVersion(s.Version),
SslEnforcement: mysql.SslEnforcementEnum(s.SSLEnforcement),
CreateMode: mysql.CreateModeGeoRestore,
SourceServerID: s.SourceServerID,
MinimalTLSVersion: mysql.MinimalTLSVersionEnum(s.MinimalTLSVersion),
Version: mysql.ServerVersion(s.Version),
SslEnforcement: mysql.SslEnforcementEnum(s.SSLEnforcement),
CreateMode: mysql.CreateModeGeoRestore,
SourceServerID: s.SourceServerID,
PublicNetworkAccess: mysql.PublicNetworkAccessEnum(azure.ToString(s.PublicNetworkAccess)),
StorageProfile: &mysql.StorageProfile{
BackupRetentionDays: azure.ToInt32PtrFromIntPtr(s.StorageProfile.BackupRetentionDays),
GeoRedundantBackup: mysql.GeoRedundantBackup(azure.ToString(s.StorageProfile.GeoRedundantBackup)),
Expand All @@ -118,11 +120,12 @@ func toMySQLProperties(s v1beta1.SQLServerParameters, adminPassword string) mysq
}
case azuredbv1beta1.CreateModeReplica:
return &mysql.ServerPropertiesForReplica{
MinimalTLSVersion: mysql.MinimalTLSVersionEnum(s.MinimalTLSVersion),
Version: mysql.ServerVersion(s.Version),
SslEnforcement: mysql.SslEnforcementEnum(s.SSLEnforcement),
CreateMode: mysql.CreateModeReplica,
SourceServerID: s.SourceServerID,
MinimalTLSVersion: mysql.MinimalTLSVersionEnum(s.MinimalTLSVersion),
Version: mysql.ServerVersion(s.Version),
SslEnforcement: mysql.SslEnforcementEnum(s.SSLEnforcement),
CreateMode: mysql.CreateModeReplica,
SourceServerID: s.SourceServerID,
PublicNetworkAccess: mysql.PublicNetworkAccessEnum(azure.ToString(s.PublicNetworkAccess)),
StorageProfile: &mysql.StorageProfile{
BackupRetentionDays: azure.ToInt32PtrFromIntPtr(s.StorageProfile.BackupRetentionDays),
GeoRedundantBackup: mysql.GeoRedundantBackup(azure.ToString(s.StorageProfile.GeoRedundantBackup)),
Expand All @@ -140,6 +143,7 @@ func toMySQLProperties(s v1beta1.SQLServerParameters, adminPassword string) mysq
Version: mysql.ServerVersion(s.Version),
SslEnforcement: mysql.SslEnforcementEnum(s.SSLEnforcement),
CreateMode: mysql.CreateModeDefault,
PublicNetworkAccess: mysql.PublicNetworkAccessEnum(azure.ToString(s.PublicNetworkAccess)),
StorageProfile: &mysql.StorageProfile{
BackupRetentionDays: azure.ToInt32PtrFromIntPtr(s.StorageProfile.BackupRetentionDays),
GeoRedundantBackup: mysql.GeoRedundantBackup(azure.ToString(s.StorageProfile.GeoRedundantBackup)),
Expand Down Expand Up @@ -180,9 +184,10 @@ func (c *MySQLServerClient) UpdateServer(ctx context.Context, cr *azuredbv1beta1
// we don't support that.
s := cr.Spec.ForProvider
properties := &mysql.ServerUpdateParametersProperties{
Version: mysql.ServerVersion(s.Version),
MinimalTLSVersion: mysql.MinimalTLSVersionEnum(s.MinimalTLSVersion),
SslEnforcement: mysql.SslEnforcementEnum(s.SSLEnforcement),
Version: mysql.ServerVersion(s.Version),
MinimalTLSVersion: mysql.MinimalTLSVersionEnum(s.MinimalTLSVersion),
SslEnforcement: mysql.SslEnforcementEnum(s.SSLEnforcement),
PublicNetworkAccess: mysql.PublicNetworkAccessEnum(azure.ToString(s.PublicNetworkAccess)),
StorageProfile: &mysql.StorageProfile{
BackupRetentionDays: azure.ToInt32PtrFromIntPtr(s.StorageProfile.BackupRetentionDays),
GeoRedundantBackup: mysql.GeoRedundantBackup(azure.ToString(s.StorageProfile.GeoRedundantBackup)),
Expand Down Expand Up @@ -321,6 +326,9 @@ func LateInitializeMySQL(p *azuredbv1beta1.SQLServerParameters, in mysql.Server)
if p.SSLEnforcement == "" {
p.SSLEnforcement = string(in.SslEnforcement)
}
if p.PublicNetworkAccess == nil {
p.PublicNetworkAccess = azure.ToStringPtr(string(in.PublicNetworkAccess))
}
}

// IsMySQLUpToDate is used to report whether given mysql.Server is in
Expand Down Expand Up @@ -352,6 +360,8 @@ func IsMySQLUpToDate(p azuredbv1beta1.SQLServerParameters, in mysql.Server) bool
return false
case azure.ToString(p.StorageProfile.StorageAutogrow) != string(in.StorageProfile.StorageAutogrow):
return false
case azure.ToString(p.PublicNetworkAccess) != string(in.PublicNetworkAccess):
return false
}
return true
}
140 changes: 140 additions & 0 deletions pkg/clients/database/mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -443,3 +443,143 @@ func TestMySQLServerFirewallRuleIsUpToDate(t *testing.T) {
})
}
}

func TestIsMysqlUpToDate(t *testing.T) {
type args struct {
p v1beta1.SQLServerParameters
in mysql.Server
}
cases := map[string]struct {
args
want bool
}{
"IsUpToDateWithAllDefault": {
args: args{
p: v1beta1.SQLServerParameters{},
in: mysql.Server{
Sku: &mysql.Sku{},
ServerProperties: &mysql.ServerProperties{
StorageProfile: &mysql.StorageProfile{},
},
},
},
want: true,
},
"IsUpToDate": {
args: args{
p: v1beta1.SQLServerParameters{
MinimalTLSVersion: "TLS1_2",
SSLEnforcement: "Enabled",
Version: "8.0.15",
Tags: map[string]string{
"created_by": "crossplane",
},
SKU: v1beta1.SKU{
Tier: "GeneralPurpose",
Capacity: 2,
Family: "Gen5",
},
PublicNetworkAccess: azure.ToStringPtr("Enabled"),
StorageProfile: v1beta1.StorageProfile{
StorageMB: 20480,
StorageAutogrow: azure.ToStringPtr("Enabled"),
BackupRetentionDays: to.IntPtr(5),
GeoRedundantBackup: azure.ToStringPtr("Disabled"),
},
},
in: mysql.Server{
Tags: map[string]*string{
"created_by": azure.ToStringPtr("crossplane"),
},
Sku: &mysql.Sku{
Tier: mysql.GeneralPurpose,
Capacity: azure.ToInt32Ptr(2),
Family: azure.ToStringPtr("Gen5"),
},
ServerProperties: &mysql.ServerProperties{
Version: "8.0.15",
StorageProfile: &mysql.StorageProfile{
StorageMB: azure.ToInt32Ptr(20480),
StorageAutogrow: mysql.StorageAutogrowEnabled,
BackupRetentionDays: azure.ToInt32Ptr(5),
GeoRedundantBackup: mysql.Disabled,
},
SslEnforcement: mysql.SslEnforcementEnumEnabled,
MinimalTLSVersion: mysql.TLS12,
PublicNetworkAccess: mysql.PublicNetworkAccessEnumEnabled,
},
},
},
want: true,
},
"IsNotUpToDate": {
args: args{
p: v1beta1.SQLServerParameters{
PublicNetworkAccess: azure.ToStringPtr("Disabled"),
},
in: mysql.Server{
Sku: &mysql.Sku{},
ServerProperties: &mysql.ServerProperties{
StorageProfile: &mysql.StorageProfile{},
PublicNetworkAccess: mysql.PublicNetworkAccessEnumEnabled,
},
},
},
want: false,
},
"IsNotUpToDateWithServerWithoutSku": {
args: args{
p: v1beta1.SQLServerParameters{},
in: mysql.Server{
ServerProperties: &mysql.ServerProperties{
StorageProfile: &mysql.StorageProfile{},
},
},
},
want: false,
},
}

for name, tc := range cases {
t.Run(name, func(t *testing.T) {
got := IsMySQLUpToDate(tc.args.p, tc.args.in)
if diff := cmp.Diff(tc.want, got); diff != "" {
t.Errorf("IsMySQLUpToDate(...): -want, +got\n%s", diff)
}
})
}
}

func TestLateInitializeMySQL(t *testing.T) {
type args struct {
p *v1beta1.SQLServerParameters
in mysql.Server
}
cases := map[string]struct {
args
want *v1beta1.SQLServerParameters
}{
"PublicNetworkAccessLateInitialize": {
args: args{
p: &v1beta1.SQLServerParameters{},
in: mysql.Server{
Sku: &mysql.Sku{},
ServerProperties: &mysql.ServerProperties{
PublicNetworkAccess: mysql.PublicNetworkAccessEnumEnabled,
},
},
},
want: &v1beta1.SQLServerParameters{
PublicNetworkAccess: azure.ToStringPtr("Enabled"),
},
},
}
for name, tc := range cases {
t.Run(name, func(t *testing.T) {
LateInitializeMySQL(tc.args.p, tc.args.in)
if diff := cmp.Diff(tc.want, tc.args.p); diff != "" {
t.Errorf("TestLateInitializeMySQL(...): -want, +got\n%s", diff)
}
})
}
}

0 comments on commit bd13ec7

Please sign in to comment.