@@ -36,6 +36,7 @@ type scriptModel struct {
3636 Platform types.String `tfsdk:"platform"`
3737 ScriptType types.String `tfsdk:"script_type"`
3838 ScriptTimeout types.Int64 `tfsdk:"script_timeout"`
39+ EncodingType types.String `tfsdk:"encoding_type"` // Optional, default is empty
3940}
4041
4142func ScriptResource () resource.Resource {
@@ -78,6 +79,13 @@ func (r *scriptResource) Schema(_ context.Context, request resource.SchemaReques
7879 Optional : true ,
7980 Description : "The description of the script." ,
8081 },
82+ "encoding_type" : schema.StringAttribute {
83+ Required : true ,
84+ Description : "The encoding type of the script content. Supports: Base64, PlainText." ,
85+ Validators : []validator.String {
86+ stringvalidator .OneOf ("Base64" , "PlainText" ),
87+ },
88+ },
8189 "script_content" : schema.StringAttribute {
8290 Required : true ,
8391 Description : "The content of the script." ,
@@ -151,6 +159,8 @@ func (r *scriptResource) Create(ctx context.Context, request resource.CreateRequ
151159 description = plan .Description .ValueString ()
152160 }
153161
162+ //encodingType := plan.EncodingType.ValueString()
163+
154164 ok , pattern := isScriptContentSafe (plan .ScriptContent .ValueString ())
155165 if ! ok {
156166 response .Diagnostics .AddError (
@@ -160,6 +170,11 @@ func (r *scriptResource) Create(ctx context.Context, request resource.CreateRequ
160170 return
161171 }
162172
173+ encodingType := "Base64" // Default encoding type
174+ if ! plan .EncodingType .IsNull () && ! plan .EncodingType .IsUnknown () && plan .EncodingType .ValueString () != "" {
175+ encodingType = plan .EncodingType .ValueString ()
176+ }
177+
163178 var systemTags []string
164179
165180 Param := param.CreateVmInstanceScriptParam {
@@ -170,6 +185,7 @@ func (r *scriptResource) Create(ctx context.Context, request resource.CreateRequ
170185 Name : plan .Name .ValueString (),
171186 Description : description ,
172187 ScriptContent : plan .ScriptContent .ValueString (),
188+ EncodingType : encodingType ,
173189 Platform : plan .Platform .ValueString (),
174190 ScriptType : plan .ScriptType .ValueString (),
175191 ScriptTimeout : int (scriptTimeout ),
@@ -194,6 +210,7 @@ func (r *scriptResource) Create(ctx context.Context, request resource.CreateRequ
194210 plan .Platform = types .StringValue (script .Platform )
195211 plan .ScriptType = types .StringValue (script .ScriptType )
196212 plan .ScriptTimeout = types .Int64Value (int64 (script .ScriptTimeout ))
213+ plan .EncodingType = types .StringValue (script .EncodingType )
197214
198215 diags = response .State .Set (ctx , plan )
199216 response .Diagnostics .Append (diags ... )
@@ -280,29 +297,48 @@ func (r *scriptResource) Update(ctx context.Context, request resource.UpdateRequ
280297 description = plan .Description .ValueString ()
281298 }
282299
283- ok , pattern := isScriptContentSafe (plan .ScriptContent .ValueString ())
284- if ! ok {
285- response .Diagnostics .AddError (
286- "Dangerous script content detected" ,
287- fmt .Sprintf ("The script content contains dangerous commands and is not allowed. %s" , pattern ),
288- )
289- return
300+ if ! plan .ScriptContent .IsNull () && ! plan .ScriptContent .IsUnknown () {
301+ ok , pattern := isScriptContentSafe (plan .ScriptContent .ValueString ())
302+ if ! ok {
303+ response .Diagnostics .AddError (
304+ "Dangerous script content detected" ,
305+ fmt .Sprintf ("The script content contains dangerous commands and is not allowed. %s" , pattern ),
306+ )
307+ return
308+ }
309+
290310 }
291311
292312 var systemTags []string
313+ detailParam := param.UpdateVmInstanceScriptDetailParam {
314+ Name : plan .Name .ValueString (),
315+ Description : description ,
316+ Platform : plan .Platform .ValueString (),
317+ ScriptType : plan .ScriptType .ValueString (),
318+ ScriptTimeout : int (scriptTimeout ),
319+ RenderParams : renderParams ,
320+ }
321+
322+ scriptContent := plan .ScriptContent .ValueString ()
323+ encodingType := plan .EncodingType .ValueString ()
324+
325+ if scriptContent != "" || encodingType != "" {
326+ if scriptContent == "" || encodingType == "" {
327+ response .Diagnostics .AddError (
328+ "Update Script Error" ,
329+ "Both scriptContent and encodingType must be provided together during update, or both must be omitted." ,
330+ )
331+ return
332+ }
333+ detailParam .ScriptContent = scriptContent
334+ detailParam .EncodingType = encodingType
335+ }
336+
293337 Param := param.UpdateVmInstanceScriptParam {
294338 BaseParam : param.BaseParam {
295339 SystemTags : systemTags ,
296340 },
297- Params : param.UpdateVmInstanceScriptDetailParam {
298- Name : plan .Name .ValueString (),
299- Description : description ,
300- ScriptContent : plan .ScriptContent .ValueString (),
301- Platform : plan .Platform .ValueString (),
302- ScriptType : plan .ScriptType .ValueString (),
303- ScriptTimeout : int (scriptTimeout ),
304- RenderParams : renderParams ,
305- },
341+ Params : detailParam ,
306342 }
307343
308344 tflog .Debug (ctx , "Updating VM instance script" , map [string ]interface {}{
0 commit comments