Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,6 @@ github.com/IBM/go-sdk-core/v5 v5.6.3/go.mod h1:tt/B9rxLkRtglE7pvqLuYikgCXaZFL3bt
github.com/IBM/go-sdk-core/v5 v5.9.5/go.mod h1:YlOwV9LeuclmT/qi/LAK2AsobbAP42veV0j68/rlZsE=
github.com/IBM/go-sdk-core/v5 v5.10.2/go.mod h1:WZPFasUzsKab/2mzt29xPcfruSk5js2ywAPwW4VJjdI=
github.com/IBM/go-sdk-core/v5 v5.17.4/go.mod h1:KsAAI7eStAWwQa4F96MLy+whYSh39JzNjklZRbN/8ns=
github.com/IBM/go-sdk-core/v5 v5.20.1 h1:dzeyifh1kfRLw8VfAIIS5okZYuqLTqplPZP/Kcsgdlo=
github.com/IBM/go-sdk-core/v5 v5.20.1/go.mod h1:Q3BYO6iDA2zweQPDGbNTtqft5tDcEpm6RTuqMlPcvbw=
github.com/IBM/go-sdk-core/v5 v5.21.0 h1:DUnYhvC4SoC8T84rx5omnhY3+xcQg/Whyoa3mDPIMkk=
github.com/IBM/go-sdk-core/v5 v5.21.0/go.mod h1:Q3BYO6iDA2zweQPDGbNTtqft5tDcEpm6RTuqMlPcvbw=
github.com/IBM/ibm-backup-recovery-sdk-go v1.0.3 h1:9TZHocmCfgmF8TGVrpP1kFyQbjcqLNW7+bM07lefpKQ=
Expand Down
9 changes: 8 additions & 1 deletion ibm/acctest/acctest.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ var (
Pi_shared_processor_pool_id string
Pi_snapshot_id string
Pi_spp_placement_group_id string
Pi_ssh_key_id string
Pi_storage_connection string
Pi_target_storage_tier string
Pi_virtual_serial_number string
Expand Down Expand Up @@ -1195,7 +1196,13 @@ func init() {
Pi_key_name = os.Getenv("PI_KEY_NAME")
if Pi_key_name == "" {
Pi_key_name = "terraform-test-power"
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'")
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'")
}

Pi_ssh_key_id = os.Getenv("PI_SSH_KEY_ID")
if Pi_ssh_key_id == "" {
Pi_ssh_key_id = "terraform-test-power"
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'")
}

Pi_network_name = os.Getenv("PI_NETWORK_NAME")
Expand Down
38 changes: 30 additions & 8 deletions ibm/service/power/data_source_ibm_pi_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ package power

import (
"context"
"fmt"
"log"

"github.com/IBM-Cloud/power-go-client/clients/instance"
"github.com/IBM-Cloud/power-go-client/helpers"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
Expand All @@ -25,11 +27,21 @@ func DataSourceIBMPIKey() *schema.Resource {
Type: schema.TypeString,
ValidateFunc: validation.NoZeroValues,
},

Arg_KeyName: {
Description: "User defined name for the SSH key.",
Required: true,
Type: schema.TypeString,
ValidateFunc: validation.NoZeroValues,
AtLeastOneOf: []string{Arg_SSHKeyID, Arg_KeyName},
ConflictsWith: []string{Arg_SSHKeyID},
Deprecated: "The pi_key_name field is deprecated. Please use pi_ssh_key_id instead",
Description: "The name of the SSH key.",
Optional: true,
Type: schema.TypeString,
},
Arg_SSHKeyID: {
AtLeastOneOf: []string{Arg_SSHKeyID, Arg_KeyName},
ConflictsWith: []string{Arg_KeyName},
Description: "The ID of the SSH key.",
Optional: true,
Type: schema.TypeString,
},

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

cloudInstanceID := d.Get(Arg_CloudInstanceID).(string)
var sshKeyID string
if v, ok := d.GetOk(Arg_SSHKeyID); ok {
sshKeyID = v.(string)
} else if v, ok := d.GetOk(Arg_KeyName); ok {
sshKeyID = v.(string)
}

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

d.SetId(*sshkeydata.Name)
Expand Down
6 changes: 3 additions & 3 deletions ibm/service/power/data_source_ibm_pi_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestAccIBMPIKeyDataSource_basic(t *testing.T) {
func testAccCheckIBMPIKeyDataSourceConfig() string {
return fmt.Sprintf(`
data "ibm_pi_key" "testacc_ds_key" {
pi_key_name = "%s"
pi_cloud_instance_id = "%s"
}`, acc.Pi_key_name, acc.Pi_cloud_instance_id)
pi_cloud_instance_id = "%[1]s"
pi_ssh_key_id = "%[2]s"
}`, acc.Pi_cloud_instance_id, acc.Pi_ssh_key_id)
}
1 change: 1 addition & 0 deletions ibm/service/power/ibm_pi_constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ const (
Arg_SPPPlacementGroupName = "pi_spp_placement_group_name"
Arg_SPPPlacementGroupPolicy = "pi_spp_placement_group_policy"
Arg_SSHKey = "pi_ssh_key"
Arg_SSHKeyID = "pi_ssh_key_id"
Arg_StartingIPAddress = "pi_starting_ip_address"
Arg_StorageConnection = "pi_storage_connection"
Arg_StoragePool = "pi_storage_pool"
Expand Down
3 changes: 2 additions & 1 deletion website/docs/d/pi_key.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ Example usage:
Review the argument references that you can specify for your data source.

- `pi_cloud_instance_id` - (Required, String) The GUID of the service instance associated with an account.
- `pi_key_name` - (Required, String) User defined name for the SSH key or SSH key ID.
- `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.
- `pi_ssh_key_id` - (Optional, String) The SSH key ID.

## Attribute Reference

Expand Down
Loading