2015-08-27 01:50:12 +02:00
|
|
|
package chef
|
|
|
|
|
|
|
|
import (
|
2015-08-27 19:02:25 +02:00
|
|
|
"encoding/json"
|
2015-08-28 03:29:25 +02:00
|
|
|
"fmt"
|
2015-08-27 01:50:12 +02:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2015-08-28 03:29:25 +02:00
|
|
|
"strings"
|
2015-08-27 01:50:12 +02:00
|
|
|
"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{
|
2016-11-14 19:07:49 +01:00
|
|
|
"server_url": {
|
2015-08-27 01:50:12 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
DefaultFunc: schema.EnvDefaultFunc("CHEF_SERVER_URL", nil),
|
|
|
|
Description: "URL of the root of the target Chef server or organization.",
|
|
|
|
},
|
2016-11-14 19:07:49 +01:00
|
|
|
"client_name": {
|
2015-08-27 01:50:12 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
DefaultFunc: schema.EnvDefaultFunc("CHEF_CLIENT_NAME", nil),
|
|
|
|
Description: "Name of a registered client within the Chef server.",
|
|
|
|
},
|
2016-11-14 19:07:49 +01:00
|
|
|
"private_key_pem": {
|
2015-08-27 01:50:12 +02:00
|
|
|
Type: schema.TypeString,
|
2016-11-14 19:07:49 +01:00
|
|
|
Optional: true,
|
2015-08-27 01:50:12 +02:00
|
|
|
DefaultFunc: providerPrivateKeyEnvDefault,
|
2016-11-14 19:07:49 +01:00
|
|
|
Deprecated: "Please use key_material instead",
|
2015-08-27 01:50:12 +02:00
|
|
|
Description: "PEM-formatted private key for client authentication.",
|
|
|
|
},
|
2016-11-14 19:07:49 +01:00
|
|
|
"key_material": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
DefaultFunc: schema.EnvDefaultFunc("CHEF_KEY_MATERIAL", ""),
|
|
|
|
},
|
|
|
|
"allow_unverified_ssl": {
|
2015-08-27 01:50:12 +02:00
|
|
|
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(),
|
2015-08-27 02:37:09 +02:00
|
|
|
"chef_data_bag": resourceChefDataBag(),
|
2015-08-27 19:02:25 +02:00
|
|
|
"chef_data_bag_item": resourceChefDataBagItem(),
|
2015-08-28 03:04:22 +02:00
|
|
|
"chef_environment": resourceChefEnvironment(),
|
2015-08-28 03:54:52 +02:00
|
|
|
"chef_node": resourceChefNode(),
|
2015-08-28 03:29:25 +02:00
|
|
|
"chef_role": resourceChefRole(),
|
2015-08-27 01:50:12 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
ConfigureFunc: providerConfigure,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func providerConfigure(d *schema.ResourceData) (interface{}, error) {
|
|
|
|
config := &chefc.Config{
|
|
|
|
Name: d.Get("client_name").(string),
|
|
|
|
BaseURL: d.Get("server_url").(string),
|
|
|
|
SkipSSL: d.Get("allow_unverified_ssl").(bool),
|
|
|
|
Timeout: 10 * time.Second,
|
|
|
|
}
|
|
|
|
|
2016-11-14 19:07:49 +01:00
|
|
|
if v, ok := d.GetOk("private_key_pem"); ok {
|
|
|
|
config.Key = v.(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
if v, ok := d.GetOk("key_material"); ok {
|
|
|
|
config.Key = v.(string)
|
|
|
|
}
|
|
|
|
|
2015-08-27 01:50:12 +02:00
|
|
|
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
|
|
|
|
}
|
2015-08-27 19:02:25 +02:00
|
|
|
|
|
|
|
func jsonStateFunc(value interface{}) string {
|
|
|
|
// Parse and re-stringify the JSON to make sure it's always kept
|
|
|
|
// in a normalized form.
|
|
|
|
in, ok := value.(string)
|
|
|
|
if !ok {
|
|
|
|
return "null"
|
|
|
|
}
|
|
|
|
var tmp map[string]interface{}
|
|
|
|
|
|
|
|
// Assuming the value must be valid JSON since it passed okay through
|
|
|
|
// our prepareDataBagItemContent function earlier.
|
|
|
|
json.Unmarshal([]byte(in), &tmp)
|
|
|
|
|
|
|
|
jsonValue, _ := json.Marshal(&tmp)
|
|
|
|
return string(jsonValue)
|
|
|
|
}
|
2015-08-28 03:29:25 +02:00
|
|
|
|
|
|
|
func runListEntryStateFunc(value interface{}) string {
|
|
|
|
// Recipes in run lists can either be naked, like "foo", or can
|
|
|
|
// be explicitly qualified as "recipe[foo]". Whichever form we use,
|
|
|
|
// the server will always normalize to the explicit form,
|
|
|
|
// so we'll normalize too and then we won't generate unnecessary
|
|
|
|
// diffs when we refresh.
|
|
|
|
in := value.(string)
|
|
|
|
if !strings.Contains(in, "[") {
|
|
|
|
return fmt.Sprintf("recipe[%s]", in)
|
|
|
|
}
|
|
|
|
return in
|
|
|
|
}
|