diff --git a/.github/workflows/code-quality.yml b/.github/workflows/code-quality.yml index cb646bf6..f4f7c7e0 100644 --- a/.github/workflows/code-quality.yml +++ b/.github/workflows/code-quality.yml @@ -2,8 +2,10 @@ name: Qodana on: workflow_dispatch: push: - branches: # Specify your branches here - - main # The 'main' branch + branches: + - main + pull_request: + types: [opened, synchronize] jobs: qodana: @@ -19,5 +21,7 @@ jobs: fetch-depth: 0 # a full history is required for pull request analysis - name: 'Qodana Scan' uses: JetBrains/qodana-action@v2024.1.5 + with: + post-pr-comment: false env: QODANA_TOKEN: ${{ secrets.QODANA_TOKEN }} diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 603b0400..173c2437 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -87,7 +87,6 @@ jobs: with: name: test-log path: /tmp/gotest.log - if-no-files-found: error - name: Check for uncommitted changes in generated docs run: make docs && git diff --exit-code diff --git a/.github/workflows/testacc.yml b/.github/workflows/testacc.yml index a7749c08..73cc4f9c 100644 --- a/.github/workflows/testacc.yml +++ b/.github/workflows/testacc.yml @@ -49,6 +49,9 @@ jobs: - name: Get dependencies run: go mod download + - name: Set up gotestfmt + run: go install github.com/gotesttools/gotestfmt/v2/cmd/gotestfmt@latest + - uses: hashicorp/setup-terraform@651471c36a6092792c552e8b1bef71e592b462d8 # v3.1.1 with: terraform_version: ${{ matrix.terraform }}.* @@ -78,4 +81,3 @@ jobs: with: name: test-log path: /tmp/gotest.log - if-no-files-found: error diff --git a/docs/resources/virtual_environment_vm2.md b/docs/resources/virtual_environment_vm2.md index 8b5aeeee..a2656af9 100644 --- a/docs/resources/virtual_environment_vm2.md +++ b/docs/resources/virtual_environment_vm2.md @@ -36,6 +36,7 @@ The attributes are also marked as optional to allow the practitioner to set (or - `description` (String) The description of the VM. - `id` (Number) The unique identifier of the VM in the Proxmox cluster. - `name` (String) The name of the VM. Doesn't have to be unique. +- `stop_on_destroy` (Boolean) Set to true to stop (rather than shutdown) the VM on destroy (defaults to `false`). - `tags` (Set of String) The tags assigned to the VM. - `template` (Boolean) Set to true to create a VM template. - `timeouts` (Attributes) (see [below for nested schema](#nestedatt--timeouts)) diff --git a/fwprovider/ha/resource_hagroup.go b/fwprovider/ha/resource_hagroup.go index 6fa6344e..f0c41d1f 100644 --- a/fwprovider/ha/resource_hagroup.go +++ b/fwprovider/ha/resource_hagroup.go @@ -81,8 +81,8 @@ func (r *hagroupResource) Schema( Optional: true, Validators: []validator.String{ stringvalidator.UTF8LengthAtLeast(1), - stringvalidator.RegexMatches(regexp.MustCompile(`^[^\s]|^$`), "must not start with whitespace"), - stringvalidator.RegexMatches(regexp.MustCompile(`[^\s]$|^$`), "must not end with whitespace"), + stringvalidator.RegexMatches(regexp.MustCompile(`^\S|^$`), "must not start with whitespace"), + stringvalidator.RegexMatches(regexp.MustCompile(`\S$|^$`), "must not end with whitespace"), }, }, "nodes": schema.MapAttribute{ diff --git a/fwprovider/ha/resource_haresource.go b/fwprovider/ha/resource_haresource.go index 58629f82..f6a7aea9 100644 --- a/fwprovider/ha/resource_haresource.go +++ b/fwprovider/ha/resource_haresource.go @@ -101,8 +101,8 @@ func (r *haResourceResource) Schema( Optional: true, Validators: []validator.String{ stringvalidator.UTF8LengthAtLeast(1), - stringvalidator.RegexMatches(regexp.MustCompile(`^[^\s]|^$`), "must not start with whitespace"), - stringvalidator.RegexMatches(regexp.MustCompile(`[^\s]$|^$`), "must not end with whitespace"), + stringvalidator.RegexMatches(regexp.MustCompile(`^\S|^$`), "must not start with whitespace"), + stringvalidator.RegexMatches(regexp.MustCompile(`\S$|^$`), "must not end with whitespace"), }, }, "group": schema.StringAttribute{ diff --git a/fwprovider/vm/model.go b/fwprovider/vm/model.go index 7148e48f..3d3f78a1 100644 --- a/fwprovider/vm/model.go +++ b/fwprovider/vm/model.go @@ -35,13 +35,14 @@ type Model struct { ID types.Int64 `tfsdk:"id"` Retries types.Int64 `tfsdk:"retries"` } `tfsdk:"clone"` - ID types.Int64 `tfsdk:"id"` - Name types.String `tfsdk:"name"` - NodeName types.String `tfsdk:"node_name"` - Tags stringset.Value `tfsdk:"tags"` - Template types.Bool `tfsdk:"template"` - Timeouts timeouts.Value `tfsdk:"timeouts"` - VGA vga.Value `tfsdk:"vga"` + ID types.Int64 `tfsdk:"id"` + Name types.String `tfsdk:"name"` + NodeName types.String `tfsdk:"node_name"` + StopOnDestroy types.Bool `tfsdk:"stop_on_destroy"` + Tags stringset.Value `tfsdk:"tags"` + Template types.Bool `tfsdk:"template"` + Timeouts timeouts.Value `tfsdk:"timeouts"` + VGA vga.Value `tfsdk:"vga"` } // read retrieves the current state of the resource from the API and updates the state. diff --git a/fwprovider/vm/resource.go b/fwprovider/vm/resource.go index 26e90e6f..b921e592 100644 --- a/fwprovider/vm/resource.go +++ b/fwprovider/vm/resource.go @@ -374,11 +374,8 @@ func (r *Resource) Delete(ctx context.Context, req resource.DeleteRequest, resp return } - // stop := d.Get(mkStopOnDestroy).(bool) - stop := false - if status.Status != "stopped" { - if stop { + if state.StopOnDestroy.ValueBool() { if e := vmStop(ctx, vmAPI); e != nil { resp.Diagnostics.AddWarning("Failed to stop VM", e.Error()) } @@ -445,6 +442,9 @@ func (r *Resource) ImportState( return } + // not clear why this is needed, but ImportStateVerify fails without it + state.StopOnDestroy = types.BoolValue(false) + diags := resp.State.Set(ctx, state) resp.Diagnostics.Append(diags...) } diff --git a/fwprovider/vm/resource_schema.go b/fwprovider/vm/resource_schema.go index d6170684..37483656 100644 --- a/fwprovider/vm/resource_schema.go +++ b/fwprovider/vm/resource_schema.go @@ -14,6 +14,7 @@ import ( "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" "github.com/hashicorp/terraform-plugin-framework/resource" "github.com/hashicorp/terraform-plugin-framework/resource/schema" + "github.com/hashicorp/terraform-plugin-framework/resource/schema/booldefault" "github.com/hashicorp/terraform-plugin-framework/resource/schema/boolplanmodifier" "github.com/hashicorp/terraform-plugin-framework/resource/schema/int64default" "github.com/hashicorp/terraform-plugin-framework/resource/schema/int64planmodifier" @@ -88,6 +89,13 @@ func (r *Resource) Schema( Description: "The name of the node where the VM is provisioned.", Required: true, }, + "stop_on_destroy": schema.BoolAttribute{ + Description: "Set to true to stop (rather than shutdown) the VM on destroy.", + MarkdownDescription: "Set to true to stop (rather than shutdown) the VM on destroy (defaults to `false`).", + Optional: true, + Computed: true, + Default: booldefault.StaticBool(false), + }, "tags": stringset.ResourceAttribute("The tags assigned to the VM.", ""), "template": schema.BoolAttribute{ Description: "Set to true to create a VM template.", diff --git a/proxmox/nodes/tasks/tasks.go b/proxmox/nodes/tasks/tasks.go index 5780de66..eeba2270 100644 --- a/proxmox/nodes/tasks/tasks.go +++ b/proxmox/nodes/tasks/tasks.go @@ -45,7 +45,8 @@ func (c *Client) GetTaskStatus(ctx context.Context, upid string) (*GetTaskStatus // Reads first 50 lines by default. func (c *Client) GetTaskLog(ctx context.Context, upid string) ([]string, error) { resBody := &GetTaskLogResponseBody{} - lines := []string{} + + var lines []string //nolint: prealloc path, err := c.BuildPath(upid, "log") if err != nil { diff --git a/proxmox/nodes/vms/custom_storage_device.go b/proxmox/nodes/vms/custom_storage_device.go index 316e4ada..5959ffdb 100644 --- a/proxmox/nodes/vms/custom_storage_device.go +++ b/proxmox/nodes/vms/custom_storage_device.go @@ -112,7 +112,7 @@ func (d *CustomStorageDevice) IsCloudInitDrive(vmID int) bool { // EncodeOptions converts a CustomStorageDevice's common options a URL value. func (d *CustomStorageDevice) EncodeOptions() string { - values := []string{} + var values []string if d.AIO != nil { values = append(values, fmt.Sprintf("aio=%s", *d.AIO)) diff --git a/proxmox/nodes/vms/custom_usb_device.go b/proxmox/nodes/vms/custom_usb_device.go index 28605f8f..e141c43d 100644 --- a/proxmox/nodes/vms/custom_usb_device.go +++ b/proxmox/nodes/vms/custom_usb_device.go @@ -31,7 +31,7 @@ func (r *CustomUSBDevice) EncodeValues(key string, v *url.Values) error { return fmt.Errorf("either device ID or resource mapping must be set") } - values := []string{} + var values []string if r.HostDevice != nil { values = append(values, fmt.Sprintf("host=%s", *(r.HostDevice))) } diff --git a/proxmoxtf/datasource/firewall/alias.go b/proxmoxtf/datasource/firewall/alias.go deleted file mode 100644 index abf144df..00000000 --- a/proxmoxtf/datasource/firewall/alias.go +++ /dev/null @@ -1,72 +0,0 @@ -/* - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at https://mozilla.org/MPL/2.0/. - */ - -package firewall - -import ( - "context" - - "github.com/hashicorp/terraform-plugin-sdk/v2/diag" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" - - "github.com/bpg/terraform-provider-proxmox/proxmox/firewall" -) - -const ( - dvAliasComment = "" - - mkAliasName = "name" - mkAliasCIDR = "cidr" - mkAliasComment = "comment" -) - -// AliasSchema defines the schema for the alias. -func AliasSchema() map[string]*schema.Schema { - return map[string]*schema.Schema{ - mkAliasName: { - Type: schema.TypeString, - Description: "Alias name", - Required: true, - }, - mkAliasCIDR: { - Type: schema.TypeString, - Description: "IP/CIDR block", - Computed: true, - }, - mkAliasComment: { - Type: schema.TypeString, - Description: "Alias comment", - Computed: true, - }, - } -} - -// AliasRead reads the alias. -func AliasRead(ctx context.Context, fw firewall.API, d *schema.ResourceData) diag.Diagnostics { - var diags diag.Diagnostics - - aliasName := d.Get(mkAliasName).(string) - - alias, err := fw.GetAlias(ctx, aliasName) - if err != nil { - return diag.FromErr(err) - } - - d.SetId(aliasName) - - err = d.Set(mkAliasCIDR, alias.CIDR) - diags = append(diags, diag.FromErr(err)...) - - if alias.Comment != nil { - err = d.Set(mkAliasComment, alias.Comment) - } else { - err = d.Set(mkAliasComment, dvAliasComment) - } - - diags = append(diags, diag.FromErr(err)...) - - return diags -} diff --git a/proxmoxtf/datasource/firewall/alias_test.go b/proxmoxtf/datasource/firewall/alias_test.go deleted file mode 100644 index 33685f91..00000000 --- a/proxmoxtf/datasource/firewall/alias_test.go +++ /dev/null @@ -1,44 +0,0 @@ -/* - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at https://mozilla.org/MPL/2.0/. - */ - -package firewall - -import ( - "testing" - - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" - "github.com/stretchr/testify/require" - - "github.com/bpg/terraform-provider-proxmox/proxmoxtf/test" -) - -// TestAliasSchemaInstantiation tests whether the AliasSchema instance can be instantiated. -func TestAliasSchemaInstantiation(t *testing.T) { - t.Parallel() - require.NotNilf(t, AliasSchema(), "Cannot instantiate AliasSchema") -} - -// TestAliasSchema tests the AliasSchema. -func TestAliasSchema(t *testing.T) { - t.Parallel() - - s := AliasSchema() - - test.AssertRequiredArguments(t, s, []string{ - mkAliasName, - }) - - test.AssertComputedAttributes(t, s, []string{ - mkAliasCIDR, - mkAliasComment, - }) - - test.AssertValueTypes(t, s, map[string]schema.ValueType{ - mkAliasName: schema.TypeString, - mkAliasCIDR: schema.TypeString, - mkAliasComment: schema.TypeString, - }) -} diff --git a/proxmoxtf/datasource/firewall/aliases.go b/proxmoxtf/datasource/firewall/aliases.go deleted file mode 100644 index 6e850ac0..00000000 --- a/proxmoxtf/datasource/firewall/aliases.go +++ /dev/null @@ -1,53 +0,0 @@ -/* - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at https://mozilla.org/MPL/2.0/. - */ - -package firewall - -import ( - "context" - - "github.com/google/uuid" - "github.com/hashicorp/terraform-plugin-sdk/v2/diag" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" - - "github.com/bpg/terraform-provider-proxmox/proxmox/firewall" -) - -const ( - mkAliasesAliasNames = "alias_names" -) - -// AliasesSchema defines the schema for the Aliases data source. -func AliasesSchema() map[string]*schema.Schema { - return map[string]*schema.Schema{ - mkAliasesAliasNames: { - Type: schema.TypeList, - Description: "Alias Names", - Computed: true, - Elem: &schema.Schema{Type: schema.TypeString}, - }, - } -} - -// AliasesRead reads the aliases. -func AliasesRead(ctx context.Context, fw firewall.API, d *schema.ResourceData) diag.Diagnostics { - list, err := fw.ListAliases(ctx) - if err != nil { - return diag.FromErr(err) - } - - aliasNames := make([]interface{}, len(list)) - - for i, v := range list { - aliasNames[i] = v.Name - } - - d.SetId(uuid.New().String()) - - err = d.Set(mkAliasesAliasNames, aliasNames) - - return diag.FromErr(err) -} diff --git a/proxmoxtf/datasource/firewall/aliases_test.go b/proxmoxtf/datasource/firewall/aliases_test.go deleted file mode 100644 index 978c3371..00000000 --- a/proxmoxtf/datasource/firewall/aliases_test.go +++ /dev/null @@ -1,37 +0,0 @@ -/* - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at https://mozilla.org/MPL/2.0/. - */ - -package firewall - -import ( - "testing" - - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" - "github.com/stretchr/testify/require" - - "github.com/bpg/terraform-provider-proxmox/proxmoxtf/test" -) - -// TestAliasesSchemaInstantiation tests whether the AliasesSchema instance can be instantiated. -func TestAliasesSchemaInstantiation(t *testing.T) { - t.Parallel() - require.NotNil(t, AliasesSchema(), "Cannot instantiate AliasesSchema") -} - -// TestAliasesSchema tests the AliasesSchema. -func TestAliasesSchema(t *testing.T) { - t.Parallel() - - s := AliasesSchema() - - test.AssertComputedAttributes(t, s, []string{ - mkAliasesAliasNames, - }) - - test.AssertValueTypes(t, s, map[string]schema.ValueType{ - mkAliasesAliasNames: schema.TypeList, - }) -} diff --git a/proxmoxtf/datasource/firewall/ipset.go b/proxmoxtf/datasource/firewall/ipset.go deleted file mode 100644 index 63b9b82f..00000000 --- a/proxmoxtf/datasource/firewall/ipset.go +++ /dev/null @@ -1,127 +0,0 @@ -/* - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at https://mozilla.org/MPL/2.0/. - */ - -package firewall - -import ( - "context" - - "github.com/hashicorp/terraform-plugin-sdk/v2/diag" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" - - "github.com/bpg/terraform-provider-proxmox/proxmox/firewall" -) - -const ( - dvIPSetCIDRComment = "" - dvIPSetCIDRNoMatch = false - - mkIPSetName = "name" - mkIPSetCIDR = "cidr" - mkIPSetCIDRName = "name" - mkIPSetCIDRComment = "comment" - mkIPSetCIDRNoMatch = "nomatch" -) - -// IPSetSchema defines the schema for the IPSet. -func IPSetSchema() map[string]*schema.Schema { - return map[string]*schema.Schema{ - mkIPSetName: { - Type: schema.TypeString, - Description: "IPSet name", - Required: true, - }, - mkIPSetCIDRComment: { - Type: schema.TypeString, - Description: "IPSet comment", - Computed: true, - }, - mkIPSetCIDR: { - Type: schema.TypeList, - Description: "List of IP or Networks", - Computed: true, - Elem: &schema.Resource{ - Schema: map[string]*schema.Schema{ - mkIPSetCIDRName: { - Type: schema.TypeString, - Description: "Network/IP specification in CIDR format", - Computed: true, - }, - mkIPSetCIDRNoMatch: { - Type: schema.TypeBool, - Description: "No match this IP/CIDR", - Computed: true, - }, - mkIPSetCIDRComment: { - Type: schema.TypeString, - Description: "IP/CIDR comment", - Computed: true, - }, - }, - }, - }, - } -} - -// IPSetRead reads the IPSet. -func IPSetRead(ctx context.Context, fw firewall.API, d *schema.ResourceData) diag.Diagnostics { - var diags diag.Diagnostics - - ipSetName := d.Get(mkIPSetName).(string) - d.SetId(ipSetName) - - ipSetList, err := fw.ListIPSets(ctx) - if err != nil { - return diag.FromErr(err) - } - - for _, ipSet := range ipSetList { - if ipSet.Name == ipSetName { - if ipSet.Comment != nil { - err = d.Set(mkIPSetCIDRComment, ipSet.Comment) - } else { - err = d.Set(mkIPSetCIDRComment, dvIPSetCIDRComment) - } - - diags = append(diags, diag.FromErr(err)...) - - break - } - } - - content, err := fw.GetIPSetContent(ctx, ipSetName) - if err != nil { - return diag.FromErr(err) - } - - //nolint:prealloc - var cidrs []interface{} - - for _, v := range content { - cirdEntry := map[string]interface{}{} - - cirdEntry[mkIPSetCIDRName] = v.CIDR - - if v.NoMatch != nil { - cirdEntry[mkIPSetCIDRNoMatch] = bool(*v.NoMatch) - } else { - cirdEntry[mkIPSetCIDRNoMatch] = dvIPSetCIDRNoMatch - } - - if v.Comment != nil { - cirdEntry[mkIPSetCIDRComment] = v.Comment - } else { - cirdEntry[mkIPSetCIDRComment] = dvIPSetCIDRComment - } - - cidrs = append(cidrs, cirdEntry) - } - - err = d.Set(mkIPSetCIDR, cidrs) - diags = append(diags, diag.FromErr(err)...) - - return diags -} diff --git a/proxmoxtf/datasource/firewall/ipset_test.go b/proxmoxtf/datasource/firewall/ipset_test.go deleted file mode 100644 index 560b0050..00000000 --- a/proxmoxtf/datasource/firewall/ipset_test.go +++ /dev/null @@ -1,58 +0,0 @@ -/* - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at https://mozilla.org/MPL/2.0/. - */ - -package firewall - -import ( - "testing" - - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" - "github.com/stretchr/testify/require" - - "github.com/bpg/terraform-provider-proxmox/proxmoxtf/test" -) - -// TestIPSetSchemaInstantiation tests whether the IPSetSchema instance can be instantiated. -func TestIPSetSchemaInstantiation(t *testing.T) { - t.Parallel() - require.NotNil(t, IPSetSchema(), "Cannot instantiate IPSetSchema") -} - -// TestIPSetSchema tests the IPSetSchema. -func TestIPSetSchema(t *testing.T) { - t.Parallel() - - s := IPSetSchema() - - test.AssertRequiredArguments(t, s, []string{ - mkIPSetName, - }) - - test.AssertComputedAttributes(t, s, []string{ - mkIPSetCIDR, - mkIPSetCIDRComment, - }) - - test.AssertValueTypes(t, s, map[string]schema.ValueType{ - mkIPSetName: schema.TypeString, - mkIPSetCIDR: schema.TypeList, - mkIPSetCIDRComment: schema.TypeString, - }) - - cird := test.AssertNestedSchemaExistence(t, s, mkIPSetCIDR) - - test.AssertComputedAttributes(t, cird, []string{ - mkIPSetCIDRName, - mkIPSetCIDRNoMatch, - mkIPSetCIDRComment, - }) - - test.AssertValueTypes(t, cird, map[string]schema.ValueType{ - mkIPSetCIDRName: schema.TypeString, - mkIPSetCIDRNoMatch: schema.TypeBool, - mkIPSetCIDRComment: schema.TypeString, - }) -} diff --git a/proxmoxtf/datasource/firewall/ipsets.go b/proxmoxtf/datasource/firewall/ipsets.go deleted file mode 100644 index c25e5d6c..00000000 --- a/proxmoxtf/datasource/firewall/ipsets.go +++ /dev/null @@ -1,53 +0,0 @@ -/* - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at https://mozilla.org/MPL/2.0/. - */ - -package firewall - -import ( - "context" - - "github.com/google/uuid" - "github.com/hashicorp/terraform-plugin-sdk/v2/diag" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" - - "github.com/bpg/terraform-provider-proxmox/proxmox/firewall" -) - -const ( - mkIPSetsIPSetNames = "ipset_names" -) - -// IPSetsSchema defines the schema for the IP sets. -func IPSetsSchema() map[string]*schema.Schema { - return map[string]*schema.Schema{ - mkIPSetsIPSetNames: { - Type: schema.TypeList, - Description: "IPSet Names", - Computed: true, - Elem: &schema.Schema{Type: schema.TypeString}, - }, - } -} - -// IPSetsRead reads the IP sets. -func IPSetsRead(ctx context.Context, fw firewall.API, d *schema.ResourceData) diag.Diagnostics { - list, err := fw.ListIPSets(ctx) - if err != nil { - return diag.FromErr(err) - } - - ipSetNames := make([]interface{}, len(list)) - - for i, v := range list { - ipSetNames[i] = v.Name - } - - d.SetId(uuid.New().String()) - - err = d.Set(mkIPSetsIPSetNames, ipSetNames) - - return diag.FromErr(err) -} diff --git a/proxmoxtf/datasource/firewall/ipsets_test.go b/proxmoxtf/datasource/firewall/ipsets_test.go deleted file mode 100644 index c6b4b65d..00000000 --- a/proxmoxtf/datasource/firewall/ipsets_test.go +++ /dev/null @@ -1,37 +0,0 @@ -/* - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at https://mozilla.org/MPL/2.0/. - */ - -package firewall - -import ( - "testing" - - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" - "github.com/stretchr/testify/require" - - "github.com/bpg/terraform-provider-proxmox/proxmoxtf/test" -) - -// TestIPSetsSchemaInstantiation tests whether the IPSetsSchema instance can be instantiated. -func TestIPSetsSchemaInstantiation(t *testing.T) { - t.Parallel() - require.NotNil(t, IPSetsSchema(), "Cannot instantiate IPSetsSchema") -} - -// TestIPSetsSchema tests the IPSetsSchema. -func TestIPSetsSchema(t *testing.T) { - t.Parallel() - - s := IPSetsSchema() - - test.AssertComputedAttributes(t, s, []string{ - mkIPSetsIPSetNames, - }) - - test.AssertValueTypes(t, s, map[string]schema.ValueType{ - mkIPSetsIPSetNames: schema.TypeList, - }) -} diff --git a/proxmoxtf/datasource/firewall/rule.go b/proxmoxtf/datasource/firewall/rule.go deleted file mode 100644 index de592572..00000000 --- a/proxmoxtf/datasource/firewall/rule.go +++ /dev/null @@ -1,132 +0,0 @@ -/* - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at https://mozilla.org/MPL/2.0/. - */ - -package firewall - -import ( - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" -) - -const ( - mkRuleAction = "action" - mkRuleComment = "comment" - mkRuleDPort = "dport" - mkRuleDest = "dest" - mkRuleEnable = "enable" - mkRuleIFace = "iface" - mkRuleLog = "log" - mkRuleMacro = "macro" - mkRulePos = "pos" - mkRuleProto = "proto" - mkRuleSource = "source" - mkRuleSPort = "sport" - mkRuleType = "type" -) - -// RuleSchema returns the schema for the firewall rule data source. -func RuleSchema() map[string]*schema.Schema { - return map[string]*schema.Schema{ - mkRulePos: { - Type: schema.TypeInt, - Description: "Rule position", - Computed: true, - }, - mkRuleAction: { - Type: schema.TypeString, - Description: "Rule action ('ACCEPT', 'DROP', 'REJECT')", - Required: true, - }, - mkRuleType: { - Type: schema.TypeString, - Description: "Rule type ('in', 'out')", - Required: true, - }, - mkRuleComment: { - Type: schema.TypeString, - Description: "Rule comment", - Computed: true, - }, - mkRuleDest: { - Type: schema.TypeString, - Description: "Packet destination address", - Computed: true, - }, - mkRuleDPort: { - Type: schema.TypeString, - Description: "TCP/UDP destination port.", - Computed: true, - }, - mkRuleEnable: { - Type: schema.TypeBool, - Description: "Enable rule", - Computed: true, - }, - mkRuleIFace: { - Type: schema.TypeString, - Description: "Network interface name.", - Computed: true, - }, - mkRuleLog: { - Type: schema.TypeString, - Description: "Log level for this rule", - Computed: true, - }, - mkRuleMacro: { - Type: schema.TypeString, - Description: "Use predefined standard macro", - Computed: true, - }, - mkRuleProto: { - Type: schema.TypeString, - Description: "Packet protocol.", - Computed: true, - }, - mkRuleSource: { - Type: schema.TypeString, - Description: "Packet source address.", - Computed: true, - }, - mkRuleSPort: { - Type: schema.TypeString, - Description: "TCP/UDP source port.", - Computed: true, - }, - } -} - -// -// func baseRuleToMap(baseRule *firewall.BaseRule, rule map[string]interface{}) { -// if baseRule.Comment != nil { -// rule[mkRuleComment] = *baseRule.Comment -// } -// if baseRule.Dest != nil { -// rule[mkRuleDest] = *baseRule.Dest -// } -// if baseRule.DPort != nil { -// rule[mkRuleDPort] = *baseRule.DPort -// } -// if baseRule.Enable != nil { -// rule[mkRuleEnable] = *baseRule.Enable -// } -// if baseRule.IFace != nil { -// rule[mkRuleIFace] = *baseRule.IFace -// } -// if baseRule.Log != nil { -// rule[mkRuleLog] = *baseRule.Log -// } -// if baseRule.Macro != nil { -// rule[mkRuleMacro] = *baseRule.Macro -// } -// if baseRule.Proto != nil { -// rule[mkRuleProto] = *baseRule.Proto -// } -// if baseRule.Source != nil { -// rule[mkRuleSource] = *baseRule.Source -// } -// if baseRule.SPort != nil { -// rule[mkRuleSPort] = *baseRule.SPort -// } -// } diff --git a/proxmoxtf/datasource/firewall/rule_test.go b/proxmoxtf/datasource/firewall/rule_test.go deleted file mode 100644 index 927efff5..00000000 --- a/proxmoxtf/datasource/firewall/rule_test.go +++ /dev/null @@ -1,63 +0,0 @@ -/* - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at https://mozilla.org/MPL/2.0/. - */ - -package firewall - -import ( - "testing" - - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" - "github.com/stretchr/testify/require" - - "github.com/bpg/terraform-provider-proxmox/proxmoxtf/test" -) - -// TestRuleInstantiation tests whether the RuleSchema instance can be instantiated. -func TestRuleSchemaInstantiation(t *testing.T) { - t.Parallel() - require.NotNilf(t, RuleSchema(), "Cannot instantiate RuleSchema") -} - -// TestRuleSchema tests the RuleSchema. -func TestRuleSchema(t *testing.T) { - t.Parallel() - - s := RuleSchema() - - test.AssertRequiredArguments(t, s, []string{ - mkRuleAction, - mkRuleType, - }) - - test.AssertComputedAttributes(t, s, []string{ - mkRuleComment, - mkRuleDest, - mkRuleDPort, - mkRuleEnable, - mkRuleIFace, - mkRuleLog, - mkRuleMacro, - mkRuleProto, - mkRuleSource, - mkRuleSPort, - }) - - test.AssertValueTypes(t, s, map[string]schema.ValueType{ - mkRulePos: schema.TypeInt, - mkRuleAction: schema.TypeString, - mkRuleType: schema.TypeString, - mkRuleComment: schema.TypeString, - mkRuleDest: schema.TypeString, - mkRuleDPort: schema.TypeString, - mkRuleEnable: schema.TypeBool, - mkRuleIFace: schema.TypeString, - mkRuleLog: schema.TypeString, - mkRuleMacro: schema.TypeString, - mkRuleProto: schema.TypeString, - mkRuleSource: schema.TypeString, - mkRuleSPort: schema.TypeString, - }) -} diff --git a/proxmoxtf/datasource/firewall/security_group.go b/proxmoxtf/datasource/firewall/security_group.go deleted file mode 100644 index 55c83462..00000000 --- a/proxmoxtf/datasource/firewall/security_group.go +++ /dev/null @@ -1,39 +0,0 @@ -/* - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at https://mozilla.org/MPL/2.0/. - */ - -package firewall - -import ( - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" -) - -const ( - mkSecurityGroupName = "name" - mkSecurityGroupComment = "comment" - mkRules = "rules" -) - -// SecurityGroupSchema defines the schema for the security group data source. -func SecurityGroupSchema() map[string]*schema.Schema { - return map[string]*schema.Schema{ - mkSecurityGroupName: { - Type: schema.TypeString, - Description: "Security group name", - Required: true, - }, - mkSecurityGroupComment: { - Type: schema.TypeString, - Description: "Security group comment", - Computed: true, - }, - mkRules: { - Type: schema.TypeList, - Description: "List of rules", - Computed: true, - Elem: &schema.Resource{Schema: RuleSchema()}, - }, - } -} diff --git a/proxmoxtf/datasource/firewall/security_group_test.go b/proxmoxtf/datasource/firewall/security_group_test.go deleted file mode 100644 index f4c53130..00000000 --- a/proxmoxtf/datasource/firewall/security_group_test.go +++ /dev/null @@ -1,44 +0,0 @@ -/* - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at https://mozilla.org/MPL/2.0/. - */ - -package firewall - -import ( - "testing" - - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" - "github.com/stretchr/testify/require" - - "github.com/bpg/terraform-provider-proxmox/proxmoxtf/test" -) - -// TestSecurityGroupSchemaInstantiation tests whether the SecurityGroupSchema instance can be instantiated. -func TestSecurityGroupSchemaInstantiation(t *testing.T) { - t.Parallel() - require.NotNil(t, SecurityGroupSchema(), "Cannot instantiate SecurityGroupSchema") -} - -// TestSecurityGroupSchema tests the SecurityGroupSchema. -func TestSecurityGroupSchema(t *testing.T) { - t.Parallel() - - s := SecurityGroupSchema() - - test.AssertRequiredArguments(t, s, []string{ - mkSecurityGroupName, - }) - - test.AssertComputedAttributes(t, s, []string{ - mkSecurityGroupComment, - mkRules, - }) - - test.AssertValueTypes(t, s, map[string]schema.ValueType{ - mkSecurityGroupName: schema.TypeString, - mkSecurityGroupComment: schema.TypeString, - mkRules: schema.TypeList, - }) -} diff --git a/proxmoxtf/datasource/firewall/security_groups.go b/proxmoxtf/datasource/firewall/security_groups.go deleted file mode 100644 index 5178b28a..00000000 --- a/proxmoxtf/datasource/firewall/security_groups.go +++ /dev/null @@ -1,27 +0,0 @@ -/* - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at https://mozilla.org/MPL/2.0/. - */ - -package firewall - -import ( - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" -) - -const ( - mkSecurityGroupsSecurityGroupNames = "security_group_names" -) - -// SecurityGroupsSchema defines the schema for the security groups. -func SecurityGroupsSchema() map[string]*schema.Schema { - return map[string]*schema.Schema{ - mkSecurityGroupsSecurityGroupNames: { - Type: schema.TypeList, - Description: "Security Group Names", - Computed: true, - Elem: &schema.Schema{Type: schema.TypeString}, - }, - } -} diff --git a/proxmoxtf/datasource/firewall/security_groups_test.go b/proxmoxtf/datasource/firewall/security_groups_test.go deleted file mode 100644 index 44fafb88..00000000 --- a/proxmoxtf/datasource/firewall/security_groups_test.go +++ /dev/null @@ -1,37 +0,0 @@ -/* - * This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at https://mozilla.org/MPL/2.0/. - */ - -package firewall - -import ( - "testing" - - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" - "github.com/stretchr/testify/require" - - "github.com/bpg/terraform-provider-proxmox/proxmoxtf/test" -) - -// TestSecurityGroupsSchemaInstantiation tests whether the SecurityGroupsSchema instance can be instantiated. -func TestSecurityGroupsSchemaInstantiation(t *testing.T) { - t.Parallel() - require.NotNil(t, SecurityGroupsSchema(), "Cannot instantiate SecurityGroupsSchema") -} - -// TestSecurityGroupsSchema tests the SecurityGroupsSchema. -func TestSecurityGroupsSchema(t *testing.T) { - t.Parallel() - - s := SecurityGroupsSchema() - - test.AssertComputedAttributes(t, s, []string{ - mkSecurityGroupsSecurityGroupNames, - }) - - test.AssertValueTypes(t, s, map[string]schema.ValueType{ - mkSecurityGroupsSecurityGroupNames: schema.TypeList, - }) -} diff --git a/proxmoxtf/resource/vm/vm.go b/proxmoxtf/resource/vm/vm.go index 741bcae8..9cf8b8ab 100644 --- a/proxmoxtf/resource/vm/vm.go +++ b/proxmoxtf/resource/vm/vm.go @@ -5512,7 +5512,7 @@ func getDiskDatastores(vm *vms.GetResponseData, d *schema.ResourceData) []string datastoresSet[fileIDParts[0]] = 1 } - datastores := []string{} + var datastores []string //nolint: prealloc for datastore := range datastoresSet { datastores = append(datastores, datastore) } diff --git a/qodana.yaml b/qodana.yaml index fa0b07db..744508c3 100644 --- a/qodana.yaml +++ b/qodana.yaml @@ -1,6 +1,5 @@ version: "1.0" -# renovate: datasource=docker depName=jetbrains/qodana-go -linter: jetbrains/qodana-go:2024.1 +linter: jetbrains/qodana-go:latest include: - name: CheckDependencyLicenses - name: RegExpRedundantEscape diff --git a/utils/strings.go b/utils/strings.go index 9d31192e..9ade04b0 100644 --- a/utils/strings.go +++ b/utils/strings.go @@ -1,10 +1,17 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + package utils // ConvertToStringSlice helps convert interface slice to string slice. func ConvertToStringSlice(interfaceSlice []interface{}) []string { - resultSlice := []string{} - for _, val := range interfaceSlice { - resultSlice = append(resultSlice, val.(string)) + resultSlice := make([]string, len(interfaceSlice)) + + for i, val := range interfaceSlice { + resultSlice[i] = val.(string) } return resultSlice