Skip to content

Commit

Permalink
refactor: migrate lacework_integration_ecr to use v2 api (#364)
Browse files Browse the repository at this point in the history
* refactor: migrate lacework_integration_ecr to use v2 api

Signed-off-by: Darren Murray <darren.murray@lacework.net>
  • Loading branch information
dmurray-lacework authored Sep 21, 2022
1 parent 3f2c581 commit 88ee9bf
Show file tree
Hide file tree
Showing 5 changed files with 241 additions and 403 deletions.
8 changes: 4 additions & 4 deletions integration/integration.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,15 @@ func GetIntegrationName(result string, integration string) string {
return res.Data.Name
}

func GetEcrWithCrossAccountCreds(result string) api.AwsEcrWithCrossAccountIntegration {
func GetEcrWithCrossAccountCreds(result string) api.AwsEcrIamRoleIntegration {
id := GetIDFromTerraResults(result)

res, err := LwClient.Integrations.GetAwsEcrWithCrossAccount(id)
if err != nil || len(res.Data) == 0 {
res, err := LwClient.V2.ContainerRegistries.GetAwsEcrIamRole(id)
if err != nil {
log.Fatalf("Unable to find integration id: %s\n Response: %v", id, res)
}

return res.Data[0]
return res.Data
}

func GetContainerRegistryIntegration(result string) api.ContainerRegIntegration {
Expand Down
14 changes: 7 additions & 7 deletions integration/resource_lacework_integration_ecr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

// TestIntegrationECRCreate applies integration terraform:
// => '../examples/resource_lacework_integration_gar'
// => '../examples/resource_lacework_integration_ecr/iam_role'
//
// It uses the go-sdk to verify the created integration,
// applies an update with new integration name and destroys it
Expand All @@ -32,10 +32,10 @@ func TestIntegrationECRCreate(t *testing.T) {
create := terraform.InitAndApplyAndIdempotent(t, terraformOptions)
createData := GetEcrWithCrossAccountCreds(create)
assert.Equal(t, "Amazon Elastic Container Registry Example", createData.Name)
assert.Equal(t, awsCreds.RoleArn, createData.Data.Credentials.RoleArn)
assert.Equal(t, awsCreds.ExternalID, createData.Data.Credentials.ExternalID)
assert.Equal(t, awsCreds.RoleArn, createData.Data.CrossAccountCredentials.RoleArn)
assert.Equal(t, awsCreds.ExternalID, createData.Data.CrossAccountCredentials.ExternalID)
assert.Equal(t, awsCreds.RegistryDomain, createData.Data.RegistryDomain)
assert.Equal(t, true, createData.Data.AwsEcrCommonData.NonOSPackageEval)
assert.Equal(t, true, createData.Data.NonOSPackageEval)

terraformOptions.Vars["integration_name"] = "Amazon Elastic Container Registry Updated"
terraformOptions.Vars["non_os_package_support"] = true
Expand All @@ -44,10 +44,10 @@ func TestIntegrationECRCreate(t *testing.T) {
updateData := GetEcrWithCrossAccountCreds(update)

assert.Equal(t, "Amazon Elastic Container Registry Updated", updateData.Name)
assert.Equal(t, awsCreds.RoleArn, updateData.Data.Credentials.RoleArn)
assert.Equal(t, awsCreds.ExternalID, updateData.Data.Credentials.ExternalID)
assert.Equal(t, awsCreds.RoleArn, updateData.Data.CrossAccountCredentials.RoleArn)
assert.Equal(t, awsCreds.ExternalID, updateData.Data.CrossAccountCredentials.ExternalID)
assert.Equal(t, awsCreds.RegistryDomain, updateData.Data.RegistryDomain)
assert.Equal(t, true, updateData.Data.AwsEcrCommonData.NonOSPackageEval)
assert.Equal(t, true, updateData.Data.NonOSPackageEval)
}
}

Expand Down
16 changes: 16 additions & 0 deletions lacework/casting.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,22 @@ func castAttributeToArrayOfKeyValueMap(d *schema.ResourceData, attr string) []ma
return aMap
}

// extract an attribute from the provided ResourceData and convert it into a map of strings
// with string keys
func castAttributeToArrayKeyMapOfStrings(d *schema.ResourceData, attr string) []map[string]string {
if castMap, ok := d.Get(attr).(map[string]interface{}); ok {
mapList := make([]map[string]string, 1)
stringMap := make(map[string]string)
for key, val := range castMap {
stringMap[key] = val.(string)
}
mapList[0] = stringMap
return mapList
}

return []map[string]string{}
}

func castAttributeToArrayOfCustomKeyValueMap(d *schema.ResourceData, attr string, key string, value string) []map[string]string {
list := d.Get(attr).(*schema.Set).List()
aMap := make([]map[string]string, len(list))
Expand Down
Loading

0 comments on commit 88ee9bf

Please sign in to comment.