Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to Go 1.0 and new build system #1

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 6 additions & 23 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,25 +1,8 @@
include $(GOROOT)/src/Make.inc
all:
go build ./...

TARG=aws
GOFILES=\
consts.go\
dialer.go\
http_dialer.go\
signer.go\
escape.go\
timeformats.go\
clean:
go clean ./...

include $(GOROOT)/src/Make.pkg

module.%: %
gomake -C $*

module_install.%: module.%
gomake -C $* install

module_clean.%:
gomake -C $* clean

modules: module.sqs module.s3 module.sdb module.ec2 module.elb
modules_install: module_install.sqs module_install.s3 module_install.sdb module_install.ec2 module_install.elb
modules_clean: module_clean.sqs module_clean.s3 module_clean.sdb module_clean.elb module_clean.ec2
install:
go install ./...
7 changes: 4 additions & 3 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ paths required for the command line tool.
fully capable of basic administration of ELB, S3, SQS, and SimpleDB resources, but
are intended primarily for testing and API usage examples.

This package should build/goinstall on its own, but to make use of the sub-modules,
you will need to run 'make modules modules_install'. (If anyone can submit a patch
to allow goinstall to do the entire project, it will be gladly accepted!)
Installation
============

go get github.com/abneptis/GoAWS

Bugs/Notes
==========
Expand Down
15 changes: 7 additions & 8 deletions consts.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
package aws

import (
"os"
"http"
"errors"
"net/http"
)

const (
DEFAULT_SIGNATURE_VERSION = "2"
DEFAULT_SIGNATURE_METHOD = "HmacSHA256"
)

var ErrorNotFound = errors.New("Not found")
var ErrorUnexpectedResponse = errors.New("Unexpected response code")
var ErrorConflicts = errors.New("Conflicts with another resources")
var ErrorForbidden = errors.New("Access denied")

var ErrorNotFound os.Error = os.NewError("Not found")
var ErrorUnexpectedResponse os.Error = os.NewError("Unexpected response code")
var ErrorConflicts os.Error = os.NewError("Conflicts with another resources")
var ErrorForbidden os.Error = os.NewError("Access denied")

func CodeToError(i int) (err os.Error) {
func CodeToError(i int) (err error) {
switch i {
case http.StatusOK:
case http.StatusNotFound:
Expand Down
90 changes: 41 additions & 49 deletions dialer.go
Original file line number Diff line number Diff line change
@@ -1,80 +1,75 @@
package aws

import (
"errors"
"net"
"os"
"sync"
"time"
// "log"
)

// Dev notes: lower-case (private) functions assume the lock is held,
// upper-case functions should use a defer lock.Unlock to ensure
// underlying dialer/socket panics will not leave locks hanging.

var ErrUnderlyingNotconnected = os.NewError("Underlying socket is not connected")
var ErrUnderlyingNotconnected = errors.New("Underlying socket is not connected")

// A Dialer is usually a closuer that
// is pre-configured to the callers tastes.
//
// (see URLDialer for an example/default generator)
type Dialer func() (net.Conn, os.Error)
type Dialer func() (net.Conn, error)

// A Reusable conn is a syncronized structure around a
// Dialer / net.Conn pair. All net.Conn calls are wrapped
// around the underlying structure. Errors are bubbled
// up, and trigger closure of the underlying socket (to
// be reopened on the next call)
type ReusableConn struct {
lock *sync.Mutex
dialer Dialer
conn net.Conn
readTimeout int64
writeTimeout int64
lock *sync.Mutex
dialer Dialer
conn net.Conn
readDeadline time.Time
writeDeadline time.Time
}

const (
_UNSET_TIMEOUT int64 = -1
)

// Create a new reusable connection with a sepcific dialer.
func NewReusableConnection(d Dialer) (c *ReusableConn) {
return &ReusableConn{
dialer: d,
conn: nil,
lock: &sync.Mutex{},
readTimeout: _UNSET_TIMEOUT,
writeTimeout: _UNSET_TIMEOUT,
dialer: d,
conn: nil,
lock: &sync.Mutex{},
}
}

// Dial is idempotent, and safe to call;
func (self *ReusableConn) Dial() (err os.Error) {
func (self *ReusableConn) Dial() (err error) {
// log.Printf("Public Dial() called")
self.lock.Lock()
defer self.lock.Unlock()
return self.dial()
}

// Dial will redial if conn is nil, and set
// timeouts if they've been set by the caller.
// deadlines if they've been set by the caller.
//
// It simply returns nil if the socket appears already connected
func (self *ReusableConn) dial() (err os.Error) {
func (self *ReusableConn) dial() (err error) {
// log.Printf("Private dial() called (%v)", self.conn)
if self.conn == nil {
self.conn, err = self.dialer()
if err == nil && self.readTimeout != _UNSET_TIMEOUT {
err = self.setReadTimeout(self.readTimeout)
if err == nil && !self.readDeadline.IsZero() {
err = self.setReadDeadline(self.readDeadline)
}
if err == nil && self.writeTimeout != _UNSET_TIMEOUT {
err = self.setWriteTimeout(self.writeTimeout)
if err == nil && !self.writeDeadline.IsZero() {
err = self.setWriteDeadline(self.writeDeadline)
}
}
// log.Printf("Private dial() complete (%v)", self.conn)
return
}

func (self *ReusableConn) close() (err os.Error) {
func (self *ReusableConn) close() (err error) {
if self.conn != nil {
err = self.conn.Close()
self.conn = nil
Expand All @@ -84,7 +79,7 @@ func (self *ReusableConn) close() (err os.Error) {

// Unlike close on a traditional socket, no error
// is raised if you close a closed (nil) connection.
func (self *ReusableConn) Close() (err os.Error) {
func (self *ReusableConn) Close() (err error) {
self.lock.Lock()
defer self.lock.Unlock()
return self.close()
Expand All @@ -111,7 +106,7 @@ func (self *ReusableConn) LocalAddr() (a net.Addr) {
return
}

func (self *ReusableConn) read(in []byte) (n int, err os.Error) {
func (self *ReusableConn) read(in []byte) (n int, err error) {
err = self.dial()
if err == nil {
n, err = self.conn.Read(in)
Expand All @@ -122,7 +117,7 @@ func (self *ReusableConn) read(in []byte) (n int, err os.Error) {
return
}

func (self *ReusableConn) write(in []byte) (n int, err os.Error) {
func (self *ReusableConn) write(in []byte) (n int, err error) {
err = self.dial()
if err == nil {
n, err = self.conn.Write(in)
Expand All @@ -133,68 +128,65 @@ func (self *ReusableConn) write(in []byte) (n int, err os.Error) {
return
}


// Read from the underlying connection, triggering a dial if needed.
// NB: For the expected case (HTTP), this shouldn't happen before the
// first Write.
func (self *ReusableConn) Read(in []byte) (n int, err os.Error) {
func (self *ReusableConn) Read(in []byte) (n int, err error) {
self.lock.Lock()
defer self.lock.Unlock()
return self.read(in)
}

// Write to the underlying connection, triggering a dial if needed.
func (self *ReusableConn) Write(out []byte) (n int, err os.Error) {
func (self *ReusableConn) Write(out []byte) (n int, err error) {
self.lock.Lock()
defer self.lock.Unlock()
return self.write(out)
}


func (self *ReusableConn) setReadTimeout(t int64) (err os.Error) {
func (self *ReusableConn) setReadDeadline(t time.Time) (err error) {
err = self.dial()
if err == nil {
err = self.conn.SetReadTimeout(t)
err = self.conn.SetReadDeadline(t)
if err == nil {
self.readTimeout = t
self.readDeadline = t
}
}
return
}

func (self *ReusableConn) setWriteTimeout(t int64) (err os.Error) {
func (self *ReusableConn) setWriteDeadline(t time.Time) (err error) {
err = self.dial()
if err == nil {
err = self.conn.SetWriteTimeout(t)
err = self.conn.SetWriteDeadline(t)
if err == nil {
self.writeTimeout = t
self.writeDeadline = t
}
}
return
}


// Sets the read timeout on the underlying socket, as well
// Sets the read deadline on the underlying socket, as well
// as an internal flag for any future re-opened connections.
func (self *ReusableConn) SetReadTimeout(t int64) (err os.Error) {
func (self *ReusableConn) SetReadDeadline(t time.Time) (err error) {
self.lock.Lock()
defer self.lock.Unlock()
return self.setReadTimeout(t)
return self.setReadDeadline(t)
}

// Sets the write timeout on the underlying socket, as well
// Sets the write deadline on the underlying socket, as well
// as an internal flag for any future re-opened connections.
func (self *ReusableConn) SetWriteTimeout(t int64) (err os.Error) {
func (self *ReusableConn) SetWriteDeadline(t time.Time) (err error) {
self.lock.Lock()
defer self.lock.Unlock()
return self.setWriteTimeout(t)
return self.setWriteDeadline(t)
}

// Conveinience function for Set(read|write)timeout
func (self *ReusableConn) SetTimeout(t int64) (err os.Error) {
err = self.SetReadTimeout(t)
// Convenience function for Set(Read|Write)Deadline
func (self *ReusableConn) SetDeadline(t time.Time) (err error) {
err = self.SetReadDeadline(t)
if err == nil {
err = self.SetWriteTimeout(t)
err = self.SetWriteDeadline(t)
}
return
}
16 changes: 0 additions & 16 deletions ec2/Makefile

This file was deleted.

5 changes: 2 additions & 3 deletions ec2/instance.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package ec2


import (
"xml"
"encoding/xml"
)

/*
The XML package doesn't do deeply-nested types with reflection well,
so much of the functionality provided is simply hacks
Expand All @@ -27,7 +27,6 @@ type ReservationSet struct {
Instances []Instance "instancesSet>item"
}


// At the level of depth we're at, XML's
// not happy with us
type Instance struct {
Expand Down
25 changes: 13 additions & 12 deletions ec2/service.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
package ec2

import (
"aws"
"github.com/abneptis/GoAWS"
"net/url"
)

import (
"http"
"encoding/xml"
"log"
"net/http/httputil"
"os"
"xml"
)

type Service struct {
conn *aws.Conn
URL *http.URL
URL *url.URL
}

func NewService(url *http.URL) (s *Service) {
func NewService(url_ *url.URL) (s *Service) {
return &Service{
URL: url,
conn: aws.NewConn(aws.URLDialer(url, nil)),
URL: url_,
conn: aws.NewConn(aws.URLDialer(url_, nil)),
}
}

func (self *Service) DescribeInstances(id *aws.Signer, filter http.Values, ic chan Instance) (err os.Error) {
func (self *Service) DescribeInstances(id *aws.Signer, filter url.Values, ic chan Instance) (err error) {
if filter == nil {
filter = http.Values{}
filter = url.Values{}
}
filter.Set("Action", "DescribeInstances")
req := aws.NewRequest(self.URL, "GET", nil, filter)
Expand All @@ -37,20 +38,20 @@ func (self *Service) DescribeInstances(id *aws.Signer, filter http.Values, ic ch
if err == nil {
defer resp.Body.Close()
xresp := describeInstancesResponse{}
err := xml.Unmarshal(resp.Body, &xresp)
err := xml.NewDecoder(resp.Body).Decode(&xresp)
if err == nil {
log.Printf("XRESP == %+v", xresp)
} else {
log.Printf("XERR == %+v", err)
}
ob, _ := http.DumpResponse(resp, true)
ob, _ := httputil.DumpResponse(resp, true)
os.Stdout.Write(ob)
}

return
}

// Closes the underlying connection
func (self *Service) Close() (err os.Error) {
func (self *Service) Close() (err error) {
return self.conn.Close()
}
Loading