Skip to content

Commit

Permalink
Merge pull request #373 from flanksource/fix/aws-resource-linking
Browse files Browse the repository at this point in the history
feat: add availability zone id config type
  • Loading branch information
moshloop authored Jan 30, 2024
2 parents b0fee8d + 68f2048 commit e19c6c9
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
1 change: 1 addition & 0 deletions api/v1/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ const (
AWSEC2Subnet = "AWS::EC2::Subnet"
AWSAccount = "AWS::::Account"
AWSAvailabilityZone = "AWS::AvailabilityZone"
AWSAvailabilityZoneID = "AWS::AvailabilityZoneID"
AWSEC2SecurityGroup = "AWS::EC2::SecurityGroup"
AWSIAMUser = "AWS::IAM::User"
AWSIAMRole = "AWS::IAM::Role"
Expand Down
44 changes: 40 additions & 4 deletions scrapers/aws/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,18 +315,47 @@ func (aws Scraper) account(ctx *AWSContext, config v1.AWS, results *v1.ScrapeRes
return
}

var uniqueAvailabilityZoneIDs = map[string]struct{}{}
for _, az := range azDescribeOutput.AvailabilityZones {
*results = append(*results, v1.ScrapeResult{
ID: lo.FromPtr(az.ZoneId),
ID: lo.FromPtr(az.ZoneName),
Type: v1.AWSAvailabilityZone,
BaseScraper: config.BaseScraper,
Config: az,
ConfigClass: "AvailabilityZone",
Aliases: []string{lo.FromPtr(az.ZoneName)},
Aliases: nil,
Name: lo.FromPtr(az.ZoneName),
ParentExternalID: lo.FromPtr(az.RegionName),
ParentType: v1.AWSRegion,
ParentExternalID: lo.FromPtr(ctx.Caller.Account),
ParentType: v1.AWSAccount,
RelationshipResults: []v1.RelationshipResult{
{
ConfigExternalID: v1.ExternalID{ConfigType: v1.AWSAvailabilityZone, ExternalID: []string{lo.FromPtr(az.ZoneName)}},
RelatedExternalID: v1.ExternalID{ConfigType: v1.AWSAvailabilityZoneID, ExternalID: []string{lo.FromPtr(az.ZoneId)}},
Relationship: "AvailabilityZoneIDAvailabilityZone",
},
{
ConfigExternalID: v1.ExternalID{ConfigType: v1.AWSAvailabilityZone, ExternalID: []string{lo.FromPtr(az.ZoneName)}},
RelatedExternalID: v1.ExternalID{ConfigType: v1.AWSRegion, ExternalID: []string{lo.FromPtr(az.RegionName)}},
Relationship: "RegionAvailabilityZone",
},
},
})

if _, ok := uniqueAvailabilityZoneIDs[lo.FromPtr(az.ZoneId)]; !ok {
*results = append(*results, v1.ScrapeResult{
ID: lo.FromPtr(az.ZoneId),
Type: v1.AWSAvailabilityZoneID,
BaseScraper: config.BaseScraper,
Config: map[string]string{"RegionName": *az.RegionName},
ConfigClass: "AvailabilityZone",
Aliases: nil,
Name: lo.FromPtr(az.ZoneId),
ParentExternalID: lo.FromPtr(az.RegionName),
ParentType: v1.AWSRegion,
})

uniqueAvailabilityZoneIDs[lo.FromPtr(az.ZoneId)] = struct{}{}
}
}
}

Expand Down Expand Up @@ -965,6 +994,13 @@ func (aws Scraper) subnets(ctx *AWSContext, config v1.AWS, results *v1.ScrapeRes
Relationship: "AvailabilityZoneSubnet",
})

// Subnet to availability zone relationship
relationships = append(relationships, v1.RelationshipResult{
RelatedExternalID: selfExternalID,
ConfigExternalID: v1.ExternalID{ExternalID: []string{lo.FromPtr(subnet.AvailabilityZoneId)}, ConfigType: v1.AWSAvailabilityZoneID},
Relationship: "AvailabilityZoneIDSubnet",
})

result := v1.ScrapeResult{
Type: v1.AWSEC2Subnet,
BaseScraper: config.BaseScraper,
Expand Down

0 comments on commit e19c6c9

Please sign in to comment.