Skip to content

Commit 13f529c

Browse files
authored
feat: reconcile user (#64)
1 parent c6ee6a1 commit 13f529c

File tree

3 files changed

+44
-7
lines changed

3 files changed

+44
-7
lines changed

apis/user/v1alpha1/user_types.go

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ import (
2727

2828
// UserParameters are the configurable fields of a User.
2929
type UserParameters struct {
30+
GroupID string `json:"groupId"`
31+
UserID string `json:"userId"`
3032
}
3133

3234
// UserObservation are the observable fields of a User.

internal/controller/user/user.go

+34-7
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,11 @@ const (
4444
errGetPC = "cannot get ProviderConfig"
4545
errGetCreds = "cannot get credentials"
4646

47-
errNewClient = "cannot create new Service"
47+
errNewClient = "cannot create new Service"
48+
errCreateUser = "cannot create User"
49+
errDeleteUser = "cannot delete User"
50+
errListUsers = "cannot list Users"
51+
errGetUser = "cannot get User"
4852
)
4953

5054
// A NoOpService does nothing.
@@ -145,8 +149,10 @@ func (c *external) Observe(ctx context.Context, mg resource.Managed) (managed.Ex
145149
return managed.ExternalObservation{}, errors.New(errNotUser)
146150
}
147151

148-
// These fmt statements should be removed in the real implementation.
149-
fmt.Printf("Observing: %+v", cr)
152+
users, err := c.cloudianService.ListUsers(ctx, cr.Spec.ForProvider.GroupID, nil)
153+
if err != nil {
154+
return managed.ExternalObservation{}, errors.Wrap(err, errListUsers)
155+
}
150156

151157
return managed.ExternalObservation{
152158
// Return false when the external resource does not exist. This lets
@@ -157,21 +163,36 @@ func (c *external) Observe(ctx context.Context, mg resource.Managed) (managed.Ex
157163
// Return false when the external resource exists, but it not up to date
158164
// with the desired managed resource state. This lets the managed
159165
// resource reconciler know that it needs to call Update.
160-
ResourceUpToDate: true,
166+
ResourceUpToDate: isUpToDate(cr.Spec.ForProvider, users),
161167

162168
// Return any details that may be required to connect to the external
163169
// resource. These will be stored as the connection secret.
164170
ConnectionDetails: managed.ConnectionDetails{},
165171
}, nil
166172
}
167173

174+
func isUpToDate(spec v1alpha1.UserParameters, users []cloudian.User) bool {
175+
for _, user := range users {
176+
if user.UserID == spec.UserID {
177+
return true
178+
}
179+
}
180+
return false
181+
}
182+
168183
func (c *external) Create(ctx context.Context, mg resource.Managed) (managed.ExternalCreation, error) {
169184
cr, ok := mg.(*v1alpha1.User)
170185
if !ok {
171186
return managed.ExternalCreation{}, errors.New(errNotUser)
172187
}
173188

174-
fmt.Printf("Creating: %+v", cr)
189+
user := cloudian.User{
190+
GroupID: cr.Spec.ForProvider.GroupID,
191+
UserID: cr.Spec.ForProvider.UserID,
192+
}
193+
if err := c.cloudianService.CreateUser(ctx, user); err != nil {
194+
return managed.ExternalCreation{}, errors.Wrap(err, errCreateUser)
195+
}
175196

176197
return managed.ExternalCreation{
177198
// Optionally return any details that may be required to connect to the
@@ -186,7 +207,7 @@ func (c *external) Update(ctx context.Context, mg resource.Managed) (managed.Ext
186207
return managed.ExternalUpdate{}, errors.New(errNotUser)
187208
}
188209

189-
fmt.Printf("Updating: %+v", cr)
210+
fmt.Printf("Pretending to Update (no managed fields to update): %+v", cr)
190211

191212
return managed.ExternalUpdate{
192213
// Optionally return any details that may be required to connect to the
@@ -201,7 +222,13 @@ func (c *external) Delete(ctx context.Context, mg resource.Managed) (managed.Ext
201222
return managed.ExternalDelete{}, errors.New(errNotUser)
202223
}
203224

204-
fmt.Printf("Deleting: %+v", cr)
225+
user := cloudian.User{
226+
GroupID: cr.Spec.ForProvider.GroupID,
227+
UserID: cr.Spec.ForProvider.UserID,
228+
}
229+
if err := c.cloudianService.DeleteUser(ctx, user); err != nil {
230+
return managed.ExternalDelete{}, errors.Wrap(err, errDeleteUser)
231+
}
205232

206233
return managed.ExternalDelete{}, nil
207234
}

package/crds/user.cloudian.crossplane.io_users.yaml

+8
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ spec:
7272
type: string
7373
forProvider:
7474
description: UserParameters are the configurable fields of a User.
75+
properties:
76+
groupId:
77+
type: string
78+
userId:
79+
type: string
80+
required:
81+
- groupId
82+
- userId
7583
type: object
7684
managementPolicies:
7785
default:

0 commit comments

Comments
 (0)