func TestSwiftBackend(t *testing.T) { if os.Getenv("OS_USERNAME") == "" || os.Getenv("OS_PASSWORD") == "" || os.Getenv("OS_AUTH_URL") == "" { t.SkipNow() } username := os.Getenv("OS_USERNAME") password := os.Getenv("OS_PASSWORD") authUrl := os.Getenv("OS_AUTH_URL") tenant := os.Getenv("OS_TENANT_NAME") ts := time.Now().UnixNano() container := fmt.Sprintf("vault-test-%d", ts) cleaner := swift.Connection{ UserName: username, ApiKey: password, AuthUrl: authUrl, Tenant: tenant, Transport: cleanhttp.DefaultPooledTransport(), } err := cleaner.Authenticate() if err != nil { t.Fatalf("err: %s", err) } err = cleaner.ContainerCreate(container, nil) if nil != err { t.Fatalf("Unable to create test container '%s': %v", container, err) } defer func() { newObjects, err := cleaner.ObjectNamesAll(container, nil) if err != nil { t.Fatalf("err: %s", err) } for _, o := range newObjects { err := cleaner.ObjectDelete(container, o) if err != nil { t.Fatalf("err: %s", err) } } err = cleaner.ContainerDelete(container) if err != nil { t.Fatalf("err: %s", err) } }() logger := logformat.NewVaultLogger(log.LevelTrace) b, err := NewBackend("swift", logger, map[string]string{ "username": username, "password": password, "container": container, "auth_url": authUrl, "tenant": tenant, }) if err != nil { t.Fatalf("err: %s", err) } testBackend(t, b) testBackend_ListPrefix(t, b) }
func main() { // check for VCAP_SERVICES, parse it if exists var runport string var runhost string // var project string var domain string username := "******" password := "******" authurl := "http://127.0.0.1:12345/auth/v1.0" globalurl := "http://127.0.0.1:12345/auth/v1.0" if runport = os.Getenv("VCAP_APP_PORT"); len(runport) == 0 { runport = DEFAULT_PORT } if runhost = os.Getenv("VCAP_APP_HOST"); len(runhost) == 0 { runhost = DEFAULT_HOST } s := os.Getenv(CREDS_ENV) u := os.Getenv(UPLOAD_DISABLED) if s != "" { log.Printf("Found " + CREDS_ENV + " environment variable, parsing ....\n") objstorev2 := make(map[string]ObjStoreV2Info) err := json.Unmarshal([]byte(s), &objstorev2) if err != nil { log.Printf("Error parsing connection information: %v\n", err.Error()) panic(err) } info := objstorev2["CloudIntegration"] if &info == nil { log.Printf("No cloud integration services accessible to this application.\n") return } creds := info.Credentials authurl = info.Authurl + "/v3" username = creds.Userid password = creds.Password domain = info.Domain } else { log.Printf("No " + CREDS_ENV + ", using defaults.\n") } log.Printf("Using host %v+\n", runhost) log.Printf("Using port %v+\n", runport) log.Printf("Using authurl %v+\n", authurl) log.Printf("Using username %v+\n", username) log.Printf("Using password %v+\n", password) log.Printf("Using domain %v+\n", domain) log.Printf("Global URI is %v+\n", globalurl) m := martini.Classic() c := swift.Connection{ UserName: username, ApiKey: password, AuthUrl: authurl, Domain: domain, } // Authenticate err2 := c.Authenticate() if err2 != nil { log.Printf("authenticate error: ", err2) // panic(err2) } m.Use(render.Renderer(render.Options{ Directory: "tmpl", Layout: "layout", Charset: "UTF-8", Funcs: []template.FuncMap{ {"getPhotoNames": func() template.HTML { var result = "" var leadstr = "<div class=\"m-item " var endstr = "\"><img src=\"pic/" var firstcls = "m-active" var endtag = "\"></div>" files, err := c.ObjectNamesAll("myphotos", nil) if err != nil { panic(err) } for i, file := range files { result = result + leadstr if i == 1 { result = result + firstcls } result = result + endstr + file + endtag } log.Printf("result is %v", result) return template.HTML(result) }, }, }, })) if u != "" { m.Get("/upload", func(r render.Render) { fmt.Printf("%v\n", "g./upload") r.HTML(200, "uploaddisabled", "") }) } else { // completely disable uplaod m.Get("/upload", func(r render.Render) { fmt.Printf("%v\n", "g./upload") r.HTML(200, "upload", "") }) m.Post("/up", func(w http.ResponseWriter, r *http.Request) { fmt.Printf("%v\n", "p./up") file, header, err := r.FormFile("fileToUpload") defer file.Close() if err != nil { fmt.Fprintln(w, err) return } tmpFilename, err := readPhotoToTempfile(file) if err != nil { fmt.Fprint(w, err) return } err2 := sendTempfileToArchive(tmpFilename, header.Filename, c) if err2 != nil { fmt.Fprintln(w, err2) return } http.Redirect(w, r, "/", 302) }) } m.Get("/", func(r render.Render) { fmt.Printf("%v\n", "g./album") r.HTML(200, "album", "") }) m.Get("/pic/:who.jpg", func(args martini.Params, res http.ResponseWriter, req *http.Request) { _, err3 := c.ObjectGet(albumfolder, args["who"]+".jpg", res, false, nil) if err3 != nil { res.WriteHeader(500) } }) m.RunOnAddr(runhost + ":" + runport) }