Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

r/devicefarm_project - add default_job_timeout_minutes and tags arguments #19574

Merged
merged 5 commits into from
Jun 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changelog/19574.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```release-note:enhancement
resource/aws_devicefarm_project: Add `default_job_timeout_minutes` and `tags` argument
```

```release-note:enhancement
resource/aws_devicefarm_project: Add plan time validation for `name`
```
93 changes: 78 additions & 15 deletions aws/resource_aws_devicefarm_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/devicefarm"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags"
)

func resourceAwsDevicefarmProject() *schema.Resource {
Expand All @@ -26,34 +28,58 @@ func resourceAwsDevicefarmProject() *schema.Resource {
},

"name": {
Type: schema.TypeString,
Required: true,
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringLenBetween(0, 256),
},
"default_job_timeout_minutes": {
Type: schema.TypeInt,
Optional: true,
},
"tags": tagsSchema(),
"tags_all": tagsSchemaComputed(),
},
CustomizeDiff: SetTagsDiff,
}
}

func resourceAwsDevicefarmProjectCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).devicefarmconn
defaultTagsConfig := meta.(*AWSClient).DefaultTagsConfig
tags := defaultTagsConfig.MergeTags(keyvaluetags.New(d.Get("tags").(map[string]interface{})))

name := d.Get("name").(string)
input := &devicefarm.CreateProjectInput{
Name: aws.String(d.Get("name").(string)),
Name: aws.String(name),
}

if v, ok := d.GetOk("default_job_timeout_minutes"); ok {
input.DefaultJobTimeoutMinutes = aws.Int64(int64(v.(int)))
}

log.Printf("[DEBUG] Creating DeviceFarm Project: %s", d.Get("name").(string))
log.Printf("[DEBUG] Creating DeviceFarm Project: %s", name)
out, err := conn.CreateProject(input)
if err != nil {
return fmt.Errorf("Error creating DeviceFarm Project: %s", err)
return fmt.Errorf("Error creating DeviceFarm Project: %w", err)
}

log.Printf("[DEBUG] Successsfully Created DeviceFarm Project: %s", *out.Project.Arn)
d.SetId(aws.StringValue(out.Project.Arn))
arn := aws.StringValue(out.Project.Arn)
log.Printf("[DEBUG] Successsfully Created DeviceFarm Project: %s", arn)
d.SetId(arn)

if len(tags) > 0 {
if err := keyvaluetags.DevicefarmUpdateTags(conn, arn, nil, tags); err != nil {
return fmt.Errorf("error updating DeviceFarm Project (%s) tags: %w", arn, err)
}
}

return resourceAwsDevicefarmProjectRead(d, meta)
}

func resourceAwsDevicefarmProjectRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).devicefarmconn
defaultTagsConfig := meta.(*AWSClient).DefaultTagsConfig
ignoreTagsConfig := meta.(*AWSClient).IgnoreTagsConfig

input := &devicefarm.GetProjectInput{
Arn: aws.String(d.Id()),
Expand All @@ -67,30 +93,64 @@ func resourceAwsDevicefarmProjectRead(d *schema.ResourceData, meta interface{})
d.SetId("")
return nil
}
return fmt.Errorf("Error reading DeviceFarm Project: %s", err)
return fmt.Errorf("Error reading DeviceFarm Project: %w", err)
}

project := out.Project
arn := aws.StringValue(project.Arn)
d.Set("name", project.Name)
d.Set("arn", arn)
d.Set("default_job_timeout_minutes", project.DefaultJobTimeoutMinutes)

tags, err := keyvaluetags.DevicefarmListTags(conn, arn)

if err != nil {
return fmt.Errorf("error listing tags for DeviceFarm Project (%s): %w", arn, err)
}

tags = tags.IgnoreAws().IgnoreConfig(ignoreTagsConfig)

//lintignore:AWSR002
if err := d.Set("tags", tags.RemoveDefaultConfig(defaultTagsConfig).Map()); err != nil {
return fmt.Errorf("error setting tags: %w", err)
}

d.Set("name", out.Project.Name)
d.Set("arn", out.Project.Arn)
if err := d.Set("tags_all", tags.Map()); err != nil {
return fmt.Errorf("error setting tags_all: %w", err)
}

return nil
}

func resourceAwsDevicefarmProjectUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).devicefarmconn

if d.HasChange("name") {
if d.HasChangesExcept("tags", "tags_all") {
input := &devicefarm.UpdateProjectInput{
Arn: aws.String(d.Id()),
Name: aws.String(d.Get("name").(string)),
Arn: aws.String(d.Id()),
}

if d.HasChange("name") {
input.Name = aws.String(d.Get("name").(string))
}

if d.HasChange("default_job_timeout_minutes") {
input.DefaultJobTimeoutMinutes = aws.Int64(int64(d.Get("default_job_timeout_minutes").(int)))
}

log.Printf("[DEBUG] Updating DeviceFarm Project: %s", d.Id())
_, err := conn.UpdateProject(input)
if err != nil {
return fmt.Errorf("Error Updating DeviceFarm Project: %s", err)
return fmt.Errorf("Error Updating DeviceFarm Project: %w", err)
}
}

if d.HasChange("tags_all") {
o, n := d.GetChange("tags_all")

if err := keyvaluetags.DevicefarmUpdateTags(conn, d.Get("arn").(string), o, n); err != nil {
return fmt.Errorf("error updating DeviceFarm Project (%s) tags: %w", d.Get("arn").(string), err)
}
}

return resourceAwsDevicefarmProjectRead(d, meta)
Expand All @@ -106,7 +166,10 @@ func resourceAwsDevicefarmProjectDelete(d *schema.ResourceData, meta interface{}
log.Printf("[DEBUG] Deleting DeviceFarm Project: %s", d.Id())
_, err := conn.DeleteProject(input)
if err != nil {
return fmt.Errorf("Error deleting DeviceFarm Project: %s", err)
if isAWSErr(err, devicefarm.ErrCodeNotFoundException, "") {
return nil
}
return fmt.Errorf("Error deleting DeviceFarm Project: %w", err)
}

return nil
Expand Down
137 changes: 137 additions & 0 deletions aws/resource_aws_devicefarm_project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
func TestAccAWSDeviceFarmProject_basic(t *testing.T) {
var proj devicefarm.Project
rName := acctest.RandomWithPrefix("tf-acc-test")
rNameUpdated := acctest.RandomWithPrefix("tf-acc-test-updated")
resourceName := "aws_devicefarm_project.test"

resource.ParallelTest(t, resource.TestCase{
Expand All @@ -35,6 +36,7 @@ func TestAccAWSDeviceFarmProject_basic(t *testing.T) {
Check: resource.ComposeTestCheckFunc(
testAccCheckDeviceFarmProjectExists(resourceName, &proj),
resource.TestCheckResourceAttr(resourceName, "name", rName),
resource.TestCheckResourceAttr(resourceName, "tags.%", "0"),
testAccMatchResourceAttrRegionalARN(resourceName, "arn", "devicefarm", regexp.MustCompile(`project:.+`)),
),
},
Expand All @@ -43,6 +45,107 @@ func TestAccAWSDeviceFarmProject_basic(t *testing.T) {
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccDeviceFarmProjectConfig(rNameUpdated),
Check: resource.ComposeTestCheckFunc(
testAccCheckDeviceFarmProjectExists(resourceName, &proj),
resource.TestCheckResourceAttr(resourceName, "name", rNameUpdated),
testAccMatchResourceAttrRegionalARN(resourceName, "arn", "devicefarm", regexp.MustCompile(`project:.+`)),
),
},
},
})
}

func TestAccAWSDeviceFarmProject_timeout(t *testing.T) {
var proj devicefarm.Project
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_devicefarm_project.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
testAccPartitionHasServicePreCheck(devicefarm.EndpointsID, t)
// Currently, DeviceFarm is only supported in us-west-2
// https://docs.aws.amazon.com/general/latest/gr/devicefarm.html
testAccRegionPreCheck(t, endpoints.UsWest2RegionID)
},
ErrorCheck: testAccErrorCheck(t, devicefarm.EndpointsID),
Providers: testAccProviders,
CheckDestroy: testAccCheckDeviceFarmProjectDestroy,
Steps: []resource.TestStep{
{
Config: testAccDeviceFarmProjectConfigDefaultJobTimeout(rName, 10),
Check: resource.ComposeTestCheckFunc(
testAccCheckDeviceFarmProjectExists(resourceName, &proj),
resource.TestCheckResourceAttr(resourceName, "name", rName),
resource.TestCheckResourceAttr(resourceName, "default_job_timeout_minutes", "10"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccDeviceFarmProjectConfigDefaultJobTimeout(rName, 20),
Check: resource.ComposeTestCheckFunc(
testAccCheckDeviceFarmProjectExists(resourceName, &proj),
resource.TestCheckResourceAttr(resourceName, "name", rName),
resource.TestCheckResourceAttr(resourceName, "default_job_timeout_minutes", "20"),
),
},
},
})
}

func TestAccAWSDeviceFarmProject_tags(t *testing.T) {
var proj devicefarm.Project
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_devicefarm_project.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
testAccPartitionHasServicePreCheck(devicefarm.EndpointsID, t)
// Currently, DeviceFarm is only supported in us-west-2
// https://docs.aws.amazon.com/general/latest/gr/devicefarm.html
testAccRegionPreCheck(t, endpoints.UsWest2RegionID)
},
ErrorCheck: testAccErrorCheck(t, devicefarm.EndpointsID),
Providers: testAccProviders,
CheckDestroy: testAccCheckDeviceFarmProjectDestroy,
Steps: []resource.TestStep{
{
Config: testAccDeviceFarmProjectConfigTags1(rName, "key1", "value1"),
Check: resource.ComposeTestCheckFunc(
testAccCheckDeviceFarmProjectExists(resourceName, &proj),
resource.TestCheckResourceAttr(resourceName, "tags.%", "1"),
resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccDeviceFarmProjectConfigTags2(rName, "key1", "value1updated", "key2", "value2"),
Check: resource.ComposeTestCheckFunc(
testAccCheckDeviceFarmProjectExists(resourceName, &proj),
resource.TestCheckResourceAttr(resourceName, "tags.%", "2"),
resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1updated"),
resource.TestCheckResourceAttr(resourceName, "tags.key2", "value2"),
),
},
{
Config: testAccDeviceFarmProjectConfigTags1(rName, "key2", "value2"),
Check: resource.ComposeTestCheckFunc(
testAccCheckDeviceFarmProjectExists(resourceName, &proj),
resource.TestCheckResourceAttr(resourceName, "tags.%", "1"),
resource.TestCheckResourceAttr(resourceName, "tags.key2", "value2"),
),
},
},
})
}
Expand Down Expand Up @@ -137,3 +240,37 @@ resource "aws_devicefarm_project" "test" {
}
`, rName)
}

func testAccDeviceFarmProjectConfigDefaultJobTimeout(rName string, timeout int) string {
return fmt.Sprintf(`
resource "aws_devicefarm_project" "test" {
name = %[1]q
default_job_timeout_minutes = %[2]d
}
`, rName, timeout)
}

func testAccDeviceFarmProjectConfigTags1(rName, tagKey1, tagValue1 string) string {
return fmt.Sprintf(`
resource "aws_devicefarm_project" "test" {
name = %[1]q

tags = {
%[2]q = %[3]q
}
}
`, rName, tagKey1, tagValue1)
}

func testAccDeviceFarmProjectConfigTags2(rName, tagKey1, tagValue1, tagKey2, tagValue2 string) string {
return fmt.Sprintf(`
resource "aws_devicefarm_project" "test" {
name = %[1]q

tags = {
%[2]q = %[3]q
%[4]q = %[5]q
}
}
`, rName, tagKey1, tagValue1, tagKey2, tagValue2)
}
3 changes: 3 additions & 0 deletions website/docs/r/devicefarm_project.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,15 @@ resource "aws_devicefarm_project" "awesome_devices" {
## Argument Reference

* `name` - (Required) The name of the project
* `default_job_timeout_minutes` - (Optional) Sets the execution timeout value (in minutes) for a project. All test runs in this project use the specified execution timeout value unless overridden when scheduling a run.
* `tags` - (Optional) A map of tags to assign to the resource. If configured with a provider [`default_tags` configuration block](/docs/providers/aws/index.html#default_tags-configuration-block) present, tags with matching keys will overwrite those defined at the provider-level.

## Attributes Reference

In addition to all arguments above, the following attributes are exported:

* `arn` - The Amazon Resource Name of this project
* `tags_all` - A map of tags assigned to the resource, including those inherited from the provider [`default_tags` configuration block](/docs/providers/aws/index.html#default_tags-configuration-block).

[aws-get-project]: http://docs.aws.amazon.com/devicefarm/latest/APIReference/API_GetProject.html

Expand Down