@@ -23,10 +23,7 @@ import scala.util.Failure
23
23
import scala .util .Success
24
24
import akka .http .scaladsl .marshallers .sprayjson .SprayJsonSupport ._
25
25
import akka .http .scaladsl .model .StatusCode
26
- import akka .http .scaladsl .model .StatusCodes .Conflict
27
- import akka .http .scaladsl .model .StatusCodes .InternalServerError
28
- import akka .http .scaladsl .model .StatusCodes .NotFound
29
- import akka .http .scaladsl .model .StatusCodes .OK
26
+ import akka .http .scaladsl .model .StatusCodes ._
30
27
import akka .http .scaladsl .server .{Directives , RequestContext , RouteResult }
31
28
import spray .json .DefaultJsonProtocol ._
32
29
import spray .json .JsObject
@@ -37,6 +34,7 @@ import org.apache.openwhisk.common.TransactionId
37
34
import org .apache .openwhisk .core .controller .PostProcess .PostProcessEntity
38
35
import org .apache .openwhisk .core .database ._
39
36
import org .apache .openwhisk .core .entity .DocId
37
+ import org .apache .openwhisk .core .entity .WhiskAction
40
38
import org .apache .openwhisk .core .entity .WhiskDocument
41
39
import org .apache .openwhisk .http .ErrorResponse
42
40
import org .apache .openwhisk .http .ErrorResponse .terminate
@@ -242,7 +240,8 @@ trait WriteOps extends Directives {
242
240
update : A => Future [A ],
243
241
create : () => Future [A ],
244
242
treatExistsAsConflict : Boolean = true ,
245
- postProcess : Option [PostProcessEntity [A ]] = None )(
243
+ postProcess : Option [PostProcessEntity [A ]] = None ,
244
+ unlock : Boolean = false )(
246
245
implicit transid : TransactionId ,
247
246
format : RootJsonFormat [A ],
248
247
notifier : Option [CacheChangeNotification ],
@@ -267,8 +266,24 @@ trait WriteOps extends Directives {
267
266
} flatMap {
268
267
case (old, a) =>
269
268
logging.debug(this , s " [PUT] entity created/updated, writing back to datastore " )
270
- factory.put(datastore, a, old) map { _ =>
271
- a
269
+ if (overwrite && ! unlock && old.getOrElse(None ).isInstanceOf [WhiskAction ]) {
270
+ val oldWhiskAction = old.getOrElse(None ).asInstanceOf [WhiskAction ]
271
+ oldWhiskAction.annotations.get(WhiskAction .lockFieldName) match {
272
+ case Some (value) if (value.convertTo[Boolean ]) => {
273
+ Future failed RejectRequest (
274
+ MethodNotAllowed ,
275
+ s " this action can't be updated until ${WhiskAction .lockFieldName} annotation is updated to false " )
276
+ }
277
+ case _ => {
278
+ factory.put(datastore, a, old) map { _ =>
279
+ a
280
+ }
281
+ }
282
+ }
283
+ } else {
284
+ factory.put(datastore, a, old) map { _ =>
285
+ a
286
+ }
272
287
}
273
288
}) {
274
289
case Success (entity) =>
@@ -322,11 +337,30 @@ trait WriteOps extends Directives {
322
337
notifier : Option [CacheChangeNotification ],
323
338
ma : Manifest [A ]) = {
324
339
onComplete(factory.get(datastore, docid) flatMap { entity =>
325
- confirm(entity) flatMap {
326
- case _ =>
327
- factory.del(datastore, entity.docinfo) map { _ =>
328
- entity
340
+ if (entity.isInstanceOf [WhiskAction ]) {
341
+ val whiskAction = entity.asInstanceOf [WhiskAction ]
342
+ whiskAction.annotations.get(WhiskAction .lockFieldName) match {
343
+ case Some (value) if (value.convertTo[Boolean ]) => {
344
+ Future failed RejectRequest (
345
+ MethodNotAllowed ,
346
+ s " this action can't be deleted until ${WhiskAction .lockFieldName} annotation is updated to false " )
347
+ }
348
+ case _ => {
349
+ confirm(entity) flatMap {
350
+ case _ =>
351
+ factory.del(datastore, entity.docinfo) map { _ =>
352
+ entity
353
+ }
354
+ }
329
355
}
356
+ }
357
+ } else {
358
+ confirm(entity) flatMap {
359
+ case _ =>
360
+ factory.del(datastore, entity.docinfo) map { _ =>
361
+ entity
362
+ }
363
+ }
330
364
}
331
365
}) {
332
366
case Success (entity) =>
0 commit comments