diff --git a/examples/10-objectstore.go b/examples/10-objectstore.go new file mode 100644 index 0000000..12a2484 --- /dev/null +++ b/examples/10-objectstore.go @@ -0,0 +1,180 @@ +package main + +import ( + "bytes" + "encoding/json" + "fmt" + "git.openstack.org/stackforge/golang-client.git/identity" + "git.openstack.org/stackforge/golang-client.git/objectstorage" + "io/ioutil" + "time" +) + +func main() { + config := getConfig() + + // Before working with object storage we need to authenticate with a project + // that has active object storage. + auth, err := identity.AuthUserNameTenantName(config.Host, + config.Username, + config.Password, + config.ProjectName) + if err != nil { + panicString := fmt.Sprint("There was an error authenticating:", err) + panic(panicString) + } + if !auth.Access.Token.Expires.After(time.Now()) { + panic("There was an error. The auth token has an invalid expiration.") + } + + // Find the endpoint for object storage. + url := "" + for _, svc := range auth.Access.ServiceCatalog { + if svc.Type == "object-store" { + url = svc.Endpoints[0].PublicURL + "/" + break + } + } + if url == "" { + panic("object-store url not found during authentication") + } + + hdr, err := objectstorage.GetAccountMeta(url, auth.Access.Token.Id) + if err != nil { + panicString := fmt.Sprint("There was an error getting account metadata:", err) + panic(panicString) + } + + // Create a new container. + if err = objectstorage.PutContainer(url+config.Container, auth.Access.Token.Id, + "X-Log-Retention", "true"); err != nil { + panicString := fmt.Sprint("PutContainer Error:", err) + panic(panicString) + } + + // Get a list of all the containers at the selected endoint. + containersJson, err := objectstorage.ListContainers(0, "", url, auth.Access.Token.Id) + if err != nil { + panic(err) + } + + type containerType struct { + Name string + Bytes, Count int + } + containersList := []containerType{} + + if err = json.Unmarshal(containersJson, &containersList); err != nil { + panic(err) + } + + found := false + for i := 0; i < len(containersList); i++ { + if containersList[i].Name == config.Container { + found = true + } + } + if !found { + panic("Created container is missing from downloaded containersList") + } + + // Set and Get container metadata. + if err = objectstorage.SetContainerMeta(url+config.Container, auth.Access.Token.Id, + "X-Container-Meta-fubar", "false"); err != nil { + panic(err) + } + + hdr, err = objectstorage.GetContainerMeta(url+config.Container, auth.Access.Token.Id) + if err != nil { + panicString := fmt.Sprint("GetContainerMeta Error:", err) + panic(panicString) + } + if hdr.Get("X-Container-Meta-fubar") != "false" { + panic("container meta does not match") + } + + // Create an object in a container. + var fContent []byte + srcFile := "10-objectstore.go" + fContent, err = ioutil.ReadFile(srcFile) + if err != nil { + panic(err) + } + + object := config.Container + "/" + srcFile + if err = objectstorage.PutObject(&fContent, url+object, auth.Access.Token.Id, + "X-Object-Meta-fubar", "false"); err != nil { + panic(err) + } + objectsJson, err := objectstorage.ListObjects(0, "", "", "", "", + url+config.Container, auth.Access.Token.Id) + + type objectType struct { + Name, Hash, Content_type, Last_modified string + Bytes int + } + objectsList := []objectType{} + + if err = json.Unmarshal(objectsJson, &objectsList); err != nil { + panic(err) + } + found = false + for i := 0; i < len(objectsList); i++ { + if objectsList[i].Name == srcFile { + found = true + } + } + if !found { + panic("created object is missing from the objectsList") + } + + // Manage object metadata + if err = objectstorage.SetObjectMeta(url+object, auth.Access.Token.Id, + "X-Object-Meta-fubar", "true"); err != nil { + panicString := fmt.Sprint("SetObjectMeta Error:", err) + panic(panicString) + } + hdr, err = objectstorage.GetObjectMeta(url+object, auth.Access.Token.Id) + if err != nil { + panicString := fmt.Sprint("GetObjectMeta Error:", err) + panic(panicString) + } + if hdr.Get("X-Object-Meta-fubar") != "true" { + panicString := fmt.Sprint("SetObjectMeta Error:", err) + panic(panicString) + } + + // Retrieve an object and check that it is the same as what as uploaded. + _, body, err := objectstorage.GetObject(url+object, auth.Access.Token.Id) + if err != nil { + panicString := fmt.Sprint("GetObject Error:", err) + panic(panicString) + } + if !bytes.Equal(fContent, body) { + panicString := fmt.Sprint("GetObject Error:", "byte comparison of uploaded != downloaded") + panic(panicString) + } + + // Duplication (Copy) an existing object. + if err = objectstorage.CopyObject(url+object, "/"+object+".dup", auth.Access.Token.Id); err != nil { + panicString := fmt.Sprint("CopyObject Error:", err) + panic(panicString) + } + + // Delete the objects. + if err = objectstorage.DeleteObject(url+object, auth.Access.Token.Id); err != nil { + panicString := fmt.Sprint("DeleteObject Error:", err) + panic(panicString) + } + if err = objectstorage.DeleteObject(url+object+".dup", auth.Access.Token.Id); err != nil { + panicString := fmt.Sprint("DeleteObject Error:", err) + panic(panicString) + } + + // Delete the container that was previously created. + if err = objectstorage.DeleteContainer(url+config.Container, + auth.Access.Token.Id); err != nil { + panicString := fmt.Sprint("DeleteContainer Error:", err) + panic(panicString) + } +} diff --git a/examples/config.json.dist b/examples/config.json.dist index 1e253b1..a5619b1 100644 --- a/examples/config.json.dist +++ b/examples/config.json.dist @@ -3,5 +3,6 @@ "Username": "", "Password": "", "ProjectID": "", - "ProjectName": "" + "ProjectName": "", + "Container": "I♡HPHelion" } \ No newline at end of file diff --git a/examples/setup.go b/examples/setup.go index 7a3e6d9..fdf2ffd 100644 --- a/examples/setup.go +++ b/examples/setup.go @@ -16,6 +16,7 @@ type testconfig struct { Password string ProjectID string ProjectName string + Container string } // getConfig provides access to credentials in other tests and examples. diff --git a/objectstorage/objectstorage_integration_test.go b/objectstorage/objectstorage_integration_test.go deleted file mode 100644 index 713ad8b..0000000 --- a/objectstorage/objectstorage_integration_test.go +++ /dev/null @@ -1,159 +0,0 @@ -package objectstorage_test - -import ( - "bytes" - "encoding/json" - "git.openstack.org/stackforge/golang-client.git/identity" - "git.openstack.org/stackforge/golang-client.git/identity/identitytest" - "git.openstack.org/stackforge/golang-client.git/objectstorage" - "io/ioutil" - "testing" -) - -//PRE-REQUISITE: Must have valid ObjectStorage account, either internally -//hosted or with one of the OpenStack providers. Identity is assumed to -//use IdentityService mechanism, instead of legacy Swift mechanism. -func TestEndToEnd(t *testing.T) { - //user.json holds the user account info needed to authenticate - account := identitytest.SetupUser("../identity/identitytest/user.json") - auth, err := identity.AuthUserNameTenantId(account.Host, - account.UserName, - account.Password, - account.TenantId) - if err != nil { - t.Fatal(err) - } - - url := "" - for _, svc := range auth.Access.ServiceCatalog { - if svc.Type == "object-store" { - url = svc.Endpoints[0].PublicURL + "/" - break - } - } - if url == "" { - t.Fatal("object-store url not found during authentication") - } - - hdr, err := objectstorage.GetAccountMeta(url, auth.Access.Token.Id) - if err != nil { - t.Error("\nGetAccountMeta error\n", err) - } - - container := "testContainer1" - if err = objectstorage.PutContainer(url+container, auth.Access.Token.Id, - "X-Log-Retention", "true"); err != nil { - t.Fatal("\nPutContainer\n", err) - } - - containersJson, err := objectstorage.ListContainers(0, "", - url, auth.Access.Token.Id) - if err != nil { - t.Fatal(err) - } - - type containerType struct { - Name string - Bytes, Count int - } - containersList := []containerType{} - - if err = json.Unmarshal(containersJson, &containersList); err != nil { - t.Error(err) - } - - found := false - for i := 0; i < len(containersList); i++ { - if containersList[i].Name == container { - found = true - } - } - if !found { - t.Fatal("created container is missing from downloaded containersList") - } - - if err = objectstorage.SetContainerMeta(url+container, auth.Access.Token.Id, - "X-Container-Meta-fubar", "false"); err != nil { - t.Error(err) - } - hdr, err = objectstorage.GetContainerMeta(url+container, auth.Access.Token.Id) - if err != nil { - t.Error("\nGetContainerMeta error\n", err) - } - if hdr.Get("X-Container-Meta-fubar") != "false" { - t.Error("container meta does not match") - } - - var fContent []byte - srcFile := "objectstorage_integration_test.go" - fContent, err = ioutil.ReadFile(srcFile) - if err != nil { - t.Fatal(err) - } - - object := container + "/" + srcFile - if err = objectstorage.PutObject(&fContent, url+object, auth.Access.Token.Id, - "X-Object-Meta-fubar", "false"); err != nil { - t.Fatal(err) - } - objectsJson, err := objectstorage.ListObjects(0, "", "", "", "", - url+container, auth.Access.Token.Id) - - type objectType struct { - Name, Hash, Content_type, Last_modified string - Bytes int - } - objectsList := []objectType{} - - if err = json.Unmarshal(objectsJson, &objectsList); err != nil { - t.Error(err) - } - found = false - for i := 0; i < len(objectsList); i++ { - if objectsList[i].Name == srcFile { - found = true - } - } - if !found { - t.Fatal("created object is missing from the objectsList") - } - - if err = objectstorage.SetObjectMeta(url+object, auth.Access.Token.Id, - "X-Object-Meta-fubar", "true"); err != nil { - t.Error("\nSetObjectMeta error\n", err) - } - hdr, err = objectstorage.GetObjectMeta(url+object, auth.Access.Token.Id) - if err != nil { - t.Error("\nGetObjectMeta error\n", err) - } - if hdr.Get("X-Object-Meta-fubar") != "true" { - t.Error("\nSetObjectMeta error\n", "object meta does not match") - } - - _, body, err := objectstorage.GetObject(url+object, auth.Access.Token.Id) - if err != nil { - t.Error("\nGetObject error\n", err) - } - if !bytes.Equal(fContent, body) { - t.Error("\nGetObject error\n", "byte comparison of uploaded != downloaded") - } - - if err = objectstorage.CopyObject(url+object, "/"+object+".dup", - auth.Access.Token.Id); err != nil { - t.Fatal("\nCopyObject error\n", err) - } - - if err = objectstorage.DeleteObject(url+object, - auth.Access.Token.Id); err != nil { - t.Fatal("\nDeleteObject error\n", err) - } - if err = objectstorage.DeleteObject(url+object+".dup", - auth.Access.Token.Id); err != nil { - t.Fatal("\nDeleteObject error\n", err) - } - - if err = objectstorage.DeleteContainer(url+container, - auth.Access.Token.Id); err != nil { - t.Error("\nDeleteContainer error\n", err) - } -}