-
Notifications
You must be signed in to change notification settings - Fork 9.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22042 from coderGo93/detective-graph2
New resource for Detective Graph
- Loading branch information
Showing
9 changed files
with
547 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
```release-note:new-resource | ||
aws_detective_graph | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.