0
0
mirror of https://github.com/bpg/terraform-provider-proxmox.git synced 2025-08-23 03:48:35 +00:00

feat(core): migrate version datasource to TF plugin framework (#440)

* feat: migrate `version` datasource to tf framework

* set up documentation generator

* add generated docs for network resources

* fix test mux server, add import examples to network resources

* fix network tests

* fix shell examples
This commit is contained in:
Pavel Boldyrev 2023-07-28 22:10:02 -04:00 committed by GitHub
parent 8e2d18053f
commit a9a7329d9f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
31 changed files with 513 additions and 275 deletions

1
.gitignore vendored
View File

@ -12,6 +12,7 @@ bin/
cache/ cache/
dist/ dist/
modules-dev/ modules-dev/
docs-gen/
.terraform/ .terraform/
.vagrant/ .vagrant/

View File

@ -23,6 +23,7 @@ clean:
rm -rf ./dist rm -rf ./dist
rm -rf ./cache rm -rf ./cache
rm -rf ./build rm -rf ./build
rm -rf ./docs-gen
build: build:
mkdir -p "$(TERRAFORM_PLUGIN_OUTPUT_DIRECTORY)" mkdir -p "$(TERRAFORM_PLUGIN_OUTPUT_DIRECTORY)"
@ -80,6 +81,10 @@ lint:
release-build: release-build:
go run -modfile=tools/go.mod github.com/goreleaser/goreleaser build --clean --skip-validate go run -modfile=tools/go.mod github.com/goreleaser/goreleaser build --clean --skip-validate
docs:
@mkdir -p ./docs-gen
@cd ./tools && go generate tools.go
targets: $(TARGETS) targets: $(TARGETS)
$(TARGETS): $(TARGETS):
@ -90,4 +95,4 @@ $(TARGETS):
-j "dist/$(NAME)_v$(VERSION)-custom_$@_amd64.zip" \ -j "dist/$(NAME)_v$(VERSION)-custom_$@_amd64.zip" \
"dist/$@/$(NAME)_v$(VERSION)-custom" "dist/$@/$(NAME)_v$(VERSION)-custom"
.PHONY: clean build example example-apply example-destroy example-init example-plan fmt init targets test $(TARGETS) .PHONY: clean build example example-apply example-destroy example-init example-plan fmt init targets test docs $(TARGETS)

View File

@ -1,29 +1,36 @@
--- ---
layout: page layout: page
title: proxmox_virtual_environment_version title: proxmox_virtual_environment_version
permalink: /data-sources/virtual_environment_version
nav_order: 20
parent: Data Sources parent: Data Sources
subcategory: Virtual Environment subcategory: Virtual Environment
description: |-
Retrieves API version details.
--- ---
# Data Source: proxmox_virtual_environment_version # Data Source: proxmox_virtual_environment_version
Retrieves the version information from the API endpoint. Retrieves API version details.
## Example Usage ## Example Usage
```terraform ```terraform
data "proxmox_virtual_environment_version" "current_version" {} data "proxmox_virtual_environment_version" "example" {}
output "data_proxmox_virtual_environment_version" {
value = {
release = data.proxmox_virtual_environment_version.example.release
repository_id = data.proxmox_virtual_environment_version.example.repository_id
version = data.proxmox_virtual_environment_version.example.version
}
}
``` ```
## Argument Reference <!-- schema generated by tfplugindocs -->
## Schema
There are no arguments available for this data source. ### Read-Only
## Attribute Reference - `id` (String) Placeholder identifier attribute.
- `release` (String) The current Proxmox VE point release in `x.y` format.
- `keyboard_layout` - The keyboard layout. - `repository_id` (String) The short git revision from which this version was build.
- `release` - The release number. - `version` (String) The full pve-manager package version of this node.
- `repository_id` - The repository identifier.
- `version` - The version string.

View File

@ -1,10 +1,10 @@
--- ---
layout: page layout: page
title: proxmox_virtual_environment_network_linux_bridge title: proxmox_virtual_environment_network_linux_bridge
permalink: /resources/virtual_environment_network_linux_bridge
nav_order: 13
parent: Resources parent: Resources
subcategory: Virtual Environment subcategory: Virtual Environment
description: |-
Manages a Linux Bridge network interface in a Proxmox VE node.
--- ---
# Resource: proxmox_virtual_environment_network_linux_bridge # Resource: proxmox_virtual_environment_network_linux_bridge
@ -14,33 +14,54 @@ Manages a Linux Bridge network interface in a Proxmox VE node.
## Example Usage ## Example Usage
```terraform ```terraform
resource "proxmox_virtual_environment_network_linux_bridge" "bridge99" { resource "proxmox_virtual_environment_network_linux_bridge" "vmbr99" {
depends_on = [
proxmox_virtual_environment_network_linux_vlan.vlan99
]
node_name = "pve" node_name = "pve"
iface = "vmbr99" name = "vmbr99"
address = "3.3.3.3/24"
comment = "created by terraform" address = "99.99.99.99/16"
mtu = 1499
comment = "vmbr99 comment"
ports = [
"ens18.99"
]
} }
``` ```
## Argument Reference <!-- schema generated by tfplugindocs -->
## Schema
- `node_name` - (Required) The name of the node to manage the interface on. ### Required
- `name` - (Required) The interface name. Must be "vmbrN", where N is a number
between 0 and 9999.
- `address` - (Optional) The interface IPv4/CIDR address. - `name` (String) The interface name. Must be `vmbrN`, where N is a number between 0 and 9999.
- `address6` - (Optional) The interface IPv6/CIDR address. - `node_name` (String) The name of the node.
- `autostart` - (Optional) Automatically start interface on boot (defaults
to `true`). ### Optional
- `ports` - (Optional) Specify the list of the interface bridge ports.
- `vlan_aware` - (Optional) Whether the interface bridge is VLAN aware (defaults - `address` (String) The interface IPv4/CIDR address.
to `true`). - `address6` (String) The interface IPv6/CIDR address.
- `comment` - (Optional) Comment for the interface. - `autostart` (Boolean) Automatically start interface on boot (defaults to `true`).
- `gateway` - (Optional) Default gateway address. - `comment` (String) Comment for the interface.
- `gateway6` - (Optional) Default IPv6 gateway address. - `gateway` (String) Default gateway address.
- `mtu` - (Optional) The interface MTU. - `gateway6` (String) Default IPv6 gateway address.
- `mtu` (Number) The interface MTU.
- `ports` (List of String) The interface bridge ports.
- `vlan_aware` (Boolean) Whether the interface bridge is VLAN aware (defaults to `true`).
### Read-Only ### Read-Only
- `id` (String) A unique identifier with format '<node name>:<iface>' - `id` (String) A unique identifier with format '<node name>:<iface>'
## Import
Import is supported using the following syntax:
```shell
# Interfaces can be imported using the `node_name:iface` format, e.g.,
terraform import proxmox_virtual_environment_network_linux_bridge.vmbr99 pve:vmbr99
```

View File

@ -1,10 +1,10 @@
--- ---
layout: page layout: page
title: proxmox_virtual_environment_network_linux_vlan title: proxmox_virtual_environment_network_linux_vlan
permalink: /resources/virtual_environment_network_linux_vlan
nav_order: 13
parent: Resources parent: Resources
subcategory: Virtual Environment subcategory: Virtual Environment
description: |-
Manages a Linux VLAN network interface in a Proxmox VE node.
--- ---
# Resource: proxmox_virtual_environment_network_linux_vlan # Resource: proxmox_virtual_environment_network_linux_vlan
@ -14,30 +14,44 @@ Manages a Linux VLAN network interface in a Proxmox VE node.
## Example Usage ## Example Usage
```terraform ```terraform
resource "proxmox_virtual_environment_network_linux_vlan" "vlan21" { resource "proxmox_virtual_environment_network_linux_vlan" "vlan99" {
node_name = "pve" node_name = "pve"
iface = "ens18.21" name = "eno0.99"
comment = "created by terraform"
comment = "VLAN 99"
} }
``` ```
## Argument Reference <!-- schema generated by tfplugindocs -->
## Schema
- `node_name` - (Required) The name of the node to manage the interface on. ### Required
- `name` - (Required) The interface name. Add the VLAN tag number to an
existing interface name, e.g. "ens18.21".
- `address` - (Optional) The interface IPv4/CIDR address. - `name` (String) The interface name. Add the VLAN tag number to an existing interface name, e.g. `ens18.21`
- `address6` - (Optional) The interface IPv6/CIDR address. - `node_name` (String) The name of the node.
- `autostart` - (Optional) Automatically start interface on boot (defaults
to `true`). ### Optional
- `comment` - (Optional) Comment for the interface.
- `gateway` - (Optional) Default gateway address. - `address` (String) The interface IPv4/CIDR address.
- `gateway6` - (Optional) Default IPv6 gateway address. - `address6` (String) The interface IPv6/CIDR address.
- `mtu` - (Optional) The interface MTU. - `autostart` (Boolean) Automatically start interface on boot (defaults to `true`).
- `comment` (String) Comment for the interface.
- `gateway` (String) Default gateway address.
- `gateway6` (String) Default IPv6 gateway address.
- `mtu` (Number) The interface MTU.
### Read-Only ### Read-Only
- `id` (String) A unique identifier with format '<node name>:<iface>' - `id` (String) A unique identifier with format '<node name>:<iface>'
- `interface` (String) The VLAN raw device. - `interface` (String) The VLAN raw device.
- `vlan` (Number) The VLAN tag - `vlan` (Number) The VLAN tag
## Import
Import is supported using the following syntax:
```shell
# Interfaces can be imported using the `node_name:iface` format, e.g.,
terraform import proxmox_virtual_environment_network_linux_vlan.vlan99 pve:vlan99
```

View File

@ -1,10 +0,0 @@
data "proxmox_virtual_environment_version" "example" {}
output "data_proxmox_virtual_environment_version" {
value = {
keyboard_layout = data.proxmox_virtual_environment_version.example.keyboard_layout
release = data.proxmox_virtual_environment_version.example.release
repository_id = data.proxmox_virtual_environment_version.example.repository_id
version = data.proxmox_virtual_environment_version.example.version
}
}

6
examples/README.md Normal file
View File

@ -0,0 +1,6 @@
`/examples` contain a set of examples that can be used to test the provider as
well as documentation examples. The examples are not meant to be used in
production.
NOTE: during migration to TF plugin framework, examples of migrated resources /
data sources will be moving from `/example` to `/examples` folder

View File

@ -0,0 +1,9 @@
data "proxmox_virtual_environment_version" "example" {}
output "data_proxmox_virtual_environment_version" {
value = {
release = data.proxmox_virtual_environment_version.example.release
repository_id = data.proxmox_virtual_environment_version.example.repository_id
version = data.proxmox_virtual_environment_version.example.version
}
}

View File

@ -0,0 +1,17 @@
terraform {
required_providers {
datadog = {
source = "bpg/proxmox"
}
}
}
provider "proxmox" {
endpoint = var.virtual_environment_endpoint
username = var.virtual_environment_username
password = var.virtual_environment_password
insecure = true
ssh {
agent = true
}
}

View File

@ -0,0 +1,14 @@
variable "virtual_environment_endpoint" {
type = string
description = "The endpoint for the Proxmox Virtual Environment API (example: https://host:port)"
}
variable "virtual_environment_password" {
type = string
description = "The password for the Proxmox Virtual Environment API"
}
variable "virtual_environment_username" {
type = string
description = "The username and realm for the Proxmox Virtual Environment API (example: root@pam)"
}

View File

@ -0,0 +1,3 @@
#!/usr/bin/env sh
#Interfaces can be imported using the `node_name:iface` format, e.g.
terraform import proxmox_virtual_environment_network_linux_bridge.vmbr99 pve:vmbr99

View File

@ -1,10 +1,3 @@
resource "proxmox_virtual_environment_network_linux_vlan" "vlan99" {
node_name = "pve"
name = "eno0.99"
comment = "VLAN 99"
}
resource "proxmox_virtual_environment_network_linux_bridge" "vmbr99" { resource "proxmox_virtual_environment_network_linux_bridge" "vmbr99" {
depends_on = [ depends_on = [
proxmox_virtual_environment_network_linux_vlan.vlan99 proxmox_virtual_environment_network_linux_vlan.vlan99

View File

@ -0,0 +1,3 @@
#!/usr/bin/env sh
#Interfaces can be imported using the `node_name:iface` format, e.g.
terraform import proxmox_virtual_environment_network_linux_vlan.vlan99 pve:vlan99

View File

@ -0,0 +1,6 @@
resource "proxmox_virtual_environment_network_linux_vlan" "vlan99" {
node_name = "pve"
name = "eno0.99"
comment = "VLAN 99"
}

View File

@ -175,8 +175,9 @@ func (r *linuxBridgeResource) Schema(
Required: true, Required: true,
}, },
"name": schema.StringAttribute{ "name": schema.StringAttribute{
Description: "The interface name.", Description: "The interface name.",
Required: true, MarkdownDescription: "The interface name. Must be `vmbrN`, where N is a number between 0 and 9999.",
Required: true,
Validators: []validator.String{ Validators: []validator.String{
stringvalidator.RegexMatches( stringvalidator.RegexMatches(
regexp.MustCompile(`^vmbr(\d{1,4})$`), regexp.MustCompile(`^vmbr(\d{1,4})$`),
@ -208,7 +209,7 @@ func (r *linuxBridgeResource) Schema(
Optional: true, Optional: true,
}, },
"autostart": schema.BoolAttribute{ "autostart": schema.BoolAttribute{
Description: "Automatically start interface on boot.", Description: "Automatically start interface on boot (defaults to `true`).",
Optional: true, Optional: true,
Computed: true, Computed: true,
Default: booldefault.StaticBool(true), Default: booldefault.StaticBool(true),
@ -229,7 +230,7 @@ func (r *linuxBridgeResource) Schema(
ElementType: types.StringType, ElementType: types.StringType,
}, },
"vlan_aware": schema.BoolAttribute{ "vlan_aware": schema.BoolAttribute{
Description: "Whether the interface bridge is VLAN aware.", Description: "Whether the interface bridge is VLAN aware (defaults to `true`).",
Optional: true, Optional: true,
Computed: true, Computed: true,
}, },
@ -463,7 +464,7 @@ func (r *linuxBridgeResource) ImportState(
if len(idParts) != 2 || idParts[0] == "" || idParts[1] == "" { if len(idParts) != 2 || idParts[0] == "" || idParts[1] == "" {
resp.Diagnostics.AddError( resp.Diagnostics.AddError(
"Unexpected Import Identifier", "Unexpected Import Identifier",
fmt.Sprintf("Expected import identifier with format: node_name:iface. Got: %q", req.ID), fmt.Sprintf("Expected import identifier with format: `node_name:iface`. Got: %q", req.ID),
) )
return return

View File

@ -155,8 +155,9 @@ func (r *linuxVLANResource) Schema(
Required: true, Required: true,
}, },
"name": schema.StringAttribute{ "name": schema.StringAttribute{
Description: "The interface name.", Description: "The interface name.",
Required: true, MarkdownDescription: "The interface name. Add the VLAN tag number to an existing interface name, e.g. `ens18.21`",
Required: true,
Validators: []validator.String{ Validators: []validator.String{
stringvalidator.LengthAtLeast(3), stringvalidator.LengthAtLeast(3),
}, },
@ -185,7 +186,7 @@ func (r *linuxVLANResource) Schema(
Optional: true, Optional: true,
}, },
"autostart": schema.BoolAttribute{ "autostart": schema.BoolAttribute{
Description: "Automatically start interface on boot.", Description: "Automatically start interface on boot (defaults to `true`).",
Optional: true, Optional: true,
Computed: true, Computed: true,
Default: booldefault.StaticBool(true), Default: booldefault.StaticBool(true),

View File

@ -0,0 +1,126 @@
/*
* 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 provider
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/bpg/terraform-provider-proxmox/proxmox"
)
// Ensure the implementation satisfies the expected interfaces.
var (
_ datasource.DataSource = &versionDatasource{}
_ datasource.DataSourceWithConfigure = &versionDatasource{}
)
// NewVersionDataSource is a helper function to simplify the provider implementation.
func NewVersionDataSource() datasource.DataSource {
return &versionDatasource{}
}
// versionDatasource is the data source implementation.
type versionDatasource struct {
client proxmox.Client
}
// versionDataSourceModel maps the data source schema data.
type versionDataSourceModel struct {
Release types.String `tfsdk:"release"`
RepositoryID types.String `tfsdk:"repository_id"`
Version types.String `tfsdk:"version"`
ID types.String `tfsdk:"id"`
}
// Metadata returns the data source type name.
func (d *versionDatasource) Metadata(
_ context.Context,
req datasource.MetadataRequest,
resp *datasource.MetadataResponse,
) {
resp.TypeName = req.ProviderTypeName + "_version"
}
// Schema defines the schema for the data source.
func (d *versionDatasource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "Retrieves API version details.",
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Description: "Placeholder identifier attribute.",
Computed: true,
},
"release": schema.StringAttribute{
Description: "The current Proxmox VE point release in `x.y` format.",
Computed: true,
},
"repository_id": schema.StringAttribute{
Description: "The short git revision from which this version was build.",
Computed: true,
},
"version": schema.StringAttribute{
Description: "The full pve-manager package version of this node.",
Computed: true,
},
},
}
}
// Configure adds the provider configured client to the data source.
func (d *versionDatasource) Configure(
_ context.Context,
req datasource.ConfigureRequest,
resp *datasource.ConfigureResponse,
) {
if req.ProviderData == nil {
return
}
client, ok := req.ProviderData.(proxmox.Client)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Resource Configure Type",
fmt.Sprintf("Expected *proxmox.Client, got: %T. Please report this issue to the provider developers.",
req.ProviderData),
)
return
}
d.client = client
}
// Read refreshes the Terraform state with the latest data.
func (d *versionDatasource) Read(ctx context.Context, _ datasource.ReadRequest, resp *datasource.ReadResponse) {
var state versionDataSourceModel
version, err := d.client.Version().Version(ctx)
if err != nil {
resp.Diagnostics.AddError(
"Unable to Read Version",
err.Error(),
)
return
}
state.Release = types.StringValue(version.Release)
state.RepositoryID = types.StringValue(version.RepositoryID)
state.Version = types.StringValue(version.Version)
state.ID = types.StringValue("version")
// Set state
diags := resp.State.Set(ctx, &state)
resp.Diagnostics.Append(diags...)
}

View File

@ -354,7 +354,9 @@ func (p *proxmoxProvider) Resources(_ context.Context) []func() resource.Resourc
} }
func (p *proxmoxProvider) DataSources(_ context.Context) []func() datasource.DataSource { func (p *proxmoxProvider) DataSources(_ context.Context) []func() datasource.DataSource {
return []func() datasource.DataSource{} return []func() datasource.DataSource{
NewVersionDataSource,
}
} }
type apiResolver struct { type apiResolver struct {

View File

@ -0,0 +1,45 @@
/*
* 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 test
import (
"context"
"fmt"
"strings"
"testing"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)
func Test_VersionDatasource(t *testing.T) {
t.Parallel()
accProviders := AccMuxProviders(context.Background(), t)
datasourceName := "data.proxmox_virtual_environment_version.test"
resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: accProviders,
Steps: []resource.TestStep{
// Read testing
{
Config: ProviderConfig + `data "proxmox_virtual_environment_version" "test" {}`,
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(datasourceName, "release", "8.0"),
resource.TestCheckResourceAttrSet(datasourceName, "repository_id"),
resource.TestCheckResourceAttrWith(datasourceName, "version", func(value string) error {
if strings.HasPrefix(value, "8.0") {
return nil
}
return fmt.Errorf("version %s does not start with 8.0", value)
}),
resource.TestCheckResourceAttrSet(datasourceName, "id"),
),
},
},
})
}

View File

@ -4,25 +4,30 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. * file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/ */
package test package network
import ( import (
"context"
"testing" "testing"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/bpg/terraform-provider-proxmox/internal/test"
) )
func Test_LinuxBridgeResource(t *testing.T) { func TestLinuxBridgeResource(t *testing.T) {
t.Parallel() t.Parallel()
accProviders := test.AccMuxProviders(context.Background(), t)
resourceName := "proxmox_virtual_environment_network_linux_bridge.test" resourceName := "proxmox_virtual_environment_network_linux_bridge.test"
resource.Test(t, resource.TestCase{ resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: AccTestProtoV6ProviderFactories, ProtoV6ProviderFactories: accProviders,
Steps: []resource.TestStep{ Steps: []resource.TestStep{
// Create and Read testing // Create and Read testing
{ {
Config: ProviderConfig + ` Config: test.ProviderConfig + `
resource "proxmox_virtual_environment_network_linux_bridge" "test" { resource "proxmox_virtual_environment_network_linux_bridge" "test" {
node_name = "pve" node_name = "pve"
name = "vmbr99" name = "vmbr99"
@ -48,7 +53,7 @@ resource "proxmox_virtual_environment_network_linux_bridge" "test" {
}, },
// Update testing // Update testing
{ {
Config: ProviderConfig + ` Config: test.ProviderConfig + `
resource "proxmox_virtual_environment_network_linux_bridge" "test" { resource "proxmox_virtual_environment_network_linux_bridge" "test" {
node_name = "pve" node_name = "pve"
name = "vmbr99" name = "vmbr99"

View File

@ -4,37 +4,42 @@
* file, You can obtain one at https://mozilla.org/MPL/2.0/. * file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/ */
package test package network
import ( import (
"context"
"testing" "testing"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/bpg/terraform-provider-proxmox/internal/test"
) )
func Test_LinuxVLANResource(t *testing.T) { func TestLinuxVLANResource(t *testing.T) {
t.Parallel() t.Parallel()
accProviders := test.AccMuxProviders(context.Background(), t)
resourceName := "proxmox_virtual_environment_network_linux_vlan.test" resourceName := "proxmox_virtual_environment_network_linux_vlan.test"
resource.Test(t, resource.TestCase{ resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: AccTestProtoV6ProviderFactories, ProtoV6ProviderFactories: accProviders,
Steps: []resource.TestStep{ Steps: []resource.TestStep{
// Create and Read testing // Create and Read testing
{ {
Config: ProviderConfig + ` Config: test.ProviderConfig + `
resource "proxmox_virtual_environment_network_linux_vlan" "test" { resource "proxmox_virtual_environment_network_linux_vlan" "test" {
node_name = "pve" node_name = "pve"
name = "ens18.33" name = "eno0.33"
comment = "created by terraform" comment = "created by terraform"
mtu = 1499 mtu = 1499
} }
`, `,
Check: resource.ComposeAggregateTestCheckFunc( Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "name", "ens18.33"), resource.TestCheckResourceAttr(resourceName, "name", "eno0.33"),
resource.TestCheckResourceAttr(resourceName, "comment", "created by terraform"), resource.TestCheckResourceAttr(resourceName, "comment", "created by terraform"),
resource.TestCheckResourceAttr(resourceName, "vlan", "33"), resource.TestCheckResourceAttr(resourceName, "vlan", "33"),
resource.TestCheckResourceAttr(resourceName, "interface", "ens18"), resource.TestCheckResourceAttr(resourceName, "interface", "eno0"),
resource.TestCheckResourceAttrSet(resourceName, "id"), resource.TestCheckResourceAttrSet(resourceName, "id"),
), ),
}, },
@ -46,10 +51,10 @@ resource "proxmox_virtual_environment_network_linux_vlan" "test" {
}, },
// Update testing // Update testing
{ {
Config: ProviderConfig + ` Config: test.ProviderConfig + `
resource "proxmox_virtual_environment_network_linux_vlan" "test" { resource "proxmox_virtual_environment_network_linux_vlan" "test" {
node_name = "pve" node_name = "pve"
name = "ens18.33" name = "eno0.33"
address = "1.1.1.1/24" address = "1.1.1.1/24"
address6 = "FE80:0000:0000:0000:0202:B3FF:FE1E:8329/64" address6 = "FE80:0000:0000:0000:0202:B3FF:FE1E:8329/64"
comment = "updated by terraform" comment = "updated by terraform"
@ -57,12 +62,12 @@ resource "proxmox_virtual_environment_network_linux_vlan" "test" {
} }
`, `,
Check: resource.ComposeAggregateTestCheckFunc( Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "name", "ens18.33"), resource.TestCheckResourceAttr(resourceName, "name", "eno0.33"),
resource.TestCheckResourceAttr(resourceName, "address", "1.1.1.1/24"), resource.TestCheckResourceAttr(resourceName, "address", "1.1.1.1/24"),
resource.TestCheckResourceAttr(resourceName, "address6", "FE80:0000:0000:0000:0202:B3FF:FE1E:8329/64"), resource.TestCheckResourceAttr(resourceName, "address6", "FE80:0000:0000:0000:0202:B3FF:FE1E:8329/64"),
resource.TestCheckResourceAttr(resourceName, "comment", "updated by terraform"), resource.TestCheckResourceAttr(resourceName, "comment", "updated by terraform"),
resource.TestCheckResourceAttr(resourceName, "vlan", "33"), resource.TestCheckResourceAttr(resourceName, "vlan", "33"),
resource.TestCheckResourceAttr(resourceName, "interface", "ens18"), resource.TestCheckResourceAttr(resourceName, "interface", "eno0"),
resource.TestCheckNoResourceAttr(resourceName, "mtu"), resource.TestCheckNoResourceAttr(resourceName, "mtu"),
resource.TestCheckResourceAttrSet(resourceName, "id"), resource.TestCheckResourceAttrSet(resourceName, "id"),
), ),

View File

@ -1,41 +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 test
import (
"github.com/hashicorp/terraform-plugin-framework/providerserver"
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
"github.com/bpg/terraform-provider-proxmox/internal/provider"
)
const (
// ProviderConfig is a shared configuration to combine with the actual
// test configuration so the Proxmox VE client is properly configured.
// It is also possible to use the PROXMOX_VE_ environment variables instead,.
ProviderConfig = `
provider "proxmox" {
username = "root@pam"
password = "password"
insecure = true
ssh {
agent = true
}
}
`
// such as updating the Makefile and running the testing through that tool.
)
// AccTestProtoV6ProviderFactories are used to instantiate a provider during
// acceptance testing. The factory function will be invoked for every Terraform
// CLI command executed to create a provider server to which the CLI can
// reattach.
//
//nolint:gochecknoglobals
var AccTestProtoV6ProviderFactories = map[string]func() (tfprotov6.ProviderServer, error){
"proxmox": providerserver.NewProtocol6WithError(provider.New("test")()),
}

View File

@ -0,0 +1,79 @@
/*
* 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 test
import (
"context"
"fmt"
"testing"
"github.com/hashicorp/terraform-plugin-framework/providerserver"
"github.com/hashicorp/terraform-plugin-go/tfprotov5"
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
"github.com/hashicorp/terraform-plugin-mux/tf5to6server"
"github.com/hashicorp/terraform-plugin-mux/tf6muxserver"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/stretchr/testify/require"
fwprovider "github.com/bpg/terraform-provider-proxmox/internal/provider"
sdkV2provider "github.com/bpg/terraform-provider-proxmox/proxmoxtf/provider"
)
const (
// ProviderConfig is a shared configuration to combine with the actual
// test configuration so the Proxmox VE client is properly configured.
// It is also possible to use the PROXMOX_VE_ environment variables instead.
ProviderConfig = `
provider "proxmox" {
username = "root@pam"
password = "password"
insecure = true
ssh {
agent = true
}
}
`
)
// AccMuxProviders returns a map of mux servers for the acceptance tests.
func AccMuxProviders(ctx context.Context, t *testing.T) map[string]func() (tfprotov6.ProviderServer, error) {
t.Helper()
// Init sdkV2 provider
sdkV2Provider, err := tf5to6server.UpgradeServer(
ctx,
func() tfprotov5.ProviderServer {
return schema.NewGRPCProviderServer(
sdkV2provider.ProxmoxVirtualEnvironment(),
)
},
)
require.NoError(t, err)
// Init framework provider
frameworkProvider := fwprovider.New("test")()
providers := []func() tfprotov6.ProviderServer{
providerserver.NewProtocol6(frameworkProvider),
func() tfprotov6.ProviderServer {
return sdkV2Provider
},
}
// Init mux servers
muxServers := map[string]func() (tfprotov6.ProviderServer, error){
"proxmox": func() (tfprotov6.ProviderServer, error) {
muxServer, e := tf6muxserver.NewMuxServer(ctx, providers...)
if e != nil {
return nil, fmt.Errorf("failed to create mux server: %w", e)
}
return muxServer, nil
},
}
return muxServers
}

12
main.go
View File

@ -23,13 +23,9 @@ import (
"github.com/bpg/terraform-provider-proxmox/proxmoxtf/provider" "github.com/bpg/terraform-provider-proxmox/proxmoxtf/provider"
) )
// If you do not have terraform installed, you can remove the formatting command, but it's suggested to // these will be set by the goreleaser configuration
// ensure the documentation is formatted properly. // to appropriate values for the compiled binary.
//go:generate terraform fmt -recursive ./example/ var version = "dev"
// Run the docs generation tool, check its repository for more information on how it works and how docs
// can be customized.
//go:generate go run -modfile=tools/go.mod github.com/hashicorp/terraform-plugin-docs/cmd/tfplugindocs
func main() { func main() {
ctx := context.Background() ctx := context.Background()
@ -52,7 +48,7 @@ func main() {
} }
providers := []func() tfprotov6.ProviderServer{ providers := []func() tfprotov6.ProviderServer{
providerserver.NewProtocol6(newProvider.New("dev")()), providerserver.NewProtocol6(newProvider.New(version)()),
func() tfprotov6.ProviderServer { func() tfprotov6.ProviderServer {
return upgradedSdkServer return upgradedSdkServer
}, },

View File

@ -13,7 +13,7 @@ type ResponseBody struct {
// ResponseData contains the data from a version response. // ResponseData contains the data from a version response.
type ResponseData struct { type ResponseData struct {
Keyboard string `json:"keyboard"` Console string `json:"console"`
Release string `json:"release"` Release string `json:"release"`
RepositoryID string `json:"repoid"` RepositoryID string `json:"repoid"`
Version string `json:"version"` Version string `json:"version"`

View File

@ -1,84 +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 datasource
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/proxmoxtf"
)
const (
mkDataSourceVirtualEnvironmentVersionKeyboardLayout = "keyboard_layout"
mkDataSourceVirtualEnvironmentVersionRelease = "release"
mkDataSourceVirtualEnvironmentVersionRepositoryID = "repository_id"
mkDataSourceVirtualEnvironmentVersionVersion = "version"
)
// Version returns a resource for the Proxmox version.
func Version() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
mkDataSourceVirtualEnvironmentVersionKeyboardLayout: {
Type: schema.TypeString,
Description: "The keyboard layout",
Computed: true,
ForceNew: true,
},
mkDataSourceVirtualEnvironmentVersionRelease: {
Type: schema.TypeString,
Description: "The release information",
Computed: true,
ForceNew: true,
},
mkDataSourceVirtualEnvironmentVersionRepositoryID: {
Type: schema.TypeString,
Description: "The repository id",
Computed: true,
ForceNew: true,
},
mkDataSourceVirtualEnvironmentVersionVersion: {
Type: schema.TypeString,
Description: "The version information",
Computed: true,
ForceNew: true,
},
},
ReadContext: versionRead,
}
}
func versionRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics
config := m.(proxmoxtf.ProviderConfiguration)
api, err := config.GetClient()
if err != nil {
return diag.FromErr(err)
}
version, err := api.Version().Version(ctx)
if err != nil {
return diag.FromErr(err)
}
d.SetId("version")
err = d.Set(mkDataSourceVirtualEnvironmentVersionKeyboardLayout, version.Keyboard)
diags = append(diags, diag.FromErr(err)...)
err = d.Set(mkDataSourceVirtualEnvironmentVersionRelease, version.Release)
diags = append(diags, diag.FromErr(err)...)
err = d.Set(mkDataSourceVirtualEnvironmentVersionRepositoryID, version.RepositoryID)
diags = append(diags, diag.FromErr(err)...)
err = d.Set(mkDataSourceVirtualEnvironmentVersionVersion, version.Version)
diags = append(diags, diag.FromErr(err)...)
return diags
}

View File

@ -1,46 +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 datasource
import (
"testing"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/bpg/terraform-provider-proxmox/proxmoxtf/test"
)
// TestVersionInstantiation tests whether the Version instance can be instantiated.
func TestVersionInstantiation(t *testing.T) {
t.Parallel()
s := Version()
if s == nil {
t.Fatalf("Cannot instantiate Version")
}
}
// TestVersionSchema tests the Version schema.
func TestVersionSchema(t *testing.T) {
t.Parallel()
s := Version()
test.AssertComputedAttributes(t, s, []string{
mkDataSourceVirtualEnvironmentVersionKeyboardLayout,
mkDataSourceVirtualEnvironmentVersionRelease,
mkDataSourceVirtualEnvironmentVersionRepositoryID,
mkDataSourceVirtualEnvironmentVersionVersion,
})
test.AssertValueTypes(t, s, map[string]schema.ValueType{
mkDataSourceVirtualEnvironmentVersionKeyboardLayout: schema.TypeString,
mkDataSourceVirtualEnvironmentVersionRelease: schema.TypeString,
mkDataSourceVirtualEnvironmentVersionRepositoryID: schema.TypeString,
mkDataSourceVirtualEnvironmentVersionVersion: schema.TypeString,
})
}

View File

@ -27,7 +27,6 @@ func createDatasourceMap() map[string]*schema.Resource {
"proxmox_virtual_environment_time": datasource.Time(), "proxmox_virtual_environment_time": datasource.Time(),
"proxmox_virtual_environment_user": datasource.User(), "proxmox_virtual_environment_user": datasource.User(),
"proxmox_virtual_environment_users": datasource.Users(), "proxmox_virtual_environment_users": datasource.Users(),
"proxmox_virtual_environment_version": datasource.Version(),
"proxmox_virtual_environment_vm": datasource.VM(), "proxmox_virtual_environment_vm": datasource.VM(),
"proxmox_virtual_environment_vms": datasource.VMs(), "proxmox_virtual_environment_vms": datasource.VMs(),
} }

View File

@ -0,0 +1,20 @@
---
layout: page
title: {{.Name}}
parent: Data Sources
subcategory: Virtual Environment
description: |-
{{ .Description | plainmarkdown | trimspace | prefixlines " " }}
---
# {{.Type}}: {{.Name}}
{{ .Description | trimspace }}
{{ if .HasExample -}}
## Example Usage
{{ printf "{{tffile %q}}" .ExampleFile }}
{{- end }}
{{ .SchemaMarkdown | trimspace }}

View File

@ -0,0 +1,28 @@
---
layout: page
title: {{.Name}}
parent: Resources
subcategory: Virtual Environment
description: |-
{{ .Description | plainmarkdown | trimspace | prefixlines " " }}
---
# {{.Type}}: {{.Name}}
{{ .Description | trimspace }}
{{ if .HasExample -}}
## Example Usage
{{ printf "{{tffile %q}}" .ExampleFile }}
{{- end }}
{{ .SchemaMarkdown | trimspace }}
{{- if .HasImport }}
## Import
Import is supported using the following syntax:
{{ printf "{{codefile \"shell\" %q}}" .ImportFile }}
{{- end }}

View File

@ -18,3 +18,16 @@ import (
_ "github.com/goreleaser/goreleaser" _ "github.com/goreleaser/goreleaser"
_ "github.com/hashicorp/terraform-plugin-docs/cmd/tfplugindocs" _ "github.com/hashicorp/terraform-plugin-docs/cmd/tfplugindocs"
) )
// Format Terraform code for use in documentation.
// If you do not have Terraform installed, you can remove the formatting command, but it is suggested
// to ensure the documentation is formatted properly.
//go:generate terraform fmt -recursive ../examples/
// Generate documentation.
//go:generate go run github.com/hashicorp/terraform-plugin-docs/cmd/tfplugindocs generate --provider-dir ../ --rendered-website-dir ./docs-gen
// Temporary: while migrating to the TF framework, we need to copy the generated docs to the right place
// for the resources / data sources that have been migrated.
//go:generate cp ../docs-gen/data-sources/virtual_environment_version.md ../docs/data-sources/
//go:generate cp ../docs-gen/resources/virtual_environment_network_linux_bridge.md ../docs/resources/
//go:generate cp ../docs-gen/resources/virtual_environment_network_linux_vlan.md ../docs/resources/