Skip to content

Commit

Permalink
provider/aws: add devicefarm project resource
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielMSchmidt committed May 9, 2017
1 parent d8108b6 commit cc42c6f
Show file tree
Hide file tree
Showing 2 changed files with 265 additions and 0 deletions.
112 changes: 112 additions & 0 deletions resource_aws_devicefarm_project.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package aws

import (
"fmt"
"log"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/devicefarm"
"github.com/hashicorp/terraform/helper/schema"
)

func resourceAwsDevicefarmProject() *schema.Resource {
return &schema.Resource{
Create: resourceAwsDevicefarmProjectCreate,
Read: resourceAwsDevicefarmProjectRead,
Update: resourceAwsDevicefarmProjectUpdate,
Delete: resourceAwsDevicefarmProjectDelete,

Schema: map[string]*schema.Schema{
"arn": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},

"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
},
}
}

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

// We need to ensure that DeviceFarm is only being run against us-west-2
// As this is the only place that AWS currently supports it
if region != "us-west-2" {
return fmt.Errorf("DeviceFarm can only be used with us-west-2. You are trying to use it on %s", region)
}

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

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

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

return resourceAwsDevicefarmProjectRead(d, meta)
}

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

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

log.Printf("[DEBUG] Reading DeviceFarm Project: %s", d.Id())
out, err := conn.GetProject(input)
if err != nil {
return fmt.Errorf("Error reading DeviceFarm Project: %s", err)
}

d.Set("name", out.Project.Name)
d.Set("arn", out.Project.Arn)

return nil
}

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

if d.HasChange("name") {
input := &devicefarm.UpdateProjectInput{
Arn: aws.String(d.Id()),
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)
}

}

return resourceAwsDevicefarmProjectRead(d, meta)
}

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

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

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 nil
}
153 changes: 153 additions & 0 deletions resource_aws_devicefarm_project_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
package aws

import (
"fmt"
"testing"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/devicefarm"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)

func TestAccAWSDeviceFarmProject_basic(t *testing.T) {
var v devicefarm.Project

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckDeviceFarmProjectDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccDeviceFarmProjectConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckDeviceFarmProjectExists(
"aws_devicefarm_project.foo", &v),
),
},
},
})
}

func TestAccAWSDeviceFarmProject_update(t *testing.T) {
var afterCreate, afterUpdate devicefarm.Project

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckDeviceFarmProjectDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccDeviceFarmProjectConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckDeviceFarmProjectExists(
"aws_devicefarm_project.foo", &afterCreate),
resource.TestCheckResourceAttr(
"aws_devicefarm_project.foo", "name", "tf-testproject-01"),
),
},

resource.TestStep{
Config: testAccDeviceFarmProjectConfigUpdate,
Check: resource.ComposeTestCheckFunc(
testAccCheckDeviceFarmProjectExists(
"aws_devicefarm_project.foo", &afterUpdate),
resource.TestCheckResourceAttr(
"aws_devicefarm_project.foo", "name", "tf-testproject-02"),
testAccCheckDeviceFarmProjectNotRecreated(
t, &afterCreate, &afterUpdate),
),
},
},
})
}

func testAccCheckDeviceFarmProjectNotRecreated(t *testing.T,
before, after *devicefarm.Project) resource.TestCheckFunc {
return func(s *terraform.State) error {
if *before.Arn != *after.Arn {
t.Fatalf("Expected DeviceFarm Project ARNs to be the same. But they were: %v, %v", *before.Arn, *after.Arn)
}
return nil
}
}

func testAccCheckDeviceFarmProjectExists(n string, v *devicefarm.Project) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}

if rs.Primary.ID == "" {
return fmt.Errorf("No ID is set")
}

conn := testAccProvider.Meta().(*AWSClient).devicefarmconn
resp, err := conn.GetProject(
&devicefarm.GetProjectInput{Arn: aws.String(rs.Primary.ID)})
if err != nil {
return err
}
if resp.Project == nil {
return fmt.Errorf("DeviceFarmProject not found")
}

*v = *resp.Project

return nil
}
}

func testAccCheckDeviceFarmProjectDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).devicefarmconn

for _, rs := range s.RootModule().Resources {
if rs.Type != "aws_devicefarm_project" {
continue
}

// Try to find the resource
resp, err := conn.GetProject(
&devicefarm.GetProjectInput{Arn: aws.String(rs.Primary.ID)})
if err == nil {
if resp.Project != nil {
return fmt.Errorf("still exist.")
}

return nil
}

// Verify the error is what we want
dferr, ok := err.(awserr.Error)
if !ok {
return err
}
if dferr.Code() != "DeviceFarmProjectNotFoundFault" {
return err
}
}

return nil
}

const testAccDeviceFarmProjectConfig = `
provider "aws" {
region = "us-west-2"
}
resource "aws_devicefarm_project" "foo" {
name = "tf-testproject-01"
}
`

const testAccDeviceFarmProjectConfigUpdate = `
provider "aws" {
region = "us-west-2"
}
resource "aws_devicefarm_project" "foo" {
name = "tf-testproject-02"
}
`

0 comments on commit cc42c6f

Please sign in to comment.