Skip to content

Commit

Permalink
V0.0.1 rc2 (#3)
Browse files Browse the repository at this point in the history
fix: generate s3 path correct without prefix
test: Update tests for AccesslogPath(s3path to access log)
fix: Adding correct default value for start-time and end-time
  • Loading branch information
dbgeek authored Mar 3, 2019
1 parent 674f76a commit 78e76d3
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 13 deletions.
2 changes: 1 addition & 1 deletion cmd/cat.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ possible user these filter

for _, v := range client.List() {
buff := &aws.WriteAtBuffer{}
key := fmt.Sprintf("%s%s%s", configuration.Prefix, accessLogFilter.AccesslogPath(), v)
key := fmt.Sprintf("%s%s", accessLogFilter.AccesslogPath(configuration.Prefix), v)
_, err := client.S3Downloader.Download(buff, &s3.GetObjectInput{
Bucket: aws.String(viper.GetString("s3-bucket")),
Key: aws.String(key),
Expand Down
11 changes: 9 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"fmt"
"os"
"time"

homedir "github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -58,9 +59,9 @@ func init() {
viper.BindPFlag("s3-bucket", rootCmd.PersistentFlags().Lookup("s3-bucket"))
rootCmd.PersistentFlags().StringP("s3-prefix", "p", ".*", "The prefix (logical hierarchy) in the bucket. If you don't specify a prefix, the logs are placed at the root level of the bucket.")
viper.BindPFlag("s3-prefix", rootCmd.PersistentFlags().Lookup("s3-prefix"))
rootCmd.PersistentFlags().StringP("start-time", "", ".*", "")
rootCmd.PersistentFlags().StringP("start-time", "", defaultStartTime().Format("2006-01-02 15:04:05"), "")
viper.BindPFlag("start-time", rootCmd.PersistentFlags().Lookup("start-time"))
rootCmd.PersistentFlags().StringP("end-time", "", ".*", "")
rootCmd.PersistentFlags().StringP("end-time", "", time.Now().Format("2006-01-02 15:04:05"), "")
viper.BindPFlag("end-time", rootCmd.PersistentFlags().Lookup("end-time"))

// Cobra also supports local flags, which will only run
Expand Down Expand Up @@ -93,3 +94,9 @@ func initConfig() {
fmt.Println("Using config file:", viper.ConfigFileUsed())
}
}

func defaultStartTime() time.Time {
t := time.Now().UTC()
d := 24 * time.Hour
return t.Truncate(d)
}
15 changes: 11 additions & 4 deletions logworker/logworker.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package logworker
import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
"time"
Expand Down Expand Up @@ -117,7 +118,7 @@ func (l *LogWorker) List() []string {
var accessLogs []string
input := &s3.ListObjectsV2Input{
Bucket: aws.String(l.Configuration.Bucket),
Prefix: aws.String(fmt.Sprintf("%s%s", l.Configuration.Prefix, l.AccessLogFilter.AccesslogPath())),
Prefix: aws.String(l.AccessLogFilter.AccesslogPath(l.Configuration.Prefix)),
Delimiter: aws.String("/"),
MaxKeys: aws.Int64(200),
}
Expand All @@ -137,8 +138,9 @@ func (l *LogWorker) List() []string {
return accessLogs
}

func (a *AccessLogFilter) AccesslogPath() string {
return fmt.Sprintf("/AWSLogs/%s/elasticloadbalancing/%s/%s/", a.AwsAccountID, a.Region, a.StartTime.Format("2006/01/02"))
func (a *AccessLogFilter) AccesslogPath(prefix string) string {
return filepath.Join(prefix, fmt.Sprintf("AWSLogs/%s/elasticloadbalancing/%s/%s/", a.AwsAccountID, a.Region, a.StartTime.Format("2006/01/02"))) + "/"

}

func (a *AccessLogFilter) beforeEndTime(accessLog string) bool {
Expand Down Expand Up @@ -176,11 +178,16 @@ func NewAccessLogFilter() AccessLogFilter {
Logger.Fatalf("Failed to parse start time. Gott error: %v", err)
fmt.Println("failed to parse starttime")
}

endTime, err := time.Parse("2006-01-02 15:04:05", viper.GetString("end-time"))
if err != nil {
Logger.Fatalf("Failed to parse end time. Gott error: %v", err)
fmt.Println("failed to parse endtime")
}
accessLogFilter := AccessLogFilter{}
accessLogFilter.AwsAccountID = viper.GetString("aws-account-id")
accessLogFilter.Region = viper.GetString("region")
accessLogFilter.StartTime = startTime // time.Now()
accessLogFilter.EndTime = endTime
accessLogFilter.LoadBalancerID = viper.GetString("load-balancer-id")
accessLogFilter.IPaddress = viper.GetString("ip-address")
accessLogFilter.RandomString = viper.GetString("random-string")
Expand Down
24 changes: 18 additions & 6 deletions logworker/logworker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,40 @@ import (
"time"
)

func TestAccessLogFilter(t *testing.T) {
func TestAccessLogFilterAccesslogPath(t *testing.T) {

tt := []struct {
name string
accessLogFilter AccessLogFilter
accessLogPath string
prefix string
out string
}{
{"test1",
{"WithoutPrefix",
AccessLogFilter{

AwsAccountID: "00000000111111",
Region: "eu-west-1",
StartTime: time.Now(),
},
fmt.Sprintf("/AWSLogs/00000000111111/elasticloadbalancing/eu-west-1/%s/", time.Now().Format("2006/01/02")),
"",
fmt.Sprintf("AWSLogs/00000000111111/elasticloadbalancing/eu-west-1/%s/", time.Now().Format("2006/01/02")),
},
{"WithPrefix",
AccessLogFilter{

AwsAccountID: "00000000111111",
Region: "eu-west-1",
StartTime: time.Now(),
},
"team-xxx",
fmt.Sprintf("team-xxx/AWSLogs/00000000111111/elasticloadbalancing/eu-west-1/%s/", time.Now().Format("2006/01/02")),
},
}

for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
if tc.accessLogFilter.AccesslogPath() != tc.accessLogPath {
t.Fatalf("accespath test %v should be %v; got %v", tc.name, tc.accessLogFilter.AccesslogPath(), tc.accessLogPath)
if tc.accessLogFilter.AccesslogPath(tc.prefix) != tc.out {
t.Fatalf("accespath test %v should be %v; got %v", tc.name, tc.out, tc.accessLogFilter.AccesslogPath(tc.prefix))
}
})
}
Expand Down

0 comments on commit 78e76d3

Please sign in to comment.