Skip to content

Commit

Permalink
Merge branch 'develop' into feature/509-aws-ec2-spot-instance-request
Browse files Browse the repository at this point in the history
  • Loading branch information
ShubhamPalriwala authored Apr 20, 2023
2 parents ff39baf + 4d95069 commit 76b6cb7
Show file tree
Hide file tree
Showing 6 changed files with 316 additions and 129 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
import { InventoryFilterData } from '../types/useInventoryTypes';
import parseURLParams from './parseURLParams';

describe('parseURLParams', () => {
it('should return a filter object', () => {
const param = 'tag:tagKey:tagValue:IS:tagValue';
const type = 'fetch';
const filter = parseURLParams(param, type);

expect(filter).toEqual({
field: 'tag:tagKey:tagValue',
operator: 'IS',
values: ['tagValue']
});
});

it('should return a filter object with a NOT operator', () => {
const param = 'tag:tagKey:tagValue:NOT:tagValue';
const type = 'fetch';
const filter = parseURLParams(param, type);

expect(filter).toEqual({
field: 'tag:tagKey:tagValue',
operator: 'NOT',
values: ['tagValue']
});
});

it('should return a filter object for the EXISTS tag operator', () => {
const param = 'tag:tagKey:EXISTS';
const type = 'display';
const filter = parseURLParams(param, type);

expect(filter).toEqual({
field: 'tag',
operator: 'EXISTS',
tagKey: 'tagKey',
values: []
});
});

it('should return a filter object for the EXISTS tag operator', () => {
const param = 'tag:tagKey:NOT_EXISTS';
const type = 'display';
const filter = parseURLParams(param, type);

expect(filter).toEqual({
field: 'tag',
operator: 'NOT_EXISTS',
tagKey: 'tagKey',
values: []
});
});

it('should return a filter object for the EXISTS tag operator if parsing for InventoryFilterData (view = true)', () => {
const param: InventoryFilterData = {
field: 'tag:tagKey',
operator: 'EXISTS',
values: []
};
const type = 'display';
const filter = parseURLParams(param, type, true);

expect(filter).toEqual({
field: 'tag',
operator: 'EXISTS',
tagKey: 'tagKey',
values: []
});
});

it('should return a filter object for the IS_NOT tag operator if parsing for InventoryFilterData (view = true)', () => {
const param: InventoryFilterData = {
field: 'tag:tagKey',
operator: 'IS_NOT',
values: ['tagValue']
};
const type = 'display';
const filter = parseURLParams(param, type, true);

expect(filter).toEqual({
field: 'tag',
operator: 'IS_NOT',
tagKey: 'tagKey',
values: ['tagValue']
});
});

it('should return a filter object for non-tag operators', () => {
const param: InventoryFilterData = {
field: 'Cost',
operator: 'IS_GREATER_THAN',
values: ['100']
};
const type = 'display';
const filter = parseURLParams(param, type, true);

expect(filter).toEqual({
field: 'Cost',
operator: 'IS_GREATER_THAN',
values: ['100']
});
});

it('should return a filter object for non-tag operators #2', () => {
const param: InventoryFilterData = {
field: 'Cost',
operator: 'IS_BETWEEN',
values: ['100', '200']
};
const type = 'display';
const filter = parseURLParams(param, type, true);

expect(filter).toEqual({
field: 'Cost',
operator: 'IS_BETWEEN',
values: ['100', '200']
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import { InventoryFilterData } from '../types/useInventoryTypes';

/** Parse the URL Params.
* - Argument of type 'fetch' will output the object to fetch an inventory list and stats based on filters.
* - Argument of type 'display' will output the object to populate the InventoryFilterSummary component */
* - Argument of type 'display' will output the object to populate the InventoryFilterSummary component
* Input:
* one portion of the URL params: e.g. tag:A:IS_EMPTY
* */
function parseURLParams(
param: string | InventoryFilterData,
type: 'fetch' | 'display',
Expand Down Expand Up @@ -79,14 +82,14 @@ function parseURLParams(
if (formatString.length > 2) {
if (
formatString.indexOf('IS_EMPTY') !== -1 ||
formatString.indexOf('IS_NOT_EMPTY') !== -1
formatString.indexOf('IS_NOT_EMPTY') !== -1 ||
formatString.indexOf('EXISTS') !== -1 ||
formatString.indexOf('NOT_EXISTS') !== -1
) {
const key = formatString.slice(1, formatString.length - 1).join(':');

filter = {
field: formatString[0],
tagKey: key,
operator: formatString[formatString.length - 1],
tagKey: formatString[1],
operator: formatString[2],
values: []
};
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export type InventoryFilterData = {
| 'NOT_CONTAINS'
| 'IS_EMPTY'
| 'IS_NOT_EMPTY'
| 'EXISTS'
| 'NOT_EXISTS'
| string
| undefined;
tagKey?: string;
Expand Down
246 changes: 123 additions & 123 deletions internal/api/v1/template.go

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions providers/aws/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func listOfSupportedServices() []providers.FetchDataFunction {
opensearch.ServiceDomains,
servicecatalog.Products,
ec2.SpotInstanceRequests,
ec2.KeyPairs,
}
}

Expand Down
61 changes: 61 additions & 0 deletions providers/aws/ec2/keypair.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package ec2

import (
"context"
"fmt"
"time"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ec2"
log "github.com/sirupsen/logrus"
"github.com/tailwarden/komiser/models"
"github.com/tailwarden/komiser/providers"
)

func KeyPairs(ctx context.Context, client providers.ProviderClient) ([]models.Resource, error) {
resources := make([]models.Resource, 0)

ec2Client := ec2.NewFromConfig(*client.AWSClient)
input := &ec2.DescribeKeyPairsInput{}

output, err := ec2Client.DescribeKeyPairs(ctx, input)
if err != nil {
return resources, err
}

for _, keypair := range output.KeyPairs {
tags := make([]models.Tag, 0)
for _, tag := range keypair.Tags {
tags = append(tags, models.Tag{
Key: aws.ToString(tag.Key),
Value: aws.ToString(tag.Value),
})
}

resources = append(resources, models.Resource{
Provider: "AWS",
Service: "EC2 KeyPair",
ResourceId: aws.ToString(keypair.KeyPairId),
Region: client.AWSClient.Region,
Name: aws.ToString(keypair.KeyName),
Cost: 0,
Tags: tags,
CreatedAt: *keypair.CreateTime,
FetchedAt: time.Now(),
Link: fmt.Sprintf("https://%s.console.aws.amazon.com/ec2/v2/home?region=%s#KeyPairs:search=%s", client.AWSClient.Region, client.AWSClient.Region, aws.ToString(keypair.KeyPairId)),
Metadata: map[string]string{
"KeyType": string(keypair.KeyType),
},
})
}

log.WithFields(log.Fields{
"provider": "AWS",
"service": "EC2 KeyPair",
"account": client.Name,
"region": client.AWSClient.Region,
"resources": len(resources),
}).Info("Fetched resources")

return resources, nil
}

0 comments on commit 76b6cb7

Please sign in to comment.