Entry point for chef provider.

This commit is contained in:
Martin Atkins 2015-08-26 16:50:12 -07:00
parent e9d152d586
commit 53aa3fb049
3 changed files with 153 additions and 0 deletions

View File

@ -0,0 +1,12 @@
package main
import (
"github.com/hashicorp/terraform/builtin/providers/chef"
"github.com/hashicorp/terraform/plugin"
)
func main() {
plugin.Serve(&plugin.ServeOpts{
ProviderFunc: chef.Provider,
})
}

View File

@ -0,0 +1,79 @@
package chef
import (
"io/ioutil"
"os"
"time"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
chefc "github.com/go-chef/chef"
)
func Provider() terraform.ResourceProvider {
return &schema.Provider{
Schema: map[string]*schema.Schema{
"server_url": &schema.Schema{
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("CHEF_SERVER_URL", nil),
Description: "URL of the root of the target Chef server or organization.",
},
"client_name": &schema.Schema{
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("CHEF_CLIENT_NAME", nil),
Description: "Name of a registered client within the Chef server.",
},
"private_key_pem": &schema.Schema{
Type: schema.TypeString,
Required: true,
DefaultFunc: providerPrivateKeyEnvDefault,
Description: "PEM-formatted private key for client authentication.",
},
"allow_unverified_ssl": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Description: "If set, the Chef client will permit unverifiable SSL certificates.",
},
},
ResourcesMap: map[string]*schema.Resource{
//"chef_acl": resourceChefAcl(),
//"chef_client": resourceChefClient(),
//"chef_cookbook": resourceChefCookbook(),
//"chef_data_bag": resourceChefDataBag(),
//"chef_data_bag_item": resourceChefDataBagItem(),
//"chef_environment": resourceChefEnvironment(),
//"chef_node": resourceChefNode(),
//"chef_role": resourceChefRole(),
},
ConfigureFunc: providerConfigure,
}
}
func providerConfigure(d *schema.ResourceData) (interface{}, error) {
config := &chefc.Config{
Name: d.Get("client_name").(string),
Key: d.Get("private_key_pem").(string),
BaseURL: d.Get("server_url").(string),
SkipSSL: d.Get("allow_unverified_ssl").(bool),
Timeout: 10 * time.Second,
}
return chefc.NewClient(config)
}
func providerPrivateKeyEnvDefault() (interface{}, error) {
if fn := os.Getenv("CHEF_PRIVATE_KEY_FILE"); fn != "" {
contents, err := ioutil.ReadFile(fn)
if err != nil {
return nil, err
}
return string(contents), nil
}
return nil, nil
}

View File

@ -0,0 +1,62 @@
package chef
import (
"os"
"testing"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
)
// To run these acceptance tests, you will need access to a Chef server.
// An easy way to get one is to sign up for a hosted Chef server account
// at https://manage.chef.io/signup , after which your base URL will
// be something like https://api.opscode.com/organizations/example/ .
// You will also need to create a "client" and write its private key to
// a file somewhere.
//
// You can then set the following environment variables to make these
// tests work:
// CHEF_SERVER_URL to the base URL as described above.
// CHEF_CLIENT_NAME to the name of the client object you created.
// CHEF_PRIVATE_KEY_FILE to the path to the private key file you created.
//
// You will probably need to edit the global permissions on your Chef
// Server account to allow this client (or all clients, if you're lazy)
// to have both List and Create access on all types of object:
// https://manage.chef.io/organizations/saymedia/global_permissions
//
// With all of that done, you can run like this:
// make testacc TEST=./builtin/providers/chef
var testAccProviders map[string]terraform.ResourceProvider
var testAccProvider *schema.Provider
func init() {
testAccProvider = Provider().(*schema.Provider)
testAccProviders = map[string]terraform.ResourceProvider{
"chef": testAccProvider,
}
}
func TestProvider(t *testing.T) {
if err := Provider().(*schema.Provider).InternalValidate(); err != nil {
t.Fatalf("err: %s", err)
}
}
func TestProvider_impl(t *testing.T) {
var _ terraform.ResourceProvider = Provider()
}
func testAccPreCheck(t *testing.T) {
if v := os.Getenv("CHEF_SERVER_URL"); v == "" {
t.Fatal("CHEF_SERVER_URL must be set for acceptance tests")
}
if v := os.Getenv("CHEF_CLIENT_NAME"); v == "" {
t.Fatal("CHEF_CLIENT_NAME must be set for acceptance tests")
}
if v := os.Getenv("CHEF_PRIVATE_KEY_FILE"); v == "" {
t.Fatal("CHEF_PRIVATE_KEY_FILE must be set for acceptance tests")
}
}