github.com/richardbowden/terraform@v0.6.12-0.20160901200758-30ea22c25211/builtin/providers/chef/provider.go (about)

     1  package chef
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"io/ioutil"
     7  	"os"
     8  	"strings"
     9  	"time"
    10  
    11  	"github.com/hashicorp/terraform/helper/schema"
    12  	"github.com/hashicorp/terraform/terraform"
    13  
    14  	chefc "github.com/go-chef/chef"
    15  )
    16  
    17  func Provider() terraform.ResourceProvider {
    18  	return &schema.Provider{
    19  		Schema: map[string]*schema.Schema{
    20  			"server_url": &schema.Schema{
    21  				Type:        schema.TypeString,
    22  				Required:    true,
    23  				DefaultFunc: schema.EnvDefaultFunc("CHEF_SERVER_URL", nil),
    24  				Description: "URL of the root of the target Chef server or organization.",
    25  			},
    26  			"client_name": &schema.Schema{
    27  				Type:        schema.TypeString,
    28  				Required:    true,
    29  				DefaultFunc: schema.EnvDefaultFunc("CHEF_CLIENT_NAME", nil),
    30  				Description: "Name of a registered client within the Chef server.",
    31  			},
    32  			"private_key_pem": &schema.Schema{
    33  				Type:        schema.TypeString,
    34  				Required:    true,
    35  				DefaultFunc: providerPrivateKeyEnvDefault,
    36  				Description: "PEM-formatted private key for client authentication.",
    37  			},
    38  			"allow_unverified_ssl": &schema.Schema{
    39  				Type:        schema.TypeBool,
    40  				Optional:    true,
    41  				Description: "If set, the Chef client will permit unverifiable SSL certificates.",
    42  			},
    43  		},
    44  
    45  		ResourcesMap: map[string]*schema.Resource{
    46  			//"chef_acl":           resourceChefAcl(),
    47  			//"chef_client":        resourceChefClient(),
    48  			//"chef_cookbook":      resourceChefCookbook(),
    49  			"chef_data_bag":      resourceChefDataBag(),
    50  			"chef_data_bag_item": resourceChefDataBagItem(),
    51  			"chef_environment":   resourceChefEnvironment(),
    52  			"chef_node":          resourceChefNode(),
    53  			"chef_role":          resourceChefRole(),
    54  		},
    55  
    56  		ConfigureFunc: providerConfigure,
    57  	}
    58  }
    59  
    60  func providerConfigure(d *schema.ResourceData) (interface{}, error) {
    61  	config := &chefc.Config{
    62  		Name:    d.Get("client_name").(string),
    63  		Key:     d.Get("private_key_pem").(string),
    64  		BaseURL: d.Get("server_url").(string),
    65  		SkipSSL: d.Get("allow_unverified_ssl").(bool),
    66  		Timeout: 10 * time.Second,
    67  	}
    68  
    69  	return chefc.NewClient(config)
    70  }
    71  
    72  func providerPrivateKeyEnvDefault() (interface{}, error) {
    73  	if fn := os.Getenv("CHEF_PRIVATE_KEY_FILE"); fn != "" {
    74  		contents, err := ioutil.ReadFile(fn)
    75  		if err != nil {
    76  			return nil, err
    77  		}
    78  		return string(contents), nil
    79  	}
    80  
    81  	return nil, nil
    82  }
    83  
    84  func jsonStateFunc(value interface{}) string {
    85  	// Parse and re-stringify the JSON to make sure it's always kept
    86  	// in a normalized form.
    87  	in, ok := value.(string)
    88  	if !ok {
    89  		return "null"
    90  	}
    91  	var tmp map[string]interface{}
    92  
    93  	// Assuming the value must be valid JSON since it passed okay through
    94  	// our prepareDataBagItemContent function earlier.
    95  	json.Unmarshal([]byte(in), &tmp)
    96  
    97  	jsonValue, _ := json.Marshal(&tmp)
    98  	return string(jsonValue)
    99  }
   100  
   101  func runListEntryStateFunc(value interface{}) string {
   102  	// Recipes in run lists can either be naked, like "foo", or can
   103  	// be explicitly qualified as "recipe[foo]". Whichever form we use,
   104  	// the server will always normalize to the explicit form,
   105  	// so we'll normalize too and then we won't generate unnecessary
   106  	// diffs when we refresh.
   107  	in := value.(string)
   108  	if !strings.Contains(in, "[") {
   109  		return fmt.Sprintf("recipe[%s]", in)
   110  	}
   111  	return in
   112  }