forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into update_vmware-govcd
* master: (34 commits) Update CHANGELOG.md provider/aws: Delete access keys before deleting IAM user (hashicorp#7766) Fix broken link to Consul demo (hashicorp#7789) provider/aws: `aws_redshift_cluster` `number_of_nodes` was having the (hashicorp#7771) provider/aws: Restore lost client.simpledbconn initialization Update vendored atlas client Make using `ssl_verify_mode` more robust (hashicorp#7769) Update CHANGELOG.md provider/aws: Rename the ECS Container Data Source test docs/azure: Small changes to remove the use of double Update docs to centralize on ARM-based Azure provider (hashicorp#7767) Update CHANGELOG.md Update CHANGELOG.md Add support for Kinesis streams shard-level metrics (hashicorp#7684) Update CHANGELOG.md Implementing aws_ami_launch_permission. (hashicorp#7365) Update CHANGELOG.md Add VersionString provider/aws: Set `storage_encrypted` to state in (hashicorp#7751) provider/fastly: Update go-fastly SDK (hashicorp#7747) ...
- Loading branch information
Showing
63 changed files
with
1,259 additions
and
408 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
builtin/providers/aws/data_source_aws_ecs_container_definition.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/ecs" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
func dataSourceAwsEcsContainerDefinition() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceAwsEcsContainerDefinitionRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"task_definition": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"container_name": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
// Computed values. | ||
"image": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"image_digest": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"cpu": &schema.Schema{ | ||
Type: schema.TypeInt, | ||
Computed: true, | ||
}, | ||
"memory": &schema.Schema{ | ||
Type: schema.TypeInt, | ||
Computed: true, | ||
}, | ||
"disable_networking": &schema.Schema{ | ||
Type: schema.TypeBool, | ||
Computed: true, | ||
}, | ||
"docker_labels": &schema.Schema{ | ||
Type: schema.TypeMap, | ||
Computed: true, | ||
Elem: schema.TypeString, | ||
}, | ||
"environment": &schema.Schema{ | ||
Type: schema.TypeMap, | ||
Computed: true, | ||
Elem: schema.TypeString, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceAwsEcsContainerDefinitionRead(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).ecsconn | ||
|
||
desc, err := conn.DescribeTaskDefinition(&ecs.DescribeTaskDefinitionInput{ | ||
TaskDefinition: aws.String(d.Get("task_definition").(string)), | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
taskDefinition := *desc.TaskDefinition | ||
for _, def := range taskDefinition.ContainerDefinitions { | ||
if aws.StringValue(def.Name) != d.Get("container_name").(string) { | ||
continue | ||
} | ||
|
||
d.SetId(fmt.Sprintf("%s/%s", aws.StringValue(taskDefinition.TaskDefinitionArn), d.Get("container_name").(string))) | ||
d.Set("image", aws.StringValue(def.Image)) | ||
d.Set("image_digest", strings.Split(aws.StringValue(def.Image), ":")[1]) | ||
d.Set("cpu", aws.Int64Value(def.Cpu)) | ||
d.Set("memory", aws.Int64Value(def.Memory)) | ||
d.Set("disable_networking", aws.BoolValue(def.DisableNetworking)) | ||
d.Set("docker_labels", aws.StringValueMap(def.DockerLabels)) | ||
|
||
var environment = map[string]string{} | ||
for _, keyValuePair := range def.Environment { | ||
environment[aws.StringValue(keyValuePair.Name)] = aws.StringValue(keyValuePair.Value) | ||
} | ||
d.Set("environment", environment) | ||
} | ||
|
||
if d.Id() == "" { | ||
return fmt.Errorf("container with name %q not found in task definition %q", d.Get("container_name").(string), d.Get("task_definition").(string)) | ||
} | ||
|
||
return nil | ||
} |
62 changes: 62 additions & 0 deletions
62
builtin/providers/aws/data_source_aws_ecs_container_definition_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package aws | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/helper/resource" | ||
) | ||
|
||
func TestAccAWSEcsDataSource_ecsContainerDefinition(t *testing.T) { | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
resource.TestStep{ | ||
Config: testAccCheckAwsEcsContainerDefinitionDataSourceConfig, | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr("data.aws_ecs_container_definition.mongo", "image", "mongo:latest"), | ||
resource.TestCheckResourceAttr("data.aws_ecs_container_definition.mongo", "memory", "128"), | ||
resource.TestCheckResourceAttr("data.aws_ecs_container_definition.mongo", "cpu", "128"), | ||
resource.TestCheckResourceAttr("data.aws_ecs_container_definition.mongo", "environment.SECRET", "KEY"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
const testAccCheckAwsEcsContainerDefinitionDataSourceConfig = ` | ||
resource "aws_ecs_cluster" "default" { | ||
name = "terraformecstest1" | ||
} | ||
resource "aws_ecs_task_definition" "mongo" { | ||
family = "mongodb" | ||
container_definitions = <<DEFINITION | ||
[ | ||
{ | ||
"cpu": 128, | ||
"environment": [{ | ||
"name": "SECRET", | ||
"value": "KEY" | ||
}], | ||
"essential": true, | ||
"image": "mongo:latest", | ||
"memory": 128, | ||
"name": "mongodb" | ||
} | ||
] | ||
DEFINITION | ||
} | ||
resource "aws_ecs_service" "mongo" { | ||
name = "mongodb" | ||
cluster = "${aws_ecs_cluster.default.id}" | ||
task_definition = "${aws_ecs_task_definition.mongo.arn}" | ||
desired_count = 1 | ||
} | ||
data "aws_ecs_container_definition" "mongo" { | ||
task_definition = "${aws_ecs_task_definition.mongo.id}" | ||
container_name = "mongodb" | ||
} | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
builtin/providers/aws/resource_aws_ami_launch_permission.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package aws | ||
|
||
import ( | ||
"fmt" | ||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/ec2" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
func resourceAwsAmiLaunchPermission() *schema.Resource { | ||
return &schema.Resource{ | ||
Exists: resourceAwsAmiLaunchPermissionExists, | ||
Create: resourceAwsAmiLaunchPermissionCreate, | ||
Read: resourceAwsAmiLaunchPermissionRead, | ||
Delete: resourceAwsAmiLaunchPermissionDelete, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"image_id": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"account_id": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceAwsAmiLaunchPermissionExists(d *schema.ResourceData, meta interface{}) (bool, error) { | ||
conn := meta.(*AWSClient).ec2conn | ||
|
||
image_id := d.Get("image_id").(string) | ||
account_id := d.Get("account_id").(string) | ||
return hasLaunchPermission(conn, image_id, account_id) | ||
} | ||
|
||
func resourceAwsAmiLaunchPermissionCreate(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).ec2conn | ||
|
||
image_id := d.Get("image_id").(string) | ||
account_id := d.Get("account_id").(string) | ||
|
||
_, err := conn.ModifyImageAttribute(&ec2.ModifyImageAttributeInput{ | ||
ImageId: aws.String(image_id), | ||
Attribute: aws.String("launchPermission"), | ||
LaunchPermission: &ec2.LaunchPermissionModifications{ | ||
Add: []*ec2.LaunchPermission{ | ||
&ec2.LaunchPermission{UserId: aws.String(account_id)}, | ||
}, | ||
}, | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("error creating ami launch permission: %s", err) | ||
} | ||
|
||
d.SetId(fmt.Sprintf("%s-%s", image_id, account_id)) | ||
return nil | ||
} | ||
|
||
func resourceAwsAmiLaunchPermissionRead(d *schema.ResourceData, meta interface{}) error { | ||
return nil | ||
} | ||
|
||
func resourceAwsAmiLaunchPermissionDelete(d *schema.ResourceData, meta interface{}) error { | ||
conn := meta.(*AWSClient).ec2conn | ||
|
||
image_id := d.Get("image_id").(string) | ||
account_id := d.Get("account_id").(string) | ||
|
||
_, err := conn.ModifyImageAttribute(&ec2.ModifyImageAttributeInput{ | ||
ImageId: aws.String(image_id), | ||
Attribute: aws.String("launchPermission"), | ||
LaunchPermission: &ec2.LaunchPermissionModifications{ | ||
Remove: []*ec2.LaunchPermission{ | ||
&ec2.LaunchPermission{UserId: aws.String(account_id)}, | ||
}, | ||
}, | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("error removing ami launch permission: %s", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func hasLaunchPermission(conn *ec2.EC2, image_id string, account_id string) (bool, error) { | ||
attrs, err := conn.DescribeImageAttribute(&ec2.DescribeImageAttributeInput{ | ||
ImageId: aws.String(image_id), | ||
Attribute: aws.String("launchPermission"), | ||
}) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
for _, lp := range attrs.LaunchPermissions { | ||
if *lp.UserId == account_id { | ||
return true, nil | ||
} | ||
} | ||
return false, nil | ||
} |
Oops, something went wrong.