Skip to content

Commit 3f1ffd4

Browse files
[Resource] [Datasource] Replace SSH Key API with new API (IBM-Cloud#6375)
* [Resource] [Datasource] Replace SSH Key API with new API * Fix deprecation message * Update and tidy documentation * Tidy code and update unit-tests with new default --------- Co-authored-by: Alexander Kita <[email protected]>
1 parent e2aed76 commit 3f1ffd4

File tree

8 files changed

+300
-33
lines changed

8 files changed

+300
-33
lines changed

ibm/service/power/data_source_ibm_pi_key.go

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,37 @@ func DataSourceIBMPIKey() *schema.Resource {
3838
Description: "Date of SSH Key creation.",
3939
Type: schema.TypeString,
4040
},
41+
Attr_Description: {
42+
Computed: true,
43+
Description: "Description of the ssh key.",
44+
Type: schema.TypeString,
45+
},
46+
Attr_KeyName: {
47+
Computed: true,
48+
Description: "Name of SSH key.",
49+
Type: schema.TypeString,
50+
},
51+
Attr_PrimaryWorkspace: {
52+
Computed: true,
53+
Description: "Indicates if the current workspace owns the ssh key or not.",
54+
Type: schema.TypeBool,
55+
},
4156
Attr_SSHKey: {
4257
Computed: true,
4358
Description: "SSH RSA key.",
4459
Sensitive: true,
4560
Type: schema.TypeString,
4661
},
62+
Attr_SSHKeyID: {
63+
Computed: true,
64+
Description: "Unique ID of SSH key.",
65+
Type: schema.TypeString,
66+
},
67+
Attr_Visibility: {
68+
Computed: true,
69+
Description: "Visibility of the ssh key.",
70+
Type: schema.TypeString,
71+
},
4772
},
4873
}
4974
}
@@ -56,15 +81,20 @@ func dataSourceIBMPIKeyRead(ctx context.Context, d *schema.ResourceData, meta in
5681

5782
cloudInstanceID := d.Get(Arg_CloudInstanceID).(string)
5883

59-
sshkeyC := instance.NewIBMPIKeyClient(ctx, sess, cloudInstanceID)
84+
sshkeyC := instance.NewIBMPISSHKeyClient(ctx, sess, cloudInstanceID)
6085
sshkeydata, err := sshkeyC.Get(d.Get(helpers.PIKeyName).(string))
6186
if err != nil {
6287
return diag.FromErr(err)
6388
}
6489

6590
d.SetId(*sshkeydata.Name)
6691
d.Set(Attr_CreationDate, sshkeydata.CreationDate.String())
92+
d.Set(Attr_Description, sshkeydata.Description)
93+
d.Set(Attr_KeyName, sshkeydata.Name)
94+
d.Set(Attr_PrimaryWorkspace, sshkeydata.PrimaryWorkspace)
6795
d.Set(Attr_SSHKey, sshkeydata.SSHKey)
96+
d.Set(Attr_SSHKeyID, sshkeydata.ID)
97+
d.Set(Attr_Visibility, sshkeydata.Visibility)
6898

6999
return nil
70100
}

ibm/service/power/data_source_ibm_pi_keys.go

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,36 @@ func DataSourceIBMPIKeys() *schema.Resource {
3838
Description: "Date of SSH key creation.",
3939
Type: schema.TypeString,
4040
},
41+
Attr_Description: {
42+
Computed: true,
43+
Description: "Description of the ssh key.",
44+
Type: schema.TypeString,
45+
},
4146
Attr_Name: {
4247
Computed: true,
4348
Description: "User defined name for the SSH key.",
4449
Type: schema.TypeString,
4550
},
51+
Attr_PrimaryWorkspace: {
52+
Computed: true,
53+
Description: "Indicates if the current workspace owns the ssh key or not.",
54+
Type: schema.TypeBool,
55+
},
4656
Attr_SSHKey: {
4757
Computed: true,
4858
Description: "SSH RSA key.",
4959
Type: schema.TypeString,
5060
},
61+
Attr_SSHKeyID: {
62+
Computed: true,
63+
Description: "Unique ID of SSH key.",
64+
Type: schema.TypeString,
65+
},
66+
Attr_Visibility: {
67+
Computed: true,
68+
Description: "Visibility of the ssh key.",
69+
Type: schema.TypeString,
70+
},
5171
},
5272
},
5373
Type: schema.TypeList,
@@ -64,7 +84,7 @@ func dataSourceIBMPIKeysRead(ctx context.Context, d *schema.ResourceData, meta i
6484

6585
cloudInstanceID := d.Get(Arg_CloudInstanceID).(string)
6686

67-
client := instance.NewIBMPIKeyClient(ctx, sess, cloudInstanceID)
87+
client := instance.NewIBMPISSHKeyClient(ctx, sess, cloudInstanceID)
6888
sshKeys, err := client.GetAll()
6989
if err != nil {
7090
log.Printf("[ERROR] get all keys failed %v", err)
@@ -74,9 +94,13 @@ func dataSourceIBMPIKeysRead(ctx context.Context, d *schema.ResourceData, meta i
7494
result := make([]map[string]interface{}, 0, len(sshKeys.SSHKeys))
7595
for _, sshKey := range sshKeys.SSHKeys {
7696
key := map[string]interface{}{
77-
Attr_CreationDate: sshKey.CreationDate.String(),
78-
Attr_Name: sshKey.Name,
79-
Attr_SSHKey: sshKey.SSHKey,
97+
Attr_CreationDate: sshKey.CreationDate.String(),
98+
Attr_Description: sshKey.Description,
99+
Attr_Name: sshKey.Name,
100+
Attr_PrimaryWorkspace: sshKey.PrimaryWorkspace,
101+
Attr_SSHKey: sshKey.SSHKey,
102+
Attr_SSHKeyID: sshKey.ID,
103+
Attr_Visibility: sshKey.Visibility,
80104
}
81105
result = append(result, key)
82106
}

ibm/service/power/ibm_pi_constants.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ const (
158158
Arg_VirtualCoresAssigned = "pi_virtual_cores_assigned"
159159
Arg_VirtualOpticalDevice = "pi_virtual_optical_device"
160160
Arg_VirtualSerialNumber = "pi_virtual_serial_number"
161+
Arg_Visibility = "pi_visibility"
161162
Arg_VolumeCloneName = "pi_volume_clone_name"
162163
Arg_VolumeCloneTaskID = "pi_volume_clone_task_id"
163164
Arg_VolumeGroupAction = "pi_volume_group_action"
@@ -389,6 +390,7 @@ const (
389390
Attr_PowerEdgeRouter = "power_edge_router"
390391
Attr_Primary = "primary"
391392
Attr_PrimaryRole = "primary_role"
393+
Attr_PrimaryWorkspace = "primary_workspace"
392394
Attr_Processors = "processors"
393395
Attr_ProcType = "proctype"
394396
Attr_Product = "product"
@@ -446,6 +448,7 @@ const (
446448
Attr_SPPPlacementGroupPolicy = "policy"
447449
Attr_SPPPlacementGroups = "spp_placement_groups"
448450
Attr_SSHKey = "ssh_key"
451+
Attr_SSHKeyID = "ssh_key_id"
449452
Attr_Start = "start"
450453
Attr_StartTime = "start_time"
451454
Attr_State = "state"
@@ -497,6 +500,7 @@ const (
497500
Attr_VirtualCoresAssigned = "virtual_cores_assigned"
498501
Attr_VirtualSerialNumber = "virtual_serial_number"
499502
Attr_VirtualSerialNumbers = "virtual_serial_numbers"
503+
Attr_Visibility = "visibility"
500504
Attr_VLanID = "vlan_id"
501505
Attr_VolumeGroupID = "volume_group_id"
502506
Attr_VolumeGroupName = "volume_group_name"
@@ -528,6 +532,7 @@ const (
528532
StockVTL = "stock-vtl"
529533

530534
// Allowed Values
535+
Account = "account"
531536
Affinity = "affinity"
532537
All = "all"
533538
Allow = "allow"
@@ -591,6 +596,7 @@ const (
591596
Vlan = "vlan"
592597
vSCSI = "vSCSI"
593598
Warning = "WARNING"
599+
Workspace = "workspace"
594600

595601
// Actions
596602
Action_HardReboot = "hard-reboot"

ibm/service/power/resource_ibm_pi_key.go

Lines changed: 108 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,28 @@ import (
1313
"github.com/IBM-Cloud/power-go-client/power/models"
1414
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
1515
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
16+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
1617
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1718
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
1819
)
1920

2021
func ResourceIBMPIKey() *schema.Resource {
2122
return &schema.Resource{
23+
CustomizeDiff: customdiff.Sequence(
24+
func(_ context.Context, diff *schema.ResourceDiff, v interface{}) error {
25+
return customizeNameAndSSHKeyPIKeyDiff(diff)
26+
},
27+
),
2228
CreateContext: resourceIBMPIKeyCreate,
2329
ReadContext: resourceIBMPIKeyRead,
2430
UpdateContext: resourceIBMPIKeyUpdate,
2531
DeleteContext: resourceIBMPIKeyDelete,
2632
Importer: &schema.ResourceImporter{},
2733

2834
Timeouts: &schema.ResourceTimeout{
29-
Create: schema.DefaultTimeout(60 * time.Minute),
30-
Delete: schema.DefaultTimeout(60 * time.Minute),
35+
Create: schema.DefaultTimeout(10 * time.Minute),
36+
Update: schema.DefaultTimeout(10 * time.Minute),
37+
Delete: schema.DefaultTimeout(10 * time.Minute),
3138
},
3239

3340
Schema: map[string]*schema.Schema{
@@ -39,6 +46,11 @@ func ResourceIBMPIKey() *schema.Resource {
3946
Type: schema.TypeString,
4047
ValidateFunc: validation.NoZeroValues,
4148
},
49+
Arg_Description: {
50+
Description: "Description of the ssh key.",
51+
Optional: true,
52+
Type: schema.TypeString,
53+
},
4254
Arg_KeyName: {
4355
Description: "User defined name for the SSH key.",
4456
Required: true,
@@ -51,7 +63,13 @@ func ResourceIBMPIKey() *schema.Resource {
5163
Type: schema.TypeString,
5264
ValidateFunc: validation.NoZeroValues,
5365
},
54-
66+
Arg_Visibility: {
67+
Default: Workspace,
68+
Description: "Visibility of the ssh key. Valid values are: [\"account\", \"workspace\"].",
69+
Optional: true,
70+
Type: schema.TypeString,
71+
ValidateFunc: validation.StringInSlice([]string{Account, Workspace}, false),
72+
},
5573
// Attributes
5674
Attr_CreationDate: {
5775
Computed: true,
@@ -60,14 +78,26 @@ func ResourceIBMPIKey() *schema.Resource {
6078
},
6179
Attr_Name: {
6280
Computed: true,
81+
Deprecated: "This field is deprecated and will be removed in a future release. Use pi_key_name instead.",
6382
Description: "User defined name for the SSH key.",
6483
Type: schema.TypeString,
6584
},
6685
Attr_Key: {
6786
Computed: true,
87+
Deprecated: "This field is deprecated and will be removed in a future release. Use pi_ssh_key instead.",
6888
Description: "SSH RSA key.",
6989
Type: schema.TypeString,
7090
},
91+
Attr_PrimaryWorkspace: {
92+
Computed: true,
93+
Description: "Indicates if the current workspace owns the ssh key or not.",
94+
Type: schema.TypeBool,
95+
},
96+
Attr_SSHKeyID: {
97+
Computed: true,
98+
Description: "Unique ID of SSH key.",
99+
Type: schema.TypeString,
100+
},
71101
},
72102
}
73103
}
@@ -83,21 +113,29 @@ func resourceIBMPIKeyCreate(ctx context.Context, d *schema.ResourceData, meta in
83113
cloudInstanceID := d.Get(Arg_CloudInstanceID).(string)
84114
name := d.Get(Arg_KeyName).(string)
85115
sshkey := d.Get(Arg_SSHKey).(string)
116+
visibility := d.Get(Arg_Visibility).(string)
86117

87118
// create key
88-
client := instance.NewIBMPIKeyClient(ctx, sess, cloudInstanceID)
89-
body := &models.SSHKey{
90-
Name: &name,
91-
SSHKey: &sshkey,
119+
client := instance.NewIBMPISSHKeyClient(ctx, sess, cloudInstanceID)
120+
body := &models.CreateWorkspaceSSHKey{
121+
Name: &name,
122+
SSHKey: &sshkey,
123+
Visibility: &visibility,
92124
}
125+
126+
if v, ok := d.GetOk(Arg_Description); ok {
127+
description := v.(string)
128+
body.Description = description
129+
}
130+
93131
sshResponse, err := client.Create(body)
94132
if err != nil {
95133
log.Printf("[DEBUG] err %s", err)
96134
return diag.FromErr(err)
97135
}
98136

99137
log.Printf("Printing the sshkey %+v", *sshResponse)
100-
d.SetId(fmt.Sprintf("%s/%s", cloudInstanceID, name))
138+
d.SetId(fmt.Sprintf("%s/%s", cloudInstanceID, *sshResponse.ID))
101139
return resourceIBMPIKeyRead(ctx, d, meta)
102140
}
103141

@@ -115,21 +153,70 @@ func resourceIBMPIKeyRead(ctx context.Context, d *schema.ResourceData, meta inte
115153
}
116154

117155
// get key
118-
sshkeyC := instance.NewIBMPIKeyClient(ctx, sess, cloudInstanceID)
156+
sshkeyC := instance.NewIBMPISSHKeyClient(ctx, sess, cloudInstanceID)
119157
sshkeydata, err := sshkeyC.Get(key)
120158
if err != nil {
121159
return diag.FromErr(err)
122160
}
123161

124-
// set attributes
162+
// Arguments
163+
d.Set(Arg_CloudInstanceID, cloudInstanceID)
164+
d.Set(Arg_Description, sshkeydata.Description)
165+
d.Set(Arg_KeyName, sshkeydata.Name)
166+
d.Set(Arg_SSHKey, sshkeydata.SSHKey)
167+
d.Set(Arg_Visibility, sshkeydata.Visibility)
168+
169+
// Attributes
125170
d.Set(Attr_CreationDate, sshkeydata.CreationDate.String())
126171
d.Set(Attr_Key, sshkeydata.SSHKey)
127172
d.Set(Attr_Name, sshkeydata.Name)
173+
d.Set(Attr_PrimaryWorkspace, sshkeydata.PrimaryWorkspace)
174+
d.Set(Attr_SSHKeyID, sshkeydata.ID)
128175

129176
return nil
130177
}
131178

132179
func resourceIBMPIKeyUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
180+
// session
181+
sess, err := meta.(conns.ClientSession).IBMPISession()
182+
if err != nil {
183+
return diag.FromErr(err)
184+
}
185+
186+
// arguments
187+
cloudInstanceID, key, err := splitID(d.Id())
188+
if err != nil {
189+
return diag.FromErr(err)
190+
}
191+
192+
client := instance.NewIBMPISSHKeyClient(ctx, sess, cloudInstanceID)
193+
updateBody := &models.UpdateWorkspaceSSHKey{}
194+
195+
if d.HasChange(Arg_Description) {
196+
newDescription := d.Get(Arg_Description).(string)
197+
updateBody.Description = &newDescription
198+
}
199+
200+
if d.HasChange(Arg_KeyName) {
201+
newKeyName := d.Get(Arg_KeyName).(string)
202+
updateBody.Name = &newKeyName
203+
}
204+
205+
if d.HasChange(Arg_SSHKey) {
206+
newSSHKey := d.Get(Arg_SSHKey).(string)
207+
updateBody.SSHKey = &newSSHKey
208+
}
209+
210+
if d.HasChange(Arg_Visibility) {
211+
newVisibility := d.Get(Arg_Visibility).(string)
212+
updateBody.Visibility = &newVisibility
213+
}
214+
215+
_, err = client.Update(key, updateBody)
216+
if err != nil {
217+
return diag.FromErr(err)
218+
}
219+
133220
return resourceIBMPIKeyRead(ctx, d, meta)
134221
}
135222

@@ -147,11 +234,21 @@ func resourceIBMPIKeyDelete(ctx context.Context, d *schema.ResourceData, meta in
147234
}
148235

149236
// delete key
150-
sshkeyC := instance.NewIBMPIKeyClient(ctx, sess, cloudInstanceID)
237+
sshkeyC := instance.NewIBMPISSHKeyClient(ctx, sess, cloudInstanceID)
151238
err = sshkeyC.Delete(key)
152239
if err != nil {
153240
return diag.FromErr(err)
154241
}
155242
d.SetId("")
156243
return nil
157244
}
245+
246+
func customizeNameAndSSHKeyPIKeyDiff(diff *schema.ResourceDiff) error {
247+
if diff.Id() != "" && diff.HasChange(Arg_KeyName) {
248+
diff.SetNewComputed(Attr_Name)
249+
}
250+
if diff.Id() != "" && diff.HasChange(Arg_SSHKey) {
251+
diff.SetNewComputed(Attr_Key)
252+
}
253+
return nil
254+
}

0 commit comments

Comments
 (0)