github.com/jrasell/terraform@v0.6.17-0.20160523115548-2652f5232949/builtin/providers/vsphere/provider.go (about)

     1  package vsphere
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/hashicorp/terraform/helper/schema"
     7  	"github.com/hashicorp/terraform/terraform"
     8  )
     9  
    10  // Provider returns a terraform.ResourceProvider.
    11  func Provider() terraform.ResourceProvider {
    12  	return &schema.Provider{
    13  		Schema: map[string]*schema.Schema{
    14  			"user": &schema.Schema{
    15  				Type:        schema.TypeString,
    16  				Required:    true,
    17  				DefaultFunc: schema.EnvDefaultFunc("VSPHERE_USER", nil),
    18  				Description: "The user name for vSphere API operations.",
    19  			},
    20  
    21  			"password": &schema.Schema{
    22  				Type:        schema.TypeString,
    23  				Required:    true,
    24  				DefaultFunc: schema.EnvDefaultFunc("VSPHERE_PASSWORD", nil),
    25  				Description: "The user password for vSphere API operations.",
    26  			},
    27  
    28  			"vsphere_server": &schema.Schema{
    29  				Type:        schema.TypeString,
    30  				Optional:    true,
    31  				DefaultFunc: schema.EnvDefaultFunc("VSPHERE_SERVER", nil),
    32  				Description: "The vSphere Server name for vSphere API operations.",
    33  			},
    34  			"allow_unverified_ssl": &schema.Schema{
    35  				Type:        schema.TypeBool,
    36  				Optional:    true,
    37  				DefaultFunc: schema.EnvDefaultFunc("VSPHERE_ALLOW_UNVERIFIED_SSL", false),
    38  				Description: "If set, VMware vSphere client will permit unverifiable SSL certificates.",
    39  			},
    40  			"vcenter_server": &schema.Schema{
    41  				Type:        schema.TypeString,
    42  				Optional:    true,
    43  				DefaultFunc: schema.EnvDefaultFunc("VSPHERE_VCENTER", nil),
    44  				Deprecated:  "This field has been renamed to vsphere_server.",
    45  			},
    46  		},
    47  
    48  		ResourcesMap: map[string]*schema.Resource{
    49  			"vsphere_file":            resourceVSphereFile(),
    50  			"vsphere_folder":          resourceVSphereFolder(),
    51  			"vsphere_virtual_machine": resourceVSphereVirtualMachine(),
    52  		},
    53  
    54  		ConfigureFunc: providerConfigure,
    55  	}
    56  }
    57  
    58  func providerConfigure(d *schema.ResourceData) (interface{}, error) {
    59  	// Handle backcompat support for vcenter_server; once that is removed,
    60  	// vsphere_server can just become a Required field that is referenced inline
    61  	// in Config below.
    62  	server := d.Get("vsphere_server").(string)
    63  
    64  	if server == "" {
    65  		server = d.Get("vcenter_server").(string)
    66  	}
    67  
    68  	if server == "" {
    69  		return nil, fmt.Errorf(
    70  			"One of vsphere_server or [deprecated] vcenter_server must be provided.")
    71  	}
    72  
    73  	config := Config{
    74  		User:          d.Get("user").(string),
    75  		Password:      d.Get("password").(string),
    76  		InsecureFlag:  d.Get("allow_unverified_ssl").(bool),
    77  		VSphereServer: server,
    78  	}
    79  
    80  	return config.Client()
    81  }