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_test_grid_project - new resource #22688

Merged
merged 3 commits into from
Jan 21, 2022
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
3 changes: 3 additions & 0 deletions .changelog/22688.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-resource
aws_devicefarm_test_grid_project
```
11 changes: 6 additions & 5 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -1010,11 +1010,12 @@ func Provider() *schema.Provider {
"aws_dax_parameter_group": dax.ResourceParameterGroup(),
"aws_dax_subnet_group": dax.ResourceSubnetGroup(),

"aws_devicefarm_device_pool": devicefarm.ResourceDevicePool(),
"aws_devicefarm_instance_profile": devicefarm.ResourceInstanceProfile(),
"aws_devicefarm_network_profile": devicefarm.ResourceNetworkProfile(),
"aws_devicefarm_project": devicefarm.ResourceProject(),
"aws_devicefarm_upload": devicefarm.ResourceUpload(),
"aws_devicefarm_device_pool": devicefarm.ResourceDevicePool(),
"aws_devicefarm_instance_profile": devicefarm.ResourceInstanceProfile(),
"aws_devicefarm_network_profile": devicefarm.ResourceNetworkProfile(),
"aws_devicefarm_project": devicefarm.ResourceProject(),
"aws_devicefarm_test_grid_project": devicefarm.ResourceTestGridProject(),
"aws_devicefarm_upload": devicefarm.ResourceUpload(),

"aws_detective_graph": detective.ResourceGraph(),
"aws_detective_invitation_accepter": detective.ResourceInvitationAccepter(),
Expand Down
25 changes: 25 additions & 0 deletions internal/service/devicefarm/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,28 @@ func FindInstanceProfileByArn(conn *devicefarm.DeviceFarm, arn string) (*devicef

return output.InstanceProfile, nil
}

func FindTestGridProjectByArn(conn *devicefarm.DeviceFarm, arn string) (*devicefarm.TestGridProject, error) {

input := &devicefarm.GetTestGridProjectInput{
ProjectArn: aws.String(arn),
}
output, err := conn.GetTestGridProject(input)

if tfawserr.ErrCodeEquals(err, devicefarm.ErrCodeNotFoundException) {
return nil, &resource.NotFoundError{
LastError: err,
LastRequest: input,
}
}

if err != nil {
return nil, err
}

if output == nil || output.TestGridProject == nil {
return nil, tfresource.NewEmptyResultError(input)
}

return output.TestGridProject, nil
}
59 changes: 59 additions & 0 deletions internal/service/devicefarm/sweep.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ func init() {
Name: "aws_devicefarm_project",
F: sweepProjects,
})

resource.AddTestSweepers("aws_devicefarm_test_grid_project", &resource.Sweeper{
Name: "aws_devicefarm_test_grid_project",
F: sweepTestGridProjects,
})
}

func sweepProjects(region string) error {
Expand Down Expand Up @@ -75,3 +80,57 @@ func sweepProjects(region string) error {

return errs.ErrorOrNil()
}

func sweepTestGridProjects(region string) error {
client, err := sweep.SharedRegionalSweepClient(region)

if err != nil {
return fmt.Errorf("error getting client: %w", err)
}

conn := client.(*conns.AWSClient).DeviceFarmConn
sweepResources := make([]*sweep.SweepResource, 0)
var errs *multierror.Error

input := &devicefarm.ListTestGridProjectsInput{}

err = conn.ListTestGridProjectsPages(input, func(page *devicefarm.ListTestGridProjectsOutput, lastPage bool) bool {
if page == nil {
return !lastPage
}

for _, project := range page.TestGridProjects {
r := ResourceTestGridProject()
d := r.Data(nil)

id := aws.StringValue(project.Arn)
d.SetId(id)

if err != nil {
err := fmt.Errorf("error reading DeviceFarm Project (%s): %w", id, err)
log.Printf("[ERROR] %s", err)
errs = multierror.Append(errs, err)
continue
}

sweepResources = append(sweepResources, sweep.NewSweepResource(r, d, client))
}

return !lastPage
})

if err != nil {
errs = multierror.Append(errs, fmt.Errorf("error listing DeviceFarm Test Grid Project for %s: %w", region, err))
}

if err := sweep.SweepOrchestrator(sweepResources); err != nil {
errs = multierror.Append(errs, fmt.Errorf("error sweeping DeviceFarm Test Grid Project for %s: %w", region, err))
}

if sweep.SkipSweepError(err) {
log.Printf("[WARN] Skipping DeviceFarm Test Grid Project sweep for %s: %s", region, errs)
return nil
}

return errs.ErrorOrNil()
}
239 changes: 239 additions & 0 deletions internal/service/devicefarm/test_grid_project.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,239 @@
package devicefarm

import (
"fmt"
"log"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/devicefarm"
"github.com/hashicorp/aws-sdk-go-base/tfawserr"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/flex"
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
"github.com/hashicorp/terraform-provider-aws/internal/verify"
)

func ResourceTestGridProject() *schema.Resource {
return &schema.Resource{
Create: resourceTestGridProjectCreate,
Read: resourceTestGridProjectRead,
Update: resourceTestGridProjectUpdate,
Delete: resourceTestGridProjectDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

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

"name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringLenBetween(0, 256),
},
"description": {
Type: schema.TypeString,
Optional: true,
},
"tags": tftags.TagsSchema(),
"tags_all": tftags.TagsSchemaComputed(),
"vpc_config": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"security_group_ids": {
Type: schema.TypeSet,
MinItems: 1,
Elem: &schema.Schema{Type: schema.TypeString},
Required: true,
},
"subnet_ids": {
Type: schema.TypeSet,
MinItems: 1,
Elem: &schema.Schema{Type: schema.TypeString},
Required: true,
},
"vpc_id": {
Type: schema.TypeString,
Required: true,
},
},
},
},
},
CustomizeDiff: verify.SetTagsDiff,
}
}

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

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

if v, ok := d.GetOk("description"); ok {
input.Description = aws.String(v.(string))
}

if v, ok := d.GetOk("vpc_config"); ok {
input.VpcConfig = expandTestGridProjectVpcConfig(v.([]interface{}))
}

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

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

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

return resourceTestGridProjectRead(d, meta)
}

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

project, err := FindTestGridProjectByArn(conn, d.Id())

if !d.IsNewResource() && tfresource.NotFound(err) {
log.Printf("[WARN] DeviceFarm Test Grid Project (%s) not found, removing from state", d.Id())
d.SetId("")
return nil
}

if err != nil {
return fmt.Errorf("error reading DeviceFarm Test Grid Project (%s): %w", d.Id(), err)
}

arn := aws.StringValue(project.Arn)
d.Set("name", project.Name)
d.Set("arn", arn)
d.Set("description", project.Description)
if err := d.Set("vpc_config", flattenTestGridProjectVpcConfig(project.VpcConfig)); err != nil {
return fmt.Errorf("error setting vpc_config: %w", err)
}

tags, err := ListTags(conn, arn)

if err != nil {
return fmt.Errorf("error listing tags for DeviceFarm Test Grid 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)
}

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

return nil
}

func resourceTestGridProjectUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*conns.AWSClient).DeviceFarmConn

if d.HasChangesExcept("tags", "tags_all") {
input := &devicefarm.UpdateTestGridProjectInput{
ProjectArn: aws.String(d.Id()),
}

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

if d.HasChange("description") {
input.Description = aws.String(d.Get("description").(string))
}

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

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

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

return resourceTestGridProjectRead(d, meta)
}

func resourceTestGridProjectDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*conns.AWSClient).DeviceFarmConn

input := &devicefarm.DeleteTestGridProjectInput{
ProjectArn: aws.String(d.Id()),
}

log.Printf("[DEBUG] Deleting DeviceFarm Test Grid Project: %s", d.Id())
_, err := conn.DeleteTestGridProject(input)
if err != nil {
if tfawserr.ErrCodeEquals(err, devicefarm.ErrCodeNotFoundException) {
return nil
}
return fmt.Errorf("Error deleting DeviceFarm Test Grid Project: %w", err)
}

return nil
}

func expandTestGridProjectVpcConfig(l []interface{}) *devicefarm.TestGridVpcConfig {
if len(l) == 0 {
return nil
}

m := l[0].(map[string]interface{})

config := &devicefarm.TestGridVpcConfig{
VpcId: aws.String(m["vpc_id"].(string)),
SubnetIds: flex.ExpandStringSet(m["subnet_ids"].(*schema.Set)),
SecurityGroupIds: flex.ExpandStringSet(m["security_group_ids"].(*schema.Set)),
}

return config
}

func flattenTestGridProjectVpcConfig(conf *devicefarm.TestGridVpcConfig) []interface{} {
if conf == nil {
return []interface{}{}
}

m := map[string]interface{}{
"vpc_id": aws.StringValue(conf.VpcId),
"subnet_ids": flex.FlattenStringSet(conf.SubnetIds),
"security_group_ids": flex.FlattenStringSet(conf.SecurityGroupIds),
}

return []interface{}{m}
}
Loading