|
| 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 | +// convertGetDeploymentOutputToDeployment converts a GetDeploymentOutput to a Deployment |
| 15 | +func convertGetDeploymentOutputToDeployment(output *apigateway.GetDeploymentOutput) *types.Deployment { |
| 16 | + return &types.Deployment{ |
| 17 | + Id: output.Id, |
| 18 | + CreatedDate: output.CreatedDate, |
| 19 | + Description: output.Description, |
| 20 | + ApiSummary: output.ApiSummary, |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +func deploymentOutputMapper(scope string, awsItem *types.Deployment) (*sdp.Item, error) { |
| 25 | + attributes, err := adapterhelpers.ToAttributesWithExclude(awsItem, "tags") |
| 26 | + if err != nil { |
| 27 | + return nil, err |
| 28 | + } |
| 29 | + |
| 30 | + item := sdp.Item{ |
| 31 | + Type: "apigateway-deployment", |
| 32 | + UniqueAttribute: "Id", |
| 33 | + Attributes: attributes, |
| 34 | + Scope: scope, |
| 35 | + } |
| 36 | + |
| 37 | + return &item, nil |
| 38 | +} |
| 39 | + |
| 40 | +func NewAPIGatewayDeploymentAdapter(client *apigateway.Client, accountID string, region string) *adapterhelpers.GetListAdapter[*types.Deployment, *apigateway.Client, *apigateway.Options] { |
| 41 | + return &adapterhelpers.GetListAdapter[*types.Deployment, *apigateway.Client, *apigateway.Options]{ |
| 42 | + ItemType: "apigateway-deployment", |
| 43 | + Client: client, |
| 44 | + AccountID: accountID, |
| 45 | + Region: region, |
| 46 | + AdapterMetadata: deploymentAdapterMetadata, |
| 47 | + GetFunc: func(ctx context.Context, client *apigateway.Client, scope, query string) (*types.Deployment, error) { |
| 48 | + f := strings.Split(query, "/") |
| 49 | + if len(f) != 2 { |
| 50 | + return nil, &sdp.QueryError{ |
| 51 | + ErrorType: sdp.QueryError_NOTFOUND, |
| 52 | + ErrorString: fmt.Sprintf("query must be in the format of: the rest-api-id/deployment-id, but found: %s", query), |
| 53 | + } |
| 54 | + } |
| 55 | + out, err := client.GetDeployment(ctx, &apigateway.GetDeploymentInput{ |
| 56 | + RestApiId: &f[0], |
| 57 | + DeploymentId: &f[1], |
| 58 | + }) |
| 59 | + if err != nil { |
| 60 | + return nil, err |
| 61 | + } |
| 62 | + return convertGetDeploymentOutputToDeployment(out), nil |
| 63 | + }, |
| 64 | + DisableList: true, |
| 65 | + SearchFunc: func(ctx context.Context, client *apigateway.Client, scope string, query string) ([]*types.Deployment, error) { |
| 66 | + f := strings.Split(query, "/") |
| 67 | + var restAPIID string |
| 68 | + var description string |
| 69 | + |
| 70 | + switch len(f) { |
| 71 | + case 1: |
| 72 | + restAPIID = f[0] |
| 73 | + case 2: |
| 74 | + restAPIID = f[0] |
| 75 | + description = f[1] |
| 76 | + default: |
| 77 | + return nil, &sdp.QueryError{ |
| 78 | + ErrorType: sdp.QueryError_NOTFOUND, |
| 79 | + ErrorString: fmt.Sprintf( |
| 80 | + "query must be in the format of: the rest-api-id/deployment-id or rest-api-id, but found: %s", |
| 81 | + query, |
| 82 | + ), |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + out, err := client.GetDeployments(ctx, &apigateway.GetDeploymentsInput{ |
| 87 | + RestApiId: &restAPIID, |
| 88 | + }) |
| 89 | + if err != nil { |
| 90 | + return nil, err |
| 91 | + } |
| 92 | + |
| 93 | + var items []*types.Deployment |
| 94 | + for _, deployment := range out.Items { |
| 95 | + if description != "" && strings.Contains(*deployment.Description, description) { |
| 96 | + items = append(items, &deployment) |
| 97 | + } else { |
| 98 | + items = append(items, &deployment) |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + return items, nil |
| 103 | + }, |
| 104 | + ItemMapper: func(_, scope string, awsItem *types.Deployment) (*sdp.Item, error) { |
| 105 | + return deploymentOutputMapper(scope, awsItem) |
| 106 | + }, |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +var deploymentAdapterMetadata = Metadata.Register(&sdp.AdapterMetadata{ |
| 111 | + Type: "apigateway-deployment", |
| 112 | + DescriptiveName: "API Gateway Deployment", |
| 113 | + Category: sdp.AdapterCategory_ADAPTER_CATEGORY_CONFIGURATION, |
| 114 | + SupportedQueryMethods: &sdp.AdapterSupportedQueryMethods{ |
| 115 | + Get: true, |
| 116 | + Search: true, |
| 117 | + GetDescription: "Get an API Gateway Deployment by its rest API ID and ID: rest-api-id/deployment-id", |
| 118 | + SearchDescription: "Search for API Gateway Deployments by their rest API ID or with rest API ID and their description: rest-api-id/deployment-description", |
| 119 | + }, |
| 120 | +}) |
0 commit comments