generated from overmindtech/source-template
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add apigateway method response adapter
- Loading branch information
1 parent
00a3057
commit 6e9d1d7
Showing
8 changed files
with
275 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package adapters | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log/slog" | ||
"strings" | ||
|
||
"github.com/aws/aws-sdk-go-v2/service/apigateway" | ||
"github.com/overmindtech/aws-source/adapterhelpers" | ||
"github.com/overmindtech/sdp-go" | ||
) | ||
|
||
func apiGatewayMethodResponseGetFunc(ctx context.Context, client apigatewayClient, scope string, input *apigateway.GetMethodResponseInput) (*sdp.Item, error) { | ||
output, err := client.GetMethodResponse(ctx, input) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
attributes, err := adapterhelpers.ToAttributesWithExclude(output, "tags") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// We create a custom ID of {rest-api-id}/{resource-id}/{http-method}/{status-code} e.g. | ||
// rest-api-id/resource-id/GET/200 | ||
methodResponseID := fmt.Sprintf( | ||
"%s/%s/%s/%s", | ||
*input.RestApiId, | ||
*input.ResourceId, | ||
*input.HttpMethod, | ||
*input.StatusCode, | ||
) | ||
err = attributes.Set("MethodResponseID", methodResponseID) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
item := &sdp.Item{ | ||
Type: "apigateway-method-response", | ||
UniqueAttribute: "MethodResponseID", | ||
Attributes: attributes, | ||
Scope: scope, | ||
} | ||
|
||
item.LinkedItemQueries = append(item.LinkedItemQueries, &sdp.LinkedItemQuery{ | ||
Query: &sdp.Query{ | ||
Type: "apigateway-method", | ||
Method: sdp.QueryMethod_GET, | ||
Query: fmt.Sprintf("%s/%s/%s", *input.RestApiId, *input.ResourceId, *input.HttpMethod), | ||
Scope: scope, | ||
}, | ||
BlastPropagation: &sdp.BlastPropagation{ | ||
// They are tightly coupled | ||
In: true, | ||
Out: true, | ||
}, | ||
}) | ||
|
||
return item, nil | ||
} | ||
|
||
func NewAPIGatewayMethodResponseAdapter(client apigatewayClient, accountID string, region string) *adapterhelpers.AlwaysGetAdapter[*apigateway.GetMethodResponseInput, *apigateway.GetMethodResponseOutput, *apigateway.GetMethodResponseInput, *apigateway.GetMethodResponseOutput, apigatewayClient, *apigateway.Options] { | ||
return &adapterhelpers.AlwaysGetAdapter[*apigateway.GetMethodResponseInput, *apigateway.GetMethodResponseOutput, *apigateway.GetMethodResponseInput, *apigateway.GetMethodResponseOutput, apigatewayClient, *apigateway.Options]{ | ||
ItemType: "apigateway-method-response", | ||
Client: client, | ||
AccountID: accountID, | ||
Region: region, | ||
AdapterMetadata: apiGatewayMethodResponseAdapterMetadata, | ||
GetFunc: apiGatewayMethodResponseGetFunc, | ||
GetInputMapper: func(scope, query string) *apigateway.GetMethodResponseInput { | ||
// We are using a custom id of {rest-api-id}/{resource-id}/{http-method}/{status-code} e.g. | ||
// rest-api-id/resource-id/GET/200 | ||
f := strings.Split(query, "/") | ||
if len(f) != 4 { | ||
slog.Error( | ||
"query must be in the format of: rest-api-id/resource-id/http-method/status-code", | ||
"found", | ||
query, | ||
) | ||
|
||
return nil | ||
} | ||
|
||
return &apigateway.GetMethodResponseInput{ | ||
RestApiId: &f[0], | ||
ResourceId: &f[1], | ||
HttpMethod: &f[2], | ||
StatusCode: &f[3], | ||
} | ||
}, | ||
DisableList: true, | ||
} | ||
} | ||
|
||
var apiGatewayMethodResponseAdapterMetadata = Metadata.Register(&sdp.AdapterMetadata{ | ||
Type: "apigateway-method-response", | ||
DescriptiveName: "API Gateway Method Response", | ||
Category: sdp.AdapterCategory_ADAPTER_CATEGORY_NETWORK, | ||
SupportedQueryMethods: &sdp.AdapterSupportedQueryMethods{ | ||
Get: true, | ||
GetDescription: "Get a Method Response by rest-api id, resource id, http-method, and status-code", | ||
Search: true, | ||
SearchDescription: "Search Method Responses by ARN", | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package adapters | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
"github.com/aws/aws-sdk-go-v2/aws" | ||
"github.com/aws/aws-sdk-go-v2/service/apigateway" | ||
"github.com/overmindtech/aws-source/adapterhelpers" | ||
"github.com/overmindtech/sdp-go" | ||
) | ||
|
||
func (m *mockAPIGatewayClient) GetMethodResponse(ctx context.Context, params *apigateway.GetMethodResponseInput, optFns ...func(*apigateway.Options)) (*apigateway.GetMethodResponseOutput, error) { | ||
return &apigateway.GetMethodResponseOutput{ | ||
ResponseModels: map[string]string{ | ||
"application/json": "Empty", | ||
}, | ||
StatusCode: aws.String("200"), | ||
}, nil | ||
} | ||
|
||
func TestApiGatewayMethodResponseGetFunc(t *testing.T) { | ||
ctx := context.Background() | ||
cli := mockAPIGatewayClient{} | ||
|
||
input := &apigateway.GetMethodResponseInput{ | ||
RestApiId: aws.String("rest-api-id"), | ||
ResourceId: aws.String("resource-id"), | ||
HttpMethod: aws.String("GET"), | ||
StatusCode: aws.String("200"), | ||
} | ||
|
||
item, err := apiGatewayMethodResponseGetFunc(ctx, &cli, "scope", input) | ||
if err != nil { | ||
t.Fatalf("unexpected error: %v", err) | ||
} | ||
|
||
if err = item.Validate(); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
methodID := fmt.Sprintf("%s/%s/%s", *input.RestApiId, *input.ResourceId, *input.HttpMethod) | ||
|
||
tests := adapterhelpers.QueryTests{ | ||
{ | ||
ExpectedType: "apigateway-method", | ||
ExpectedMethod: sdp.QueryMethod_GET, | ||
ExpectedQuery: methodID, | ||
ExpectedScope: "scope", | ||
}, | ||
} | ||
|
||
tests.Execute(t, item) | ||
} | ||
|
||
func TestNewAPIGatewayMethodResponseAdapter(t *testing.T) { | ||
config, account, region := adapterhelpers.GetAutoConfig(t) | ||
|
||
client := apigateway.NewFromConfig(config) | ||
|
||
adapter := NewAPIGatewayMethodResponseAdapter(client, account, region) | ||
|
||
test := adapterhelpers.E2ETest{ | ||
Adapter: adapter, | ||
Timeout: 10 * time.Second, | ||
SkipList: true, | ||
} | ||
|
||
test.Run(t) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters