|
| 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/aws/aws-sdk-go-v2/service/apigateway/types" |
| 10 | + "github.com/overmindtech/aws-source/adapterhelpers" |
| 11 | + "github.com/overmindtech/sdp-go" |
| 12 | +) |
| 13 | + |
| 14 | +func convertGetBasePathMappingOutputToBasePathMapping(output *apigateway.GetBasePathMappingOutput) *types.BasePathMapping { |
| 15 | + return &types.BasePathMapping{ |
| 16 | + BasePath: output.BasePath, |
| 17 | + RestApiId: output.RestApiId, |
| 18 | + Stage: output.Stage, |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +func basePathMappingOutputMapper(query, scope string, awsItem *types.BasePathMapping) (*sdp.Item, error) { |
| 23 | + attributes, err := adapterhelpers.ToAttributesWithExclude(awsItem, "tags") |
| 24 | + if err != nil { |
| 25 | + return nil, err |
| 26 | + } |
| 27 | + |
| 28 | + domainName := strings.Split(query, "/")[0] |
| 29 | + |
| 30 | + err = attributes.Set("UniqueAttribute", fmt.Sprintf("%s/%s", domainName, *awsItem.BasePath)) |
| 31 | + |
| 32 | + item := sdp.Item{ |
| 33 | + Type: "apigateway-base-path-mapping", |
| 34 | + UniqueAttribute: "BasePath", |
| 35 | + Attributes: attributes, |
| 36 | + Scope: scope, |
| 37 | + } |
| 38 | + |
| 39 | + item.LinkedItemQueries = append(item.LinkedItemQueries, &sdp.LinkedItemQuery{ |
| 40 | + Query: &sdp.Query{ |
| 41 | + Type: "apigateway-domain-name", |
| 42 | + Method: sdp.QueryMethod_GET, |
| 43 | + Query: domainName, |
| 44 | + Scope: scope, |
| 45 | + }, |
| 46 | + BlastPropagation: &sdp.BlastPropagation{ |
| 47 | + // If domain name changes, the base path mapping will be affected |
| 48 | + In: true, |
| 49 | + // If base path mapping changes, the domain name won't be affected |
| 50 | + Out: false, |
| 51 | + }, |
| 52 | + }) |
| 53 | + |
| 54 | + if awsItem.RestApiId != nil { |
| 55 | + item.LinkedItemQueries = append(item.LinkedItemQueries, &sdp.LinkedItemQuery{ |
| 56 | + Query: &sdp.Query{ |
| 57 | + Type: "apigateway-rest-api", |
| 58 | + Method: sdp.QueryMethod_GET, |
| 59 | + Query: *awsItem.RestApiId, |
| 60 | + Scope: scope, |
| 61 | + }, |
| 62 | + BlastPropagation: &sdp.BlastPropagation{ |
| 63 | + // They are tightly coupled, so we need to propagate the blast to the linked item |
| 64 | + In: true, |
| 65 | + Out: true, |
| 66 | + }, |
| 67 | + }) |
| 68 | + } |
| 69 | + |
| 70 | + return &item, nil |
| 71 | +} |
| 72 | + |
| 73 | +func NewAPIGatewayBasePathMappingAdapter(client *apigateway.Client, accountID string, region string) *adapterhelpers.GetListAdapter[*types.BasePathMapping, *apigateway.Client, *apigateway.Options] { |
| 74 | + return &adapterhelpers.GetListAdapter[*types.BasePathMapping, *apigateway.Client, *apigateway.Options]{ |
| 75 | + ItemType: "apigateway-base-path-mapping", |
| 76 | + Client: client, |
| 77 | + AccountID: accountID, |
| 78 | + Region: region, |
| 79 | + AdapterMetadata: basePathMappingAdapterMetadata, |
| 80 | + GetFunc: func(ctx context.Context, client *apigateway.Client, scope, query string) (*types.BasePathMapping, error) { |
| 81 | + f := strings.Split(query, "/") |
| 82 | + if len(f) != 2 { |
| 83 | + return nil, &sdp.QueryError{ |
| 84 | + ErrorType: sdp.QueryError_NOTFOUND, |
| 85 | + ErrorString: fmt.Sprintf("query must be in the format of: the domain-name/base-path, but found: %s", query), |
| 86 | + } |
| 87 | + } |
| 88 | + out, err := client.GetBasePathMapping(ctx, &apigateway.GetBasePathMappingInput{ |
| 89 | + DomainName: &f[0], |
| 90 | + BasePath: &f[1], |
| 91 | + }) |
| 92 | + if err != nil { |
| 93 | + return nil, err |
| 94 | + } |
| 95 | + return convertGetBasePathMappingOutputToBasePathMapping(out), nil |
| 96 | + }, |
| 97 | + DisableList: true, |
| 98 | + SearchFunc: func(ctx context.Context, client *apigateway.Client, scope string, query string) ([]*types.BasePathMapping, error) { |
| 99 | + out, err := client.GetBasePathMappings(ctx, &apigateway.GetBasePathMappingsInput{ |
| 100 | + DomainName: &query, |
| 101 | + }) |
| 102 | + if err != nil { |
| 103 | + return nil, err |
| 104 | + } |
| 105 | + |
| 106 | + var items []*types.BasePathMapping |
| 107 | + for _, basePathMapping := range out.Items { |
| 108 | + items = append(items, &basePathMapping) |
| 109 | + } |
| 110 | + |
| 111 | + return items, nil |
| 112 | + }, |
| 113 | + ItemMapper: func(query, scope string, awsItem *types.BasePathMapping) (*sdp.Item, error) { |
| 114 | + return basePathMappingOutputMapper(query, scope, awsItem) |
| 115 | + }, |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +var basePathMappingAdapterMetadata = Metadata.Register(&sdp.AdapterMetadata{ |
| 120 | + Type: "apigateway-base-path-mapping", |
| 121 | + DescriptiveName: "API Gateway Base Path Mapping", |
| 122 | + Category: sdp.AdapterCategory_ADAPTER_CATEGORY_CONFIGURATION, |
| 123 | + SupportedQueryMethods: &sdp.AdapterSupportedQueryMethods{ |
| 124 | + Get: true, |
| 125 | + Search: true, |
| 126 | + GetDescription: "Get an API Gateway Base Path Mapping by its domain name and base path: domain-name/base-path", |
| 127 | + SearchDescription: "Search for API Gateway Base Path Mappings by their domain name: domain-name", |
| 128 | + }, |
| 129 | +}) |
0 commit comments