Skip to content

Commit eef2d21

Browse files
authored
port to v2 sdk (#969)
Changes that were required to upgrade the SDK: * DNS hosts is now a set and it is read from libvirt into the state * DCHP enablement is now computed, and uses a workaround to be explicitly set or disabled by the user while being enabled by default, unfortunately, using `GetOkExists` for now * cloudinit disks are read back, and a workaround (custom serial) is used to mark them as owned by the cloudinit setting. This workaround needs to be improved/cleaned up. * Ignition provider uses the old v1 SDK and [my contribution to update it to v2](community-terraform-providers/terraform-provider-ignition#15) has not been merged, therefore our tests refer to [my upgraded fork](https://github.com/dmacvicar/terraform-provider-ignition). * Some settings are now read back from libvirt, like firmware, nvram, cpu mode, etc. Future work could be to port the Create/Read/etc functions to their new equivalents, which have better support for Context and Diagnostics.
1 parent 68c9bb0 commit eef2d21

37 files changed

+792
-352
lines changed

.github/workflows/test.yml

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,14 @@ jobs:
1717
lint:
1818
name: Lint
1919
runs-on: ubuntu-latest
20+
strategy:
21+
matrix:
22+
go-version: ["1.18.x"]
2023
steps:
21-
- uses: actions/checkout@v2
24+
- uses: actions/setup-go@v3
25+
with:
26+
go-version: ${{ matrix.go-version }}
27+
- uses: actions/checkout@v3
2228
with:
2329
fetch-depth: 2
2430
- name: terraform fmt
@@ -28,20 +34,21 @@ jobs:
2834
uses: golangci/golangci-lint-action@v3
2935
with:
3036
version: v1.48.0
37+
go-version: ${{ matrix.go-version }}
3138
only-new-issues: true
3239
build:
3340
name: Build
3441
runs-on: ${{ matrix.os }}
3542
strategy:
3643
matrix:
3744
include:
38-
- {os: ubuntu-latest, go: 1.16}
39-
- {os: windows-latest, go: 1.16}
40-
- {os: macos-latest, go: 1.16}
45+
- {os: ubuntu-latest, go: 1.18}
46+
- {os: windows-latest, go: 1.18}
47+
- {os: macos-latest, go: 1.18}
4148
timeout-minutes: 10
4249
steps:
4350
- name: Set up Go
44-
uses: actions/setup-go@v2
51+
uses: actions/setup-go@v3
4552
with:
4653
go-version: ${{ matrix.go }}
4754
id: go

.golangci.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,14 @@ linters:
2424
- errorlint
2525
- errcheck
2626
- gomnd
27+
2728
issues:
2829
new-from-rev: HEAD~
30+
exclude-rules:
31+
- text: "Error return value of `d.Set` is not checked"
32+
linters:
33+
- errcheck
34+
- text: "Magic number: 1024, in <argument> detected"
35+
linters:
36+
- gomnd
37+

go.mod

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,19 @@ module github.com/dmacvicar/terraform-provider-libvirt
22

33
require (
44
github.com/c4milo/gotoolkit v0.0.0-20170704181456-e37eeabad07e // indirect
5-
github.com/coreos/go-semver v0.3.0 // indirect
5+
github.com/community-terraform-providers/terraform-provider-ignition/v2 v2.1.2
66
github.com/davecgh/go-spew v1.1.1
77
github.com/digitalocean/go-libvirt v0.0.0-20220616141158-7ed4ed4decd9
8-
github.com/fatih/color v1.10.0 // indirect
98
github.com/google/uuid v1.1.2
10-
github.com/hashicorp/go-getter v1.6.2 // indirect
11-
github.com/hashicorp/terraform-plugin-sdk v1.9.0
9+
github.com/hashicorp/terraform-plugin-sdk/v2 v2.21.0
1210
github.com/hooklift/assert v0.0.0-20170704181755-9d1defd6d214 // indirect
1311
github.com/hooklift/iso9660 v1.0.0
1412
github.com/mattn/goveralls v0.0.2
1513
github.com/pborman/uuid v1.2.0 // indirect
16-
github.com/stretchr/testify v1.7.0
17-
github.com/terraform-providers/terraform-provider-ignition v1.2.1
18-
github.com/ulikunitz/xz v0.5.8 // indirect
19-
golang.org/x/crypto v0.0.0-20220112180741-5e0467b6c7ce
14+
github.com/stretchr/testify v1.7.2
15+
golang.org/x/crypto v0.0.0-20220517005047-85d78b3ac167
2016
golang.org/x/lint v0.0.0-20200302205851-738671d3881b
17+
golang.org/x/sys v0.0.0-20220517195934-5e4e11fc645e // indirect
2118
gopkg.in/yaml.v2 v2.4.0 // indirect
2219
libvirt.org/go/libvirtxml v1.8003.0
2320
)
@@ -27,3 +24,5 @@ replace git.apache.org/thrift.git => github.com/apache/thrift v0.0.0-20180902110
2724
replace golang.org/x/crypto v0.0.0-20220112180741-5e0467b6c7ce => github.com/dmacvicar/golang-x-crypto v0.0.0-20220126233154-a96af8f07497
2825

2926
go 1.13
27+
28+
replace github.com/community-terraform-providers/terraform-provider-ignition/v2 => github.com/dmacvicar/terraform-provider-ignition/v2 v2.1.3-0.20210701165004-13acf61ca184

go.sum

Lines changed: 423 additions & 132 deletions
Large diffs are not rendered by default.

libvirt/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77

88
libvirt "github.com/digitalocean/go-libvirt"
99
uri "github.com/dmacvicar/terraform-provider-libvirt/libvirt/uri"
10-
"github.com/hashicorp/terraform-plugin-sdk/helper/mutexkv"
10+
"github.com/dmacvicar/terraform-provider-libvirt/libvirt/helper/mutexkv"
1111
)
1212

1313
// Config struct for the libvirt-provider

libvirt/data_source_libvirt_network.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import (
55
"net"
66
"strconv"
77

8-
"github.com/hashicorp/terraform-plugin-sdk/helper/hashcode"
9-
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
8+
"github.com/dmacvicar/terraform-provider-libvirt/libvirt/helper/hashcode"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1010
)
1111

1212
// a libvirt network DNS host template datasource

libvirt/data_source_libvirt_network_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ import (
44
"fmt"
55
"testing"
66

7-
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
8-
"github.com/hashicorp/terraform-plugin-sdk/terraform"
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
99
)
1010

1111
func TestAccLibvirtNetworkDataSource_DNSHostTemplate(t *testing.T) {

libvirt/domain.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ import (
1313

1414
"github.com/davecgh/go-spew/spew"
1515
libvirt "github.com/digitalocean/go-libvirt"
16-
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
17-
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
16+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
17+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1818
"libvirt.org/go/libvirtxml"
1919
)
2020

@@ -187,6 +187,9 @@ func domainGetIfacesInfo(virConn *libvirt.Libvirt, domain libvirt.Domain, rd *sc
187187

188188
func newDiskForCloudInit(virConn *libvirt.Libvirt, volumeKey string) (libvirtxml.DomainDisk, error) {
189189
disk := libvirtxml.DomainDisk{
190+
// HACK mark the disk as belonging to the cloudinit
191+
// resource so we can ignore it
192+
Serial: "cloudinit",
190193
Device: "cdrom",
191194
Target: &libvirtxml.DomainDiskTarget{
192195
// Last device letter possible with a single IDE controller on i440FX

libvirt/domain_def.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"os"
77

88
libvirt "github.com/digitalocean/go-libvirt"
9-
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1010
"libvirt.org/go/libvirtxml"
1111
)
1212

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package hashcode
2+
3+
import (
4+
"hash/crc32"
5+
)
6+
7+
func String(s string) int {
8+
v := int(crc32.ChecksumIEEE([]byte(s)))
9+
if v >= 0 {
10+
return v
11+
}
12+
if -v >= 0 {
13+
return -v
14+
}
15+
// v == MinInt
16+
return 0
17+
}

0 commit comments

Comments
 (0)