Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
T
terraform-provider-one
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Redmine
Redmine
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Environments
Analytics
Analytics
CI / CD
Repository
Value Stream
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ims
terraform-provider-one
Commits
f5464b34
Commit
f5464b34
authored
Dec 01, 2017
by
Yann KAISER
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Changed the configuration creation code
parent
740919a8
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
98 additions
and
25 deletions
+98
-25
one/provider.go
one/provider.go
+38
-25
one/provider_test.go
one/provider_test.go
+60
-0
No files found.
one/provider.go
View file @
f5464b34
...
...
@@ -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
:
passw
or
d
,
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
}
one/provider_test.go
View file @
f5464b34
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
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment