Skip to content

Commit cd5282b

Browse files
committed
refactor: rename CreateContactGroupIdempotent to CreateContactGroup and simplify its implementation
1 parent bfd1129 commit cd5282b

File tree

3 files changed

+7
-39
lines changed

3 files changed

+7
-39
lines changed

internal/controller/contactgroup_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ func (r *ContactGroupController) Reconcile(ctx context.Context, req ctrl.Request
195195
// First creation – condition not present yet
196196
case existingCond == nil:
197197
// Create ContactGroup on email provider
198-
emailProviderContactGroup, err := r.EmailProvider.CreateContactGroupIdempotent(ctx, *contactGroup)
198+
emailProviderContactGroup, err := r.EmailProvider.CreateContactGroup(ctx, *contactGroup)
199199
if err != nil {
200200
log.Error(err, "Failed to create ContactGroup on email provider")
201201
return ctrl.Result{}, fmt.Errorf("failed to create ContactGroup on email provider: %w", err)

internal/controller/contactgroup_controller_test.go

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -149,29 +149,6 @@ var _ = ginkgo.Describe("ContactGroupController", func() {
149149
gomega.Expect(cond.ObservedGeneration).To(gomega.Equal(int64(2)))
150150
})
151151
})
152-
153-
ginkgo.Context("when the contact group already exists on the provider", func() {
154-
ginkgo.It("does not create a new group and uses the existing provider ID", func() {
155-
// Pre-populate provider with an existing group with the same display name
156-
provider.listOut = emailprovider.ListContactGroupsOutput{
157-
ContactGroups: []emailprovider.GetContactGroupOutput{{
158-
ContactGroupID: "cg-existing",
159-
DisplayName: emailprovider.GetDeterministicContactGroupDisplayName(group),
160-
}},
161-
}
162-
163-
_, err := controller.Reconcile(ctx, ctrl.Request{NamespacedName: types.NamespacedName{Name: group.Name, Namespace: group.Namespace}})
164-
gomega.Expect(err).NotTo(gomega.HaveOccurred())
165-
166-
fetched := &notificationv1.ContactGroup{}
167-
gomega.Expect(k8sClient.Get(ctx, types.NamespacedName{Name: group.Name, Namespace: group.Namespace}, fetched)).To(gomega.Succeed())
168-
169-
cond := meta.FindStatusCondition(fetched.Status.Conditions, notificationv1.ContactGroupReadyCondition)
170-
gomega.Expect(cond).NotTo(gomega.BeNil())
171-
gomega.Expect(fetched.Status.ProviderID).To(gomega.Equal("cg-existing"))
172-
gomega.Expect(provider.createdGroups).To(gomega.BeEmpty()) // no create called
173-
})
174-
})
175152
})
176153

177154
var _ = ginkgo.Describe("contactGroupFinalizer", func() {

internal/emailprovider/service.go

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -69,22 +69,13 @@ func (s *Service) Send(ctx context.Context,
6969
})
7070
}
7171

72-
// CreateContactGroupIdempotent creates a contact group on the email provider.
73-
// This is an indempotent operation. If a contact group with the same display name already exists, it will return the existing contact group.
74-
func (s *Service) CreateContactGroupIdempotent(ctx context.Context, cg notificationmiloapiscomv1alpha1.ContactGroup) (CreateContactGroupOutput, error) {
72+
// CreateContactGroup creates a contact group on the email provider.
73+
func (s *Service) CreateContactGroup(ctx context.Context, cg notificationmiloapiscomv1alpha1.ContactGroup) (CreateContactGroupOutput, error) {
7574
displayName := GetDeterministicContactGroupDisplayName(&cg)
76-
contactGroup, err := s.GetContactGroupByDisplayName(ctx, displayName)
77-
if err != nil {
78-
if errors.IsNotFound(err) {
79-
return s.provider.CreateContactGroup(ctx, CreateContactGroupInput{
80-
DisplayName: displayName,
81-
})
82-
}
83-
return CreateContactGroupOutput{}, err
84-
}
85-
return CreateContactGroupOutput{
86-
ContactGroupID: contactGroup.ContactGroupID,
87-
}, nil
75+
return s.provider.CreateContactGroup(ctx, CreateContactGroupInput{
76+
DisplayName: displayName,
77+
})
78+
8879
}
8980

9081
// GetContactGroup returns the email provider contact group id of the contact group.

0 commit comments

Comments
 (0)