0
0
mirror of https://github.com/bpg/terraform-provider-proxmox.git synced 2025-07-08 14:55:02 +00:00
terraform-provider-proxmox/proxmoxtf/datasource/hosts.go
renovate[bot] 6a8f367c46
chore(deps): update golangci/golangci-lint (v2.1.6 → v2.2.1) (#2013)
* chore(deps): update golangci/golangci-lint (v2.1.6 → v2.2.1)

| datasource      | package                | from   | to     |
| --------------- | ---------------------- | ------ | ------ |
| github-releases | golangci/golangci-lint | v2.1.6 | v2.2.1 |

* chore: update rules & run linter

Signed-off-by: Pavel Boldyrev <627562+bpg@users.noreply.github.com>

---------

Signed-off-by: Pavel Boldyrev <627562+bpg@users.noreply.github.com>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Pavel Boldyrev <627562+bpg@users.noreply.github.com>
2025-07-04 17:56:38 -04:00

155 lines
4.3 KiB
Go

/*
* 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"
"fmt"
"strings"
"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 (
mkDataSourceVirtualEnvironmentHostsAddresses = "addresses"
mkDataSourceVirtualEnvironmentHostsDigest = "digest"
mkDataSourceVirtualEnvironmentHostsEntries = "entries"
mkDataSourceVirtualEnvironmentHostsEntriesAddress = "address"
mkDataSourceVirtualEnvironmentHostsEntriesHostnames = "hostnames"
mkDataSourceVirtualEnvironmentHostsHostnames = "hostnames"
mkDataSourceVirtualEnvironmentHostsNodeName = "node_name"
)
// Hosts returns a resource for the Proxmox hosts.
func Hosts() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
mkDataSourceVirtualEnvironmentHostsAddresses: {
Type: schema.TypeList,
Description: "The addresses",
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
mkDataSourceVirtualEnvironmentHostsDigest: {
Type: schema.TypeString,
Description: "The SHA1 digest",
Computed: true,
},
mkDataSourceVirtualEnvironmentHostsEntries: {
Type: schema.TypeList,
Description: "The host entries",
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
mkDataSourceVirtualEnvironmentHostsEntriesAddress: {
Type: schema.TypeString,
Description: "The address",
Computed: true,
},
mkDataSourceVirtualEnvironmentHostsEntriesHostnames: {
Type: schema.TypeList,
Description: "The hostnames",
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
},
},
mkDataSourceVirtualEnvironmentHostsHostnames: {
Type: schema.TypeList,
Description: "The hostnames",
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeList,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
mkDataSourceVirtualEnvironmentHostsNodeName: {
Type: schema.TypeString,
Description: "The node name",
Required: true,
},
},
ReadContext: hostsRead,
}
}
func hostsRead(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)
}
nodeName := d.Get(mkDataSourceVirtualEnvironmentHostsNodeName).(string)
hosts, err := api.Node(nodeName).GetHosts(ctx)
if err != nil {
return diag.FromErr(err)
}
d.SetId(fmt.Sprintf("%s_hosts", nodeName))
// Parse the entries in the hosts file.
var addresses []interface{}
var entries []interface{}
var hostnames []interface{}
lines := strings.Split(hosts.Data, "\n")
for _, line := range lines {
if strings.HasPrefix(line, "#") {
continue
}
line = strings.ReplaceAll(line, "\t", " ")
values := strings.Split(line, " ")
if values[0] == "" {
continue
}
addresses = append(addresses, values[0])
entry := map[string]interface{}{}
var hostnamesForAddress []interface{}
for _, hostname := range values[1:] {
if hostname != "" {
hostnamesForAddress = append(hostnamesForAddress, hostname)
}
}
entry[mkDataSourceVirtualEnvironmentHostsEntriesAddress] = values[0]
entry[mkDataSourceVirtualEnvironmentHostsEntriesHostnames] = hostnamesForAddress
entries = append(entries, entry)
hostnames = append(hostnames, hostnamesForAddress)
}
err = d.Set(mkDataSourceVirtualEnvironmentHostsAddresses, addresses)
diags = append(diags, diag.FromErr(err)...)
if hosts.Digest != nil {
err = d.Set(mkDataSourceVirtualEnvironmentHostsDigest, *hosts.Digest)
} else {
err = d.Set(mkDataSourceVirtualEnvironmentHostsDigest, "")
}
diags = append(diags, diag.FromErr(err)...)
err = d.Set(mkDataSourceVirtualEnvironmentHostsEntries, entries)
diags = append(diags, diag.FromErr(err)...)
err = d.Set(mkDataSourceVirtualEnvironmentHostsHostnames, hostnames)
diags = append(diags, diag.FromErr(err)...)
return diags
}