0
0
mirror of https://github.com/bpg/terraform-provider-proxmox.git synced 2025-06-30 18:42:58 +00:00
terraform-provider-proxmox/proxmoxtf/datasource/hosts.go
Pavel Boldyrev 1f006aa82b
feat: API client cleanup and refactoring (#323)
* cleanup 1

* continue refactoring

* more refactoring

* move VMs under nodes

* move container and other apis under nodes

* cleanups

* enabled revive.exported linter & add comments to exported stuff

* enable godot linter

* enable wsl linter

* enable thelper linter

* enable govet linter

* cleanup after rebase

* cleanup after rebase

* extract SSH ops into a separate interface

* fix linter error

* move ssh code to its own package

* cleaning up VirtualEnvironmentClient receivers

* on the finish line

* not sure what else I forgot... 🤔

* fix ssh connection and upload

* renaming client interfaces

* final cleanups
2023-05-26 01:32:51 +00:00

154 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
}