Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cluster): add proxmox_virtual_environment_metrics_server resource #1719

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ The following assumptions are made about the test environment:
- It has one node named `pve`
- The node has local storages named `local` and `local-lvm`
- The "Snippets" content type is enabled in the `local` storage
- Default Linux Bridge "vmbr0" is VLAN aware (datacenter -> pve -> network -> edit & apply)

Create `example/terraform.tfvars` with the following variables:

Expand Down
42 changes: 42 additions & 0 deletions docs/data-sources/virtual_environment_metrics_server.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
layout: page
title: proxmox_virtual_environment_metrics_server
parent: Data Sources
subcategory: Virtual Environment
description: |-
Retrieves information about a specific PVE metric server.
---

# Data Source: proxmox_virtual_environment_metrics_server

Retrieves information about a specific PVE metric server.

## Example Usage

```terraform
data "proxmox_virtual_environment_metrics_server" "example" {
name = "example_influxdb"
}

output "data_proxmox_virtual_environment_metrics_server" {
value = {
server = data.proxmox_virtual_environment_metrics_server.example.server
port = data.proxmox_virtual_environment_metrics_server.example.port
}
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `name` (String) Unique name that will be ID of this metric server in PVE.

### Read-Only

- `disable` (Boolean) Indicates if the metric server is disabled.
- `id` (String) The unique identifier of this resource.
- `port` (Number) Server network port.
- `server` (String) Server dns name or IP address.
- `type` (String) Plugin type. Either `graphite` or `influxdb`.
68 changes: 68 additions & 0 deletions docs/resources/virtual_environment_metrics_server.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
---
layout: page
title: proxmox_virtual_environment_metrics_server
parent: Resources
subcategory: Virtual Environment
description: |-
Manages PVE metrics server.
---

# Resource: proxmox_virtual_environment_metrics_server

Manages PVE metrics server.

## Example Usage

```terraform
resource "proxmox_virtual_environment_metrics_server" "influxdb_server" {
name = "example_influxdb_server"
server = "192.168.3.2"
port = 8089
type = "influxdb"
}

resource "proxmox_virtual_environment_metrics_server" "graphite_server" {
name = "example_graphite_server"
server = "192.168.4.2"
port = 2003
type = "graphite"
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `name` (String) Unique name that will be ID of this metric server in PVE.
- `port` (Number) Server network port.
- `server` (String) Server dns name or IP address.
- `type` (String) Plugin type. Choice is between `graphite` | `influxdb`.

### Optional

- `disable` (Boolean) Set this to `true` to disable this metric server.
- `graphite_path` (String) Root graphite path (ex: `proxmox.mycluster.mykey`).
- `graphite_proto` (String) Protocol to send graphite data. Choice is between `udp` | `tcp`. If not set, PVE default is `udp`.
- `influx_api_path_prefix` (String) An API path prefix inserted between `<host>:<port>/` and `/api2/`. Can be useful if the InfluxDB service runs behind a reverse proxy.
- `influx_bucket` (String) The InfluxDB bucket/db. Only necessary when using the http v2 api.
- `influx_db_proto` (String) Protocol for InfluxDB. Choice is between `udp` | `http` | `https`. If not set, PVE default is `udp`.
- `influx_max_body_size` (Number) InfluxDB max-body-size in bytes. Requests are batched up to this size. If not set, PVE default is `25000000`.
- `influx_organization` (String) The InfluxDB organization. Only necessary when using the http v2 api. Has no meaning when using v2 compatibility api.
- `influx_token` (String, Sensitive) The InfluxDB access token. Only necessary when using the http v2 api. If the v2 compatibility api is used, use `user:password` instead.
- `influx_verify` (Boolean) Set to `false` to disable certificate verification for https endpoints.
- `mtu` (Number) MTU (maximum transmission unit) for metrics transmission over UDP. If not set, PVE default is `1500` (allowed `512` - `65536`).
- `timeout` (Number) TCP socket timeout in seconds. If not set, PVE default is `1`.

### Read-Only

- `id` (String) The unique identifier of this resource.

## Import

Import is supported using the following syntax:

```shell
#!/usr/bin/env sh
terraform import proxmox_virtual_environment_metrics_server.example example
```
3 changes: 3 additions & 0 deletions example/data_source_virtual_environment_metrics_server.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
data "proxmox_virtual_environment_metrics_server" "example" {
name = "example_influxdb_server"
}
24 changes: 24 additions & 0 deletions example/resource_virtual_environment_metrics_server.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
resource "proxmox_virtual_environment_metrics_server" "influxdb_server" {
name = "example_influxdb_server"
server = "192.168.3.2"
port = 18089
type = "influxdb"

}

resource "proxmox_virtual_environment_metrics_server" "graphite_server" {
name = "example_graphite_server"
server = "192.168.4.2"
port = 20033
type = "graphite"
}

resource "proxmox_virtual_environment_metrics_server" "graphite_server2" {
name = "example_graphite_server2"
server = "192.168.4.3"
port = 20033
type = "graphite"
mtu = 60000
timeout = 5
graphite_proto = "udp"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
data "proxmox_virtual_environment_metrics_server" "example" {
name = "example_influxdb"
}

output "data_proxmox_virtual_environment_metrics_server" {
value = {
server = data.proxmox_virtual_environment_metrics_server.example.server
port = data.proxmox_virtual_environment_metrics_server.example.port
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env sh
terraform import proxmox_virtual_environment_metrics_server.example example
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
resource "proxmox_virtual_environment_metrics_server" "influxdb_server" {
name = "example_influxdb_server"
server = "192.168.3.2"
port = 8089
type = "influxdb"
}

resource "proxmox_virtual_environment_metrics_server" "graphite_server" {
name = "example_graphite_server"
server = "192.168.4.2"
port = 2003
type = "graphite"
}
130 changes: 130 additions & 0 deletions fwprovider/cluster/metrics/datasource_metrics_server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
* 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 metrics

import (
"context"
"fmt"

"github.com/bpg/terraform-provider-proxmox/fwprovider/attribute"
"github.com/bpg/terraform-provider-proxmox/fwprovider/config"
"github.com/bpg/terraform-provider-proxmox/proxmox/cluster/metrics"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
)

// Ensure the implementation satisfies the expected interfaces.
var (
_ datasource.DataSource = &metricsServerDatasource{}
_ datasource.DataSourceWithConfigure = &metricsServerDatasource{}
)

type metricsServerDatasource struct {
client *metrics.Client
}

// NewMetricsServerDatasource creates new metrics server data source.
func NewMetricsServerDatasource() datasource.DataSource {
return &metricsServerDatasource{}
}

func (r *metricsServerDatasource) Metadata(
_ context.Context,
req datasource.MetadataRequest,
resp *datasource.MetadataResponse,
) {
resp.TypeName = req.ProviderTypeName + "_metrics_server"
}

func (r *metricsServerDatasource) Configure(
_ context.Context,
req datasource.ConfigureRequest,
resp *datasource.ConfigureResponse,
) {
if req.ProviderData == nil {
return
}

cfg, ok := req.ProviderData.(config.DataSource)

if !ok {
resp.Diagnostics.AddError(
"Unexpected Resource Configure Type",
fmt.Sprintf("Expected *proxmox.Client, got: %T", req.ProviderData),
)

return
}

r.client = cfg.Client.Cluster().Metrics()
}

func (r *metricsServerDatasource) Schema(
_ context.Context,
_ datasource.SchemaRequest,
resp *datasource.SchemaResponse,
) {
resp.Schema = schema.Schema{
Description: "Retrieves information about a specific PVE metric server.",
Attributes: map[string]schema.Attribute{
"id": attribute.ID(),
"name": schema.StringAttribute{
Description: "Unique name that will be ID of this metric server in PVE.",
Required: true,
},
"disable": schema.BoolAttribute{
Description: "Indicates if the metric server is disabled.",
Computed: true,
},
"port": schema.Int64Attribute{
Description: "Server network port.",
Computed: true,
},
"server": schema.StringAttribute{
Description: "Server dns name or IP address.",
Computed: true,
},
"type": schema.StringAttribute{
Description: "Plugin type. Either `graphite` or `influxdb`.",
Computed: true,
},
},
}
}

func (r *metricsServerDatasource) Read(
ctx context.Context,
req datasource.ReadRequest,
resp *datasource.ReadResponse,
) {
var state metricsServerDatasourceModel

resp.Diagnostics.Append(req.Config.Get(ctx, &state)...)

if resp.Diagnostics.HasError() {
return
}

state.ID = state.Name

data, err := r.client.GetServer(ctx, state.ID.ValueString())
if err != nil {
resp.Diagnostics.AddError(
"Unable to Refresh Resource",
"An unexpected error occurred while attempting to refresh datasource state. "+
"Please retry the operation or report this issue to the provider developers.\n\n"+
"Error: "+err.Error(),
)

return
}

readModel := &metricsServerDatasourceModel{}
readModel.importFromAPI(state.ID.ValueString(), data)

resp.Diagnostics.Append(resp.State.Set(ctx, readModel)...)
}
Loading