@@ -25,9 +25,6 @@ import (
2525
2626 "{{ .GophercloudModule }}"
2727 corev1 "k8s.io/api/core/v1"
28- {{- if len .ImportDependencies }}
29- apierrors "k8s.io/apimachinery/pkg/api/errors"
30- {{- end }}
3128 "k8s.io/utils/ptr"
3229 ctrl "sigs.k8s.io/controller-runtime"
3330 "sigs.k8s.io/controller-runtime/pkg/client"
@@ -37,6 +34,9 @@ import (
3734 "github.com/k-orc/openstack-resource-controller/v2/internal/controllers/generic/progress"
3835 "github.com/k-orc/openstack-resource-controller/v2/internal/logging"
3936 "github.com/k-orc/openstack-resource-controller/v2/internal/osclients"
37+ {{- if len .ImportDependencies }}
38+ "github.com/k-orc/openstack-resource-controller/v2/internal/util/dependency"
39+ {{- end }}
4040 orcerrors "github.com/k-orc/openstack-resource-controller/v2/internal/util/errors"
4141)
4242
@@ -106,24 +106,12 @@ func (actuator {{ .PackageName }}Actuator) ListOSResourcesForImport(ctx context.
106106 var reconcileStatus progress.ReconcileStatus
107107{{- range .ImportDependencies }}
108108{{ $depNameCamelCase := . | camelCase }}
109- {{ $depNameCamelCase }} := &orcv1alpha1.{{ . }}{}
110- if filter.{{ . }}Ref != nil {
111- {{ $depNameCamelCase }}Key := client.ObjectKey{Name: string(*filter.{{ . }}Ref), Namespace: obj.Namespace}
112- if err := actuator.k8sClient.Get(ctx, {{ $depNameCamelCase }}Key, {{ $depNameCamelCase }}); err != nil {
113- if apierrors.IsNotFound(err) {
114- reconcileStatus = reconcileStatus.WithReconcileStatus(
115- progress.WaitingOnObject("{{ . }}", {{ $depNameCamelCase }}Key.Name, progress.WaitingOnCreation))
116- } else {
117- reconcileStatus = reconcileStatus.WithReconcileStatus(
118- progress.WrapError(fmt.Errorf("fetching {{ $depNameCamelCase }} %s: %w", {{ $depNameCamelCase }}Key.Name, err)))
119- }
120- } else {
121- if !orcv1alpha1.IsAvailable({{ $depNameCamelCase }}) || {{ $depNameCamelCase }}.Status.ID == nil {
122- reconcileStatus = reconcileStatus.WithReconcileStatus(
123- progress.WaitingOnObject("{{ . }}", {{ $depNameCamelCase }}Key.Name, progress.WaitingOnReady))
124- }
125- }
126- }
109+ {{ $depNameCamelCase }}, rs := dependency.FetchDependency(
110+ ctx, actuator.k8sClient, obj.Namespace,
111+ filter.{{ . }}Ref, "{{ . }}",
112+ func(dep *orcv1alpha1.{{ . }}) bool { return orcv1alpha1.IsAvailable(dep) && dep.Status.ID != nil },
113+ )
114+ reconcileStatus = reconcileStatus.WithReconcileStatus(rs)
127115{{- end }}
128116
129117 if needsReschedule, _ := reconcileStatus.NeedsReschedule(); needsReschedule {
@@ -135,12 +123,12 @@ func (actuator {{ .PackageName }}Actuator) ListOSResourcesForImport(ctx context.
135123 Name: string(ptr.Deref(filter.Name, "")),
136124 Description: string(ptr.Deref(filter.Description, "")),
137125{{- range .ImportDependencies }}
138- {{ . }}: ptr.Deref({{ . | camelCase }}.Status.ID, ""),
126+ {{ . }}ID : ptr.Deref({{ . | camelCase }}.Status.ID, ""),
139127{{- end }}
140128 // TODO(scaffolding): Add more import filters
141129 }
142130
143- return actuator.osClient.List{{ .Kind }}s(ctx, listOpts), nil
131+ return actuator.osClient.List{{ .Kind }}s(ctx, listOpts), {{ if len .ImportDependencies }}reconcileStatus{{ else }} nil{{ end }}
144132}
145133
146134func (actuator {{ .PackageName }}Actuator) CreateResource(ctx context.Context, obj orcObjectPT) (*osResourceT, progress.ReconcileStatus) {
0 commit comments