swift-get-nodes (Go remix)

I've been playing with golang for a little while now. I don't have a particular reason for doing so. It just seemed like a nice alternative to C/Java. So far thats mostly true, and I've had a lot fun picking it up.

Unfortunately, I suck at this whole programming thing. So the way I've been trying to learn Go is by rewriting things I'm familiar with. That means rewriting things like statsd (which I guess is like my de facto hello world app now), a swift-recon client, a very basic swift-proxy implementation, and this...a swift-get-nodes alternative.

In this particular instance @redbo had already done the heavy lifting by providing this bit of code that let me load the Swift ring:

https://gist.github.com/redbo/7592814

The only real addition I had to make was a function that lets you easily get the partition number for a provided swift path:

func GetPart(path string, conf *GetNodesConfig) (uint, []byte) {  
        hash := md5.New()
        target := conf.HashPrefix + path + conf.HashSuffix
        io.WriteString(hash, target)
        buf := bytes.NewBuffer([]byte(hash.Sum(nil)))
        var part uint32
        err := binary.Read(buf, binary.BigEndian, &part)
        if err != nil {
                panic(err)
        }
        return uint(part >> conf.PartShift), hash.Sum(nil)
}

That's basically the same as Swift's Ring().get_part() and hash_path() functions. The trickiest part there turned out to be remember to actually do the part shift.

Once that was working I thought I'd scratch a long time itch I've had and improve on the original swift-get-nodes a tiny bit. 90% of the time when I'm running a swift-get-nodes its to check whether something exists or how big something is. We might as well go ahead and check whether the account/container/object exists for the operator and dump the details.

Theres nothing special about that, I just grab the 3 hosts the acct/cont/obj should be on and fire off 3 head requests, I don't even use concurrency.

Anyway...as you might expect if you want to play with this you can:

go get github.com/pandemicsyn/go-swift-get-nodes  

Lastly, here's some sample output incase you don't want to bother trying it:

#Success
fhines@47.ronin.io:~$ go-swift-get-nodes /etc/swift/object.ring.gz /AUTH_f1317546-589c-4ab2-a136-bf6a658b19f6/omgmonkey/ff.jpg  
Ring File:    /etc/swift/object.ring.gz  
Target:        /AUTH_f1317546-589c-4ab2-a136-bf6a658b19f6/omgmonkey/ff.jpg  
Partion:    113446  
Hash:        6ec9aa9170d6858cc8d53a7b220bf168

Node 1: 200 OK  
Url: http://127.0.0.1:6010/sdb1/113446/AUTH_f1317546-589c-4ab2-a136-bf6a658b19f6/omgmonkey/ff.jpg  
 X-Timestamp: [1382728047.59973]
 Last-Modified: [Fri, 25 Oct 2013 19:07:27 GMT]
 Content-Length: [384845]
 Etag: ["1d17f9281079a3fb59593b4e76918d86"]
 Content-Type: [image/jpeg]
 Date: [Sat, 30 Nov 2013 06:16:20 GMT]

Node 2: 200 OK  
Url: http://127.0.0.1:6030/sdb3/113446/AUTH_f1317546-589c-4ab2-a136-bf6a658b19f6/omgmonkey/ff.jpg  
 X-Timestamp: [1382728047.59973]
 Last-Modified: [Fri, 25 Oct 2013 19:07:27 GMT]
 Content-Length: [384845]
 Etag: ["1d17f9281079a3fb59593b4e76918d86"]
 Content-Type: [image/jpeg]
 Date: [Sat, 30 Nov 2013 06:16:20 GMT]

Node 3: 200 OK  
Url: http://127.0.0.1:6020/sdb2/113446/AUTH_f1317546-589c-4ab2-a136-bf6a658b19f6/omgmonkey/ff.jpg  
 X-Timestamp: [1382728047.59973]
 Last-Modified: [Fri, 25 Oct 2013 19:07:27 GMT]
 Content-Length: [384845]
 Etag: ["1d17f9281079a3fb59593b4e76918d86"]
 Content-Type: [image/jpeg]
 Date: [Sat, 30 Nov 2013 06:16:20 GMT]
#Failure
fhines@47:~$ go-swift-get-nodes /etc/swift/object.ring.gz /AUTH_f1317546-589c-4ab2-a136-bf6a658b19f6/omgmonkey/cats.gif  
Ring File:    /etc/swift/object.ring.gz  
Target:        /AUTH_f1317546-589c-4ab2-a136-bf6a658b19f6/omgmonkey/cats.gif  
Partion:    202558  
Hash:        c5cf8f74abf7616c381a49578e1a0e8e

Node 1: 404 Not Found  
Url: http://127.0.0.1:6040/sdb4/202558/AUTH_f1317546-589c-4ab2-a136-bf6a658b19f6/omgmonkey/cats.gif  
 Content-Type: [text/html; charset=UTF-8]
 Content-Length: [0]
 Date: [Sat, 30 Nov 2013 06:16:32 GMT]

Node 2: 404 Not Found  
Url: http://127.0.0.1:6030/sdb3/202558/AUTH_f1317546-589c-4ab2-a136-bf6a658b19f6/omgmonkey/cats.gif  
 Content-Type: [text/html; charset=UTF-8]
 Content-Length: [0]
 Date: [Sat, 30 Nov 2013 06:16:32 GMT]

Node 3: 404 Not Found  
Url: http://127.0.0.1:6010/sdb1/202558/AUTH_f1317546-589c-4ab2-a136-bf6a658b19f6/omgmonkey/cats.gif  
 Content-Type: [text/html; charset=UTF-8]
 Content-Length: [0]
 Date: [Sat, 30 Nov 2013 06:16:32 GMT]
(╯°□°)╯ ~:74@sǝuıɥɟ$