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

New resource for Detective Graph #22042

Merged
merged 10 commits into from
Dec 9, 2021
3 changes: 3 additions & 0 deletions .changelog/22042.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-resource
aws_detective_graph
```
3 changes: 3 additions & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import (
"github.com/hashicorp/terraform-provider-aws/internal/service/datapipeline"
"github.com/hashicorp/terraform-provider-aws/internal/service/datasync"
"github.com/hashicorp/terraform-provider-aws/internal/service/dax"
"github.com/hashicorp/terraform-provider-aws/internal/service/detective"
"github.com/hashicorp/terraform-provider-aws/internal/service/devicefarm"
"github.com/hashicorp/terraform-provider-aws/internal/service/directconnect"
"github.com/hashicorp/terraform-provider-aws/internal/service/dlm"
Expand Down Expand Up @@ -970,6 +971,8 @@ func Provider() *schema.Provider {

"aws_devicefarm_project": devicefarm.ResourceProject(),

"aws_detective_graph": detective.ResourceGraph(),

"aws_dx_bgp_peer": directconnect.ResourceBGPPeer(),
"aws_dx_connection": directconnect.ResourceConnection(),
"aws_dx_connection_association": directconnect.ResourceConnectionAssociation(),
Expand Down
52 changes: 52 additions & 0 deletions internal/service/detective/find.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package detective

import (
"context"
"fmt"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/detective"
"github.com/hashicorp/aws-sdk-go-base/tfawserr"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func FindDetectiveGraphByArn(conn *detective.Detective, ctx context.Context, arn string) (*detective.Graph, error) {
input := &detective.ListGraphsInput{}
var result *detective.Graph

err := conn.ListGraphsPagesWithContext(ctx, input, func(page *detective.ListGraphsOutput, lastPage bool) bool {
if page == nil {
return !lastPage
}

for _, graph := range page.GraphList {
if graph == nil {
continue
}

if aws.StringValue(graph.Arn) == arn {
result = graph
return false
}
}
return !lastPage
})
if tfawserr.ErrCodeEquals(err, detective.ErrCodeResourceNotFoundException) {
return nil, &resource.NotFoundError{
LastError: err,
LastRequest: input,
}
}
if err != nil {
return nil, err
}

if result == nil {
return nil, &resource.NotFoundError{
Message: fmt.Sprintf("No detective graph with arn %q", arn),
LastRequest: input,
}
}

return result, nil
}
4 changes: 4 additions & 0 deletions internal/service/detective/generate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
//go:generate go run ../../generate/tags/main.go -ServiceTagsMap -ListTags -UpdateTags
// ONLY generate directives and package declaration! Do not add anything else to this file.

package detective
151 changes: 151 additions & 0 deletions internal/service/detective/graph.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
package detective

import (
"context"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/detective"
"github.com/hashicorp/aws-sdk-go-base/tfawserr"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
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 ResourceGraph() *schema.Resource {
return &schema.Resource{
CreateContext: resourceGraphCreate,
ReadContext: resourceGraphRead,
UpdateContext: resourceGraphUpdate,
DeleteContext: resourceGraphDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"created_time": {
Type: schema.TypeString,
Computed: true,
},
"graph_arn": {
Type: schema.TypeString,
Computed: true,
},
"tags": tftags.TagsSchema(),
"tags_all": tftags.TagsSchemaComputed(),
},
CustomizeDiff: verify.SetTagsDiff,
}
}

func resourceGraphCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).DetectiveConn

defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
tags := defaultTagsConfig.MergeTags(tftags.New(d.Get("tags").(map[string]interface{})))

input := &detective.CreateGraphInput{}

if len(tags) > 0 {
input.Tags = Tags(tags.IgnoreAWS())
}

var output *detective.CreateGraphOutput
var err error
err = resource.RetryContext(ctx, DetectiveOperationTimeout, func() *resource.RetryError {
output, err = conn.CreateGraphWithContext(ctx, input)
if err != nil {
if tfawserr.ErrCodeEquals(err, detective.ErrCodeInternalServerException) {
return resource.RetryableError(err)
}

return resource.NonRetryableError(err)
}

return nil
})

if tfresource.TimedOut(err) {
output, err = conn.CreateGraphWithContext(ctx, input)
}

if err != nil {
return diag.Errorf("error creating detective Graph: %s", err)
}

d.SetId(aws.StringValue(output.GraphArn))

return resourceGraphRead(ctx, d, meta)
}

func resourceGraphRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).DetectiveConn

defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig

resp, err := FindDetectiveGraphByArn(conn, ctx, d.Id())

if !d.IsNewResource() && tfawserr.ErrCodeEquals(err, detective.ErrCodeResourceNotFoundException) || resp == nil {
d.SetId("")
return nil
}
if err != nil {
return diag.Errorf("error reading detective Graph (%s): %s", d.Id(), err)
}

d.Set("created_time", aws.TimeValue(resp.CreatedTime).Format(time.RFC3339))
d.Set("graph_arn", resp.Arn)

tags, err := ListTags(conn, aws.StringValue(resp.Arn))

if err != nil {
return diag.Errorf("error listing tags for Detective Graph (%s): %s", d.Id(), err)
}

tags = tags.IgnoreAWS().IgnoreConfig(ignoreTagsConfig)

if err = d.Set("tags", tags.RemoveDefaultConfig(defaultTagsConfig).Map()); err != nil {
return diag.Errorf("error setting `%s` for Detective Graph (%s): %s", "tags", d.Id(), err)
}

if err = d.Set("tags_all", tags.Map()); err != nil {
return diag.Errorf("error setting `%s` for Detective Graph (%s): %s", "tags_all", d.Id(), err)
}

return nil
}

func resourceGraphUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).DetectiveConn

if d.HasChange("tags") {
o, n := d.GetChange("tags")
if err := UpdateTags(conn, d.Id(), o, n); err != nil {
return diag.Errorf("error updating detective Graph tags (%s): %s", d.Id(), err)
}
}

return resourceGraphRead(ctx, d, meta)
}

func resourceGraphDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
conn := meta.(*conns.AWSClient).DetectiveConn

input := &detective.DeleteGraphInput{
GraphArn: aws.String(d.Id()),
}

_, err := conn.DeleteGraphWithContext(ctx, input)
if err != nil {
if tfawserr.ErrCodeEquals(err, detective.ErrCodeResourceNotFoundException) {
return nil
}
return diag.Errorf("error deleting detective Graph (%s): %s", d.Id(), err)
}

return nil
}
Loading