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

feat: retry mechanism for all cloud resources #81

Merged
merged 8 commits into from
Mar 8, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 2 additions & 0 deletions examples/resource_lacework_integration_aws_cfg/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ resource "lacework_integration_aws_cfg" "example" {
role_arn = "arn:aws:iam::1234567890:role/lacework_iam_example_role"
external_id = "12345"
}

retries = 10
}
6 changes: 0 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@ go 1.15
require (
github.com/BurntSushi/toml v0.3.1
github.com/agext/levenshtein v1.2.3 // indirect
github.com/apparentlymart/go-cidr v1.1.0 // indirect
github.com/aws/aws-sdk-go v1.33.3 // indirect
github.com/hashicorp/go-multierror v1.1.0 // indirect
github.com/hashicorp/go-uuid v1.0.2 // indirect
github.com/hashicorp/hcl/v2 v2.6.0 // indirect
github.com/hashicorp/terraform-plugin-sdk/v2 v2.4.3
github.com/hashicorp/terraform-svchost v0.0.0-20191119180714-d2e4933b9136 // indirect
github.com/hashicorp/yamux v0.0.0-20200609203250-aecfd211c9ce // indirect
github.com/lacework/go-sdk v0.2.21-0.20210224193400-129bc2861f7b
github.com/mattn/go-colorable v0.1.7 // indirect
Expand All @@ -20,12 +18,8 @@ require (
github.com/mitchellh/mapstructure v1.3.2 // indirect
github.com/oklog/run v1.1.0 // indirect
github.com/pkg/errors v0.9.1
github.com/posener/complete v1.2.3 // indirect
github.com/spf13/afero v1.3.1 // indirect
github.com/stretchr/testify v1.7.0
github.com/vmihailenco/msgpack v4.0.4+incompatible // indirect
github.com/zclconf/go-cty v1.5.1 // indirect
github.com/zclconf/go-cty-yaml v1.0.2 // indirect
go.uber.org/zap v1.15.0 // indirect
golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae // indirect
)
67 changes: 1 addition & 66 deletions go.sum

Large diffs are not rendered by default.

72 changes: 45 additions & 27 deletions lacework/resource_lacework_integration_aws_cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"log"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

"github.com/lacework/go-sdk/api"
Expand Down Expand Up @@ -34,6 +35,11 @@ func resourceLaceworkIntegrationAwsCfg() *schema.Resource {
Optional: true,
Default: true,
},
"retries": {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@scottford-lw @dmurray-lacework Should this be max_retries instead? 🤔

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, i think that better describes this parameters function.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

max_retries seems to be more the common nomenclature. At least from a quick search.
https://registry.terraform.io/providers/hashicorp/aws/latest/docs#max_retries

Not a sticking point for me and happy for this to go with either. But I do prefer max_retries

Type: schema.TypeInt,
Optional: true,
Default: 5,
},
"credentials": {
Type: schema.TypeList,
MaxItems: 1,
Expand Down Expand Up @@ -73,8 +79,10 @@ func resourceLaceworkIntegrationAwsCfg() *schema.Resource {

func resourceLaceworkIntegrationAwsCfgCreate(d *schema.ResourceData, meta interface{}) error {
var (
lacework = meta.(*api.Client)
aws = api.NewAwsIntegration(d.Get("name").(string),
lacework = meta.(*api.Client)
retries = 0
maxretries = d.Get("retries").(int)
aws = api.NewAwsIntegration(d.Get("name").(string),
api.AwsCfgIntegration,
api.AwsIntegrationData{
Credentials: api.AwsIntegrationCreds{
Expand All @@ -84,37 +92,47 @@ func resourceLaceworkIntegrationAwsCfgCreate(d *schema.ResourceData, meta interf
},
)
)

if !d.Get("enabled").(bool) {
aws.Enabled = 0
}

// @afiune should we do this if there is sensitive information?
log.Printf("[INFO] Creating %s integration with data:\n%+v\n", api.AwsCfgIntegration.String(), aws)
response, err := lacework.Integrations.CreateAws(aws)
if err != nil {
return err
}

log.Println("[INFO] Verifying server response data")
err = validateAwsIntegrationResponse(&response)
if err != nil {
return err
}

// @afiune at this point of time, we know the data field has a single value
integration := response.Data[0]
d.SetId(integration.IntgGuid)
d.Set("name", integration.Name)
d.Set("intg_guid", integration.IntgGuid)
d.Set("enabled", integration.Enabled == 1)
return resource.Retry(d.Timeout(schema.TimeoutCreate), func() *resource.RetryError {
log.Printf("[INFO] Creating %s integration (retry:%d)\n", api.AwsCfgIntegration.String(), retries)
response, err := lacework.Integrations.CreateAws(aws)
if err != nil {
if retries >= maxretries {
return resource.NonRetryableError(fmt.Errorf("Error creating %s integration: %s", api.AwsCfgIntegration.String(), err))
}
retries++
log.Printf("[INFO] Unable to create %s integration: \n%s\n", api.AwsCfgIntegration.String(), err)
return resource.RetryableError(fmt.Errorf(
"Unable to create %s integration (retrying %d of %d)",
api.AwsCfgIntegration.String(), retries, maxretries,
))
}

d.Set("created_or_updated_time", integration.CreatedOrUpdatedTime)
d.Set("created_or_updated_by", integration.CreatedOrUpdatedBy)
d.Set("type_name", integration.TypeName)
d.Set("org_level", integration.IsOrg == 1)
log.Printf("[INFO] Verifying server response.\n%v\n", response)
err = validateAwsIntegrationResponse(&response)
if err != nil {
return resource.NonRetryableError(err)
}

log.Printf("[INFO] Created %s integration with guid: %v\n", api.AwsCfgIntegration.String(), integration.IntgGuid)
return nil
// @afiune at this point of time, we know the data field has a single value
integration := response.Data[0]
d.SetId(integration.IntgGuid)
d.Set("name", integration.Name)
d.Set("intg_guid", integration.IntgGuid)
d.Set("enabled", integration.Enabled == 1)

d.Set("created_or_updated_time", integration.CreatedOrUpdatedTime)
d.Set("created_or_updated_by", integration.CreatedOrUpdatedBy)
d.Set("type_name", integration.TypeName)
d.Set("org_level", integration.IsOrg == 1)

log.Printf("[INFO] Created %s integration with guid: %v\n", api.AwsCfgIntegration.String(), integration.IntgGuid)
return resource.NonRetryableError(nil)
})
}

func resourceLaceworkIntegrationAwsCfgRead(d *schema.ResourceData, meta interface{}) error {
Expand Down
19 changes: 0 additions & 19 deletions vendor/github.com/apparentlymart/go-cidr/LICENSE

This file was deleted.

236 changes: 0 additions & 236 deletions vendor/github.com/apparentlymart/go-cidr/cidr/cidr.go

This file was deleted.

Loading