Skip to content

Commit

Permalink
fix: linter issues (#158)
Browse files Browse the repository at this point in the history
* fix: remove io/ioutil

* fix: lost diagnostic context

* fix: various linter errors

* fix: json manifests
  • Loading branch information
bpg authored Oct 24, 2022
1 parent 552af4d commit 0fad160
Show file tree
Hide file tree
Showing 16 changed files with 54 additions and 41 deletions.
10 changes: 3 additions & 7 deletions proxmox/common_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func (r *CustomLineBreakSeparatedList) UnmarshalJSON(b []byte) error {

// MarshalJSON converts a boolean to a JSON value.
func (r *CustomPrivileges) MarshalJSON() ([]byte, error) {
var privileges map[string]CustomBool
privileges := map[string]CustomBool{}

for _, v := range *r {
privileges[v] = true
Expand All @@ -135,10 +135,8 @@ func (r *CustomPrivileges) UnmarshalJSON(b []byte) error {
return err
}

switch privileges.(type) {
switch s := privileges.(type) {
case string:
s := privileges.(string)

if s != "" {
*r = strings.Split(s, ",")
} else {
Expand All @@ -159,9 +157,7 @@ func (r *CustomPrivileges) UnmarshalJSON(b []byte) error {

// MarshalJSON converts a boolean to a JSON value.
func (r CustomTimestamp) MarshalJSON() ([]byte, error) {
var timestamp time.Time

timestamp = time.Time(r)
timestamp := time.Time(r)
buffer := bytes.NewBufferString(strconv.FormatInt(timestamp.Unix(), 10))

return buffer.Bytes(), nil
Expand Down
3 changes: 1 addition & 2 deletions proxmox/virtual_environment_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"fmt"
"github.com/hashicorp/terraform-plugin-log/tflog"
"io"
"io/ioutil"
"net/http"
"net/url"
"strings"
Expand Down Expand Up @@ -190,7 +189,7 @@ func (c *VirtualEnvironmentClient) DoRequest(ctx context.Context, method, path s
return fErr
}
} else {
data, _ := ioutil.ReadAll(res.Body)
data, _ := io.ReadAll(res.Body)
tflog.Warn(ctx, "unhandled HTTP response body", map[string]interface{}{
"data": string(data),
})
Expand Down
2 changes: 1 addition & 1 deletion proxmox/virtual_environment_cluster_ipset.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func (c *VirtualEnvironmentClient) AddCIDRToIPSet(ctx context.Context, id string

// UpdateIPSet updates an IPSet.
func (c *VirtualEnvironmentClient) UpdateIPSet(ctx context.Context, d *VirtualEnvironmentClusterIPSetUpdateRequestBody) error {
return c.DoRequest(ctx, hmPOST, fmt.Sprint("cluster/firewall/ipset/"), d, nil)
return c.DoRequest(ctx, hmPOST, "cluster/firewall/ipset/", d, nil)
}

// DeleteIPSet delete an IPSet
Expand Down
4 changes: 2 additions & 2 deletions proxmox/virtual_environment_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func (c *VirtualEnvironmentClient) WaitForContainerState(ctx context.Context, no

time.Sleep(200 * time.Millisecond)

timeElapsed = time.Now().Sub(timeStart)
timeElapsed = time.Since(timeStart)

if ctx.Err() != nil {
return ctx.Err()
Expand Down Expand Up @@ -145,7 +145,7 @@ func (c *VirtualEnvironmentClient) WaitForContainerLock(ctx context.Context, nod

time.Sleep(200 * time.Millisecond)

timeElapsed = time.Now().Sub(timeStart)
timeElapsed = time.Since(timeStart)

if ctx.Err() != nil {
return ctx.Err()
Expand Down
2 changes: 1 addition & 1 deletion proxmox/virtual_environment_container_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ type VirtualEnvironmentContainerRebootRequestBody struct {

// VirtualEnvironmentContainerShutdownRequestBody contains the body for a container shutdown request.
type VirtualEnvironmentContainerShutdownRequestBody struct {
ForceStop *CustomBool `json:"forceStop,omitempty,int" url:"forceStop,omitempty,int"`
ForceStop *CustomBool `json:"forceStop,omitempty" url:"forceStop,omitempty,int"`
Timeout *int `json:"timeout,omitempty" url:"timeout,omitempty"`
}

Expand Down
17 changes: 13 additions & 4 deletions proxmox/virtual_environment_datastores.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"context"
"errors"
"fmt"
"github.com/hashicorp/terraform-plugin-log/tflog"
"io"
"io/ioutil"
"mime/multipart"
"net/url"
"os"
Expand Down Expand Up @@ -84,7 +84,13 @@ func (c *VirtualEnvironmentClient) UploadFileToDatastore(ctx context.Context, d
defer w.Close()
defer m.Close()

m.WriteField("content", d.ContentType)
err := m.WriteField("content", d.ContentType)
if err != nil {
tflog.Error(ctx, "failed to write 'content' field", map[string]interface{}{
"error": err,
})
return
}

part, err := m.CreateFormFile("filename", d.FileName)

Expand All @@ -101,15 +107,18 @@ func (c *VirtualEnvironmentClient) UploadFileToDatastore(ctx context.Context, d

// We need to store the multipart content in a temporary file to avoid using high amounts of memory.
// This is necessary due to Proxmox VE not supporting chunked transfers in v6.1 and earlier versions.
tempMultipartFile, err := ioutil.TempFile("", "multipart")
tempMultipartFile, err := os.CreateTemp("", "multipart")

if err != nil {
return nil, err
}

tempMultipartFileName := tempMultipartFile.Name()

io.Copy(tempMultipartFile, r)
_, err = io.Copy(tempMultipartFile, r)
if err != nil {
return nil, err
}

err = tempMultipartFile.Close()

Expand Down
2 changes: 1 addition & 1 deletion proxmox/virtual_environment_nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ func (c *VirtualEnvironmentClient) WaitForNodeTask(ctx context.Context, nodeName

time.Sleep(200 * time.Millisecond)

timeElapsed = time.Now().Sub(timeStart)
timeElapsed = time.Since(timeStart)

if ctx.Err() != nil {
return ctx.Err()
Expand Down
8 changes: 4 additions & 4 deletions proxmox/virtual_environment_vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ func (c *VirtualEnvironmentClient) WaitForNetworkInterfacesFromVMAgent(ctx conte

time.Sleep(200 * time.Millisecond)

timeElapsed = time.Now().Sub(timeStart)
timeElapsed = time.Since(timeStart)

if ctx.Err() != nil {
return nil, ctx.Err()
Expand Down Expand Up @@ -462,7 +462,7 @@ func (c *VirtualEnvironmentClient) WaitForNoNetworkInterfacesFromVMAgent(ctx con

time.Sleep(200 * time.Millisecond)

timeElapsed = time.Now().Sub(timeStart)
timeElapsed = time.Since(timeStart)

if ctx.Err() != nil {
return ctx.Err()
Expand Down Expand Up @@ -496,7 +496,7 @@ func (c *VirtualEnvironmentClient) WaitForVMConfigUnlock(ctx context.Context, no

time.Sleep(200 * time.Millisecond)

timeElapsed = time.Now().Sub(timeStart)
timeElapsed = time.Since(timeStart)

if ctx.Err() != nil {
return ctx.Err()
Expand Down Expand Up @@ -531,7 +531,7 @@ func (c *VirtualEnvironmentClient) WaitForVMState(ctx context.Context, nodeName

time.Sleep(200 * time.Millisecond)

timeElapsed = time.Now().Sub(timeStart)
timeElapsed = time.Since(timeStart)

if ctx.Err() != nil {
return ctx.Err()
Expand Down
14 changes: 7 additions & 7 deletions proxmox/virtual_environment_vm_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ type VirtualEnvironmentVMListResponseData struct {
// VirtualEnvironmentVMMoveDiskRequestBody contains the body for a VM move disk request.
type VirtualEnvironmentVMMoveDiskRequestBody struct {
BandwidthLimit *int `json:"bwlimit,omitempty" url:"bwlimit,omitempty"`
DeleteOriginalDisk *CustomBool `json:"delete,omitempty,int" url:"delete,omitempty,int"`
DeleteOriginalDisk *CustomBool `json:"delete,omitempty" url:"delete,omitempty,int"`
Digest *string `json:"digest,omitempty" url:"digest,omitempty"`
Disk string `json:"disk" url:"disk"`
TargetStorage string `json:"storage" url:"storage"`
Expand All @@ -531,14 +531,14 @@ type VirtualEnvironmentVMResizeDiskRequestBody struct {
Digest *string `json:"digest,omitempty" url:"digest,omitempty"`
Disk string `json:"disk" url:"disk"`
Size string `json:"size" url:"size"`
SkipLock *CustomBool `json:"skiplock,omitempty,int" url:"skiplock,omitempty,int"`
SkipLock *CustomBool `json:"skiplock,omitempty" url:"skiplock,omitempty,int"`
}

// VirtualEnvironmentVMShutdownRequestBody contains the body for a VM shutdown request.
type VirtualEnvironmentVMShutdownRequestBody struct {
ForceStop *CustomBool `json:"forceStop,omitempty,int" url:"forceStop,omitempty,int"`
KeepActive *CustomBool `json:"keepActive,omitempty,int" url:"keepActive,omitempty,int"`
SkipLock *CustomBool `json:"skipLock,omitempty,int" url:"skipLock,omitempty,int"`
ForceStop *CustomBool `json:"forceStop,omitempty" url:"forceStop,omitempty,int"`
KeepActive *CustomBool `json:"keepActive,omitempty" url:"keepActive,omitempty,int"`
SkipLock *CustomBool `json:"skipLock,omitempty" url:"skipLock,omitempty,int"`
Timeout *int `json:"timeout,omitempty" url:"timeout,omitempty"`
}

Expand Down Expand Up @@ -980,9 +980,9 @@ func (r CustomSpiceEnhancements) EncodeValues(key string, v *url.Values) error {

if r.FolderSharing != nil {
if *r.FolderSharing {
values = append(values, fmt.Sprintf("foldersharing=1"))
values = append(values, "foldersharing=1")
} else {
values = append(values, fmt.Sprintf("foldersharing=0"))
values = append(values, "foldersharing=0")
}
}

Expand Down
4 changes: 2 additions & 2 deletions proxmoxtf/data_source_virtual_environment_nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func dataSourceVirtualEnvironmentNodes() *schema.Resource {
Type: schema.TypeList,
Description: "The uptime in seconds for each node",
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Elem: &schema.Schema{Type: schema.TypeInt},
},
},
ReadContext: dataSourceVirtualEnvironmentNodesRead,
Expand Down Expand Up @@ -183,5 +183,5 @@ func dataSourceVirtualEnvironmentNodesRead(ctx context.Context, d *schema.Resour
err = d.Set(mkDataSourceVirtualEnvironmentNodesUptime, uptime)
diags = append(diags, diag.FromErr(err)...)

return nil
return diags
}
2 changes: 2 additions & 0 deletions proxmoxtf/resource_virtual_environment_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -859,6 +859,7 @@ func resourceVirtualEnvironmentContainerCreateClone(ctx context.Context, d *sche

template := proxmox.CustomBool(d.Get(mkResourceVirtualEnvironmentContainerTemplate).(bool))

//nolint:gosimple
if template != dvResourceVirtualEnvironmentContainerTemplate {
updateBody.Template = &template
}
Expand Down Expand Up @@ -1642,6 +1643,7 @@ func resourceVirtualEnvironmentContainerRead(ctx context.Context, d *schema.Reso

currentTemplate := d.Get(mkResourceVirtualEnvironmentContainerTemplate).(bool)

//nolint:gosimple
if len(clone) == 0 || currentTemplate != dvResourceVirtualEnvironmentContainerTemplate {
if containerConfig.Template != nil {
err = d.Set(mkResourceVirtualEnvironmentContainerTemplate, bool(*containerConfig.Template))
Expand Down
5 changes: 2 additions & 3 deletions proxmoxtf/resource_virtual_environment_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
Expand Down Expand Up @@ -261,7 +260,7 @@ func resourceVirtualEnvironmentFileCreate(ctx context.Context, d *schema.Resourc
}
}(res.Body)

tempDownloadedFile, err := ioutil.TempFile("", "download")
tempDownloadedFile, err := os.CreateTemp("", "download")
if err != nil {
return diag.FromErr(err)
}
Expand Down Expand Up @@ -330,7 +329,7 @@ func resourceVirtualEnvironmentFileCreate(ctx context.Context, d *schema.Resourc
}
}

tempRawFile, err := ioutil.TempFile("", "raw")
tempRawFile, err := os.CreateTemp("", "raw")
if err != nil {
return diag.FromErr(err)
}
Expand Down
2 changes: 1 addition & 1 deletion proxmoxtf/resource_virtual_environment_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func resourceVirtualEnvironmentPoolRead(ctx context.Context, d *schema.ResourceD
err = d.Set(mkResourceVirtualEnvironmentPoolMembers, members)
diags = append(diags, diag.FromErr(err)...)

return diag.FromErr(err)
return diags
}

func resourceVirtualEnvironmentPoolUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
Expand Down
2 changes: 1 addition & 1 deletion proxmoxtf/resource_virtual_environment_time.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func resourceVirtualEnvironmentTimeRead(ctx context.Context, d *schema.ResourceD
err = d.Set(mkDataSourceVirtualEnvironmentTimeUTCTime, time.Time(nodeTime.UTCTime).Format(time.RFC3339))
diags = append(diags, diag.FromErr(err)...)

return nil
return diags
}

func resourceVirtualEnvironmentTimeUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
Expand Down
10 changes: 8 additions & 2 deletions proxmoxtf/resource_virtual_environment_vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -1194,6 +1194,7 @@ func resourceVirtualEnvironmentVMCreateClone(ctx context.Context, d *schema.Reso

var del []string

//nolint:gosimple
if acpi != dvResourceVirtualEnvironmentVMACPI {
updateBody.ACPI = &acpi
}
Expand Down Expand Up @@ -1378,10 +1379,12 @@ func resourceVirtualEnvironmentVMCreateClone(ctx context.Context, d *schema.Reso

updateBody.StartOnBoot = &onBoot

//nolint:gosimple
if tabletDevice != dvResourceVirtualEnvironmentVMTabletDevice {
updateBody.TabletDeviceEnabled = &tabletDevice
}

//nolint:gosimple
if template != dvResourceVirtualEnvironmentVMTemplate {
updateBody.Template = &template
}
Expand Down Expand Up @@ -3157,6 +3160,7 @@ func resourceVirtualEnvironmentVMReadPrimitiveValues(d *schema.ResourceData, vmC
clone := d.Get(mkResourceVirtualEnvironmentVMClone).([]interface{})
currentACPI := d.Get(mkResourceVirtualEnvironmentVMACPI).(bool)

//nolint:gosimple
if len(clone) == 0 || currentACPI != dvResourceVirtualEnvironmentVMACPI {
if vmConfig.ACPI != nil {
err = d.Set(mkResourceVirtualEnvironmentVMACPI, bool(*vmConfig.ACPI))
Expand Down Expand Up @@ -3215,13 +3219,14 @@ func resourceVirtualEnvironmentVMReadPrimitiveValues(d *schema.ResourceData, vmC
diags = append(diags, diag.FromErr(err)...)
}

if d.Get(mkResourceVirtualEnvironmentVMTemplate).(bool) != true {
if !d.Get(mkResourceVirtualEnvironmentVMTemplate).(bool) {
err = d.Set(mkResourceVirtualEnvironmentVMStarted, vmStatus.Status == "running")
diags = append(diags, diag.FromErr(err)...)
}

currentTabletDevice := d.Get(mkResourceVirtualEnvironmentVMTabletDevice).(bool)

//nolint:gosimple
if len(clone) == 0 || currentTabletDevice != dvResourceVirtualEnvironmentVMTabletDevice {
if vmConfig.TabletDeviceEnabled != nil {
err = d.Set(mkResourceVirtualEnvironmentVMTabletDevice, bool(*vmConfig.TabletDeviceEnabled))
Expand All @@ -3234,6 +3239,7 @@ func resourceVirtualEnvironmentVMReadPrimitiveValues(d *schema.ResourceData, vmC

currentTemplate := d.Get(mkResourceVirtualEnvironmentVMTemplate).(bool)

//nolint:gosimple
if len(clone) == 0 || currentTemplate != dvResourceVirtualEnvironmentVMTemplate {
if vmConfig.Template != nil {
err = d.Set(mkResourceVirtualEnvironmentVMTemplate, bool(*vmConfig.Template))
Expand Down Expand Up @@ -3383,7 +3389,7 @@ func resourceVirtualEnvironmentVMUpdate(ctx context.Context, d *schema.ResourceD
cdromEnabled := cdromBlock[mkResourceVirtualEnvironmentVMCDROMEnabled].(bool)
cdromFileID := cdromBlock[mkResourceVirtualEnvironmentVMCDROMFileID].(string)

if cdromEnabled == false && cdromFileID == "" {
if !cdromEnabled && cdromFileID == "" {
del = append(del, "ide3")
}

Expand Down
8 changes: 5 additions & 3 deletions proxmoxtf/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func getContentTypeValidator() schema.SchemaValidateDiagFunc {
}, false))
}

//nolint:unused
func getCPUFlagsValidator() schema.SchemaValidateDiagFunc {
return validation.ToDiagFunc(func(i interface{}, k string) (ws []string, es []error) {
list, ok := i.([]interface{})
Expand Down Expand Up @@ -332,6 +333,7 @@ func getVGATypeValidator() schema.SchemaValidateDiagFunc {
}, false))
}

//nolint:unused
func getVLANIDsValidator() schema.SchemaValidateDiagFunc {
return validation.ToDiagFunc(func(i interface{}, k string) (ws []string, es []error) {
min := 1
Expand Down Expand Up @@ -507,7 +509,7 @@ func testComputedAttributes(t *testing.T, s *schema.Resource, keys []string) {
t.Fatalf("Error in Schema: Missing definition for \"%s\"", v)
}

if s.Schema[v].Computed != true {
if !s.Schema[v].Computed {
t.Fatalf("Error in Schema: Attribute \"%s\" is not computed", v)
}
}
Expand All @@ -531,7 +533,7 @@ func testOptionalArguments(t *testing.T, s *schema.Resource, keys []string) {
t.Fatalf("Error in Schema: Missing definition for \"%s\"", v)
}

if s.Schema[v].Optional != true {
if !s.Schema[v].Optional {
t.Fatalf("Error in Schema: Argument \"%s\" is not optional", v)
}
}
Expand All @@ -543,7 +545,7 @@ func testRequiredArguments(t *testing.T, s *schema.Resource, keys []string) {
t.Fatalf("Error in Schema: Missing definition for \"%s\"", v)
}

if s.Schema[v].Required != true {
if !s.Schema[v].Required {
t.Fatalf("Error in Schema: Argument \"%s\" is not required", v)
}
}
Expand Down

0 comments on commit 0fad160

Please sign in to comment.