88 "strings"
99
1010 "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault"
11- "github.com/stackitcloud/stackit-sdk-go/services/kms/wait"
11+ "github.com/stackitcloud/stackit-sdk-go/services/kms/v1api/ wait"
1212
1313 "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
1414 "github.com/hashicorp/terraform-plugin-framework/resource"
@@ -20,7 +20,7 @@ import (
2020 "github.com/hashicorp/terraform-plugin-log/tflog"
2121 "github.com/stackitcloud/stackit-sdk-go/core/oapierror"
2222 sdkUtils "github.com/stackitcloud/stackit-sdk-go/core/utils"
23- "github.com/stackitcloud/stackit-sdk-go/services/kms"
23+ kms "github.com/stackitcloud/stackit-sdk-go/services/kms/v1api "
2424 "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion"
2525 "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
2626 kmsUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/kms/utils"
@@ -259,29 +259,28 @@ func (r *keyResource) Create(ctx context.Context, req resource.CreateRequest, re
259259 return
260260 }
261261
262- createResponse , err := r .client .CreateKey (ctx , projectId , region , keyRingId ).CreateKeyPayload (* payload ).Execute ()
262+ createResponse , err := r .client .DefaultAPI . CreateKey (ctx , projectId , region , keyRingId ).CreateKeyPayload (* payload ).Execute ()
263263 if err != nil {
264264 core .LogAndAddError (ctx , & resp .Diagnostics , "Error creating key" , fmt .Sprintf ("Calling API: %v" , err ))
265265 return
266266 }
267267
268268 ctx = core .LogResponse (ctx )
269269
270- if createResponse == nil || createResponse . Id == nil {
270+ if createResponse == nil {
271271 core .LogAndAddError (ctx , & resp .Diagnostics , "Error creating key" , "API returned empty response" )
272272 return
273273 }
274274
275- keyId := * createResponse .Id
276275 // Write id attributes to state before polling via the wait handler - just in case anything goes wrong during the wait handler
277276 ctx = utils .SetAndLogStateFields (ctx , & resp .Diagnostics , & resp .State , map [string ]any {
278277 "project_id" : projectId ,
279278 "region" : region ,
280279 "keyring_id" : keyRingId ,
281- "key_id" : keyId ,
280+ "key_id" : createResponse . Id ,
282281 })
283282
284- waitHandlerResp , err := wait .CreateOrUpdateKeyWaitHandler (ctx , r .client , projectId , region , keyRingId , keyId ).WaitWithContext (ctx )
283+ waitHandlerResp , err := wait .CreateOrUpdateKeyWaitHandler (ctx , r .client . DefaultAPI , projectId , region , keyRingId , createResponse . Id ).WaitWithContext (ctx )
285284 if err != nil {
286285 core .LogAndAddError (ctx , & resp .Diagnostics , "Error waiting for key creation" , fmt .Sprintf ("Calling API: %v" , err ))
287286 return
@@ -321,7 +320,7 @@ func (r *keyResource) Read(ctx context.Context, req resource.ReadRequest, resp *
321320 ctx = tflog .SetField (ctx , "region" , region )
322321 ctx = tflog .SetField (ctx , "key_id" , keyId )
323322
324- keyResponse , err := r .client .GetKey (ctx , projectId , region , keyRingId , keyId ).Execute ()
323+ keyResponse , err := r .client .DefaultAPI . GetKey (ctx , projectId , region , keyRingId , keyId ).Execute ()
325324 if err != nil {
326325 var oapiErr * oapierror.GenericOpenAPIError
327326 ok := errors .As (err , & oapiErr )
@@ -368,7 +367,7 @@ func (r *keyResource) Delete(ctx context.Context, req resource.DeleteRequest, re
368367 region := r .providerData .GetRegionWithOverride (model .Region )
369368 keyId := model .KeyId .ValueString ()
370369
371- err := r .client .DeleteKey (ctx , projectId , region , keyRingId , keyId ).Execute ()
370+ err := r .client .DefaultAPI . DeleteKey (ctx , projectId , region , keyRingId , keyId ).Execute ()
372371 if err != nil {
373372 core .LogAndAddError (ctx , & resp .Diagnostics , "Error deleting key" , fmt .Sprintf ("Calling API: %v" , err ))
374373 }
@@ -413,28 +412,20 @@ func mapFields(key *kms.Key, model *Model, region string) error {
413412 var keyId string
414413 if model .KeyId .ValueString () != "" {
415414 keyId = model .KeyId .ValueString ()
416- } else if key .Id != nil {
417- keyId = * key .Id
418415 } else {
419- return fmt . Errorf ( "key id not present" )
416+ keyId = key . Id
420417 }
421418
422419 model .Id = utils .BuildInternalTerraformId (model .ProjectId .ValueString (), region , model .KeyRingId .ValueString (), keyId )
423420 model .KeyId = types .StringValue (keyId )
424- model .DisplayName = types .StringPointerValue (key .DisplayName )
421+ model .DisplayName = types .StringValue (key .DisplayName )
425422 model .Region = types .StringValue (region )
426- model .ImportOnly = types .BoolPointerValue (key .ImportOnly )
423+ model .ImportOnly = types .BoolValue (key .ImportOnly )
427424 model .AccessScope = types .StringValue (string (key .GetAccessScope ()))
428425 model .Algorithm = types .StringValue (string (key .GetAlgorithm ()))
429426 model .Purpose = types .StringValue (string (key .GetPurpose ()))
430427 model .Protection = types .StringValue (string (key .GetProtection ()))
431-
432- // TODO: workaround - remove once STACKITKMS-377 is resolved (just write the return value from the API to the state then)
433- if ! (model .Description .IsNull () && key .Description != nil && * key .Description == "" ) {
434- model .Description = types .StringPointerValue (key .Description )
435- } else {
436- model .Description = types .StringNull ()
437- }
428+ model .Description = types .StringPointerValue (key .Description )
438429
439430 return nil
440431}
@@ -443,13 +434,19 @@ func toCreatePayload(model *Model) (*kms.CreateKeyPayload, error) {
443434 if model == nil {
444435 return nil , fmt .Errorf ("nil model" )
445436 }
437+
438+ var accessScope * kms.AccessScope
439+ if ! utils .IsUndefined (model .AccessScope ) {
440+ accessScope = new (kms.AccessScope (model .AccessScope .ValueString ()))
441+ }
442+
446443 return & kms.CreateKeyPayload {
447- AccessScope : kms . CreateKeyPayloadGetAccessScopeAttributeType ( conversion . StringValueToPointer ( model . AccessScope )) ,
448- Algorithm : kms .CreateKeyPayloadGetAlgorithmAttributeType ( conversion . StringValueToPointer ( model .Algorithm )),
449- Protection : kms .CreateKeyPayloadGetProtectionAttributeType ( conversion . StringValueToPointer ( model .Protection )),
444+ AccessScope : accessScope ,
445+ Algorithm : kms .Algorithm ( model .Algorithm . ValueString ( )),
446+ Protection : kms .Protection ( model .Protection . ValueString ( )),
450447 Description : conversion .StringValueToPointer (model .Description ),
451- DisplayName : conversion . StringValueToPointer ( model .DisplayName ),
448+ DisplayName : model .DisplayName . ValueString ( ),
452449 ImportOnly : conversion .BoolValueToPointer (model .ImportOnly ),
453- Purpose : kms .CreateKeyPayloadGetPurposeAttributeType ( conversion . StringValueToPointer ( model .Purpose )),
450+ Purpose : kms .Purpose ( model .Purpose . ValueString ( )),
454451 }, nil
455452}
0 commit comments