diff --git a/CHANGELOG.md b/CHANGELOG.md index c6324e8d..d1d4840f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ FEATURES: * **New Data Source:** `proxmox_virtual_environment_dns` +* **New Data Source:** `proxmox_virtual_environment_hosts` * **New Resource:** `proxmox_virtual_environment_certificate` * **New Resource:** `proxmox_virtual_environment_dns` diff --git a/README.md b/README.md index 3f223ae4..ddb0fa2c 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,7 @@ A Terraform Provider which adds support for Proxmox solutions. - [DNS](#dns-proxmox_virtual_environment_dns) - [Group](#group-proxmox_virtual_environment_group) - [Groups](#groups-proxmox_virtual_environment_groups) + - [Hosts](#hosts-proxmox_virtual_environment_hosts) - [Nodes](#nodes-proxmox_virtual_environment_nodes) - [Pool](#pool-proxmox_virtual_environment_pool) - [Pools](#pools-proxmox_virtual_environment_pools) @@ -146,6 +147,16 @@ This data source doesn't accept arguments. * `comments` - The group comments * `group_ids` - The group ids +##### Hosts (proxmox_virtual_environment_hosts) + +###### Arguments +* `node_name` - (Required) A node name + +###### Attributes +* `addresses` - The IP addresses +* `digest` - The SHA1 digest +* `hostnames` - The hostnames associated with each of the IP addresses + ##### Nodes (proxmox_virtual_environment_nodes) ###### Arguments diff --git a/example/data_source_virtual_environment_hosts.tf b/example/data_source_virtual_environment_hosts.tf new file mode 100644 index 00000000..1f901a6a --- /dev/null +++ b/example/data_source_virtual_environment_hosts.tf @@ -0,0 +1,15 @@ +data "proxmox_virtual_environment_hosts" "example" { + node_name = "${data.proxmox_virtual_environment_nodes.example.names[0]}" +} + +output "data_proxmox_virtual_environment_hosts_example_addresses" { + value = "${data.proxmox_virtual_environment_hosts.example.addresses}" +} + +output "data_proxmox_virtual_environment_hosts_example_digest" { + value = "${data.proxmox_virtual_environment_hosts.example.digest}" +} + +output "data_proxmox_virtual_environment_hosts_example_hostnames" { + value = "${data.proxmox_virtual_environment_hosts.example.hostnames}" +} diff --git a/proxmox/virtual_environment_hosts.go b/proxmox/virtual_environment_hosts.go new file mode 100644 index 00000000..d6522f34 --- /dev/null +++ b/proxmox/virtual_environment_hosts.go @@ -0,0 +1,32 @@ +/* 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 proxmox + +import ( + "errors" + "fmt" + "net/url" +) + +// GetHosts retrieves the Hosts configuration for a node. +func (c *VirtualEnvironmentClient) GetHosts(nodeName string) (*VirtualEnvironmentHostsGetResponseData, error) { + resBody := &VirtualEnvironmentHostsGetResponseBody{} + err := c.DoRequest(hmGET, fmt.Sprintf("nodes/%s/hosts", url.PathEscape(nodeName)), nil, resBody) + + if err != nil { + return nil, err + } + + if resBody.Data == nil { + return nil, errors.New("The server did not include a data object in the response") + } + + return resBody.Data, nil +} + +// UpdateHosts updates the Hosts configuration for a node. +func (c *VirtualEnvironmentClient) UpdateHosts(nodeName string, d *VirtualEnvironmentHostsUpdateRequestBody) error { + return c.DoRequest(hmPOST, fmt.Sprintf("nodes/%s/hosts", url.PathEscape(nodeName)), d, nil) +} diff --git a/proxmox/virtual_environment_hosts_types.go b/proxmox/virtual_environment_hosts_types.go new file mode 100644 index 00000000..739ce7c6 --- /dev/null +++ b/proxmox/virtual_environment_hosts_types.go @@ -0,0 +1,22 @@ +/* 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 proxmox + +// VirtualEnvironmentHostsGetResponseBody contains the body from a hosts get response. +type VirtualEnvironmentHostsGetResponseBody struct { + Data *VirtualEnvironmentHostsGetResponseData `json:"data,omitempty"` +} + +// VirtualEnvironmentHostsGetResponseData contains the data from a hosts get response. +type VirtualEnvironmentHostsGetResponseData struct { + Data string `json:"data"` + Digest *string `json:"digest,omitempty"` +} + +// VirtualEnvironmentHostsUpdateRequestBody contains the body for a hosts update request. +type VirtualEnvironmentHostsUpdateRequestBody struct { + Data string `json:"data" url:"data"` + Digest *string `json:"digest,omitempty" url:"digest,omitempty"` +} diff --git a/proxmoxtf/data_source_virtual_environment_hosts.go b/proxmoxtf/data_source_virtual_environment_hosts.go new file mode 100644 index 00000000..bd94c9c5 --- /dev/null +++ b/proxmoxtf/data_source_virtual_environment_hosts.go @@ -0,0 +1,111 @@ +/* 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 proxmoxtf + +import ( + "fmt" + "strings" + + "github.com/hashicorp/terraform/helper/schema" +) + +const ( + mkDataSourceVirtualEnvironmentHostsAddresses = "addresses" + mkDataSourceVirtualEnvironmentHostsDigest = "digest" + mkDataSourceVirtualEnvironmentHostsHostnames = "hostnames" + mkDataSourceVirtualEnvironmentHostsNodeName = "node_name" +) + +func dataSourceVirtualEnvironmentHosts() *schema.Resource { + return &schema.Resource{ + Schema: map[string]*schema.Schema{ + mkDataSourceVirtualEnvironmentHostsAddresses: &schema.Schema{ + Type: schema.TypeList, + Description: "The addresses", + Computed: true, + Elem: &schema.Schema{Type: schema.TypeString}, + }, + mkDataSourceVirtualEnvironmentHostsDigest: &schema.Schema{ + Type: schema.TypeString, + Description: "The SHA1 digest", + Computed: true, + }, + mkDataSourceVirtualEnvironmentHostsHostnames: &schema.Schema{ + Type: schema.TypeList, + Description: "The hostnames", + Computed: true, + Elem: &schema.Schema{ + Type: schema.TypeList, + Elem: &schema.Schema{Type: schema.TypeString}, + }, + }, + mkDataSourceVirtualEnvironmentHostsNodeName: &schema.Schema{ + Type: schema.TypeString, + Description: "The node name", + Required: true, + }, + }, + Read: dataSourceVirtualEnvironmentHostsRead, + } +} + +func dataSourceVirtualEnvironmentHostsRead(d *schema.ResourceData, m interface{}) error { + config := m.(providerConfiguration) + veClient, err := config.GetVEClient() + + if err != nil { + return err + } + + nodeName := d.Get(mkDataSourceVirtualEnvironmentHostsNodeName).(string) + hosts, err := veClient.GetHosts(nodeName) + + if err != nil { + return err + } + + d.SetId(fmt.Sprintf("%s_hosts", nodeName)) + + // Parse the entries in the hosts file. + addresses := []interface{}{} + 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]) + hostnamesForAddress := []interface{}{} + + for _, hostname := range values[1:] { + if hostname != "" { + hostnamesForAddress = append(hostnamesForAddress, hostname) + } + } + + hostnames = append(hostnames, hostnamesForAddress) + } + + d.Set(mkDataSourceVirtualEnvironmentHostsAddresses, addresses) + + if hosts.Digest != nil { + d.Set(mkDataSourceVirtualEnvironmentHostsDigest, *hosts.Digest) + } else { + d.Set(mkDataSourceVirtualEnvironmentHostsDigest, "") + } + + d.Set(mkDataSourceVirtualEnvironmentHostsHostnames, hostnames) + + return nil +} diff --git a/proxmoxtf/data_source_virtual_environment_hosts_test.go b/proxmoxtf/data_source_virtual_environment_hosts_test.go new file mode 100644 index 00000000..26842c15 --- /dev/null +++ b/proxmoxtf/data_source_virtual_environment_hosts_test.go @@ -0,0 +1,46 @@ +/* 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 proxmoxtf + +import ( + "github.com/hashicorp/terraform/helper/schema" + "testing" +) + +// TestDataSourceVirtualEnvironmentHostsInstantiation tests whether the DataSourceVirtualEnvironmentHosts instance can be instantiated. +func TestDataSourceVirtualEnvironmentHostsInstantiation(t *testing.T) { + s := dataSourceVirtualEnvironmentHosts() + + if s == nil { + t.Fatalf("Cannot instantiate dataSourceVirtualEnvironmentHosts") + } +} + +// TestDataSourceVirtualEnvironmentHostsSchema tests the dataSourceVirtualEnvironmentHosts schema. +func TestDataSourceVirtualEnvironmentHostsSchema(t *testing.T) { + s := dataSourceVirtualEnvironmentHosts() + + testRequiredArguments(t, s, []string{ + mkDataSourceVirtualEnvironmentHostsNodeName, + }) + + testComputedAttributes(t, s, []string{ + mkDataSourceVirtualEnvironmentHostsAddresses, + mkDataSourceVirtualEnvironmentHostsDigest, + mkDataSourceVirtualEnvironmentHostsHostnames, + }) + + testSchemaValueTypes(t, s, []string{ + mkDataSourceVirtualEnvironmentHostsAddresses, + mkDataSourceVirtualEnvironmentHostsDigest, + mkDataSourceVirtualEnvironmentHostsHostnames, + mkDataSourceVirtualEnvironmentHostsNodeName, + }, []schema.ValueType{ + schema.TypeList, + schema.TypeString, + schema.TypeList, + schema.TypeString, + }) +} diff --git a/proxmoxtf/provider.go b/proxmoxtf/provider.go index 64be255f..26a82ff5 100644 --- a/proxmoxtf/provider.go +++ b/proxmoxtf/provider.go @@ -34,6 +34,7 @@ func Provider() *schema.Provider { "proxmox_virtual_environment_dns": dataSourceVirtualEnvironmentDNS(), "proxmox_virtual_environment_group": dataSourceVirtualEnvironmentGroup(), "proxmox_virtual_environment_groups": dataSourceVirtualEnvironmentGroups(), + "proxmox_virtual_environment_hosts": dataSourceVirtualEnvironmentHosts(), "proxmox_virtual_environment_nodes": dataSourceVirtualEnvironmentNodes(), "proxmox_virtual_environment_pool": dataSourceVirtualEnvironmentPool(), "proxmox_virtual_environment_pools": dataSourceVirtualEnvironmentPools(),