mirror of
https://github.com/bpg/terraform-provider-proxmox.git
synced 2025-07-04 21:14:05 +00:00
Add hosts data source
This commit is contained in:
parent
1eb0fe0a34
commit
2d824e37bc
@ -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`
|
||||
|
||||
|
11
README.md
11
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
|
||||
|
15
example/data_source_virtual_environment_hosts.tf
Normal file
15
example/data_source_virtual_environment_hosts.tf
Normal file
@ -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}"
|
||||
}
|
32
proxmox/virtual_environment_hosts.go
Normal file
32
proxmox/virtual_environment_hosts.go
Normal file
@ -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)
|
||||
}
|
22
proxmox/virtual_environment_hosts_types.go
Normal file
22
proxmox/virtual_environment_hosts_types.go
Normal file
@ -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"`
|
||||
}
|
111
proxmoxtf/data_source_virtual_environment_hosts.go
Normal file
111
proxmoxtf/data_source_virtual_environment_hosts.go
Normal file
@ -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
|
||||
}
|
46
proxmoxtf/data_source_virtual_environment_hosts_test.go
Normal file
46
proxmoxtf/data_source_virtual_environment_hosts_test.go
Normal file
@ -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,
|
||||
})
|
||||
}
|
@ -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(),
|
||||
|
Loading…
Reference in New Issue
Block a user