Skip to content

Commit f53f262

Browse files
committed
add apigateway vpc link adapter
1 parent 119ed95 commit f53f262

File tree

3 files changed

+194
-0
lines changed

3 files changed

+194
-0
lines changed

adapters/apigateway-vpc-link.go

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
package adapters
2+
3+
import (
4+
"context"
5+
"strings"
6+
7+
"github.com/aws/aws-sdk-go-v2/service/apigateway"
8+
"github.com/aws/aws-sdk-go-v2/service/apigateway/types"
9+
"github.com/overmindtech/aws-source/adapterhelpers"
10+
"github.com/overmindtech/sdp-go"
11+
)
12+
13+
// convertGetVpcLinkOutputToVpcLink converts a GetVpcLinkOutput to a VpcLink
14+
func convertGetVpcLinkOutputToVpcLink(output *apigateway.GetVpcLinkOutput) *types.VpcLink {
15+
return &types.VpcLink{
16+
Id: output.Id,
17+
Name: output.Name,
18+
Description: output.Description,
19+
TargetArns: output.TargetArns,
20+
Status: output.Status,
21+
Tags: output.Tags,
22+
}
23+
}
24+
25+
func vpcLinkListFunc(ctx context.Context, client *apigateway.Client, _ string) ([]*types.VpcLink, error) {
26+
out, err := client.GetVpcLinks(ctx, &apigateway.GetVpcLinksInput{})
27+
if err != nil {
28+
return nil, err
29+
}
30+
31+
var items []*types.VpcLink
32+
for _, vpcLink := range out.Items {
33+
items = append(items, &vpcLink)
34+
}
35+
36+
return items, nil
37+
}
38+
39+
func vpcLinkOutputMapper(scope string, awsItem *types.VpcLink) (*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-vpc-link",
47+
UniqueAttribute: "Id",
48+
Attributes: attributes,
49+
Scope: scope,
50+
Tags: awsItem.Tags,
51+
}
52+
53+
// The status of the VPC link. The valid values are AVAILABLE , PENDING , DELETING , or FAILED.
54+
switch awsItem.Status {
55+
case types.VpcLinkStatusAvailable:
56+
item.Health = sdp.Health_HEALTH_OK.Enum()
57+
case types.VpcLinkStatusPending:
58+
item.Health = sdp.Health_HEALTH_PENDING.Enum()
59+
case types.VpcLinkStatusDeleting:
60+
item.Health = sdp.Health_HEALTH_PENDING.Enum()
61+
case types.VpcLinkStatusFailed:
62+
item.Health = sdp.Health_HEALTH_ERROR.Enum()
63+
}
64+
65+
for _, targetArn := range awsItem.TargetArns {
66+
item.LinkedItemQueries = append(item.LinkedItemQueries, &sdp.LinkedItemQuery{
67+
Query: &sdp.Query{
68+
Type: "elbv2-load-balancer",
69+
Method: sdp.QueryMethod_SEARCH,
70+
Query: targetArn,
71+
Scope: scope,
72+
},
73+
BlastPropagation: &sdp.BlastPropagation{
74+
// Any change on the load balancer will affect the VPC link
75+
In: true,
76+
// Any change on the VPC link won't affect the load balancer
77+
Out: false,
78+
},
79+
})
80+
}
81+
82+
return &item, nil
83+
}
84+
85+
func NewAPIGatewayVpcLinkAdapter(client *apigateway.Client, accountID string, region string) *adapterhelpers.GetListAdapter[*types.VpcLink, *apigateway.Client, *apigateway.Options] {
86+
return &adapterhelpers.GetListAdapter[*types.VpcLink, *apigateway.Client, *apigateway.Options]{
87+
ItemType: "apigateway-vpc-link",
88+
Client: client,
89+
AccountID: accountID,
90+
Region: region,
91+
AdapterMetadata: vpcLinkAdapterMetadata,
92+
GetFunc: func(ctx context.Context, client *apigateway.Client, scope, query string) (*types.VpcLink, error) {
93+
out, err := client.GetVpcLink(ctx, &apigateway.GetVpcLinkInput{
94+
VpcLinkId: &query,
95+
})
96+
if err != nil {
97+
return nil, err
98+
}
99+
return convertGetVpcLinkOutputToVpcLink(out), nil
100+
},
101+
ListFunc: vpcLinkListFunc,
102+
SearchFunc: func(ctx context.Context, client *apigateway.Client, scope string, query string) ([]*types.VpcLink, error) {
103+
out, err := client.GetVpcLinks(ctx, &apigateway.GetVpcLinksInput{})
104+
if err != nil {
105+
return nil, err
106+
}
107+
108+
var items []*types.VpcLink
109+
for _, vpcLink := range out.Items {
110+
if strings.Contains(*vpcLink.Name, query) {
111+
items = append(items, &vpcLink)
112+
}
113+
}
114+
115+
return items, nil
116+
},
117+
ItemMapper: func(_, scope string, awsItem *types.VpcLink) (*sdp.Item, error) {
118+
return vpcLinkOutputMapper(scope, awsItem)
119+
},
120+
}
121+
}
122+
123+
var vpcLinkAdapterMetadata = Metadata.Register(&sdp.AdapterMetadata{
124+
Type: "apigateway-vpc-link",
125+
DescriptiveName: "VPC Link",
126+
Category: sdp.AdapterCategory_ADAPTER_CATEGORY_NETWORK,
127+
SupportedQueryMethods: &sdp.AdapterSupportedQueryMethods{
128+
Get: true,
129+
List: true,
130+
Search: true,
131+
GetDescription: "Get a VPC Link by ID",
132+
ListDescription: "List all VPC Links",
133+
SearchDescription: "Search for VPC Links by their name",
134+
},
135+
})
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package adapters
2+
3+
import (
4+
"testing"
5+
"time"
6+
7+
"github.com/aws/aws-sdk-go-v2/aws"
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 TestVpcLinkOutputMapper(t *testing.T) {
15+
awsItem := &types.VpcLink{
16+
Id: aws.String("vpc-link-id"),
17+
Name: aws.String("vpc-link-name"),
18+
Description: aws.String("vpc-link-description"),
19+
TargetArns: []string{"arn:aws:elasticloadbalancing:us-west-2:123456789012:loadbalancer/app/my-load-balancer/50dc6c495c0c9188"},
20+
Status: types.VpcLinkStatusAvailable,
21+
Tags: map[string]string{"key": "value"},
22+
}
23+
24+
item, err := vpcLinkOutputMapper("scope", awsItem)
25+
if err != nil {
26+
t.Fatalf("unexpected error: %v", err)
27+
}
28+
29+
if err := item.Validate(); err != nil {
30+
t.Error(err)
31+
}
32+
33+
tests := adapterhelpers.QueryTests{
34+
{
35+
ExpectedType: "elbv2-load-balancer",
36+
ExpectedMethod: sdp.QueryMethod_SEARCH,
37+
ExpectedQuery: "arn:aws:elasticloadbalancing:us-west-2:123456789012:loadbalancer/app/my-load-balancer/50dc6c495c0c9188",
38+
ExpectedScope: "scope",
39+
},
40+
}
41+
42+
tests.Execute(t, item)
43+
}
44+
45+
func TestNewAPIGatewayVpcLinkAdapter(t *testing.T) {
46+
config, account, region := adapterhelpers.GetAutoConfig(t)
47+
48+
client := apigateway.NewFromConfig(config)
49+
50+
adapter := NewAPIGatewayVpcLinkAdapter(client, account, region)
51+
52+
test := adapterhelpers.E2ETest{
53+
Adapter: adapter,
54+
Timeout: 10 * time.Second,
55+
}
56+
57+
test.Run(t)
58+
}

proc/proc.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,7 @@ func InitializeAwsSourceEngine(ctx context.Context, ec *discovery.EngineConfig,
482482
adapters.NewAPIGatewayMethodAdapter(apigatewayClient, *callerID.Account, cfg.Region),
483483
adapters.NewAPIGatewayMethodResponseAdapter(apigatewayClient, *callerID.Account, cfg.Region),
484484
adapters.NewAPIGatewayIntegrationAdapter(apigatewayClient, *callerID.Account, cfg.Region),
485+
adapters.NewAPIGatewayVpcLinkAdapter(apigatewayClient, *callerID.Account, cfg.Region),
485486

486487
// SSM
487488
adapters.NewSSMParameterAdapter(ssmClient, *callerID.Account, cfg.Region),

0 commit comments

Comments
 (0)