@@ -18,13 +18,14 @@ package group
18
18
19
19
import (
20
20
"context"
21
- "fmt"
22
21
23
22
"github.com/pkg/errors"
24
23
"k8s.io/apimachinery/pkg/types"
24
+ "k8s.io/utils/ptr"
25
25
ctrl "sigs.k8s.io/controller-runtime"
26
26
"sigs.k8s.io/controller-runtime/pkg/client"
27
27
28
+ xpv1 "github.com/crossplane/crossplane-runtime/apis/common/v1"
28
29
"github.com/crossplane/crossplane-runtime/pkg/connection"
29
30
"github.com/crossplane/crossplane-runtime/pkg/controller"
30
31
"github.com/crossplane/crossplane-runtime/pkg/event"
@@ -44,7 +45,11 @@ const (
44
45
errGetPC = "cannot get ProviderConfig"
45
46
errGetCreds = "cannot get credentials"
46
47
47
- errNewClient = "cannot create new Service"
48
+ errNewClient = "cannot create new Service"
49
+ errCreateGroup = "cannot create Group"
50
+ errDeleteGroup = "cannot delete Group"
51
+ errGetGroup = "cannot get Group"
52
+ errUpdateGroup = "cannot update Group"
48
53
)
49
54
50
55
// A NoOpService does nothing.
@@ -145,8 +150,15 @@ func (c *external) Observe(ctx context.Context, mg resource.Managed) (managed.Ex
145
150
return managed.ExternalObservation {}, errors .New (errNotGroup )
146
151
}
147
152
148
- // These fmt statements should be removed in the real implementation.
149
- fmt .Printf ("Observing: %+v" , cr )
153
+ observedGroup , err := c .cloudianService .GetGroup (ctx , cr .Spec .ForProvider .GroupID )
154
+ if err != nil {
155
+ if errors .Is (err , cloudian .ErrNotFound ) {
156
+ return managed.ExternalObservation {ResourceExists : false }, nil
157
+ }
158
+ return managed.ExternalObservation {}, errors .Wrap (err , errGetGroup )
159
+ }
160
+
161
+ cr .SetConditions (xpv1 .Available ())
150
162
151
163
return managed.ExternalObservation {
152
164
// Return false when the external resource does not exist. This lets
@@ -157,7 +169,7 @@ func (c *external) Observe(ctx context.Context, mg resource.Managed) (managed.Ex
157
169
// Return false when the external resource exists, but it not up to date
158
170
// with the desired managed resource state. This lets the managed
159
171
// resource reconciler know that it needs to call Update.
160
- ResourceUpToDate : true ,
172
+ ResourceUpToDate : isUpToDate ( cr . Spec . ForProvider , * observedGroup ) ,
161
173
162
174
// Return any details that may be required to connect to the external
163
175
// resource. These will be stored as the connection secret.
@@ -171,7 +183,11 @@ func (c *external) Create(ctx context.Context, mg resource.Managed) (managed.Ext
171
183
return managed.ExternalCreation {}, errors .New (errNotGroup )
172
184
}
173
185
174
- fmt .Printf ("Creating: %+v" , cr )
186
+ cr .SetConditions (xpv1 .Creating ())
187
+
188
+ if err := c .cloudianService .CreateGroup (ctx , newGroupFromParams (cr .Spec .ForProvider )); err != nil {
189
+ return managed.ExternalCreation {}, errors .Wrap (err , errCreateGroup )
190
+ }
175
191
176
192
return managed.ExternalCreation {
177
193
// Optionally return any details that may be required to connect to the
@@ -186,7 +202,9 @@ func (c *external) Update(ctx context.Context, mg resource.Managed) (managed.Ext
186
202
return managed.ExternalUpdate {}, errors .New (errNotGroup )
187
203
}
188
204
189
- fmt .Printf ("Updating: %+v" , cr )
205
+ if err := c .cloudianService .UpdateGroup (ctx , newGroupFromParams (cr .Spec .ForProvider )); err != nil {
206
+ return managed.ExternalUpdate {}, errors .Wrap (err , errUpdateGroup )
207
+ }
190
208
191
209
return managed.ExternalUpdate {
192
210
// Optionally return any details that may be required to connect to the
@@ -201,12 +219,35 @@ func (c *external) Delete(ctx context.Context, mg resource.Managed) (managed.Ext
201
219
return managed.ExternalDelete {}, errors .New (errNotGroup )
202
220
}
203
221
204
- fmt .Printf ("Deleting: %+v" , cr )
222
+ cr .SetConditions (xpv1 .Deleting ())
223
+
224
+ if err := c .cloudianService .DeleteGroup (ctx , cr .Spec .ForProvider .GroupID ); err != nil {
225
+ return managed.ExternalDelete {}, errors .Wrap (err , errDeleteGroup )
226
+ }
205
227
206
228
return managed.ExternalDelete {}, nil
207
229
}
208
230
209
231
func (c * external ) Disconnect (ctx context.Context ) error {
210
- // TODO implement me
211
- panic ("implement me" )
232
+ return nil
233
+ }
234
+
235
+ func isUpToDate (desired v1alpha1.GroupParameters , observed cloudian.Group ) bool {
236
+ return newGroupFromParams (desired ) == observed
237
+ }
238
+
239
+ func newGroupFromParams (gp v1alpha1.GroupParameters ) cloudian.Group {
240
+ defaultsGroup := cloudian .NewGroup (gp .GroupID )
241
+ return cloudian.Group {
242
+ Active : gp .Active ,
243
+ GroupID : gp .GroupID ,
244
+ GroupName : gp .GroupName ,
245
+ LDAPEnabled : ptr .Deref (gp .LDAPEnabled , defaultsGroup .LDAPEnabled ),
246
+ LDAPGroup : ptr .Deref (gp .LDAPGroup , defaultsGroup .LDAPGroup ),
247
+ LDAPMatchAttribute : ptr .Deref (gp .LDAPMatchAttribute , defaultsGroup .LDAPMatchAttribute ),
248
+ LDAPSearch : ptr .Deref (gp .LDAPSearch , defaultsGroup .LDAPSearch ),
249
+ LDAPSearchUserBase : ptr .Deref (gp .LDAPSearchUserBase , defaultsGroup .LDAPSearchUserBase ),
250
+ LDAPServerURL : ptr .Deref (gp .LDAPServerURL , defaultsGroup .LDAPServerURL ),
251
+ LDAPUserDNTemplate : ptr .Deref (gp .LDAPUserDNTemplate , defaultsGroup .LDAPUserDNTemplate ),
252
+ }
212
253
}
0 commit comments