def getSubnetScopes()

in app/collectors/vpc.scala [56:100]


  def getSubnetScopes(
      vpcId: String,
      subnets: List[AwsSubnet]
  ): Map[String, SubnetScope] = {
    val req = DescribeRouteTablesRequest
      .builder()
      .filters(Filter.builder().name("vpc-id").values(vpcId).build)
      .build
    val tablesData =
      client.describeRouteTablesPaginator(req).routeTables().asScala

    // Let's convert the AWS data into something more useful for our purposes.
    val tables = tablesData.map(table => {
      val assocs = table.associations().asScala.toList
      val routes = table.routes().asScala.toList

      val isMain = assocs.exists(assoc => assoc.main())

      // It feels like there should be a better way to detect the presence of an AWS Internet Gateway but apparently this is it :(.
      val tableHasIgw = routes.exists(route =>
        Option(route.gatewayId()).getOrElse("").startsWith("igw")
      )
      val subnetIDs = assocs.flatMap(assoc => Option(assoc.subnetId()).toList)
      RouteTable(
        isMain = isMain,
        hasInternetGateway = tableHasIgw,
        subnetIDs = subnetIDs.toSet
      )
    })

    val data = subnets.map(subnet => {
      // If there is no explicit route table associated with a subnet, the VPC 'main' route table is used instead.
      val main = tables.find(table => table.isMain)
      val associatedTable =
        tables.find(table => table.subnetIDs.contains(subnet.subnetId))

      val isPublic =
        associatedTable.orElse(main).exists(table => table.hasInternetGateway)
      val scope = if (isPublic) Public else Private

      (subnet.subnetId -> scope)
    })

    data.toMap
  }