@@ -19,10 +19,10 @@ package hostaggregate
1919import (
2020 "context"
2121 "iter"
22+ "strconv"
2223
2324 "github.com/gophercloud/gophercloud/v2/openstack/compute/v2/aggregates"
2425 corev1 "k8s.io/api/core/v1"
25- "k8s.io/utils/ptr"
2626 ctrl "sigs.k8s.io/controller-runtime"
2727 "sigs.k8s.io/controller-runtime/pkg/client"
2828
@@ -36,7 +36,7 @@ import (
3636
3737// OpenStack resource types
3838type (
39- osResourceT = aggregates.HostAggregate
39+ osResourceT = aggregates.Aggregate
4040
4141 createResourceActuator = interfaces.CreateResourceActuator [orcObjectPT , orcObjectT , filterT , osResourceT ]
4242 deleteResourceActuator = interfaces.DeleteResourceActuator [orcObjectPT , orcObjectT , osResourceT ]
@@ -52,12 +52,14 @@ type hostaggregateActuator struct {
5252var _ createResourceActuator = hostaggregateActuator {}
5353var _ deleteResourceActuator = hostaggregateActuator {}
5454
55+ // TODO(stephenfin): I suspect we need to change the interface since Nova expects integer IDs
5556func (hostaggregateActuator ) GetResourceID (osResource * osResourceT ) string {
56- return osResource .ID
57+ return strconv . Itoa ( osResource .ID )
5758}
5859
5960func (actuator hostaggregateActuator ) GetOSResourceByID (ctx context.Context , id string ) (* osResourceT , progress.ReconcileStatus ) {
60- resource , err := actuator .osClient .GetHostAggregate (ctx , id )
61+ iid , err := strconv .Atoi (id )
62+ resource , err := actuator .osClient .GetHostAggregate (ctx , iid )
6163 if err != nil {
6264 return nil , progress .WrapError (err )
6365 }
@@ -70,30 +72,36 @@ func (actuator hostaggregateActuator) ListOSResourcesForAdoption(ctx context.Con
7072 return nil , false
7173 }
7274
73- // TODO(scaffolding) If you need to filter resources on fields that the List() function
74- // of gophercloud does not support, it's possible to perform client-side filtering.
75- // Check osclients.ResourceFilter
75+ var filters []osclients.ResourceFilter [osResourceT ]
7676
77- listOpts := aggregates.ListOpts {
78- Name : getResourceName (orcObject ),
79- Description : ptr .Deref (resourceSpec .Description , "" ),
80- }
77+ // NOTE: The API doesn't allow filtering by name or description, we'll have to do it client-side.
78+ filters = append (filters ,
79+ func (f * aggregates.Aggregate ) bool {
80+ name := getResourceName (orcObject )
81+ // Compare non-pointer values
82+ return f .Name == name
83+ },
84+ )
8185
82- return actuator .osClient . ListHostAggregates (ctx , listOpts ), true
86+ return actuator .listOSResources (ctx , filters ), true
8387}
8488
8589func (actuator hostaggregateActuator ) ListOSResourcesForImport (ctx context.Context , obj orcObjectPT , filter filterT ) (iter.Seq2 [* osResourceT , error ], progress.ReconcileStatus ) {
86- // TODO(scaffolding) If you need to filter resources on fields that the List() function
87- // of gophercloud does not support, it's possible to perform client-side filtering.
88- // Check osclients.ResourceFilter
90+ var filters []osclients.ResourceFilter [osResourceT ]
8991
90- listOpts := aggregates.ListOpts {
91- Name : string (ptr .Deref (filter .Name , "" )),
92- Description : string (ptr .Deref (filter .Description , "" )),
93- // TODO(scaffolding): Add more import filters
92+ // NOTE: The API doesn't allow filtering by name or description, we'll have to do it client-side.
93+ if filter .Name != nil {
94+ filters = append (filters , func (f * aggregates.Aggregate ) bool {
95+ return f .Name == string (* filter .Name )
96+ })
9497 }
9598
96- return actuator .osClient .ListHostAggregates (ctx , listOpts ), nil
99+ return actuator .listOSResources (ctx , filters ), nil
100+ }
101+
102+ func (actuator hostaggregateActuator ) listOSResources (ctx context.Context , filters []osclients.ResourceFilter [osResourceT ]) iter.Seq2 [* aggregates.Aggregate , error ] {
103+ volumetypes := actuator .osClient .ListHostAggregates (ctx )
104+ return osclients .Filter (volumetypes , filters ... )
97105}
98106
99107func (actuator hostaggregateActuator ) CreateResource (ctx context.Context , obj orcObjectPT ) (* osResourceT , progress.ReconcileStatus ) {
@@ -106,8 +114,6 @@ func (actuator hostaggregateActuator) CreateResource(ctx context.Context, obj or
106114 }
107115 createOpts := aggregates.CreateOpts {
108116 Name : getResourceName (obj ),
109- Description : ptr .Deref (resource .Description , "" ),
110- // TODO(scaffolding): Add more fields
111117 }
112118
113119 osResource , err := actuator .osClient .CreateHostAggregate (ctx , createOpts )
@@ -138,7 +144,6 @@ func (actuator hostaggregateActuator) updateResource(ctx context.Context, obj or
138144 updateOpts := aggregates.UpdateOpts {}
139145
140146 handleNameUpdate (& updateOpts , obj , osResource )
141- handleDescriptionUpdate (& updateOpts , resource , osResource )
142147
143148 // TODO(scaffolding): add handler for all fields supporting mutability
144149
@@ -167,7 +172,7 @@ func (actuator hostaggregateActuator) updateResource(ctx context.Context, obj or
167172}
168173
169174func needsUpdate (updateOpts aggregates.UpdateOpts ) (bool , error ) {
170- updateOptsMap , err := updateOpts .ToHostAggregateUpdateMap ()
175+ updateOptsMap , err := updateOpts .ToAggregatesUpdateMap ()
171176 if err != nil {
172177 return false , err
173178 }
@@ -183,14 +188,7 @@ func needsUpdate(updateOpts aggregates.UpdateOpts) (bool, error) {
183188func handleNameUpdate (updateOpts * aggregates.UpdateOpts , obj orcObjectPT , osResource * osResourceT ) {
184189 name := getResourceName (obj )
185190 if osResource .Name != name {
186- updateOpts .Name = & name
187- }
188- }
189-
190- func handleDescriptionUpdate (updateOpts * aggregates.UpdateOpts , resource * resourceSpecT , osResource * osResourceT ) {
191- description := ptr .Deref (resource .Description , "" )
192- if osResource .Description != description {
193- updateOpts .Description = & description
191+ updateOpts .Name = name
194192 }
195193}
196194
0 commit comments