Skip to content

Commit

Permalink
Merge pull request #30 from cobbler/feature/upgrade-tf-sdk-v2
Browse files Browse the repository at this point in the history
Update Terraform SDK to v2
  • Loading branch information
SchoolGuy authored Oct 8, 2024
2 parents 02a9181 + 9c06423 commit eec6581
Show file tree
Hide file tree
Showing 24 changed files with 562 additions and 955 deletions.
85 changes: 44 additions & 41 deletions .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,55 +8,58 @@ on:
- 'v*'
pull_request:

permissions:
contents: read

jobs:

build:
name: Build
runs-on: ubuntu-latest
timeout-minutes: 5
steps:

- name: Set up Go
uses: actions/[email protected]
with:
go-version: '1.17'
id: go

- name: Check out code into the Go module directory
uses: actions/[email protected]

- name: Get dependencies
run: |
go mod download
sudo apt-get install -y xorriso
- name: Build
run: |
go build -v .
- name: Check out code into the Go module directory
uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version-file: 'go.mod'
id: go
- name: Get dependencies
run: |
go mod download
sudo apt-get install -y xorriso
- name: Build
run: |
go build -v .
unit:
name: Unit Tests
needs: build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v4
with:
go-version-file: 'go.mod'
- run: go test -v -cover ./...
test:
name: Testing
name: Integration Tests
needs: build
runs-on: ubuntu-latest
steps:

- name: Set up Go
uses: actions/[email protected]
with:
go-version: '1.17'
id: go

- name: Check out code into the Go module directory
uses: actions/[email protected]

- name: Get dependencies
run: |
go mod download
- name: Make Test
run: |
make testacc
- name: Make Vet
run: |
make vet
- name: Check out code into the Go module directory
uses: actions/checkout@v4
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version-file: 'go.mod'
id: go
- name: Get dependencies
run: |
go mod download
- name: Make Test
run: |
make testacc
- name: Make Vet
run: |
make vet
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,6 @@ terraform-provider-cobbler
debug
dist/
vendor/
docker/cobbler_source/
extracted_iso_image/
*.iso
39 changes: 16 additions & 23 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,34 +1,27 @@
run:
deadline: 5m
# Visit https://golangci-lint.run/ for usage documentation
# and information on other useful linters
issues:
max-per-linter: 0
max-same-issues: 0

linters:
fast: false
disable-all: true
enable:
- asciicheck
- bodyclose
- deadcode
- depguard
- dogsled
- exhaustive
- durationcheck
- errcheck
- exportloopref
- forcetypeassert
- godot
- gofmt
- goheader
- goimports
- golint
- govet
- gosimple
- ineffassign
- makezero
- misspell
- nakedret
- nolintlint
- rowserrcheck
- nilerr
- predeclared
- staticcheck
- structcheck
- typecheck
- tenv
- unconvert
- unparam
- unused
- varcheck
- whitespace

service:
golangci-lint-version: 1.32.x # use the fixed version to not introduce new linters unexpectedly
- govet
2 changes: 1 addition & 1 deletion GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ test: fmtcheck
.PHONY: testacc
testacc:
@COBBLER_VERSION=v3.3.0 sh -c "'./docker/start.sh' $(COBBLER_SERVER_URL)"
TF_ACC=1 COBBLER_URL=$(COBBLER_SERVER_URL) COBBLER_USERNAME=cobbler COBBLER_PASSWORD=cobbler go test -v -cover $(TEST)
TF_LOG=TRACE TF_ACC_LOG=TRACE TF_LOG_PATH_MASK="test-%s.log" TF_ACC=1 COBBLER_URL=$(COBBLER_SERVER_URL) COBBLER_USERNAME=cobbler COBBLER_PASSWORD=cobbler go test -v -cover $(TEST)

vet:
@echo "go vet ."
Expand Down
4 changes: 1 addition & 3 deletions cobbler/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"fmt"
"net/http"

"github.com/hashicorp/terraform-plugin-sdk/helper/pathorcontents"

cobbler "github.com/cobbler/cobblerclient"
)

Expand All @@ -31,7 +29,7 @@ func (c *Config) loadAndValidate() error {

tlsConfig := &tls.Config{}
if c.CACertFile != "" {
caCert, _, err := pathorcontents.Read(c.CACertFile)
caCert, _, err := Read(c.CACertFile)
if err != nil {
return fmt.Errorf("Error reading CA Cert: %s", err)
}
Expand Down
120 changes: 62 additions & 58 deletions cobbler/provider.go
Original file line number Diff line number Diff line change
@@ -1,75 +1,79 @@
package cobbler

import (
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
"context"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

// Provider does the talking to the Cobbler API.
func Provider() terraform.ResourceProvider {
return &schema.Provider{
Schema: map[string]*schema.Schema{
"url": {
Description: "The url to the Cobbler service. This can also be specified with the `COBBLER_URL` shell environment variable.",
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("COBBLER_URL", nil),
},
func New(version string) func() *schema.Provider {
return func() *schema.Provider {
p := &schema.Provider{
Schema: map[string]*schema.Schema{
"url": {
Description: "The url to the Cobbler service. This can also be specified with the `COBBLER_URL` shell environment variable.",
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("COBBLER_URL", nil),
},

"username": {
Description: "The username to the Cobbler service. This can also be specified with the `COBBLER_USERNAME` shell environment variable.",
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("COBBLER_USERNAME", nil),
},
"username": {
Description: "The username to the Cobbler service. This can also be specified with the `COBBLER_USERNAME` shell environment variable.",
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("COBBLER_USERNAME", nil),
},

"password": {
Description: "The password to the Cobbler service. This can also be specified with the `COBBLER_PASSWORD` shell environment variable.",
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("COBBLER_PASSWORD", nil),
},
"password": {
Description: "The password to the Cobbler service. This can also be specified with the `COBBLER_PASSWORD` shell environment variable.",
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("COBBLER_PASSWORD", nil),
},

"insecure": {
Description: "The url to the Cobbler service. This can also be specified with the `COBBLER_URL` shell environment variable.",
Type: schema.TypeBool,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("COBBLER_INSECURE", nil),
},
"insecure": {
Description: "The url to the Cobbler service. This can also be specified with the `COBBLER_URL` shell environment variable.",
Type: schema.TypeBool,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("COBBLER_INSECURE", nil),
},

"cacert_file": {
Description: "The path or contents of an SSL CA certificate. This can also be specified with the `COBBLER_CACERT_FILE`shell environment variable.",
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("COBBLER_CACERT_FILE", nil),
"cacert_file": {
Description: "The path or contents of an SSL CA certificate. This can also be specified with the `COBBLER_CACERT_FILE`shell environment variable.",
Type: schema.TypeString,
Optional: true,
DefaultFunc: schema.EnvDefaultFunc("COBBLER_CACERT_FILE", nil),
},
},
},

ResourcesMap: map[string]*schema.Resource{
"cobbler_distro": resourceDistro(),
"cobbler_template_file": resourceTemplateFile(),
"cobbler_profile": resourceProfile(),
"cobbler_repo": resourceRepo(),
"cobbler_snippet": resourceSnippet(),
"cobbler_system": resourceSystem(),
},
ResourcesMap: map[string]*schema.Resource{
"cobbler_distro": resourceDistro(),
"cobbler_template_file": resourceTemplateFile(),
"cobbler_profile": resourceProfile(),
"cobbler_repo": resourceRepo(),
"cobbler_snippet": resourceSnippet(),
"cobbler_system": resourceSystem(),
},
}
p.ConfigureContextFunc = configure(version, p)

ConfigureFunc: configureProvider,
return p
}
}

func configureProvider(d *schema.ResourceData) (interface{}, error) {
config := Config{
CACertFile: d.Get("cacert_file").(string),
Insecure: d.Get("insecure").(bool),
URL: d.Get("url").(string),
Username: d.Get("username").(string),
Password: d.Get("password").(string),
}
func configure(version string, p *schema.Provider) func(context.Context, *schema.ResourceData) (interface{}, diag.Diagnostics) {
return func(ctx context.Context, d *schema.ResourceData) (interface{}, diag.Diagnostics) {
config := Config{
CACertFile: d.Get("cacert_file").(string),
Insecure: d.Get("insecure").(bool),
URL: d.Get("url").(string),
Username: d.Get("username").(string),
Password: d.Get("password").(string),
}

if err := config.loadAndValidate(); err != nil {
return nil, err
}
if err := config.loadAndValidate(); err != nil {
return nil, diag.FromErr(err)
}

return &config, nil
return &config, nil
}
}
32 changes: 20 additions & 12 deletions cobbler/provider_test.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,41 @@
package cobbler

import (
cobbler "github.com/cobbler/cobblerclient"
"net/http"
"os"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

var testAccCobblerProviders map[string]terraform.ResourceProvider
var testAccCobblerProvider *schema.Provider
var cobblerApiClient cobbler.Client
var testAccProviderFactories = map[string]func() (*schema.Provider, error){
"cobbler": func() (*schema.Provider, error) {
return New("dev")(), nil
},
}

func init() {
testAccCobblerProvider = Provider().(*schema.Provider)
testAccCobblerProviders = map[string]terraform.ResourceProvider{
"cobbler": testAccCobblerProvider,
cobblerApiClient = cobbler.NewClient(&http.Client{}, cobbler.ClientConfig{
URL: os.Getenv("COBBLER_URL"),
Username: os.Getenv("COBBLER_USERNAME"),
Password: os.Getenv("COBBLER_PASSWORD"),
})
_, _ = cobblerApiClient.Login()
testAccProviderFactories = map[string]func() (*schema.Provider, error){
"cobbler": func() (*schema.Provider, error) {
return New("dev")(), nil
},
}
}

func TestProvider(t *testing.T) {
if err := Provider().(*schema.Provider).InternalValidate(); err != nil {
if err := New("dev")().InternalValidate(); err != nil {
t.Fatalf("err: %s", err)
}
}

func TestProvider_impl(t *testing.T) {
var _ terraform.ResourceProvider = Provider()
}

func testAccCobblerPreCheck(t *testing.T) {
v := os.Getenv("COBBLER_USERNAME")
if v == "" {
Expand Down
2 changes: 1 addition & 1 deletion cobbler/resource_cobbler_distro.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"log"

cobbler "github.com/cobbler/cobblerclient"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func resourceDistro() *schema.Resource {
Expand Down
Loading

0 comments on commit eec6581

Please sign in to comment.