Skip to content

Commit

Permalink
add timout arg
Browse files Browse the repository at this point in the history
  • Loading branch information
DrFaust92 committed May 28, 2021
1 parent 77463f6 commit ea08426
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 20 deletions.
58 changes: 38 additions & 20 deletions aws/resource_aws_devicefarm_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ 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"
)

func resourceAwsDevicefarmProject() *schema.Resource {
Expand All @@ -26,8 +27,13 @@ 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,
},
},
}
Expand All @@ -36,18 +42,24 @@ func resourceAwsDevicefarmProject() *schema.Resource {
func resourceAwsDevicefarmProjectCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).devicefarmconn

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)

return resourceAwsDevicefarmProjectRead(d, meta)
}
Expand All @@ -67,30 +79,36 @@ 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)
}

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

return nil
}

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

input := &devicefarm.UpdateProjectInput{
Arn: aws.String(d.Id()),
}

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

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)
}
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: %w", err)
}

return resourceAwsDevicefarmProjectRead(d, meta)
Expand All @@ -106,7 +124,7 @@ 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)
return fmt.Errorf("Error deleting DeviceFarm Project: %w", err)
}

return nil
Expand Down
60 changes: 60 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 Down Expand Up @@ -43,6 +44,56 @@ 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"),
),
},
},
})
}
Expand Down Expand Up @@ -137,3 +188,12 @@ 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)
}

0 comments on commit ea08426

Please sign in to comment.