tysonnorris commented on a change in pull request #4935:
URL: https://github.com/apache/openwhisk/pull/4935#discussion_r468927539
##########
File path:
core/controller/src/main/scala/org/apache/openwhisk/core/controller/Actions.scala
##########
@@ -338,6 +336,56 @@ trait WhiskActionsApi extends WhiskCollectionAPI with
PostActionActivation with
deleteEntity(WhiskAction, entityStore, entityName.toDocId, (a:
WhiskAction) => Future.successful({}))
}
+ /**GET --save which retrieves all source code for an entity **/
+ private def getEntityWithSourceCode(entityName: FullyQualifiedEntityName,
env: Option[Parameters])(
+ implicit transid: TransactionId) = {
+ getEntity(WhiskAction.resolveActionAndMergeParameters(entityStore,
entityName), Some { action: WhiskAction =>
+ val mergedAction = env map {
+ action inherit _
+ } getOrElse action
+ complete(OK, mergedAction)
+ })
+ }
+
+ /**Standard GET which just retrieves metadata**/
+ private def getEntityMetaData(entityName: FullyQualifiedEntityName, env:
Option[Parameters])(
+ implicit transid: TransactionId) = {
+ getEntity(WhiskActionMetaData.resolveActionAndMergeParameters(entityStore,
entityName), Some {
+ action: WhiskActionMetaData =>
+ val mergedAction = env map {
+ action inherit _
+ } getOrElse action
+ complete(OK, mergedAction)
+ })
+ }
+
+ /** Checks for package binding case. we don't want to allow get for a
package binding in shared package */
+ private def fetchEntity(entityName: FullyQualifiedEntityName, env:
Option[Parameters], code: Boolean)(
+ implicit transid: TransactionId) = {
+ if (entityName.path.defaultPackage) {
+ if (code) {
+ getEntityWithSourceCode(entityName, env)
+ } else {
+ getEntityMetaData(entityName, env)
+ }
+ } else {
+ getEntity(
+ WhiskPackage.resolveBinding(entityStore, entityName.path.toDocId,
mergeParameters = true),
+ Some { pkg: WhiskPackage =>
+ val originalPackageLocation = pkg.fullyQualifiedName(withVersion =
false).namespace
+ if (executeOnly && originalPackageLocation != entityName.namespace) {
+ terminate(Forbidden,
forbiddenGetActionBinding(entityName.toDocId.asString))
+ } else {
+ if (code) {
+ getEntityWithSourceCode(entityName, env)
+ } else {
+ getEntityMetaData(entityName, env)
+ }
+ }
+ })
+ }
+ }
+
Review comment:
here's an example that condenses this a bit using `Either` to use either
entityName(if package is authorized) or forbidden message (if package is not
authorized), and then processes the future (similar to `getEntity`, but not
using `getEntity`):
```
/** Checks for package binding case. we don't want to allow get for a
package binding in shared package */
private def fetchEntity(entityName: FullyQualifiedEntityName, env:
Option[Parameters], code: Boolean)(
implicit transid: TransactionId) = {
val resolvedPkg: Future[Either[String, FullyQualifiedEntityName]] = if
(entityName.path.defaultPackage) {
Future.successful(Right(entityName))
} else {
WhiskPackage.resolveBinding(entityStore, entityName.path.toDocId,
mergeParameters = true).map { pkg =>
val originalPackageLocation = pkg.fullyQualifiedName(withVersion =
false).namespace
if (executeOnly && originalPackageLocation != entityName.namespace) {
Left(forbiddenGetActionBinding(entityName.toDocId.asString))
} else {
Right(entityName)
}
}
}
onComplete(resolvedPkg) {
case Success(pkgFuture) =>
pkgFuture match {
case Left(f) => terminate(Forbidden, f)
case Right(_) =>
if (code) {
getEntity(WhiskAction.resolveActionAndMergeParameters(entityStore, entityName),
Some {
action: WhiskAction =>
val mergedAction = env map {
action inherit _
} getOrElse action
complete(OK, mergedAction)
})
} else {
getEntity(WhiskActionMetaData.resolveActionAndMergeParameters(entityStore,
entityName), Some {
action: WhiskActionMetaData =>
val mergedAction = env map {
action inherit _
} getOrElse action
complete(OK, mergedAction)
})
}
}
case Failure(t: Throwable) =>
logging.error(this, s"[GET] package ${entityName.path.toDocId}
failed: ${t.getMessage}")
terminate(InternalServerError)
}
}
```
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]