|
| 1 | +package ec2 |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/apache/arrow/go/v13/arrow" |
| 7 | + "github.com/aws/aws-sdk-go-v2/aws" |
| 8 | + "github.com/aws/aws-sdk-go-v2/service/ec2" |
| 9 | + "github.com/aws/aws-sdk-go-v2/service/ec2/types" |
| 10 | + "github.com/cloudquery/cloudquery/plugins/source/aws/client" |
| 11 | + "github.com/cloudquery/plugin-sdk/v3/schema" |
| 12 | +) |
| 13 | + |
| 14 | +func imageAttributesLastLaunchTime() *schema.Table { |
| 15 | + return &schema.Table{ |
| 16 | + Name: "aws_ec2_image_last_launched_times", |
| 17 | + Description: `https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeImageAttribute.html. |
| 18 | +The date and time, in ISO 8601 date-time format, when the AMI was last used to launch an EC2 instance. When the AMI is used to launch an instance, there is a 24-hour delay before that usage is reported.`, |
| 19 | + Resolver: fetchEc2ImageAttributeLastLaunchTime, |
| 20 | + Columns: []schema.Column{ |
| 21 | + { |
| 22 | + Name: "image_arn", |
| 23 | + Type: arrow.BinaryTypes.String, |
| 24 | + Resolver: schema.ParentColumnResolver("arn"), |
| 25 | + PrimaryKey: true, |
| 26 | + }, |
| 27 | + { |
| 28 | + Name: "last_launched_time", |
| 29 | + Type: arrow.FixedWidthTypes.Timestamp_us, |
| 30 | + Resolver: schema.PathResolver("Value"), |
| 31 | + }, |
| 32 | + }, |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +func fetchEc2ImageAttributeLastLaunchTime(ctx context.Context, meta schema.ClientMeta, parent *schema.Resource, res chan<- any) error { |
| 37 | + c := meta.(*client.Client) |
| 38 | + p := parent.Item.(types.Image) |
| 39 | + if aws.ToString(p.OwnerId) != c.AccountID { |
| 40 | + return nil |
| 41 | + } |
| 42 | + svc := c.Services().Ec2 |
| 43 | + output, err := svc.DescribeImageAttribute(ctx, &ec2.DescribeImageAttributeInput{ |
| 44 | + Attribute: types.ImageAttributeNameLastLaunchedTime, |
| 45 | + ImageId: p.ImageId, |
| 46 | + }, func(options *ec2.Options) { |
| 47 | + options.Region = c.Region |
| 48 | + }) |
| 49 | + if err != nil { |
| 50 | + return err |
| 51 | + } |
| 52 | + res <- output.LastLaunchedTime |
| 53 | + return nil |
| 54 | +} |
0 commit comments