Skip to content

Commit

Permalink
🧹 improve aws ec2 instance resource (#3761)
Browse files Browse the repository at this point in the history
  • Loading branch information
vjeffrey authored Apr 17, 2024
1 parent df910af commit 72792e0
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 49 deletions.
4 changes: 2 additions & 2 deletions providers/aws/resources/aws.lr
Original file line number Diff line number Diff line change
Expand Up @@ -2402,7 +2402,7 @@ private aws.ec2.instance @defaults("instanceId region state instanceType archite
// List of devices attached to the instance (such as EBS volume)
deviceMappings []aws.ec2.instance.device
// List of security groups (IDs) associated with the instance
securityGroups []aws.ec2.securitygroup
securityGroups() []aws.ec2.securitygroup
// Platform details
platformDetails string
// Public DNS name for the instance
Expand All @@ -2422,7 +2422,7 @@ private aws.ec2.instance @defaults("instanceId region state instanceType archite
// Tags on the instance
tags map[string]string
// Image that was used for the instance
image aws.ec2.image
image() aws.ec2.image
// Launch time of the instance
launchTime time
// Private IP address for the instance
Expand Down
30 changes: 27 additions & 3 deletions providers/aws/resources/aws.lr.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

97 changes: 53 additions & 44 deletions providers/aws/resources/aws_ec2.go
Original file line number Diff line number Diff line change
Expand Up @@ -692,15 +692,6 @@ func (a *mqlAwsEc2) gatherInstanceInfo(instances []ec2types.Reservation, imdsvVe
}
mqlDevices = append(mqlDevices, mqlInstanceDevice)
}
sgs := []interface{}{}
for i := range instance.SecurityGroups {
mqlSg, err := NewResource(a.MqlRuntime, "aws.ec2.securitygroup",
map[string]*llx.RawData{"arn": llx.StringData(fmt.Sprintf(securityGroupArnPattern, regionVal, conn.AccountId(), convert.ToString(instance.SecurityGroups[i].GroupId)))})
if err != nil {
return nil, err
}
sgs = append(sgs, mqlSg)
}

stateReason, err := convert.JsonToDict(instance.StateReason)
if err != nil {
Expand Down Expand Up @@ -738,27 +729,13 @@ func (a *mqlAwsEc2) gatherInstanceInfo(instances []ec2types.Reservation, imdsvVe
"region": llx.StringData(regionVal),
"rootDeviceName": llx.StringDataPtr(instance.RootDeviceName),
"rootDeviceType": llx.StringData(string(instance.RootDeviceType)),
"securityGroups": llx.ArrayData(sgs, types.Resource("aws.ec2.securitygroup")),
"state": llx.StringData(string(instance.State.Name)),
"stateReason": llx.MapData(stateReason, types.Any),
"stateTransitionReason": llx.StringDataPtr(instance.StateTransitionReason),
"stateTransitionTime": llx.TimeData(stateTransitionTime),
"tags": llx.MapData(Ec2TagsToMap(instance.Tags), types.String),
}

if instance.ImageId != nil {
mqlImage, err := NewResource(a.MqlRuntime, "aws.ec2.image",
map[string]*llx.RawData{"arn": llx.StringData(fmt.Sprintf(imageArnPattern, regionVal, conn.AccountId(), convert.ToString(instance.ImageId)))})
if err == nil {
args["image"] = llx.ResourceData(mqlImage, mqlImage.MqlName())
} else {
// this is a common case, logging the error here only creates confusion
args["image"] = llx.NilData
}
} else {
args["image"] = llx.NilData
}

// add vpc if there is one
if instance.VpcId != nil {
arn := fmt.Sprintf(vpcArnPattern, regionVal, conn.AccountId(), convert.ToString(instance.VpcId))
Expand All @@ -767,33 +744,69 @@ func (a *mqlAwsEc2) gatherInstanceInfo(instances []ec2types.Reservation, imdsvVe
args["vpcArn"] = llx.NilData
}

// only add a keypair if the ec2 instance has one attached
if instance.KeyName != nil {
mqlKeyPair, err := NewResource(a.MqlRuntime, "aws.ec2.keypair",
map[string]*llx.RawData{
"region": llx.StringData(regionVal),
"name": llx.StringData(convert.ToString(instance.KeyName)),
})
if err == nil {
args["keypair"] = llx.ResourceData(mqlKeyPair, mqlKeyPair.MqlName())
} else {
log.Error().Err(err).Msg("cannot find keypair")
args["keypair"] = llx.NilData
}
} else {
args["keypair"] = llx.NilData
}

mqlEc2Instance, err := CreateResource(a.MqlRuntime, "aws.ec2.instance", args)
if err != nil {
return nil, err
}
mqlEc2Instance.(*mqlAwsEc2Instance).instanceCache = instance
res = append(res, mqlEc2Instance)
}
}
return res, nil
}

type mqlAwsEc2InstanceInternal struct {
instanceCache ec2types.Instance
}

func (i *mqlAwsEc2Instance) securityGroups() ([]interface{}, error) {
if i.instanceCache.SecurityGroups != nil {
sgs := []interface{}{}
conn := i.MqlRuntime.Connection.(*connection.AwsConnection)

for j := range i.instanceCache.SecurityGroups {
mqlSg, err := NewResource(i.MqlRuntime, "aws.ec2.securitygroup",
map[string]*llx.RawData{"arn": llx.StringData(fmt.Sprintf(securityGroupArnPattern, i.Region.Data, conn.AccountId(), convert.ToString(i.instanceCache.SecurityGroups[j].GroupId)))})
if err != nil {
return nil, err
}
sgs = append(sgs, mqlSg)
}
return sgs, nil
}
i.SecurityGroups.State = plugin.StateIsSet | plugin.StateIsNull
return nil, nil
}

func (i *mqlAwsEc2Instance) image() (*mqlAwsEc2Image, error) {
if i.instanceCache.ImageId != nil {
conn := i.MqlRuntime.Connection.(*connection.AwsConnection)

mqlImage, err := NewResource(i.MqlRuntime, "aws.ec2.image",
map[string]*llx.RawData{"arn": llx.StringData(fmt.Sprintf(imageArnPattern, i.Region.Data, conn.AccountId(), convert.ToString(i.instanceCache.ImageId)))})
if err == nil {
return mqlImage.(*mqlAwsEc2Image), nil
}
}
i.Image.State = plugin.StateIsSet | plugin.StateIsNull
return nil, nil
}

func (i *mqlAwsEc2Instance) keypair() (*mqlAwsEc2Keypair, error) {
if i.instanceCache.KeyName != nil {
mqlKeyPair, err := NewResource(i.MqlRuntime, "aws.ec2.keypair",
map[string]*llx.RawData{
"region": llx.StringData(i.Region.Data),
"name": llx.StringData(convert.ToString(i.instanceCache.KeyName)),
})
if err == nil {
return mqlKeyPair.(*mqlAwsEc2Keypair), nil
}
}
i.Keypair.State = plugin.StateIsSet | plugin.StateIsNull
return nil, nil
}

func (i *mqlAwsEc2Image) id() (string, error) {
return i.Arn.Data, nil
}
Expand Down Expand Up @@ -926,10 +939,6 @@ func (a *mqlAwsEc2Instance) vpc() (*mqlAwsVpc, error) {
}
}

func (a *mqlAwsEc2Instance) keypair() (*mqlAwsEc2Keypair, error) {
return a.Keypair.Data, nil
}

func (a *mqlAwsEc2Instance) ssm() (interface{}, error) {
instanceId := a.InstanceId.Data
region := a.Region.Data
Expand Down

0 comments on commit 72792e0

Please sign in to comment.