Skip to content

Commit

Permalink
SCC-PostureManagement Create scope issue (IBM-Cloud#3708)
Browse files Browse the repository at this point in the history
* 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 <Sugata.Mazumdar1@ibm.com>
  • Loading branch information
2 people authored and SunithaGudisagarIBM1 committed Sep 14, 2022
1 parent 9ce19f6 commit 754d8f8
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 40 deletions.
35 changes: 25 additions & 10 deletions ibm/service/scc/resource_ibm_scc_posture_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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
}

Expand All @@ -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())

Expand All @@ -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())

Expand Down
17 changes: 14 additions & 3 deletions ibm/service/scc/resource_ibm_scc_posture_collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package scc_test

import (
"fmt"
"os"
"testing"
"time"

Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand Down
35 changes: 25 additions & 10 deletions ibm/service/scc/resource_ibm_scc_posture_credential.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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())

Expand Down Expand Up @@ -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())

Expand Down
16 changes: 13 additions & 3 deletions ibm/service/scc/resource_ibm_scc_posture_credential_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package scc_test

import (
"fmt"
"os"
"testing"

acc "github.com/IBM-Cloud/terraform-provider-ibm/ibm/acctest"
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand Down
43 changes: 32 additions & 11 deletions ibm/service/scc/resource_ibm_scc_posture_scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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))

Expand All @@ -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
}
Expand All @@ -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

Expand Down Expand Up @@ -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())

Expand Down
16 changes: 13 additions & 3 deletions ibm/service/scc/resource_ibm_scc_posture_scope_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package scc_test

import (
"fmt"
"os"
"testing"
"time"

Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand Down

0 comments on commit 754d8f8

Please sign in to comment.