Skip to content

Commit

Permalink
Fix linting errors
Browse files Browse the repository at this point in the history
  • Loading branch information
taliesins committed Dec 18, 2023
1 parent 7ab0e69 commit 5d79058
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 53 deletions.
14 changes: 8 additions & 6 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ linters:
#- funlen
#- gochecknoinits #needed for document generation
- goconst
#- gocritic
- gocritic
#- gocyclo
- gofmt
- goimports
Expand Down Expand Up @@ -56,11 +56,13 @@ linters-settings:
ignore: github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema:ForceNew|Set,fmt:.*,io:Close
depguard:
rules:
main:
files:
- $all
allow: []
deny: []
logger:
deny:
# logging is allowed only by logutils.Log,
# logrus is allowed to use only in logutils package.
- pkg: "github.com/sirupsen/logrus"
desc: logging is allowed only by logutils.Log

run:
modules-download-mode: mod
timeout: 10m
Expand Down
2 changes: 1 addition & 1 deletion api/hyperv-winrm/vm_firmware.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func (c *ClientConfig) CreateOrUpdateVmFirmwares(ctx context.Context, vmName str
return nil
}
if len(vmFirmwares) > 1 {
return fmt.Errorf("Only 1 vm firmware setting allowed per a vm")
return fmt.Errorf("only 1 vm firmware setting allowed per a vm")
}

vmFirmware := vmFirmwares[0]
Expand Down
2 changes: 1 addition & 1 deletion api/hyperv-winrm/vm_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func (c *ClientConfig) CreateOrUpdateVmProcessors(ctx context.Context, vmName st
return nil
}
if len(vmProcessors) > 1 {
return fmt.Errorf("Only 1 vm processor setting allowed per a vm")
return fmt.Errorf("only 1 vm processor setting allowed per a vm")
}

vmProcessor := vmProcessors[0]
Expand Down
10 changes: 2 additions & 8 deletions internal/provider/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,15 @@ func (c *Config) Client() (comm api.Client, err error) {
" Password: %t\n"+
" HTTPS: %t\n"+
" Insecure: %t\n"+

" NTLM: %t\n"+

" KrbRealm: %s\n"+
" KrbSpn: %s\n"+
" KrbConfig: %s\n"+
" KrbCCache: %s\n"+

" TLSServerName: %s\n"+
" CACert: %t\n"+
" Cert: %t\n"+
" Key: %t\n"+

" ScriptPath: %s\n"+
" Timeout: %s",
c.Host,
Expand Down Expand Up @@ -126,10 +122,8 @@ func GetWinrmClient(config *Config) (winrmClient *winrm.Client, err error) {
KrbCCache: config.KrbCCache,
}
}
} else {
if config.NTLM {
params.TransportDecorator = func() winrm.Transporter { return &winrm.ClientNTLM{} }
}
} else if config.NTLM {
params.TransportDecorator = func() winrm.Transporter { return &winrm.ClientNTLM{} }
}

if endpoint.Timeout.Seconds() > 0 {
Expand Down
4 changes: 2 additions & 2 deletions internal/provider/data_source_hyperv_machine_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func dataSourceHyperVMachineInstance() *schema.Resource {
return true
}

//When specifying path on new-vm it will auto append machine name on the end
// When specifying path on new-vm it will auto append machine name on the end
name := d.Get("name").(string)
computedPath := newValue
if !strings.HasSuffix(computedPath, "\\") {
Expand Down Expand Up @@ -793,7 +793,7 @@ func dataSourceHyperVMachineInstance() *schema.Resource {
return true
}

//When specifying path on new-vm it will auto append machine name on the end
// When specifying path on new-vm it will auto append machine name on the end
name := d.Get("name").(string)
computedPath := newValue
if !strings.HasSuffix(computedPath, "\\") {
Expand Down
14 changes: 8 additions & 6 deletions internal/provider/data_source_hyperv_network_switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,43 +149,45 @@ func datasourceHyperVNetworkSwitchRead(ctx context.Context, d *schema.ResourceDa
return nil
}

if s.SwitchType == api.VMSwitchType_Private {
switch s.SwitchType {
case api.VMSwitchType_Private:
if s.AllowManagementOS {
return diag.Errorf("[ERROR][hyperv][read] Unable to set AllowManagementOS to true if switch type is private")
}

if len(s.NetAdapterNames) > 0 {
return diag.Errorf("[ERROR][hyperv][read] Unable to set NetAdapterNames when switch type is private")
}
} else if s.SwitchType == api.VMSwitchType_Internal {
case api.VMSwitchType_Internal:
if !s.AllowManagementOS {
return diag.Errorf("[ERROR][hyperv][read] Unable to set AllowManagementOS to false if switch type is internal")
}

if len(s.NetAdapterNames) > 0 {
return diag.Errorf("[ERROR][hyperv][read] Unable to set NetAdapterNames when switch type is internal")
}
} else if s.SwitchType == api.VMSwitchType_External {
case api.VMSwitchType_External:
if len(s.NetAdapterNames) < 1 {
return diag.Errorf("[ERROR][hyperv][read] Must specify NetAdapterNames if switch type is external")
}
}

if s.BandwidthReservationMode == api.VMSwitchBandwidthMode_Absolute {
switch {
case s.BandwidthReservationMode == api.VMSwitchBandwidthMode_Absolute:
if s.DefaultFlowMinimumBandwidthWeight != 0 {
return diag.Errorf("[ERROR][hyperv][read] DefaultFlowMinimumBandwidthWeight should be 0 if bandwidth reservation mode is absolute")
}
if s.DefaultFlowMinimumBandwidthAbsolute < 0 {
return diag.Errorf("[ERROR][hyperv][read] Bandwidth absolute must be 0 or greater")
}
} else if s.BandwidthReservationMode == api.VMSwitchBandwidthMode_Weight || (s.BandwidthReservationMode == api.VMSwitchBandwidthMode_Default && (!s.IovEnabled)) {
case s.BandwidthReservationMode == api.VMSwitchBandwidthMode_Weight || (s.BandwidthReservationMode == api.VMSwitchBandwidthMode_Default && (!s.IovEnabled)):
if s.DefaultFlowMinimumBandwidthAbsolute != 0 {
return diag.Errorf("[ERROR][hyperv][read] DefaultFlowMinimumBandwidthAbsolute should be 0 if bandwidth reservation mode is weight")
}
if s.DefaultFlowMinimumBandwidthWeight < 1 || s.DefaultFlowMinimumBandwidthWeight > 100 {
return diag.Errorf("[ERROR][hyperv][read] Bandwidth weight must be between 1 and 100")
}
} else {
default:
if s.DefaultFlowMinimumBandwidthWeight != 0 {
return diag.Errorf("[ERROR][hyperv][read] DefaultFlowMinimumBandwidthWeight should be 0 if bandwidth reservation mode is none")
}
Expand Down
4 changes: 2 additions & 2 deletions internal/provider/resource_hyperv_machine_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -828,7 +828,7 @@ func resourceHyperVMachineInstance() *schema.Resource {
return true
}

//When specifying path on new-vm it will auto append machine name on the end
// When specifying path on new-vm it will auto append machine name on the end
name := d.Get("name").(string)
computedPath := newValue
if !strings.HasSuffix(computedPath, "\\") {
Expand Down Expand Up @@ -948,7 +948,7 @@ func resourceHyperVMachineInstanceCreate(ctx context.Context, d *schema.Resource
}

if existing.Exists {
return diag.FromErr(fmt.Errorf("A resource with the ID %q already exists - to be managed via Terraform this resource needs to be imported into the State. Please see the resource documentation for %q for more information.\n terraform import %s.<resource name> %s", name, "hyperv_machine_instance", "hyperv_machine_instance", name))
return diag.FromErr(fmt.Errorf("a resource with the ID %q already exists - to be managed via Terraform this resource needs to be imported into the State. Please see the resource documentation for %q for more information.\n terraform import %s.<resource name> %s", name, "hyperv_machine_instance", "hyperv_machine_instance", name))
}
}

Expand Down
44 changes: 25 additions & 19 deletions internal/provider/resource_hyperv_network_switch.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func resourceHyperVNetworkSwitchCreate(ctx context.Context, d *schema.ResourceDa
}

if existing.Exists {
return diag.FromErr(fmt.Errorf("A resource with the ID %q already exists - to be managed via Terraform this resource needs to be imported into the State. Please see the resource documentation for %q for more information.\n terraform import %s.<resource name> %s", switchName, "hyperv_network_switch", "hyperv_network_switch", switchName))
return diag.FromErr(fmt.Errorf("a resource with the ID %q already exists - to be managed via Terraform this resource needs to be imported into the State. Please see the resource documentation for %q for more information.\n terraform import %s.<resource name> %s", switchName, "hyperv_network_switch", "hyperv_network_switch", switchName))
}
}

Expand All @@ -185,43 +185,45 @@ func resourceHyperVNetworkSwitchCreate(ctx context.Context, d *schema.ResourceDa
defaultQueueVmmqQueuePairs := int32((d.Get("default_queue_vmmq_queue_pairs")).(int))
defaultQueueVrssEnabled := (d.Get("default_queue_vrss_enabled")).(bool)

if switchType == api.VMSwitchType_Private {
switch switchType {
case api.VMSwitchType_Private:
if allowManagementOS {
return diag.Errorf("[ERROR][hyperv][create] Unable to set AllowManagementOS to true if switch type is private")
}

if len(netAdapterNames) > 0 {
return diag.Errorf("[ERROR][hyperv][create] Unable to set NetAdapterNames when switch type is private")
}
} else if switchType == api.VMSwitchType_Internal {
case api.VMSwitchType_Internal:
if !allowManagementOS {
return diag.Errorf("[ERROR][hyperv][create] Unable to set AllowManagementOS to false if switch type is internal")
}

if len(netAdapterNames) > 0 {
return diag.Errorf("[ERROR][hyperv][create] Unable to set NetAdapterNames when switch type is internal")
}
} else if switchType == api.VMSwitchType_External {
case api.VMSwitchType_External:
if len(netAdapterNames) < 1 {
return diag.Errorf("[ERROR][hyperv][create] Must specify NetAdapterNames if switch type is external")
}
}

if bandwidthReservationMode == api.VMSwitchBandwidthMode_Absolute {
switch {
case bandwidthReservationMode == api.VMSwitchBandwidthMode_Absolute:
if defaultFlowMinimumBandwidthWeight != 0 {
return diag.Errorf("[ERROR][hyperv][create] Unable to set DefaultFlowMinimumBandwidthWeight if bandwidth reservation mode is absolute")
}
if defaultFlowMinimumBandwidthAbsolute < 0 {
return diag.Errorf("[ERROR][hyperv][create] Bandwidth absolute must be 0 or greater")
}
} else if bandwidthReservationMode == api.VMSwitchBandwidthMode_Weight || (bandwidthReservationMode == api.VMSwitchBandwidthMode_Default && (!iovEnabled)) {
case bandwidthReservationMode == api.VMSwitchBandwidthMode_Weight || (bandwidthReservationMode == api.VMSwitchBandwidthMode_Default && (!iovEnabled)):
if defaultFlowMinimumBandwidthAbsolute != 0 {
return diag.Errorf("[ERROR][hyperv][create] Unable to set DefaultFlowMinimumBandwidthAbsolute if bandwidth reservation mode is weight")
}
if defaultFlowMinimumBandwidthWeight < 1 || defaultFlowMinimumBandwidthWeight > 100 {
return diag.Errorf("[ERROR][hyperv][create] Bandwidth weight must be between 1 and 100")
}
} else {
default:
if defaultFlowMinimumBandwidthWeight != 0 {
return diag.Errorf("[ERROR][hyperv][create] Unable to set DefaultFlowMinimumBandwidthWeight if bandwidth reservation mode is none")
}
Expand Down Expand Up @@ -268,43 +270,45 @@ func resourceHyperVNetworkSwitchRead(ctx context.Context, d *schema.ResourceData
return diag.FromErr(err)
}

if s.SwitchType == api.VMSwitchType_Private {
switch s.SwitchType {
case api.VMSwitchType_Private:
if s.AllowManagementOS {
return diag.Errorf("[ERROR][hyperv][read] Unable to set AllowManagementOS to true if switch type is private")
}

if len(s.NetAdapterNames) > 0 {
return diag.Errorf("[ERROR][hyperv][read] Unable to set NetAdapterNames when switch type is private")
}
} else if s.SwitchType == api.VMSwitchType_Internal {
case api.VMSwitchType_Internal:
if !s.AllowManagementOS {
return diag.Errorf("[ERROR][hyperv][read] Unable to set AllowManagementOS to false if switch type is internal")
}

if len(s.NetAdapterNames) > 0 {
return diag.Errorf("[ERROR][hyperv][read] Unable to set NetAdapterNames when switch type is internal")
}
} else if s.SwitchType == api.VMSwitchType_External {
case api.VMSwitchType_External:
if len(s.NetAdapterNames) < 1 {
return diag.Errorf("[ERROR][hyperv][read] Must specify NetAdapterNames if switch type is external")
}
}

if s.BandwidthReservationMode == api.VMSwitchBandwidthMode_Absolute {
switch {
case s.BandwidthReservationMode == api.VMSwitchBandwidthMode_Absolute:
if s.DefaultFlowMinimumBandwidthWeight != 0 {
return diag.Errorf("[ERROR][hyperv][read] DefaultFlowMinimumBandwidthWeight should be 0 if bandwidth reservation mode is absolute")
}
if s.DefaultFlowMinimumBandwidthAbsolute < 0 {
return diag.Errorf("[ERROR][hyperv][read] Bandwidth absolute must be 0 or greater")
}
} else if s.BandwidthReservationMode == api.VMSwitchBandwidthMode_Weight || (s.BandwidthReservationMode == api.VMSwitchBandwidthMode_Default && (!s.IovEnabled)) {
case s.BandwidthReservationMode == api.VMSwitchBandwidthMode_Weight || (s.BandwidthReservationMode == api.VMSwitchBandwidthMode_Default && (!s.IovEnabled)):
if s.DefaultFlowMinimumBandwidthAbsolute != 0 {
return diag.Errorf("[ERROR][hyperv][read] DefaultFlowMinimumBandwidthAbsolute should be 0 if bandwidth reservation mode is weight")
}
if s.DefaultFlowMinimumBandwidthWeight < 1 || s.DefaultFlowMinimumBandwidthWeight > 100 {
return diag.Errorf("[ERROR][hyperv][read] Bandwidth weight must be between 1 and 100")
}
} else {
default:
if s.DefaultFlowMinimumBandwidthWeight != 0 {
return diag.Errorf("[ERROR][hyperv][read] DefaultFlowMinimumBandwidthWeight should be 0 if bandwidth reservation mode is none")
}
Expand Down Expand Up @@ -389,43 +393,45 @@ func resourceHyperVNetworkSwitchUpdate(ctx context.Context, d *schema.ResourceDa
defaultQueueVmmqQueuePairs := int32((d.Get("default_queue_vmmq_queue_pairs")).(int))
defaultQueueVrssEnabled := (d.Get("default_queue_vrss_enabled")).(bool)

if switchType == api.VMSwitchType_Private {
switch switchType {
case api.VMSwitchType_Private:
if allowManagementOS {
return diag.Errorf("[ERROR][hyperv][update] Unable to set AllowManagementOS to true if switch type is private")
}

if len(netAdapterNames) > 0 {
return diag.Errorf("[ERROR][hyperv][update] Unable to set NetAdapterNames when switch type is private")
}
} else if switchType == api.VMSwitchType_Internal {
case api.VMSwitchType_Internal:
if !allowManagementOS {
return diag.Errorf("[ERROR][hyperv][update] Unable to set AllowManagementOS to false if switch type is internal")
}

if len(netAdapterNames) > 0 {
return diag.Errorf("[ERROR][hyperv][update] Unable to set NetAdapterNames when switch type is internal")
}
} else if switchType == api.VMSwitchType_External {
case api.VMSwitchType_External:
if len(netAdapterNames) < 1 {
return diag.Errorf("[ERROR][hyperv][update] Must specify NetAdapterNames if switch type is external")
}
}

if bandwidthReservationMode == api.VMSwitchBandwidthMode_Absolute {
switch {
case bandwidthReservationMode == api.VMSwitchBandwidthMode_Absolute:
if defaultFlowMinimumBandwidthWeight != 0 {
return diag.Errorf("[ERROR][hyperv][update] Unable to set DefaultFlowMinimumBandwidthWeight if bandwidth reservation mode is absolute")
}
if defaultFlowMinimumBandwidthAbsolute < 0 {
return diag.Errorf("[ERROR][hyperv][update] Bandwidth absolute must be 0 or greater")
}
} else if bandwidthReservationMode == api.VMSwitchBandwidthMode_Weight || (bandwidthReservationMode == api.VMSwitchBandwidthMode_Default && (!iovEnabled)) {
case bandwidthReservationMode == api.VMSwitchBandwidthMode_Weight || (bandwidthReservationMode == api.VMSwitchBandwidthMode_Default && (!iovEnabled)):
if defaultFlowMinimumBandwidthAbsolute != 0 {
return diag.Errorf("[ERROR][hyperv][update] Unable to set DefaultFlowMinimumBandwidthAbsolute if bandwidth reservation mode is weight")
}
if defaultFlowMinimumBandwidthWeight < 1 || defaultFlowMinimumBandwidthWeight > 100 {
return diag.Errorf("[ERROR][hyperv][update] Bandwidth weight must be between 1 and 100")
}
} else {
default:
if defaultFlowMinimumBandwidthWeight != 0 {
return diag.Errorf("[ERROR][hyperv][update] Unable to set DefaultFlowMinimumBandwidthWeight if bandwidth reservation mode is none")
}
Expand Down
4 changes: 2 additions & 2 deletions internal/provider/resource_hyperv_vhd.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func resourceHyperVVhd() *schema.Resource {
ConflictsWith: []string{
"parent_path",
},
ValidateDiagFunc: IsDivisibleBy(4096), //Technical it could also be 512
ValidateDiagFunc: IsDivisibleBy(4096), // Technical it could also be 512
Description: "This field is mutually exclusive with the field `parent_path`. The maximum size, in bytes, of the virtual hard disk to be created. This size must be divisible by 4096 so that it fits into logical blocks.",
},
"block_size": {
Expand Down Expand Up @@ -232,7 +232,7 @@ func resourceHyperVVhdCreate(ctx context.Context, d *schema.ResourceData, meta i
}

if existing.Exists {
return diag.FromErr(fmt.Errorf("A resource with the ID %q already exists - to be managed via Terraform this resource needs to be imported into the State. Please see the resource documentation for %q for more information.\n terraform import %s.<resource name> %s", path, "hyperv_vhd", "hyperv_vhd", path))
return diag.FromErr(fmt.Errorf("a resource with the ID %q already exists - to be managed via Terraform this resource needs to be imported into the State. Please see the resource documentation for %q for more information.\n terraform import %s.<resource name> %s", path, "hyperv_vhd", "hyperv_vhd", path))
}
}

Expand Down
5 changes: 4 additions & 1 deletion powershell/provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func winPath(path string) string {
path = fmt.Sprintf("'%s'", strings.Trim(path, "'\""))
}

return strings.Replace(path, "/", "\\", -1)
return strings.ReplaceAll(path, "/", "\\")
}

func doCopy(client *winrm.Client, maxChunks int, in io.Reader, toPath string) (remoteAbsolutePath string, err error) {
Expand Down Expand Up @@ -375,6 +375,9 @@ func shellExecute(shell *winrm.Shell, command string, arguments ...string) (int,

func uploadScript(client *winrm.Client, fileName string, command string) (remoteAbsolutePath string, err error) {
tmpFile, err := ioutil.TempFile(os.TempDir(), fileName)
if err != nil {
return "", fmt.Errorf("error creating temp file: %s", err)
}
writer := bufio.NewWriter(tmpFile)
if _, err := writer.WriteString(command); err != nil {
return "", fmt.Errorf("error preparing shell script: %s", err)
Expand Down
10 changes: 5 additions & 5 deletions powershell/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ type executePowershellFromCommandLineTemplateOptions struct {

var executePowershellFromCommandLineTemplate = template.Must(template.New("ExecuteCommandFromCommandLine").Funcs(template.FuncMap{
"escapeDoubleQuotes": func(textToEscape string) string {
textToEscape = strings.Replace(textToEscape, "\n", "", -1)
textToEscape = strings.Replace(textToEscape, "\r", "", -1)
textToEscape = strings.Replace(textToEscape, "\t", "", -1)
textToEscape = strings.Replace(textToEscape, `"`, `\"`, -1)
textToEscape = strings.ReplaceAll(textToEscape, "\n", "")
textToEscape = strings.ReplaceAll(textToEscape, "\r", "")
textToEscape = strings.ReplaceAll(textToEscape, "\t", "")
textToEscape = strings.ReplaceAll(textToEscape, `"`, `\"`)
return textToEscape
},
}).Parse(`powershell -NoProfile -ExecutionPolicy Bypass "{{escapeDoubleQuotes .Powershell}}"`))
Expand All @@ -38,7 +38,7 @@ type elevatedCommandTemplateOptions struct {

var elevatedCommandTemplate = template.Must(template.New("ElevatedCommand").Funcs(template.FuncMap{
"escapeSingleQuotes": func(textToEscape string) string {
return strings.Replace(textToEscape, `'`, `''`, -1)
return strings.ReplaceAll(textToEscape, `'`, `''`)
},
}).Parse(`
function GetTempFile($fileName) {
Expand Down

0 comments on commit 5d79058

Please sign in to comment.