|
| 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 | +}) |
0 commit comments