Commit f5464b34 authored by Yann KAISER's avatar Yann KAISER

Changed the configuration creation code

parent 740919a8
......@@ -71,8 +71,8 @@ func Provider() *schema.Provider {
func providerConfigure(d *schema.ResourceData) (interface{}, error) {
// Setup configuration
var username string
var passwd string
var username = ""
var passwd = ""
usern, okUsername := d.GetOk("username")
if okUsername {
......@@ -85,27 +85,10 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
}
if !okUsername && !okPwd {
usr, err := user.Current()
if err != nil {
return nil, fmt.Errorf("Internal error: GO could not get the current OS user")
}
if _, err = os.Stat(usr.HomeDir + "/.one/one_auth"); os.IsNotExist(err) {
return nil, fmt.Errorf("You must create the auth file at ~/.one/one_auth to authenticate")
}
fileContent, err := ioutil.ReadFile(usr.HomeDir + "/.one/one_auth")
splittedContent, err := GetCredentials()
if err != nil {
return nil, err
}
splittedContent := strings.Split(string(fileContent), "\n")
splittedContent = strings.Split(splittedContent[0], ":")
if len(splittedContent) != 2 {
return nil, fmt.Errorf("Bad format for authentication file (~/.one/one_auth)")
}
username = splittedContent[0]
passwd = splittedContent[1]
}
......@@ -120,13 +103,43 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
vmTimeout = v.(int)
}
config := Config{
endpoint: d.Get("endpoint").(string),
config := CreateConfig(d.Get("endpoint").(string), username, passwd, oneflowHost, vmTimeout)
// Return new client
return config.Client()
}
func CreateConfig(endpoint string, username string, password string, oneflowHost string, vmTimeout int) Config {
return Config{
endpoint: endpoint,
username: username,
password: passwd,
password: password,
oneflowHost: oneflowHost,
vmTimeout: vmTimeout,
}
// Return new client
return config.Client()
}
func GetCredentials() ([]string, error) {
usr, err := user.Current()
if err != nil {
return nil, fmt.Errorf("Internal error: GO could not get the current OS user")
}
if _, err = os.Stat(usr.HomeDir + "/.one/one_auth"); os.IsNotExist(err) {
return nil, fmt.Errorf("You must create the auth file at ~/.one/one_auth to authenticate")
}
fileContent, err := ioutil.ReadFile(usr.HomeDir + "/.one/one_auth")
if err != nil {
return nil, err
}
splittedContent := strings.Split(string(fileContent), "\n")
splittedContent = strings.Split(splittedContent[0], ":")
if len(splittedContent) != 2 {
return nil, fmt.Errorf("Bad format for authentication file (~/.one/one_auth)")
}
return splittedContent, nil
}
package one
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"os/user"
"testing"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
)
type ConfigStruct struct {
endpoint string `JSON:"endpoint"`
username string `JSON:"username"`
password string `JSON:"password"`
oneflowHost string `JSON:"oneflow_host"`
vmTimeout int `JSON:"vm_timeout"`
}
var testAccProviders map[string]terraform.ResourceProvider
var testAccProvider *schema.Provider
......@@ -14,3 +29,48 @@ func init() {
"one": testAccProvider,
}
}
func testAccPreCheck(t *testing.T) {
usr, _ := user.Current()
if _, err := os.Stat(usr.HomeDir + "/.one/one_auth"); os.IsNotExist(err) {
t.Fatal("You must create the auth file at ~/.one/one_auth to authenticate")
}
}
func CreateConfigFromJSON(jsonFilePath string) (*Config, error) {
bytes, err := ioutil.ReadFile(jsonFilePath)
if err != nil {
return nil, err
}
var conf map[string]interface{}
credentials, err := GetCredentials()
if err := json.Unmarshal(bytes, &conf); err != nil {
return nil, fmt.Errorf("error while reading the file")
}
vmTimeout := conf["vm_timeout"].(float64)
var createdConfig = CreateConfig(conf["endpoint"].(string), credentials[0], credentials[1],
conf["oneflow_host"].(string), int(vmTimeout))
return &createdConfig, nil
}
func GenerateClientInfos(jsonFilePath string) (*Client, error) {
config, err := CreateConfigFromJSON(jsonFilePath)
if err != nil {
return nil, err
}
clientItf, err := config.Client()
if err != nil {
return nil, err
}
return clientItf.(*Client), err
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment