Skip to content

Commit 3e3138b

Browse files
committed
rebase
Signed-off-by: Mikel Landa <mikel.landa@pepperservicing.es>
1 parent 67aeb05 commit 3e3138b

File tree

5 files changed

+44
-8
lines changed

5 files changed

+44
-8
lines changed

apis/mssql/v1alpha1/user_types.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ import (
2222
xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
2323
)
2424

25+
// UserTypes
26+
const (
27+
UserTypeLocal = "Local"
28+
UserTypeAD = "AD"
29+
)
30+
2531
// A UserSpec defines the desired state of a Database.
2632
type UserSpec struct {
2733
xpv1.ResourceSpec `json:",inline"`

apis/mssql/v1alpha1/zz_generated.deepcopy.go

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/mssql/ad/user.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ kind: User
33
metadata:
44
name: example-user-ad
55
annotations:
6-
crossplane.io/external-name: "example-user@example.com"
6+
##change to real user inside your AD
7+
crossplane.io/external-name: "example-user@example-domain.com"
78
spec:
89
forProvider:
910
type: AD

package/crds/mssql.sql.crossplane.io_users.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ spec:
170170
- name
171171
- namespace
172172
type: object
173+
type:
174+
type: string
173175
type: object
174176
managementPolicies:
175177
default:

pkg/controller/mssql/user/reconciler.go

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,20 @@ func (c *external) Observe(ctx context.Context, mg resource.Managed) (managed.Ex
137137
}
138138

139139
var name string
140+
var query string
141+
var userType string
142+
if cr.Spec.ForProvider.Type == nil {
143+
userType = v1alpha1.UserTypeLocal
144+
} else {
145+
userType = *cr.Spec.ForProvider.Type
146+
}
147+
switch userType {
148+
case v1alpha1.UserTypeAD:
149+
query = "SELECT name FROM sys.database_principals WHERE type IN ('E','X') AND name = @p1"
150+
case v1alpha1.UserTypeLocal:
151+
query = "SELECT name FROM sys.database_principals WHERE type = 'S' AND name = @p1"
152+
default:
153+
return managed.ExternalObservation{}, errors.Errorf("Type '%s' is not valid", *cr.Spec.ForProvider.Type)
140154

141155
query := "SELECT name FROM sys.database_principals WHERE type = 'S' AND name = @p1"
142156
err := c.db.Scan(ctx, xsql.Query{
@@ -170,9 +184,17 @@ func (c *external) Create(ctx context.Context, mg resource.Managed) (managed.Ext
170184
return managed.ExternalCreation{}, errors.New(errNotUser)
171185
}
172186
var query string
173-
var pw string
174-
if t := cr.Spec.ForProvider.Type; t == nil || *t != "AD" {
175-
187+
var outPw string
188+
var userType string
189+
if cr.Spec.ForProvider.Type == nil {
190+
userType = v1alpha1.UserTypeLocal
191+
} else {
192+
userType = *cr.Spec.ForProvider.Type
193+
}
194+
switch userType {
195+
case v1alpha1.UserTypeAD:
196+
query = fmt.Sprintf("CREATE USER %s FROM EXTERNAL PROVIDER", mssql.QuoteIdentifier(meta.GetExternalName(cr)))
197+
case v1alpha1.UserTypeLocal:
176198
pw, _, err := c.getPassword(ctx, cr)
177199
if err != nil {
178200
return managed.ExternalCreation{}, err
@@ -183,10 +205,10 @@ func (c *external) Create(ctx context.Context, mg resource.Managed) (managed.Ext
183205
return managed.ExternalCreation{}, err
184206
}
185207
}
186-
208+
outPw = pw
187209
query = fmt.Sprintf("CREATE USER %s WITH PASSWORD=%s", mssql.QuoteIdentifier(meta.GetExternalName(cr)), mssql.QuoteValue(pw))
188-
} else {
189-
query = fmt.Sprintf("CREATE USER %s", mssql.QuoteIdentifier(meta.GetExternalName(cr)))
210+
default:
211+
return managed.ExternalCreation{}, errors.Errorf("Type '%s' is not valid", *cr.Spec.ForProvider.Type)
190212

191213
}
192214

@@ -205,7 +227,7 @@ func (c *external) Create(ctx context.Context, mg resource.Managed) (managed.Ext
205227
}
206228

207229
return managed.ExternalCreation{
208-
ConnectionDetails: c.db.GetConnectionDetails(meta.GetExternalName(cr), pw),
230+
ConnectionDetails: c.db.GetConnectionDetails(meta.GetExternalName(cr), outPw),
209231
}, nil
210232
}
211233

0 commit comments

Comments
 (0)