|
| 1 | +package adapters |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "log/slog" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/aws/aws-sdk-go-v2/service/apigateway" |
| 10 | + "github.com/overmindtech/aws-source/adapterhelpers" |
| 11 | + "github.com/overmindtech/sdp-go" |
| 12 | +) |
| 13 | + |
| 14 | +type apiGatewayIntegrationGetter interface { |
| 15 | + GetIntegration(ctx context.Context, params *apigateway.GetIntegrationInput, optFns ...func(*apigateway.Options)) (*apigateway.GetIntegrationOutput, error) |
| 16 | +} |
| 17 | + |
| 18 | +func apiGatewayIntegrationGetFunc(ctx context.Context, client apiGatewayIntegrationGetter, scope string, input *apigateway.GetIntegrationInput) (*sdp.Item, error) { |
| 19 | + output, err := client.GetIntegration(ctx, input) |
| 20 | + if err != nil { |
| 21 | + return nil, err |
| 22 | + } |
| 23 | + |
| 24 | + attributes, err := adapterhelpers.ToAttributesWithExclude(output, "tags") |
| 25 | + if err != nil { |
| 26 | + return nil, err |
| 27 | + } |
| 28 | + |
| 29 | + // We create a custom ID of {rest-api-id}/{resource-id}/{http-method} e.g. |
| 30 | + // rest-api-id/resource-id/GET |
| 31 | + integrationID := fmt.Sprintf( |
| 32 | + "%s/%s/%s", |
| 33 | + *input.RestApiId, |
| 34 | + *input.ResourceId, |
| 35 | + *input.HttpMethod, |
| 36 | + ) |
| 37 | + err = attributes.Set("IntegrationID", integrationID) |
| 38 | + if err != nil { |
| 39 | + return nil, err |
| 40 | + } |
| 41 | + |
| 42 | + item := &sdp.Item{ |
| 43 | + Type: "apigateway-integration", |
| 44 | + UniqueAttribute: "IntegrationID", |
| 45 | + Attributes: attributes, |
| 46 | + Scope: scope, |
| 47 | + } |
| 48 | + |
| 49 | + item.LinkedItemQueries = append(item.LinkedItemQueries, &sdp.LinkedItemQuery{ |
| 50 | + Query: &sdp.Query{ |
| 51 | + Type: "apigateway-method", |
| 52 | + Method: sdp.QueryMethod_GET, |
| 53 | + Query: fmt.Sprintf("%s/%s/%s", *input.RestApiId, *input.ResourceId, *input.HttpMethod), |
| 54 | + Scope: scope, |
| 55 | + }, |
| 56 | + BlastPropagation: &sdp.BlastPropagation{ |
| 57 | + // They are tightly coupled |
| 58 | + In: true, |
| 59 | + Out: true, |
| 60 | + }, |
| 61 | + }) |
| 62 | + |
| 63 | + if output.ConnectionId != nil { |
| 64 | + item.LinkedItemQueries = append(item.LinkedItemQueries, &sdp.LinkedItemQuery{ |
| 65 | + Query: &sdp.Query{ |
| 66 | + Type: "apigateway-vpc-link", |
| 67 | + Method: sdp.QueryMethod_GET, |
| 68 | + Query: *output.ConnectionId, |
| 69 | + Scope: scope, |
| 70 | + }, |
| 71 | + BlastPropagation: &sdp.BlastPropagation{ |
| 72 | + // If VPC link goes away, so does the integration |
| 73 | + In: true, |
| 74 | + // If integration goes away, VPC link is still there |
| 75 | + Out: false, |
| 76 | + }, |
| 77 | + }) |
| 78 | + } |
| 79 | + |
| 80 | + return item, nil |
| 81 | +} |
| 82 | + |
| 83 | +func NewAPIGatewayIntegrationAdapter(client apiGatewayIntegrationGetter, accountID string, region string) *adapterhelpers.AlwaysGetAdapter[*apigateway.GetIntegrationInput, *apigateway.GetIntegrationOutput, *apigateway.GetIntegrationInput, *apigateway.GetIntegrationOutput, apiGatewayIntegrationGetter, *apigateway.Options] { |
| 84 | + return &adapterhelpers.AlwaysGetAdapter[*apigateway.GetIntegrationInput, *apigateway.GetIntegrationOutput, *apigateway.GetIntegrationInput, *apigateway.GetIntegrationOutput, apiGatewayIntegrationGetter, *apigateway.Options]{ |
| 85 | + ItemType: "apigateway-integration", |
| 86 | + Client: client, |
| 87 | + AccountID: accountID, |
| 88 | + Region: region, |
| 89 | + AdapterMetadata: apiGatewayIntegrationAdapterMetadata, |
| 90 | + GetFunc: apiGatewayIntegrationGetFunc, |
| 91 | + GetInputMapper: func(scope, query string) *apigateway.GetIntegrationInput { |
| 92 | + // We are using a custom id of {rest-api-id}/{resource-id}/{http-method} e.g. |
| 93 | + // rest-api-id/resource-id/GET |
| 94 | + f := strings.Split(query, "/") |
| 95 | + if len(f) != 3 { |
| 96 | + slog.Error( |
| 97 | + "query must be in the format of: rest-api-id/resource-id/http-method", |
| 98 | + "found", |
| 99 | + query, |
| 100 | + ) |
| 101 | + |
| 102 | + return nil |
| 103 | + } |
| 104 | + |
| 105 | + return &apigateway.GetIntegrationInput{ |
| 106 | + RestApiId: &f[0], |
| 107 | + ResourceId: &f[1], |
| 108 | + HttpMethod: &f[2], |
| 109 | + } |
| 110 | + }, |
| 111 | + DisableList: true, |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +var apiGatewayIntegrationAdapterMetadata = Metadata.Register(&sdp.AdapterMetadata{ |
| 116 | + Type: "apigateway-integration", |
| 117 | + DescriptiveName: "API Gateway Integration", |
| 118 | + Category: sdp.AdapterCategory_ADAPTER_CATEGORY_NETWORK, |
| 119 | + SupportedQueryMethods: &sdp.AdapterSupportedQueryMethods{ |
| 120 | + Get: true, |
| 121 | + GetDescription: "Get an Integration by rest-api id, resource id, and http-method", |
| 122 | + Search: true, |
| 123 | + SearchDescription: "Search Integrations by ARN", |
| 124 | + }, |
| 125 | +}) |
0 commit comments