-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(resource): lacework_external_id (#556)
* feat(data_source): lacework_external_id Signed-off-by: Salim Afiune Maya <afiune@lacework.net> --------- Signed-off-by: Salim Afiune Maya <afiune@lacework.net> Co-authored-by: David McTavish <david.mctavish@lacework.net>
- Loading branch information
Showing
6 changed files
with
165 additions
and
2 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,37 @@ | ||
--- | ||
subcategory: "Other Resources" | ||
layout: "lacework" | ||
page_title: "Lacework: lacework_external_id" | ||
description: |- | ||
Generates an External ID using Lacework format. | ||
--- | ||
|
||
# lacework\_external\_id | ||
|
||
This resource generates an External ID (EID) using Lacework format. These IDs are used to create integrations. | ||
|
||
The v2 format is: | ||
``` | ||
lweid:<csp>:<version>:<lw_tenant_name>:<aws_acct_id>:<random_string_size_10> | ||
``` | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
resource "lacework_external_id" "aws_123456789012" { | ||
csp = "aws" | ||
account_id = "123456789012" | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
* `csp` - (Required) The Cloud Service Provider. Valid CSP's include: `aws`, `google`, `oci`, and `azure`. | ||
* `account_id` - (Required) The account id from the CSP to be integrated. | ||
|
||
## Attribute Reference | ||
|
||
The following attributes are exported: | ||
|
||
* `v2` - The generated External ID version 2. | ||
|
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,27 @@ | ||
terraform { | ||
required_providers { | ||
lacework = { | ||
source = "lacework/lacework" | ||
} | ||
} | ||
} | ||
|
||
provider "lacework" {} | ||
|
||
# Format | ||
# | ||
# lweid:<csp>:<version>:<lw_tenant_name>:<aws_acct_id>:<random_string_size_10> | ||
# | ||
resource "lacework_external_id" "aws_123456789012" { | ||
csp = "aws" | ||
account_id = "123456789012" | ||
} | ||
|
||
# Example output | ||
# | ||
# lweid:aws:v2:customerdemo:123456789012:dkl31.09ip | ||
# | ||
output "external_id" { | ||
value = lacework_external_id.aws_123456789012.v2 | ||
} | ||
|
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,21 @@ | ||
package integration | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/gruntwork-io/terratest/modules/terraform" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
// TestExternalIdCreate uses the Terraform plan at: | ||
// => '../examples/resource_lacework_external_id' | ||
func TestExternalIdCreate(t *testing.T) { | ||
terraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{ | ||
TerraformDir: "../examples/resource_lacework_external_id", | ||
}) | ||
defer terraform.Destroy(t, terraformOptions) | ||
|
||
terraform.InitAndApplyAndIdempotent(t, terraformOptions) | ||
externalId := terraform.Output(t, terraformOptions, "external_id") | ||
assert.NotEmpty(t, externalId) | ||
} |
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
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,72 @@ | ||
package lacework | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" | ||
"github.com/pkg/errors" | ||
|
||
"github.com/lacework/go-sdk/api" | ||
"github.com/lacework/go-sdk/lwdomain" | ||
) | ||
|
||
var externalIDValidCsp = []string{"aws", "google", "oci", "azure"} | ||
|
||
func resourceLaceworkExternalID() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: schema.Noop, | ||
Create: resourceLaceworkExternalIDCreate, | ||
Delete: schema.Noop, | ||
Schema: map[string]*schema.Schema{ | ||
"csp": { | ||
Type: schema.TypeString, | ||
Description: fmt.Sprintf("The Cloud Service Provider (%s)", strings.Join(externalIDValidCsp, ", ")), | ||
ValidateFunc: validation.StringInSlice(externalIDValidCsp, false), | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"account_id": { | ||
Type: schema.TypeString, | ||
Description: "The account id from the CSP to be integrated", | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
"v2": { | ||
Type: schema.TypeString, | ||
Description: "Generated EID version 2 ('lweid:<csp>:<version>:<lw_tenant_name>:<aws_acct_id>:<random_string_size_10>')", | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceLaceworkExternalIDCreate(d *schema.ResourceData, meta interface{}) error { | ||
lacework := meta.(*api.Client) | ||
url, err := lwdomain.New(lacework.URL()) | ||
if err != nil { | ||
return errors.Wrap(err, "Unable to get the Lacework account") | ||
} | ||
|
||
// EID V2 Format | ||
// | ||
// lweid:<csp>:<version>:<lw_tenant_name>:<aws_acct_id>:<random_string_size_10> | ||
// | ||
var ( | ||
version = "v2" | ||
EID = strings.Join([]string{ | ||
"lweid", | ||
d.Get("csp").(string), | ||
version, | ||
url.Account, | ||
d.Get("account_id").(string), | ||
randomStringExternalID(10), | ||
}, ":") | ||
) | ||
|
||
d.SetId(EID) | ||
d.Set("v2", EID) | ||
|
||
return nil | ||
} |