Skip to content

Commit

Permalink
should work now
Browse files Browse the repository at this point in the history
  • Loading branch information
William Mortl authored and jananivMS committed Jun 4, 2020
1 parent 8d38903 commit d96ac33
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 30 deletions.
5 changes: 4 additions & 1 deletion api/v1alpha1/azuresqldatabase_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 5 additions & 2 deletions api/v1beta1/azuresqldatabase_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 14 additions & 1 deletion config/samples/azure_v1beta1_azuresqldatabase.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,17 @@ spec:
location: westus
resourceGroup: resourcegroup-azure-operators
server: sqlserver-sample-777
edition: 0
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
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
54 changes: 38 additions & 16 deletions pkg/resourcemanager/azuresql/azuresqldb/azuresqldb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand All @@ -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,
},
},
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
15 changes: 10 additions & 5 deletions pkg/resourcemanager/azuresql/azuresqldb/azuresqldb_reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
7 changes: 4 additions & 3 deletions pkg/resourcemanager/azuresql/azuresqlshared/getgoclients.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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())
Expand Down

0 comments on commit d96ac33

Please sign in to comment.