github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/profitbricks/provider.go (about)

     1  package profitbricks
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/hashicorp/terraform/helper/schema"
     6  	"github.com/hashicorp/terraform/terraform"
     7  	"github.com/profitbricks/profitbricks-sdk-go"
     8  )
     9  
    10  // Provider returns a schema.Provider for DigitalOcean.
    11  func Provider() terraform.ResourceProvider {
    12  	return &schema.Provider{
    13  		Schema: map[string]*schema.Schema{
    14  			"username": {
    15  				Type:        schema.TypeString,
    16  				Required:    true,
    17  				DefaultFunc: schema.EnvDefaultFunc("PROFITBRICKS_USERNAME", nil),
    18  				Description: "Profitbricks username for API operations.",
    19  			},
    20  			"password": {
    21  				Type:        schema.TypeString,
    22  				Required:    true,
    23  				DefaultFunc: schema.EnvDefaultFunc("PROFITBRICKS_PASSWORD", nil),
    24  				Description: "Profitbricks password for API operations.",
    25  			},
    26  			"endpoint": {
    27  				Type:        schema.TypeString,
    28  				Optional:    true,
    29  				DefaultFunc: schema.EnvDefaultFunc("PROFITBRICKS_API_URL", profitbricks.Endpoint),
    30  				Description: "Profitbricks REST API URL.",
    31  			},
    32  			"retries": {
    33  				Type:     schema.TypeInt,
    34  				Optional: true,
    35  				Default:  50,
    36  			},
    37  		},
    38  
    39  		ResourcesMap: map[string]*schema.Resource{
    40  			"profitbricks_datacenter":   resourceProfitBricksDatacenter(),
    41  			"profitbricks_ipblock":      resourceProfitBricksIPBlock(),
    42  			"profitbricks_firewall":     resourceProfitBricksFirewall(),
    43  			"profitbricks_lan":          resourceProfitBricksLan(),
    44  			"profitbricks_loadbalancer": resourceProfitBricksLoadbalancer(),
    45  			"profitbricks_nic":          resourceProfitBricksNic(),
    46  			"profitbricks_server":       resourceProfitBricksServer(),
    47  			"profitbricks_volume":       resourceProfitBricksVolume(),
    48  		},
    49  		DataSourcesMap: map[string]*schema.Resource{
    50  			"profitbricks_datacenter": dataSourceDataCenter(),
    51  			"profitbricks_location":   dataSourceLocation(),
    52  			"profitbricks_image":      dataSourceImage(),
    53  		},
    54  		ConfigureFunc: providerConfigure,
    55  	}
    56  }
    57  
    58  func providerConfigure(d *schema.ResourceData) (interface{}, error) {
    59  
    60  	if _, ok := d.GetOk("username"); !ok {
    61  		return nil, fmt.Errorf("ProfitBricks username has not been provided.")
    62  	}
    63  
    64  	if _, ok := d.GetOk("password"); !ok {
    65  		return nil, fmt.Errorf("ProfitBricks password has not been provided.")
    66  	}
    67  
    68  	config := Config{
    69  		Username: d.Get("username").(string),
    70  		Password: d.Get("password").(string),
    71  		Endpoint: d.Get("endpoint").(string),
    72  		Retries:  d.Get("retries").(int),
    73  	}
    74  
    75  	return config.Client()
    76  }