@@ -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
2021func 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
132179func 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