<commit_before>package app

import (
	"fmt"
	"github.com/timeredbull/keystone"
	"github.com/timeredbull/tsuru/config"
	"github.com/timeredbull/tsuru/db"
	"github.com/timeredbull/tsuru/log"
	"labix.org/v2/mgo/bson"
)

type KeystoneEnv struct {
	TenantId  string
	UserId    string
	AccessKey string
}

var (
	Client     *keystone.Client
	authUrl    string
	authUser   string
	authPass   string
	authTenant string
)

func getAuth() (err error) {
	if authUrl == "" {
		authUrl, err = config.GetString("nova:auth-url")
		if err != nil {
			log.Printf("ERROR: %s", err.Error())
			return
		}
	}
	if authUser == "" {
		authUser, err = config.GetString("nova:user")
		if err != nil {
			log.Printf("ERROR: %s", err.Error())
			return
		}
	}
	if authPass == "" {
		authPass, err = config.GetString("nova:password")
		if err != nil {
			log.Printf("ERROR: %s", err.Error())
			return
		}
	}
	if authTenant == "" {
		authTenant, err = config.GetString("nova:tenant")
		if err != nil {
			log.Printf("ERROR: %s", err.Error())
			return
		}
	}
	return
}

func NewTenant(a *App) (tId string, err error) {
	err = getAuth()
	Client, err = keystone.NewClient(authUser, authPass, authTenant, authUrl)
	if err != nil {
		return
	}
	desc := fmt.Sprintf("Tenant for %s", a.Name)
	log.Print(fmt.Sprintf("DEBUG: attempting to create tenant %s via keystone api...", a.Name))
	t, err := Client.NewTenant(a.Name, desc, true)
	if err != nil {
		log.Printf("ERROR: %s", err.Error())
		return
	}
	tId = t.Id
	a.KeystoneEnv.TenantId = tId
	err = db.Session.Apps().Update(bson.M{"name": a.Name}, &a)
	if err != nil {
		log.Printf("ERROR: %s", err.Error())
		return
	}
	log.Printf("DEBUG: tenant %s successfuly created.", a.Name)
	return
}
<commit_msg>api.app: documenting NewTenant function<commit_after>package app

import (
	"fmt"
	"github.com/timeredbull/keystone"
	"github.com/timeredbull/tsuru/config"
	"github.com/timeredbull/tsuru/db"
	"github.com/timeredbull/tsuru/log"
	"labix.org/v2/mgo/bson"
)

type KeystoneEnv struct {
	TenantId  string
	UserId    string
	AccessKey string
}

var (
	Client     *keystone.Client
	authUrl    string
	authUser   string
	authPass   string
	authTenant string
)

func getAuth() (err error) {
	if authUrl == "" {
		authUrl, err = config.GetString("nova:auth-url")
		if err != nil {
			log.Printf("ERROR: %s", err.Error())
			return
		}
	}
	if authUser == "" {
		authUser, err = config.GetString("nova:user")
		if err != nil {
			log.Printf("ERROR: %s", err.Error())
			return
		}
	}
	if authPass == "" {
		authPass, err = config.GetString("nova:password")
		if err != nil {
			log.Printf("ERROR: %s", err.Error())
			return
		}
	}
	if authTenant == "" {
		authTenant, err = config.GetString("nova:tenant")
		if err != nil {
			log.Printf("ERROR: %s", err.Error())
			return
		}
	}
	return
}

// Creates a tenant using keystone api
// and stores it in database embedded in
// the app document
// Returns the id of the created tenant in
// case of success and error in case of failure
func NewTenant(a *App) (tId string, err error) {
	err = getAuth()
	Client, err = keystone.NewClient(authUser, authPass, authTenant, authUrl)
	if err != nil {
		return
	}
	desc := fmt.Sprintf("Tenant for %s", a.Name)
	log.Print(fmt.Sprintf("DEBUG: attempting to create tenant %s via keystone api...", a.Name))
	t, err := Client.NewTenant(a.Name, desc, true)
	if err != nil {
		log.Printf("ERROR: %s", err.Error())
		return
	}
	tId = t.Id
	a.KeystoneEnv.TenantId = tId
	err = db.Session.Apps().Update(bson.M{"name": a.Name}, &a)
	if err != nil {
		log.Printf("ERROR: %s", err.Error())
		return
	}
	log.Printf("DEBUG: tenant %s successfuly created.", a.Name)
	return
}

{'fraction_non_alphanumeric': 0.1585999453103637, 'fraction_numerical': 0.0005468963631391851, 'mean_word_length': 3.455542021924482, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1733450', 'n_tokens_mistral': 1477, 'n_tokens_neox': 1414, 'n_words': 336}