provider/aws: add devicefarm project resource

This commit is contained in:
Daniel Schmidt 2017-05-09 09:48:44 +02:00
parent 995121f9ce
commit 6a7e81d8ae
3 changed files with 301 additions and 0 deletions

View File

@ -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
}

View File

@ -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"
}
`

View File

@ -0,0 +1,36 @@
---
layout: "aws"
page_title: "AWS: aws_devicefarm_project"
sidebar_current: "docs-aws-resource-devicefarm-project"
description: |-
Provides a Devicefarm project
---
# aws\_devicefarm\_project
Provides a resource to manage AWS Device Farm Projects.
Please keep in mind that this feature is only supported on the "us-west-2" region.
This resource will error if you try to create a project in another region.
For more information about Device Farm Projects, see the AWS Documentation on
[Device Farm Projects][aws-get-project].
## Basic Example Usage
```hcl
resource "aws_devicefarm_project" "awesome_devices"
```
## Argument Reference
There are currently no other arguments besides the name supported.
## Attributes Reference
The following attributes are exported:
* `name` - The name of the project
* `arn` - The Amazon Resource Name of this project (computed)
[aws-get-project]: http://docs.aws.amazon.com/devicefarm/latest/APIReference/API_GetProject.html