crusadecoder
crusadecoder

Reputation: 669

Looping through a file in Groovy to create JSON Template

I am trying to construct a JSON template for a Service in Openshift. I need to setup a groovy script that will loop through a text file containing multiple ports in order to create the port section of the JSON template.

Here is what the file containing the ports look like.

cat ports.txt
  9000
  8090
  7010
  6012

Here is my groovy script to create the template. The script contains hard coded text for port 9000.

def builder = new groovy.json.JsonBuilder()
builder.apiVersion {
  apiVersion 'v1'
  kind 'Service'
  metadata {
    name 'apache'
    labels {
     app "apache"
     name "apache"
 }
}
spec {
selector {
  app 'apache'
 }
  ports {
  name "9000-tcp"
  protocol "TCP"
  port "9000"
  targetPort "9000"
  }
}
}
println builder.toPrettyString()

Running the script displays the following:

{
"apiVersion": {
    "apiVersion": "v1",
    "kind": "Service",
    "metadata": {
        "name": "apache",
        "labels": {
            "app": "apache",
            "name": "apache"
        }
    },
    "spec": {
        "selector": {
            "app": "apache"
        },
        "ports": {
            "name": "80-tcp",
            "protocol": "TCP",
            "port": "8081"
        }
    }
}

}

I would like the end product to look like this

{
    "apiVersion": "v1",
    "kind": "Service",
    "metadata": {
        "name": "apache",
        "labels": {
            "app": "apache",
            "name": "apache"
        }
    },
    "spec": {
        "selector": {
            "app": "apache"
        },
        "ports": {
            "name": "80-tcp",
            "protocol": "TCP",
            "port": "8081"
        }
        "ports": {
            "name": "8090-tcp",
            "protocol": "TCP",
            "port": "8090",
            "targetPort": "8090"
        }
        "ports": {
            "name": "7010-tcp",
            "protocol": "TCP",
            "port": "7010",
            "targetPort": "7010"
        }
        "ports": {
            "name": "6012-tcp",
            "protocol": "TCP",
            "port": "6012",
            "targetPort": "6012"
        }
    }
}
}

How can i embed a loop in this template to add create as many ports as there are in the file.txt file.

Upvotes: 1

Views: 512

Answers (1)

tim_yates
tim_yates

Reputation: 171114

The output you propose isn't valid JSON... You cannot have multiple ports objects at the same level as you show... You could however make ports into a list of objects like so:

def builder = new groovy.json.JsonBuilder()
builder {
    apiVersion 'v1'
    kind 'Service'
    metadata {
        name 'apache'
        labels {
            app "apache"
            name "apache"
        }
    }
    spec {
        selector {
            app 'apache'
        }
        ports new File('ports.txt').readLines()*.trim().collect { p ->
            [name: "$p-tcp", protocol: "TCP", port: "$p", targetPort: "$p"]
        }
    }
}

println builder.toPrettyString()

The output from that is:

    {
        "apiVersion": "v1",
        "kind": "Service",
        "metadata": {
            "name": "apache",
            "labels": {
                "app": "apache",
                "name": "apache"
            }
        },
        "spec": {
            "selector": {
                "app": "apache"
            },
            "ports": [
                {
                    "name": "9000-tcp",
                    "protocol": "TCP",
                    "port": "9000",
                    "targetPort": "9000"
                },
                {
                    "name": "8090-tcp",
                    "protocol": "TCP",
                    "port": "8090",
                    "targetPort": "8090"
                },
                {
                    "name": "7010-tcp",
                    "protocol": "TCP",
                    "port": "7010",
                    "targetPort": "7010"
                },
                {
                    "name": "6012-tcp",
                    "protocol": "TCP",
                    "port": "6012",
                    "targetPort": "6012"
                }
            ]
        }
    }

Upvotes: 2

Related Questions