|
| 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 convertGetModelOutputToModel(output *apigateway.GetModelOutput) *types.Model { |
| 15 | + return &types.Model{ |
| 16 | + Id: output.Id, |
| 17 | + Name: output.Name, |
| 18 | + Description: output.Description, |
| 19 | + Schema: output.Schema, |
| 20 | + ContentType: output.ContentType, |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +func modelOutputMapper(query, scope string, awsItem *types.Model) (*sdp.Item, error) { |
| 25 | + attributes, err := adapterhelpers.ToAttributesWithExclude(awsItem, "tags") |
| 26 | + if err != nil { |
| 27 | + return nil, err |
| 28 | + } |
| 29 | + |
| 30 | + restAPIID := strings.Split(query, "/")[0] |
| 31 | + |
| 32 | + err = attributes.Set("UniqueAttribute", fmt.Sprintf("%s/%s", restAPIID, *awsItem.Name)) |
| 33 | + |
| 34 | + item := sdp.Item{ |
| 35 | + Type: "apigateway-model", |
| 36 | + UniqueAttribute: "Name", |
| 37 | + Attributes: attributes, |
| 38 | + Scope: scope, |
| 39 | + } |
| 40 | + |
| 41 | + item.LinkedItemQueries = append(item.LinkedItemQueries, &sdp.LinkedItemQuery{ |
| 42 | + Query: &sdp.Query{ |
| 43 | + Type: "apigateway-rest-api", |
| 44 | + Method: sdp.QueryMethod_GET, |
| 45 | + Query: restAPIID, |
| 46 | + Scope: scope, |
| 47 | + }, |
| 48 | + BlastPropagation: &sdp.BlastPropagation{ |
| 49 | + // They are tightly coupled, so we need to propagate the blast to the linked item |
| 50 | + In: true, |
| 51 | + Out: true, |
| 52 | + }, |
| 53 | + }) |
| 54 | + |
| 55 | + return &item, nil |
| 56 | +} |
| 57 | + |
| 58 | +func NewAPIGatewayModelAdapter(client *apigateway.Client, accountID string, region string) *adapterhelpers.GetListAdapter[*types.Model, *apigateway.Client, *apigateway.Options] { |
| 59 | + return &adapterhelpers.GetListAdapter[*types.Model, *apigateway.Client, *apigateway.Options]{ |
| 60 | + ItemType: "apigateway-model", |
| 61 | + Client: client, |
| 62 | + AccountID: accountID, |
| 63 | + Region: region, |
| 64 | + AdapterMetadata: modelAdapterMetadata, |
| 65 | + GetFunc: func(ctx context.Context, client *apigateway.Client, scope, query string) (*types.Model, error) { |
| 66 | + f := strings.Split(query, "/") |
| 67 | + if len(f) != 2 { |
| 68 | + return nil, &sdp.QueryError{ |
| 69 | + ErrorType: sdp.QueryError_NOTFOUND, |
| 70 | + ErrorString: fmt.Sprintf("query must be in the format of: the rest-api-id/model-name, but found: %s", query), |
| 71 | + } |
| 72 | + } |
| 73 | + out, err := client.GetModel(ctx, &apigateway.GetModelInput{ |
| 74 | + RestApiId: &f[0], |
| 75 | + ModelName: &f[1], |
| 76 | + }) |
| 77 | + if err != nil { |
| 78 | + return nil, err |
| 79 | + } |
| 80 | + return convertGetModelOutputToModel(out), nil |
| 81 | + }, |
| 82 | + DisableList: true, |
| 83 | + SearchFunc: func(ctx context.Context, client *apigateway.Client, scope string, query string) ([]*types.Model, error) { |
| 84 | + out, err := client.GetModels(ctx, &apigateway.GetModelsInput{ |
| 85 | + RestApiId: &query, |
| 86 | + }) |
| 87 | + if err != nil { |
| 88 | + return nil, err |
| 89 | + } |
| 90 | + |
| 91 | + var items []*types.Model |
| 92 | + for _, model := range out.Items { |
| 93 | + items = append(items, &model) |
| 94 | + } |
| 95 | + |
| 96 | + return items, nil |
| 97 | + }, |
| 98 | + ItemMapper: func(query, scope string, awsItem *types.Model) (*sdp.Item, error) { |
| 99 | + return modelOutputMapper(query, scope, awsItem) |
| 100 | + }, |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +var modelAdapterMetadata = Metadata.Register(&sdp.AdapterMetadata{ |
| 105 | + Type: "apigateway-model", |
| 106 | + DescriptiveName: "API Gateway Model", |
| 107 | + Category: sdp.AdapterCategory_ADAPTER_CATEGORY_CONFIGURATION, |
| 108 | + SupportedQueryMethods: &sdp.AdapterSupportedQueryMethods{ |
| 109 | + Get: true, |
| 110 | + Search: true, |
| 111 | + GetDescription: "Get an API Gateway Model by its rest API ID and model name: rest-api-id/model-name", |
| 112 | + SearchDescription: "Search for API Gateway Models by their rest API ID: rest-api-id", |
| 113 | + }, |
| 114 | +}) |
0 commit comments