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
다음은 한 레코드에 대한 전체 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"
}
}
}
}
}
}
}
와 같은 것을 사용하여이 작업을 수행하는 더 좋은 방법이있을 수 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
data
/code123
)post
)uri
해당 요소 의 필드에 대상 문자열이 포함되어 있는지 확인 합니다. find()
문자열이 없으면 -1을 반환합니다.uri
주어진에서이 key
당신이 찾고있는 문자열을 포함, 그것은 덮어 connectionId
당신이 원하는 값으로 필드를이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다