@@ -13,28 +13,21 @@ 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"
1716 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1817 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
1918)
2019
2120func ResourceIBMPIKey () * schema.Resource {
2221 return & schema.Resource {
23- CustomizeDiff : customdiff .Sequence (
24- func (_ context.Context , diff * schema.ResourceDiff , v interface {}) error {
25- return customizeNameAndSSHKeyPIKeyDiff (diff )
26- },
27- ),
2822 CreateContext : resourceIBMPIKeyCreate ,
2923 ReadContext : resourceIBMPIKeyRead ,
3024 UpdateContext : resourceIBMPIKeyUpdate ,
3125 DeleteContext : resourceIBMPIKeyDelete ,
3226 Importer : & schema.ResourceImporter {},
3327
3428 Timeouts : & schema.ResourceTimeout {
35- Create : schema .DefaultTimeout (10 * time .Minute ),
36- Update : schema .DefaultTimeout (10 * time .Minute ),
37- Delete : schema .DefaultTimeout (10 * time .Minute ),
29+ Create : schema .DefaultTimeout (60 * time .Minute ),
30+ Delete : schema .DefaultTimeout (60 * time .Minute ),
3831 },
3932
4033 Schema : map [string ]* schema.Schema {
@@ -46,11 +39,6 @@ func ResourceIBMPIKey() *schema.Resource {
4639 Type : schema .TypeString ,
4740 ValidateFunc : validation .NoZeroValues ,
4841 },
49- Arg_Description : {
50- Description : "Description of the ssh key." ,
51- Optional : true ,
52- Type : schema .TypeString ,
53- },
5442 Arg_KeyName : {
5543 Description : "User defined name for the SSH key." ,
5644 Required : true ,
@@ -63,13 +51,7 @@ func ResourceIBMPIKey() *schema.Resource {
6351 Type : schema .TypeString ,
6452 ValidateFunc : validation .NoZeroValues ,
6553 },
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- },
54+
7355 // Attributes
7456 Attr_CreationDate : {
7557 Computed : true ,
@@ -78,26 +60,14 @@ func ResourceIBMPIKey() *schema.Resource {
7860 },
7961 Attr_Name : {
8062 Computed : true ,
81- Deprecated : "This field is deprecated and will be removed in a future release. Use pi_key_name instead." ,
8263 Description : "User defined name for the SSH key." ,
8364 Type : schema .TypeString ,
8465 },
8566 Attr_Key : {
8667 Computed : true ,
87- Deprecated : "This field is deprecated and will be removed in a future release. Use pi_ssh_key instead." ,
8868 Description : "SSH RSA key." ,
8969 Type : schema .TypeString ,
9070 },
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- },
10171 },
10272 }
10373}
@@ -113,29 +83,21 @@ func resourceIBMPIKeyCreate(ctx context.Context, d *schema.ResourceData, meta in
11383 cloudInstanceID := d .Get (Arg_CloudInstanceID ).(string )
11484 name := d .Get (Arg_KeyName ).(string )
11585 sshkey := d .Get (Arg_SSHKey ).(string )
116- visibility := d .Get (Arg_Visibility ).(string )
11786
11887 // create key
119- client := instance .NewIBMPISSHKeyClient (ctx , sess , cloudInstanceID )
120- body := & models.CreateWorkspaceSSHKey {
121- Name : & name ,
122- SSHKey : & sshkey ,
123- Visibility : & visibility ,
88+ client := instance .NewIBMPIKeyClient (ctx , sess , cloudInstanceID )
89+ body := & models.SSHKey {
90+ Name : & name ,
91+ SSHKey : & sshkey ,
12492 }
125-
126- if v , ok := d .GetOk (Arg_Description ); ok {
127- description := v .(string )
128- body .Description = description
129- }
130-
13193 sshResponse , err := client .Create (body )
13294 if err != nil {
13395 log .Printf ("[DEBUG] err %s" , err )
13496 return diag .FromErr (err )
13597 }
13698
13799 log .Printf ("Printing the sshkey %+v" , * sshResponse )
138- d .SetId (fmt .Sprintf ("%s/%s" , cloudInstanceID , * sshResponse . ID ))
100+ d .SetId (fmt .Sprintf ("%s/%s" , cloudInstanceID , name ))
139101 return resourceIBMPIKeyRead (ctx , d , meta )
140102}
141103
@@ -153,70 +115,21 @@ func resourceIBMPIKeyRead(ctx context.Context, d *schema.ResourceData, meta inte
153115 }
154116
155117 // get key
156- sshkeyC := instance .NewIBMPISSHKeyClient (ctx , sess , cloudInstanceID )
118+ sshkeyC := instance .NewIBMPIKeyClient (ctx , sess , cloudInstanceID )
157119 sshkeydata , err := sshkeyC .Get (key )
158120 if err != nil {
159121 return diag .FromErr (err )
160122 }
161123
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
124+ // set attributes
170125 d .Set (Attr_CreationDate , sshkeydata .CreationDate .String ())
171126 d .Set (Attr_Key , sshkeydata .SSHKey )
172127 d .Set (Attr_Name , sshkeydata .Name )
173- d .Set (Attr_PrimaryWorkspace , sshkeydata .PrimaryWorkspace )
174- d .Set (Attr_SSHKeyID , sshkeydata .ID )
175128
176129 return nil
177130}
178131
179132func 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-
220133 return resourceIBMPIKeyRead (ctx , d , meta )
221134}
222135
@@ -234,21 +147,11 @@ func resourceIBMPIKeyDelete(ctx context.Context, d *schema.ResourceData, meta in
234147 }
235148
236149 // delete key
237- sshkeyC := instance .NewIBMPISSHKeyClient (ctx , sess , cloudInstanceID )
150+ sshkeyC := instance .NewIBMPIKeyClient (ctx , sess , cloudInstanceID )
238151 err = sshkeyC .Delete (key )
239152 if err != nil {
240153 return diag .FromErr (err )
241154 }
242155 d .SetId ("" )
243156 return nil
244157}
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