Example #1
0
func DescribeInstances() {
	var filterFlags filterOption
	filter := ec2.NewFilter()
	flags := flag.NewFlagSet("describe-instances", flag.ExitOnError)
	//config := flags.String("c", "none", "the configuration file")
	regionString := flags.String("r", "us-east-1", "the AWS region")
	flags.Var(&filterFlags, "f", "the filter")
	flags.Parse(os.Args[3:])
	auth, err := aws.GetAuth("", "", "", time.Now().Add(time.Second*3600))
	if err != nil {
		panic(err)
	}
	region := aws.Regions[*regionString]
	connection := ec2.New(auth, region)
	for idx := range filterFlags {
		tokens := strings.Split(filterFlags[idx], "=")
		filter.Add(tokens[0], tokens[1])
		//fmt.Printf("foo bar %s", *config)
	}
	resp, err := connection.DescribeInstances(nil, filter)
	if err != nil {
		panic(err)
	}
	reservations := resp.Reservations
	j, err := json.Marshal(reservations)
	if err != nil {
		panic(err)
	}
	os.Stdout.Write(j)
}
Example #2
0
func CreateTags() {
	flags := flag.NewFlagSet("create-tags", flag.ExitOnError)
	regionString := flags.String("r", "us-east-1", "the AWS region")
	tagKey := flags.String("k", "", "tag key")
	tagValue := flags.String("v", "", "tag value")
	flags.Parse(os.Args[4:])
	instanceIdString := os.Args[3]
	auth, authErr := aws.GetAuth("", "", "", time.Now().Add(time.Second*3600))
	if authErr != nil {
		panic(authErr)
	}
	region := aws.Regions[*regionString]
	connection := ec2.New(auth, region)
	instanceIds := make([]string, 1)
	instanceIds[0] = instanceIdString
	tag := ec2.Tag{Key: *tagKey, Value: *tagValue}
	tags := make([]ec2.Tag, 1)
	tags[0] = tag
	resp, err := connection.CreateTags(instanceIds, tags)
	if err != nil {
		panic(err)
		os.Exit(1)
	}
	j, err := json.Marshal(resp)
	os.Stdout.Write(j)
}
Example #3
0
func Publish() {
	flags := flag.NewFlagSet("publish", flag.ExitOnError)
	regionString := flags.String("r", "us-east-1", "the AWS region")
	subjectString := flags.String("s", "no subject", "the subject of the message")
	formatString := flags.String("f", "", "the format of the message")
	topicString := flags.String("t", "", "the AWS SNS topic")
	helpBool := flags.Bool("h", false, "print this help message")
	flags.Parse(os.Args[3:])
	if *helpBool {
		flags.PrintDefaults()
		os.Exit(0)
	}
	stdinBytes, stdinErr := ioutil.ReadAll(os.Stdin)
	if stdinErr != nil {
		panic(stdinErr)
	}
	auth, authErr := aws.GetAuth("", "", "", time.Now().Add(time.Second*3600))
	if authErr != nil {
		panic(authErr)
	}
	region := aws.Regions[*regionString]
	xns := sns.New(auth, region)
	pubOpt := &sns.PublishOpt{string(stdinBytes), *formatString, *subjectString, *topicString}
	result, snsErr := xns.Publish(pubOpt)
	if snsErr != nil {
		panic(snsErr)
	}
	j, err := json.Marshal(result)
	if err != nil {
		panic(err)
	}
	os.Stdout.Write(j)
	os.Exit(0)
}
Example #4
0
func (s *S) TestGetAuthEnv(c *gocheck.C) {
	os.Clearenv()
	os.Setenv("AWS_SECRET_ACCESS_KEY", "secret")
	os.Setenv("AWS_ACCESS_KEY_ID", "access")
	auth, err := aws.GetAuth("", "", "", time.Time{})
	c.Assert(err, gocheck.IsNil)
	c.Assert(auth, gocheck.Equals, aws.Auth{SecretKey: "secret", AccessKey: "access"})
}
Example #5
0
func (s *S) TestGetAuthStatic(c *gocheck.C) {
	exptdate := time.Now().Add(time.Hour)
	auth, err := aws.GetAuth("access", "secret", "token", exptdate)
	c.Assert(err, gocheck.IsNil)
	c.Assert(auth.AccessKey, gocheck.Equals, "access")
	c.Assert(auth.SecretKey, gocheck.Equals, "secret")
	c.Assert(auth.Token(), gocheck.Equals, "token")
	c.Assert(auth.Expiration(), gocheck.Equals, exptdate)
}
Example #6
0
func DeleteSnapshot() {
	flags := flag.NewFlagSet("delete-snapshot", flag.ExitOnError)
	regionString := flags.String("r", "us-east-1", "the AWS region")
	flags.Parse(os.Args[4:])
	snapshotIds := strings.Split(os.Args[3], ",")
	auth, authErr := aws.GetAuth("", "", "", time.Now().Add(time.Second*3600))
	if authErr != nil {
		panic(authErr)
	}
	region := aws.Regions[*regionString]
	connection := ec2.New(auth, region)

	resp, err := connection.DeleteSnapshots(snapshotIds)
	if err != nil {
		panic(err)
		os.Exit(1)
	}
	j, err := json.Marshal(resp)
	os.Stdout.Write(j)
}
Example #7
0
func CreateSnapshot() {
	flags := flag.NewFlagSet("create-snapshot", flag.ExitOnError)
	regionString := flags.String("r", "us-east-1", "the AWS region")
	descriptionString := flags.String("d", "", "description")
	flags.Parse(os.Args[4:])
	volumeIdString := os.Args[3]
	auth, authErr := aws.GetAuth("", "", "", time.Now().Add(time.Second*3600))
	if authErr != nil {
		panic(authErr)
	}
	region := aws.Regions[*regionString]
	connection := ec2.New(auth, region)
	resp, err := connection.CreateSnapshot(volumeIdString, *descriptionString)
	if err != nil {
		panic(err)
		os.Exit(1)
	}
	j, err := json.Marshal(resp)
	os.Stdout.Write(j)
}