// NewClient returns a new instance of the client, authenticating in the
// provided authUrl.
//
// For authentication, it uses the parameters username, password and tenantName
// to issue a request to the authUrl. The new generated token is stored in the
// Client instance, as is the service catalog.
NewClient returns a new instance of the client, authenticating in the
provided authUrl.

For authentication, it uses the parameters username, password and tenantName
to issue a request to the authUrl. The new generated token is stored in the
Client instance, as is the service catalog.
// NewClient returns a new instance of the client authenticating in the // provided authUrl . // // For authentication it uses the parameters username password and tenantName // to issue a request to the authUrl . The new generated token is stored in the // Client instance as is the service catalog .
func NewClient(username, password, tenantName, authUrl string) (*Client, error) {
	b := bytes.NewBufferString(fmt.Sprintf(`{"auth": {"passwordCredentials": {"username": "%s", "password":"%s"}, "tenantName": "%s"}}`, username, password, tenantName))
	response, err := http.Post(authUrl+"/tokens", "application/json", b)
	if err != nil {
		panic(err)
	}
	defer response.Body.Close()
	result, _ := ioutil.ReadAll(response.Body)
	var data map[string]map[string]interface{}
	err = json.Unmarshal(result, &data)
	if err != nil {
		return nil, err
	}
	if response.StatusCode > 399 {
		return nil, errors.New(data["error"]["title"].(string))
	}
	token := data["access"]["token"].(map[string]interface{})["id"].(string)
	client := Client{Token: token, authUrl: authUrl}
	catalogs, ok := data["access"]["serviceCatalog"].([]interface{})
	if !ok {
		return nil, errors.New("Error while accessing serviceCatalog key in returned json")
	}
	for _, c := range catalogs {
		catalog := c.(map[string]interface{})
		serviceCatalog := ServiceCatalog{
			Name: catalog["name"].(string),
			Type: catalog["type"].(string),
		}
		for _, e := range catalog["endpoints"].([]interface{}) {
			endpoint := map[string]string{}
			for k, v := range e.(map[string]interface{}) {
				endpoint[k] = v.(string)
			}
			serviceCatalog.Endpoints = append(serviceCatalog.Endpoints, endpoint)
		}
		client.Catalogs = append(client.Catalogs, serviceCatalog)
	}
	return &client, nil
}
func NewClient ( username , password , tenantName , authUrl string ) ( * Client , error ) { b := bytes . NewBufferString ( fmt . Sprintf ( `{"auth": {"passwordCredentials": {"username": "%s", "password":"%s"}, "tenantName": "%s"}}` , username , password , tenantName ) ) response , err := http . Post ( authUrl + " " , " " , b ) if err != nil { panic ( err ) } defer response . Body . Close ( ) result , _ := ioutil . ReadAll ( response . Body ) var data map [ string ] map [ string ] interface { } err = json . Unmarshal ( result , & data ) if err != nil { return nil , err } if response . StatusCode > 399 { return nil , errors . New ( data [ " " ] [ " " ] . ( string ) ) } token := data [ " " ] [ " " ] . ( map [ string ] interface { } ) [ " " ] . ( string ) client := Client { Token : token , authUrl : authUrl } catalogs , ok := data [ " " ] [ " " ] . ( [ ] interface { } ) if ! ok { return nil , errors . New ( " " ) } for _ , c := range catalogs { catalog := c . ( map [ string ] interface { } ) serviceCatalog := ServiceCatalog { Name : catalog [ " " ] . ( string ) , Type : catalog [ " " ] . ( string ) , } for _ , e := range catalog [ " " ] . ( [ ] interface { } ) { endpoint := map [ string ] string { } for k , v := range e . ( map [ string ] interface { } ) { endpoint [ k ] = v . ( string ) } serviceCatalog . Endpoints = append ( serviceCatalog . Endpoints , endpoint ) } client . Catalogs = append ( client . Catalogs , serviceCatalog ) } return & client , nil }
NewClient returns a new instance of the client, authenticating in the
provided authUrl.
NewClient returns a new instance of the client authenticating in the provided authUrl .