forked from IBM-Cloud/terraform-provider-ibm
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SCC: adding datasources for automated WP connection and Profile attac…
…hments (IBM-Cloud#5179) * init: pushing out the scc_profiles_datasource * feat: adding the data_source provider_types * fix: using TypeSet for unique assessments * bug: working on resource_ibm_scc_profile_test * Changing various resources to typeSet. * adding the datasource * adding the datasource control libraries and profiles * Updating some documentation * adding the updates to documentation and fmt'ing * Adding the err checks for interface conversion * making both fields to be required if instantiated * fixing the formatting of the test --------- Co-authored-by: Timothy-Yao <timothy.yao@ibm.com>
- Loading branch information
Showing
24 changed files
with
1,122 additions
and
54 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
214 changes: 214 additions & 0 deletions
214
ibm/service/scc/data_source_ibm_scc_control_libraries.go
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,214 @@ | ||
// Copyright IBM Corp. 2023 All Rights Reserved. | ||
// Licensed under the Mozilla Public License v2.0 | ||
|
||
package scc | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
|
||
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns" | ||
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/validate" | ||
"github.com/IBM/scc-go-sdk/v5/securityandcompliancecenterapiv3" | ||
) | ||
|
||
func DataSourceIbmSccControlLibraries() *schema.Resource { | ||
return AddSchemaData(&schema.Resource{ | ||
ReadContext: dataSourceIbmSccControlLibrariesRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"control_library_type": { | ||
Type: schema.TypeString, | ||
Description: "The type of control library to be found.", | ||
ValidateFunc: validate.InvokeValidator("ibm_scc_control_library", "control_library_type"), | ||
Optional: true, | ||
}, | ||
"control_libraries": &schema.Schema{ | ||
Type: schema.TypeList, | ||
Computed: true, | ||
Description: "The list of control libraries found.", | ||
Elem: &schema.Resource{ | ||
Schema: map[string]*schema.Schema{ | ||
"id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "The ID of the control library.", | ||
}, | ||
"account_id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "The ID of associated with the control library.", | ||
}, | ||
// "instance_id": { | ||
// Type: schema.TypeString, | ||
// Computed: true, | ||
// Description: "The profile description.", | ||
// }, | ||
"control_library_name": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "The name of the control library.", | ||
}, | ||
"control_library_description": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "The description of the control library.", | ||
}, | ||
"control_library_type": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "The type of the control library.", | ||
}, | ||
"version_group_label": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "The version group label of the control library.", | ||
}, | ||
"control_library_version": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "The version of the control library.", | ||
}, | ||
"latest": { | ||
Type: schema.TypeBool, | ||
Computed: true, | ||
Description: "The latest version of the control library.", | ||
}, | ||
// "hierarchy_enabled": { | ||
// Type: schema.TypeBool, | ||
// Computed: true, | ||
// Description: "The indication of whether hierarchy is enabled for the control library.", | ||
// }, | ||
"created_by": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "The user who created the control library.", | ||
}, | ||
"created_on": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "The date when the control library was created.", | ||
}, | ||
"updated_by": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "The user who updated the control library.", | ||
}, | ||
"updated_on": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "The date when the control library was updated.", | ||
}, | ||
"controls_count": { | ||
Type: schema.TypeInt, | ||
Computed: true, | ||
Description: "The number of controls for the control library.", | ||
}, | ||
// "control_parents_count": { | ||
// Type: schema.TypeInt, | ||
// Computed: true, | ||
// Description: "The number of parent controls for the control library.", | ||
// }, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func dataSourceIbmSccControlLibrariesRead(context context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
securityandcompliancecenterapiClient, err := meta.(conns.ClientSession).SecurityAndComplianceCenterV3() | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
listControlLibrariesOptions := &securityandcompliancecenterapiv3.ListControlLibrariesOptions{} | ||
listControlLibrariesOptions.SetInstanceID(d.Get("instance_id").(string)) | ||
if val, ok := d.GetOk("control_library_type"); ok && val != nil { | ||
listControlLibrariesOptions.SetControlLibraryType(val.(string)) | ||
} | ||
|
||
pager, err := securityandcompliancecenterapiClient.NewControlLibrariesPager(listControlLibrariesOptions) | ||
if err != nil { | ||
log.Printf("[DEBUG] ListControlLibrarysWithContext failed %s", err) | ||
return diag.FromErr(fmt.Errorf("ListControlLibrarysWithContext failed %s", err)) | ||
} | ||
controlLibraryList, err := pager.GetAll() | ||
if err != nil { | ||
log.Printf("[DEBUG] ListControlLibrarysWithContext failed %s", err) | ||
return diag.FromErr(fmt.Errorf("ListControlLibrarysWithContext failed %s", err)) | ||
} | ||
d.SetId(fmt.Sprintf("%s/control_libraries", d.Get("instance_id").(string))) | ||
if err = d.Set("instance_id", d.Get("instance_id")); err != nil { | ||
return diag.FromErr(fmt.Errorf("Error setting instance_id %s", err)) | ||
} | ||
controlLibraries := []map[string]interface{}{} | ||
for _, cl := range controlLibraryList { | ||
modelMap, err := dataSourceIbmSccControlLibraryToMap(&cl) | ||
if err != nil { | ||
return diag.FromErr(fmt.Errorf("Error setting control library:%v\n%s", cl, err)) | ||
} | ||
controlLibraries = append(controlLibraries, modelMap) | ||
} | ||
if err = d.Set("control_libraries", controlLibraries); err != nil { | ||
return diag.FromErr(fmt.Errorf("Error setting control_libraries: %s", err)) | ||
} | ||
return nil | ||
} | ||
|
||
func dataSourceIbmSccControlLibraryToMap(controlLibrary *securityandcompliancecenterapiv3.ControlLibraryItem) (map[string]interface{}, error) { | ||
modelMap := make(map[string]interface{}) | ||
if controlLibrary.ID != nil { | ||
modelMap["id"] = controlLibrary.ID | ||
} | ||
if controlLibrary.AccountID != nil { | ||
modelMap["account_id"] = controlLibrary.AccountID | ||
} | ||
// if controlLibrary.InstanceID != nil { | ||
// modelMap["instance_id"] = controlLibrary.InstanceID | ||
// } | ||
if controlLibrary.ControlLibraryName != nil { | ||
modelMap["control_library_name"] = controlLibrary.ControlLibraryName | ||
} | ||
if controlLibrary.ControlLibraryDescription != nil { | ||
modelMap["control_library_description"] = controlLibrary.ControlLibraryDescription | ||
} | ||
if controlLibrary.ControlLibraryType != nil { | ||
modelMap["control_library_type"] = controlLibrary.ControlLibraryType | ||
} | ||
if controlLibrary.VersionGroupLabel != nil { | ||
modelMap["version_group_label"] = controlLibrary.VersionGroupLabel | ||
} | ||
if controlLibrary.ControlLibraryVersion != nil { | ||
modelMap["control_library_version"] = controlLibrary.ControlLibraryVersion | ||
} | ||
if controlLibrary.Latest != nil { | ||
modelMap["latest"] = controlLibrary.Latest | ||
} | ||
// if controlLibrary.HierarchyEnabled != nil { | ||
// modelMap["hierarchy_enabled"] = controlLibrary.HierarchyEnabled | ||
// } | ||
if controlLibrary.CreatedBy != nil { | ||
modelMap["created_by"] = controlLibrary.CreatedBy | ||
} | ||
if controlLibrary.CreatedOn != nil { | ||
modelMap["created_on"] = controlLibrary.CreatedOn.String() | ||
} | ||
if controlLibrary.UpdatedBy != nil { | ||
modelMap["updated_by"] = controlLibrary.UpdatedBy | ||
} | ||
if controlLibrary.UpdatedOn != nil { | ||
modelMap["updated_on"] = controlLibrary.UpdatedOn.String() | ||
} | ||
if controlLibrary.ControlsCount != nil { | ||
modelMap["controls_count"] = controlLibrary.ControlsCount | ||
} | ||
// if controlLibrary.ControlParentCount != nil { | ||
// modelMap["controls_parents_count"] = controlLibrary.ControlParentsCount | ||
// } | ||
return modelMap, nil | ||
} |
59 changes: 59 additions & 0 deletions
59
ibm/service/scc/data_source_ibm_scc_control_libraries_test.go
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,59 @@ | ||
package scc_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
|
||
acc "github.com/IBM-Cloud/terraform-provider-ibm/ibm/acctest" | ||
) | ||
|
||
func TestAccIbmSccControlLibrariesDataSourceBasic(t *testing.T) { | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { acc.TestAccPreCheckScc(t) }, | ||
Providers: acc.TestAccProviders, | ||
Steps: []resource.TestStep{ | ||
resource.TestStep{ | ||
Config: testAccCheckIbmSccControlLibrariesDataSourceConfigBasic(acc.SccInstanceID), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttrSet("data.ibm_scc_control_libraries.scc_control_libraries_instance", "instance_id"), | ||
resource.TestCheckResourceAttrSet("data.ibm_scc_control_libraries.scc_control_libraries_instance", "control_libraries.#"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccIbmSccControlLibrariesDataSourceAllArgs(t *testing.T) { | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { acc.TestAccPreCheckScc(t) }, | ||
Providers: acc.TestAccProviders, | ||
Steps: []resource.TestStep{ | ||
resource.TestStep{ | ||
Config: testAccCheckIbmSccControlLibrariesDataSourceConfigAllArgs(acc.SccInstanceID), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttrSet("data.ibm_scc_control_libraries.scc_control_libraries_instance", "instance_id"), | ||
resource.TestCheckResourceAttrSet("data.ibm_scc_control_libraries.scc_control_libraries_instance", "control_libraries.#"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckIbmSccControlLibrariesDataSourceConfigBasic(instanceID string) string { | ||
return fmt.Sprintf(` | ||
data "ibm_scc_control_libraries" "scc_control_libraries_instance" { | ||
instance_id = "%s" | ||
} | ||
`, instanceID) | ||
} | ||
|
||
func testAccCheckIbmSccControlLibrariesDataSourceConfigAllArgs(instanceID string) string { | ||
return fmt.Sprintf(` | ||
data "ibm_scc_control_libraries" "scc_control_libraries_instance" { | ||
control_library_type = "predefined" | ||
instance_id = "%s" | ||
} | ||
`, instanceID) | ||
} |
Oops, something went wrong.