URLに基づいて、uri
次のようなものを見つけてTest123.elb.us-east-1.amazonaws.com
からに変更する必要があります。connectionId
hkl876
xed763
例: から まで探してTest999.elb.us-east-1.amazonaws.com
更新connectionId
hkl876
klm812
ファイルのサンプル内容です。
"x-amazon-apigateway-integration": {
"uri": "http://Test123.elb.us-east-1.amazonaws.com:8765/emote",
"responses": {
"200": {
"statusCode": "200",
......
......
"connectionType": "VPC_LINK",
"connectionId": "hkl876",
"httpMethod": "POST",
"type": "http"
}
},
"x-amazon-apigateway-integration": {
"uri": "http://Test999.elb.us-east-1.amazonaws.com:4567/authcode/v1/remote",
"responses": {
"200": {
"statusCode": "200",
......
......
"connectionType": "VPC_LINK",
"connectionId": "hkl876",
"httpMethod": "PUT",
"type": "http"
}
ご提案いただきありがとうございます。
jsonファイル全体でこのソリューションを試すと、次のエラーメッセージが表示されます。
Traceback (most recent call last):
File "sample.py", line 16, in <module>
if data[key]['uri'].find("test123.elb.us-east-1.amazonaws.com") > 0:
TypeError: string indices must be integers
これは1つのレコードの完全なSwaggerファイルです。
{
"swagger": "2.0",
"info": {
"version": "2019-02-19T19:13:11Z"
},
"host": "abc.com",
"schemes": [
"http"
],
"paths": {
"/code123": {
"post": {
"produces": [
"application/json"
],
"parameters": [
{
"name": "x-correlationid",
"in": "header",
"required": true,
"type": "string"
},
{
"name": "content-type",
"in": "header",
"required": true,
"type": "string"
}
],
"responses": {
"200": {
"description": "200 response",
"schema": {
"$ref": "#/definitions/Empty"
},
"headers": {
"Access-Control-Allow-Origin": {
"type": "string"
}
}
},
"security": [
{
"RequestTokenAuthorizer": []
},
{
"api_key": []
}
],
"x-amazon-apigateway-integration": {
"uri": "http://test123.elb.us-east-1.amazonaws.com:2768/sample/code",
"responses": {
"200": {
"statusCode": "200",
"responseParameters": {
"method.response.header.Access-Control-Allow-Origin": "'*'"
}
},
"requestParameters": {
"integration.request.header.x-correlationid": "method.request.header.x-correlationid",
"integration.request.header.x-brand": "method.request.header.x-brand"
},
"passthroughBehavior": "when_no_templates",
"connectionType": "VPC_LINK",
"connectionId": "xyz879",
"httpMethod": "POST",
"type": "http"
}
}
}
}
}
}
}
答え1
同様にこれを行うより良い方法があるかもしれませんが、jq
私はこのツールを習得したことがありません。私はこれを達成するためにPythonを使います。更新されたJSONドキュメントを見ると、次のようになります。
{
"swagger": "2.0",
"info": {
"version": "2019-02-19T19:13:11Z"
},
"host": "abc.com",
"schemes": [
"http"
],
"paths": {
"/code123": {
"post": {
"produces": [
"application/json"
],
"parameters": [
{
"name": "x-correlationid",
"in": "header",
"required": true,
"type": "string"
},
{
"name": "content-type",
"in": "header",
"required": true,
"type": "string"
}
],
"responses": {
"200": {
"description": "200 response",
"schema": {
"$ref": "#/definitions/Empty"
},
"headers": {
"Access-Control-Allow-Origin": {
"type": "string"
}
}
},
"security": [
{
"RequestTokenAuthorizer": []
},
{
"api_key": []
}
],
"x-amazon-apigateway-integration": {
"uri": "http://test123.elb.us-east-1.amazonaws.com:2768/sample/code",
"responses": {
"200": {
"statusCode": "200",
"responseParameters": {
"method.response.header.Access-Control-Allow-Origin": "'*'"
}
},
"requestParameters": {
"integration.request.header.x-correlationid": "method.request.header.x-correlationid",
"integration.request.header.x-brand": "method.request.header.x-brand"
},
"passthroughBehavior": "when_no_templates",
"connectionType": "VPC_LINK",
"connectionId": "xyz879",
"httpMethod": "POST",
"type": "http"
}
}
}
}
}
}
}
次のPythonスクリプトを実行します(上記の例では命名ex.json
)。
#!/usr/bin/env python3
import json
with open('ex.json') as json_file:
data = json.load(json_file)
for path in data['paths']:
for method in data['paths'][path]:
if data['paths'][path][method]['responses']['x-amazon-apigateway-integration']['uri'].find("test123.elb.us-east-1.amazonaws.com") > 0:
data['paths'][path][method]['responses']['x-amazon-apigateway-integration']['responses']['connectionId'] = 'xed763'
print(json.dumps(data, indent=4))
connectionId
最初の項目のフィールドが変更され、次の出力が得られます。
{
"swagger": "2.0",
"info": {
"version": "2019-02-19T19:13:11Z"
},
"host": "abc.com",
"schemes": [
"http"
],
"paths": {
"/code123": {
"post": {
"produces": [
"application/json"
],
"parameters": [
{
"name": "x-correlationid",
"in": "header",
"required": true,
"type": "string"
},
{
"name": "content-type",
"in": "header",
"required": true,
"type": "string"
}
],
"responses": {
"200": {
"description": "200 response",
"schema": {
"$ref": "#/definitions/Empty"
},
"headers": {
"Access-Control-Allow-Origin": {
"type": "string"
}
}
},
"security": [
{
"RequestTokenAuthorizer": []
},
{
"api_key": []
}
],
"x-amazon-apigateway-integration": {
"uri": "http://test123.elb.us-east-1.amazonaws.com:2768/sample/code",
"responses": {
"200": {
"statusCode": "200",
"responseParameters": {
"method.response.header.Access-Control-Allow-Origin": "'*'"
}
},
"requestParameters": {
"integration.request.header.x-correlationid": "method.request.header.x-correlationid",
"integration.request.header.x-brand": "method.request.header.x-brand"
},
"passthroughBehavior": "when_no_templates",
"connectionType": "VPC_LINK",
"connectionId": "xed763",
"httpMethod": "POST",
"type": "http"
}
}
}
}
}
}
}
Pythonスクリプト:
- ファイルを開き、
ex.json
開いたファイルを呼び出します。json_file
- JSONをPython辞書に読み込みます。
data
- 文書のパスを繰り返します(例
/code123
:)。 - 各パスを繰り返す方法(例
post
:) uri
この要素のフィールドにターゲット文字列が含まれていることを確認してください。find()
文字列がない場合は-1を返します。uri
与えられたatにkey
探している文字列が含まれている場合は、connectionId
必要な値でフィールドを上書きします。- ループが完了したら(修正できます)、JSONを標準出力に印刷します。