-
Notifications
You must be signed in to change notification settings - Fork 433
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into feature/509-aws-ec2-spot-instance-request
- Loading branch information
Showing
6 changed files
with
316 additions
and
129 deletions.
There are no files selected for viewing
120 changes: 120 additions & 0 deletions
120
dashboard/components/inventory/hooks/useInventory/helpers/parseURLParams.test.ts
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,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'] | ||
}); | ||
}); | ||
}); |
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
Large diffs are not rendered by default.
Oops, something went wrong.
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
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 | ||
} |