From 1598bb20431e5b96e43880c65b2212782a2b3537 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20Tobias=20Skjong-B=C3=B8rsting?= Date: Tue, 2 Aug 2022 19:52:48 +0200 Subject: [PATCH] Fix nil pointer dereference accessing CPUS String() (#11) * fixes #10 * Also fixes changes when an optional attribute is omitted --- internal/provider/resource_instance.go | 9 +++++- internal/provider/utils.go | 45 +++++++++++++++----------- 2 files changed, 34 insertions(+), 20 deletions(-) diff --git a/internal/provider/resource_instance.go b/internal/provider/resource_instance.go index 808970b..6b48c7f 100644 --- a/internal/provider/resource_instance.go +++ b/internal/provider/resource_instance.go @@ -119,10 +119,17 @@ func (r instanceResource) Create(ctx context.Context, req tfsdk.CreateResourceRe "name": plan.Name.String(), }) + var cpus string + if plan.CPUS.Null { + cpus = "" + } else { + cpus = plan.CPUS.Value.String() + } + _, err := multipass.LaunchV2(&multipass.LaunchReqV2{ Name: plan.Name.Value, Image: plan.Image.Value, - CPUS: plan.CPUS.Value.String(), + CPUS: cpus, Memory: plan.Memory.Value, Disk: plan.Disk.Value, CloudInitFile: plan.CloudInitFile.Value, diff --git a/internal/provider/utils.go b/internal/provider/utils.go index e5a790f..64fff20 100644 --- a/internal/provider/utils.go +++ b/internal/provider/utils.go @@ -18,32 +18,39 @@ func QueryInstance(state Instance) (*Instance, error) { } // Check CPUS - cpus := new(big.Float) - cpus.SetString(out.CPUS) - - // Check Memory - if state.Memory.Null { - state.Memory = types.String{Value: "0MiB"} + current_cpus := state.CPUS + // If CPUS is not specified, then ignore changes + if !state.CPUS.Null { + cpus := new(big.Float) + cpus.SetString(out.CPUS) + if cpus != state.CPUS.Value { + current_cpus = types.Number{Value: cpus} + } } + // Check Memory current_memory := state.Memory - if equal, err := CompareDataSizes(out.Memory, state.Memory.Value); err != nil { - return nil, errors.New("Error comparing memory size: " + err.Error()) - } else { - if !*equal { - current_memory = types.String{Value: RemoveZeroDecimalAndSpaces(out.Memory)} + // If Memory is not specified, then ignore changes + if !state.Memory.Null { + if equal, err := CompareDataSizes(out.Memory, state.Memory.Value); err != nil { + return nil, errors.New("Error comparing memory size: " + err.Error()) + } else { + if !*equal { + current_memory = types.String{Value: RemoveZeroDecimalAndSpaces(out.Memory)} + } } } // Check Disk - var current_disk types.String - if equal, err := CompareDataSizes(out.Disk, state.Disk.Value); err != nil { - return nil, errors.New("Error comparing disk size: " + err.Error()) - } else { - if *equal { - current_disk = state.Disk + current_disk := state.Disk + // If Disk is not specified, then ignore changes + if !state.Disk.Null { + if equal, err := CompareDataSizes(out.Disk, state.Disk.Value); err != nil { + return nil, errors.New("Error comparing disk size: " + err.Error()) } else { - current_disk = types.String{Value: RemoveZeroDecimalAndSpaces(out.Disk)} + if !*equal { + current_disk = types.String{Value: RemoveZeroDecimalAndSpaces(out.Disk)} + } } } @@ -51,7 +58,7 @@ func QueryInstance(state Instance) (*Instance, error) { var result = Instance{ Name: state.Name, Image: state.Image, - CPUS: types.Number{Value: cpus}, + CPUS: current_cpus, Memory: current_memory, Disk: current_disk, CloudInitFile: state.CloudInitFile,