diff --git a/controllers/clusterresources/cassandrauser_controller.go b/controllers/clusterresources/cassandrauser_controller.go index 1742bb1ba..02991b0e5 100644 --- a/controllers/clusterresources/cassandrauser_controller.go +++ b/controllers/clusterresources/cassandrauser_controller.go @@ -125,17 +125,29 @@ func (r *CassandraUserReconciler) Reconcile(ctx context.Context, req ctrl.Reques if event == models.CreatingEvent { l.Info("Creating user", "user", u, "cluster ID", clusterID) - err = r.API.CreateUser(u.ToInstAPI(username, password), clusterID, instaclustr.CassandraBundleUser) + exists, err := CheckIfUserExistsOnInstaclustrAPI(username, clusterID, instaclustr.CassandraBundleUser, r.API) if err != nil { - l.Error(err, "Cannot create a user for the Cassandra cluster", - "cluster ID", clusterID, - "username", username) - r.EventRecorder.Eventf(u, models.Warning, models.CreatingEvent, - "Cannot create user. Reason: %v", err) - + l.Error(err, "Cannot check if user exists ") + r.EventRecorder.Eventf( + u, models.Warning, models.CreationFailed, + "Cannot check if user exists. Reason: %v", err, + ) return models.ReconcileRequeue, nil } + if !exists { + err = r.API.CreateUser(u.ToInstAPI(username, password), clusterID, instaclustr.CassandraBundleUser) + if err != nil { + l.Error(err, "Cannot create a user for the Cassandra cluster", + "cluster ID", clusterID, + "username", username) + r.EventRecorder.Eventf(u, models.Warning, models.CreatingEvent, + "Cannot create user. Reason: %v", err) + + return models.ReconcileRequeue, nil + } + } + event = models.Created u.Status.ClustersEvents[clusterID] = event @@ -159,15 +171,27 @@ func (r *CassandraUserReconciler) Reconcile(ctx context.Context, req ctrl.Reques if event == models.DeletingEvent { l.Info("Deleting user from a cluster", "cluster ID", clusterID) - err = r.API.DeleteUser(username, clusterID, instaclustr.CassandraBundleUser) + exists, err := CheckIfUserExistsOnInstaclustrAPI(username, clusterID, instaclustr.CassandraBundleUser, r.API) if err != nil { - l.Error(err, "Cannot delete Cassandra user") - r.EventRecorder.Eventf(u, models.Warning, models.DeletingEvent, - "Cannot delete user. Reason: %v", err) - + l.Error(err, "Cannot check if user exists ") + r.EventRecorder.Eventf( + u, models.Warning, models.CreationFailed, + "Cannot check if user exists. Reason: %v", err, + ) return models.ReconcileRequeue, nil } + if exists { + err = r.API.DeleteUser(username, clusterID, instaclustr.CassandraBundleUser) + if err != nil { + l.Error(err, "Cannot delete Cassandra user") + r.EventRecorder.Eventf(u, models.Warning, models.DeletingEvent, + "Cannot delete user. Reason: %v", err) + + return models.ReconcileRequeue, nil + } + } + l.Info("User has been deleted for cluster", "username", username, "cluster ID", clusterID) r.EventRecorder.Eventf(u, models.Normal, models.Deleted, diff --git a/controllers/tests/cassandra_plus_users_test.go b/controllers/tests/cassandra_plus_users_test.go index a94e24eab..816d41974 100644 --- a/controllers/tests/cassandra_plus_users_test.go +++ b/controllers/tests/cassandra_plus_users_test.go @@ -382,8 +382,8 @@ var _ = Describe("Basic Cassandra User controller + Basic Cassandra cluster cont return false } - for i, useRef := range cassandra1.Spec.UserRefs { - if user2.Name == useRef.Name && user2.Namespace == useRef.Namespace { + for i, userRef := range cassandra1.Spec.UserRefs { + if user2.Name == userRef.Name && user2.Namespace == userRef.Namespace { cassandra1.Spec.UserRefs = removeUserByIndex(cassandra1.Spec.UserRefs, i) Expect(k8sClient.Patch(ctx, &cassandra1, patch)).Should(Succeed()) }