Skip to content

Commit

Permalink
Move GenerateHostname to its own package so it can be imported (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
deedubs committed May 30, 2018
1 parent 53fa0d8 commit 7309b54
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 27 deletions.
32 changes: 32 additions & 0 deletions identity/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package identity

import (
"strings"

"github.com/aws/aws-sdk-go/service/ec2"
)

// GenerateHostname Generates a hostname from the ec2.Instance and tags
func GenerateHostname(instance ec2.Instance) (*string, error) {

privateIP := instance.PrivateIpAddress
hashedPrivateIP := strings.Replace(*privateIP, ".", "-", 10)

tags := tagsToMap(instance.Tags)

hostname := tags["HostnamePrefix"] + hashedPrivateIP

return &hostname, nil
}

func tagsToMap(tags []*ec2.Tag) map[string]string {

tagMap := make(map[string]string)

for _, element := range tags {
var el = *element
tagMap[*el.Key] = *el.Value
}

return tagMap
}
29 changes: 2 additions & 27 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import (
"flag"
"io/ioutil"
"log"
"strings"
"syscall"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/ec2metadata"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/massiveco/aws-hostname/identity"
)

const hostnamePath = "/etc/hostname"
Expand All @@ -30,7 +30,7 @@ func main() {
log.Fatal(err)
}

hostname, err := GenerateHostname(*instance)
hostname, err := identity.GenerateHostname(*instance)
if err != nil {
log.Fatal(err)
}
Expand Down Expand Up @@ -99,28 +99,3 @@ func getInstance() (*ec2.Instance, error) {
}
return nil, nil
}

// GenerateHostname Generates a hostname from the ec2.Instance and tags
func GenerateHostname(instance ec2.Instance) (*string, error) {

privateIP := instance.PrivateIpAddress
hashedPrivateIP := strings.Replace(*privateIP, ".", "-", 10)

tags := tagsToMap(instance.Tags)

hostname := tags["HostnamePrefix"] + hashedPrivateIP

return &hostname, nil
}

func tagsToMap(tags []*ec2.Tag) map[string]string {

tagMap := make(map[string]string)

for _, element := range tags {
var el = *element
tagMap[*el.Key] = *el.Value
}

return tagMap
}

0 comments on commit 7309b54

Please sign in to comment.