Skip to content

Commit

Permalink
Merge pull request hashicorp#14681 from svanharmelen/f-review
Browse files Browse the repository at this point in the history
Use `helpers.shema.Provisoner` in Chef provisioner V2
  • Loading branch information
grubernaut authored May 30, 2017
2 parents ff3166f + 0e42273 commit 7894478
Show file tree
Hide file tree
Showing 16 changed files with 603 additions and 390 deletions.
5 changes: 1 addition & 4 deletions builtin/bins/provisioner-chef/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@ package main
import (
"github.com/hashicorp/terraform/builtin/provisioners/chef"
"github.com/hashicorp/terraform/plugin"
"github.com/hashicorp/terraform/terraform"
)

func main() {
plugin.Serve(&plugin.ServeOpts{
ProvisionerFunc: func() terraform.ResourceProvisioner {
return new(chef.ResourceProvisioner)
},
ProvisionerFunc: chef.Provisioner,
})
}
11 changes: 3 additions & 8 deletions builtin/provisioners/chef/linux_provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ const (
installURL = "https://www.chef.io/chef/install.sh"
)

func (p *Provisioner) linuxInstallChefClient(
o terraform.UIOutput,
comm communicator.Communicator) error {

func (p *provisioner) linuxInstallChefClient(o terraform.UIOutput, comm communicator.Communicator) error {
// Build up the command prefix
prefix := ""
if p.HTTPProxy != "" {
Expand All @@ -26,7 +23,7 @@ func (p *Provisioner) linuxInstallChefClient(
if p.HTTPSProxy != "" {
prefix += fmt.Sprintf("https_proxy='%s' ", p.HTTPSProxy)
}
if p.NOProxy != nil {
if len(p.NOProxy) > 0 {
prefix += fmt.Sprintf("no_proxy='%s' ", strings.Join(p.NOProxy, ","))
}

Expand All @@ -46,9 +43,7 @@ func (p *Provisioner) linuxInstallChefClient(
return p.runCommand(o, comm, fmt.Sprintf("%srm -f install.sh", prefix))
}

func (p *Provisioner) linuxCreateConfigFiles(
o terraform.UIOutput,
comm communicator.Communicator) error {
func (p *provisioner) linuxCreateConfigFiles(o terraform.UIOutput, comm communicator.Communicator) error {
// Make sure the config directory exists
if err := p.runCommand(o, comm, "mkdir -p "+linuxConfDir); err != nil {
return err
Expand Down
55 changes: 29 additions & 26 deletions builtin/provisioners/chef/linux_provisioner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,23 @@ import (
"testing"

"github.com/hashicorp/terraform/communicator"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
)

func TestResourceProvider_linuxInstallChefClient(t *testing.T) {
cases := map[string]struct {
Config *terraform.ResourceConfig
Config map[string]interface{}
Commands map[string]bool
}{
"Sudo": {
Config: testConfig(t, map[string]interface{}{
Config: map[string]interface{}{
"node_name": "nodename1",
"run_list": []interface{}{"cookbook::recipe"},
"server_url": "https://chef.local",
"user_name": "bob",
"user_key": "USER-KEY",
}),
},

Commands: map[string]bool{
"sudo curl -LO https://www.chef.io/chef/install.sh": true,
Expand All @@ -31,15 +32,15 @@ func TestResourceProvider_linuxInstallChefClient(t *testing.T) {
},

"NoSudo": {
Config: testConfig(t, map[string]interface{}{
Config: map[string]interface{}{
"node_name": "nodename1",
"prevent_sudo": true,
"run_list": []interface{}{"cookbook::recipe"},
"secret_key": "SECRET-KEY",
"server_url": "https://chef.local",
"user_name": "bob",
"user_key": "USER-KEY",
}),
},

Commands: map[string]bool{
"curl -LO https://www.chef.io/chef/install.sh": true,
Expand All @@ -49,15 +50,15 @@ func TestResourceProvider_linuxInstallChefClient(t *testing.T) {
},

"HTTPProxy": {
Config: testConfig(t, map[string]interface{}{
Config: map[string]interface{}{
"http_proxy": "http://proxy.local",
"node_name": "nodename1",
"prevent_sudo": true,
"run_list": []interface{}{"cookbook::recipe"},
"server_url": "https://chef.local",
"user_name": "bob",
"user_key": "USER-KEY",
}),
},

Commands: map[string]bool{
"http_proxy='http://proxy.local' curl -LO https://www.chef.io/chef/install.sh": true,
Expand All @@ -67,15 +68,15 @@ func TestResourceProvider_linuxInstallChefClient(t *testing.T) {
},

"HTTPSProxy": {
Config: testConfig(t, map[string]interface{}{
Config: map[string]interface{}{
"https_proxy": "https://proxy.local",
"node_name": "nodename1",
"prevent_sudo": true,
"run_list": []interface{}{"cookbook::recipe"},
"server_url": "https://chef.local",
"user_name": "bob",
"user_key": "USER-KEY",
}),
},

Commands: map[string]bool{
"https_proxy='https://proxy.local' curl -LO https://www.chef.io/chef/install.sh": true,
Expand All @@ -85,7 +86,7 @@ func TestResourceProvider_linuxInstallChefClient(t *testing.T) {
},

"NoProxy": {
Config: testConfig(t, map[string]interface{}{
Config: map[string]interface{}{
"http_proxy": "http://proxy.local",
"no_proxy": []interface{}{"http://local.local", "http://local.org"},
"node_name": "nodename1",
Expand All @@ -94,7 +95,7 @@ func TestResourceProvider_linuxInstallChefClient(t *testing.T) {
"server_url": "https://chef.local",
"user_name": "bob",
"user_key": "USER-KEY",
}),
},

Commands: map[string]bool{
"http_proxy='http://proxy.local' no_proxy='http://local.local,http://local.org' " +
Expand All @@ -107,15 +108,15 @@ func TestResourceProvider_linuxInstallChefClient(t *testing.T) {
},

"Version": {
Config: testConfig(t, map[string]interface{}{
Config: map[string]interface{}{
"node_name": "nodename1",
"prevent_sudo": true,
"run_list": []interface{}{"cookbook::recipe"},
"server_url": "https://chef.local",
"user_name": "bob",
"user_key": "USER-KEY",
"version": "11.18.6",
}),
},

Commands: map[string]bool{
"curl -LO https://www.chef.io/chef/install.sh": true,
Expand All @@ -125,14 +126,15 @@ func TestResourceProvider_linuxInstallChefClient(t *testing.T) {
},
}

r := new(ResourceProvisioner)
o := new(terraform.MockUIOutput)
c := new(communicator.MockCommunicator)

for k, tc := range cases {
c.Commands = tc.Commands

p, err := r.decodeConfig(tc.Config)
p, err := decodeConfig(
schema.TestResourceDataRaw(t, Provisioner().(*schema.Provisioner).Schema, tc.Config),
)
if err != nil {
t.Fatalf("Error: %v", err)
}
Expand All @@ -148,20 +150,20 @@ func TestResourceProvider_linuxInstallChefClient(t *testing.T) {

func TestResourceProvider_linuxCreateConfigFiles(t *testing.T) {
cases := map[string]struct {
Config *terraform.ResourceConfig
Config map[string]interface{}
Commands map[string]bool
Uploads map[string]string
}{
"Sudo": {
Config: testConfig(t, map[string]interface{}{
Config: map[string]interface{}{
"ohai_hints": []interface{}{"test-fixtures/ohaihint.json"},
"node_name": "nodename1",
"run_list": []interface{}{"cookbook::recipe"},
"secret_key": "SECRET-KEY",
"server_url": "https://chef.local",
"user_name": "bob",
"user_key": "USER-KEY",
}),
},

Commands: map[string]bool{
"sudo mkdir -p " + linuxConfDir: true,
Expand All @@ -188,15 +190,15 @@ func TestResourceProvider_linuxCreateConfigFiles(t *testing.T) {
},

"NoSudo": {
Config: testConfig(t, map[string]interface{}{
Config: map[string]interface{}{
"node_name": "nodename1",
"prevent_sudo": true,
"run_list": []interface{}{"cookbook::recipe"},
"secret_key": "SECRET-KEY",
"server_url": "https://chef.local",
"user_name": "bob",
"user_key": "USER-KEY",
}),
},

Commands: map[string]bool{
"mkdir -p " + linuxConfDir: true,
Expand All @@ -211,7 +213,7 @@ func TestResourceProvider_linuxCreateConfigFiles(t *testing.T) {
},

"Proxy": {
Config: testConfig(t, map[string]interface{}{
Config: map[string]interface{}{
"http_proxy": "http://proxy.local",
"https_proxy": "https://proxy.local",
"no_proxy": []interface{}{"http://local.local", "https://local.local"},
Expand All @@ -223,7 +225,7 @@ func TestResourceProvider_linuxCreateConfigFiles(t *testing.T) {
"ssl_verify_mode": "verify_none",
"user_name": "bob",
"user_key": "USER-KEY",
}),
},

Commands: map[string]bool{
"mkdir -p " + linuxConfDir: true,
Expand All @@ -238,7 +240,7 @@ func TestResourceProvider_linuxCreateConfigFiles(t *testing.T) {
},

"Attributes JSON": {
Config: testConfig(t, map[string]interface{}{
Config: map[string]interface{}{
"attributes_json": `{"key1":{"subkey1":{"subkey2a":["val1","val2","val3"],` +
`"subkey2b":{"subkey3":"value3"}}},"key2":"value2"}`,
"node_name": "nodename1",
Expand All @@ -248,7 +250,7 @@ func TestResourceProvider_linuxCreateConfigFiles(t *testing.T) {
"server_url": "https://chef.local",
"user_name": "bob",
"user_key": "USER-KEY",
}),
},

Commands: map[string]bool{
"mkdir -p " + linuxConfDir: true,
Expand All @@ -264,15 +266,16 @@ func TestResourceProvider_linuxCreateConfigFiles(t *testing.T) {
},
}

r := new(ResourceProvisioner)
o := new(terraform.MockUIOutput)
c := new(communicator.MockCommunicator)

for k, tc := range cases {
c.Commands = tc.Commands
c.Uploads = tc.Uploads

p, err := r.decodeConfig(tc.Config)
p, err := decodeConfig(
schema.TestResourceDataRaw(t, Provisioner().(*schema.Provisioner).Schema, tc.Config),
)
if err != nil {
t.Fatalf("Error: %v", err)
}
Expand Down
Loading

0 comments on commit 7894478

Please sign in to comment.