Skip to main content

A small tool for mutual conversion of JSON, YAML with embedded parameters.

Project description

convert2vars

A small tool for mutual conversion of JSON, YAML with embedded parameters. Parameter processing is handled by the powerful Jinja2 template engine. JSON, which is often used in APIs, and YAML, which is adopted by popular platforms such as kubernetes (k8s) and ansible, can be converted to each other and dynamic parameter value editing can be performed.

This document has been translated into English by machine translation. Please note that some parts may be inaccurate.

[Japanese]

Installation

$ pip install convert2vars

Requirements

  • Python 3.7 or later

Usage

Basic Usage

The following simple example shows how to convert a k8s Deployment file. This file contains two parameters (REPLICAS and CONTAINER_PORT), which are converted to the specified values by using the -e option.

$ cat k8s-deployment.yml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: {{ REPLICAS | default(2, True) }}
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: {{ CONTAINER_PORT | default(80, True) }}

$ convert2vars convert -e REPLICAS=4 -e CONTAINER_PORT=8080 -t k8s-deployment.yml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 4
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - image: nginx:1.14.2
        name: nginx
        ports:
        - containerPort: 8080

Specifying Parameters

Parameter values can be specified in the following ways, or a combination of several methods.

  • -e option

    • Specify by command line option as described above. Multiple parameters can also be specified.
  • Environment variables

    • After setting environment variables, execution with the -E option produces the same results as described above.
    $ export REPLICAS=4
    $ export CONTAINER_PORT=8080
    $ convert2vars convert -t k8s-deployment.yml -E
    
    • --dotenv-file option can also be specified to use a combination of dotenv files.
    $ cat dotenv-temp
    CONTAINER_PORT=8080
    REPLICAS=4
    
    $ convert2vars convert -t k8s-deployment.yml --dotenv-file dotenv-temp -E
    
  • Parameter files

    • By using a file that enumerates parameters and their values, many parameters can be specified at once.
    • JSON, YAML, and ini format files are available. The following three parameter files all produce the same results as in the example with the -e option described above.
    ## JSON format (k8s-parameter.json)
    {
    "REPLICAS": 3,
    "CONTAINER_PORT": 8080
    }
    
    ## YAML format (k8s-parameter.yml)
    REPLICAS: 3
    CONTAINER_PORT: 8080
    
    ## ini format (k8s-parameter.ini)
    [default]
    REPLICAS=3
    CONTAINER_PORT=8080
    
    • When using a parameter file, use the -i and -t options together.
    $ convert2vars convert -t k8s-deployment.yml -i k8s-parameter.json
    

Specifying complex parameter values

Parameter values with complex structures such as lists and hashes can be specified in JSON format. Parameter values enclosed in [[] or {} are handled as JSON format.

The following is an example of specifying a list and a hash as parameter values.

$ cat complex_parameters.yml
instances: {{ TARGET_INSTANCES }}
instance_properties: {{ INSTANCE_PROPERTIES }}

$ convert2vars convert -e TARGET_INSTANCES='["vm01","vm02"]' -e INSTANCE_PROPERTIES='{"instance_type": "m1.small"}' -t complex_parameters.yml
instances: ['vm01', 'vm02']
instance_properties: {'instance_type': 'm1.small'}

Also, by using a parameter file, complex parameter values can be specified in YAML as well as JSON. The following results are the same as the previous example with the -e option.

$ cat complex_parameters.yml
TARGET_INSTANCES:
  - "vm01"
  - "vm02"
INSTANCE_PROPERTIES:
  instance_type: "m1.small"

$ convert2vars convert -i complex_parameters.yml -t complex_parameters.yml

Template engine support

The filter, conditional branching, and iteration functions provided by Jinja2 can be used without modification. In the following example, since the parameter values are not explicitly specified, they are converted to the default values of 2 and 80 by Jinja2's default filter.

$ convert2vars convert -t samples/templates/k8s-deployment.yml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 2
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - image: nginx:1.14.2
        name: nginx
        ports:
        - containerPort: 80

Format Conversion

For JSON and YAML, it is possible to mutually convert formats. In the following example, the -f option is used to convert to JSON format. To perform format conversion, do not use the -t option, but specify the conversion source file as a parameter file with the -i option.

$ convert2vars convert -e REPLICAS=4 -e CONTAINER_PORT=8080 -i k8s-deployment.yml -f json | jq .
{
  "apiVersion": "apps/v1",
  "kind": "Deployment",
  "metadata": {
    "name": "nginx-deployment"
  },
  "spec": {
    "replicas": 4,
    "template": {
      "metadata": {
        "labels": {
          "app": "nginx"
        }
      },
      "spec": {
        "containers": [
          {
            "name": "nginx",
            "image": "nginx:1.14.2",
            "ports": [
              {
                "containerPort": 8080
              }
            ]
          }
        ]
      }
    }
  }
}

License

The convert2vars module was written by Takeyuki Moriyama. convert2vars is released under the MIT license.

See the file LICENSE for more details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

convert2vars-1.0.2.tar.gz (12.9 kB view details)

Uploaded Source

Built Distribution

convert2vars-1.0.2-py3-none-any.whl (36.6 kB view details)

Uploaded Python 3

File details

Details for the file convert2vars-1.0.2.tar.gz.

File metadata

  • Download URL: convert2vars-1.0.2.tar.gz
  • Upload date:
  • Size: 12.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.2

File hashes

Hashes for convert2vars-1.0.2.tar.gz
Algorithm Hash digest
SHA256 2c4c645197024fc62c2b5621138fed0d5230bf3f062222b69859437b363260bf
MD5 508f41cd6a349c28ecc0c6f0d4bbb0f7
BLAKE2b-256 040cd2f9095f53317c8521f8589e7831268d8c7a89fa92099aa7eb907c43295c

See more details on using hashes here.

File details

Details for the file convert2vars-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: convert2vars-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 36.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.2

File hashes

Hashes for convert2vars-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 2c82a5a34a54967d12676c576e233f78b9e95dbbe9db697ea13f8faab1c84761
MD5 7b3a73d7d6ffd4c477a04bae10b6daab
BLAKE2b-256 4758b72bd17b084dff80738d3c65be348081f621acc65ce175d6a097559e7ac2

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page