Skip to content

Commit 8a500f8

Browse files
authored
Merge pull request #335 from rounakdatta/repository-project-scoping
fix(repositories): Support project scoping for Observe, Delete in Repository resources
2 parents ec7f091 + 2cbc0e8 commit 8a500f8

2 files changed

Lines changed: 175 additions & 0 deletions

File tree

pkg/controller/repositories/controller.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ func (e *external) Observe(ctx context.Context, mg resource.Managed) (managed.Ex
128128
Repo: meta.GetExternalName(cr),
129129
}
130130

131+
if cr.Spec.ForProvider.Project != nil {
132+
repoQuery.AppProject = *cr.Spec.ForProvider.Project
133+
}
134+
131135
observedRepository, err := e.client.Get(ctx, &repoQuery)
132136

133137
if err != nil && repositories.IsErrorPermissionDenied(err) || repositories.IsErrorRepositoryNotFound(err) {
@@ -275,6 +279,10 @@ func (e *external) Delete(ctx context.Context, mg resource.Managed) (managed.Ext
275279
Repo: meta.GetExternalName(cr),
276280
}
277281

282+
if cr.Spec.ForProvider.Project != nil {
283+
repoQuery.AppProject = *cr.Spec.ForProvider.Project
284+
}
285+
278286
_, err := e.client.DeleteRepository(ctx, &repoQuery)
279287

280288
return managed.ExternalDelete{}, errors.Wrap(err, errDeleteFailed)

pkg/controller/repositories/controller_test.go

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,112 @@ func TestObserve(t *testing.T) {
255255
err: nil,
256256
},
257257
},
258+
"SuccessfulWithAppProject": {
259+
args: args{
260+
client: withMockClient(t, func(mcs *mockclient.MockRepositoryServiceClient) {
261+
mcs.EXPECT().Get(
262+
context.Background(),
263+
&argocdRepository.RepoQuery{
264+
Repo: testRepositoryExternalName,
265+
AppProject: "test-project",
266+
},
267+
).Return(
268+
&argocdv1alpha1.Repository{
269+
Repo: testRepo,
270+
Name: testRepositoryExternalName,
271+
Project: "test-project",
272+
}, nil)
273+
}),
274+
cr: Repository(
275+
withExternalName(testRepositoryExternalName),
276+
withSpec(v1alpha1.RepositoryParameters{
277+
Name: ptr.To(testRepositoryExternalName),
278+
Repo: testRepo,
279+
Project: ptr.To("test-project"),
280+
Insecure: &testInsecure,
281+
EnableLFS: &testEnableLFS,
282+
InheritedCreds: &testInheritedCreds,
283+
EnableOCI: &testEnableOCI,
284+
}),
285+
),
286+
},
287+
want: want{
288+
cr: Repository(
289+
withExternalName(testRepositoryExternalName),
290+
withSpec(v1alpha1.RepositoryParameters{
291+
Name: ptr.To(testRepositoryExternalName),
292+
Repo: testRepo,
293+
Project: ptr.To("test-project"),
294+
Insecure: &testInsecure,
295+
EnableLFS: &testEnableLFS,
296+
InheritedCreds: &testInheritedCreds,
297+
EnableOCI: &testEnableOCI,
298+
}),
299+
withConditions(xpv1.Available()),
300+
withObservation(v1alpha1.RepositoryObservation{
301+
ConnectionState: v1alpha1.ConnectionState{},
302+
}),
303+
),
304+
result: managed.ExternalObservation{
305+
ResourceExists: true,
306+
ResourceUpToDate: true,
307+
ResourceLateInitialized: false,
308+
},
309+
err: nil,
310+
},
311+
},
312+
"SuccessfulWithoutAppProject": {
313+
args: args{
314+
client: withMockClient(t, func(mcs *mockclient.MockRepositoryServiceClient) {
315+
mcs.EXPECT().Get(
316+
context.Background(),
317+
&argocdRepository.RepoQuery{
318+
Repo: testRepositoryExternalName,
319+
},
320+
).Return(
321+
&argocdv1alpha1.Repository{
322+
Repo: testRepo,
323+
Name: testRepositoryExternalName,
324+
}, nil)
325+
}),
326+
cr: Repository(
327+
withExternalName(testRepositoryExternalName),
328+
withSpec(v1alpha1.RepositoryParameters{
329+
Name: ptr.To(testRepositoryExternalName),
330+
Repo: testRepo,
331+
Project: nil, // Explicitly nil
332+
Insecure: &testInsecure,
333+
EnableLFS: &testEnableLFS,
334+
InheritedCreds: &testInheritedCreds,
335+
EnableOCI: &testEnableOCI,
336+
}),
337+
),
338+
},
339+
want: want{
340+
cr: Repository(
341+
withExternalName(testRepositoryExternalName),
342+
withSpec(v1alpha1.RepositoryParameters{
343+
Name: ptr.To(testRepositoryExternalName),
344+
Repo: testRepo,
345+
Project: nil,
346+
Insecure: &testInsecure,
347+
EnableLFS: &testEnableLFS,
348+
InheritedCreds: &testInheritedCreds,
349+
EnableOCI: &testEnableOCI,
350+
}),
351+
withConditions(xpv1.Available()),
352+
withObservation(v1alpha1.RepositoryObservation{
353+
ConnectionState: v1alpha1.ConnectionState{},
354+
}),
355+
),
356+
result: managed.ExternalObservation{
357+
ResourceExists: true,
358+
ResourceUpToDate: true,
359+
ResourceLateInitialized: false,
360+
},
361+
err: nil,
362+
},
363+
},
258364
}
259365

260366
for name, tc := range cases {
@@ -551,6 +657,67 @@ func TestDelete(t *testing.T) {
551657
err: errors.Wrap(errBoom, errDeleteFailed),
552658
},
553659
},
660+
"SuccessfulWithAppProject": {
661+
args: args{
662+
client: withMockClient(t, func(mcs *mockclient.MockRepositoryServiceClient) {
663+
mcs.EXPECT().DeleteRepository(
664+
context.Background(),
665+
&argocdRepository.RepoQuery{
666+
Repo: testRepositoryExternalName,
667+
AppProject: "test-project",
668+
},
669+
).Return(
670+
&argocdRepository.RepoResponse{}, nil)
671+
}),
672+
cr: Repository(
673+
withExternalName(testRepositoryExternalName),
674+
withSpec(v1alpha1.RepositoryParameters{
675+
Repo: testRepositoryExternalName,
676+
Project: ptr.To("test-project"),
677+
}),
678+
),
679+
},
680+
want: want{
681+
cr: Repository(
682+
withExternalName(testRepositoryExternalName),
683+
withSpec(v1alpha1.RepositoryParameters{
684+
Repo: testRepositoryExternalName,
685+
Project: ptr.To("test-project"),
686+
}),
687+
),
688+
err: nil,
689+
},
690+
},
691+
"SuccessfulWithoutAppProject": {
692+
args: args{
693+
client: withMockClient(t, func(mcs *mockclient.MockRepositoryServiceClient) {
694+
mcs.EXPECT().DeleteRepository(
695+
context.Background(),
696+
&argocdRepository.RepoQuery{
697+
Repo: testRepositoryExternalName,
698+
},
699+
).Return(
700+
&argocdRepository.RepoResponse{}, nil)
701+
}),
702+
cr: Repository(
703+
withExternalName(testRepositoryExternalName),
704+
withSpec(v1alpha1.RepositoryParameters{
705+
Repo: testRepositoryExternalName,
706+
Project: nil, // Explicitly nil
707+
}),
708+
),
709+
},
710+
want: want{
711+
cr: Repository(
712+
withExternalName(testRepositoryExternalName),
713+
withSpec(v1alpha1.RepositoryParameters{
714+
Repo: testRepositoryExternalName,
715+
Project: nil,
716+
}),
717+
),
718+
err: nil,
719+
},
720+
},
554721
}
555722

556723
for name, tc := range cases {

0 commit comments

Comments
 (0)