Skip to content

Commit

Permalink
refactor: migrate resource_lacework_integration_docker_hub to use v2 …
Browse files Browse the repository at this point in the history
…api (#378)

* refactor: migrate resource_lacework_integration_docker_hub to use v2 api
  • Loading branch information
dmurray-lacework authored and afiune committed Nov 8, 2022
1 parent b6ff8f8 commit 0e916c8
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 160 deletions.
224 changes: 67 additions & 157 deletions lacework/resource_lacework_integration_docker_hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,34 +43,6 @@ func resourceLaceworkIntegrationDockerHub() *schema.Resource {
Sensitive: true,
Description: "The password for the specified Docker Hub user",
},

// TODO @afiune remove these resources when we release v1.0
"limit_by_tag": {
Type: schema.TypeString,
Optional: true,
Default: "*",
Description: "A comma-separated list of image tags to limit the assessment of images with matching tags",
Deprecated: "This attribute will be replaced by a new attribute `limit_by_tags` in version 1.0 of the Lacework provider",
ConflictsWith: []string{"limit_by_tags"},
},
"limit_by_label": {
Type: schema.TypeString,
Optional: true,
Default: "*",
Description: "A comma-separated list of image labels to limit the assessment of images with matching labels",
Deprecated: "This attribute will be replaced by a new attribute `limit_by_labels` in version 1.0 of the Lacework provider",
ConflictsWith: []string{"limit_by_labels"},
},

"limit_by_repos": {
Type: schema.TypeString,
Optional: true,
Description: "A comma-separated list of repositories to assess",
Deprecated: "This attribute will be replaced by a new attribute `limit_by_repositories` in version 1.0 of the Lacework provider",
ConflictsWith: []string{"limit_by_repositories"},
},
// END TODO @afiune

"limit_by_tags": {
Type: schema.TypeList,
Elem: &schema.Schema{
Expand All @@ -79,9 +51,8 @@ func resourceLaceworkIntegrationDockerHub() *schema.Resource {
return strings.TrimSpace(val.(string))
},
},
Optional: true,
Description: "A list of image tags to limit the assessment of images with matching tags",
ConflictsWith: []string{"limit_by_tag"},
Optional: true,
Description: "A list of image tags to limit the assessment of images with matching tags",
},
"limit_by_labels": {
Type: schema.TypeMap,
Expand All @@ -91,9 +62,8 @@ func resourceLaceworkIntegrationDockerHub() *schema.Resource {
return strings.TrimSpace(val.(string))
},
},
Optional: true,
Description: "A key based map of labels to limit the assessment of images with matching key:value labels",
ConflictsWith: []string{"limit_by_label"},
Optional: true,
Description: "A key based map of labels to limit the assessment of images with matching key:value labels",
},
"limit_by_repositories": {
Type: schema.TypeList,
Expand All @@ -103,9 +73,8 @@ func resourceLaceworkIntegrationDockerHub() *schema.Resource {
return strings.TrimSpace(val.(string))
},
},
Optional: true,
Description: "A list of repositories to assess",
ConflictsWith: []string{"limit_by_repos"},
Optional: true,
Description: "A list of repositories to assess",
},
"limit_num_imgs": {
Type: schema.TypeInt,
Expand Down Expand Up @@ -145,30 +114,15 @@ func resourceLaceworkIntegrationDockerHub() *schema.Resource {

func resourceLaceworkIntegrationDockerHubCreate(d *schema.ResourceData, meta interface{}) error {
lacework := meta.(*api.Client)

limitByTags := d.Get("limit_by_tag").(string)
if tags := castAttributeToStringSlice(d, "limit_by_tags"); len(tags) != 0 {
limitByTags = strings.Join(tags, ",")
}

limitByLabels := d.Get("limit_by_label").(string)
if labels := castAttributeToStringKeyMapOfStrings(d, "limit_by_labels"); len(labels) != 0 {
limitByLabels = joinMapStrings(labels, ",")
}

limitByRepos := d.Get("limit_by_repos").(string)
if repos := castAttributeToStringSlice(d, "limit_by_repositories"); len(repos) != 0 {
limitByRepos = strings.Join(repos, ",")
}

data := api.NewDockerHubRegistryIntegration(d.Get("name").(string),
api.ContainerRegData{
LimitByTag: limitByTags,
LimitByLabel: limitByLabels,
LimitByRep: limitByRepos,
data := api.NewContainerRegistry(d.Get("name").(string),
api.DockerhubContainerRegistry,
api.DockerhubData{
LimitByTag: castAttributeToStringSlice(d, "limit_by_tags"),
LimitByRep: castAttributeToStringSlice(d, "limit_by_repositories"),
LimitByLabel: castAttributeToArrayKeyMapOfStrings(d, "limit_by_labels"),
LimitNumImg: d.Get("limit_num_imgs").(int),
NonOSPackageEval: d.Get("non_os_package_support").(bool),
Credentials: api.ContainerRegCreds{
Credentials: api.DockerhubCredentials{
Username: d.Get("username").(string),
Password: d.Get("password").(string),
},
Expand All @@ -179,78 +133,55 @@ func resourceLaceworkIntegrationDockerHubCreate(d *schema.ResourceData, meta int
data.Enabled = 0
}

log.Printf("[INFO] Creating %s integration %s registry type with data:\n%+v\n",
api.ContainerRegistryIntegration.String(), api.DockerHubRegistry.String(), data)
response, err := lacework.Integrations.CreateContainerRegistry(data)
log.Printf("[INFO] Creating %s registry type with data:\n%+v\n", api.DockerhubContainerRegistry.String(), data)
response, err := lacework.V2.ContainerRegistries.Create(data)
if err != nil {
return err
}

for _, integration := range response.Data {
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 %s registry type with guid: %v\n",
api.ContainerRegistryIntegration.String(), api.DockerHubRegistry.String(), integration.IntgGuid)
return nil
}
integration := response.Data
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.Type)
d.Set("org_level", integration.IsOrg == 1)

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

func resourceLaceworkIntegrationDockerHubRead(d *schema.ResourceData, meta interface{}) error {
lacework := meta.(*api.Client)

log.Printf("[INFO] Reading %s integration %s registry type with guid: %v\n",
api.ContainerRegistryIntegration.String(), api.DockerHubRegistry.String(), d.Id())
response, err := lacework.Integrations.GetContainerRegistry(d.Id())
log.Printf("[INFO] Reading %s registry type with guid: %v\n", api.DockerhubContainerRegistry.String(), d.Id())
response, err := lacework.V2.ContainerRegistries.GetDockerhub(d.Id())

if err != nil {
return resourceNotFound(d, err)
}

for _, integration := range response.Data {
if integration.IntgGuid == d.Id() {
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)

d.Set("username", integration.Data.Credentials.Username)
d.Set("limit_num_imgs", integration.Data.LimitNumImg)
d.Set("non_os_package_support", integration.Data.NonOSPackageEval)

if _, ok := d.GetOk("limit_by_tags"); ok {
d.Set("limit_by_tags", strings.Split(integration.Data.LimitByTag, ","))
} else {
d.Set("limit_by_tag", integration.Data.LimitByTag)
}

if _, ok := d.GetOk("limit_by_labels"); ok {
d.Set("limit_by_labels", strings.Split(integration.Data.LimitByLabel, ","))
} else {
d.Set("limit_by_label", integration.Data.LimitByLabel)
}
integration := response.Data
if integration.IntgGuid == d.Id() {
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.Type)
d.Set("org_level", integration.IsOrg == 1)

if _, ok := d.GetOk("limit_by_repositories"); ok {
d.Set("limit_by_repositories", strings.Split(integration.Data.LimitByRep, ","))
} else {
d.Set("limit_by_repos", integration.Data.LimitByRep)
}
d.Set("username", integration.Data.Credentials.Username)
d.Set("non_os_package_support", integration.Data.NonOSPackageEval)
d.Set("limit_num_imgs", response.Data.Data.LimitNumImg)
d.Set("limit_by_tags", response.Data.Data.LimitByTag)
d.Set("limit_by_repositories", response.Data.Data.LimitByRep)
d.Set("limit_by_labels", castArrayOfStringKeyMapOfStringsToLimitByLabelSet(response.Data.Data.LimitByLabel))

log.Printf("[INFO] Read %s integration %s registry type with guid: %v\n",
api.ContainerRegistryIntegration.String(), api.DockerHubRegistry.String(), integration.IntgGuid)
return nil
}
log.Printf("[INFO] Read %s registry type with guid: %v\n", api.DockerhubContainerRegistry.String(), integration.IntgGuid)
return nil
}

d.SetId("")
Expand All @@ -259,30 +190,15 @@ func resourceLaceworkIntegrationDockerHubRead(d *schema.ResourceData, meta inter

func resourceLaceworkIntegrationDockerHubUpdate(d *schema.ResourceData, meta interface{}) error {
lacework := meta.(*api.Client)

limitByTags := d.Get("limit_by_tag").(string)
if tags := castAttributeToStringSlice(d, "limit_by_tags"); len(tags) != 0 {
limitByTags = strings.Join(tags, ",")
}

limitByLabels := d.Get("limit_by_label").(string)
if labels := castAttributeToStringKeyMapOfStrings(d, "limit_by_labels"); len(labels) != 0 {
limitByLabels = joinMapStrings(labels, ",")
}

limitByRepos := d.Get("limit_by_repos").(string)
if repos := castAttributeToStringSlice(d, "limit_by_repositories"); len(repos) != 0 {
limitByRepos = strings.Join(repos, ",")
}

data := api.NewDockerHubRegistryIntegration(d.Get("name").(string),
api.ContainerRegData{
LimitByTag: limitByTags,
LimitByLabel: limitByLabels,
LimitByRep: limitByRepos,
data := api.NewContainerRegistry(d.Get("name").(string),
api.DockerhubContainerRegistry,
api.DockerhubData{
LimitByTag: castAttributeToStringSlice(d, "limit_by_tags"),
LimitByRep: castAttributeToStringSlice(d, "limit_by_repositories"),
LimitNumImg: d.Get("limit_num_imgs").(int),
LimitByLabel: castAttributeToArrayKeyMapOfStrings(d, "limit_by_labels"),
NonOSPackageEval: d.Get("non_os_package_support").(bool),
Credentials: api.ContainerRegCreds{
Credentials: api.DockerhubCredentials{
Username: d.Get("username").(string),
Password: d.Get("password").(string),
},
Expand All @@ -295,28 +211,24 @@ func resourceLaceworkIntegrationDockerHubUpdate(d *schema.ResourceData, meta int

data.IntgGuid = d.Id()

log.Printf("[INFO] Updating %s integration %s registry type with data:\n%+v\n",
api.ContainerRegistryIntegration.String(), api.DockerHubRegistry.String(), data)
response, err := lacework.Integrations.UpdateContainerRegistry(data)
log.Printf("[INFO] Updating %s registry type with data:\n%+v\n", api.DockerhubContainerRegistry.String(), data)
response, err := lacework.V2.ContainerRegistries.UpdateDockerhub(data)
if err != nil {
return err
}

for _, integration := range response.Data {
if integration.IntgGuid == d.Id() {
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] Updated %s integration %s registry type with guid: %v\n",
api.ContainerRegistryIntegration.String(), api.DockerHubRegistry.String(), d.Id())
integration := response.Data
if integration.IntgGuid == d.Id() {
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.Type)
d.Set("org_level", integration.IsOrg == 1)

return nil
}
log.Printf("[INFO] Updated %s registry type with guid: %v\n", api.DockerhubContainerRegistry.String(), d.Id())
return nil
}

return nil
Expand All @@ -325,16 +237,14 @@ func resourceLaceworkIntegrationDockerHubUpdate(d *schema.ResourceData, meta int
func resourceLaceworkIntegrationDockerHubDelete(d *schema.ResourceData, meta interface{}) error {
lacework := meta.(*api.Client)

log.Printf("[INFO] Deleting %s integration %s registry type with guid: %v\n",
api.ContainerRegistryIntegration.String(), api.DockerHubRegistry.String(), d.Id())
log.Printf("[INFO] Deleting %s registry type with guid: %v\n", api.DockerhubContainerRegistry.String(), d.Id())

_, err := lacework.Integrations.Delete(d.Id())
err := lacework.V2.ContainerRegistries.Delete(d.Id())
if err != nil {
return err
}

log.Printf("[INFO] Deleted %s integration %s registry type with guid: %v\n",
api.ContainerRegistryIntegration.String(), api.DockerHubRegistry.String(), d.Id())
log.Printf("[INFO] Deleted %s registry type with guid: %v\n", api.DockerhubContainerRegistry.String(), d.Id())

return nil
}
6 changes: 3 additions & 3 deletions lacework/resource_lacework_integration_docker_hub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,9 @@ resource "%s" "%s" {
enabled = %t
username = "%s"
password = "%s"
limit_by_tag = "%s"
limit_by_label = "%s"
limit_by_repos = "%s"
limit_by_tags = "%s"
limit_by_labels = "%s"
limit_by_repositories = "%s"
limit_num_imgs = %s
}
`,
Expand Down

0 comments on commit 0e916c8

Please sign in to comment.