@@ -2,15 +2,19 @@ package keymanager
22
33import (
44 "context"
5+ "fmt"
56
7+ "github.com/hashicorp/go-cty/cty"
68 "github.com/hashicorp/terraform-plugin-sdk/v2/diag"
9+ "github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
710 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
811 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
912 key_manager "github.com/scaleway/scaleway-sdk-go/api/key_manager/v1alpha1"
1013 "github.com/scaleway/terraform-provider-scaleway/v2/internal/dsf"
1114 "github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional"
1215 "github.com/scaleway/terraform-provider-scaleway/v2/internal/services/account"
1316 "github.com/scaleway/terraform-provider-scaleway/v2/internal/types"
17+ "github.com/scaleway/terraform-provider-scaleway/v2/internal/verify"
1418)
1519
1620func ResourceKeyManagerKey () * schema.Resource {
@@ -19,6 +23,9 @@ func ResourceKeyManagerKey() *schema.Resource {
1923 ReadContext : resourceKeyManagerKeyRead ,
2024 UpdateContext : resourceKeyManagerKeyUpdate ,
2125 DeleteContext : resourceKeyManagerKeyDelete ,
26+ CustomizeDiff : customdiff .All (
27+ validateUsageAlgorithmCombination (),
28+ ),
2229 Schema : map [string ]* schema.Schema {
2330 "name" : {
2431 Type : schema .TypeString ,
@@ -33,7 +40,32 @@ func ResourceKeyManagerKey() *schema.Resource {
3340 ValidateFunc : validation .StringInSlice ([]string {
3441 "symmetric_encryption" , "asymmetric_encryption" , "asymmetric_signing" ,
3542 }, false ),
36- Description : "Key usage. Keys with a usage set to 'symmetric_encryption' can encrypt and decrypt data using the AES-256-GCM key algorithm. Possible values: symmetric_encryption, asymmetric_encryption, asymmetric_signing." ,
43+ Description : "Key usage type. Possible values: symmetric_encryption, asymmetric_encryption, asymmetric_signing." ,
44+ },
45+ "algorithm" : {
46+ Type : schema .TypeString ,
47+ Required : true ,
48+ Description : "Algorithm to use for the key. The valid algorithms depend on the usage type." ,
49+ ValidateDiagFunc : func (i any , p cty.Path ) diag.Diagnostics {
50+ var allKnownAlgos []string
51+
52+ symAlgos := key_manager .KeyAlgorithmSymmetricEncryption ("" ).Values ()
53+ for _ , algo := range symAlgos {
54+ allKnownAlgos = append (allKnownAlgos , string (algo ))
55+ }
56+
57+ asymEncAlgos := key_manager .KeyAlgorithmAsymmetricEncryption ("" ).Values ()
58+ for _ , algo := range asymEncAlgos {
59+ allKnownAlgos = append (allKnownAlgos , string (algo ))
60+ }
61+
62+ asymSignAlgos := key_manager .KeyAlgorithmAsymmetricSigning ("" ).Values ()
63+ for _ , algo := range asymSignAlgos {
64+ allKnownAlgos = append (allKnownAlgos , string (algo ))
65+ }
66+
67+ return verify .ValidateStringInSliceWithWarning (allKnownAlgos , "algorithm" )(i , p )
68+ },
3769 },
3870 "description" : {
3971 Type : schema .TypeString ,
@@ -116,7 +148,15 @@ func resourceKeyManagerKeyCreate(ctx context.Context, d *schema.ResourceData, m
116148 createReq .Origin = key_manager .KeyOrigin (v .(string ))
117149 }
118150
119- createReq .Usage = ExpandKeyUsage (d .Get ("usage" ).(string ))
151+ usage := d .Get ("usage" ).(string )
152+ algorithm := d .Get ("algorithm" ).(string )
153+
154+ keyUsage , err := expandUsageAlgorithm (usage , algorithm )
155+ if err != nil {
156+ return diag .FromErr (err )
157+ }
158+
159+ createReq .Usage = keyUsage
120160
121161 key , err := api .CreateKey (createReq )
122162 if err != nil {
@@ -145,7 +185,13 @@ func resourceKeyManagerKeyRead(ctx context.Context, d *schema.ResourceData, m an
145185 _ = d .Set ("name" , key .Name )
146186 _ = d .Set ("project_id" , key .ProjectID )
147187 _ = d .Set ("region" , key .Region .String ())
148- _ = d .Set ("usage" , UsageToString (key .Usage ))
188+
189+ usageType := UsageToString (key .Usage )
190+ algorithm := AlgorithmFromKeyUsage (key .Usage )
191+
192+ _ = d .Set ("usage" , usageType )
193+ _ = d .Set ("algorithm" , algorithm )
194+
149195 _ = d .Set ("description" , key .Description )
150196 _ = d .Set ("tags" , key .Tags )
151197 _ = d .Set ("rotation_count" , int (key .RotationCount ))
@@ -222,3 +268,31 @@ func resourceKeyManagerKeyDelete(ctx context.Context, d *schema.ResourceData, m
222268
223269 return nil
224270}
271+
272+ func validateUsageAlgorithmCombination () schema.CustomizeDiffFunc {
273+ return func (ctx context.Context , diff * schema.ResourceDiff , _ any ) error {
274+ return nil
275+ }
276+ }
277+
278+ func expandUsageAlgorithm (usage , algorithm string ) (* key_manager.KeyUsage , error ) {
279+ switch usage {
280+ case usageSymmetricEncryption :
281+ typedAlgo := key_manager .KeyAlgorithmSymmetricEncryption (algorithm )
282+
283+ return & key_manager.KeyUsage {SymmetricEncryption : & typedAlgo }, nil
284+
285+ case usageAsymmetricEncryption :
286+ typedAlgo := key_manager .KeyAlgorithmAsymmetricEncryption (algorithm )
287+
288+ return & key_manager.KeyUsage {AsymmetricEncryption : & typedAlgo }, nil
289+
290+ case usageAsymmetricSigning :
291+ typedAlgo := key_manager .KeyAlgorithmAsymmetricSigning (algorithm )
292+
293+ return & key_manager.KeyUsage {AsymmetricSigning : & typedAlgo }, nil
294+
295+ default :
296+ return nil , fmt .Errorf ("unknown usage type: %s" , usage )
297+ }
298+ }
0 commit comments