#!/usr/bin/env python3

import argparse
import json


# This is the location of the IaaS swagger
old_filename = "swagger/vra-iaas.json"
new_filename = "swagger/vra-iaas-fixed.json"


def replace_value(d, k, v, new):
    if k in d and d[k] == v:
        print(d[k])
        print("found one")
        d[k] = new
    for child in d.values():
        if isinstance(child, dict):
            replace_value(child, k, v, new)
        elif isinstance(child, list):
            for i in child:
                if isinstance(i, dict):
                    replace_value(i, k, v, new)


def rename_iaas_models(swagger):
    definitions = [
        ('Deployment', 'IaaSDeployment', '#/definitions/Deployment', '#/definitions/IaaSDeployment'),
        ('About', 'IaaSAbout', '#/definitions/About', '#/definitions/IaaSAbout'),
        ('Project', 'IaaSProject', '#/definitions/Project', '#/definitions/IaaSProject'),
        ('ProjectSpecification', 'IaaSProjectSpecification', '#/definitions/ProjectSpecification', '#/definitions/IaaSProjectSpecification'),
        ('ProjectResourceMetadata', 'IaaSProjectResourceMetadata', '#/definitions/ProjectResourceMetadata', '#/definitions/IaaSProjectResourceMetadata'),
    ]

    for (oldDef, newDef, oldRef, newRef) in definitions:
        v = swagger['definitions'][oldDef]
        del swagger['definitions'][oldDef]
        swagger['definitions'][newDef] = v
        replace_value(swagger, "$ref", oldRef, newRef)


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    args = parser.parse_args()

    # read in the swagger spec
    swagger = json.loads(open(old_filename).read())

    rename_iaas_models(swagger)

    # Overwrite the swagger spec
    f = open(new_filename, "w")
    f.write(json.dumps(swagger, sort_keys=False, indent=2))
    f.write('\n')

a2ae237bd93c07c5a56eb7b01a99e176163f7e58
<mask token>

<mask token>


def rename_iaas_models(swagger):
    definitions = [('Deployment', 'IaaSDeployment',
        '#/definitions/Deployment', '#/definitions/IaaSDeployment'), (
        'About', 'IaaSAbout', '#/definitions/About',
        '#/definitions/IaaSAbout'), ('Project', 'IaaSProject',
        '#/definitions/Project', '#/definitions/IaaSProject'), (
        'ProjectSpecification', 'IaaSProjectSpecification',
        '#/definitions/ProjectSpecification',
        '#/definitions/IaaSProjectSpecification'), (
        'ProjectResourceMetadata', 'IaaSProjectResourceMetadata',
        '#/definitions/ProjectResourceMetadata',
        '#/definitions/IaaSProjectResourceMetadata')]
    for oldDef, newDef, oldRef, newRef in definitions:
        v = swagger['definitions'][oldDef]
        del swagger['definitions'][oldDef]
        swagger['definitions'][newDef] = v
        replace_value(swagger, '$ref', oldRef, newRef)


<mask token>

<mask token>
old_filename = 'swagger/vra-iaas.json'
new_filename = 'swagger/vra-iaas-fixed.json'


def replace_value(d, k, v, new):
    if k in d and d[k] == v:
        print(d[k])
        print('found one')
        d[k] = new
    for child in d.values():
        if isinstance(child, dict):
            replace_value(child, k, v, new)
        elif isinstance(child, list):
            for i in child:
                if isinstance(i, dict):
                    replace_value(i, k, v, new)


def rename_iaas_models(swagger):
    definitions = [('Deployment', 'IaaSDeployment',
        '#/definitions/Deployment', '#/definitions/IaaSDeployment'), (
        'About', 'IaaSAbout', '#/definitions/About',
        '#/definitions/IaaSAbout'), ('Project', 'IaaSProject',
        '#/definitions/Project', '#/definitions/IaaSProject'), (
        'ProjectSpecification', 'IaaSProjectSpecification',
        '#/definitions/ProjectSpecification',
        '#/definitions/IaaSProjectSpecification'), (
        'ProjectResourceMetadata', 'IaaSProjectResourceMetadata',
        '#/definitions/ProjectResourceMetadata',
        '#/definitions/IaaSProjectResourceMetadata')]
    for oldDef, newDef, oldRef, newRef in definitions:
        v = swagger['definitions'][oldDef]
        del swagger['definitions'][oldDef]
        swagger['definitions'][newDef] = v
        replace_value(swagger, '$ref', oldRef, newRef)


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    args = parser.parse_args()
    swagger = json.loads(open(old_filename).read())
    rename_iaas_models(swagger)
    f = open(new_filename, 'w')
    f.write(json.dumps(swagger, sort_keys=False, indent=2))
    f.write('\n')

import argparse
import json
old_filename = 'swagger/vra-iaas.json'
new_filename = 'swagger/vra-iaas-fixed.json'


def replace_value(d, k, v, new):
    if k in d and d[k] == v:
        print(d[k])
        print('found one')
        d[k] = new
    for child in d.values():
        if isinstance(child, dict):
            replace_value(child, k, v, new)
        elif isinstance(child, list):
            for i in child:
                if isinstance(i, dict):
                    replace_value(i, k, v, new)


def rename_iaas_models(swagger):
    definitions = [('Deployment', 'IaaSDeployment',
        '#/definitions/Deployment', '#/definitions/IaaSDeployment'), (
        'About', 'IaaSAbout', '#/definitions/About',
        '#/definitions/IaaSAbout'), ('Project', 'IaaSProject',
        '#/definitions/Project', '#/definitions/IaaSProject'), (
        'ProjectSpecification', 'IaaSProjectSpecification',
        '#/definitions/ProjectSpecification',
        '#/definitions/IaaSProjectSpecification'), (
        'ProjectResourceMetadata', 'IaaSProjectResourceMetadata',
        '#/definitions/ProjectResourceMetadata',
        '#/definitions/IaaSProjectResourceMetadata')]
    for oldDef, newDef, oldRef, newRef in definitions:
        v = swagger['definitions'][oldDef]
        del swagger['definitions'][oldDef]
        swagger['definitions'][newDef] = v
        replace_value(swagger, '$ref', oldRef, newRef)


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    args = parser.parse_args()
    swagger = json.loads(open(old_filename).read())
    rename_iaas_models(swagger)
    f = open(new_filename, 'w')
    f.write(json.dumps(swagger, sort_keys=False, indent=2))
    f.write('\n')

#!/usr/bin/env python3

import argparse
import json


# This is the location of the IaaS swagger
old_filename = "swagger/vra-iaas.json"
new_filename = "swagger/vra-iaas-fixed.json"


def replace_value(d, k, v, new):
    if k in d and d[k] == v:
        print(d[k])
        print("found one")
        d[k] = new
    for child in d.values():
        if isinstance(child, dict):
            replace_value(child, k, v, new)
        elif isinstance(child, list):
            for i in child:
                if isinstance(i, dict):
                    replace_value(i, k, v, new)


def rename_iaas_models(swagger):
    definitions = [
        ('Deployment', 'IaaSDeployment', '#/definitions/Deployment', '#/definitions/IaaSDeployment'),
        ('About', 'IaaSAbout', '#/definitions/About', '#/definitions/IaaSAbout'),
        ('Project', 'IaaSProject', '#/definitions/Project', '#/definitions/IaaSProject'),
        ('ProjectSpecification', 'IaaSProjectSpecification', '#/definitions/ProjectSpecification', '#/definitions/IaaSProjectSpecification'),
        ('ProjectResourceMetadata', 'IaaSProjectResourceMetadata', '#/definitions/ProjectResourceMetadata', '#/definitions/IaaSProjectResourceMetadata'),
    ]

    for (oldDef, newDef, oldRef, newRef) in definitions:
        v = swagger['definitions'][oldDef]
        del swagger['definitions'][oldDef]
        swagger['definitions'][newDef] = v
        replace_value(swagger, "$ref", oldRef, newRef)


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    args = parser.parse_args()

    # read in the swagger spec
    swagger = json.loads(open(old_filename).read())

    rename_iaas_models(swagger)

    # Overwrite the swagger spec
    f = open(new_filename, "w")
    f.write(json.dumps(swagger, sort_keys=False, indent=2))
    f.write('\n')
