@@ -10,7 +10,6 @@ import (
1010 "fmt"
1111 "time"
1212
13- "github.com/Masterminds/semver/v3"
1413 "google.golang.org/grpc/codes"
1514 "google.golang.org/protobuf/types/known/fieldmaskpb"
1615
@@ -25,7 +24,6 @@ import (
2524 inv_status "github.com/open-edge-platform/infra-core/inventory/v2/pkg/status"
2625 "github.com/open-edge-platform/infra-core/inventory/v2/pkg/util"
2726 "github.com/open-edge-platform/infra-core/inventory/v2/pkg/validator"
28- "github.com/open-edge-platform/infra-managers/maintenance/pkg/status"
2927 inv_utils "github.com/open-edge-platform/infra-managers/maintenance/pkg/utils"
3028)
3129
@@ -159,56 +157,6 @@ func GetInstanceResourceByHostGUID(
159157 return instance , nil
160158}
161159
162- func GetOSUpdatePolicyByInstanceID (
163- ctx context.Context , c inv_client.TenantAwareInventoryClient , tenantID string , instanceID string ,
164- ) (* computev1.OSUpdatePolicyResource , error ) {
165- // TODO: Optimize and use caches, we could use ResourceID based caches.
166- zlog .Debug ().Msgf ("GetOSUpdatePolicyByInstanceID: tenantID=%s, InstanceID=%s" , tenantID , instanceID )
167- childCtx , cancel := context .WithTimeout (ctx , * inventoryTimeout )
168- defer cancel ()
169-
170- // First retrieve the Instance resource.
171- instanceResp , err := c .Get (childCtx , tenantID , instanceID )
172- if err != nil {
173- return nil , err
174- }
175- if err = validator .ValidateMessage (instanceResp ); err != nil {
176- zlog .InfraSec ().InfraErr (err ).Msg ("" )
177- return nil , errors .Wrap (err )
178- }
179- instance , err := util.UnwrapResource [* computev1.InstanceResource ](instanceResp .GetResource ())
180- if err != nil {
181- zlog .InfraSec ().InfraErr (err ).Msgf ("Failed to unwrap resource: %s" , instanceResp .GetResource ())
182- return nil , err
183- }
184- if instance .GetOsUpdatePolicy () == nil {
185- zlog .InfraSec ().Warn ().Msgf ("OSUpdatePolicy not present in the Instance Resource: %s" , instanceResp .GetResource ())
186- return & computev1.OSUpdatePolicyResource {}, nil
187- }
188-
189- // Now retrieve the OSUpdatePolicy resource, so we get all eager loaded fields.
190- osPolicyUpdateResp , err := c .Get (childCtx , tenantID , instance .GetOsUpdatePolicy ().GetResourceId ())
191- if err != nil {
192- zlog .InfraSec ().InfraErr (err ).Msgf (
193- "Failed to get OSUpdatePolicy: tenantID=%s, instanceID=%s" , tenantID , instanceID )
194- return nil , err
195- }
196- if err = validator .ValidateMessage (osPolicyUpdateResp ); err != nil {
197- zlog .InfraSec ().InfraErr (err ).Msg ("" )
198- return nil , errors .Wrap (err )
199- }
200- osUpdatePolicy , err := util.UnwrapResource [* computev1.OSUpdatePolicyResource ](osPolicyUpdateResp .GetResource ())
201- if err != nil {
202- zlog .InfraSec ().InfraErr (err ).Msgf ("Failed to unwrap resource: %s" , instanceResp .GetResource ())
203- return nil , err
204- }
205-
206- zlog .Debug ().Msgf ("GetOSUpdatePolicyByInstanceID: tenantID=%s, InstanceID=%s, OSUpdatePolicyID=%s" ,
207- tenantID , instanceID , osUpdatePolicy .GetResourceId ())
208-
209- return osUpdatePolicy , nil
210- }
211-
212160func UpdateInstance (
213161 ctx context.Context ,
214162 c inv_client.TenantAwareInventoryClient ,
@@ -310,72 +258,6 @@ func GetOSResourceIDByProfileInfo(ctx context.Context, c inv_client.TenantAwareI
310258 return osResID , nil
311259}
312260
313- func GetLatestImmutableOSByProfile (
314- ctx context.Context ,
315- c inv_client.TenantAwareInventoryClient ,
316- tenantID , profileName string ,
317- ) (* os_v1.OperatingSystemResource , error ) {
318- // TODO: Add caching layer
319- zlog .Debug ().Msgf ("GetLatestImmutableOSByProfile: tenantID=%s, profileName=%s" , tenantID , profileName )
320-
321- childCtx , cancel := context .WithTimeout (ctx , * inventoryTimeout )
322- defer cancel ()
323-
324- filter := fmt .Sprintf ("%s=%q AND %s=%s AND %s=%q" ,
325- os_v1 .OperatingSystemResourceFieldTenantId , tenantID ,
326- os_v1 .OperatingSystemResourceFieldOsType , os_v1 .OsType_OS_TYPE_IMMUTABLE .String (),
327- os_v1 .OperatingSystemResourceFieldProfileName , profileName ,
328- )
329-
330- resp , err := c .List (childCtx , & inv_v1.ResourceFilter {
331- Resource : & inv_v1.Resource {Resource : & inv_v1.Resource_Os {}},
332- Filter : filter ,
333- })
334- if err != nil {
335- return nil , err
336- }
337-
338- if len (resp .GetResources ()) == 0 {
339- return nil , errors .Errorfc (
340- codes .NotFound , "OS resource not found: tenantID=%s, profile_name=%s" , tenantID , profileName )
341- }
342-
343- // Find the OS profile with the highest semantic version using Masterminds/semver
344- var latestOS * os_v1.OperatingSystemResource
345- var latestVersion * semver.Version
346-
347- for _ , resource := range resp .GetResources () {
348- os := resource .GetResource ().GetOs ()
349- if err := validator .ValidateMessage (os ); err != nil {
350- zlog .Warn ().Err (err ).Msgf ("Invalid OS resource: %s" , os .GetResourceId ())
351- continue // Skip invalid OS resources
352- }
353-
354- currentVersionStr := os .GetProfileVersion ()
355- currentVersion , err := semver .NewVersion (currentVersionStr )
356- if err != nil {
357- zlog .Warn ().Err (err ).Msgf ("Failed to parse semantic version: %s" , currentVersionStr )
358- continue // Skip this OS if the version is invalid
359- }
360-
361- // If this is the first valid version we've seen, or if it's higher than our current latest
362- if latestVersion == nil || currentVersion .GreaterThan (latestVersion ) {
363- latestOS = os
364- latestVersion = currentVersion
365- }
366- }
367-
368- if latestOS == nil {
369- return nil , errors .Errorfc (
370- codes .NotFound , "No valid OS resource found: tenantID=%s, profile_name=%s" , tenantID , profileName )
371- }
372-
373- zlog .Debug ().Msgf ("Found OS resource with resourceID: %s, version: %s" ,
374- latestOS .GetResourceId (), latestVersion .String ())
375-
376- return latestOS , nil
377- }
378-
379261func GetOSResourceByID (
380262 ctx context.Context ,
381263 c inv_client.TenantAwareInventoryClient ,
@@ -408,144 +290,3 @@ func GetOSResourceByID(
408290
409291 return osResource , nil
410292}
411-
412- func CreateOSUpdateRun (
413- ctx context.Context , c inv_client.TenantAwareInventoryClient , tenantID string , osUpRun * computev1.OSUpdateRunResource ,
414- ) error {
415- childCtx , cancel := context .WithTimeout (ctx , * inventoryTimeout )
416- defer cancel ()
417-
418- zlog .Info ().Msgf ("Create a new OSUpdateRun resource: %v" , osUpRun )
419- res := & inv_v1.Resource {
420- Resource : & inv_v1.Resource_OsUpdateRun {
421- OsUpdateRun : osUpRun ,
422- },
423- }
424- runRes , err := c .Create (childCtx , tenantID , res )
425- if err != nil {
426- zlog .InfraSec ().InfraErr (err ).Msgf ("Failed to create OSUpdateRun resource. OSUpdateRun: %v" , res .GetOsUpdateRun ())
427- return err
428- }
429-
430- zlog .Info ().Msgf ("New OSUpdateRun resource created. OSUpdateRun: %v" , runRes )
431-
432- return err
433- }
434-
435- func DeleteOSUpdateRun (
436- ctx context.Context , c inv_client.TenantAwareInventoryClient , tenantID string , osUpRun * computev1.OSUpdateRunResource ,
437- ) error {
438- zlog .Info ().Msgf ("Delete OSUpdateRun resource: %v" , osUpRun )
439-
440- childCtx , cancel := context .WithTimeout (ctx , * inventoryTimeout )
441- defer cancel ()
442-
443- _ , err := c .Delete (childCtx , tenantID , osUpRun .GetResourceId ())
444- if err != nil {
445- zlog .InfraSec ().InfraErr (err ).Msgf ("Failed to delete OSUpdateRun resource, resourceID: %s" ,
446- osUpRun .GetResourceId ())
447- return err
448- }
449- zlog .Debug ().Msgf ("Deleted OSUpdateRun resource, resourseID: %s" , osUpRun .GetResourceId ())
450-
451- return err
452- }
453-
454- func UpdateOSUpdateRun (
455- ctx context.Context ,
456- c inv_client.TenantAwareInventoryClient ,
457- tenantID string ,
458- instanceID string ,
459- updateStatus * inv_status.ResourceStatus ,
460- updateStatusDetail string ,
461- runResID string ,
462- ) error {
463- zlog .Debug ().Msgf (
464- "UpdateInstanceStatus: tenantID=%s, InstanceID=%s, OSUpdateRunID=%s, NewUpdateStatus=%v, LastUpdateDetail=%s" ,
465- tenantID , instanceID , runResID , & updateStatus , updateStatusDetail )
466-
467- now := time .Now ().UTC ().Format (inv_utils .ISO8601Format )
468- run := & computev1.OSUpdateRunResource {
469- Status : updateStatus .Status ,
470- StatusIndicator : updateStatus .StatusIndicator ,
471- StatusTimestamp : now ,
472- }
473-
474- fields := []string {
475- computev1 .OSUpdateRunResourceFieldStatus ,
476- computev1 .OSUpdateRunResourceFieldStatusIndicator ,
477- computev1 .OSUpdateRunResourceFieldStatusTimestamp ,
478- }
479-
480- if updateStatusDetail != "" {
481- run .StatusDetails = updateStatusDetail
482- fields = append (fields , computev1 .OSUpdateRunResourceFieldStatusDetails )
483- }
484-
485- if updateStatus .Status == status .StatusCompleted ||
486- updateStatus .Status == status .StatusFailed {
487- run .EndTime = now
488- fields = append (fields , computev1 .OSUpdateRunResourceFieldEndTime )
489- }
490-
491- fieldMask , err := fieldmaskpb .New (run , fields ... )
492- if err != nil {
493- // This should never happen
494- zlog .InfraSec ().InfraErr (err ).Msg ("should never happen" )
495- return err
496- }
497-
498- childCtx , cancel := context .WithTimeout (ctx , * inventoryTimeout )
499- defer cancel ()
500-
501- _ , err = c .Update (childCtx , tenantID , runResID , fieldMask , & inv_v1.Resource {
502- Resource : & inv_v1.Resource_OsUpdateRun {
503- OsUpdateRun : run ,
504- },
505- })
506- return err
507- }
508-
509- func GetLatestOSUpdateRunByInstanceID (
510- ctx context.Context ,
511- c inv_client.TenantAwareInventoryClient ,
512- tenantID , instID string ,
513- ) (* computev1.OSUpdateRunResource , error ) {
514- // TODO: Add caching layer
515- zlog .Debug ().Msgf ("GetLatestOSUpdateRunByInstanceID: tenantID=%s, instance=%s" , tenantID , instID )
516-
517- childCtx , cancel := context .WithTimeout (ctx , * inventoryTimeout )
518- defer cancel ()
519-
520- filter := fmt .Sprintf ("%s=%q AND %s.%s=%q" ,
521- computev1 .OSUpdateRunResourceFieldTenantId , tenantID ,
522- computev1 .OSUpdateRunResourceEdgeInstance ,
523- computev1 .InstanceResourceFieldResourceId , instID ,
524- // TODO: unset computev1.OSUpdateRunResourceFieldEndTime,
525- )
526-
527- resp , err := c .List (childCtx , & inv_v1.ResourceFilter {
528- Resource : & inv_v1.Resource {Resource : & inv_v1.Resource_OsUpdateRun {}},
529- Filter : filter ,
530- OrderBy : "start_time desc" ,
531- Limit : 1 ,
532- })
533- if err != nil {
534- zlog .InfraSec ().InfraErr (err ).Msgf ("GetLatestOSUpdateRunByInstanceID: tenanatID=%s, instance=%s" , tenantID , instID )
535- return nil , err
536- }
537-
538- if len (resp .GetResources ()) == 0 {
539- return nil , errors .Errorfc (
540- codes .NotFound , "OSUpdateRun resource not found: tenantID=%s, instance=%s" , tenantID , instID )
541- }
542-
543- run := resp .GetResources ()[0 ].GetResource ().GetOsUpdateRun ()
544- if err := validator .ValidateMessage (run ); err != nil {
545- return nil , errors .Wrap (err )
546- }
547-
548- zlog .Debug ().Msgf ("Found OSUpdateRun resource with resourceID: %s" , run .GetResourceId ())
549-
550- return run , nil
551- }
0 commit comments