Skip to content

Commit 3bc504c

Browse files
authored
Deprecate Arg_KeyName for Arg_SSHKeyID (IBM-Cloud#6505)
Update doc to add deprecation
1 parent 49d193c commit 3bc504c

File tree

5 files changed

+44
-13
lines changed

5 files changed

+44
-13
lines changed

ibm/acctest/acctest.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ var (
259259
Pi_shared_processor_pool_id string
260260
Pi_snapshot_id string
261261
Pi_spp_placement_group_id string
262+
Pi_ssh_key_id string
262263
Pi_storage_connection string
263264
Pi_target_storage_tier string
264265
Pi_virtual_serial_number string
@@ -1195,7 +1196,13 @@ func init() {
11951196
Pi_key_name = os.Getenv("PI_KEY_NAME")
11961197
if Pi_key_name == "" {
11971198
Pi_key_name = "terraform-test-power"
1198-
fmt.Println("[INFO] Set the environment variable PI_KEY_NAME for testing ibm_pi_key_name resource else it is set to default value 'terraform-test-power'")
1199+
fmt.Println("[INFO] Set the environment variable PI_KEY_NAME for testing ibm_pi_key resource else it is set to default value 'terraform-test-power'")
1200+
}
1201+
1202+
Pi_ssh_key_id = os.Getenv("PI_SSH_KEY_ID")
1203+
if Pi_ssh_key_id == "" {
1204+
Pi_ssh_key_id = "terraform-test-power"
1205+
fmt.Println("[INFO] Set the environment variable PI_SSH_KEY_ID for testing ibm_pi_key resource else it is set to default value 'terraform-test-power'")
11991206
}
12001207

12011208
Pi_network_name = os.Getenv("PI_NETWORK_NAME")

ibm/service/power/data_source_ibm_pi_key.go

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ package power
55

66
import (
77
"context"
8+
"fmt"
9+
"log"
810

911
"github.com/IBM-Cloud/power-go-client/clients/instance"
10-
"github.com/IBM-Cloud/power-go-client/helpers"
1112
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
13+
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
1214
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1315
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1416
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
@@ -25,11 +27,21 @@ func DataSourceIBMPIKey() *schema.Resource {
2527
Type: schema.TypeString,
2628
ValidateFunc: validation.NoZeroValues,
2729
},
30+
2831
Arg_KeyName: {
29-
Description: "User defined name for the SSH key.",
30-
Required: true,
31-
Type: schema.TypeString,
32-
ValidateFunc: validation.NoZeroValues,
32+
AtLeastOneOf: []string{Arg_SSHKeyID, Arg_KeyName},
33+
ConflictsWith: []string{Arg_SSHKeyID},
34+
Deprecated: "The pi_key_name field is deprecated. Please use pi_ssh_key_id instead",
35+
Description: "The name of the SSH key.",
36+
Optional: true,
37+
Type: schema.TypeString,
38+
},
39+
Arg_SSHKeyID: {
40+
AtLeastOneOf: []string{Arg_SSHKeyID, Arg_KeyName},
41+
ConflictsWith: []string{Arg_KeyName},
42+
Description: "The ID of the SSH key.",
43+
Optional: true,
44+
Type: schema.TypeString,
3345
},
3446

3547
// Attributes
@@ -76,15 +88,25 @@ func DataSourceIBMPIKey() *schema.Resource {
7688
func dataSourceIBMPIKeyRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
7789
sess, err := meta.(conns.ClientSession).IBMPISession()
7890
if err != nil {
79-
return diag.FromErr(err)
91+
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("IBMPISession failed: %s", err.Error()), "(Data) ibm_pi_key", "read")
92+
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
93+
return tfErr.GetDiag()
8094
}
8195

8296
cloudInstanceID := d.Get(Arg_CloudInstanceID).(string)
97+
var sshKeyID string
98+
if v, ok := d.GetOk(Arg_SSHKeyID); ok {
99+
sshKeyID = v.(string)
100+
} else if v, ok := d.GetOk(Arg_KeyName); ok {
101+
sshKeyID = v.(string)
102+
}
83103

84104
sshkeyC := instance.NewIBMPISSHKeyClient(ctx, sess, cloudInstanceID)
85-
sshkeydata, err := sshkeyC.Get(d.Get(helpers.PIKeyName).(string))
105+
sshkeydata, err := sshkeyC.Get(sshKeyID)
86106
if err != nil {
87-
return diag.FromErr(err)
107+
tfErr := flex.TerraformErrorf(err, fmt.Sprintf("Get failed: %s", err.Error()), "(Data) ibm_pi_key", "read")
108+
log.Printf("[DEBUG]\n%s", tfErr.GetDebugMessage())
109+
return tfErr.GetDiag()
88110
}
89111

90112
d.SetId(*sshkeydata.Name)

ibm/service/power/data_source_ibm_pi_key_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func TestAccIBMPIKeyDataSource_basic(t *testing.T) {
3030
func testAccCheckIBMPIKeyDataSourceConfig() string {
3131
return fmt.Sprintf(`
3232
data "ibm_pi_key" "testacc_ds_key" {
33-
pi_key_name = "%s"
34-
pi_cloud_instance_id = "%s"
35-
}`, acc.Pi_key_name, acc.Pi_cloud_instance_id)
33+
pi_cloud_instance_id = "%[1]s"
34+
pi_ssh_key_id = "%[2]s"
35+
}`, acc.Pi_cloud_instance_id, acc.Pi_ssh_key_id)
3636
}

ibm/service/power/ibm_pi_constants.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ const (
160160
Arg_SPPPlacementGroupName = "pi_spp_placement_group_name"
161161
Arg_SPPPlacementGroupPolicy = "pi_spp_placement_group_policy"
162162
Arg_SSHKey = "pi_ssh_key"
163+
Arg_SSHKeyID = "pi_ssh_key_id"
163164
Arg_StartingIPAddress = "pi_starting_ip_address"
164165
Arg_StorageConnection = "pi_storage_connection"
165166
Arg_StoragePool = "pi_storage_pool"

website/docs/d/pi_key.html.markdown

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ Example usage:
4040
Review the argument references that you can specify for your data source.
4141

4242
- `pi_cloud_instance_id` - (Required, String) The GUID of the service instance associated with an account.
43-
- `pi_key_name` - (Required, String) User defined name for the SSH key or SSH key ID.
43+
- `pi_key_name` - (Deprecated, Optional, String) User defined name for the SSH key or SSH key ID. Passing the name of the instance could fail or fetch stale data. Please pass an id and use `pi_ssh_key_id` instead.
44+
- `pi_ssh_key_id` - (Optional, String) The SSH key ID.
4445

4546
## Attribute Reference
4647

0 commit comments

Comments
 (0)