|
| 1 | +package adapters |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/aws/aws-sdk-go-v2/service/apigateway" |
| 9 | + "github.com/overmindtech/aws-source/adapterhelpers" |
| 10 | + "github.com/overmindtech/sdp-go" |
| 11 | +) |
| 12 | + |
| 13 | +type apigatewayClient interface { |
| 14 | + GetMethod(ctx context.Context, params *apigateway.GetMethodInput, optFns ...func(*apigateway.Options)) (*apigateway.GetMethodOutput, error) |
| 15 | + GetMethodResponse(ctx context.Context, params *apigateway.GetMethodResponseInput, optFns ...func(*apigateway.Options)) (*apigateway.GetMethodResponseOutput, error) |
| 16 | +} |
| 17 | + |
| 18 | +func apiGatewayMethodGetFunc(ctx context.Context, client apigatewayClient, scope string, input *apigateway.GetMethodInput) (*sdp.Item, error) { |
| 19 | + if input == nil { |
| 20 | + return nil, &sdp.QueryError{ |
| 21 | + ErrorType: sdp.QueryError_NOTFOUND, |
| 22 | + ErrorString: "query must be in the format of: the rest-api-id/resource-id/http-method", |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + output, err := client.GetMethod(ctx, input) |
| 27 | + if err != nil { |
| 28 | + return nil, err |
| 29 | + } |
| 30 | + |
| 31 | + attributes, err := adapterhelpers.ToAttributesWithExclude(output, "tags") |
| 32 | + if err != nil { |
| 33 | + return nil, err |
| 34 | + } |
| 35 | + |
| 36 | + // We create a custom ID of {rest-api-id}/{resource-id}/{http-method} e.g. |
| 37 | + // rest-api-id/resource-id/GET |
| 38 | + methodID := fmt.Sprintf( |
| 39 | + "%s/%s/%s", |
| 40 | + *input.RestApiId, |
| 41 | + *input.ResourceId, |
| 42 | + *input.HttpMethod, |
| 43 | + ) |
| 44 | + err = attributes.Set("MethodID", methodID) |
| 45 | + if err != nil { |
| 46 | + return nil, err |
| 47 | + } |
| 48 | + |
| 49 | + item := &sdp.Item{ |
| 50 | + Type: "apigateway-method", |
| 51 | + UniqueAttribute: "MethodID", |
| 52 | + Attributes: attributes, |
| 53 | + Scope: scope, |
| 54 | + } |
| 55 | + |
| 56 | + if output.MethodIntegration != nil { |
| 57 | + item.LinkedItemQueries = append(item.LinkedItemQueries, &sdp.LinkedItemQuery{ |
| 58 | + Query: &sdp.Query{ |
| 59 | + Type: "apigateway-integration", |
| 60 | + Method: sdp.QueryMethod_GET, |
| 61 | + Query: methodID, |
| 62 | + Scope: scope, |
| 63 | + }, |
| 64 | + BlastPropagation: &sdp.BlastPropagation{ |
| 65 | + // They are tightly coupled |
| 66 | + In: true, |
| 67 | + Out: true, |
| 68 | + }, |
| 69 | + }) |
| 70 | + } |
| 71 | + |
| 72 | + if output.AuthorizerId != nil { |
| 73 | + item.LinkedItemQueries = append(item.LinkedItemQueries, &sdp.LinkedItemQuery{ |
| 74 | + Query: &sdp.Query{ |
| 75 | + Type: "apigateway-authorizer", |
| 76 | + Method: sdp.QueryMethod_GET, |
| 77 | + Query: fmt.Sprintf("%s/%s", *input.RestApiId, *output.AuthorizerId), |
| 78 | + Scope: scope, |
| 79 | + }, |
| 80 | + BlastPropagation: &sdp.BlastPropagation{ |
| 81 | + // Deleting authorizer will affect the method |
| 82 | + In: true, |
| 83 | + // Deleting method won't affect the authorizer |
| 84 | + Out: false, |
| 85 | + }, |
| 86 | + }) |
| 87 | + } |
| 88 | + |
| 89 | + if output.RequestValidatorId != nil { |
| 90 | + item.LinkedItemQueries = append(item.LinkedItemQueries, &sdp.LinkedItemQuery{ |
| 91 | + Query: &sdp.Query{ |
| 92 | + Type: "apigateway-request-validator", |
| 93 | + Method: sdp.QueryMethod_GET, |
| 94 | + Query: fmt.Sprintf("%s/%s", *input.RestApiId, *output.RequestValidatorId), |
| 95 | + Scope: scope, |
| 96 | + }, |
| 97 | + BlastPropagation: &sdp.BlastPropagation{ |
| 98 | + // Deleting request validator will affect the method |
| 99 | + In: true, |
| 100 | + // Deleting method won't affect the request validator |
| 101 | + Out: false, |
| 102 | + }, |
| 103 | + }) |
| 104 | + } |
| 105 | + |
| 106 | + for statusCode := range output.MethodResponses { |
| 107 | + if input.RestApiId != nil && input.ResourceId != nil && input.HttpMethod != nil { |
| 108 | + item.LinkedItemQueries = append(item.LinkedItemQueries, &sdp.LinkedItemQuery{ |
| 109 | + Query: &sdp.Query{ |
| 110 | + Type: "apigateway-method-response", |
| 111 | + Method: sdp.QueryMethod_GET, |
| 112 | + Query: fmt.Sprintf("%s/%s/%s/%s", *input.RestApiId, *input.ResourceId, *input.HttpMethod, statusCode), |
| 113 | + Scope: scope, |
| 114 | + }, |
| 115 | + BlastPropagation: &sdp.BlastPropagation{ |
| 116 | + // They are tightly coupled |
| 117 | + In: true, |
| 118 | + Out: true, |
| 119 | + }, |
| 120 | + }) |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + return item, nil |
| 125 | +} |
| 126 | + |
| 127 | +func NewAPIGatewayMethodAdapter(client apigatewayClient, accountID string, region string) *adapterhelpers.AlwaysGetAdapter[*apigateway.GetMethodInput, *apigateway.GetMethodOutput, *apigateway.GetMethodInput, *apigateway.GetMethodOutput, apigatewayClient, *apigateway.Options] { |
| 128 | + return &adapterhelpers.AlwaysGetAdapter[*apigateway.GetMethodInput, *apigateway.GetMethodOutput, *apigateway.GetMethodInput, *apigateway.GetMethodOutput, apigatewayClient, *apigateway.Options]{ |
| 129 | + ItemType: "apigateway-method", |
| 130 | + Client: client, |
| 131 | + AccountID: accountID, |
| 132 | + Region: region, |
| 133 | + AdapterMetadata: apiGatewayMethodAdapterMetadata, |
| 134 | + GetFunc: apiGatewayMethodGetFunc, |
| 135 | + GetInputMapper: func(scope, query string) *apigateway.GetMethodInput { |
| 136 | + // We are using a custom id of {rest-api-id}/{resource-id}/{http-method} e.g. |
| 137 | + // rest-api-id/resource-id/GET |
| 138 | + f := strings.Split(query, "/") |
| 139 | + if len(f) != 3 { |
| 140 | + return nil |
| 141 | + } |
| 142 | + |
| 143 | + return &apigateway.GetMethodInput{ |
| 144 | + RestApiId: &f[0], |
| 145 | + ResourceId: &f[1], |
| 146 | + HttpMethod: &f[2], |
| 147 | + } |
| 148 | + }, |
| 149 | + DisableList: true, |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +var apiGatewayMethodAdapterMetadata = Metadata.Register(&sdp.AdapterMetadata{ |
| 154 | + Type: "apigateway-method", |
| 155 | + DescriptiveName: "API Gateway Method", |
| 156 | + Category: sdp.AdapterCategory_ADAPTER_CATEGORY_NETWORK, |
| 157 | + SupportedQueryMethods: &sdp.AdapterSupportedQueryMethods{ |
| 158 | + Get: true, |
| 159 | + GetDescription: "Get a Method by rest-api id, resource id and http-method", |
| 160 | + Search: true, |
| 161 | + SearchDescription: "Search Methods by ARN", |
| 162 | + }, |
| 163 | + PotentialLinks: []string{ |
| 164 | + "apigateway-integration", |
| 165 | + "apigateway-authorizer", |
| 166 | + "apigateway-request-validator", |
| 167 | + "apigateway-method-response", |
| 168 | + }, |
| 169 | +}) |
0 commit comments