@@ -4,12 +4,11 @@ import (
44 "context"
55 "errors"
66 "fmt"
7- "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
87 "net/http"
9- "regexp"
108 "strings"
119 "time"
1210
11+ "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
1312 "github.com/hashicorp/terraform-plugin-framework/resource"
1413 "github.com/hashicorp/terraform-plugin-framework/resource/schema"
1514 "github.com/hashicorp/terraform-plugin-framework/resource/schema/mapplanmodifier"
@@ -151,7 +150,7 @@ func (r *tokenResource) Configure(ctx context.Context, req resource.ConfigureReq
151150
152151// ModifyPlan implements resource.ResourceWithModifyPlan.
153152// Use the modifier to set the effective region in the current plan.
154- func (r * tokenResource ) ModifyPlan (ctx context.Context , req resource.ModifyPlanRequest , resp * resource.ModifyPlanResponse ) {
153+ func (r * tokenResource ) ModifyPlan (ctx context.Context , req resource.ModifyPlanRequest , resp * resource.ModifyPlanResponse ) { // nolint:gocritic // function signature required by Terraform
155154 var configModel Model
156155
157156 // skip initial empty configuration to avoid follow-up errors
@@ -249,15 +248,13 @@ func (r *tokenResource) Schema(_ context.Context, _ resource.SchemaRequest, resp
249248 Optional : true ,
250249 Validators : []validator.String {
251250 stringvalidator .LengthBetween (1 , 2000 ),
252- stringvalidator .RegexMatches (regexp .MustCompile (`^[0-9a-zA-Z\s.:\/\-]+$` ), "" ),
253251 },
254252 },
255253 "name" : schema.StringAttribute {
256254 Description : "Name of the model serving auth token." ,
257255 Required : true ,
258256 Validators : []validator.String {
259257 stringvalidator .LengthBetween (1 , 200 ),
260- stringvalidator .RegexMatches (regexp .MustCompile (`^[0-9a-zA-Z\s_-]+$` ), "" ),
261258 },
262259 },
263260 "state" : schema.StringAttribute {
@@ -278,7 +275,7 @@ func (r *tokenResource) Schema(_ context.Context, _ resource.SchemaRequest, resp
278275}
279276
280277// Create creates the resource and sets the initial Terraform state.
281- func (r * tokenResource ) Create (ctx context.Context , req resource.CreateRequest , resp * resource.CreateResponse ) {
278+ func (r * tokenResource ) Create (ctx context.Context , req resource.CreateRequest , resp * resource.CreateResponse ) { // nolint:gocritic // function signature required by Terraform
282279 // Retrieve values from plan
283280 var model Model
284281 diags := req .Plan .Get (ctx , & model )
@@ -307,7 +304,7 @@ func (r *tokenResource) Create(ctx context.Context, req resource.CreateRequest,
307304 if errors .As (err , & oapiErr ) {
308305 if oapiErr .StatusCode == http .StatusNotFound {
309306 core .LogAndAddError (ctx , & resp .Diagnostics , "Error enabling model serving" ,
310- fmt .Sprintf ("Service not availiable in region %s \n %v" , region , err ),
307+ fmt .Sprintf ("Service not available in region %s \n %v" , region , err ),
311308 )
312309 return
313310 }
@@ -378,7 +375,7 @@ func (r *tokenResource) Create(ctx context.Context, req resource.CreateRequest,
378375}
379376
380377// Read refreshes the Terraform state with the latest data.
381- func (r * tokenResource ) Read (ctx context.Context , req resource.ReadRequest , resp * resource.ReadResponse ) {
378+ func (r * tokenResource ) Read (ctx context.Context , req resource.ReadRequest , resp * resource.ReadResponse ) { // nolint:gocritic // function signature required by Terraform
382379 var model Model
383380 diags := req .State .Get (ctx , & model )
384381 resp .Diagnostics .Append (diags ... )
@@ -424,7 +421,7 @@ func (r *tokenResource) Read(ctx context.Context, req resource.ReadRequest, resp
424421 }
425422
426423 // Map response body to schema
427- err = mapGetResponse (getTokenResp , & model , & model )
424+ err = mapGetResponse (getTokenResp , & model )
428425 if err != nil {
429426 core .LogAndAddError (ctx , & resp .Diagnostics , "Error reading model serving auth token" , fmt .Sprintf ("Processing API payload: %v" , err ))
430427 return
@@ -519,7 +516,9 @@ func (r *tokenResource) Update(ctx context.Context, req resource.UpdateRequest,
519516 return
520517 }
521518
522- err = mapGetResponse (waitResp , & model , & state )
519+ // Since STACKIT is not saving the content of the token. We have to use it from the state.
520+ model .Token = state .Token
521+ err = mapGetResponse (waitResp , & model )
523522 if err != nil {
524523 core .LogAndAddError (ctx , & resp .Diagnostics , "Error updating model serving auth token" , fmt .Sprintf ("Processing API payload: %v" , err ))
525524 return
@@ -620,43 +619,35 @@ func mapCreateResponse(tokenCreateResp *modelserving.CreateTokenResponse, waitRe
620619 return nil
621620}
622621
623- func mapToken (token * modelserving.Token , model , state * Model ) error {
624- if token == nil {
622+ func mapGetResponse (tokenGetResp * modelserving.GetTokenResponse , model * Model ) error {
623+ if tokenGetResp == nil {
624+ return fmt .Errorf ("response input is nil" )
625+ }
626+
627+ if tokenGetResp .Token == nil {
625628 return fmt .Errorf ("response input is nil" )
626629 }
627630 if model == nil {
628631 return fmt .Errorf ("model input is nil" )
629632 }
630- if state == nil {
631- return fmt .Errorf ("state input is nil" )
632- }
633633
634634 // theoretically, should never happen, but still catch null pointers
635635 validUntil := types .StringNull ()
636- if token .ValidUntil != nil {
637- validUntil = types .StringValue (token .ValidUntil .Format (time .RFC3339 ))
636+ if tokenGetResp . Token .ValidUntil != nil {
637+ validUntil = types .StringValue (tokenGetResp . Token .ValidUntil .Format (time .RFC3339 ))
638638 }
639639
640640 idParts := []string {model .ProjectId .ValueString (), model .Region .ValueString (), model .TokenId .ValueString ()}
641641 model .Id = types .StringValue (strings .Join (idParts , core .Separator ))
642- model .TokenId = types .StringPointerValue (token .Id )
643- model .Name = types .StringPointerValue (token .Name )
644- model .State = types .StringPointerValue (token .State )
642+ model .TokenId = types .StringPointerValue (tokenGetResp . Token .Id )
643+ model .Name = types .StringPointerValue (tokenGetResp . Token .Name )
644+ model .State = types .StringPointerValue (tokenGetResp . Token .State )
645645 model .ValidUntil = validUntil
646- model .Description = types .StringPointerValue (token .Description )
647- model .Token = state .Token
646+ model .Description = types .StringPointerValue (tokenGetResp .Token .Description )
648647
649648 return nil
650649}
651650
652- func mapGetResponse (tokenGetResp * modelserving.GetTokenResponse , model * Model , state * Model ) error {
653- if tokenGetResp == nil {
654- return fmt .Errorf ("response input is nil" )
655- }
656-
657- return mapToken (tokenGetResp .Token , model , state )
658- }
659-
660651func toCreatePayload (model * Model ) (* modelserving.CreateTokenPayload , error ) {
661652 if model == nil {
662653 return nil , fmt .Errorf ("nil model" )
0 commit comments