From 8e447ff58cfb00039479c7a5c5e9755bfa7f7c97 Mon Sep 17 00:00:00 2001 From: Simon Deziel Date: Tue, 10 Dec 2024 13:32:23 -0500 Subject: [PATCH 01/31] Makefile: drop unneeded Go version in `go mod tidy` Signed-off-by: Simon Deziel --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index b2da4bd6..a979ec65 100644 --- a/Makefile +++ b/Makefile @@ -65,7 +65,7 @@ static-analysis: .PHONY: update-gomod update-gomod: $(GO) get -t -v -u ./... - $(GO) mod tidy --go=1.23.3 + $(GO) mod tidy $(GO) get toolchain@none @echo "Dependencies updated" From 9852d08064298d94e5838021fc8108166f6163b2 Mon Sep 17 00:00:00 2001 From: Simon Deziel Date: Tue, 10 Dec 2024 13:40:12 -0500 Subject: [PATCH 02/31] Align golangci-lint config with that of LXD Signed-off-by: Simon Deziel --- .golangci.yml | 102 ++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 95 insertions(+), 7 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index dcd832b6..886dd258 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -1,18 +1,106 @@ linters: - # TODO: remove the `disable` section - disable: - - errcheck - - ineffassign - - staticcheck - - unused enable: + - gci - godot - gofmt - misspell - whitespace - - gci + - revive +issues: + exclude-use-default: false linters-settings: gci: sections: - standard - default + errcheck: + exclude-functions: + - (*os.File).Close + revive: + rules: + # https://github.com/mgechev/revive/blob/2a1701aadbedfcc175cb92836a51407bec382652/RULES_DESCRIPTIONS.md#exported + - name: exported + arguments: + - "checkPrivateReceivers" + - "disableStutteringCheck" + + # https://github.com/mgechev/revive/blob/2a1701aadbedfcc175cb92836a51407bec382652/RULES_DESCRIPTIONS.md#unchecked-type-assertion + - name: unchecked-type-assertion + arguments: + - { "acceptIgnoredAssertionResult": true } + + # https://github.com/mgechev/revive/blob/2a1701aadbedfcc175cb92836a51407bec382652/RULES_DESCRIPTIONS.md#var-naming + - name: var-naming + arguments: # The arguments here are quite odd looking. See the rule description. + - [ ] + - [ ] + - [ { "upperCaseConst": true } ] + + # https://github.com/mgechev/revive/blob/2a1701aadbedfcc175cb92836a51407bec382652/RULES_DESCRIPTIONS.md#early-return + - name: early-return + + # https://github.com/mgechev/revive/blob/2a1701aadbedfcc175cb92836a51407bec382652/RULES_DESCRIPTIONS.md#redundant-import-alias + - name: redundant-import-alias + + # https://github.com/mgechev/revive/blob/2a1701aadbedfcc175cb92836a51407bec382652/RULES_DESCRIPTIONS.md#redefines-builtin-id + - name: redefines-builtin-id + + # https://github.com/mgechev/revive/blob/2a1701aadbedfcc175cb92836a51407bec382652/RULES_DESCRIPTIONS.md#struct-tag + - name: struct-tag + + # https://github.com/mgechev/revive/blob/2a1701aadbedfcc175cb92836a51407bec382652/RULES_DESCRIPTIONS.md#receiver-naming + - name: receiver-naming + + # https://github.com/mgechev/revive/blob/2a1701aadbedfcc175cb92836a51407bec382652/RULES_DESCRIPTIONS.md#deep-exit + - name: deep-exit + + # https://github.com/mgechev/revive/blob/2a1701aadbedfcc175cb92836a51407bec382652/RULES_DESCRIPTIONS.md#bool-literal-in-expr + - name: bool-literal-in-expr + + # https://github.com/mgechev/revive/blob/2a1701aadbedfcc175cb92836a51407bec382652/RULES_DESCRIPTIONS.md#comment-spacings + - name: comment-spacings + + # https://github.com/mgechev/revive/blob/2a1701aadbedfcc175cb92836a51407bec382652/RULES_DESCRIPTIONS.md#confusing-results + - name: confusing-results + + # https://github.com/mgechev/revive/blob/2a1701aadbedfcc175cb92836a51407bec382652/RULES_DESCRIPTIONS.md#use-any + - name: use-any + + # https://github.com/mgechev/revive/blob/2a1701aadbedfcc175cb92836a51407bec382652/RULES_DESCRIPTIONS.md#bare-return + - name: bare-return + + # https://github.com/mgechev/revive/blob/2a1701aadbedfcc175cb92836a51407bec382652/RULES_DESCRIPTIONS.md#empty-block + - name: empty-block + + # https://github.com/mgechev/revive/blob/2a1701aadbedfcc175cb92836a51407bec382652/RULES_DESCRIPTIONS.md#range-val-address + - name: range-val-address + + # https://github.com/mgechev/revive/blob/2a1701aadbedfcc175cb92836a51407bec382652/RULES_DESCRIPTIONS.md#range-val-in-closure + - name: range-val-in-closure + + # https://github.com/mgechev/revive/blob/2a1701aadbedfcc175cb92836a51407bec382652/RULES_DESCRIPTIONS.md#var-declaration + - name: var-declaration + + # https://github.com/mgechev/revive/blob/2a1701aadbedfcc175cb92836a51407bec382652/RULES_DESCRIPTIONS.md#useless-break + - name: useless-break + + # https://github.com/mgechev/revive/blob/2a1701aadbedfcc175cb92836a51407bec382652/RULES_DESCRIPTIONS.md#error-naming + - name: error-naming + + # https://github.com/mgechev/revive/blob/2a1701aadbedfcc175cb92836a51407bec382652/RULES_DESCRIPTIONS.md#indent-error-flow + - name: indent-error-flow + + # https://github.com/mgechev/revive/blob/2a1701aadbedfcc175cb92836a51407bec382652/RULES_DESCRIPTIONS.md#datarace + - name: datarace + + # https://github.com/mgechev/revive/blob/2a1701aadbedfcc175cb92836a51407bec382652/RULES_DESCRIPTIONS.md#modifies-value-receiver + - name: modifies-value-receiver + + # https://github.com/mgechev/revive/blob/2a1701aadbedfcc175cb92836a51407bec382652/RULES_DESCRIPTIONS.md#empty-lines + - name: empty-lines + + # https://github.com/mgechev/revive/blob/2a1701aadbedfcc175cb92836a51407bec382652/RULES_DESCRIPTIONS.md#duplicated-imports + - name: duplicated-imports + + # https://github.com/mgechev/revive/blob/2a1701aadbedfcc175cb92836a51407bec382652/RULES_DESCRIPTIONS.md#error-return + - name: error-return From 9a7ad5041982d08a8d1dd0a59298f43769b2ba2c Mon Sep 17 00:00:00 2001 From: Simon Deziel Date: Tue, 10 Dec 2024 15:11:32 -0500 Subject: [PATCH 03/31] golangci: disable exported and var-naming revive rules for now Signed-off-by: Simon Deziel --- .golangci.yml | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index 886dd258..026b3360 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -18,23 +18,23 @@ linters-settings: - (*os.File).Close revive: rules: - # https://github.com/mgechev/revive/blob/2a1701aadbedfcc175cb92836a51407bec382652/RULES_DESCRIPTIONS.md#exported - - name: exported - arguments: - - "checkPrivateReceivers" - - "disableStutteringCheck" + ## https://github.com/mgechev/revive/blob/2a1701aadbedfcc175cb92836a51407bec382652/RULES_DESCRIPTIONS.md#exported + #- name: exported + # arguments: + # - "checkPrivateReceivers" + # - "disableStutteringCheck" # https://github.com/mgechev/revive/blob/2a1701aadbedfcc175cb92836a51407bec382652/RULES_DESCRIPTIONS.md#unchecked-type-assertion - name: unchecked-type-assertion arguments: - { "acceptIgnoredAssertionResult": true } - # https://github.com/mgechev/revive/blob/2a1701aadbedfcc175cb92836a51407bec382652/RULES_DESCRIPTIONS.md#var-naming - - name: var-naming - arguments: # The arguments here are quite odd looking. See the rule description. - - [ ] - - [ ] - - [ { "upperCaseConst": true } ] + ## https://github.com/mgechev/revive/blob/2a1701aadbedfcc175cb92836a51407bec382652/RULES_DESCRIPTIONS.md#var-naming + #- name: var-naming + # arguments: # The arguments here are quite odd looking. See the rule description. + # - [ ] + # - [ ] + # - [ { "upperCaseConst": true } ] # https://github.com/mgechev/revive/blob/2a1701aadbedfcc175cb92836a51407bec382652/RULES_DESCRIPTIONS.md#early-return - name: early-return From 29e4e0656895930a593ba9eae59b75cbe1509e08 Mon Sep 17 00:00:00 2001 From: Simon Deziel Date: Tue, 10 Dec 2024 13:40:31 -0500 Subject: [PATCH 04/31] internal/instance/resource_instance_snapshot: simplify if/else (revive) Signed-off-by: Simon Deziel --- .../instance/resource_instance_snapshot.go | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/internal/instance/resource_instance_snapshot.go b/internal/instance/resource_instance_snapshot.go index 7f7af56f..689d787e 100644 --- a/internal/instance/resource_instance_snapshot.go +++ b/internal/instance/resource_instance_snapshot.go @@ -152,16 +152,16 @@ func (r InstanceSnapshotResource) Create(ctx context.Context, req resource.Creat // Wait for snapshot operation to complete. serr = op.Wait() - if serr != nil { - if snapshotReq.Stateful && strings.Contains(serr.Error(), "Dumping FAILED") { - log.Printf("[DEBUG] Error creating stateful snapshot [retry %d]: %v", i, serr) - time.Sleep(3 * time.Second) - } else if strings.Contains(serr.Error(), "file has vanished") { - // Ignore, try again. - time.Sleep(3 * time.Second) - } else { - break - } + if serr == nil { + break + } + + if snapshotReq.Stateful && strings.Contains(serr.Error(), "Dumping FAILED") { + log.Printf("[DEBUG] Error creating stateful snapshot [retry %d]: %v", i, serr) + time.Sleep(3 * time.Second) + } else if strings.Contains(serr.Error(), "file has vanished") { + // Ignore, try again. + time.Sleep(3 * time.Second) } else { break } From da816d1dc95ce4b0b8d38e488cb4134b4178977a Mon Sep 17 00:00:00 2001 From: Simon Deziel Date: Tue, 10 Dec 2024 13:42:30 -0500 Subject: [PATCH 05/31] internal/common/lxd_interface: fix comment of exported structs Signed-off-by: Simon Deziel --- internal/common/lxd_interface.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/common/lxd_interface.go b/internal/common/lxd_interface.go index b6094261..ef376229 100644 --- a/internal/common/lxd_interface.go +++ b/internal/common/lxd_interface.go @@ -10,7 +10,7 @@ import ( "github.com/hashicorp/terraform-plugin-framework/types" ) -// Interface represents LXD instance network interface. +// InterfaceModel represents LXD instance network interface. type InterfaceModel struct { // Real name of the interface within the instance. If config interface is // defined as "eth0", the real interface within a container will have the @@ -27,7 +27,7 @@ type InterfaceModel struct { Addresses types.List `tfsdk:"ips"` } -// Interface IP is a wrapper of the interface IP address. +// IPModel is a wrapper of the interface IP address. type IPModel struct { // IP address. Address types.String `tfsdk:"address"` From 64b705f36a4301aa7f206a7580f30f7a69fda8b9 Mon Sep 17 00:00:00 2001 From: Simon Deziel Date: Tue, 10 Dec 2024 13:57:55 -0500 Subject: [PATCH 06/31] internal/utils: fix comment for DiffSlices Signed-off-by: Simon Deziel --- internal/utils/utils.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/utils/utils.go b/internal/utils/utils.go index fa6ff214..4f055bcc 100644 --- a/internal/utils/utils.go +++ b/internal/utils/utils.go @@ -71,7 +71,7 @@ func SortMapKeys[T any](m map[string]T) []string { return keys } -// DiffSlice compares two slices and returns removed and added elements. +// DiffSlices compares two slices and returns removed and added elements. // Note: Does not find differences for duplicate elemnts. func DiffSlices[T comparable](sliceA []T, sliceB []T) (removed []T, added []T) { mapA := make(map[T]bool, len(sliceA)) From fa4aa91a48c4777a9e6b016812565b6eb24d2fb8 Mon Sep 17 00:00:00 2001 From: Simon Deziel Date: Tue, 10 Dec 2024 13:58:28 -0500 Subject: [PATCH 07/31] internal/common/lxd_exec: add comments for exported type/struct Signed-off-by: Simon Deziel --- internal/common/lxd_exec.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/internal/common/lxd_exec.go b/internal/common/lxd_exec.go index b4429b7f..4810caaa 100644 --- a/internal/common/lxd_exec.go +++ b/internal/common/lxd_exec.go @@ -13,6 +13,7 @@ import ( "github.com/terraform-lxd/terraform-provider-lxd/internal/utils" ) +// ExecTriggerType represents an exec trigger. type ExecTriggerType string const ( @@ -25,6 +26,7 @@ func (t ExecTriggerType) String() string { return string(t) } +// ExecModel represents exec command to be executed on LXD instance. type ExecModel struct { Command types.List `tfsdk:"command"` Environment types.Map `tfsdk:"environment"` From 23782bb6c9f8e3d423c770c8b8182927626a5d41 Mon Sep 17 00:00:00 2001 From: Simon Deziel Date: Tue, 10 Dec 2024 13:59:01 -0500 Subject: [PATCH 08/31] internal/common/lxd_import: `s/importId/importID/g` Signed-off-by: Simon Deziel --- internal/common/lxd_import.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/internal/common/lxd_import.go b/internal/common/lxd_import.go index 3a6ba98f..a10412c2 100644 --- a/internal/common/lxd_import.go +++ b/internal/common/lxd_import.go @@ -37,25 +37,25 @@ type ImportMetadata struct { // "image": "jammy" // option // "optKey2": "value" // option // } -func (m ImportMetadata) ParseImportID(importId string) (map[string]string, diag.Diagnostic) { - if strings.TrimSpace(importId) == "" { - return nil, newImportIDError(m, importId, fmt.Errorf("Import ID cannot be empty")) +func (m ImportMetadata) ParseImportID(importID string) (map[string]string, diag.Diagnostic) { + if strings.TrimSpace(importID) == "" { + return nil, newImportIDError(m, importID, fmt.Errorf("Import ID cannot be empty")) } // First split by comma to determine mandatory and optional parts. - parts := strings.Split(importId, ",") + parts := strings.Split(importID, ",") // Extract fields (including project and remote) from first part. result, err := processFields(parts[0], m.RequiredFields) if err != nil { - return nil, newImportIDError(m, importId, err) + return nil, newImportIDError(m, importID, err) } // Extract options. if len(parts) > 1 { options, err := processOptions(parts[1:], m.AllowedOptions) if err != nil { - return nil, newImportIDError(m, importId, err) + return nil, newImportIDError(m, importID, err) } for k, v := range options { From ea3a11731b1aa89996a9073b54ea9a38205f5520 Mon Sep 17 00:00:00 2001 From: Simon Deziel Date: Tue, 10 Dec 2024 13:59:30 -0500 Subject: [PATCH 09/31] internal/network/resource_network_forward: add comment to exported struct Signed-off-by: Simon Deziel --- internal/network/resource_network_forward.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/network/resource_network_forward.go b/internal/network/resource_network_forward.go index e784846b..a4f34754 100644 --- a/internal/network/resource_network_forward.go +++ b/internal/network/resource_network_forward.go @@ -35,7 +35,7 @@ type NetworkForwardModel struct { Config types.Map `tfsdk:"config"` } -// NetworkForwardModel resource data model that matches the schema. +// NetworkForwardPortModel resource data model that matches the schema. type NetworkForwardPortModel struct { Description types.String `tfsdk:"description"` Protocol types.String `tfsdk:"protocol"` From 606fae6698c9fcbf002cd13118a950567d2e49e2 Mon Sep 17 00:00:00 2001 From: Simon Deziel Date: Tue, 10 Dec 2024 14:00:08 -0500 Subject: [PATCH 10/31] internal/profile/resource_profile: add comment to exported struct Signed-off-by: Simon Deziel --- internal/profile/resource_profile.go | 1 + 1 file changed, 1 insertion(+) diff --git a/internal/profile/resource_profile.go b/internal/profile/resource_profile.go index cff46231..b4e667af 100644 --- a/internal/profile/resource_profile.go +++ b/internal/profile/resource_profile.go @@ -26,6 +26,7 @@ import ( provider_config "github.com/terraform-lxd/terraform-provider-lxd/internal/provider-config" ) +// ProfileModel represents a LXD profile. type ProfileModel struct { Name types.String `tfsdk:"name"` Description types.String `tfsdk:"description"` From b2bbe7669c1e5a9577ada0a01c8f0ed142025ddf Mon Sep 17 00:00:00 2001 From: Simon Deziel Date: Tue, 10 Dec 2024 14:00:37 -0500 Subject: [PATCH 11/31] internal/storage/resource_storage_bucket_key: add comment to exported struct Signed-off-by: Simon Deziel --- internal/storage/resource_storage_bucket_key.go | 1 + 1 file changed, 1 insertion(+) diff --git a/internal/storage/resource_storage_bucket_key.go b/internal/storage/resource_storage_bucket_key.go index 8511e5f4..df3a251c 100644 --- a/internal/storage/resource_storage_bucket_key.go +++ b/internal/storage/resource_storage_bucket_key.go @@ -22,6 +22,7 @@ import ( provider_config "github.com/terraform-lxd/terraform-provider-lxd/internal/provider-config" ) +// StorageBucketKeyModel represents a LXD storage bucket key. type StorageBucketKeyModel struct { Name types.String `tfsdk:"name"` Description types.String `tfsdk:"description"` From b62e1739f655cba052c43d678ee1111fed10138b Mon Sep 17 00:00:00 2001 From: Simon Deziel Date: Tue, 10 Dec 2024 14:00:52 -0500 Subject: [PATCH 12/31] internal/storage/resource_storage_pool: add comment to exported struct Signed-off-by: Simon Deziel --- internal/storage/resource_storage_pool.go | 1 + 1 file changed, 1 insertion(+) diff --git a/internal/storage/resource_storage_pool.go b/internal/storage/resource_storage_pool.go index f26918da..ba49e68c 100644 --- a/internal/storage/resource_storage_pool.go +++ b/internal/storage/resource_storage_pool.go @@ -25,6 +25,7 @@ import ( provider_config "github.com/terraform-lxd/terraform-provider-lxd/internal/provider-config" ) +// StoragePoolModel represents a LXD storage pool. type StoragePoolModel struct { Name types.String `tfsdk:"name"` Description types.String `tfsdk:"description"` From c7a81761fcb9ade5617991505e0717505d1ec0ea Mon Sep 17 00:00:00 2001 From: Simon Deziel Date: Tue, 10 Dec 2024 14:00:58 -0500 Subject: [PATCH 13/31] internal/storage/resource_storage_volume_copy: add comment to exported struct Signed-off-by: Simon Deziel --- internal/storage/resource_storage_volume_copy.go | 1 + 1 file changed, 1 insertion(+) diff --git a/internal/storage/resource_storage_volume_copy.go b/internal/storage/resource_storage_volume_copy.go index 8f23a843..dc7eceb7 100644 --- a/internal/storage/resource_storage_volume_copy.go +++ b/internal/storage/resource_storage_volume_copy.go @@ -17,6 +17,7 @@ import ( provider_config "github.com/terraform-lxd/terraform-provider-lxd/internal/provider-config" ) +// StorageVolumeCopyModel represents a LXD storage volume copy. type StorageVolumeCopyModel struct { Name types.String `tfsdk:"name"` Pool types.String `tfsdk:"pool"` From 52845a2ffa4e1fc8a0bd1c12e0fc704b4dccb776 Mon Sep 17 00:00:00 2001 From: Simon Deziel Date: Tue, 10 Dec 2024 14:01:18 -0500 Subject: [PATCH 14/31] internal/utils/buffer: add comments to exported funcs Signed-off-by: Simon Deziel --- internal/utils/buffer.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/internal/utils/buffer.go b/internal/utils/buffer.go index 373e4d3d..422c8554 100644 --- a/internal/utils/buffer.go +++ b/internal/utils/buffer.go @@ -20,7 +20,7 @@ type bufferCloser struct { mux *sync.RWMutex } -// BufferCloser returns a buffer that wraps bytes.Buffer and satisfies +// NewBufferCloser returns a buffer that wraps bytes.Buffer and satisfies // io.Closer. func NewBufferCloser() Buffer { return bufferCloser{ @@ -58,18 +58,22 @@ func NewDiscardCloser() Buffer { return discardCloser{} } +// Write provides a nop implementation for io.discardCloser. func (b discardCloser) Write(p []byte) (int, error) { return len(p), nil } +// Read provides a nop implementation for io.discardCloser. func (b discardCloser) Read(p []byte) (int, error) { return len(p), nil } +// Close provides a nop implementation for io.discardCloser. func (b discardCloser) Close() error { return nil } +// String implements fmt.Stringer for io.discardCloser. func (b discardCloser) String() string { return "" } From d56e40d9c34561de07b5df9629aee03a1fca7598 Mon Sep 17 00:00:00 2001 From: Simon Deziel Date: Tue, 10 Dec 2024 14:08:12 -0500 Subject: [PATCH 15/31] internal/acctest/checks: ignore conn.Close() error Signed-off-by: Simon Deziel --- internal/acctest/checks.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/acctest/checks.go b/internal/acctest/checks.go index 4d8370de..32edc245 100644 --- a/internal/acctest/checks.go +++ b/internal/acctest/checks.go @@ -121,7 +121,7 @@ func PreCheckLocalServerHTTPS(t *testing.T) { t.Skip(`Skipping remote provider test. LXD is not available on "https://127.0.0.1:8443"`) } - conn.Close() + _ = conn.Close() } // ConfigureTrustPassword sets and returns the trust password. If the server From 4eb2269aa5f25fa9b3b601950a24e235f6f13247 Mon Sep 17 00:00:00 2001 From: Simon Deziel Date: Tue, 10 Dec 2024 14:08:44 -0500 Subject: [PATCH 16/31] internal/common/lxd_import: `s/importId/importID/g` Signed-off-by: Simon Deziel --- internal/common/lxd_import.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/common/lxd_import.go b/internal/common/lxd_import.go index a10412c2..3140757c 100644 --- a/internal/common/lxd_import.go +++ b/internal/common/lxd_import.go @@ -142,7 +142,7 @@ func processOptions(options []string, allowedOptions []string) (map[string]strin } // newImportIDError converts an error into terraform diagnostic. -func newImportIDError(m ImportMetadata, importId string, err error) diag.Diagnostic { +func newImportIDError(m ImportMetadata, importID string, err error) diag.Diagnostic { remote := "[:]" project := "[/]" if len(m.RequiredFields) > 1 { @@ -157,7 +157,7 @@ func newImportIDError(m ImportMetadata, importId string, err error) diag.Diagnos fields := fmt.Sprintf("<%s>", strings.Join(m.RequiredFields, ">/<")) return diag.NewErrorDiagnostic( - fmt.Sprintf("Invalid import ID: %q", importId), + fmt.Sprintf("Invalid import ID: %q", importID), fmt.Sprintf( "%v.\n\nValid import format:\nimport lxd_%s. %s%s%s%s", err, m.ResourceName, remote, project, fields, options, From 8c66fd7aacdc4c46c2dce9559400a005b39ee0c1 Mon Sep 17 00:00:00 2001 From: Simon Deziel Date: Tue, 10 Dec 2024 14:34:42 -0500 Subject: [PATCH 17/31] internal/provider/provider: remove dup'ed import Signed-off-by: Simon Deziel --- internal/provider/provider.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/internal/provider/provider.go b/internal/provider/provider.go index fea16a95..3a2ec052 100644 --- a/internal/provider/provider.go +++ b/internal/provider/provider.go @@ -20,7 +20,6 @@ import ( "github.com/terraform-lxd/terraform-provider-lxd/internal/network" "github.com/terraform-lxd/terraform-provider-lxd/internal/profile" "github.com/terraform-lxd/terraform-provider-lxd/internal/project" - config "github.com/terraform-lxd/terraform-provider-lxd/internal/provider-config" provider_config "github.com/terraform-lxd/terraform-provider-lxd/internal/provider-config" "github.com/terraform-lxd/terraform-provider-lxd/internal/storage" "github.com/terraform-lxd/terraform-provider-lxd/internal/truststore" @@ -182,7 +181,7 @@ func (p *LxdProvider) Configure(ctx context.Context, req provider.ConfigureReque } } - remotes := make(map[string]config.LxdRemote) + remotes := make(map[string]provider_config.LxdRemote) // Read remotes from Terraform schema. for _, remote := range data.Remotes { @@ -193,7 +192,7 @@ func (p *LxdProvider) Configure(ctx context.Context, req provider.ConfigureReque protocol = "lxd" } - address, err := config.DetermineLXDAddress(protocol, remote.Address.ValueString()) + address, err := provider_config.DetermineLXDAddress(protocol, remote.Address.ValueString()) if err != nil { resp.Diagnostics.AddError(fmt.Sprintf("Invalid remote %q", name), err.Error()) return From 71f1304e5fbd7e593034093badce19d0e2696c9d Mon Sep 17 00:00:00 2001 From: Simon Deziel Date: Tue, 10 Dec 2024 14:42:52 -0500 Subject: [PATCH 18/31] internal/instance/resource_instance_file: use named returns in splitFileResourceID() Signed-off-by: Simon Deziel --- internal/instance/resource_instance_file.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/instance/resource_instance_file.go b/internal/instance/resource_instance_file.go index 9a8e7866..ef463aaa 100644 --- a/internal/instance/resource_instance_file.go +++ b/internal/instance/resource_instance_file.go @@ -340,7 +340,7 @@ func createFileResourceID(remote string, instanceName string, targetPath string) // splitFileResourceID splits file ID into remote, intanceName, and // targetPath strings. -func splitFileResourceID(id string) (string, string, string) { +func splitFileResourceID(id string) (remote string, instanceName string, targetPath string) { pieces := strings.SplitN(id, ":", 3) return pieces[0], pieces[1], pieces[2] } From 6722c576cd93ec488e9279f4cb820ab4b6da5e5f Mon Sep 17 00:00:00 2001 From: Simon Deziel Date: Tue, 10 Dec 2024 14:43:24 -0500 Subject: [PATCH 19/31] internal/instance/resource_instance_file: don't access slice out of bound in splitFileResourceID() Signed-off-by: Simon Deziel --- internal/instance/resource_instance_file.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/internal/instance/resource_instance_file.go b/internal/instance/resource_instance_file.go index ef463aaa..49447f1c 100644 --- a/internal/instance/resource_instance_file.go +++ b/internal/instance/resource_instance_file.go @@ -342,5 +342,9 @@ func createFileResourceID(remote string, instanceName string, targetPath string) // targetPath strings. func splitFileResourceID(id string) (remote string, instanceName string, targetPath string) { pieces := strings.SplitN(id, ":", 3) + if len(pieces) != 3 { + return "", "", "" + } + return pieces[0], pieces[1], pieces[2] } From 4f16d02229cfc56c0950e4ce68070451cab09fae Mon Sep 17 00:00:00 2001 From: Simon Deziel Date: Tue, 10 Dec 2024 14:43:51 -0500 Subject: [PATCH 20/31] internal/instance/resource_instance_file: no need for fmt.Sprintf() in createFileResourceID() Signed-off-by: Simon Deziel --- internal/instance/resource_instance_file.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/instance/resource_instance_file.go b/internal/instance/resource_instance_file.go index 49447f1c..51f2f45b 100644 --- a/internal/instance/resource_instance_file.go +++ b/internal/instance/resource_instance_file.go @@ -335,7 +335,7 @@ func (r InstanceFileResource) Delete(ctx context.Context, req resource.DeleteReq // createFileResourceID creates new file ID by concatenating remote, // instnaceName, and targetPath using colon. func createFileResourceID(remote string, instanceName string, targetPath string) string { - return fmt.Sprintf("%s:%s:%s", remote, instanceName, targetPath) + return remote + ":" + instanceName + ":" + targetPath } // splitFileResourceID splits file ID into remote, intanceName, and From 44b88696c3caf95a10acea10ee33255c43e20318 Mon Sep 17 00:00:00 2001 From: Simon Deziel Date: Tue, 10 Dec 2024 14:46:14 -0500 Subject: [PATCH 21/31] internal/instance/resource_instance: avoid redefining `new` Signed-off-by: Simon Deziel --- internal/instance/resource_instance.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/internal/instance/resource_instance.go b/internal/instance/resource_instance.go index 25a51255..2f1c76b2 100644 --- a/internal/instance/resource_instance.go +++ b/internal/instance/resource_instance.go @@ -959,16 +959,16 @@ func (r InstanceResource) Update(ctx context.Context, req resource.UpdateRequest // Execute commands. for _, k := range utils.SortMapKeys(newExecs) { - new := newExecs[k] - old := oldExecs[k] + newExec := newExecs[k] + oldExec := oldExecs[k] // Copy run count from state (if exists). - if old != nil { - new.RunCount = old.RunCount + if oldExec != nil { + newExec.RunCount = oldExec.RunCount } - if instanceRunning && new.IsTriggered(instanceStarted) { - diags := new.Execute(ctx, server, instance.Name) + if instanceRunning && newExec.IsTriggered(instanceStarted) { + diags := newExec.Execute(ctx, server, instance.Name) if diags.HasError() { resp.Diagnostics.Append(diags...) return From 0933074a2d839eeb6e79928a98ca25a8c81585d8 Mon Sep 17 00:00:00 2001 From: Simon Deziel Date: Tue, 10 Dec 2024 16:01:36 -0500 Subject: [PATCH 22/31] internal/image/resource_publish_image: check if type cast was successful Signed-off-by: Simon Deziel --- internal/image/resource_publish_image.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/internal/image/resource_publish_image.go b/internal/image/resource_publish_image.go index 20465681..e130efda 100644 --- a/internal/image/resource_publish_image.go +++ b/internal/image/resource_publish_image.go @@ -267,7 +267,12 @@ func (r PublishImageResource) Create(ctx context.Context, req resource.CreateReq // Extract fingerprint from operation response. opResp := op.Get() - imageFingerprint := opResp.Metadata["fingerprint"].(string) + imageFingerprint, ok := opResp.Metadata["fingerprint"].(string) + if !ok { + resp.Diagnostics.AddError("Failed to extract fingerprint from operation response", "") + return + } + plan.Fingerprint = types.StringValue(imageFingerprint) // Update Terraform state. From ee3e21065de150c5cb9407c34ee1e286600f42eb Mon Sep 17 00:00:00 2001 From: Simon Deziel Date: Tue, 10 Dec 2024 16:02:07 -0500 Subject: [PATCH 23/31] internal/provider-config/config: check if type casts were successful Signed-off-by: Simon Deziel --- internal/provider-config/config.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/internal/provider-config/config.go b/internal/provider-config/config.go index b0a3a7c9..2d7d37db 100644 --- a/internal/provider-config/config.go +++ b/internal/provider-config/config.go @@ -224,7 +224,11 @@ func (p *LxdProviderConfig) InstanceServer(remoteName string, project string, ta return nil, fmt.Errorf("Remote %q (%s) is not an InstanceServer", remoteName, connInfo.Protocol) } - instServer := server.(lxd.InstanceServer) + instServer, ok := server.(lxd.InstanceServer) + if !ok { + return nil, fmt.Errorf("Remote %q is not an InstanceServer", remoteName) + } + instServer = instServer.UseProject(project) instServer = instServer.UseTarget(target) @@ -248,7 +252,12 @@ func (p *LxdProviderConfig) ImageServer(remoteName string) (lxd.ImageServer, err return nil, fmt.Errorf("Remote %q (%s / %s) is not an ImageServer", remoteName, connInfo.Protocol, connInfo.Addresses[0]) } - return server.(lxd.ImageServer), nil + imageServer, ok := server.(lxd.ImageServer) + if !ok { + return nil, fmt.Errorf("Remote %q is not an ImageServer", remoteName) + } + + return imageServer, nil } // server returns a server for the named remote. The returned server From 5cb165878754a457bf064562e7921c5b7b44b268 Mon Sep 17 00:00:00 2001 From: Simon Deziel Date: Tue, 10 Dec 2024 16:02:50 -0500 Subject: [PATCH 24/31] internal/provider/provider_test: ignore errors from os.Setenv/Unsetenv Signed-off-by: Simon Deziel --- internal/provider/provider_test.go | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/internal/provider/provider_test.go b/internal/provider/provider_test.go index 7825f9a4..54e2851d 100644 --- a/internal/provider/provider_test.go +++ b/internal/provider/provider_test.go @@ -74,9 +74,9 @@ MDEtMDEtMDFUMDA6MDA6MDBaIgp9Cg==` // as environment variable. PreConfig: func() { configDir = t.TempDir() - os.Setenv("LXD_REMOTE", "tf-remote-token-fqdn") - os.Setenv("LXD_ADDR", "https://127.0.0.1:8443") - os.Setenv("LXD_TOKEN", acctest.ConfigureTrustToken(t)) + _ = os.Setenv("LXD_REMOTE", "tf-remote-token-fqdn") + _ = os.Setenv("LXD_ADDR", "https://127.0.0.1:8443") + _ = os.Setenv("LXD_TOKEN", acctest.ConfigureTrustToken(t)) }, Config: testAccProvider_remoteServerEnv(configDir), Check: resource.ComposeTestCheckFunc( @@ -121,9 +121,9 @@ func TestAccProvider_trustPassword(t *testing.T) { // as environment variable. PreConfig: func() { configDir = t.TempDir() - os.Setenv("LXD_REMOTE", "tf-remote-pass-fqdn") - os.Setenv("LXD_ADDR", "https://127.0.0.1:8443") - os.Setenv("LXD_PASSWORD", acctest.ConfigureTrustPassword(t)) + _ = os.Setenv("LXD_REMOTE", "tf-remote-pass-fqdn") + _ = os.Setenv("LXD_ADDR", "https://127.0.0.1:8443") + _ = os.Setenv("LXD_PASSWORD", acctest.ConfigureTrustPassword(t)) }, Config: testAccProvider_remoteServerEnv(configDir), Check: resource.ComposeTestCheckFunc( @@ -329,8 +329,8 @@ func testCheckClientCert(configDir string, shouldExist bool) resource.TestCheckF // resetLXDRemoteEnvVars unsets all environment variables that are supported by // the provider. func resetLXDRemoteEnvVars() { - os.Unsetenv("LXD_REMOTE") - os.Unsetenv("LXD_ADDR") - os.Unsetenv("LXD_PASSWORD") - os.Unsetenv("LXD_TOKEN") + _ = os.Unsetenv("LXD_REMOTE") + _ = os.Unsetenv("LXD_ADDR") + _ = os.Unsetenv("LXD_PASSWORD") + _ = os.Unsetenv("LXD_TOKEN") } From e9a1bd24d5865623191d5b3fc99b5981b71de1b7 Mon Sep 17 00:00:00 2001 From: Simon Deziel Date: Tue, 10 Dec 2024 16:04:14 -0500 Subject: [PATCH 25/31] internal/image/resource_publish_image: name receiver Signed-off-by: Simon Deziel --- internal/image/resource_publish_image.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/image/resource_publish_image.go b/internal/image/resource_publish_image.go index e130efda..6282f117 100644 --- a/internal/image/resource_publish_image.go +++ b/internal/image/resource_publish_image.go @@ -423,7 +423,7 @@ func (r PublishImageResource) Delete(ctx context.Context, req resource.DeleteReq // SyncState fetches the server's current state for a published image and // updates the provided model. It then applies this updated model as the // new state in Terraform. -func (_ PublishImageResource) SyncState(ctx context.Context, tfState *tfsdk.State, server lxd.InstanceServer, m PublishImageModel) diag.Diagnostics { +func (r PublishImageResource) SyncState(ctx context.Context, tfState *tfsdk.State, server lxd.InstanceServer, m PublishImageModel) diag.Diagnostics { var respDiags diag.Diagnostics imageFingerprint := m.Fingerprint.ValueString() From d6fd8fb6859c0852a16df00e995f65e7cb9628b4 Mon Sep 17 00:00:00 2001 From: Simon Deziel Date: Tue, 10 Dec 2024 16:04:25 -0500 Subject: [PATCH 26/31] internal/instance/resource_instance: name receiver Signed-off-by: Simon Deziel --- internal/instance/resource_instance.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/instance/resource_instance.go b/internal/instance/resource_instance.go index 2f1c76b2..6069b8d2 100644 --- a/internal/instance/resource_instance.go +++ b/internal/instance/resource_instance.go @@ -1195,7 +1195,7 @@ func (r InstanceResource) SyncState(ctx context.Context, tfState *tfsdk.State, s } // ComputedKeys returns list of computed config keys. -func (_ InstanceModel) ComputedKeys() []string { +func (m InstanceModel) ComputedKeys() []string { return []string{ "image.", "volatile.", From e4740b9e293c033a13539ac37eca6be6b994860c Mon Sep 17 00:00:00 2001 From: Simon Deziel Date: Tue, 10 Dec 2024 16:04:34 -0500 Subject: [PATCH 27/31] internal/network/resource_network: name receiver Signed-off-by: Simon Deziel --- internal/network/resource_network.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/network/resource_network.go b/internal/network/resource_network.go index b1ce55ff..65a3cd0b 100644 --- a/internal/network/resource_network.go +++ b/internal/network/resource_network.go @@ -343,7 +343,7 @@ func (r NetworkResource) SyncState(ctx context.Context, tfState *tfsdk.State, se } // ComputedKeys returns list of computed LXD config keys. -func (_ NetworkModel) ComputedKeys() []string { +func (m NetworkModel) ComputedKeys() []string { return []string{ "bridge.mtu", "ipv4.address", From 68119346469ec6fd37d8e2c152251cab7d26b32b Mon Sep 17 00:00:00 2001 From: Simon Deziel Date: Tue, 10 Dec 2024 16:04:45 -0500 Subject: [PATCH 28/31] internal/project/resource_project: name receiver Signed-off-by: Simon Deziel --- internal/project/resource_project.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/project/resource_project.go b/internal/project/resource_project.go index db3c4183..5004ded7 100644 --- a/internal/project/resource_project.go +++ b/internal/project/resource_project.go @@ -295,7 +295,7 @@ func (r ProjectResource) SyncState(ctx context.Context, tfState *tfsdk.State, se } // ComputedKeys returns list of computed config keys. -func (_ ProjectModel) ComputedKeys() []string { +func (m ProjectModel) ComputedKeys() []string { return []string{ "features.images", "features.profiles", From b6f5b7b93755900daa61234dab4e81a0a6ac2816 Mon Sep 17 00:00:00 2001 From: Simon Deziel Date: Tue, 10 Dec 2024 16:05:02 -0500 Subject: [PATCH 29/31] internal/storage/resource_storage_bucket: name receiver Signed-off-by: Simon Deziel --- internal/storage/resource_storage_bucket.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/storage/resource_storage_bucket.go b/internal/storage/resource_storage_bucket.go index 40360834..5b4eb2f2 100644 --- a/internal/storage/resource_storage_bucket.go +++ b/internal/storage/resource_storage_bucket.go @@ -335,7 +335,7 @@ func (r StorageBucketResource) SyncState(ctx context.Context, tfState *tfsdk.Sta } // ComputedKeys returns list of computed config keys. -func (_ StorageBucketModel) ComputedKeys() []string { +func (m StorageBucketModel) ComputedKeys() []string { return []string{ "block.filesystem", "block.mount_options", From 194e210e592b80b17b2092aff1edfe1ab04534f3 Mon Sep 17 00:00:00 2001 From: Simon Deziel Date: Tue, 10 Dec 2024 16:05:09 -0500 Subject: [PATCH 30/31] internal/storage/resource_storage_pool: name receiver Signed-off-by: Simon Deziel --- internal/storage/resource_storage_pool.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/storage/resource_storage_pool.go b/internal/storage/resource_storage_pool.go index ba49e68c..53435784 100644 --- a/internal/storage/resource_storage_pool.go +++ b/internal/storage/resource_storage_pool.go @@ -348,7 +348,7 @@ func (r StoragePoolResource) SyncState(ctx context.Context, tfState *tfsdk.State } // ComputedKeys returns list of computed config keys. -func (_ StoragePoolModel) ComputedKeys(driver string) []string { +func (m StoragePoolModel) ComputedKeys(driver string) []string { var keys []string switch driver { From e121dadfa150e4566faa466d8fd41c053d7d6011 Mon Sep 17 00:00:00 2001 From: Simon Deziel Date: Tue, 10 Dec 2024 16:05:16 -0500 Subject: [PATCH 31/31] internal/storage/resource_storage_volume: name receivers Signed-off-by: Simon Deziel --- internal/storage/resource_storage_volume.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/storage/resource_storage_volume.go b/internal/storage/resource_storage_volume.go index 19bc1979..16f954da 100644 --- a/internal/storage/resource_storage_volume.go +++ b/internal/storage/resource_storage_volume.go @@ -382,7 +382,7 @@ func (r StorageVolumeResource) SyncState(ctx context.Context, tfState *tfsdk.Sta } // ComputedKeys returns list of computed config keys. -func (_ StorageVolumeModel) ComputedKeys() []string { +func (m StorageVolumeModel) ComputedKeys() []string { return []string{ "block.filesystem", "block.mount_options", @@ -390,7 +390,7 @@ func (_ StorageVolumeModel) ComputedKeys() []string { } } -func (_ StorageVolumeModel) InheritedStoragePoolVolumeKeys(server lxd.InstanceServer, poolName string) ([]string, error) { +func (m StorageVolumeModel) InheritedStoragePoolVolumeKeys(server lxd.InstanceServer, poolName string) ([]string, error) { pool, _, err := server.GetStoragePool(poolName) if err != nil { return nil, err