Skip to content

Commit

Permalink
Update aws host mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
romulets committed Jan 8, 2025
1 parent eca77ed commit b785901
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 32 deletions.
11 changes: 6 additions & 5 deletions internal/inventory/asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,12 @@ type Cloud struct {
}

type Host struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Architecture string `json:"architecture,omitempty"`
Type string `json:"type,omitempty"`
IP string `json:"ip,omitempty"`
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Architecture string `json:"architecture,omitempty"`
Type string `json:"type,omitempty"`
IP string `json:"ip,omitempty"`
MacAddress []string `json:"mac,omitempty"`
}

type User struct {
Expand Down
41 changes: 19 additions & 22 deletions internal/inventory/awsfetcher/fetcher_ec2_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,46 +58,43 @@ func (e *ec2InstanceFetcher) Fetch(ctx context.Context, assetChannel chan<- inve
return
}

for _, instance := range instances {
if instance == nil {
for _, i := range instances {
if i == nil {
continue
}

iamFetcher := inventory.EmptyEnricher()
if instance.IamInstanceProfile != nil {
if i.IamInstanceProfile != nil {
iamFetcher = inventory.WithUser(inventory.User{
ID: pointers.Deref(instance.IamInstanceProfile.Id),
Name: pointers.Deref(instance.IamInstanceProfile.Arn),
ID: pointers.Deref(i.IamInstanceProfile.Arn),
})
}

subnetIds := []string{}
if id := pointers.Deref(instance.SubnetId); id != "" {
subnetIds = append(subnetIds, id)
}
assetChannel <- inventory.NewAssetEvent(
inventory.AssetClassificationAwsEc2Instance,
instance.GetResourceArn(),
instance.GetResourceName(),
i.GetResourceArn(),
pointers.Deref(i.PrivateDnsName),

inventory.WithRawAsset(instance),
inventory.WithLabels(e.getTags(instance)),
inventory.WithRawAsset(i),
inventory.WithLabels(e.getTags(i)),
inventory.WithCloud(inventory.Cloud{
Provider: inventory.AwsCloudProvider,
Region: instance.Region,
AvailabilityZone: e.getAvailabilityZone(instance),
Region: i.Region,
AvailabilityZone: e.getAvailabilityZone(i),
AccountID: e.AccountId,
AccountName: e.AccountName,
InstanceID: pointers.Deref(instance.InstanceId),
InstanceName: instance.GetResourceName(),
MachineType: string(instance.InstanceType),
InstanceID: pointers.Deref(i.InstanceId),
InstanceName: i.GetResourceName(),
MachineType: string(i.InstanceType),
ServiceName: "AWS EC2",
}),
inventory.WithHost(inventory.Host{
Name: instance.GetResourceName(),
Architecture: string(instance.Architecture),
Type: string(instance.InstanceType),
IP: pointers.Deref(instance.PublicIpAddress),
ID: pointers.Deref(i.InstanceId),
Name: pointers.Deref(i.PrivateDnsName),
Architecture: string(i.Architecture),
Type: string(i.InstanceType),
IP: pointers.Deref(i.PublicIpAddress),
MacAddress: i.GetResourceMacAddresses(),
}),
iamFetcher,
)
Expand Down
21 changes: 16 additions & 5 deletions internal/inventory/awsfetcher/fetcher_ec2_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ func TestEC2InstanceFetcher_Fetch(t *testing.T) {
Placement: &types.Placement{
AvailabilityZone: pointers.Ref("1a"),
},
NetworkInterfaces: []types.InstanceNetworkInterface{
{
MacAddress: pointers.Ref("mac1"),
},
{
MacAddress: pointers.Ref("mac2"),
},
},
},
Region: "us-east",
}
Expand All @@ -79,7 +87,7 @@ func TestEC2InstanceFetcher_Fetch(t *testing.T) {
inventory.NewAssetEvent(
inventory.AssetClassificationAwsEc2Instance,
"arn:aws:ec2:us-east::ec2/234567890",
"test-server",
"private-dns",
inventory.WithRawAsset(instance1),
inventory.WithLabels(map[string]string{"Name": "test-server", "key": "value"}),
inventory.WithCloud(inventory.Cloud{
Expand All @@ -94,14 +102,15 @@ func TestEC2InstanceFetcher_Fetch(t *testing.T) {
ServiceName: "AWS EC2",
}),
inventory.WithHost(inventory.Host{
Name: "test-server",
ID: "234567890",
Name: "private-dns",
Architecture: string(types.ArchitectureValuesX8664),
Type: "instance-type",
IP: "public-ip-addr",
MacAddress: []string{"mac1", "mac2"},
}),
inventory.WithUser(inventory.User{
ID: "a123123",
Name: "123123:123123:123123",
ID: "123123:123123:123123",
}),
),

Expand All @@ -122,7 +131,9 @@ func TestEC2InstanceFetcher_Fetch(t *testing.T) {
MachineType: "",
ServiceName: "AWS EC2",
}),
inventory.WithHost(inventory.Host{}),
inventory.WithHost(inventory.Host{
MacAddress: []string{},
}),
),
}

Expand Down

0 comments on commit b785901

Please sign in to comment.