<commit_before>package config

import (
	"errors"
	"fmt"
	"github.com/aws/aws-sdk-go/service/ec2"
	"github.com/bobtfish/AWSnycast/aws"
	"github.com/bobtfish/AWSnycast/healthcheck"
	"github.com/bobtfish/AWSnycast/instancemetadata"
	"gopkg.in/yaml.v2"
	"io/ioutil"
	"log"
)

type Config struct {
	PollTime           uint                                `yaml:"poll_time"`
	Healthchecks       map[string]*healthcheck.Healthcheck `yaml:"healthchecks"`
	RemoteHealthchecks map[string]*healthcheck.Healthcheck `yaml:"remote_healthchecks"`
	RouteTables        map[string]*RouteTable              `yaml:"routetables"`
}

type RouteTableFindSpec struct {
	Type   string            `yaml:"type"`
	Config map[string]string `yaml:"config"`
}

var routeFindTypes map[string]func(RouteTableFindSpec) (aws.RouteTableFilter, error)

func init() {
	routeFindTypes = make(map[string]func(RouteTableFindSpec) (aws.RouteTableFilter, error))
	routeFindTypes["by_tag"] = func(spec RouteTableFindSpec) (aws.RouteTableFilter, error) {
		if _, ok := spec.Config["key"]; !ok {
			return nil, errors.New("No key in config for by_tag route table finder")
		}
		if _, ok := spec.Config["value"]; !ok {
			return nil, errors.New("No value in config for by_tag route table finder")
		}
		return aws.RouteTableFilterTagMatch{
			Key:   spec.Config["key"],
			Value: spec.Config["value"],
		}, nil
	}
}

func (spec RouteTableFindSpec) GetFilter() (aws.RouteTableFilter, error) {
	if genFilter, found := routeFindTypes[spec.Type]; found {
		return genFilter(spec)
	}
	return nil, errors.New(fmt.Sprintf("Route table finder type '%s' not found in the registry", spec.Type))
}

type RouteTable struct {
	Find           RouteTableFindSpec      `yaml:"find"`
	ManageRoutes   []*aws.ManageRoutesSpec `yaml:"manage_routes"`
	ec2RouteTables []*ec2.RouteTable
}

func (r *RouteTable) UpdateEc2RouteTables(rt []*ec2.RouteTable) error {
	filter, err := r.Find.GetFilter()
	if err != nil {
		return err
	}
	r.ec2RouteTables = aws.FilterRouteTables(filter, rt)
	if len(r.ec2RouteTables) == 0 {
		return errors.New("No route table in AWS matched filter spec")
	}
	for _, manage := range r.ManageRoutes {
		manage.UpdateEc2RouteTables(r.ec2RouteTables)
	}
	return nil
}

func (r *RouteTable) RunEc2Updates(manager aws.RouteTableManager, noop bool) error {
	for _, rtb := range r.ec2RouteTables {
		log.Printf("Finder found route table %v", rtb)
		for _, manageRoute := range r.ManageRoutes {
			manageRoute.Manager = manager // FIXME - this is gross
			log.Printf("Trying to manage route to %s", manageRoute.Cidr)
			if err := manager.ManageInstanceRoute(*rtb, *manageRoute, noop); err != nil {
				return err
			}
		}
	}
	return nil
}

func (c *Config) Default(im instancemetadata.InstanceMetadata, manager aws.RouteTableManager) {
	if c.PollTime == 0 {
		c.PollTime = 300 // Default to every 5m
	}
	if c.Healthchecks == nil {
		c.Healthchecks = make(map[string]*healthcheck.Healthcheck)
	}
	if c.RemoteHealthchecks == nil {
		c.RemoteHealthchecks = make(map[string]*healthcheck.Healthcheck)
	}
	if c.RouteTables != nil {
		for _, v := range c.RouteTables {
			v.Default(im.Instance, manager)
		}
	} else {
		c.RouteTables = make(map[string]*RouteTable)
	}
	for _, v := range c.Healthchecks {
		v.Default(im)
	}
	for _, v := range c.RemoteHealthchecks {
		v.Default(im)<commit_msg>Remove gross hack<commit_after>package config

import (
	"errors"
	"fmt"
	"github.com/aws/aws-sdk-go/service/ec2"
	"github.com/bobtfish/AWSnycast/aws"
	"github.com/bobtfish/AWSnycast/healthcheck"
	"github.com/bobtfish/AWSnycast/instancemetadata"
	"gopkg.in/yaml.v2"
	"io/ioutil"
	"log"
)

type Config struct {
	PollTime           uint                                `yaml:"poll_time"`
	Healthchecks       map[string]*healthcheck.Healthcheck `yaml:"healthchecks"`
	RemoteHealthchecks map[string]*healthcheck.Healthcheck `yaml:"remote_healthchecks"`
	RouteTables        map[string]*RouteTable              `yaml:"routetables"`
}

type RouteTableFindSpec struct {
	Type   string            `yaml:"type"`
	Config map[string]string `yaml:"config"`
}

var routeFindTypes map[string]func(RouteTableFindSpec) (aws.RouteTableFilter, error)

func init() {
	routeFindTypes = make(map[string]func(RouteTableFindSpec) (aws.RouteTableFilter, error))
	routeFindTypes["by_tag"] = func(spec RouteTableFindSpec) (aws.RouteTableFilter, error) {
		if _, ok := spec.Config["key"]; !ok {
			return nil, errors.New("No key in config for by_tag route table finder")
		}
		if _, ok := spec.Config["value"]; !ok {
			return nil, errors.New("No value in config for by_tag route table finder")
		}
		return aws.RouteTableFilterTagMatch{
			Key:   spec.Config["key"],
			Value: spec.Config["value"],
		}, nil
	}
}

func (spec RouteTableFindSpec) GetFilter() (aws.RouteTableFilter, error) {
	if genFilter, found := routeFindTypes[spec.Type]; found {
		return genFilter(spec)
	}
	return nil, errors.New(fmt.Sprintf("Route table finder type '%s' not found in the registry", spec.Type))
}

type RouteTable struct {
	Find           RouteTableFindSpec      `yaml:"find"`
	ManageRoutes   []*aws.ManageRoutesSpec `yaml:"manage_routes"`
	ec2RouteTables []*ec2.RouteTable
}

func (r *RouteTable) UpdateEc2RouteTables(rt []*ec2.RouteTable) error {
	filter, err := r.Find.GetFilter()
	if err != nil {
		return err
	}
	r.ec2RouteTables = aws.FilterRouteTables(filter, rt)
	if len(r.ec2RouteTables) == 0 {
		return errors.New("No route table in AWS matched filter spec")
	}
	for _, manage := range r.ManageRoutes {
		manage.UpdateEc2RouteTables(r.ec2RouteTables)
	}
	return nil
}

func (r *RouteTable) RunEc2Updates(manager aws.RouteTableManager, noop bool) error {
	for _, rtb := range r.ec2RouteTables {
		log.Printf("Finder found route table %v", rtb)
		for _, manageRoute := range r.ManageRoutes {
			log.Printf("Trying to manage route to %s", manageRoute.Cidr)
			if err := manager.ManageInstanceRoute(*rtb, *manageRoute, noop); err != nil {
				return err
			}
		}
	}
	return nil
}

func (c *Config) Default(im instancemetadata.InstanceMetadata, manager aws.RouteTableManager) {
	if c.PollTime == 0 {
		c.PollTime = 300 // Default to every 5m
	}
	if c.Healthchecks == nil {
		c.Healthchecks = make(map[string]*healthcheck.Healthcheck)
	}
	if c.RemoteHealthchecks == nil {
		c.RemoteHealthchecks = make(map[string]*healthcheck.Healthcheck)
	}
	if c.RouteTables != nil {
		for _, v := range c.RouteTables {
			v.Default(im.Instance, manager)
		}
	} else {
		c.RouteTables = make(map[string]*RouteTable)
	}
	for _, v := range c.Healthchecks {
		v.Default(im)
	}
	for _, v := range c.RemoteHealthchecks {
		v.Default(im)
{'fraction_non_alphanumeric': 0.12900786448880822, 'fraction_numerical': 0.0054446460980036296, 'mean_word_length': 4.067432950191571, '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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '571584', 'n_tokens_mistral': 2386, 'n_tokens_neox': 2258, 'n_words': 507}