@@ -5,31 +5,164 @@ package server
55
66import (
77 "context"
8+ "time"
89
910 "google.golang.org/grpc/codes"
11+ "google.golang.org/protobuf/types/known/timestamppb"
1012
1113 computev1 "github.com/open-edge-platform/infra-core/apiv2/v2/internal/pbapi/resources/compute/v1"
14+ statusv1 "github.com/open-edge-platform/infra-core/apiv2/v2/internal/pbapi/resources/status/v1"
1215 restv1 "github.com/open-edge-platform/infra-core/apiv2/v2/internal/pbapi/services/v1"
16+ inv_computev1 "github.com/open-edge-platform/infra-core/inventory/v2/pkg/api/compute/v1"
17+ inventory "github.com/open-edge-platform/infra-core/inventory/v2/pkg/api/inventory/v1"
1318 "github.com/open-edge-platform/infra-core/inventory/v2/pkg/errors"
19+ "github.com/open-edge-platform/infra-core/inventory/v2/pkg/validator"
1420)
1521
16- func (is * InventorygRPCServer ) ListOSUpdateRun (_ context.Context , _ * restv1.ListOSUpdateRunRequest ) (
22+ func parseTimestamp (ts string ) (* timestamppb.Timestamp , error ) {
23+ if ts == "" {
24+ zlog .Warn ().Msgf ("timestamp is empty" )
25+ return nil , errors .Errorfc (
26+ codes .InvalidArgument , "timestamp is empty" ,
27+ )
28+ }
29+ parsedTime , err := time .Parse (ISO8601TimeFormat , ts )
30+ if err != nil {
31+ zlog .Warn ().Err (err ).Msgf ("Failed to parse timestamp: %s" , ts )
32+ return nil , err
33+ }
34+ return timestamppb .New (parsedTime ), nil
35+ }
36+
37+ func fromInvOSUpdateRunResource (invOSUpdateRunResource * inv_computev1.OSUpdateRunResource ) (* computev1.OSUpdateRun , error ) {
38+ if invOSUpdateRunResource == nil {
39+ return & computev1.OSUpdateRun {}, nil
40+ }
41+ instance , err := fromInvInstance (invOSUpdateRunResource .GetInstance ())
42+ if err != nil {
43+ zlog .Warn ().Err (err ).Msgf ("Failed to get the inventory instance edge from OS Update Run resource" )
44+ return nil , err
45+ }
46+ invStatusTimestamp , err := parseTimestamp (invOSUpdateRunResource .GetStatusTimestamp ())
47+ if err != nil && err .Error () != errors .Errorfc (codes .InvalidArgument , "timestamp is empty" ).Error () {
48+ zlog .Warn ().Msgf ("Status timestamp parsing failed for OS Update Run resource: %s" , invOSUpdateRunResource .GetResourceId ())
49+ return nil , errors .Errorfc (
50+ codes .InvalidArgument , "status timestamp parsing failed for OS Update Run resource: %s" ,
51+ invOSUpdateRunResource .GetResourceId (),
52+ )
53+ }
54+ invStartTime , err := parseTimestamp (invOSUpdateRunResource .GetStartTime ())
55+ if err != nil && err .Error () != errors .Errorfc (codes .InvalidArgument , "timestamp is empty" ).Error () {
56+ zlog .Warn ().Msgf ("Start time parsing failed for OS Update Run resource: %s" , invOSUpdateRunResource .GetResourceId ())
57+ return nil , errors .Errorfc (
58+ codes .InvalidArgument , "start time parsing failed for OS Update Run resource: %s" ,
59+ invOSUpdateRunResource .GetResourceId (),
60+ )
61+ }
62+ invEndTime , err := parseTimestamp (invOSUpdateRunResource .GetEndTime ())
63+ if err != nil && err .Error () != errors .Errorfc (codes .InvalidArgument , "timestamp is empty" ).Error () {
64+ zlog .Warn ().Msgf ("End time parsing failed for OS Update Run resource: %s" , invOSUpdateRunResource .GetResourceId ())
65+ return nil , errors .Errorfc (
66+ codes .InvalidArgument , "end time parsing failed for OS Update Run resource: %s" ,
67+ invOSUpdateRunResource .GetResourceId (),
68+ )
69+ }
70+
71+ osUpdateRunResource := & computev1.OSUpdateRun {
72+ ResourceId : invOSUpdateRunResource .GetResourceId (),
73+ Name : invOSUpdateRunResource .GetName (),
74+ Description : invOSUpdateRunResource .GetDescription (),
75+ AppliedPolicy : fromInvOSUpdatePolicy (invOSUpdateRunResource .GetAppliedPolicy ()),
76+ Instance : instance ,
77+ StatusIndicator : statusv1 .StatusIndication (invOSUpdateRunResource .GetStatusIndicator ()),
78+ Status : invOSUpdateRunResource .GetStatus (),
79+ StatusDetails : invOSUpdateRunResource .GetStatusDetails (),
80+ StatusTimestamp : invStatusTimestamp ,
81+ StartTime : invStartTime ,
82+ EndTime : invEndTime ,
83+ Timestamps : GrpcToOpenAPITimestamps (invOSUpdateRunResource ),
84+ }
85+ return osUpdateRunResource , nil
86+ }
87+
88+ func (is * InventorygRPCServer ) ListOSUpdateRun (ctx context.Context , req * restv1.ListOSUpdateRunRequest ) (
1789 * restv1.ListOSUpdateRunResponse , error ,
1890) {
19- // TODO implement me
20- return nil , errors .Errorfc (codes .Unimplemented , "ListOSUpdateRun not implemented" )
91+ zlog .Debug ().Msg ("ListOSUpdateRunResources" )
92+
93+ filter := & inventory.ResourceFilter {
94+ Resource : & inventory.Resource {
95+ Resource : & inventory.Resource_OsUpdateRun {
96+ OsUpdateRun : & inv_computev1.OSUpdateRunResource {},
97+ },
98+ },
99+ Offset : req .GetOffset (),
100+ Limit : req .GetPageSize (),
101+ OrderBy : req .GetOrderBy (),
102+ Filter : req .GetFilter (),
103+ }
104+ if err := validator .ValidateMessage (filter ); err != nil {
105+ zlog .InfraSec ().InfraErr (err ).Msg ("failed to validate query params" )
106+ return nil , errors .Wrap (err )
107+ }
108+ invResp , err := is .InvClient .List (ctx , filter )
109+ if err != nil {
110+ zlog .InfraErr (err ).Msg ("Failed to list OS resources from inventory" )
111+ return nil , errors .Wrap (err )
112+ }
113+
114+ osUpdateRunResources := []* computev1.OSUpdateRun {}
115+ for _ , invRes := range invResp .GetResources () {
116+ osUpdateRunResource , err := fromInvOSUpdateRunResource (invRes .GetResource ().GetOsUpdateRun ())
117+ if err != nil {
118+ zlog .InfraErr (err ).Msgf ("Failed to convert inventory OS Update Run resource %s" ,
119+ invRes .GetResource ().GetOsUpdateRun ().GetResourceId ())
120+ return nil , errors .Wrap (err )
121+ }
122+ osUpdateRunResources = append (osUpdateRunResources , osUpdateRunResource )
123+ }
124+
125+ resp := & restv1.ListOSUpdateRunResponse {
126+ OsUpdateRuns : osUpdateRunResources ,
127+ TotalElements : invResp .GetTotalElements (),
128+ HasNext : invResp .GetHasNext (),
129+ }
130+ zlog .Debug ().Msgf ("Listed %s" , resp )
131+ return resp , nil
21132}
22133
23- func (is * InventorygRPCServer ) GetOSUpdateRun (_ context.Context , _ * restv1.GetOSUpdateRunRequest ) (
134+ func (is * InventorygRPCServer ) GetOSUpdateRun (ctx context.Context , req * restv1.GetOSUpdateRunRequest ) (
24135 * computev1.OSUpdateRun , error ,
25136) {
26- // TODO implement me
27- return nil , errors .Errorfc (codes .Unimplemented , "GetOSUpdateRun not implemented" )
137+ zlog .Debug ().Msg ("GetOSUpdateRunResource" )
138+
139+ invResp , err := is .InvClient .Get (ctx , req .GetResourceId ())
140+ if err != nil {
141+ zlog .InfraErr (err ).Msg ("Failed to get OS Update Run resource from inventory" )
142+ return nil , errors .Wrap (err )
143+ }
144+
145+ invOSUpdateRunResource := invResp .GetResource ().GetOsUpdateRun ()
146+ osUpdateRunResource , err := fromInvOSUpdateRunResource (invOSUpdateRunResource )
147+ if err != nil {
148+ zlog .InfraErr (err ).Msgf ("Failed to convert inventory OS Update Run resource %s" , invOSUpdateRunResource .GetResourceId ())
149+ return nil , errors .Wrap (err )
150+ }
151+
152+ zlog .Debug ().Msgf ("Got %s" , osUpdateRunResource )
153+ return osUpdateRunResource , nil
28154}
29155
30- func (is * InventorygRPCServer ) DeleteOSUpdateRun (_ context.Context , _ * restv1.DeleteOSUpdateRunRequest ) (
156+ func (is * InventorygRPCServer ) DeleteOSUpdateRun (ctx context.Context , req * restv1.DeleteOSUpdateRunRequest ) (
31157 * restv1.DeleteOSUpdateRunResponse , error ,
32158) {
33- // TODO implement me
34- return nil , errors .Errorfc (codes .Unimplemented , "DeleteOSUpdateRun not implemented" )
159+ zlog .Debug ().Msg ("DeleteOSUpdateRunResource" )
160+
161+ _ , err := is .InvClient .Delete (ctx , req .GetResourceId ())
162+ if err != nil {
163+ zlog .InfraErr (err ).Msg ("Failed to delete OS Update Run resource from inventory" )
164+ return nil , errors .Wrap (err )
165+ }
166+ zlog .Debug ().Msgf ("Deleted %s" , req .GetResourceId ())
167+ return & restv1.DeleteOSUpdateRunResponse {}, nil
35168}
0 commit comments