Skip to content

Commit 1c07021

Browse files
authored
Merge pull request #293 from stone-payments/feature/add-public-access-in-postgresql
Implement spec.publicNetworkAccess for PostgreSQL
2 parents 33f728d + 2cf7d16 commit 1c07021

6 files changed

+195
-22
lines changed

apis/database/v1beta1/sql_types.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,12 @@ type SQLServerParameters struct {
164164

165165
// TODO(hasheddan): support InfrastructureEncryption
166166

167-
// TODO(hasheddan): support PublicNetworkAccess
167+
// PublicNetworkAccess - Whether or not public network access is allowed for
168+
// this server. Value is optional but if passed in,
169+
// must be 'Enabled' or 'Disabled'.
170+
// +kubebuilder:validation:Enum=Enabled;Disabled
171+
// +optional
172+
PublicNetworkAccess *string `json:"publicNetworkAccess,omitempty"`
168173

169174
// CreateMode - Possible values include: 'CreateModeDefault', 'CreateModePointInTimeRestore', 'CreateModeGeoRestore', 'CreateModeReplica'
170175
// +optional
@@ -194,7 +199,7 @@ type SQLServerParameters struct {
194199
}
195200

196201
// CreateMode controls the creation behaviour
197-
// Keep synced with "github.com/Azure/azure-sdk-for-go/services/postgresql/mgmt/2017-12-01/postgresql".MinimalTLSVersionEnum
202+
// Keep synced with "github.com/Azure/azure-sdk-for-go/services/postgresql/mgmt/2017-12-01/postgresql".CreateMode
198203
// +kubebuilder:validation:Enum=Default;GeoRestore;PointInTimeRestore;Replica
199204
type CreateMode string
200205

apis/database/v1beta1/zz_generated.deepcopy.go

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package/crds/database.azure.crossplane.io_mysqlservers.yaml

+6
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@ spec:
7474
minimalTlsVersion:
7575
description: MinimalTLSVersion - control TLS connection policy
7676
type: string
77+
publicNetworkAccess:
78+
description: PublicNetworkAccess - Whether or not public network access is allowed for this server. Value is optional but if passed in, must be 'Enabled' or 'Disabled'.
79+
enum:
80+
- Enabled
81+
- Disabled
82+
type: string
7783
resourceGroupName:
7884
description: ResourceGroupName specifies the name of the resource group that should contain this SQLServer.
7985
type: string

package/crds/database.azure.crossplane.io_postgresqlservers.yaml

+6
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,12 @@ spec:
7474
minimalTlsVersion:
7575
description: MinimalTLSVersion - control TLS connection policy
7676
type: string
77+
publicNetworkAccess:
78+
description: PublicNetworkAccess - Whether or not public network access is allowed for this server. Value is optional but if passed in, must be 'Enabled' or 'Disabled'.
79+
enum:
80+
- Enabled
81+
- Disabled
82+
type: string
7783
resourceGroupName:
7884
description: ResourceGroupName specifies the name of the resource group that should contain this SQLServer.
7985
type: string

pkg/clients/database/postgresql.go

+31-20
Original file line numberDiff line numberDiff line change
@@ -75,18 +75,19 @@ func (c *PostgreSQLServerClient) GetServer(ctx context.Context, cr *azuredbv1bet
7575
return c.ServersClient.Get(ctx, cr.Spec.ForProvider.ResourceGroupName, meta.GetExternalName(cr))
7676
}
7777

78-
// toMySQLProperties converts the CrossPlane ForProvider object to a PostgreSQL Azure properties object
78+
// toPGSQLProperties converts the CrossPlane ForProvider object to a PostgreSQL Azure properties object
7979
func toPGSQLProperties(s v1beta1.SQLServerParameters, adminPassword string) postgresql.BasicServerPropertiesForCreate {
8080
createMode := pointerToCreateMode(s.CreateMode)
8181
switch createMode {
8282
case azuredbv1beta1.CreateModePointInTimeRestore:
8383
return &postgresql.ServerPropertiesForRestore{
84-
MinimalTLSVersion: postgresql.MinimalTLSVersionEnum(s.MinimalTLSVersion),
85-
Version: postgresql.ServerVersion(s.Version),
86-
SslEnforcement: postgresql.SslEnforcementEnum(s.SSLEnforcement),
87-
CreateMode: postgresql.CreateModePointInTimeRestore,
88-
RestorePointInTime: safeDate(s.RestorePointInTime),
89-
SourceServerID: s.SourceServerID,
84+
MinimalTLSVersion: postgresql.MinimalTLSVersionEnum(s.MinimalTLSVersion),
85+
Version: postgresql.ServerVersion(s.Version),
86+
SslEnforcement: postgresql.SslEnforcementEnum(s.SSLEnforcement),
87+
PublicNetworkAccess: postgresql.PublicNetworkAccessEnum(azure.ToString(s.PublicNetworkAccess)),
88+
CreateMode: postgresql.CreateModePointInTimeRestore,
89+
RestorePointInTime: safeDate(s.RestorePointInTime),
90+
SourceServerID: s.SourceServerID,
9091
StorageProfile: &postgresql.StorageProfile{
9192
BackupRetentionDays: azure.ToInt32PtrFromIntPtr(s.StorageProfile.BackupRetentionDays),
9293
GeoRedundantBackup: postgresql.GeoRedundantBackup(azure.ToString(s.StorageProfile.GeoRedundantBackup)),
@@ -96,11 +97,12 @@ func toPGSQLProperties(s v1beta1.SQLServerParameters, adminPassword string) post
9697
}
9798
case azuredbv1beta1.CreateModeGeoRestore:
9899
return &postgresql.ServerPropertiesForGeoRestore{
99-
MinimalTLSVersion: postgresql.MinimalTLSVersionEnum(s.MinimalTLSVersion),
100-
Version: postgresql.ServerVersion(s.Version),
101-
SslEnforcement: postgresql.SslEnforcementEnum(s.SSLEnforcement),
102-
SourceServerID: s.SourceServerID,
103-
CreateMode: postgresql.CreateModeGeoRestore,
100+
MinimalTLSVersion: postgresql.MinimalTLSVersionEnum(s.MinimalTLSVersion),
101+
Version: postgresql.ServerVersion(s.Version),
102+
SslEnforcement: postgresql.SslEnforcementEnum(s.SSLEnforcement),
103+
SourceServerID: s.SourceServerID,
104+
PublicNetworkAccess: postgresql.PublicNetworkAccessEnum(azure.ToString(s.PublicNetworkAccess)),
105+
CreateMode: postgresql.CreateModeGeoRestore,
104106
StorageProfile: &postgresql.StorageProfile{
105107
BackupRetentionDays: azure.ToInt32PtrFromIntPtr(s.StorageProfile.BackupRetentionDays),
106108
GeoRedundantBackup: postgresql.GeoRedundantBackup(azure.ToString(s.StorageProfile.GeoRedundantBackup)),
@@ -110,11 +112,12 @@ func toPGSQLProperties(s v1beta1.SQLServerParameters, adminPassword string) post
110112
}
111113
case azuredbv1beta1.CreateModeReplica:
112114
return &postgresql.ServerPropertiesForReplica{
113-
MinimalTLSVersion: postgresql.MinimalTLSVersionEnum(s.MinimalTLSVersion),
114-
Version: postgresql.ServerVersion(s.Version),
115-
SslEnforcement: postgresql.SslEnforcementEnum(s.SSLEnforcement),
116-
CreateMode: postgresql.CreateModeReplica,
117-
SourceServerID: s.SourceServerID,
115+
MinimalTLSVersion: postgresql.MinimalTLSVersionEnum(s.MinimalTLSVersion),
116+
Version: postgresql.ServerVersion(s.Version),
117+
SslEnforcement: postgresql.SslEnforcementEnum(s.SSLEnforcement),
118+
PublicNetworkAccess: postgresql.PublicNetworkAccessEnum(azure.ToString(s.PublicNetworkAccess)),
119+
CreateMode: postgresql.CreateModeReplica,
120+
SourceServerID: s.SourceServerID,
118121
StorageProfile: &postgresql.StorageProfile{
119122
BackupRetentionDays: azure.ToInt32PtrFromIntPtr(s.StorageProfile.BackupRetentionDays),
120123
GeoRedundantBackup: postgresql.GeoRedundantBackup(azure.ToString(s.StorageProfile.GeoRedundantBackup)),
@@ -131,6 +134,7 @@ func toPGSQLProperties(s v1beta1.SQLServerParameters, adminPassword string) post
131134
AdministratorLoginPassword: &adminPassword,
132135
Version: postgresql.ServerVersion(s.Version),
133136
SslEnforcement: postgresql.SslEnforcementEnum(s.SSLEnforcement),
137+
PublicNetworkAccess: postgresql.PublicNetworkAccessEnum(azure.ToString(s.PublicNetworkAccess)),
134138
CreateMode: postgresql.CreateModeDefault,
135139
StorageProfile: &postgresql.StorageProfile{
136140
BackupRetentionDays: azure.ToInt32PtrFromIntPtr(s.StorageProfile.BackupRetentionDays),
@@ -172,9 +176,10 @@ func (c *PostgreSQLServerClient) UpdateServer(ctx context.Context, cr *azuredbv1
172176
// we don't support that.
173177
s := cr.Spec.ForProvider
174178
properties := &postgresql.ServerUpdateParametersProperties{
175-
Version: postgresql.ServerVersion(s.Version),
176-
MinimalTLSVersion: postgresql.MinimalTLSVersionEnum(s.MinimalTLSVersion),
177-
SslEnforcement: postgresql.SslEnforcementEnum(s.SSLEnforcement),
179+
Version: postgresql.ServerVersion(s.Version),
180+
MinimalTLSVersion: postgresql.MinimalTLSVersionEnum(s.MinimalTLSVersion),
181+
SslEnforcement: postgresql.SslEnforcementEnum(s.SSLEnforcement),
182+
PublicNetworkAccess: postgresql.PublicNetworkAccessEnum(azure.ToString(s.PublicNetworkAccess)),
178183
StorageProfile: &postgresql.StorageProfile{
179184
BackupRetentionDays: azure.ToInt32PtrFromIntPtr(s.StorageProfile.BackupRetentionDays),
180185
GeoRedundantBackup: postgresql.GeoRedundantBackup(azure.ToString(s.StorageProfile.GeoRedundantBackup)),
@@ -313,6 +318,10 @@ func LateInitializePostgreSQL(p *azuredbv1beta1.SQLServerParameters, in postgres
313318
if p.SSLEnforcement == "" {
314319
p.SSLEnforcement = string(in.SslEnforcement)
315320
}
321+
322+
if p.PublicNetworkAccess == nil {
323+
p.PublicNetworkAccess = azure.ToStringPtr(string(in.PublicNetworkAccess))
324+
}
316325
}
317326

318327
// IsPostgreSQLUpToDate is used to report whether given postgresql.Server is in
@@ -344,6 +353,8 @@ func IsPostgreSQLUpToDate(p azuredbv1beta1.SQLServerParameters, in postgresql.Se
344353
return false
345354
case azure.ToString(p.StorageProfile.StorageAutogrow) != string(in.StorageProfile.StorageAutogrow):
346355
return false
356+
case azure.ToString(p.PublicNetworkAccess) != string(in.PublicNetworkAccess):
357+
return false
347358
}
348359
return true
349360
}

pkg/clients/database/postgresql_test.go

+140
Original file line numberDiff line numberDiff line change
@@ -431,3 +431,143 @@ func TestPostgreSQLServerFirewallRuleIsUpToDate(t *testing.T) {
431431
})
432432
}
433433
}
434+
435+
func TestIsPostgreSQLUpToDate(t *testing.T) {
436+
type args struct {
437+
p v1beta1.SQLServerParameters
438+
in postgresql.Server
439+
}
440+
cases := map[string]struct {
441+
args
442+
want bool
443+
}{
444+
"IsUpToDateWithAllDefault": {
445+
args: args{
446+
p: v1beta1.SQLServerParameters{},
447+
in: postgresql.Server{
448+
Sku: &postgresql.Sku{},
449+
ServerProperties: &postgresql.ServerProperties{
450+
StorageProfile: &postgresql.StorageProfile{},
451+
},
452+
},
453+
},
454+
want: true,
455+
},
456+
"IsUpToDate": {
457+
args: args{
458+
p: v1beta1.SQLServerParameters{
459+
MinimalTLSVersion: "TLS1_2",
460+
SSLEnforcement: "Enabled",
461+
Version: "9.6",
462+
Tags: map[string]string{
463+
"created_by": "crossplane",
464+
},
465+
SKU: v1beta1.SKU{
466+
Tier: "GeneralPurpose",
467+
Capacity: 2,
468+
Family: "Gen5",
469+
},
470+
PublicNetworkAccess: azure.ToStringPtr("Enabled"),
471+
StorageProfile: v1beta1.StorageProfile{
472+
StorageMB: 20480,
473+
StorageAutogrow: azure.ToStringPtr("Enabled"),
474+
BackupRetentionDays: to.IntPtr(5),
475+
GeoRedundantBackup: azure.ToStringPtr("Disabled"),
476+
},
477+
},
478+
in: postgresql.Server{
479+
Tags: map[string]*string{
480+
"created_by": azure.ToStringPtr("crossplane"),
481+
},
482+
Sku: &postgresql.Sku{
483+
Tier: postgresql.GeneralPurpose,
484+
Capacity: azure.ToInt32Ptr(2),
485+
Family: azure.ToStringPtr("Gen5"),
486+
},
487+
ServerProperties: &postgresql.ServerProperties{
488+
Version: "9.6",
489+
StorageProfile: &postgresql.StorageProfile{
490+
StorageMB: azure.ToInt32Ptr(20480),
491+
StorageAutogrow: postgresql.StorageAutogrowEnabled,
492+
BackupRetentionDays: azure.ToInt32Ptr(5),
493+
GeoRedundantBackup: postgresql.Disabled,
494+
},
495+
SslEnforcement: postgresql.SslEnforcementEnumEnabled,
496+
MinimalTLSVersion: postgresql.TLS12,
497+
PublicNetworkAccess: postgresql.PublicNetworkAccessEnumEnabled,
498+
},
499+
},
500+
},
501+
want: true,
502+
},
503+
"IsNotUpToDate": {
504+
args: args{
505+
p: v1beta1.SQLServerParameters{
506+
PublicNetworkAccess: azure.ToStringPtr("Disabled"),
507+
},
508+
in: postgresql.Server{
509+
Sku: &postgresql.Sku{},
510+
ServerProperties: &postgresql.ServerProperties{
511+
StorageProfile: &postgresql.StorageProfile{},
512+
PublicNetworkAccess: postgresql.PublicNetworkAccessEnumEnabled,
513+
},
514+
},
515+
},
516+
want: false,
517+
},
518+
"IsNotUpToDateWithServerWithoutSku": {
519+
args: args{
520+
p: v1beta1.SQLServerParameters{},
521+
in: postgresql.Server{
522+
ServerProperties: &postgresql.ServerProperties{
523+
StorageProfile: &postgresql.StorageProfile{},
524+
},
525+
},
526+
},
527+
want: false,
528+
},
529+
}
530+
531+
for name, tc := range cases {
532+
t.Run(name, func(t *testing.T) {
533+
got := IsPostgreSQLUpToDate(tc.args.p, tc.args.in)
534+
if diff := cmp.Diff(tc.want, got); diff != "" {
535+
t.Errorf("IsPostgreSQLUpToDate(...): -want, +got\n%s", diff)
536+
}
537+
})
538+
}
539+
}
540+
541+
func TestLateInitializePostgreSQL(t *testing.T) {
542+
type args struct {
543+
p *v1beta1.SQLServerParameters
544+
in postgresql.Server
545+
}
546+
cases := map[string]struct {
547+
args
548+
want *v1beta1.SQLServerParameters
549+
}{
550+
"PublicNetworkAccessLateInitialize": {
551+
args: args{
552+
p: &v1beta1.SQLServerParameters{},
553+
in: postgresql.Server{
554+
Sku: &postgresql.Sku{},
555+
ServerProperties: &postgresql.ServerProperties{
556+
PublicNetworkAccess: postgresql.PublicNetworkAccessEnumEnabled,
557+
},
558+
},
559+
},
560+
want: &v1beta1.SQLServerParameters{
561+
PublicNetworkAccess: azure.ToStringPtr("Enabled"),
562+
},
563+
},
564+
}
565+
for name, tc := range cases {
566+
t.Run(name, func(t *testing.T) {
567+
LateInitializePostgreSQL(tc.args.p, tc.args.in)
568+
if diff := cmp.Diff(tc.want, tc.args.p); diff != "" {
569+
t.Errorf("LateInitializePostgreSQL(...): -want, +got\n%s", diff)
570+
}
571+
})
572+
}
573+
}

0 commit comments

Comments
 (0)