@@ -2,15 +2,16 @@ package actions
2
2
3
3
import (
4
4
"context"
5
- "errors"
6
5
"fmt"
7
6
"log/slog"
8
7
9
8
"connectrpc.com/connect"
10
9
"github.com/opentdf/platform/protocol/go/policy/actions"
11
10
"github.com/opentdf/platform/protocol/go/policy/actions/actionsconnect"
12
11
"github.com/opentdf/platform/service/logger"
12
+ "github.com/opentdf/platform/service/logger/audit"
13
13
"github.com/opentdf/platform/service/pkg/config"
14
+ "github.com/opentdf/platform/service/pkg/db"
14
15
"github.com/opentdf/platform/service/pkg/serviceregistry"
15
16
16
17
policyconfig "github.com/opentdf/platform/service/policy/config"
@@ -78,22 +79,118 @@ func NewRegistration(ns string, dbRegister serviceregistry.DBRegister) *servicer
78
79
}
79
80
}
80
81
81
- func (a * ActionService ) GetAction (context.Context , * connect.Request [actions.GetActionRequest ]) (* connect.Response [actions.GetActionResponse ], error ) {
82
- return nil , connect .NewError (connect .CodeUnimplemented , errors .New ("GetAction is not implemented" ))
82
+ func (a * ActionService ) GetAction (ctx context.Context , req * connect.Request [actions.GetActionRequest ]) (* connect.Response [actions.GetActionResponse ], error ) {
83
+ rsp := & actions.GetActionResponse {}
84
+
85
+ a .logger .DebugContext (ctx , "getting action" , slog .Any ("identifier" , req .Msg .GetIdentifier ()))
86
+
87
+ action , err := a .dbClient .GetAction (ctx , req .Msg )
88
+ if err != nil {
89
+ return nil , db .StatusifyError (err , db .ErrTextGetRetrievalFailed , slog .Any ("identifier" , req .Msg .GetIdentifier ()))
90
+ }
91
+ rsp .Action = action
92
+
93
+ return connect .NewResponse (rsp ), nil
83
94
}
84
95
85
- func (a * ActionService ) ListActions (context.Context , * connect.Request [actions.ListActionsRequest ]) (* connect.Response [actions.ListActionsResponse ], error ) {
86
- return nil , connect .NewError (connect .CodeUnimplemented , errors .New ("ListActions is not implemented" ))
96
+ func (a * ActionService ) ListActions (ctx context.Context , req * connect.Request [actions.ListActionsRequest ]) (* connect.Response [actions.ListActionsResponse ], error ) {
97
+ a .logger .DebugContext (ctx , "listing actions" )
98
+ rsp , err := a .dbClient .ListActions (ctx , req .Msg )
99
+ if err != nil {
100
+ return nil , db .StatusifyError (err , db .ErrTextListRetrievalFailed )
101
+ }
102
+ a .logger .DebugContext (ctx , "listed actions" )
103
+ return connect .NewResponse (rsp ), nil
87
104
}
88
105
89
- func (a * ActionService ) CreateAction (context.Context , * connect.Request [actions.CreateActionRequest ]) (* connect.Response [actions.CreateActionResponse ], error ) {
90
- return nil , connect .NewError (connect .CodeUnimplemented , errors .New ("CreateAction is not implemented" ))
106
+ func (a * ActionService ) CreateAction (ctx context.Context , req * connect.Request [actions.CreateActionRequest ]) (* connect.Response [actions.CreateActionResponse ], error ) {
107
+ a .logger .DebugContext (ctx , "creating action" , slog .String ("name" , req .Msg .GetName ()))
108
+ auditParams := audit.PolicyEventParams {
109
+ ActionType : audit .ActionTypeCreate ,
110
+ ObjectType : audit .ObjectTypeAction ,
111
+ }
112
+ rsp := & actions.CreateActionResponse {}
113
+
114
+ err := a .dbClient .RunInTx (ctx , func (txClient * policydb.PolicyDBClient ) error {
115
+ action , err := txClient .CreateAction (ctx , req .Msg )
116
+ if err != nil {
117
+ return err
118
+ }
119
+
120
+ auditParams .ObjectID = action .GetId ()
121
+ auditParams .Original = action
122
+ a .logger .Audit .PolicyCRUDSuccess (ctx , auditParams )
123
+
124
+ rsp .Action = action
125
+ return nil
126
+ })
127
+ if err != nil {
128
+ a .logger .Audit .PolicyCRUDFailure (ctx , auditParams )
129
+ return nil , db .StatusifyError (err , db .ErrTextCreationFailed , slog .String ("action" , req .Msg .String ()))
130
+ }
131
+ return connect .NewResponse (rsp ), nil
91
132
}
92
133
93
- func (a * ActionService ) UpdateAction (context.Context , * connect.Request [actions.UpdateActionRequest ]) (* connect.Response [actions.UpdateActionResponse ], error ) {
94
- return nil , connect .NewError (connect .CodeUnimplemented , errors .New ("UpdateAction is not implemented" ))
134
+ func (a * ActionService ) UpdateAction (ctx context.Context , req * connect.Request [actions.UpdateActionRequest ]) (* connect.Response [actions.UpdateActionResponse ], error ) {
135
+ actionID := req .Msg .GetId ()
136
+ a .logger .DebugContext (ctx , "updating action" , slog .String ("id" , actionID ))
137
+ rsp := & actions.UpdateActionResponse {}
138
+
139
+ auditParams := audit.PolicyEventParams {
140
+ ActionType : audit .ActionTypeUpdate ,
141
+ ObjectType : audit .ObjectTypeAction ,
142
+ ObjectID : actionID ,
143
+ }
144
+
145
+ err := a .dbClient .RunInTx (ctx , func (txClient * policydb.PolicyDBClient ) error {
146
+ original , err := txClient .GetAction (ctx , & actions.GetActionRequest {
147
+ Identifier : & actions.GetActionRequest_Id {
148
+ Id : actionID ,
149
+ },
150
+ })
151
+ if err != nil {
152
+ return err
153
+ }
154
+
155
+ updated , err := txClient .UpdateAction (ctx , req .Msg )
156
+ if err != nil {
157
+ return err
158
+ }
159
+
160
+ auditParams .Original = original
161
+ auditParams .Updated = updated
162
+ a .logger .Audit .PolicyCRUDSuccess (ctx , auditParams )
163
+
164
+ rsp .Action = updated
165
+ return nil
166
+ })
167
+ if err != nil {
168
+ a .logger .Audit .PolicyCRUDFailure (ctx , auditParams )
169
+ return nil , db .StatusifyError (err , db .ErrTextUpdateFailed , slog .String ("action" , req .Msg .String ()))
170
+ }
171
+
172
+ return connect .NewResponse (rsp ), nil
95
173
}
96
174
97
- func (a * ActionService ) DeleteAction (context.Context , * connect.Request [actions.DeleteActionRequest ]) (* connect.Response [actions.DeleteActionResponse ], error ) {
98
- return nil , connect .NewError (connect .CodeUnimplemented , errors .New ("DeleteAction is not implemented" ))
175
+ func (a * ActionService ) DeleteAction (ctx context.Context , req * connect.Request [actions.DeleteActionRequest ]) (* connect.Response [actions.DeleteActionResponse ], error ) {
176
+ rsp := & actions.DeleteActionResponse {}
177
+ actionID := req .Msg .GetId ()
178
+
179
+ auditParams := audit.PolicyEventParams {
180
+ ActionType : audit .ActionTypeDelete ,
181
+ ObjectType : audit .ObjectTypeAction ,
182
+ ObjectID : actionID ,
183
+ }
184
+ a .logger .DebugContext (ctx , "deleting action" , slog .String ("id" , actionID ))
185
+
186
+ deleted , err := a .dbClient .DeleteAction (ctx , req .Msg )
187
+ if err != nil {
188
+ a .logger .Audit .PolicyCRUDFailure (ctx , auditParams )
189
+ return nil , db .StatusifyError (err , db .ErrTextDeletionFailed , slog .String ("action" , req .Msg .String ()))
190
+ }
191
+
192
+ a .logger .Audit .PolicyCRUDSuccess (ctx , auditParams )
193
+ rsp .Action = deleted
194
+
195
+ return connect .NewResponse (rsp ), nil
99
196
}
0 commit comments