Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor ssh key data source and documentation #32

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 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
52 changes: 19 additions & 33 deletions ibm/service/power/data_source_ibm_pi_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,79 +6,65 @@ package power
import (
"context"

"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"

"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/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

func DataSourceIBMPIKey() *schema.Resource {

return &schema.Resource{
ReadContext: dataSourceIBMPIKeyRead,
Schema: map[string]*schema.Schema{

// Arguments
Arg_KeyName: {
Type: schema.TypeString,
Arg_CloudInstanceID: {
Description: "The GUID of the service instance associated with an account.",
Required: true,
Description: "SSH key name for a pcloud tenant",
Type: schema.TypeString,
ValidateFunc: validation.NoZeroValues,
},
Arg_CloudInstanceID: {
Type: schema.TypeString,
Arg_KeyName: {
Required: true,
Description: "User defined name for the SSH key.",
ValidateFunc: validation.NoZeroValues,
Type: schema.TypeString,
},

// Attributes
Attr_KeyCreationDate: {
Type: schema.TypeString,
Attr_CreationDate: {
Computed: true,
Description: "Date of sshkey creation",
},
Attr_Key: {
Description: "Date of SSH Key creation.",
Type: schema.TypeString,
Sensitive: true,
Computed: true,
Description: "SSH RSA key",
},
"sshkey": {
Type: schema.TypeString,
Sensitive: true,
Computed: true,
Deprecated: "This field is deprecated, use ssh_key instead",
Attr_SSHKey: {
Computed: true,
Description: "SSH RSA key.",
Sensitive: true,
Type: schema.TypeString,
},
},
ismirlia marked this conversation as resolved.
Show resolved Hide resolved
}
}

func dataSourceIBMPIKeyRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {

// session
sess, err := meta.(conns.ClientSession).IBMPISession()
if err != nil {
return diag.FromErr(err)
}

// arguments
cloudInstanceID := d.Get(helpers.PICloudInstanceId).(string)
cloudInstanceID := d.Get(Arg_CloudInstanceID).(string)

// get key
sshkeyC := instance.NewIBMPIKeyClient(ctx, sess, cloudInstanceID)
sshkeydata, err := sshkeyC.Get(d.Get(helpers.PIKeyName).(string))
if err != nil {
return diag.FromErr(err)
}

// set attributes
d.SetId(*sshkeydata.Name)
d.Set(Attr_KeyCreationDate, sshkeydata.CreationDate.String())
d.Set(Attr_Key, sshkeydata.SSHKey)
d.Set("sshkey", sshkeydata.SSHKey) // TODO: deprecated, to remove
d.Set(Attr_CreationDate, sshkeydata.CreationDate.String())
d.Set(Attr_SSHKey, sshkeydata.SSHKey)

return nil
}
10 changes: 4 additions & 6 deletions ibm/service/power/data_source_ibm_pi_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
)

func TestAccIBMPIKeyDataSource_basic(t *testing.T) {

resource.Test(t, resource.TestCase{
PreCheck: func() { acc.TestAccPreCheck(t) },
Providers: acc.TestAccProviders,
Expand All @@ -30,9 +29,8 @@ 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)

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)
}
39 changes: 16 additions & 23 deletions ibm/service/power/data_source_ibm_pi_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,78 +13,71 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"

st "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"
)

func DataSourceIBMPIKeys() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceIBMPIKeysRead,
Schema: map[string]*schema.Schema{

// Arguments
Arg_CloudInstanceID: {
Type: schema.TypeString,
Description: "The GUID of the service instance associated with an account.",
Required: true,
Description: "PI cloud instance ID",
Type: schema.TypeString,
ValidateFunc: validation.NoZeroValues,
},

// Attributes
Attr_Keys: {
Type: schema.TypeList,
Computed: true,
Description: "SSH Keys",
Description: "List of all the SSH keys.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
Attr_KeyName: {
Type: schema.TypeString,
Attr_KeyCreationDate: {
Computed: true,
Description: "User defined name for the SSH key",
},
Attr_Key: {
Description: "Date of SSH key creation.",
Type: schema.TypeString,
Computed: true,
Description: "SSH RSA key",
},
Attr_KeyCreationDate: {
Attr_Name: {
Computed: true,
Description: "User defined name for the SSH key.",
Type: schema.TypeString,
},
Attr_SSHKey: {
Computed: true,
Description: "Date of SSH key creation",
Description: "SSH RSA key.",
Type: schema.TypeString,
},
},
},
Type: schema.TypeList,
},
},
}
}

func dataSourceIBMPIKeysRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {

// session
sess, err := meta.(conns.ClientSession).IBMPISession()
if err != nil {
return diag.FromErr(err)
}

// arguments
cloudInstanceID := d.Get(helpers.PICloudInstanceId).(string)
cloudInstanceID := d.Get(Arg_CloudInstanceID).(string)

// get keys
client := st.NewIBMPIKeyClient(ctx, sess, cloudInstanceID)
sshKeys, err := client.GetAll()
if err != nil {
log.Printf("[ERROR] get all keys failed %v", err)
return diag.FromErr(err)
}

// set attributes
result := make([]map[string]interface{}, 0, len(sshKeys.SSHKeys))
for _, sshKey := range sshKeys.SSHKeys {
key := map[string]interface{}{
Attr_KeyName: sshKey.Name,
Attr_Key: sshKey.SSHKey,
Attr_KeyCreationDate: sshKey.CreationDate.String(),
Attr_Name: sshKey.Name,
Attr_SSHKey: sshKey.SSHKey,
}
result = append(result, key)
}
Expand Down
3 changes: 1 addition & 2 deletions ibm/service/power/data_source_ibm_pi_keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,5 @@ func testAccCheckIBMPIKeysDataSourceConfig() string {
return fmt.Sprintf(`
data "ibm_pi_keys" "test" {
pi_cloud_instance_id = "%s"
}
`, acc.Pi_cloud_instance_id)
}`, acc.Pi_cloud_instance_id)
}
Loading
Loading