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

Check Name Availability/Validation for SQL Resources #245

Merged
merged 17 commits into from
Oct 2, 2019
Merged
Show file tree
Hide file tree
Changes from 11 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
24 changes: 23 additions & 1 deletion controllers/sqlserver_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ type SqlServerReconciler struct {
func (r *SqlServerReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
ctx := context.Background()
log := r.Log.WithValues("sqlserver", req.NamespacedName)

var instance azurev1.SqlServer

if err := r.Get(ctx, req.NamespacedName, &instance); err != nil {
Expand All @@ -64,6 +63,17 @@ func (r *SqlServerReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
return ctrl.Result{}, client.IgnoreNotFound(err)
}

location := instance.Spec.Location
name := instance.ObjectMeta.Name
groupName := instance.Spec.ResourceGroup

sdkClient := sql.GoSDKClient{
Ctx: ctx,
ResourceGroupName: groupName,
ServerName: name,
Location: location,
}

if helpers.IsBeingDeleted(&instance) {
if helpers.HasFinalizer(&instance, SQLServerFinalizerName) {
if err := r.deleteExternal(&instance); err != nil {
Expand Down Expand Up @@ -96,6 +106,18 @@ func (r *SqlServerReconciler) Reconcile(req ctrl.Request) (ctrl.Result, error) {
}
}

availableResp, err := sdkClient.CheckNameAvailability()
if err != nil {
log.Info("error validating name")
return ctrl.Result{}, err
}
log.Info(availableResp.Message)
melonrush13 marked this conversation as resolved.
Show resolved Hide resolved
if strings.Contains(availableResp.Message, "unsupported characters") {
melonrush13 marked this conversation as resolved.
Show resolved Hide resolved
log.Info("Servername is invalid")
melonrush13 marked this conversation as resolved.
Show resolved Hide resolved
r.Recorder.Event(&instance, "Warning", "Failed", "Servername is invalid")
return ctrl.Result{Requeue: false}, fmt.Errorf("Servername invalid %s", availableResp.Name)
}

if !instance.IsSubmitted() {
r.Recorder.Event(&instance, "Normal", "Submitting", "starting resource reconciliation")
if err := r.reconcileExternal(&instance); err != nil {
Expand Down
1 change: 1 addition & 0 deletions pkg/resourcemanager/sqlclient/resourceclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ type ResourceClient interface {
DeleteSQLFirewallRule(ruleName string) (err error)
GetServer() (result sql.Server, err error)
IsAsyncNotCompleted(err error) (result bool)
CheckNameAvailability() (result AvailabilityResponse, err error)
}
19 changes: 19 additions & 0 deletions pkg/resourcemanager/sqlclient/sqlclient_godsk.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,3 +243,22 @@ func (sdk GoSDKClient) GetServer() (result sql.Server, err error) {
sdk.ServerName,
)
}

// CheckNameAvailability determines whether a SQL resource can be created with the specified name
func (sdk GoSDKClient) CheckNameAvailability() (result AvailabilityResponse, err error) {
serversClient := getGoServersClient()
typeOfService := "Microsoft.Sql/servers"
melonrush13 marked this conversation as resolved.
Show resolved Hide resolved

response, err := serversClient.CheckNameAvailability(
sdk.Ctx,
sql.CheckNameAvailabilityRequest{
Name: to.StringPtr(sdk.ServerName),
Type: to.StringPtr(typeOfService),
},
)
if err != nil {
return result, err
}

return ToAvailabilityResponse(response), err
}
20 changes: 20 additions & 0 deletions pkg/resourcemanager/sqlclient/sqlproperties.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,23 @@ func translateDBEdition(in DBEdition) (result sql.DatabaseEdition) {

return result
}

// AvailabilityResponse is the response for checking name validation
type AvailabilityResponse struct {
Available bool
Message string
Name string
}

// ToAvailabilityResponse converts CheckNameAvailabilityResponse to AvailabilityResponse
func ToAvailabilityResponse(response sql.CheckNameAvailabilityResponse) (result AvailabilityResponse) {
result.Available = *response.Available
if response.Message != nil {
result.Message = *response.Message
}
if response.Name != nil {
result.Name = *response.Name
}

return result
}