<commit_before>package aws

import (
	"errors"
	"fmt"
	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/credentials"
	"github.com/aws/aws-sdk-go/aws/ec2metadata"
	"github.com/aws/aws-sdk-go/service/ec2"
	"github.com/awslabs/aws-sdk-go/aws/credentials/ec2rolecreds"
	"github.com/bobtfish/AWSnycast/healthcheck"
	"log"
	"net"
	"net/http"
	"strings"
	"time"
)

type MyEC2Conn interface {
	CreateRoute(*ec2.CreateRouteInput) (*ec2.CreateRouteOutput, error)
	ReplaceRoute(*ec2.ReplaceRouteInput) (*ec2.ReplaceRouteOutput, error)
	DescribeRouteTables(*ec2.DescribeRouteTablesInput) (*ec2.DescribeRouteTablesOutput, error)
	DeleteRoute(*ec2.DeleteRouteInput) (*ec2.DeleteRouteOutput, error)
}

type MetadataFetcher interface {
	Available() bool
	GetMetadata(string) (string, error)
}

func NewMetadataFetcher(debug bool) MetadataFetcher {
	c := ec2metadata.Config{}
	if debug {
		c.LogLevel = aws.LogLevel(aws.LogDebug)
	}
	return ec2metadata.New(&c)
}

type ManageRoutesSpec struct {
	Cidr            string `yaml:"cidr"`
	Instance        string `yaml:"instance"`
	InstanceIsSelf  bool   `yaml:"-"`
	HealthcheckName string `yaml:"healthcheck"`
	healthcheck     healthcheck.CanBeHealthy
	IfUnhealthy     bool `yaml:"if_unhealthy"`
}

func (r *ManageRoutesSpec) Default(instance string) {
	if !strings.Contains(r.Cidr, "/") {
		r.Cidr = fmt.Sprintf("%s/32", r.Cidr)
	}
	if r.Instance == "" {
		r.Instance = "SELF"
	}
	if r.Instance == "SELF" {
		r.InstanceIsSelf = true
		r.Instance = instance
	}
}

func (r *ManageRoutesSpec) Validate(name string, healthchecks map[string]*healthcheck.Healthcheck) error {
	if r.Cidr == "" {
		return errors.New(fmt.Sprintf("cidr is not defined in %s", name))
	}
	if _, _, err := net.ParseCIDR(r.Cidr); err != nil {
		return errors.New(fmt.Sprintf("Could not parse %s in %s", err.Error(), name))
	}
	if r.HealthcheckName != "" {
		hc, ok := healthchecks[r.HealthcheckName]
		if !ok {
			return errors.New(fmt.Sprintf("Route table %s, upsert %s cannot find healthcheck '%s'", name, r.Cidr, r.HealthcheckName))
		}
		r.healthcheck = hc
	}
	return nil
}

type RouteTableFetcher interface {
	GetRouteTables() ([]*ec2.RouteTable, error)
	ManageInstanceRoute(ec2.RouteTable, ManageRoutesSpec, bool) error
}

type RouteTableFetcherEC2 struct {
	Region string
	conn   MyEC2Conn
}

func getCreateRouteInput(rtb ec2.RouteTable, cidr string, instance string) ec2.CreateRouteInput {
	return ec2.CreateRouteInput{
		RouteTableId:         rtb.RouteTableId,
		DestinationCidrBlock: aws.String(cidr),
		InstanceId:           aws.String(instance),
	}
}

func (r RouteTableFetcherEC2) ManageInstanceRoute(rtb ec2.RouteTable, rs ManageRoutesSpec, noop bool) error {
	/*if rs.HealthcheckName != "" {
		if rs.InstanceIsSelf {
		}
	}*/
	route := findRouteFromRouteTable(rtb, rs.Cidr)
	if route != nil {
		if err := r.ReplaceInstanceRoute(rtb.RouteTableId, route, rs.Cidr, rs.Instance, rs.IfUnhealthy, noop); err != nil {
			return err
		}
		return nil
	}
	if rs.HealthcheckName != "" && !rs.healthcheck.IsHealthy() {
		return nil
	}

	opts := getCreateRouteInput(rtb, rs.Cidr, rs.Instance)

	log.Printf("[INFO] Creating route for %s: %#v", *rtb.RouteTableId, opts)
	if !noop {
		if _, err := r.conn.CreateRoute(&opts); err != nil {<commit_msg>Add this logic up here<commit_after>package aws

import (
	"errors"
	"fmt"
	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/credentials"
	"github.com/aws/aws-sdk-go/aws/ec2metadata"
	"github.com/aws/aws-sdk-go/service/ec2"
	"github.com/awslabs/aws-sdk-go/aws/credentials/ec2rolecreds"
	"github.com/bobtfish/AWSnycast/healthcheck"
	"log"
	"net"
	"net/http"
	"strings"
	"time"
)

type MyEC2Conn interface {
	CreateRoute(*ec2.CreateRouteInput) (*ec2.CreateRouteOutput, error)
	ReplaceRoute(*ec2.ReplaceRouteInput) (*ec2.ReplaceRouteOutput, error)
	DescribeRouteTables(*ec2.DescribeRouteTablesInput) (*ec2.DescribeRouteTablesOutput, error)
	DeleteRoute(*ec2.DeleteRouteInput) (*ec2.DeleteRouteOutput, error)
}

type MetadataFetcher interface {
	Available() bool
	GetMetadata(string) (string, error)
}

func NewMetadataFetcher(debug bool) MetadataFetcher {
	c := ec2metadata.Config{}
	if debug {
		c.LogLevel = aws.LogLevel(aws.LogDebug)
	}
	return ec2metadata.New(&c)
}

type ManageRoutesSpec struct {
	Cidr            string `yaml:"cidr"`
	Instance        string `yaml:"instance"`
	InstanceIsSelf  bool   `yaml:"-"`
	HealthcheckName string `yaml:"healthcheck"`
	healthcheck     healthcheck.CanBeHealthy
	IfUnhealthy     bool `yaml:"if_unhealthy"`
}

func (r *ManageRoutesSpec) Default(instance string) {
	if !strings.Contains(r.Cidr, "/") {
		r.Cidr = fmt.Sprintf("%s/32", r.Cidr)
	}
	if r.Instance == "" {
		r.Instance = "SELF"
	}
	if r.Instance == "SELF" {
		r.InstanceIsSelf = true
		r.Instance = instance
	}
}

func (r *ManageRoutesSpec) Validate(name string, healthchecks map[string]*healthcheck.Healthcheck) error {
	if r.Cidr == "" {
		return errors.New(fmt.Sprintf("cidr is not defined in %s", name))
	}
	if _, _, err := net.ParseCIDR(r.Cidr); err != nil {
		return errors.New(fmt.Sprintf("Could not parse %s in %s", err.Error(), name))
	}
	if r.HealthcheckName != "" {
		hc, ok := healthchecks[r.HealthcheckName]
		if !ok {
			return errors.New(fmt.Sprintf("Route table %s, upsert %s cannot find healthcheck '%s'", name, r.Cidr, r.HealthcheckName))
		}
		r.healthcheck = hc
	}
	return nil
}

type RouteTableFetcher interface {
	GetRouteTables() ([]*ec2.RouteTable, error)
	ManageInstanceRoute(ec2.RouteTable, ManageRoutesSpec, bool) error
}

type RouteTableFetcherEC2 struct {
	Region string
	conn   MyEC2Conn
}

func getCreateRouteInput(rtb ec2.RouteTable, cidr string, instance string) ec2.CreateRouteInput {
	return ec2.CreateRouteInput{
		RouteTableId:         rtb.RouteTableId,
		DestinationCidrBlock: aws.String(cidr),
		InstanceId:           aws.String(instance),
	}
}

func (r RouteTableFetcherEC2) ManageInstanceRoute(rtb ec2.RouteTable, rs ManageRoutesSpec, noop bool) error {
	/*if rs.HealthcheckName != "" {
		if rs.InstanceIsSelf {
		}
	}*/
	route := findRouteFromRouteTable(rtb, rs.Cidr)
	if route != nil {
		if route.InstanceId != nil && *(route.InstanceId) == rs.Instance {
			log.Printf("Skipping doing anything, %s is already routed via %s", rs.Cidr, rs.Instance)
			return nil
		}

		if err := r.ReplaceInstanceRoute(rtb.RouteTableId, route, rs.Cidr, rs.Instance, rs.IfUnhealthy, noop); err != nil {
			return err
		}
		return nil
	}
	if rs.HealthcheckName != "" && !rs.healthcheck.IsHealthy() {
		return nil
	}

	opts := getCreateRouteInput(rtb, rs.Cidr, rs.Instance)

	log.Printf("[INFO] Creating route for %s: %#v", *rtb.RouteTableId, opts)
	if !noop {
		if _, err := r.conn.CreateRoute(&opts); err != nil {
{'fraction_non_alphanumeric': 0.1400268136451661, 'fraction_numerical': 0.007448234768359899, 'mean_word_length': 4.392771084337349, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '162403', 'n_tokens_mistral': 2549, 'n_tokens_neox': 2508, 'n_words': 502}