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

     1  package aws
     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 terraform.ResourceProvider.
    11  func Provider() terraform.ResourceProvider {
    12  	// TODO: Move the validation to this, requires conditional schemas
    13  	// TODO: Move the configuration to this, requires validation
    14  
    15  	return &schema.Provider{
    16  		Schema: map[string]*schema.Schema{
    17  			"access_key": &schema.Schema{
    18  				Type:        schema.TypeString,
    19  				Required:    true,
    20  				DefaultFunc: envDefaultFunc("AWS_ACCESS_KEY"),
    21  				Description: descriptions["access_key"],
    22  			},
    23  
    24  			"secret_key": &schema.Schema{
    25  				Type:        schema.TypeString,
    26  				Required:    true,
    27  				DefaultFunc: envDefaultFunc("AWS_SECRET_KEY"),
    28  				Description: descriptions["secret_key"],
    29  			},
    30  
    31  			"region": &schema.Schema{
    32  				Type:         schema.TypeString,
    33  				Required:     true,
    34  				DefaultFunc:  envDefaultFunc("AWS_REGION"),
    35  				Description:  descriptions["region"],
    36  				InputDefault: "us-east-1",
    37  			},
    38  		},
    39  
    40  		ResourcesMap: map[string]*schema.Resource{
    41  			"aws_autoscaling_group":       resourceAwsAutoscalingGroup(),
    42  			"aws_db_instance":             resourceAwsDbInstance(),
    43  			"aws_db_parameter_group":      resourceAwsDbParameterGroup(),
    44  			"aws_db_security_group":       resourceAwsDbSecurityGroup(),
    45  			"aws_db_subnet_group":         resourceAwsDbSubnetGroup(),
    46  			"aws_eip":                     resourceAwsEip(),
    47  			"aws_elb":                     resourceAwsElb(),
    48  			"aws_instance":                resourceAwsInstance(),
    49  			"aws_internet_gateway":        resourceAwsInternetGateway(),
    50  			"aws_key_pair":                resourceAwsKeyPair(),
    51  			"aws_launch_configuration":    resourceAwsLaunchConfiguration(),
    52  			"aws_network_acl":             resourceAwsNetworkAcl(),
    53  			"aws_route53_record":          resourceAwsRoute53Record(),
    54  			"aws_route53_zone":            resourceAwsRoute53Zone(),
    55  			"aws_route_table":             resourceAwsRouteTable(),
    56  			"aws_route_table_association": resourceAwsRouteTableAssociation(),
    57  			"aws_s3_bucket":               resourceAwsS3Bucket(),
    58  			"aws_security_group":          resourceAwsSecurityGroup(),
    59  			"aws_subnet":                  resourceAwsSubnet(),
    60  			"aws_vpc":                     resourceAwsVpc(),
    61  		},
    62  
    63  		ConfigureFunc: providerConfigure,
    64  	}
    65  }
    66  
    67  var descriptions map[string]string
    68  
    69  func init() {
    70  	descriptions = map[string]string{
    71  		"region": "The region where AWS operations will take place. Examples\n" +
    72  			"are us-east-1, us-west-2, etc.",
    73  
    74  		"access_key": "The access key for API operations. You can retrieve this\n" +
    75  			"from the 'Security & Credentials' section of the AWS console.",
    76  
    77  		"secret_key": "The secret key for API operations. You can retrieve this\n" +
    78  			"from the 'Security & Credentials' section of the AWS console.",
    79  	}
    80  }
    81  
    82  func envDefaultFunc(k string) schema.SchemaDefaultFunc {
    83  	return func() (interface{}, error) {
    84  		if v := os.Getenv(k); v != "" {
    85  			return v, nil
    86  		}
    87  
    88  		return nil, nil
    89  	}
    90  }
    91  
    92  func providerConfigure(d *schema.ResourceData) (interface{}, error) {
    93  	config := Config{
    94  		AccessKey: d.Get("access_key").(string),
    95  		SecretKey: d.Get("secret_key").(string),
    96  		Region:    d.Get("region").(string),
    97  	}
    98  
    99  	return config.Client()
   100  }