Skip to content

Commit

Permalink
Respect store_value argument when creating apikey for ServiceId IBM-C…
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Byrne authored and deepaksibm committed Jun 14, 2024
1 parent b2f180b commit 856d1c3
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 10 deletions.
5 changes: 3 additions & 2 deletions ibm/service/iamidentity/resource_ibm_iam_service_api_key.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
// Copyright IBM Corp. 2017, 2024 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0

package iamidentity
Expand Down Expand Up @@ -75,6 +75,7 @@ func ResourceIBMIAMServiceAPIKey() *schema.Resource {
"store_value": {
Type: schema.TypeBool,
Optional: true,
Default: true,
DiffSuppressFunc: flex.ApplyOnce,
Description: "Boolean value deciding whether API key value is retrievable in the future",
},
Expand Down Expand Up @@ -171,7 +172,7 @@ func resourceIBMIAMServiceAPIkeyCreate(d *schema.ResourceData, meta interface{})
createAPIKeyOptions.Apikey = &apikeyString
}

if strvalue, ok := d.GetOk("store_value"); ok {
if strvalue := d.Get("store_value"); strvalue != nil {
value := strvalue.(bool)
createAPIKeyOptions.StoreValue = &value
}
Expand Down
55 changes: 47 additions & 8 deletions ibm/service/iamidentity/resource_ibm_iam_service_api_key_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
// Copyright IBM Corp. 2017, 2024 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0

package iamidentity_test
Expand All @@ -22,23 +22,25 @@ func TestAccIBMIAMServiceAPIKey_Basic(t *testing.T) {
serviceName := fmt.Sprintf("terraform_iam_ser_%d", acctest.RandIntRange(10, 100))
name := fmt.Sprintf("terraform_iam_%d", acctest.RandIntRange(10, 100))
updateName := fmt.Sprintf("terraform_iam_%d", acctest.RandIntRange(10, 100))
storeValue := true

resource.Test(t, resource.TestCase{
PreCheck: func() { acc.TestAccPreCheck(t) },
Providers: acc.TestAccProviders,
CheckDestroy: testAccCheckIBMIAMServiceAPIKeyDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckIBMIAMServiceAPIKeyBasic(serviceName, name),
Config: testAccCheckIBMIAMServiceAPIKeyBasic(serviceName, name, storeValue),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckIBMIAMServiceAPIKeyExists("ibm_iam_service_api_key.testacc_apiKey", apiKey),
testAccCheckIBMIAMServiceAPIKeyExistsWithValidation("ibm_iam_service_api_key.testacc_apiKey", apiKey, storeValue),
resource.TestCheckResourceAttr("ibm_iam_service_api_key.testacc_apiKey", "name", name),
resource.TestCheckResourceAttrSet("ibm_iam_service_api_key.testacc_apiKey", "apikey"),
),
},
{
Config: testAccCheckIBMIAMServiceAPIKeyUpdateWithSameName(serviceName, name),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckIBMIAMServiceAPIKeyExists("ibm_iam_service_api_key.testacc_apiKey", apiKey),
testAccCheckIBMIAMServiceAPIKeyExistsWithValidation("ibm_iam_service_api_key.testacc_apiKey", apiKey, storeValue),
resource.TestCheckResourceAttr("ibm_iam_service_api_key.testacc_apiKey", "name", name),
resource.TestCheckResourceAttr("ibm_iam_service_api_key.testacc_apiKey", "description", "Service API Key for test scenario1"),
),
Expand All @@ -54,11 +56,34 @@ func TestAccIBMIAMServiceAPIKey_Basic(t *testing.T) {
})
}

func TestAccIBMIAMServiceAPIKey_doNotStoreApikeyValue(t *testing.T) {
var apiKey string
serviceName := fmt.Sprintf("terraform_iam_ser_%d", acctest.RandIntRange(10, 100))
name := fmt.Sprintf("terraform_iam_%d", acctest.RandIntRange(10, 100))
storeValue := false

resource.Test(t, resource.TestCase{
PreCheck: func() { acc.TestAccPreCheck(t) },
Providers: acc.TestAccProviders,
CheckDestroy: testAccCheckIBMIAMServiceAPIKeyDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckIBMIAMServiceAPIKeyBasic(serviceName, name, storeValue),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckIBMIAMServiceAPIKeyExistsWithValidation("ibm_iam_service_api_key.testacc_apiKey", apiKey, storeValue),
resource.TestCheckResourceAttr("ibm_iam_service_api_key.testacc_apiKey", "name", name),
),
},
},
})
}

func TestAccIBMIAMServiceAPIKey_import(t *testing.T) {
var apiKey string
serviceName := fmt.Sprintf("terraform_iam_ser_%d", acctest.RandIntRange(10, 100))
name := fmt.Sprintf("terraform_iam_%d", acctest.RandIntRange(10, 100))
resourceName := "ibm_iam_service_api_key.testacc_apiKey"
storeValue := true

resource.Test(t, resource.TestCase{
PreCheck: func() { acc.TestAccPreCheck(t) },
Expand All @@ -68,7 +93,7 @@ func TestAccIBMIAMServiceAPIKey_import(t *testing.T) {
{
Config: testAccCheckIBMIAMServiceAPIKeyImport(serviceName, name),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckIBMIAMServiceAPIKeyExists(resourceName, apiKey),
testAccCheckIBMIAMServiceAPIKeyExistsWithValidation(resourceName, apiKey, storeValue),
resource.TestCheckResourceAttr(resourceName, "name", name),
resource.TestCheckResourceAttr(resourceName, "description", "Service API Key for test scenario2"),
),
Expand All @@ -77,6 +102,9 @@ func TestAccIBMIAMServiceAPIKey_import(t *testing.T) {
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{
"store_value",
},
},
},
})
Expand Down Expand Up @@ -105,7 +133,7 @@ func testAccCheckIBMIAMServiceAPIKeyDestroy(s *terraform.State) error {
return nil
}

func testAccCheckIBMIAMServiceAPIKeyExists(n string, apiKey string) resource.TestCheckFunc {
func testAccCheckIBMIAMServiceAPIKeyExistsWithValidation(n string, apiKey string, apikeyValueExpected bool) resource.TestCheckFunc {

return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
Expand All @@ -127,12 +155,22 @@ func testAccCheckIBMIAMServiceAPIKeyExists(n string, apiKey string) resource.Tes
return err
}

if apikeyValueExpected {
if foundAPIKey.Apikey == nil {
return fmt.Errorf("apikey value should be present")
}
} else {
if foundAPIKey.Apikey != nil {
return fmt.Errorf("apikey value should not be present")
}
}

apiKey = *foundAPIKey.ID
return nil
}
}

func testAccCheckIBMIAMServiceAPIKeyBasic(serviceName, name string) string {
func testAccCheckIBMIAMServiceAPIKeyBasic(serviceName, name string, storeValue bool) string {
return fmt.Sprintf(`
resource "ibm_iam_service_id" "serviceID" {
Expand All @@ -142,8 +180,9 @@ func testAccCheckIBMIAMServiceAPIKeyBasic(serviceName, name string) string {
resource "ibm_iam_service_api_key" "testacc_apiKey" {
name = "%s"
iam_service_id = ibm_iam_service_id.serviceID.iam_id
store_value = "%t"
}
`, serviceName, name)
`, serviceName, name, storeValue)
}

func testAccCheckIBMIAMServiceAPIKeyUpdateWithSameName(serviceName, name string) string {
Expand Down

0 comments on commit 856d1c3

Please sign in to comment.