github.com/chalford/terraform@v0.3.7-0.20150113080010-a78c69a8c81f/builtin/providers/atlas/provider.go (about)

     1  package atlas
     2  
     3  import (
     4  	"os"
     5  
     6  	"github.com/hashicorp/atlas-go/v1"
     7  	"github.com/hashicorp/terraform/helper/schema"
     8  	"github.com/hashicorp/terraform/terraform"
     9  )
    10  
    11  const (
    12  	// defaultAtlasServer is the default endpoint for Atlas if
    13  	// none is specified.
    14  	defaultAtlasServer = "https://atlas.hashicorp.com"
    15  )
    16  
    17  // Provider returns a terraform.ResourceProvider.
    18  func Provider() terraform.ResourceProvider {
    19  	return &schema.Provider{
    20  		Schema: map[string]*schema.Schema{
    21  			"token": &schema.Schema{
    22  				Type:        schema.TypeString,
    23  				Required:    true,
    24  				DefaultFunc: envDefaultFunc("ATLAS_TOKEN", nil),
    25  				Description: descriptions["token"],
    26  			},
    27  
    28  			"address": &schema.Schema{
    29  				Type:        schema.TypeString,
    30  				Optional:    true,
    31  				DefaultFunc: envDefaultFunc("ATLAS_ADDRESS", defaultAtlasServer),
    32  				Description: descriptions["address"],
    33  			},
    34  		},
    35  
    36  		ResourcesMap: map[string]*schema.Resource{
    37  			"atlas_artifact": resourceArtifact(),
    38  		},
    39  
    40  		ConfigureFunc: providerConfigure,
    41  	}
    42  }
    43  
    44  func providerConfigure(d *schema.ResourceData) (interface{}, error) {
    45  	var err error
    46  	client := atlas.DefaultClient()
    47  	if v := d.Get("address").(string); v != "" {
    48  		client, err = atlas.NewClient(v)
    49  		if err != nil {
    50  			return nil, err
    51  		}
    52  	}
    53  	client.Token = d.Get("token").(string)
    54  
    55  	return client, nil
    56  }
    57  
    58  func envDefaultFunc(k string, alt interface{}) schema.SchemaDefaultFunc {
    59  	return func() (interface{}, error) {
    60  		if v := os.Getenv(k); v != "" {
    61  			return v, nil
    62  		}
    63  
    64  		return alt, nil
    65  	}
    66  }
    67  
    68  var descriptions map[string]string
    69  
    70  func init() {
    71  	descriptions = map[string]string{
    72  		"address": "The address of the Atlas server. If blank, the public\n" +
    73  			"server at atlas.hashicorp.com will be used.",
    74  
    75  		"token": "The access token for reading artifacts. This is required\n" +
    76  			"if reading private artifacts.",
    77  	}
    78  }