diff --git a/catalog/consul.go b/catalog/consul.go index 16f1db2..fe5c20b 100644 --- a/catalog/consul.go +++ b/catalog/consul.go @@ -2,12 +2,13 @@ package catalog import ( "fmt" + "strconv" "strings" "sync" "time" "github.com/hashicorp/consul/api" - "github.com/hashicorp/go-hclog" + hclog "github.com/hashicorp/go-hclog" ) const ( @@ -302,11 +303,18 @@ func (c *consul) create(services map[string]service) int { meta[ConsulSourceKey] = ConsulAWSTag meta[ConsulAWSNS] = ns meta[ConsulAWSID] = n.awsID + + p, err := strconv.Atoi(n.attributes["Port"]) + if err != nil { + panic("Bad port") + } + service := api.AgentService{ ID: id, Service: name, Tags: []string{ConsulAWSTag}, - Address: h, + Address: n.attributes["Address"], + Port: p, Meta: meta, } if n.port != 0 { @@ -314,12 +322,12 @@ func (c *consul) create(services map[string]service) int { } reg := api.CatalogRegistration{ Node: ConsulAWSNodeName, - Address: h, + Address: "127.0.0.1", NodeMeta: map[string]string{ConsulSourceKey: ConsulAWSTag}, SkipNodeUpdate: true, Service: &service, } - _, err := c.client.Catalog().Register(®, nil) + _, err = c.client.Catalog().Register(®, nil) if err != nil { c.log.Error("cannot create service", "error", err.Error()) } else { diff --git a/subcommand/auth.go b/subcommand/auth.go index bcd3cd9..c49d2c0 100644 --- a/subcommand/auth.go +++ b/subcommand/auth.go @@ -2,9 +2,43 @@ package subcommand import ( "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/aws/ec2metadata" "github.com/aws/aws-sdk-go-v2/aws/external" + hclog "github.com/hashicorp/go-hclog" + "os" + "time" ) +type WithEC2MetadataRegion struct { + Client *ec2metadata.EC2Metadata +} + +func (p WithEC2MetadataRegion) GetRegion() (string, error) { + return p.Client.Region() +} + func AWSConfig() (aws.Config, error) { - return external.LoadDefaultAWSConfig() + cfg, err := external.LoadDefaultAWSConfig() + if err != nil { + panic("unable to load SDK config, " + err.Error()) + } + + region, ok := os.LookupEnv("AWS_REGION") + if !ok { + + originalTimeout := cfg.HTTPClient.Timeout + cfg.HTTPClient.Timeout = 2 * time.Second + metaClient := ec2metadata.New(cfg) + + if !metaClient.Available() { + panic("Metadata service cannot be reached.") + } + + cfg.HTTPClient.Timeout = originalTimeout + hclog.Default().Info("Autodetected region") + region, _ = metaClient.Region() + } + + cfg.Region = region + return cfg, err } diff --git a/subcommand/sync-catalog/command.go b/subcommand/sync-catalog/command.go index 4d2699e..4d5406b 100644 --- a/subcommand/sync-catalog/command.go +++ b/subcommand/sync-catalog/command.go @@ -11,6 +11,7 @@ import ( "github.com/hashicorp/consul-aws/catalog" "github.com/hashicorp/consul-aws/subcommand" "github.com/hashicorp/consul/command/flags" + hclog "github.com/hashicorp/go-hclog" "github.com/mitchellh/cli" ) @@ -24,6 +25,7 @@ type Command struct { http *flags.HTTPFlags flagToConsul bool flagToAWS bool + flagAWSNamespace string flagAWSNamespaceID string flagAWSServicePrefix string flagAWSDeprecatedPullInterval string @@ -42,6 +44,8 @@ func (c *Command) init() { "If true, AWS services will be synced to Consul. (Defaults to false)") c.flags.BoolVar(&c.flagToAWS, "to-aws", false, "If true, Consul services will be synced to AWS. (Defaults to false)") + c.flags.StringVar(&c.flagAWSNamespace, "aws-namespace", + "", "The AWS namespace to sync with Consul services.") c.flags.StringVar(&c.flagAWSNamespaceID, "aws-namespace-id", "", "The AWS namespace to sync with Consul services.") c.flags.StringVar(&c.flagAWSServicePrefix, "aws-service-prefix", @@ -78,8 +82,12 @@ func (c *Command) Run(args []string) int { c.UI.Error("Should have no non-flag arguments.") return 1 } - if len(c.flagAWSNamespaceID) == 0 { - c.UI.Error("Please provide -aws-namespace-id.") + if len(c.flagAWSNamespaceID) == 0 && len(c.flagAWSNamespace) == 0 { + c.UI.Error("Please provide -aws-namespace or -aws-namespace-id.") + return 1 + } + if len(c.flagAWSNamespaceID) > 0 && len(c.flagAWSNamespace) > 0 { + c.UI.Error("Please provide -aws-namespace or -aws-namespace-id.") return 1 } config, err := subcommand.AWSConfig() @@ -89,6 +97,28 @@ func (c *Command) Run(args []string) int { } awsClient := sd.New(config) + if len(c.flagAWSNamespace) > 0 { + + resp := awsClient.ListNamespacesRequest(&sd.ListNamespacesInput{}) + pager := resp.Paginate() + for pager.Next() { + page := pager.CurrentPage() + for _, namespace := range page.Namespaces { + if c.flagAWSNamespace == *namespace.Name { + hclog.Default().Info("NamespaceId found", *namespace.Id) + c.flagAWSNamespaceID = *namespace.Id + } + } + } + if err := pager.Err(); err != nil { + panic("paging failed, " + err.Error()) + } + } + + if len(c.flagAWSNamespaceID) == 0 { + panic("Namespace not found, aborting...") + } + consulClient, err := c.http.APIClient() if err != nil { c.UI.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err))