func TestSessionGet(t *testing.T) { tokn := "eaaafd18-0fed-4b3a-81b4-663c99ec1cbb" var apiServer = testUtil.CreateGetJsonTestServer( t, tokn, `{"id":"id1","name":"Chris"}`, nil, ) expected := TestStruct{ID: "id1", Name: "Chris"} actual := TestStruct{} s, _ := openstack.NewSession(nil, nil, nil) var headers http.Header = http.Header{} headers.Set("X-Auth-Token", tokn) headers.Set("Accept", "application/json") headers.Set("Etag", "md5hash-blahblah") resp, err := s.Get(apiServer.URL, nil, &headers) if err != nil { t.Error(err) } testUtil.IsNil(t, err) rbody, err := ioutil.ReadAll(resp.Body) if err != nil { t.Error(err) } if err = json.Unmarshal(rbody, &actual); err != nil { t.Error(err) } testUtil.Equals(t, expected, actual) }
// Image examples. func main() { config := getConfig() // Authenticate with a username, password, tenant id. creds := openstack.AuthOpts{ AuthUrl: config.Host, Project: config.ProjectName, Username: config.Username, Password: config.Password, } auth, err := openstack.DoAuthRequest(creds) if err != nil { panicString := fmt.Sprint("There was an error authenticating:", err) panic(panicString) } if !auth.GetExpiration().After(time.Now()) { panic("There was an error. The auth token has an invalid expiration.") } // Find the endpoint for the image service. url, err := auth.GetEndpoint("image", "") if url == "" || err != nil { panic("v1 image service url not found during authentication") } // Make a new client with these creds sess, err := openstack.NewSession(nil, auth, nil) if err != nil { panicString := fmt.Sprint("Error crating new Session:", err) panic(panicString) } imageService := image.Service{ Session: *sess, Client: *http.DefaultClient, URL: url + "/v1", // We're forcing Image v1 for now } imagesDetails, err := imageService.ImagesDetail() if err != nil { panicString := fmt.Sprint("Cannot access images:", err) panic(panicString) } var imageIDs = make([]string, 0) for _, element := range imagesDetails { imageIDs = append(imageIDs, element.ID) } if len(imageIDs) == 0 { panicString := fmt.Sprint("No images found, check to make sure access is correct") panic(panicString) } }
// renewToken gets the keystone token from service AuthOpts func (validator *Validator) renewToken() error { // Get the service token to get the token revocation list auth, err := openstack.DoAuthRequest(validator.SvcAuthOpts) if err != nil { return err } // Make a new client with these creds serviceTokenSession, err = openstack.NewSession(nil, auth, nil) if err != nil { return err } return nil }
func testImageServiceAction(t *testing.T, uriEndsWith string, testData string, imageServiceAction func(*image.Service)) { anon := func(req *http.Request) { reqURL := req.URL.String() if !strings.HasSuffix(reqURL, uriEndsWith) { t.Error(errors.New("Incorrect url created, expected:" + uriEndsWith + " at the end, actual url:" + reqURL)) } } apiServer := testUtil.CreateGetJSONTestRequestServer(t, tokn, testData, anon) defer apiServer.Close() auth := openstack.AuthToken{ Access: openstack.AccessType{ Token: openstack.Token{ ID: tokn, }, }, } sess, _ := openstack.NewSession(http.DefaultClient, auth, nil) imageService := image.Service{ Session: *sess, URL: apiServer.URL, } imageServiceAction(&imageService) }