Skip to content

Commit

Permalink
Fix nil pointer dereference accessing CPUS String() (#11)
Browse files Browse the repository at this point in the history
* fixes #10

* Also fixes changes when an optional attribute is omitted
  • Loading branch information
larstobi authored Aug 2, 2022
1 parent 7cd6157 commit 1598bb2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 20 deletions.
9 changes: 8 additions & 1 deletion internal/provider/resource_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
45 changes: 26 additions & 19 deletions internal/provider/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,40 +18,47 @@ 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)}
}
}
}

// Generate resource state struct
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,
Expand Down

0 comments on commit 1598bb2

Please sign in to comment.