github.com/articulate/terraform@v0.6.13-0.20160303003731-8d31c93862de/builtin/providers/aws/provider.go (about) 1 package aws 2 3 import ( 4 "bytes" 5 "fmt" 6 7 "github.com/hashicorp/terraform/helper/hashcode" 8 "github.com/hashicorp/terraform/helper/mutexkv" 9 "github.com/hashicorp/terraform/helper/schema" 10 "github.com/hashicorp/terraform/terraform" 11 ) 12 13 // Provider returns a terraform.ResourceProvider. 14 func Provider() terraform.ResourceProvider { 15 // TODO: Move the validation to this, requires conditional schemas 16 // TODO: Move the configuration to this, requires validation 17 18 // The actual provider 19 return &schema.Provider{ 20 Schema: map[string]*schema.Schema{ 21 "access_key": &schema.Schema{ 22 Type: schema.TypeString, 23 Optional: true, 24 Default: "", 25 Description: descriptions["access_key"], 26 }, 27 28 "secret_key": &schema.Schema{ 29 Type: schema.TypeString, 30 Optional: true, 31 Default: "", 32 Description: descriptions["secret_key"], 33 }, 34 35 "profile": &schema.Schema{ 36 Type: schema.TypeString, 37 Optional: true, 38 Default: "", 39 Description: descriptions["profile"], 40 }, 41 42 "shared_credentials_file": &schema.Schema{ 43 Type: schema.TypeString, 44 Optional: true, 45 Default: "", 46 Description: descriptions["shared_credentials_file"], 47 }, 48 49 "token": &schema.Schema{ 50 Type: schema.TypeString, 51 Optional: true, 52 Default: "", 53 Description: descriptions["token"], 54 }, 55 56 "region": &schema.Schema{ 57 Type: schema.TypeString, 58 Required: true, 59 DefaultFunc: schema.MultiEnvDefaultFunc([]string{ 60 "AWS_REGION", 61 "AWS_DEFAULT_REGION", 62 }, nil), 63 Description: descriptions["region"], 64 InputDefault: "us-east-1", 65 }, 66 67 "max_retries": &schema.Schema{ 68 Type: schema.TypeInt, 69 Optional: true, 70 Default: 11, 71 Description: descriptions["max_retries"], 72 }, 73 74 "allowed_account_ids": &schema.Schema{ 75 Type: schema.TypeSet, 76 Elem: &schema.Schema{Type: schema.TypeString}, 77 Optional: true, 78 ConflictsWith: []string{"forbidden_account_ids"}, 79 Set: schema.HashString, 80 }, 81 82 "forbidden_account_ids": &schema.Schema{ 83 Type: schema.TypeSet, 84 Elem: &schema.Schema{Type: schema.TypeString}, 85 Optional: true, 86 ConflictsWith: []string{"allowed_account_ids"}, 87 Set: schema.HashString, 88 }, 89 90 "dynamodb_endpoint": &schema.Schema{ 91 Type: schema.TypeString, 92 Optional: true, 93 Default: "", 94 Description: descriptions["dynamodb_endpoint"], 95 }, 96 97 "kinesis_endpoint": &schema.Schema{ 98 Type: schema.TypeString, 99 Optional: true, 100 Default: "", 101 Description: descriptions["kinesis_endpoint"], 102 }, 103 "endpoints": endpointsSchema(), 104 105 "insecure": &schema.Schema{ 106 Type: schema.TypeBool, 107 Optional: true, 108 Default: false, 109 Description: descriptions["insecure"], 110 }, 111 }, 112 113 ResourcesMap: map[string]*schema.Resource{ 114 "aws_ami": resourceAwsAmi(), 115 "aws_ami_copy": resourceAwsAmiCopy(), 116 "aws_ami_from_instance": resourceAwsAmiFromInstance(), 117 "aws_app_cookie_stickiness_policy": resourceAwsAppCookieStickinessPolicy(), 118 "aws_autoscaling_group": resourceAwsAutoscalingGroup(), 119 "aws_autoscaling_notification": resourceAwsAutoscalingNotification(), 120 "aws_autoscaling_policy": resourceAwsAutoscalingPolicy(), 121 "aws_autoscaling_schedule": resourceAwsAutoscalingSchedule(), 122 "aws_cloudformation_stack": resourceAwsCloudFormationStack(), 123 "aws_cloudtrail": resourceAwsCloudTrail(), 124 "aws_cloudwatch_event_rule": resourceAwsCloudWatchEventRule(), 125 "aws_cloudwatch_event_target": resourceAwsCloudWatchEventTarget(), 126 "aws_cloudwatch_log_group": resourceAwsCloudWatchLogGroup(), 127 "aws_autoscaling_lifecycle_hook": resourceAwsAutoscalingLifecycleHook(), 128 "aws_cloudwatch_metric_alarm": resourceAwsCloudWatchMetricAlarm(), 129 "aws_codedeploy_app": resourceAwsCodeDeployApp(), 130 "aws_codedeploy_deployment_group": resourceAwsCodeDeployDeploymentGroup(), 131 "aws_codecommit_repository": resourceAwsCodeCommitRepository(), 132 "aws_customer_gateway": resourceAwsCustomerGateway(), 133 "aws_db_instance": resourceAwsDbInstance(), 134 "aws_db_parameter_group": resourceAwsDbParameterGroup(), 135 "aws_db_security_group": resourceAwsDbSecurityGroup(), 136 "aws_db_subnet_group": resourceAwsDbSubnetGroup(), 137 "aws_directory_service_directory": resourceAwsDirectoryServiceDirectory(), 138 "aws_dynamodb_table": resourceAwsDynamoDbTable(), 139 "aws_ebs_volume": resourceAwsEbsVolume(), 140 "aws_ecr_repository": resourceAwsEcrRepository(), 141 "aws_ecr_repository_policy": resourceAwsEcrRepositoryPolicy(), 142 "aws_ecs_cluster": resourceAwsEcsCluster(), 143 "aws_ecs_service": resourceAwsEcsService(), 144 "aws_ecs_task_definition": resourceAwsEcsTaskDefinition(), 145 "aws_efs_file_system": resourceAwsEfsFileSystem(), 146 "aws_efs_mount_target": resourceAwsEfsMountTarget(), 147 "aws_eip": resourceAwsEip(), 148 "aws_elasticache_cluster": resourceAwsElasticacheCluster(), 149 "aws_elasticache_parameter_group": resourceAwsElasticacheParameterGroup(), 150 "aws_elasticache_security_group": resourceAwsElasticacheSecurityGroup(), 151 "aws_elasticache_subnet_group": resourceAwsElasticacheSubnetGroup(), 152 "aws_elasticsearch_domain": resourceAwsElasticSearchDomain(), 153 "aws_elb": resourceAwsElb(), 154 "aws_flow_log": resourceAwsFlowLog(), 155 "aws_glacier_vault": resourceAwsGlacierVault(), 156 "aws_iam_access_key": resourceAwsIamAccessKey(), 157 "aws_iam_account_password_policy": resourceAwsIamAccountPasswordPolicy(), 158 "aws_iam_group_policy": resourceAwsIamGroupPolicy(), 159 "aws_iam_group": resourceAwsIamGroup(), 160 "aws_iam_group_membership": resourceAwsIamGroupMembership(), 161 "aws_iam_instance_profile": resourceAwsIamInstanceProfile(), 162 "aws_iam_policy": resourceAwsIamPolicy(), 163 "aws_iam_policy_attachment": resourceAwsIamPolicyAttachment(), 164 "aws_iam_role_policy": resourceAwsIamRolePolicy(), 165 "aws_iam_role": resourceAwsIamRole(), 166 "aws_iam_saml_provider": resourceAwsIamSamlProvider(), 167 "aws_iam_server_certificate": resourceAwsIAMServerCertificate(), 168 "aws_iam_user_policy": resourceAwsIamUserPolicy(), 169 "aws_iam_user": resourceAwsIamUser(), 170 "aws_instance": resourceAwsInstance(), 171 "aws_internet_gateway": resourceAwsInternetGateway(), 172 "aws_key_pair": resourceAwsKeyPair(), 173 "aws_kinesis_firehose_delivery_stream": resourceAwsKinesisFirehoseDeliveryStream(), 174 "aws_kinesis_stream": resourceAwsKinesisStream(), 175 "aws_lambda_function": resourceAwsLambdaFunction(), 176 "aws_lambda_event_source_mapping": resourceAwsLambdaEventSourceMapping(), 177 "aws_lambda_alias": resourceAwsLambdaAlias(), 178 "aws_lambda_permission": resourceAwsLambdaPermission(), 179 "aws_launch_configuration": resourceAwsLaunchConfiguration(), 180 "aws_lb_cookie_stickiness_policy": resourceAwsLBCookieStickinessPolicy(), 181 "aws_main_route_table_association": resourceAwsMainRouteTableAssociation(), 182 "aws_nat_gateway": resourceAwsNatGateway(), 183 "aws_network_acl": resourceAwsNetworkAcl(), 184 "aws_network_acl_rule": resourceAwsNetworkAclRule(), 185 "aws_network_interface": resourceAwsNetworkInterface(), 186 "aws_opsworks_stack": resourceAwsOpsworksStack(), 187 "aws_opsworks_java_app_layer": resourceAwsOpsworksJavaAppLayer(), 188 "aws_opsworks_haproxy_layer": resourceAwsOpsworksHaproxyLayer(), 189 "aws_opsworks_static_web_layer": resourceAwsOpsworksStaticWebLayer(), 190 "aws_opsworks_php_app_layer": resourceAwsOpsworksPhpAppLayer(), 191 "aws_opsworks_rails_app_layer": resourceAwsOpsworksRailsAppLayer(), 192 "aws_opsworks_nodejs_app_layer": resourceAwsOpsworksNodejsAppLayer(), 193 "aws_opsworks_memcached_layer": resourceAwsOpsworksMemcachedLayer(), 194 "aws_opsworks_mysql_layer": resourceAwsOpsworksMysqlLayer(), 195 "aws_opsworks_ganglia_layer": resourceAwsOpsworksGangliaLayer(), 196 "aws_opsworks_custom_layer": resourceAwsOpsworksCustomLayer(), 197 "aws_placement_group": resourceAwsPlacementGroup(), 198 "aws_proxy_protocol_policy": resourceAwsProxyProtocolPolicy(), 199 "aws_rds_cluster": resourceAwsRDSCluster(), 200 "aws_rds_cluster_instance": resourceAwsRDSClusterInstance(), 201 "aws_redshift_cluster": resourceAwsRedshiftCluster(), 202 "aws_redshift_security_group": resourceAwsRedshiftSecurityGroup(), 203 "aws_redshift_parameter_group": resourceAwsRedshiftParameterGroup(), 204 "aws_redshift_subnet_group": resourceAwsRedshiftSubnetGroup(), 205 "aws_route53_delegation_set": resourceAwsRoute53DelegationSet(), 206 "aws_route53_record": resourceAwsRoute53Record(), 207 "aws_route53_zone_association": resourceAwsRoute53ZoneAssociation(), 208 "aws_route53_zone": resourceAwsRoute53Zone(), 209 "aws_route53_health_check": resourceAwsRoute53HealthCheck(), 210 "aws_route": resourceAwsRoute(), 211 "aws_route_table": resourceAwsRouteTable(), 212 "aws_route_table_association": resourceAwsRouteTableAssociation(), 213 "aws_s3_bucket": resourceAwsS3Bucket(), 214 "aws_s3_bucket_object": resourceAwsS3BucketObject(), 215 "aws_security_group": resourceAwsSecurityGroup(), 216 "aws_security_group_rule": resourceAwsSecurityGroupRule(), 217 "aws_spot_instance_request": resourceAwsSpotInstanceRequest(), 218 "aws_sqs_queue": resourceAwsSqsQueue(), 219 "aws_sns_topic": resourceAwsSnsTopic(), 220 "aws_sns_topic_subscription": resourceAwsSnsTopicSubscription(), 221 "aws_subnet": resourceAwsSubnet(), 222 "aws_volume_attachment": resourceAwsVolumeAttachment(), 223 "aws_vpc_dhcp_options_association": resourceAwsVpcDhcpOptionsAssociation(), 224 "aws_vpc_dhcp_options": resourceAwsVpcDhcpOptions(), 225 "aws_vpc_peering_connection": resourceAwsVpcPeeringConnection(), 226 "aws_vpc": resourceAwsVpc(), 227 "aws_vpc_endpoint": resourceAwsVpcEndpoint(), 228 "aws_vpn_connection": resourceAwsVpnConnection(), 229 "aws_vpn_connection_route": resourceAwsVpnConnectionRoute(), 230 "aws_vpn_gateway": resourceAwsVpnGateway(), 231 }, 232 233 ConfigureFunc: providerConfigure, 234 } 235 } 236 237 var descriptions map[string]string 238 239 func init() { 240 descriptions = map[string]string{ 241 "region": "The region where AWS operations will take place. Examples\n" + 242 "are us-east-1, us-west-2, etc.", 243 244 "access_key": "The access key for API operations. You can retrieve this\n" + 245 "from the 'Security & Credentials' section of the AWS console.", 246 247 "secret_key": "The secret key for API operations. You can retrieve this\n" + 248 "from the 'Security & Credentials' section of the AWS console.", 249 250 "profile": "The profile for API operations. If not set, the default profile\n" + 251 "created with `aws configure` will be used.", 252 253 "shared_credentials_file": "The path to the shared credentials file. If not set\n" + 254 "this defaults to ~/.aws/credentials.", 255 256 "token": "session token. A session token is only required if you are\n" + 257 "using temporary security credentials.", 258 259 "max_retries": "The maximum number of times an AWS API request is\n" + 260 "being executed. If the API request still fails, an error is\n" + 261 "thrown.", 262 263 "dynamodb_endpoint": "Use this to override the default endpoint URL constructed from the `region`.\n" + 264 "It's typically used to connect to dynamodb-local.", 265 266 "kinesis_endpoint": "Use this to override the default endpoint URL constructed from the `region`.\n" + 267 "It's typically used to connect to kinesalite.", 268 269 "iam_endpoint": "Use this to override the default endpoint URL constructed from the `region`.\n", 270 271 "ec2_endpoint": "Use this to override the default endpoint URL constructed from the `region`.\n", 272 273 "elb_endpoint": "Use this to override the default endpoint URL constructed from the `region`.\n", 274 275 "insecure": "Explicitly allow the provider to perform \"insecure\" SSL requests. If omitted," + 276 "default value is `false`", 277 } 278 } 279 280 func providerConfigure(d *schema.ResourceData) (interface{}, error) { 281 config := Config{ 282 AccessKey: d.Get("access_key").(string), 283 SecretKey: d.Get("secret_key").(string), 284 Profile: d.Get("profile").(string), 285 CredsFilename: d.Get("shared_credentials_file").(string), 286 Token: d.Get("token").(string), 287 Region: d.Get("region").(string), 288 MaxRetries: d.Get("max_retries").(int), 289 DynamoDBEndpoint: d.Get("dynamodb_endpoint").(string), 290 KinesisEndpoint: d.Get("kinesis_endpoint").(string), 291 Insecure: d.Get("insecure").(bool), 292 } 293 294 endpointsSet := d.Get("endpoints").(*schema.Set) 295 296 for _, endpointsSetI := range endpointsSet.List() { 297 endpoints := endpointsSetI.(map[string]interface{}) 298 config.IamEndpoint = endpoints["iam"].(string) 299 config.Ec2Endpoint = endpoints["ec2"].(string) 300 config.ElbEndpoint = endpoints["elb"].(string) 301 } 302 303 if v, ok := d.GetOk("allowed_account_ids"); ok { 304 config.AllowedAccountIds = v.(*schema.Set).List() 305 } 306 307 if v, ok := d.GetOk("forbidden_account_ids"); ok { 308 config.ForbiddenAccountIds = v.(*schema.Set).List() 309 } 310 311 return config.Client() 312 } 313 314 // This is a global MutexKV for use within this plugin. 315 var awsMutexKV = mutexkv.NewMutexKV() 316 317 func endpointsSchema() *schema.Schema { 318 return &schema.Schema{ 319 Type: schema.TypeSet, 320 Optional: true, 321 Elem: &schema.Resource{ 322 Schema: map[string]*schema.Schema{ 323 "iam": &schema.Schema{ 324 Type: schema.TypeString, 325 Optional: true, 326 Default: "", 327 Description: descriptions["iam_endpoint"], 328 }, 329 330 "ec2": &schema.Schema{ 331 Type: schema.TypeString, 332 Optional: true, 333 Default: "", 334 Description: descriptions["ec2_endpoint"], 335 }, 336 337 "elb": &schema.Schema{ 338 Type: schema.TypeString, 339 Optional: true, 340 Default: "", 341 Description: descriptions["elb_endpoint"], 342 }, 343 }, 344 }, 345 Set: endpointsToHash, 346 } 347 } 348 349 func endpointsToHash(v interface{}) int { 350 var buf bytes.Buffer 351 m := v.(map[string]interface{}) 352 buf.WriteString(fmt.Sprintf("%s-", m["iam"].(string))) 353 buf.WriteString(fmt.Sprintf("%s-", m["ec2"].(string))) 354 buf.WriteString(fmt.Sprintf("%s-", m["elb"].(string))) 355 356 return hashcode.String(buf.String()) 357 }