package util import ( "errors" "fmt" "os" "github.com/sirupsen/logrus" raxidentity "github.rackspace.com/gorax/clients/raxIdentity" "github.rackspace.com/gorax/tools/lagger" "github.rackspace.com/gorax/tools/passkey" ) type Init struct { PK *passkey.Client Env *raxidentity.IdentityAuth Application string } // InitAPIClients is intended to keep all init functions across NSI APIs consistent and // easily maintainable. Input is the app name (string) and a (bool) to init the passkey client. func InitAPIClients(app string, pkInit bool) (*Init, error) { if os.Getenv("VM") == "" { return nil, errors.New("Unable to get VM environment variable for hostname.") } // Default log level is set to INFO, if you pass a DEBUG environment var // to the app, we will set the log level to DEBUG. lvl := logrus.InfoLevel if os.Getenv("DEBUG") != "" { lvl = logrus.DebugLevel } // Create an instance of Init type. i := &Init{ Application: app, // raxidentity type to initialize the identity class. Env: &raxidentity.IdentityAuth{ Staging: IsStaging(os.Getenv("ENVIRONMENT")), Internal: true, }, } var err error i.Env.Log, err = lagger.NewLogger(os.Getenv("VM"), app, lvl) if err != nil { return nil, fmt.Errorf("Unable to initialize Logger: %v", err.Error()) } // If the invoking application requires credentials/API keys, we will populate // the PK sub type. if pkInit { // Use environment variable if present first user := os.Getenv("PASSKEY_USER") pwd := os.Getenv("PASSKEY_PW") if user != "" && pwd != "" { i.Env.Log.Info(i.Env.Log.AppMessage("Using environment variables to initialize passkey")) i.PK, err = passkey.New(user, pwd, i.Env.Staging, *i.Env.Log) if err != nil { return i, fmt.Errorf("Unable to initialize credentials: %v", err.Error()) } return i, nil } i.Env.Log.Info(i.Env.Log.AppMessage("Using file to initialize passkey")) i.PK, err = passkey.NewFromFile("netsec01", "", i.Env.Staging, *i.Env.Log) if err != nil { return i, fmt.Errorf("Unable to initialize credentials: %v", err.Error()) } return i, nil } return i, nil }