Skip to content

Commit 7bb04c5

Browse files
dubeecsantanapr
authored andcommitted
Optionally fetch code when getting an action (#2908)
1 parent 02626d7 commit 7bb04c5

4 files changed

Lines changed: 49 additions & 30 deletions

File tree

common/scala/src/main/scala/whisk/core/entity/Exec.scala

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -384,13 +384,7 @@ protected[core] object ExecMetaDataBase extends ArgNormalizer[ExecMetaDataBase]
384384
throw new DeserializationException(
385385
s"'image' must be a string defined in 'exec' for '${Exec.BLACKBOX}' actions")
386386
}
387-
val code: Option[String] = obj.fields.get("code") match {
388-
case Some(JsString(i)) => if (i.trim.nonEmpty) Some(i) else None
389-
case Some(_) =>
390-
throw new DeserializationException(
391-
s"if defined, 'code' must a string defined in 'exec' for '${Exec.BLACKBOX}' actions")
392-
case None => None
393-
}
387+
394388
val native = execManifests.blackboxImages.contains(image)
395389
BlackBoxExecMetaData(native)
396390

@@ -403,28 +397,9 @@ protected[core] object ExecMetaDataBase extends ArgNormalizer[ExecMetaDataBase]
403397

404398
manifest.attached
405399
.map { a =>
406-
val jar: Attachment[String] = {
407-
// java actions once stored the attachment in "jar" instead of "code"
408-
obj.fields.get("code").orElse(obj.fields.get("jar"))
409-
} map {
410-
attFmt[String].read(_)
411-
} getOrElse {
412-
throw new DeserializationException(
413-
s"'code' must be a valid base64 string in 'exec' for '$kind' actions")
414-
}
415-
val main = optMainField.orElse {
416-
if (manifest.requireMain.exists(identity)) {
417-
throw new DeserializationException(s"'main' must be a string defined in 'exec' for '$kind' actions")
418-
} else None
419-
}
420400
CodeExecMetaDataAsAttachment(manifest)
421401
}
422402
.getOrElse {
423-
val code: String = obj.fields.get("code") match {
424-
case Some(JsString(c)) => c
425-
case _ =>
426-
throw new DeserializationException(s"'code' must be a string defined in 'exec' for '$kind' actions")
427-
}
428403
CodeExecMetaDataAsString(manifest)
429404
}
430405
}

core/controller/src/main/resources/apiv1swagger.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,13 @@
203203
"description": "Name of action to fetch",
204204
"required": true,
205205
"type": "string"
206+
},
207+
{
208+
"name": "code",
209+
"in": "query",
210+
"description": "Include action code in the result",
211+
"required": false,
212+
"type": "boolean"
206213
}
207214
],
208215
"produces": [

core/controller/src/main/scala/whisk/core/controller/Actions.scala

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -294,10 +294,24 @@ trait WhiskActionsApi extends WhiskCollectionAPI with PostActionActivation with
294294
*/
295295
override def fetch(user: Identity, entityName: FullyQualifiedEntityName, env: Option[Parameters])(
296296
implicit transid: TransactionId) = {
297-
getEntity(WhiskAction, entityStore, entityName.toDocId, Some { action: WhiskAction =>
298-
val mergedAction = env map { action inherit _ } getOrElse action
299-
complete(OK, mergedAction)
300-
})
297+
parameter('code ? true) { code =>
298+
code match {
299+
case true =>
300+
getEntity(WhiskAction, entityStore, entityName.toDocId, Some { action: WhiskAction =>
301+
val mergedAction = env map {
302+
action inherit _
303+
} getOrElse action
304+
complete(OK, mergedAction)
305+
})
306+
case false =>
307+
getEntity(WhiskActionMetaData, entityStore, entityName.toDocId, Some { action: WhiskActionMetaData =>
308+
val mergedAction = env map {
309+
action inherit _
310+
} getOrElse action
311+
complete(OK, mergedAction)
312+
})
313+
}
314+
}
301315
}
302316

303317
/**

tests/src/test/scala/whisk/core/controller/test/ActionsApiTests.scala

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,29 @@ class ActionsApiTests extends ControllerTestCommon with WhiskActionsApi {
159159
}
160160
}
161161

162+
it should "get action using code query parameter" in {
163+
implicit val tid = transid()
164+
val action = WhiskAction(namespace, aname(), jsDefault("??"), Parameters("x", "b"))
165+
166+
put(entityStore, action)
167+
168+
Get(s"$collectionPath/${action.name}?code=false") ~> Route.seal(routes(creds)) ~> check {
169+
status should be(OK)
170+
val response = responseAs[JsObject]
171+
response.fields("exec").asJsObject.fields should not(contain key "code")
172+
responseAs[WhiskActionMetaData] shouldBe a[WhiskActionMetaData]
173+
}
174+
175+
Seq(s"$collectionPath/${action.name}", s"$collectionPath/${action.name}?code=true").foreach { path =>
176+
Get(path) ~> Route.seal(routes(creds)) ~> check {
177+
status should be(OK)
178+
val response = responseAs[JsObject]
179+
response.fields("exec").asJsObject.fields("code") should be("??".toJson)
180+
responseAs[WhiskAction] shouldBe a[WhiskAction]
181+
}
182+
}
183+
}
184+
162185
it should "report NotFound for get non existent action" in {
163186
implicit val tid = transid()
164187
Get(s"$collectionPath/xyz") ~> Route.seal(routes(creds)) ~> check {

0 commit comments

Comments
 (0)