github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/openstack/provider.go (about)

     1  package openstack
     2  
     3  import (
     4  	"os"
     5  
     6  	"github.com/hashicorp/terraform/helper/schema"
     7  	"github.com/hashicorp/terraform/terraform"
     8  )
     9  
    10  // Provider returns a schema.Provider for OpenStack.
    11  func Provider() terraform.ResourceProvider {
    12  	return &schema.Provider{
    13  		Schema: map[string]*schema.Schema{
    14  			"auth_url": &schema.Schema{
    15  				Type:        schema.TypeString,
    16  				Required:    true,
    17  				DefaultFunc: envDefaultFunc("OS_AUTH_URL"),
    18  			},
    19  			"user_name": &schema.Schema{
    20  				Type:        schema.TypeString,
    21  				Optional:    true,
    22  				DefaultFunc: envDefaultFunc("OS_USERNAME"),
    23  			},
    24  			"user_id": &schema.Schema{
    25  				Type:     schema.TypeString,
    26  				Optional: true,
    27  				Default:  "",
    28  			},
    29  			"tenant_id": &schema.Schema{
    30  				Type:     schema.TypeString,
    31  				Optional: true,
    32  				Default:  "",
    33  			},
    34  			"tenant_name": &schema.Schema{
    35  				Type:        schema.TypeString,
    36  				Optional:    true,
    37  				DefaultFunc: envDefaultFunc("OS_TENANT_NAME"),
    38  			},
    39  			"password": &schema.Schema{
    40  				Type:        schema.TypeString,
    41  				Optional:    true,
    42  				DefaultFunc: envDefaultFunc("OS_PASSWORD"),
    43  			},
    44  			"api_key": &schema.Schema{
    45  				Type:        schema.TypeString,
    46  				Optional:    true,
    47  				DefaultFunc: envDefaultFuncAllowMissing("OS_AUTH_TOKEN"),
    48  			},
    49  			"domain_id": &schema.Schema{
    50  				Type:     schema.TypeString,
    51  				Optional: true,
    52  				Default:  "",
    53  			},
    54  			"domain_name": &schema.Schema{
    55  				Type:     schema.TypeString,
    56  				Optional: true,
    57  				Default:  "",
    58  			},
    59  			"insecure": &schema.Schema{
    60  				Type:     schema.TypeBool,
    61  				Optional: true,
    62  				Default:  false,
    63  			},
    64  			"endpoint_type": &schema.Schema{
    65  				Type:        schema.TypeString,
    66  				Optional:    true,
    67  				DefaultFunc: envDefaultFuncAllowMissing("OS_ENDPOINT_TYPE"),
    68  			},
    69  		},
    70  
    71  		ResourcesMap: map[string]*schema.Resource{
    72  			"openstack_blockstorage_volume_v1":         resourceBlockStorageVolumeV1(),
    73  			"openstack_compute_instance_v2":            resourceComputeInstanceV2(),
    74  			"openstack_compute_keypair_v2":             resourceComputeKeypairV2(),
    75  			"openstack_compute_secgroup_v2":            resourceComputeSecGroupV2(),
    76  			"openstack_compute_servergroup_v2":         resourceComputeServerGroupV2(),
    77  			"openstack_compute_floatingip_v2":          resourceComputeFloatingIPV2(),
    78  			"openstack_fw_firewall_v1":                 resourceFWFirewallV1(),
    79  			"openstack_fw_policy_v1":                   resourceFWPolicyV1(),
    80  			"openstack_fw_rule_v1":                     resourceFWRuleV1(),
    81  			"openstack_lb_monitor_v1":                  resourceLBMonitorV1(),
    82  			"openstack_lb_pool_v1":                     resourceLBPoolV1(),
    83  			"openstack_lb_vip_v1":                      resourceLBVipV1(),
    84  			"openstack_networking_network_v2":          resourceNetworkingNetworkV2(),
    85  			"openstack_networking_subnet_v2":           resourceNetworkingSubnetV2(),
    86  			"openstack_networking_floatingip_v2":       resourceNetworkingFloatingIPV2(),
    87  			"openstack_networking_port_v2":             resourceNetworkingPortV2(),
    88  			"openstack_networking_router_v2":           resourceNetworkingRouterV2(),
    89  			"openstack_networking_router_interface_v2": resourceNetworkingRouterInterfaceV2(),
    90  			"openstack_objectstorage_container_v1":     resourceObjectStorageContainerV1(),
    91  		},
    92  
    93  		ConfigureFunc: configureProvider,
    94  	}
    95  }
    96  
    97  func configureProvider(d *schema.ResourceData) (interface{}, error) {
    98  	config := Config{
    99  		IdentityEndpoint: d.Get("auth_url").(string),
   100  		Username:         d.Get("user_name").(string),
   101  		UserID:           d.Get("user_id").(string),
   102  		Password:         d.Get("password").(string),
   103  		APIKey:           d.Get("api_key").(string),
   104  		TenantID:         d.Get("tenant_id").(string),
   105  		TenantName:       d.Get("tenant_name").(string),
   106  		DomainID:         d.Get("domain_id").(string),
   107  		DomainName:       d.Get("domain_name").(string),
   108  		Insecure:         d.Get("insecure").(bool),
   109  		EndpointType:     d.Get("endpoint_type").(string),
   110  	}
   111  
   112  	if err := config.loadAndValidate(); err != nil {
   113  		return nil, err
   114  	}
   115  
   116  	return &config, nil
   117  }
   118  
   119  func envDefaultFunc(k string) schema.SchemaDefaultFunc {
   120  	return func() (interface{}, error) {
   121  		if v := os.Getenv(k); v != "" {
   122  			return v, nil
   123  		}
   124  
   125  		return nil, nil
   126  	}
   127  }
   128  
   129  func envDefaultFuncAllowMissing(k string) schema.SchemaDefaultFunc {
   130  	return func() (interface{}, error) {
   131  		v := os.Getenv(k)
   132  		return v, nil
   133  	}
   134  }