From 754d8f8ff76074839c152d07feab74dfb09b294f Mon Sep 17 00:00:00 2001 From: Sugata Mazumdar Date: Thu, 14 Apr 2022 11:34:17 +0530 Subject: [PATCH] SCC-PostureManagement Create scope issue (#3708) * SCC-PostureManagement Create scope issue Create scope issue * SCC-PostureManagement Collector ID, Account ID fix Collector ID issue in resourceIBMSccPostureCollectorsRead and Account Id issue * SCC-PostureManagement Account ID fix Account Id issue from client session and not env var. * SCC-PostureManagement Account ID fix Account Id issue from client session and not env var. * SCC-PostureManagement Cred ID, Account ID fix Cred ID issue in resourceIBMSccPostureCredentialsRead and Account Id issue * SCC-PostureManagement Account ID fix Account Id issue from client session and not env var. * SCC-PostureManagement Scope ID, Account ID fix Scope ID issue in resourceIBMSccPostureScopesRead and Account Id issue * Add format code * Fixes for id field Co-authored-by: Sugata Mazumdar --- .../scc/resource_ibm_scc_posture_collector.go | 35 ++++++++++----- ...resource_ibm_scc_posture_collector_test.go | 17 ++++++-- .../resource_ibm_scc_posture_credential.go | 35 ++++++++++----- ...esource_ibm_scc_posture_credential_test.go | 16 +++++-- .../scc/resource_ibm_scc_posture_scope.go | 43 ++++++++++++++----- .../resource_ibm_scc_posture_scope_test.go | 16 +++++-- 6 files changed, 122 insertions(+), 40 deletions(-) diff --git a/ibm/service/scc/resource_ibm_scc_posture_collector.go b/ibm/service/scc/resource_ibm_scc_posture_collector.go index ffb53b718f..b6f14542b4 100644 --- a/ibm/service/scc/resource_ibm_scc_posture_collector.go +++ b/ibm/service/scc/resource_ibm_scc_posture_collector.go @@ -7,7 +7,6 @@ import ( "context" "fmt" "log" - "os" "github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns" "github.com/IBM-Cloud/terraform-provider-ibm/ibm/validate" @@ -115,7 +114,12 @@ func resourceIBMSccPostureCollectorsCreate(context context.Context, d *schema.Re } createCollectorOptions := &posturemanagementv2.CreateCollectorOptions{} - createCollectorOptions.SetAccountID(os.Getenv("SCC_POSTURE_ACCOUNT_ID")) + + userDetails, err := meta.(conns.ClientSession).BluemixUserDetails() + if err != nil { + return diag.FromErr(fmt.Errorf("[ERROR] Error getting userDetails %s", err)) + } + createCollectorOptions.SetAccountID(userDetails.UserAccount) createCollectorOptions.SetName(d.Get("name").(string)) createCollectorOptions.SetIsPublic(d.Get("is_public").(bool)) @@ -147,25 +151,26 @@ func resourceIBMSccPostureCollectorsRead(context context.Context, d *schema.Reso return diag.FromErr(err) } - listCollectorsOptions := &posturemanagementv2.ListCollectorsOptions{} + getCollectorsOptions := &posturemanagementv2.GetCollectorOptions{} userDetails, err := meta.(conns.ClientSession).BluemixUserDetails() if err != nil { return diag.FromErr(fmt.Errorf("[ERROR] Error getting userDetails %s", err)) } accountID := userDetails.UserAccount - listCollectorsOptions.SetAccountID(accountID) + getCollectorsOptions.SetAccountID(accountID) + getCollectorsOptions.SetID(d.Id()) - collectorList, response, err := postureManagementClient.ListCollectorsWithContext(context, listCollectorsOptions) + collector, response, err := postureManagementClient.GetCollectorWithContext(context, getCollectorsOptions) if err != nil { if response != nil && response.StatusCode == 404 { d.SetId("") return nil } - log.Printf("[DEBUG] ListCollectorsWithContext failed %s\n%s", err, response) - return diag.FromErr(fmt.Errorf("ListCollectorsWithContext failed %s\n%s", err, response)) + log.Printf("[DEBUG] GetCollectorWithContext failed %s\n%s", err, response) + return diag.FromErr(fmt.Errorf("GetCollectorWithContext failed %s\n%s", err, response)) } - d.SetId(*(collectorList.Collectors[0].ID)) + d.SetId(*(collector.ID)) return nil } @@ -176,7 +181,12 @@ func resourceIBMSccPostureCollectorsUpdate(context context.Context, d *schema.Re } updateCollectorOptions := &posturemanagementv2.UpdateCollectorOptions{} - updateCollectorOptions.SetAccountID(os.Getenv("SCC_POSTURE_ACCOUNT_ID")) + + userDetails, err := meta.(conns.ClientSession).BluemixUserDetails() + if err != nil { + return diag.FromErr(fmt.Errorf("[ERROR] Error getting userDetails %s", err)) + } + updateCollectorOptions.SetAccountID(userDetails.UserAccount) updateCollectorOptions.SetID(d.Id()) @@ -201,7 +211,12 @@ func resourceIBMSccPostureCollectorsDelete(context context.Context, d *schema.Re } deleteCollectorOptions := &posturemanagementv2.DeleteCollectorOptions{} - deleteCollectorOptions.SetAccountID(os.Getenv("SCC_POSTURE_ACCOUNT_ID")) + + userDetails, err := meta.(conns.ClientSession).BluemixUserDetails() + if err != nil { + return diag.FromErr(fmt.Errorf("[ERROR] Error getting userDetails %s", err)) + } + deleteCollectorOptions.SetAccountID(userDetails.UserAccount) deleteCollectorOptions.SetID(d.Id()) diff --git a/ibm/service/scc/resource_ibm_scc_posture_collector_test.go b/ibm/service/scc/resource_ibm_scc_posture_collector_test.go index e0469328fc..e1a1f22b14 100644 --- a/ibm/service/scc/resource_ibm_scc_posture_collector_test.go +++ b/ibm/service/scc/resource_ibm_scc_posture_collector_test.go @@ -5,7 +5,6 @@ package scc_test import ( "fmt" - "os" "testing" "time" @@ -143,7 +142,13 @@ func testAccCheckIBMSccPostureCollectorsExists(n string, obj posturemanagementv2 } listCollectorsOptions := &posturemanagementv2.ListCollectorsOptions{} - listCollectorsOptions.SetAccountID(os.Getenv("SCC_POSTURE_ACCOUNT_ID")) + + userDetails, err := acc.TestAccProvider.Meta().(conns.ClientSession).BluemixUserDetails() + if err != nil { + return err + } + + listCollectorsOptions.SetAccountID(userDetails.UserAccount) newCollector, _, err := postureManagementClient.ListCollectors(listCollectorsOptions) if err != nil { @@ -166,7 +171,13 @@ func testAccCheckIBMSccPostureCollectorsDestroy(s *terraform.State) error { } listCollectorsOptions := &posturemanagementv2.ListCollectorsOptions{} - listCollectorsOptions.SetAccountID(os.Getenv("SCC_POSTURE_ACCOUNT_ID")) + + userDetails, err := acc.TestAccProvider.Meta().(conns.ClientSession).BluemixUserDetails() + if err != nil { + return err + } + + listCollectorsOptions.SetAccountID(userDetails.UserAccount) // Try to find the key _, response, err := postureManagementClient.ListCollectors(listCollectorsOptions) diff --git a/ibm/service/scc/resource_ibm_scc_posture_credential.go b/ibm/service/scc/resource_ibm_scc_posture_credential.go index b6403ef5c8..b51d9c2fd0 100644 --- a/ibm/service/scc/resource_ibm_scc_posture_credential.go +++ b/ibm/service/scc/resource_ibm_scc_posture_credential.go @@ -7,7 +7,6 @@ import ( "context" "fmt" "log" - "os" "github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns" "github.com/IBM-Cloud/terraform-provider-ibm/ibm/validate" @@ -148,7 +147,12 @@ func resourceIBMSccPostureCredentialsCreate(context context.Context, d *schema.R } createCredentialOptions := &posturemanagementv2.CreateCredentialOptions{} - createCredentialOptions.SetAccountID(os.Getenv("SCC_POSTURE_ACCOUNT_ID")) + + userDetails, err := meta.(conns.ClientSession).BluemixUserDetails() + if err != nil { + return diag.FromErr(fmt.Errorf("[ERROR] Error getting userDetails %s", err)) + } + createCredentialOptions.SetAccountID(userDetails.UserAccount) createCredentialOptions.SetEnabled(d.Get("enabled").(bool)) createCredentialOptions.SetType(d.Get("type").(string)) @@ -206,25 +210,26 @@ func resourceIBMSccPostureCredentialsRead(context context.Context, d *schema.Res return diag.FromErr(err) } - listCredentialsOptions := &posturemanagementv2.ListCredentialsOptions{} + getCredentialsOptions := &posturemanagementv2.GetCredentialOptions{} userDetails, err := meta.(conns.ClientSession).BluemixUserDetails() if err != nil { return diag.FromErr(fmt.Errorf("[ERROR] Error getting userDetails %s", err)) } accountID := userDetails.UserAccount - listCredentialsOptions.SetAccountID(accountID) + getCredentialsOptions.SetAccountID(accountID) + getCredentialsOptions.SetID(d.Id()) - credentialList, response, err := postureManagementClient.ListCredentialsWithContext(context, listCredentialsOptions) + credential, response, err := postureManagementClient.GetCredentialWithContext(context, getCredentialsOptions) if err != nil { if response != nil && response.StatusCode == 404 { d.SetId("") return nil } - log.Printf("[DEBUG] ListCredentialsWithContext failed %s\n%s", err, response) - return diag.FromErr(fmt.Errorf("ListCredentialsWithContext failed %s\n%s", err, response)) + log.Printf("[DEBUG] GetCredentialWithContext failed %s\n%s", err, response) + return diag.FromErr(fmt.Errorf("GetCredentialWithContext failed %s\n%s", err, response)) } - d.SetId(*(credentialList.Credentials[0].ID)) + d.SetId(*(credential.ID)) return nil } @@ -254,7 +259,12 @@ func resourceIBMSccPostureCredentialsUpdate(context context.Context, d *schema.R } updateCredentialOptions := &posturemanagementv2.UpdateCredentialOptions{} - updateCredentialOptions.SetAccountID(os.Getenv("SCC_POSTURE_ACCOUNT_ID")) + + userDetails, err := meta.(conns.ClientSession).BluemixUserDetails() + if err != nil { + return diag.FromErr(fmt.Errorf("[ERROR] Error getting userDetails %s", err)) + } + updateCredentialOptions.SetAccountID(userDetails.UserAccount) updateCredentialOptions.SetID(d.Id()) @@ -290,7 +300,12 @@ func resourceIBMSccPostureCredentialsDelete(context context.Context, d *schema.R } deleteCredentialOptions := &posturemanagementv2.DeleteCredentialOptions{} - deleteCredentialOptions.SetAccountID(os.Getenv("SCC_POSTURE_ACCOUNT_ID")) + + userDetails, err := meta.(conns.ClientSession).BluemixUserDetails() + if err != nil { + return diag.FromErr(fmt.Errorf("[ERROR] Error getting userDetails %s", err)) + } + deleteCredentialOptions.SetAccountID(userDetails.UserAccount) deleteCredentialOptions.SetID(d.Id()) diff --git a/ibm/service/scc/resource_ibm_scc_posture_credential_test.go b/ibm/service/scc/resource_ibm_scc_posture_credential_test.go index 7ff1f38945..90c2eaf119 100644 --- a/ibm/service/scc/resource_ibm_scc_posture_credential_test.go +++ b/ibm/service/scc/resource_ibm_scc_posture_credential_test.go @@ -5,7 +5,6 @@ package scc_test import ( "fmt" - "os" "testing" acc "github.com/IBM-Cloud/terraform-provider-ibm/ibm/acctest" @@ -101,7 +100,13 @@ func testAccCheckIBMSccPostureCredentialsExists(n string, obj posturemanagementv } listCredentialsOptions := &posturemanagementv2.ListCredentialsOptions{} - listCredentialsOptions.SetAccountID(os.Getenv("SCC_POSTURE_ACCOUNT_ID")) + + userDetails, err := acc.TestAccProvider.Meta().(conns.ClientSession).BluemixUserDetails() + if err != nil { + return err + } + + listCredentialsOptions.SetAccountID(userDetails.UserAccount) newCredential, _, err := postureManagementClient.ListCredentials(listCredentialsOptions) if err != nil { @@ -124,7 +129,12 @@ func testAccCheckIBMSccPostureCredentialsDestroy(s *terraform.State) error { } listCredentialsOptions := &posturemanagementv2.ListCredentialsOptions{} - listCredentialsOptions.SetAccountID(os.Getenv("SCC_POSTURE_ACCOUNT_ID")) + + userDetails, err := acc.TestAccProvider.Meta().(conns.ClientSession).BluemixUserDetails() + if err != nil { + return err + } + listCredentialsOptions.SetAccountID(userDetails.UserAccount) // Try to find the key _, response, err := postureManagementClient.ListCredentials(listCredentialsOptions) diff --git a/ibm/service/scc/resource_ibm_scc_posture_scope.go b/ibm/service/scc/resource_ibm_scc_posture_scope.go index a2b3445207..f85fcec222 100644 --- a/ibm/service/scc/resource_ibm_scc_posture_scope.go +++ b/ibm/service/scc/resource_ibm_scc_posture_scope.go @@ -7,7 +7,6 @@ import ( "context" "fmt" "log" - "os" "github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns" "github.com/IBM-Cloud/terraform-provider-ibm/ibm/validate" @@ -110,11 +109,21 @@ func resourceIBMSccPostureScopesCreate(context context.Context, d *schema.Resour } createScopeOptions := &posturemanagementv2.CreateScopeOptions{} - createScopeOptions.SetAccountID(os.Getenv("SCC_POSTURE_ACCOUNT_ID")) + + userDetails, err := meta.(conns.ClientSession).BluemixUserDetails() + if err != nil { + return diag.FromErr(fmt.Errorf("[ERROR] Error getting userDetails %s", err)) + } + createScopeOptions.SetAccountID(userDetails.UserAccount) createScopeOptions.SetName(d.Get("name").(string)) createScopeOptions.SetDescription(d.Get("description").(string)) - createScopeOptions.SetCollectorIds([]string{"4188"}) //[]string{ + collector_ids_int := d.Get("collector_ids").([]interface{}) + collector_ids := make([]string, len(collector_ids_int)) + for i, collector_id := range collector_ids_int { + collector_ids[i] = collector_id.(string) + } + createScopeOptions.SetCollectorIds(collector_ids) //[]string{ createScopeOptions.SetCredentialID(d.Get("credential_id").(string)) createScopeOptions.SetCredentialType(d.Get("credential_type").(string)) @@ -135,25 +144,27 @@ func resourceIBMSccPostureScopesRead(context context.Context, d *schema.Resource return diag.FromErr(err) } - listScopesOptions := &posturemanagementv2.ListScopesOptions{} + getScopesOptions := &posturemanagementv2.GetScopeDetailsOptions{} userDetails, err := meta.(conns.ClientSession).BluemixUserDetails() if err != nil { return diag.FromErr(fmt.Errorf("[ERROR] Error getting userDetails %s", err)) } accountID := userDetails.UserAccount - listScopesOptions.SetAccountID(accountID) + getScopesOptions.SetAccountID(accountID) + getScopesOptions.SetID(d.Id()) + + scope, response, err := postureManagementClient.GetScopeDetailsWithContext(context, getScopesOptions) - scopeList, response, err := postureManagementClient.ListScopesWithContext(context, listScopesOptions) - d.SetId(*(scopeList.Scopes[0].ID)) if err != nil { if response != nil && response.StatusCode == 404 { d.SetId("") return nil } - log.Printf("[DEBUG] ListScopesWithContext failed %s\n%s", err, response) - return diag.FromErr(fmt.Errorf("ListScopesWithContext failed %s\n%s", err, response)) + log.Printf("[DEBUG] GetScopeDetailsWithContext failed %s\n%s", err, response) + return diag.FromErr(fmt.Errorf("GetScopeDetailsWithContext failed %s\n%s", err, response)) } + d.SetId(*scope.ID) return nil } @@ -165,7 +176,12 @@ func resourceIBMSccPostureScopesUpdate(context context.Context, d *schema.Resour } updateScopeDetailsOptions := &posturemanagementv2.UpdateScopeDetailsOptions{} - updateScopeDetailsOptions.SetAccountID(os.Getenv("SCC_POSTURE_ACCOUNT_ID")) + + userDetails, err := meta.(conns.ClientSession).BluemixUserDetails() + if err != nil { + return diag.FromErr(fmt.Errorf("[ERROR] Error getting userDetails %s", err)) + } + updateScopeDetailsOptions.SetAccountID(userDetails.UserAccount) hasChange := false @@ -198,7 +214,12 @@ func resourceIBMSccPostureScopesDelete(context context.Context, d *schema.Resour } deleteScopeOptions := &posturemanagementv2.DeleteScopeOptions{} - deleteScopeOptions.SetAccountID(os.Getenv("SCC_POSTURE_ACCOUNT_ID")) + + userDetails, err := meta.(conns.ClientSession).BluemixUserDetails() + if err != nil { + return diag.FromErr(fmt.Errorf("[ERROR] Error getting userDetails %s", err)) + } + deleteScopeOptions.SetAccountID(userDetails.UserAccount) deleteScopeOptions.SetID(d.Id()) diff --git a/ibm/service/scc/resource_ibm_scc_posture_scope_test.go b/ibm/service/scc/resource_ibm_scc_posture_scope_test.go index 73c9ebd09e..bda5ac2163 100644 --- a/ibm/service/scc/resource_ibm_scc_posture_scope_test.go +++ b/ibm/service/scc/resource_ibm_scc_posture_scope_test.go @@ -5,7 +5,6 @@ package scc_test import ( "fmt" - "os" "testing" "time" @@ -136,7 +135,12 @@ func testAccCheckIBMSccPostureScopesExists(n string, obj posturemanagementv2.Sco } listScopesOptions := &posturemanagementv2.ListScopesOptions{} - listScopesOptions.SetAccountID(os.Getenv("SCC_POSTURE_ACCOUNT_ID")) + + userDetails, err := acc.TestAccProvider.Meta().(conns.ClientSession).BluemixUserDetails() + if err != nil { + return err + } + listScopesOptions.SetAccountID(userDetails.UserAccount) newScope, _, err := postureManagementClient.ListScopes(listScopesOptions) if err != nil { @@ -159,7 +163,13 @@ func testAccCheckIBMSccPostureScopesDestroy(s *terraform.State) error { } listScopesOptions := &posturemanagementv2.ListScopesOptions{} - listScopesOptions.SetAccountID(os.Getenv("SCC_POSTURE_ACCOUNT_ID")) + + userDetails, err := acc.TestAccProvider.Meta().(conns.ClientSession).BluemixUserDetails() + if err != nil { + return err + } + + listScopesOptions.SetAccountID(userDetails.UserAccount) // Try to find the key _, response, err := postureManagementClient.ListScopes(listScopesOptions)