func main() { creds := aws.Creds(os.Getenv("research_key"), os.Getenv("research_secret"), "") cli := elb.New(creds, "us-east-1", nil) resp, err := descElb("grinder-perf", cli) if err != nil { fmt.Println(err) } // fmt.Println(resp) for _, v := range resp.LoadBalancerDescriptions { descInstanceHealth(v.Instances, cli) } //fmt.Printf("%v", reflect.TypeOf(cli)) // args := os.Args[1:] // for i := 0; i < len(args); i++ { //elbinput := elb.DescribeAccessPointsInput{LoadBalancerNames: []string{args[i]},} //elbs, err := cli.DescribeLoadBalancers(&elbinput) //elbs, err := cli.DescribeLoadBalancers(nil) // if err != nil { // fmt.Print(err) // } // for _, elb := range elbs.LoadBalancerDescriptions { // fmt.Println("LoadBalancer Name:", *elb.LoadBalancerName) // for _, instance := range elb.Instances { // fmt.Println(*instance.InstanceID) // } // } // } }
func main() { AccessKey := os.Getenv("AWS_ACCESS_KEY") SecretKey := os.Getenv("AWS_SECRET_KEY") creds := aws.Creds(AccessKey, SecretKey, "") svc := ec2.New(&aws.Config{Credentials: creds, Region: "ap-southeast-1"}) resp, err := svc.DescribeInstances(nil) fmt.Println(reflect.TypeOf(resp)) if err != nil { panic(err) } //fmt.Println(resp.Reservations) for _, reservation := range resp.Reservations { //fmt.Println(reservation.Instances) for _, instance := range reservation.Instances { for _, t := range instance.Tags { if *t.Value == "cassandra" { fmt.Print(*instance.InstanceID, "\t", *instance.InstanceType, "\t", *instance.PrivateIPAddress, "\t") fmt.Println(*t.Value) fmt.Println() } } } } }
func doesSubDomainExist(subDomain string) bool { // if it's configured for testing then skip this step if utils.Cfg.AWSSettings.Route53AccessKeyId == "" { return false } creds := aws.Creds(utils.Cfg.AWSSettings.Route53AccessKeyId, utils.Cfg.AWSSettings.Route53SecretAccessKey, "") r53 := route53.New(aws.DefaultConfig.Merge(&aws.Config{Credentials: creds, Region: utils.Cfg.AWSSettings.Route53Region})) r53req := &route53.ListResourceRecordSetsInput{ HostedZoneID: aws.String(utils.Cfg.AWSSettings.Route53ZoneId), MaxItems: aws.String("1"), StartRecordName: aws.String(fmt.Sprintf("%v.%v.", subDomain, utils.Cfg.ServiceSettings.Domain)), } if result, err := r53.ListResourceRecordSets(r53req); err != nil { l4g.Error("error in doesSubDomainExist domain=%v err=%v", subDomain, err) return true } else { for _, v := range result.ResourceRecordSets { if v.Name != nil && *v.Name == fmt.Sprintf("%v.%v.", subDomain, utils.Cfg.ServiceSettings.Domain) { return true } } } return false }
func main() { creds := aws.Creds("ACCESS", "SECRET", "") cli := elb.New(creds, "us-east-1", nil) // elbs := elb.DescribeAccessPointsInput{LoadBalancerNames: []string{"grinder-perf", "etcd-test"}} // elbs.LoadBalancerNames = []string{"test"} // resp, err := cli.DescribeLoadBalancers(&elbs) resp, err := getElbInfo("grinder-perf", &cli) if err != nil { fmt.Println(err) return } for _, v := range resp.LoadBalancerDescriptions { // elbInfo[*v.DNSName] getInstanceHealth(v.Instances, &cli) // for _, instance := range v.Instances { // health = getInstanceHealth(id, &cli) // elbInfo[*v.DNSName][*instance.InstanceID] // fmt.Println(elbInfo) // } // elbInfo["test"]["instance"] = string(*v.Instances[0].InstanceID) // fmt.Println(*v.Instances[0].InstanceID) // fmt.Println(v.AvailabilityZones) } fmt.Println("######################", elbInfo) }
func TestRestRequestXMLError(t *testing.T) { var m sync.Mutex var httpReq *http.Request server := httptest.NewServer(http.HandlerFunc( func(w http.ResponseWriter, r *http.Request) { m.Lock() defer m.Unlock() httpReq = r w.Header().Set("Content-Type", "application/xml") w.WriteHeader(500) fmt.Fprintln(w, `<Error> <Code>bonus</Code> <BucketName>bingo</BucketName> <Message>the bad thing</Message> <RequestId>woo woo</RequestId> <HostId>woo woo</HostId> </Error>`) }, )) defer server.Close() client := aws.RestClient{ Context: aws.Context{ Service: "animals", Region: "us-west-2", Credentials: aws.Creds( "accessKeyID", "secretAccessKey", "securityToken", ), }, Client: http.DefaultClient, } req, err := http.NewRequest("GET", server.URL+"/yay", nil) if err != nil { t.Fatal(err) } _, err = client.Do(req) if err == nil { t.Fatal("Expected an error but none was returned") } if err, ok := err.(aws.APIError); ok { if v, want := err.Code, "bonus"; v != want { t.Errorf("Error code was %v, but expected %v", v, want) } if v, want := err.Message, "the bad thing"; v != want { t.Errorf("Error message was %v, but expected %v", v, want) } } else { t.Errorf("Unknown error returned: %#v", err) } }
func NewManager(addr string, password string, awsId string, awsKey string, zoneId string, defaultTTL int64, reservedPrefixes []string, maxUserDomains int) (*Manager, error) { log.Debugf("connecting to redis: addr=%s", addr) pool := &redis.Pool{ MaxIdle: 3, IdleTimeout: 240 * time.Second, Dial: func() (redis.Conn, error) { c, err := redis.Dial("tcp", addr) if err != nil { return nil, err } if password != "" { if _, err := c.Do("AUTH", password); err != nil { c.Close() return nil, err } } return c, err }, TestOnBorrow: func(c redis.Conn, t time.Time) error { _, err := c.Do("PING") return err }, } log.Debugf("maximum user domains: %d", maxUserDomains) creds := aws.Creds(awsId, awsKey, "") awsConfig := &aws.Config{ Credentials: creds, } r53 := route53.New(awsConfig) params := &route53.GetHostedZoneInput{ ID: aws.String(zoneId), } resp, err := r53.GetHostedZone(params) if err != nil { return nil, err } if resp == nil { return nil, fmt.Errorf("no zone returned") } zoneBase := *resp.HostedZone.Name log.Infof("connected to route53: zone=%s", zoneBase) return &Manager{ pool: pool, r53: r53, zoneId: zoneId, defaultTTL: defaultTTL, zoneBase: zoneBase, reservedPrefixes: reservedPrefixes, maxUserDomains: maxUserDomains, }, nil }
//NewS3Source create s3 source with access key and secret key func NewS3Source(accessKey, secretKey string) *S3Source { cli := s3.New(&aws.Config{ Credentials: aws.Creds(accessKey, secretKey, ""), Region: "ap-southeast-1", }) return &S3Source{cli} }
func (c *AWSCluster) FindCredentials() (aws.CredentialsProvider, error) { if c.base.CredentialID == "aws_env" { return aws.EnvCreds() } cred, err := c.base.FindCredentials() if err != nil { return nil, err } return aws.Creds(cred.ID, cred.Secret, ""), nil }
func (c *AWSCluster) SetCreds(creds *Credential) error { if creds == nil || creds.ID == "aws_env" { c.base.CredentialID = "aws_env" awsCreds, err := aws.EnvCreds() if err != nil { return err } c.creds = awsCreds } else { c.base.credential = creds c.base.CredentialID = creds.ID c.creds = aws.Creds(creds.ID, creds.Secret, "") } c.ec2 = ec2.New(c.creds, c.Region, nil) c.cf = cloudformation.New(c.creds, c.Region, nil) return nil }
func TestJSONRequestError(t *testing.T) { server := httptest.NewServer(http.HandlerFunc( func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(400) fmt.Fprintln(w, `{"__type":"Problem", "message":"What even"}`) }, )) defer server.Close() client := aws.JSONClient{ Context: aws.Context{ Service: "animals", Region: "us-west-2", Credentials: aws.Creds( "accessKeyID", "secretAccessKey", "securityToken", ), }, Client: http.DefaultClient, Endpoint: server.URL, TargetPrefix: "Animals", JSONVersion: "1.1", } req := fakeJSONRequest{Name: "Penny"} var resp fakeJSONResponse err := client.Do("PetTheDog", "POST", "/", req, &resp) if err == nil { t.Fatal("Expected an error but none was returned") } if err, ok := err.(aws.APIError); ok { if v, want := err.Type, "Problem"; v != want { t.Errorf("Error type was %v, but expected %v", v, want) } if v, want := err.Message, "What even"; v != want { t.Errorf("Error message was %v, but expected %v", v, want) } } else { t.Errorf("Unknown error returned: %#v", err) } }
func createSubDomain(subDomain string, target string) { if utils.Cfg.AWSSettings.Route53AccessKeyId == "" { return } creds := aws.Creds(utils.Cfg.AWSSettings.Route53AccessKeyId, utils.Cfg.AWSSettings.Route53SecretAccessKey, "") r53 := route53.New(aws.DefaultConfig.Merge(&aws.Config{Credentials: creds, Region: utils.Cfg.AWSSettings.Route53Region})) rr := route53.ResourceRecord{ Value: aws.String(target), } rrs := make([]*route53.ResourceRecord, 1) rrs[0] = &rr change := route53.Change{ Action: aws.String("CREATE"), ResourceRecordSet: &route53.ResourceRecordSet{ Name: aws.String(fmt.Sprintf("%v.%v", subDomain, utils.Cfg.ServiceSettings.Domain)), TTL: aws.Long(300), Type: aws.String("CNAME"), ResourceRecords: rrs, }, } changes := make([]*route53.Change, 1) changes[0] = &change r53req := &route53.ChangeResourceRecordSetsInput{ HostedZoneID: aws.String(utils.Cfg.AWSSettings.Route53ZoneId), ChangeBatch: &route53.ChangeBatch{ Changes: changes, }, } if _, err := r53.ChangeResourceRecordSets(r53req); err != nil { l4g.Error("erro in createSubDomain domain=%v err=%v", subDomain, err) return } }
func main() { // files := map[string]*s3driver.MemoryFile{ // "/": &s3driver.MemoryFile{graval.NewDirItem(""), nil}, // } credentialsProvider := aws.Creds(awsAccessKeyID, awsSecretAccessKey, "") factory := &s3driver.S3DriverFactory{ Username: username, Password: password, AWSRegion: awsRegion, AWSCredentialsProvider: credentialsProvider, AWSBucketName: awsBucketName, } server := graval.NewFTPServer(&graval.FTPServerOpts{ ServerName: serverName, Factory: factory, Hostname: host, Port: port, PassiveOpts: &graval.PassiveOpts{ ListenAddress: host, NatAddress: host, PassivePorts: &graval.PassivePorts{ Low: 42000, High: 45000, }, }, }) log.Printf("FTP2S3 server listening on %s:%d", host, port) log.Printf("Access: ftp://%s:%s@%s:%d/", username, password, host, port) err := server.ListenAndServe() if err != nil { log.Fatal(err) } }
func TestQueryRequest(t *testing.T) { var m sync.Mutex var httpReq *http.Request var form url.Values server := httptest.NewServer(http.HandlerFunc( func(w http.ResponseWriter, r *http.Request) { m.Lock() defer m.Unlock() httpReq = r if err := r.ParseForm(); err != nil { t.Fatal(err) } form = r.Form fmt.Fprintln(w, `<Thing><IpAddress>woo</IpAddress></Thing>`) }, )) defer server.Close() client := aws.QueryClient{ Context: aws.Context{ Service: "animals", Region: "us-west-2", Credentials: aws.Creds( "accessKeyID", "secretAccessKey", "securityToken", ), }, Client: http.DefaultClient, Endpoint: server.URL, APIVersion: "1.1", } req := fakeQueryRequest{ PresentString: aws.String("string"), PresentBoolean: aws.True(), PresentInteger: aws.Integer(1), PresentLong: aws.Long(2), PresentDouble: aws.Double(1.2), PresentFloat: aws.Float(2.3), PresentTime: time.Date(2001, 1, 1, 2, 1, 1, 0, time.FixedZone("UTC+1", 3600)), PresentSlice: []string{"one", "two"}, PresentStruct: &EmbeddedStruct{Value: aws.String("v")}, PresentStructSlice: []EmbeddedStruct{ {Value: aws.String("p")}, {Value: aws.String("q")}, }, PresentMap: map[string]EmbeddedStruct{ "aa": EmbeddedStruct{Value: aws.String("AA")}, "bb": EmbeddedStruct{Value: aws.String("BB")}, }, } var resp fakeQueryResponse if err := client.Do("GetIP", "POST", "/", &req, &resp); err != nil { t.Fatal(err) } m.Lock() defer m.Unlock() if v, want := httpReq.Method, "POST"; v != want { t.Errorf("Method was %v but expected %v", v, want) } if httpReq.Header.Get("Authorization") == "" { t.Error("Authorization header is missing") } if v, want := httpReq.Header.Get("Content-Type"), "application/x-www-form-urlencoded"; v != want { t.Errorf("Content-Type was %v but expected %v", v, want) } if v, want := httpReq.Header.Get("User-Agent"), "aws-go"; v != want { t.Errorf("User-Agent was %v but expected %v", v, want) } if err := httpReq.ParseForm(); err != nil { t.Fatal(err) } expectedForm := url.Values{ "Action": []string{"GetIP"}, "Version": []string{"1.1"}, "PresentString": []string{"string"}, "PresentBoolean": []string{"true"}, "PresentInteger": []string{"1"}, "PresentLong": []string{"2"}, "PresentDouble": []string{"1.2"}, "PresentFloat": []string{"2.3"}, "PresentTime": []string{"2001-01-01T01:01:01Z"}, "PresentSlice.1": []string{"one"}, "PresentSlice.2": []string{"two"}, "PresentStruct.Value": []string{"v"}, "PresentStructSlice.1.Value": []string{"p"}, "PresentStructSlice.2.Value": []string{"q"}, "PresentMap.1.Name": []string{"aa"}, "PresentMap.1.Value.Value": []string{"AA"}, "PresentMap.2.Name": []string{"bb"}, "PresentMap.2.Value.Value": []string{"BB"}, } if !reflect.DeepEqual(form, expectedForm) { t.Errorf("Post body was \n%s\n but expected \n%s", form.Encode(), expectedForm.Encode()) } if want := (fakeQueryResponse{IPAddress: "woo"}); want != resp { t.Errorf("Response was %#v, but expected %#v", resp, want) } }
func TestQueryRequestError(t *testing.T) { var m sync.Mutex var httpReq *http.Request var form url.Values server := httptest.NewServer(http.HandlerFunc( func(w http.ResponseWriter, r *http.Request) { m.Lock() defer m.Unlock() httpReq = r if err := r.ParseForm(); err != nil { t.Fatal(err) } form = r.Form w.WriteHeader(400) fmt.Fprintln(w, `<ErrorResponse> <RequestId>woo</RequestId> <Error> <Type>Problem</Type> <Code>Uh Oh</Code> <Message>You done did it</Message> </Error> </ErrorResponse>`) }, )) defer server.Close() client := aws.QueryClient{ Context: aws.Context{ Service: "animals", Region: "us-west-2", Credentials: aws.Creds( "accessKeyID", "secretAccessKey", "securityToken", ), }, Client: http.DefaultClient, Endpoint: server.URL, APIVersion: "1.1", } req := fakeQueryRequest{} var resp fakeQueryResponse err := client.Do("GetIP", "POST", "/", &req, &resp) if err == nil { t.Fatal("Expected an error but none was returned") } if err, ok := err.(aws.APIError); ok { if v, want := err.Type, "Problem"; v != want { t.Errorf("Error type was %v, but expected %v", v, want) } if v, want := err.Code, "Uh Oh"; v != want { t.Errorf("Error type was %v, but expected %v", v, want) } if v, want := err.Message, "You done did it"; v != want { t.Errorf("Error message was %v, but expected %v", v, want) } } else { t.Errorf("Unknown error returned: %#v", err) } }
func (s3 *s3) newClient() *awsS3.S3 { return awsS3.New(&aws.Config{ Credentials: aws.Creds(s3.conf.AWS.AccessKeyID, s3.conf.AWS.SecretAccessKey, ""), Region: s3.conf.AWS.Region, }) }
func TestRestRequest(t *testing.T) { var m sync.Mutex var httpReq *http.Request server := httptest.NewServer(http.HandlerFunc( func(w http.ResponseWriter, r *http.Request) { m.Lock() defer m.Unlock() httpReq = r fmt.Fprintln(w, `woo`) }, )) defer server.Close() client := aws.RestClient{ Context: aws.Context{ Service: "animals", Region: "us-west-2", Credentials: aws.Creds( "accessKeyID", "secretAccessKey", "securityToken", ), }, Client: http.DefaultClient, } req, err := http.NewRequest("GET", server.URL+"/yay", nil) if err != nil { t.Fatal(err) } resp, err := client.Do(req) if err != nil { t.Fatal(err) } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { t.Fatal(err) } if v, want := string(body), "woo\n"; v != want { t.Errorf("Response entity was %q, but expected %q", v, want) } m.Lock() defer m.Unlock() if v, want := httpReq.Method, "GET"; v != want { t.Errorf("Method was %v but expected %v", v, want) } if httpReq.Header.Get("Authorization") == "" { t.Error("Authorization header is missing") } if v, want := httpReq.Header.Get("User-Agent"), "aws-go"; v != want { t.Errorf("User-Agent was %v but expected %v", v, want) } if v, want := httpReq.URL.String(), "/yay"; v != want { t.Errorf("URL was %v but expected %v", v, want) } }
func TestJSONRequest(t *testing.T) { var m sync.Mutex var httpReq *http.Request var body []byte server := httptest.NewServer(http.HandlerFunc( func(w http.ResponseWriter, r *http.Request) { m.Lock() defer m.Unlock() b, err := ioutil.ReadAll(r.Body) if err != nil { t.Fatal(err) } defer r.Body.Close() httpReq = r body = b fmt.Fprintln(w, `{"TailWagged":true}`) }, )) defer server.Close() client := aws.JSONClient{ Context: aws.Context{ Service: "animals", Region: "us-west-2", Credentials: aws.Creds( "accessKeyID", "secretAccessKey", "securityToken", ), }, Client: http.DefaultClient, Endpoint: server.URL, TargetPrefix: "Animals", JSONVersion: "1.1", } req := fakeJSONRequest{Name: "Penny"} var resp fakeJSONResponse if err := client.Do("PetTheDog", "POST", "/", req, &resp); err != nil { t.Fatal(err) } m.Lock() defer m.Unlock() if v, want := httpReq.Method, "POST"; v != want { t.Errorf("Method was %v but expected %v", v, want) } if httpReq.Header.Get("Authorization") == "" { t.Error("Authorization header is missing") } if v, want := httpReq.Header.Get("Content-Type"), "application/x-amz-json-1.1"; v != want { t.Errorf("Content-Type was %v but expected %v", v, want) } if v, want := httpReq.Header.Get("User-Agent"), "aws-go"; v != want { t.Errorf("User-Agent was %v but expected %v", v, want) } if v, want := httpReq.Header.Get("X-Amz-Target"), "Animals.PetTheDog"; v != want { t.Errorf("X-Amz-Target was %v but expected %v", v, want) } if v, want := string(body), `{"Name":"Penny"}`; v != want { t.Errorf("Body was %v but expected %v", v, want) } if v, want := resp, (fakeJSONResponse{TailWagged: true}); v != want { t.Errorf("Response was %#v but expected %#v", v, want) } }