Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not delete previous annotation and support delete annotation via CLI #4940

Merged
merged 3 commits into from
Aug 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ case class WhiskActionPut(exec: Option[Exec] = None,
limits: Option[ActionLimitsOption] = None,
version: Option[SemVer] = None,
publish: Option[Boolean] = None,
annotations: Option[Parameters] = None) {
annotations: Option[Parameters] = None,
delAnnotations: Option[Array[String]] = None) {

protected[core] def replace(exec: Exec) = {
WhiskActionPut(Some(exec), parameters, limits, version, publish, annotations)
Expand Down Expand Up @@ -643,5 +644,5 @@ object ActionLimitsOption extends DefaultJsonProtocol {
}

object WhiskActionPut extends DefaultJsonProtocol {
implicit val serdes = jsonFormat6(WhiskActionPut.apply)
implicit val serdes = jsonFormat7(WhiskActionPut.apply)
}
7 changes: 7 additions & 0 deletions core/controller/src/main/resources/apiv1swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -1910,6 +1910,13 @@
},
"description": "annotations on the item"
},
"delAnnotations": {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wish we can have a better name for this.
But for now, I have no good idea.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hm... regarding better name, i have no idea for this as well.

"type": "array",
"items": {
"type": "string"
},
"description": "The list of annotations to be deleted from the item"
},
"parameters": {
"type": "array",
"items": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,13 @@ trait WhiskActionsApi extends WhiskCollectionAPI with PostActionActivation with

val exec = content.exec getOrElse action.exec

val newAnnotations = content.delAnnotations
.map { annotationArray =>
annotationArray.foldRight(action.annotations)((a: String, b: Parameters) => b - a)
}
.map(_ ++ content.annotations)
.getOrElse(action.annotations ++ content.annotations)

WhiskAction(
action.namespace,
action.name,
Expand All @@ -545,7 +552,7 @@ trait WhiskActionsApi extends WhiskCollectionAPI with PostActionActivation with
limits,
content.version getOrElse action.version.upPatch,
content.publish getOrElse action.publish,
WhiskActionsApi.amendAnnotations(content.annotations getOrElse action.annotations, exec, create = false))
WhiskActionsApi.amendAnnotations(newAnnotations, exec, create = false))
.revision[WhiskAction](action.docinfo.rev)
}

Expand Down
5 changes: 5 additions & 0 deletions tests/src/test/scala/common/WskCliOperations.scala
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ class CliActionOperations(override val wsk: RunCliCmd)
docker: Option[String] = None,
parameters: Map[String, JsValue] = Map.empty,
annotations: Map[String, JsValue] = Map.empty,
delAnnotations: Array[String] = Array(),
parameterFile: Option[String] = None,
annotationFile: Option[String] = None,
timeout: Option[Duration] = None,
Expand Down Expand Up @@ -229,6 +230,10 @@ class CliActionOperations(override val wsk: RunCliCmd)
annotations flatMap { p =>
Seq("-a", p._1, p._2.compactPrint)
}
} ++ {
delAnnotations flatMap { p =>
Seq("--del-annotation", p)
}
} ++ {
parameterFile map { pf =>
Seq("-P", pf)
Expand Down
1 change: 1 addition & 0 deletions tests/src/test/scala/common/WskOperations.scala
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ trait ActionOperations extends DeleteFromCollectionOperations with ListOrGetFrom
docker: Option[String] = None,
parameters: Map[String, JsValue] = Map.empty,
annotations: Map[String, JsValue] = Map.empty,
delAnnotations: Array[String] = Array(),
parameterFile: Option[String] = None,
annotationFile: Option[String] = None,
timeout: Option[Duration] = None,
Expand Down
3 changes: 3 additions & 0 deletions tests/src/test/scala/common/rest/WskRestOperations.scala
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ class RestActionOperations(implicit val actorSystem: ActorSystem)
docker: Option[String] = None,
parameters: Map[String, JsValue] = Map.empty,
annotations: Map[String, JsValue] = Map.empty,
delAnnotations: Array[String] = Array(),
parameterFile: Option[String] = None,
annotationFile: Option[String] = None,
timeout: Option[Duration] = None,
Expand Down Expand Up @@ -364,6 +365,8 @@ class RestActionOperations(implicit val actorSystem: ActorSystem)
content = content + ("annotations" -> annos.toJson)
if (limits.nonEmpty)
content = content + ("limits" -> limits.toJson)
if (delAnnotations.nonEmpty)
content = content + ("delAnnotations" -> delAnnotations.toJson)
content
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,38 @@ class WskRestBasicUsageTests extends TestHelpers with WskTestHelpers with WskAct
}
}

it should "delete the given annotations using delAnnotations" in withAssetCleaner(wskprops) { (wp, assetHelper) =>
val name = "hello"

assetHelper.withCleaner(wsk.action, name) { (action, _) =>
val annotations = Map("key1" -> "value1".toJson, "key2" -> "value2".toJson)
action.create(name, Some(TestUtils.getTestActionFilename("hello.js")), annotations = annotations)
val annotationString = wsk.parseJsonString(wsk.action.get(name).stdout).fields("annotations").toString

annotationString should include(""""key":"key1"""")
annotationString should include(""""value":"value1"""")
annotationString should include(""""key":"key2"""")
annotationString should include(""""value":"value2"""")

//Delete key1 only
val delAnnotations = Array("key1")

action.create(
name,
Some(TestUtils.getTestActionFilename("hello.js")),
delAnnotations = delAnnotations,
update = true)
val newAnnotationString = wsk.parseJsonString(wsk.action.get(name).stdout).fields("annotations").toString

newAnnotationString should not include (""""key":"key1"""")
newAnnotationString should not include (""""value":"value1"""")
newAnnotationString should include(""""key":"key2"""")
newAnnotationString should include(""""value":"value2"""")

action.create(name, Some(TestUtils.getTestActionFilename("hello.js")), update = true)
}
}

it should "create, and get an action to verify file parameter and annotation parsing" in withAssetCleaner(wskprops) {
(wp, assetHelper) =>
val name = "actionAnnotAndParamParsing"
Expand Down
35 changes: 35 additions & 0 deletions tests/src/test/scala/system/basic/WskActionTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -357,4 +357,39 @@ class WskActionTests extends TestHelpers with WskTestHelpers with JsHelpers with
}
}

it should "not delete existing annotations when updating action with new annotation" in withAssetCleaner(wskprops) {
(wp, assetHelper) =>
val name = "hello"

assetHelper.withCleaner(wsk.action, name) { (action, _) =>
val annotations = Map("key1" -> "value1".toJson, "key2" -> "value2".toJson)
action.create(name, Some(TestUtils.getTestActionFilename("hello.js")), annotations = annotations)
val annotationString = wsk.parseJsonString(wsk.action.get(name).stdout).fields("annotations").toString

annotationString should include(""""key":"key1"""")
annotationString should include(""""value":"value1"""")
annotationString should include(""""key":"key2"""")
annotationString should include(""""value":"value2"""")

val newAnnotations = Map("key3" -> "value3".toJson, "key4" -> "value4".toJson)
action.create(
name,
Some(TestUtils.getTestActionFilename("hello.js")),
annotations = newAnnotations,
update = true)
val newAnnotationString = wsk.parseJsonString(wsk.action.get(name).stdout).fields("annotations").toString

newAnnotationString should include(""""key":"key1"""")
newAnnotationString should include(""""value":"value1"""")
newAnnotationString should include(""""key":"key2"""")
newAnnotationString should include(""""value":"value2"""")
newAnnotationString should include(""""key":"key3"""")
newAnnotationString should include(""""value":"value3"""")
newAnnotationString should include(""""key":"key4"""")
newAnnotationString should include(""""value":"value4"""")

action.create(name, Some(TestUtils.getTestActionFilename("hello.js")), update = true)
}
}

}