kind: Rules
metadata:
  namespace: xl-bank
spec:
  - name: "xl-bank-labels"
    condition:
      deployedType: yak.Json
    processor:
      - type: freemarker
        description: "Adding the labels to the resulting YAML file"
        parameters:
          template: |
            { "metadata": {
                "labels": {
                  "environment":"${environment}}",
                  "version":"${version}"
                }
              }
            }
          variables:
            version: "#{#ctx.deployedApplication.version.name}"
            environment: "#{#ctx.deployedApplication.environment.name}"
      - type: freemarker
        description: "Multiplies replicas by 5"
        parameters:
          template: |
            { "spec" : {
               "replicas":"?"
              }
            }
      - type: patch
        description: "Using patching to create and change content"
        weight: 11
        merge:
          type: overlay
        phase: PATCH
        parameters:
          patch:
            - op: add
              path: "/metadata/labels/newLabel"
              value: "I was added"
            - op: replace
              path: "/metadata/labels/myLabel"
              value: "I was replaced"
            - op: remove
              path: "/metadata/labels/environment"
      - type: groovy
        description: "Using groovy inline script to create and change content"
        parameters:
          script: |
            import com.xebialabs.deployit.plugin.stitch.service.engine.processor.handler.groovy.MapFunction

            def stitch(spec, params, document, ctx) {
              document.map('$..labels', new MapFunction() {
                Object map(currentValue) {
                  currentValue.put("myGroovyInlineLabel", "myGroovyValue")
                  currentValue.put("inlineGroovyDeployedId", ctx.deployed.id)
                  currentValue
                }
              })
              document.getMutatedDocument()
            }
      - type: groovy
        description: "Using groovy script file to create and change content"
        parameters:
          scriptFile: AddLabels.groovy
