0
0
mirror of https://github.com/bpg/terraform-provider-proxmox.git synced 2025-07-02 03:22:59 +00:00
terraform-provider-proxmox/proxmoxtf/resource/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

241 lines
6.7 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 resource
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/proxmox/nodes"
"github.com/bpg/terraform-provider-proxmox/proxmoxtf"
)
const (
mkResourceVirtualEnvironmentHostsAddresses = "addresses"
mkResourceVirtualEnvironmentHostsDigest = "digest"
mkResourceVirtualEnvironmentHostsEntries = "entries"
mkResourceVirtualEnvironmentHostsEntriesAddress = "address"
mkResourceVirtualEnvironmentHostsEntriesHostnames = "hostnames"
mkResourceVirtualEnvironmentHostsEntry = "entry"
mkResourceVirtualEnvironmentHostsEntryAddress = "address"
mkResourceVirtualEnvironmentHostsEntryHostnames = "hostnames"
mkResourceVirtualEnvironmentHostsHostnames = "hostnames"
mkResourceVirtualEnvironmentHostsNodeName = "node_name"
)
// Hosts returns a resource that manages hosts settings for a node.
func Hosts() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
mkResourceVirtualEnvironmentHostsAddresses: {
Type: schema.TypeList,
Description: "The addresses",
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
mkResourceVirtualEnvironmentHostsDigest: {
Type: schema.TypeString,
Description: "The SHA1 digest",
Computed: true,
},
mkResourceVirtualEnvironmentHostsEntries: {
Type: schema.TypeList,
Description: "The host entries",
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
mkResourceVirtualEnvironmentHostsEntriesAddress: {
Type: schema.TypeString,
Description: "The address",
Computed: true,
},
mkResourceVirtualEnvironmentHostsEntriesHostnames: {
Type: schema.TypeList,
Description: "The hostnames",
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
},
},
mkResourceVirtualEnvironmentHostsEntry: {
Type: schema.TypeList,
Description: "The host entries",
Required: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
mkResourceVirtualEnvironmentHostsEntryAddress: {
Type: schema.TypeString,
Description: "The address",
Required: true,
},
mkResourceVirtualEnvironmentHostsEntryHostnames: {
Type: schema.TypeList,
Description: "The hostnames",
Required: true,
Elem: &schema.Schema{Type: schema.TypeString},
MinItems: 1,
},
},
},
MinItems: 1,
},
mkResourceVirtualEnvironmentHostsHostnames: {
Type: schema.TypeList,
Description: "The hostnames",
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeList,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
mkResourceVirtualEnvironmentHostsNodeName: {
Type: schema.TypeString,
Description: "The node name",
Required: true,
},
},
CreateContext: hostsCreate,
ReadContext: hostsRead,
UpdateContext: hostsUpdate,
DeleteContext: hostsDelete,
}
}
func hostsCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
diags := hostsUpdate(ctx, d, m)
if diags.HasError() {
return diags
}
nodeName := d.Get(mkResourceVirtualEnvironmentHostsNodeName).(string)
d.SetId(fmt.Sprintf("%s_hosts", nodeName))
return diags
}
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(mkResourceVirtualEnvironmentHostsNodeName).(string)
hosts, err := api.Node(nodeName).GetHosts(ctx)
if err != nil {
return diag.FromErr(err)
}
// 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[mkResourceVirtualEnvironmentHostsEntriesAddress] = values[0]
entry[mkResourceVirtualEnvironmentHostsEntriesHostnames] = hostnamesForAddress
entries = append(entries, entry)
hostnames = append(hostnames, hostnamesForAddress)
}
err = d.Set(mkResourceVirtualEnvironmentHostsAddresses, addresses)
diags = append(diags, diag.FromErr(err)...)
if hosts.Digest != nil {
err = d.Set(mkResourceVirtualEnvironmentHostsDigest, *hosts.Digest)
} else {
err = d.Set(mkResourceVirtualEnvironmentHostsDigest, "")
}
diags = append(diags, diag.FromErr(err)...)
err = d.Set(mkResourceVirtualEnvironmentHostsEntries, entries)
diags = append(diags, diag.FromErr(err)...)
err = d.Set(mkResourceVirtualEnvironmentHostsEntry, entries)
diags = append(diags, diag.FromErr(err)...)
err = d.Set(mkResourceVirtualEnvironmentHostsHostnames, hostnames)
diags = append(diags, diag.FromErr(err)...)
return diags
}
func hostsUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
config := m.(proxmoxtf.ProviderConfiguration)
api, err := config.GetClient()
if err != nil {
return diag.FromErr(err)
}
entry := d.Get(mkResourceVirtualEnvironmentHostsEntry).([]interface{})
nodeName := d.Get(mkResourceVirtualEnvironmentHostsNodeName).(string)
// Generate the data for the hosts file based on the specified entries.
body := nodes.HostsUpdateRequestBody{
Data: "",
}
for _, e := range entry {
eMap := e.(map[string]interface{})
address := eMap[mkResourceVirtualEnvironmentHostsEntryAddress].(string)
hostnames := eMap[mkResourceVirtualEnvironmentHostsEntryHostnames].([]interface{})
body.Data += address
for _, h := range hostnames {
hostname := h.(string)
body.Data += " " + hostname
}
body.Data += "\n"
}
err = api.Node(nodeName).UpdateHosts(ctx, &body)
if err != nil {
return diag.FromErr(err)
}
return hostsRead(ctx, d, m)
}
func hostsDelete(_ context.Context, d *schema.ResourceData, _ interface{}) diag.Diagnostics {
d.SetId("")
return nil
}