|
| 1 | +package adapters |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "github.com/aws/aws-sdk-go-v2/service/apigateway" |
| 6 | + "github.com/aws/aws-sdk-go-v2/service/apigateway/types" |
| 7 | + "github.com/overmindtech/aws-source/adapterhelpers" |
| 8 | + "github.com/overmindtech/sdp-go" |
| 9 | + "strings" |
| 10 | +) |
| 11 | + |
| 12 | +// convertGetApiKeyOutputToApiKey converts a GetApiKeyOutput to an ApiKey |
| 13 | +func convertGetApiKeyOutputToApiKey(output *apigateway.GetApiKeyOutput) *types.ApiKey { |
| 14 | + return &types.ApiKey{ |
| 15 | + Id: output.Id, |
| 16 | + Name: output.Name, |
| 17 | + Enabled: output.Enabled, |
| 18 | + CreatedDate: output.CreatedDate, |
| 19 | + LastUpdatedDate: output.LastUpdatedDate, |
| 20 | + StageKeys: output.StageKeys, |
| 21 | + Tags: output.Tags, |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +func apiKeyListFunc(ctx context.Context, client *apigateway.Client, _ string) ([]*types.ApiKey, error) { |
| 26 | + out, err := client.GetApiKeys(ctx, &apigateway.GetApiKeysInput{}) |
| 27 | + if err != nil { |
| 28 | + return nil, err |
| 29 | + } |
| 30 | + |
| 31 | + var items []*types.ApiKey |
| 32 | + for _, apiKey := range out.Items { |
| 33 | + items = append(items, &apiKey) |
| 34 | + } |
| 35 | + |
| 36 | + return items, nil |
| 37 | +} |
| 38 | + |
| 39 | +func apiKeyOutputMapper(scope string, awsItem *types.ApiKey) (*sdp.Item, error) { |
| 40 | + attributes, err := adapterhelpers.ToAttributesWithExclude(awsItem, "tags") |
| 41 | + if err != nil { |
| 42 | + return nil, err |
| 43 | + } |
| 44 | + |
| 45 | + item := sdp.Item{ |
| 46 | + Type: "apigateway-api-key", |
| 47 | + UniqueAttribute: "Id", |
| 48 | + Attributes: attributes, |
| 49 | + Scope: scope, |
| 50 | + Tags: awsItem.Tags, |
| 51 | + } |
| 52 | + |
| 53 | + for _, key := range awsItem.StageKeys { |
| 54 | + // {restApiId}/{stage} |
| 55 | + restAPIID := strings.Split(key, "/")[0] |
| 56 | + if restAPIID != "" { |
| 57 | + item.LinkedItemQueries = append(item.LinkedItemQueries, &sdp.LinkedItemQuery{ |
| 58 | + Query: &sdp.Query{ |
| 59 | + Type: "apigateway-rest-api", |
| 60 | + Method: sdp.QueryMethod_GET, |
| 61 | + Query: restAPIID, |
| 62 | + Scope: scope, |
| 63 | + }, |
| 64 | + BlastPropagation: &sdp.BlastPropagation{ |
| 65 | + // They are tightly coupled, so we need to propagate both ways |
| 66 | + In: true, |
| 67 | + Out: true, |
| 68 | + }, |
| 69 | + }) |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + return &item, nil |
| 74 | +} |
| 75 | + |
| 76 | +func NewAPIGatewayApiKeyAdapter(client *apigateway.Client, accountID string, region string) *adapterhelpers.GetListAdapter[*types.ApiKey, *apigateway.Client, *apigateway.Options] { |
| 77 | + return &adapterhelpers.GetListAdapter[*types.ApiKey, *apigateway.Client, *apigateway.Options]{ |
| 78 | + ItemType: "apigateway-api-key", |
| 79 | + Client: client, |
| 80 | + AccountID: accountID, |
| 81 | + Region: region, |
| 82 | + AdapterMetadata: apiKeyAdapterMetadata, |
| 83 | + GetFunc: func(ctx context.Context, client *apigateway.Client, scope, query string) (*types.ApiKey, error) { |
| 84 | + out, err := client.GetApiKey(ctx, &apigateway.GetApiKeyInput{ |
| 85 | + ApiKey: &query, |
| 86 | + }) |
| 87 | + if err != nil { |
| 88 | + return nil, err |
| 89 | + } |
| 90 | + return convertGetApiKeyOutputToApiKey(out), nil |
| 91 | + }, |
| 92 | + ListFunc: apiKeyListFunc, |
| 93 | + SearchFunc: func(ctx context.Context, client *apigateway.Client, scope string, query string) ([]*types.ApiKey, error) { |
| 94 | + out, err := client.GetApiKeys(ctx, &apigateway.GetApiKeysInput{ |
| 95 | + NameQuery: &query, |
| 96 | + }) |
| 97 | + if err != nil { |
| 98 | + return nil, err |
| 99 | + } |
| 100 | + |
| 101 | + var items []*types.ApiKey |
| 102 | + for _, apiKey := range out.Items { |
| 103 | + items = append(items, &apiKey) |
| 104 | + } |
| 105 | + |
| 106 | + return items, nil |
| 107 | + }, |
| 108 | + ItemMapper: func(_, scope string, awsItem *types.ApiKey) (*sdp.Item, error) { |
| 109 | + return apiKeyOutputMapper(scope, awsItem) |
| 110 | + }, |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +var apiKeyAdapterMetadata = Metadata.Register(&sdp.AdapterMetadata{ |
| 115 | + Type: "apigateway-api-key", |
| 116 | + DescriptiveName: "API Key", |
| 117 | + Category: sdp.AdapterCategory_ADAPTER_CATEGORY_SECURITY, |
| 118 | + SupportedQueryMethods: &sdp.AdapterSupportedQueryMethods{ |
| 119 | + Get: true, |
| 120 | + List: true, |
| 121 | + Search: true, |
| 122 | + GetDescription: "Get an API Key by ID", |
| 123 | + ListDescription: "List all API Keys", |
| 124 | + SearchDescription: "Search for API Keys by their name", |
| 125 | + }, |
| 126 | +}) |
0 commit comments