From d96ac3303a9bd77842c57a5b18fd36abda76f6c9 Mon Sep 17 00:00:00 2001 From: William Mortl Date: Thu, 28 May 2020 16:57:27 -0600 Subject: [PATCH] should work now --- api/v1alpha1/azuresqldatabase_conversion.go | 5 +- api/v1beta1/azuresqldatabase_types.go | 7 ++- .../azure_v1beta1_azuresqldatabase.yaml | 15 +++++- go.sum | 2 + .../azuresql/azuresqldb/azuresqldb.go | 54 +++++++++++++------ .../azuresql/azuresqldb/azuresqldb_manager.go | 5 +- .../azuresqldb/azuresqldb_reconcile.go | 15 ++++-- .../azuresql/azuresqlserver/azuresqlserver.go | 2 +- .../azuresql/azuresqlshared/getgoclients.go | 7 +-- 9 files changed, 82 insertions(+), 30 deletions(-) diff --git a/api/v1alpha1/azuresqldatabase_conversion.go b/api/v1alpha1/azuresqldatabase_conversion.go index b7c4c594d05..862b0698eee 100644 --- a/api/v1alpha1/azuresqldatabase_conversion.go +++ b/api/v1alpha1/azuresqldatabase_conversion.go @@ -20,7 +20,10 @@ func (src *AzureSqlDatabase) ConvertTo(dstRaw conversion.Hub) error { dst.Spec.Server = src.Spec.Server dst.Spec.Edition = v1beta1.DBEdition(src.Spec.Edition) dst.Spec.DbName = src.Spec.DbName - dst.Spec.LongTermRetention = "" + dst.Spec.WeeklyRetention = "" + dst.Spec.MonthlyRetention = "" + dst.Spec.YearlyRetention = "" + dst.Spec.WeekOfYear = 0 // Status dst.Status = v1beta1.ASOStatus(src.Status) diff --git a/api/v1beta1/azuresqldatabase_types.go b/api/v1beta1/azuresqldatabase_types.go index 48ae375e91c..d0db646950f 100644 --- a/api/v1beta1/azuresqldatabase_types.go +++ b/api/v1beta1/azuresqldatabase_types.go @@ -21,8 +21,11 @@ type AzureSqlDatabaseSpec struct { Server string `json:"server"` Edition DBEdition `json:"edition"` // optional - DbName string `json:"dbName,omitempty"` - LongTermRetention string `json:"longTermRetention,omitempty"` + DbName string `json:"dbName,omitempty"` + WeeklyRetention string `json:"weeklyRetention,omitempty"` + MonthlyRetention string `json:"monthlyRetention,omitempty"` + YearlyRetention string `json:"yearlyRetention,omitempty"` + WeekOfYear int32 `json:"weekOfYear,omitempty"` } // +kubebuilder:object:root=true diff --git a/config/samples/azure_v1beta1_azuresqldatabase.yaml b/config/samples/azure_v1beta1_azuresqldatabase.yaml index 607e261b0ac..fefe9a8e822 100644 --- a/config/samples/azure_v1beta1_azuresqldatabase.yaml +++ b/config/samples/azure_v1beta1_azuresqldatabase.yaml @@ -9,4 +9,17 @@ spec: location: westus resourceGroup: resourcegroup-azure-operators server: sqlserver-sample-777 - edition: 0 \ No newline at end of file + edition: 0 + # the following fields are optional: + + # The weekly retention policy for an LTR backup in an ISO 8601 duration format (https://en.wikipedia.org/wiki/ISO_8601#Durations) + # weeklyRetention: P12W + + # The monthly retention policy for an LTR backup in an ISO 8601 duration format + # monthlyRetention: P3M + + # The yearly retention policy for an LTR backup in an ISO 8601 duration format + # yearlyRetention: P5Y + + # The week of year to take the yearly backup, valid values [1, 52] + # weekOfYear: 16 \ No newline at end of file diff --git a/go.sum b/go.sum index d76a6b4f9c4..f027ad4dc75 100644 --- a/go.sum +++ b/go.sum @@ -6,12 +6,14 @@ cloud.google.com/go v0.38.0 h1:ROfEUZz+Gh5pa62DJWXSaonyu3StP6EA6lPEXPI6mCo= cloud.google.com/go v0.38.0/go.mod h1:990N+gfupTy94rShfmMCWGDn0LpTmnzTp2qbd1dvSRU= contrib.go.opencensus.io/exporter/ocagent v0.4.12 h1:jGFvw3l57ViIVEPKKEUXPcLYIXJmQxLUh6ey1eJhwyc= contrib.go.opencensus.io/exporter/ocagent v0.4.12/go.mod h1:450APlNTSR6FrvC3CTRqYosuDstRB9un7SOx2k/9ckA= +github.com/Azure/azure-sdk-for-go v0.2.0-beta h1:wYBqYNMWr0WL2lcEZi+dlK9n+N0wJ0Pjs4BKeOnDjfQ= github.com/Azure/azure-sdk-for-go v38.0.0+incompatible h1:3D2O4g8AwDwyWkM1HpMFVux/ccQJmGJHXsE004Wsu1Q= github.com/Azure/azure-sdk-for-go v38.0.0+incompatible/go.mod h1:9XXNKU+eRnpl9moKnB4QOLf1HestfXbmab5FXxiDBjc= github.com/Azure/azure-sdk-for-go v40.6.0+incompatible h1:ULjp/a/UsBfnZcl45jjywhcBKex/k/A1cG9s9NapLFw= github.com/Azure/azure-sdk-for-go v41.0.0+incompatible h1:nQc4CAuBSr8rO0aZ90NvHoKyWYodhtzSAS4DPDrCtqo= github.com/Azure/azure-sdk-for-go v41.1.0+incompatible h1:AkS9XaeC8TDd0W0UiJRnEcYEHBO6xzILqqswHiNlSZQ= github.com/Azure/azure-sdk-for-go v41.2.0+incompatible h1:JOlv1wDuxcJi1ExJpQLNfEj6znsTFt2TiwQMow2YaXI= +github.com/Azure/azure-sdk-for-go v42.3.0+incompatible h1:PAHkmPqd/vQV4LJcqzEUM1elCyTMWjbrO8oFMl0dvBE= github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78/go.mod h1:LmzpDX56iTiv29bbRTIsUNlaFfuhWRQBWjQdVyAevI8= github.com/Azure/go-autorest/autorest v0.1.0/go.mod h1:AKyIcETwSUFxIcs/Wnq/C+kwCtlEYGUVd7FPNb2slmg= github.com/Azure/go-autorest/autorest v0.5.0 h1:Mlm9qy2fpQ9MvfyI41G2Zf5B4CsgjjNbLOWszfK6KrY= diff --git a/pkg/resourcemanager/azuresql/azuresqldb/azuresqldb.go b/pkg/resourcemanager/azuresql/azuresqldb/azuresqldb.go index 08b5f3cd720..a42fa4883b2 100644 --- a/pkg/resourcemanager/azuresql/azuresqldb/azuresqldb.go +++ b/pkg/resourcemanager/azuresql/azuresqldb/azuresqldb.go @@ -7,9 +7,9 @@ import ( "context" "fmt" "net/http" - "strings" "github.com/Azure/azure-sdk-for-go/services/preview/sql/mgmt/2015-05-01-preview/sql" + sql3 "github.com/Azure/azure-sdk-for-go/services/preview/sql/mgmt/v3.0/sql" azuresqlshared "github.com/Azure/azure-service-operator/pkg/resourcemanager/azuresql/azuresqlshared" "github.com/Azure/go-autorest/autorest" @@ -114,18 +114,7 @@ func (_ *AzureSqlDbManager) CreateOrUpdateDB(ctx context.Context, resourceGroupN } // AddLongTermRetention enables / disables long term retention -func (_ *AzureSqlDbManager) AddLongTermRetention(ctx context.Context, resourceGroupName string, serverName string, databaseName string, enabledDisabled string) (*http.Response, error) { - - var state sql.BackupLongTermRetentionPolicyState - if strings.EqualFold(enabledDisabled, "enabled") { - state = sql.Enabled - } else if strings.EqualFold(enabledDisabled, "disabled") { - state = sql.Disabled - } else { - return &http.Response{ - StatusCode: 0, - }, fmt.Errorf("State for LongTermRetentionPolicy must be enabled or disabled (or empty)") - } +func (_ *AzureSqlDbManager) AddLongTermRetention(ctx context.Context, resourceGroupName string, serverName string, databaseName string, weeklyRetention string, monthlyRetention string, yearlyRetention string, weekOfYear int32) (*http.Response, error) { longTermClient, err := azuresqlshared.GetBackupLongTermRetentionPoliciesClient() if err != nil { @@ -134,14 +123,47 @@ func (_ *AzureSqlDbManager) AddLongTermRetention(ctx context.Context, resourceGr }, err } + // validate the input and exit if nothing needs to happen - this is ok! + if weeklyRetention == "" && monthlyRetention == "" && yearlyRetention == "" { + return &http.Response{ + StatusCode: 200, + }, nil + } + + // validate the pairing of yearly retention and week of year + if yearlyRetention != "" && (weekOfYear <= 0 || weekOfYear > 52) { + return &http.Response{ + StatusCode: 500, + }, fmt.Errorf("weekOfYear must be greater than 0 and less or equal to 52 when yearlyRetention is used") + } + + // create pointers so that we can pass nils if needed + pWeeklyRetention := &weeklyRetention + if weeklyRetention == "" { + pWeeklyRetention = nil + } + pMonthlyRetention := &monthlyRetention + if monthlyRetention == "" { + pMonthlyRetention = nil + } + pYearlyRetention := &yearlyRetention + pWeekOfYear := &weekOfYear + if yearlyRetention == "" { + pYearlyRetention = nil + pWeekOfYear = nil + } + future, err := longTermClient.CreateOrUpdate( ctx, resourceGroupName, serverName, databaseName, - sql.BackupLongTermRetentionPolicy{ - BackupLongTermRetentionPolicyProperties: &sql.BackupLongTermRetentionPolicyProperties{ - State: state, + sql3.BackupLongTermRetentionPolicy{ + LongTermRetentionPolicyProperties: &sql3.LongTermRetentionPolicyProperties{ + WeeklyRetention: pWeeklyRetention, + MonthlyRetention: pMonthlyRetention, + YearlyRetention: pYearlyRetention, + WeekOfYear: pWeekOfYear, }, }, ) diff --git a/pkg/resourcemanager/azuresql/azuresqldb/azuresqldb_manager.go b/pkg/resourcemanager/azuresql/azuresqldb/azuresqldb_manager.go index d1bf8f54c7e..3da47179052 100644 --- a/pkg/resourcemanager/azuresql/azuresqldb/azuresqldb_manager.go +++ b/pkg/resourcemanager/azuresql/azuresqldb/azuresqldb_manager.go @@ -40,7 +40,10 @@ type SqlDbManager interface { resourceGroupName string, serverName string, databaseName string, - enabledDisabled string) (*http.Response, error) + weeklyRetention string, + monthlyRetention string, + yearlyRetention string, + weekOfYear int32) (*http.Response, error) resourcemanager.ARMClient } diff --git a/pkg/resourcemanager/azuresql/azuresqldb/azuresqldb_reconcile.go b/pkg/resourcemanager/azuresql/azuresqldb/azuresqldb_reconcile.go index 7d38487a96f..5cc9496ea47 100644 --- a/pkg/resourcemanager/azuresql/azuresqldb/azuresqldb_reconcile.go +++ b/pkg/resourcemanager/azuresql/azuresqldb/azuresqldb_reconcile.go @@ -62,11 +62,16 @@ func (db *AzureSqlDbManager) Ensure(ctx context.Context, obj runtime.Object, opt if err == nil { // optionally set the long term retention policy - if instance.Spec.LongTermRetention != "" { - _, err = db.AddLongTermRetention(ctx, groupName, server, dbName, instance.Spec.LongTermRetention) - if err != nil { - return false, err - } + _, err = db.AddLongTermRetention(ctx, + groupName, + server, + dbName, + instance.Spec.WeeklyRetention, + instance.Spec.MonthlyRetention, + instance.Spec.YearlyRetention, + instance.Spec.WeekOfYear) + if err != nil { + return false, err } // db exists, we have successfully provisioned everything diff --git a/pkg/resourcemanager/azuresql/azuresqlserver/azuresqlserver.go b/pkg/resourcemanager/azuresql/azuresqlserver/azuresqlserver.go index 22a5b116b6f..fe714f059d8 100644 --- a/pkg/resourcemanager/azuresql/azuresqlserver/azuresqlserver.go +++ b/pkg/resourcemanager/azuresql/azuresqlserver/azuresqlserver.go @@ -8,7 +8,7 @@ import ( "errors" "net/http" - "github.com/Azure/azure-sdk-for-go/services/preview/sql/mgmt/2015-05-01-preview/sql" + "github.com/Azure/azure-sdk-for-go/services/preview/sql/mgmt/2015-05-01/sql" azuresqlshared "github.com/Azure/azure-service-operator/pkg/resourcemanager/azuresql/azuresqlshared" "github.com/Azure/azure-service-operator/pkg/secrets" "github.com/Azure/go-autorest/autorest" diff --git a/pkg/resourcemanager/azuresql/azuresqlshared/getgoclients.go b/pkg/resourcemanager/azuresql/azuresqlshared/getgoclients.go index 4c261f02eeb..98471606778 100644 --- a/pkg/resourcemanager/azuresql/azuresqlshared/getgoclients.go +++ b/pkg/resourcemanager/azuresql/azuresqlshared/getgoclients.go @@ -6,6 +6,7 @@ package azuresqlshared import ( network "github.com/Azure/azure-sdk-for-go/services/network/mgmt/2019-09-01/network" "github.com/Azure/azure-sdk-for-go/services/preview/sql/mgmt/2015-05-01-preview/sql" + sql3 "github.com/Azure/azure-sdk-for-go/services/preview/sql/mgmt/v3.0/sql" "github.com/Azure/azure-service-operator/pkg/resourcemanager/config" "github.com/Azure/azure-service-operator/pkg/resourcemanager/iam" ) @@ -83,11 +84,11 @@ func GetGoNetworkSubnetClient() (network.SubnetsClient, error) { } // GetBackupLongTermRetentionPoliciesClient retrieves a Subnetclient -func GetBackupLongTermRetentionPoliciesClient() (sql.BackupLongTermRetentionPoliciesClient, error) { - BackupClient := sql.NewBackupLongTermRetentionPoliciesClientWithBaseURI(config.BaseURI(), config.SubscriptionID()) +func GetBackupLongTermRetentionPoliciesClient() (sql3.BackupLongTermRetentionPoliciesClient, error) { + BackupClient := sql3.NewBackupLongTermRetentionPoliciesClientWithBaseURI(config.BaseURI(), config.SubscriptionID()) a, err := iam.GetResourceManagementAuthorizer() if err != nil { - return sql.BackupLongTermRetentionPoliciesClient{}, err + return sql3.BackupLongTermRetentionPoliciesClient{}, err } BackupClient.Authorizer = a BackupClient.AddToUserAgent(config.UserAgent())