mirror of
https://github.com/bpg/terraform-provider-proxmox.git
synced 2025-08-22 19:38:35 +00:00
Merge pull request #19 from danitso/feature-time-zone
Add time resource
This commit is contained in:
commit
a8f3db6576
@ -1,5 +1,10 @@
|
|||||||
## 0.4.0 (UNRELEASED)
|
## 0.4.0 (UNRELEASED)
|
||||||
|
|
||||||
|
FEATURES:
|
||||||
|
|
||||||
|
* **New Data Source:** `proxmox_virtual_environment_time`
|
||||||
|
* **New Resource:** `proxmox_virtual_environment_time`
|
||||||
|
|
||||||
ENHANCEMENTS:
|
ENHANCEMENTS:
|
||||||
|
|
||||||
* provider/configuration: Add `virtual_environment.otp` argument for TOTP support
|
* provider/configuration: Add `virtual_environment.otp` argument for TOTP support
|
||||||
|
@ -0,0 +1,30 @@
|
|||||||
|
---
|
||||||
|
layout: page
|
||||||
|
title: Time
|
||||||
|
permalink: /data-sources/virtual-environment/time
|
||||||
|
nav_order: 11
|
||||||
|
parent: Virtual Environment Data Sources
|
||||||
|
grand_parent: Data Sources
|
||||||
|
---
|
||||||
|
|
||||||
|
# Data Source: Time
|
||||||
|
|
||||||
|
Retrieves the current time for a specific node.
|
||||||
|
|
||||||
|
## Example Usage
|
||||||
|
|
||||||
|
```
|
||||||
|
data "proxmox_virtual_environment_time" "first_node_time" {
|
||||||
|
node_name = "first-node"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Arguments Reference
|
||||||
|
|
||||||
|
* `node_name` - (Required) A node name.
|
||||||
|
|
||||||
|
## Attributes Reference
|
||||||
|
|
||||||
|
* `local_time` - The node's local time.
|
||||||
|
* `time_zone` - The node's time zone.
|
||||||
|
* `utc_time` - The node's local time formatted as UTC.
|
@ -2,7 +2,7 @@
|
|||||||
layout: page
|
layout: page
|
||||||
title: User
|
title: User
|
||||||
permalink: /data-sources/virtual-environment/user
|
permalink: /data-sources/virtual-environment/user
|
||||||
nav_order: 11
|
nav_order: 12
|
||||||
parent: Virtual Environment Data Sources
|
parent: Virtual Environment Data Sources
|
||||||
grand_parent: Data Sources
|
grand_parent: Data Sources
|
||||||
---
|
---
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
layout: page
|
layout: page
|
||||||
title: Users
|
title: Users
|
||||||
permalink: /data-sources/virtual-environment/users
|
permalink: /data-sources/virtual-environment/users
|
||||||
nav_order: 12
|
nav_order: 13
|
||||||
parent: Virtual Environment Data Sources
|
parent: Virtual Environment Data Sources
|
||||||
grand_parent: Data Sources
|
grand_parent: Data Sources
|
||||||
---
|
---
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
layout: page
|
layout: page
|
||||||
title: Version
|
title: Version
|
||||||
permalink: /data-sources/virtual-environment/version
|
permalink: /data-sources/virtual-environment/version
|
||||||
nav_order: 13
|
nav_order: 14
|
||||||
parent: Virtual Environment Data Sources
|
parent: Virtual Environment Data Sources
|
||||||
grand_parent: Data Sources
|
grand_parent: Data Sources
|
||||||
---
|
---
|
||||||
|
@ -0,0 +1,31 @@
|
|||||||
|
---
|
||||||
|
layout: page
|
||||||
|
title: Time
|
||||||
|
permalink: /ressources/virtual-environment/time
|
||||||
|
nav_order: 9
|
||||||
|
parent: Virtual Environment Resources
|
||||||
|
grand_parent: Resources
|
||||||
|
---
|
||||||
|
|
||||||
|
# Resource: Time
|
||||||
|
|
||||||
|
Manages the time for a specific node.
|
||||||
|
|
||||||
|
## Example Usage
|
||||||
|
|
||||||
|
```
|
||||||
|
resource "proxmox_virtual_environment_time" "first_node_time" {
|
||||||
|
node_name = "first-node"
|
||||||
|
time_zone = "UTC"
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Arguments Reference
|
||||||
|
|
||||||
|
* `node_name` - (Required) A node name.
|
||||||
|
* `time_zone` - (Required) The node's time zone.
|
||||||
|
|
||||||
|
## Attributes Reference
|
||||||
|
|
||||||
|
* `local_time` - The node's local time.
|
||||||
|
* `utc_time` - The node's local time formatted as UTC.
|
@ -2,7 +2,7 @@
|
|||||||
layout: page
|
layout: page
|
||||||
title: User
|
title: User
|
||||||
permalink: /ressources/virtual-environment/user
|
permalink: /ressources/virtual-environment/user
|
||||||
nav_order: 9
|
nav_order: 10
|
||||||
parent: Virtual Environment Resources
|
parent: Virtual Environment Resources
|
||||||
grand_parent: Resources
|
grand_parent: Resources
|
||||||
---
|
---
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
layout: page
|
layout: page
|
||||||
title: VM
|
title: VM
|
||||||
permalink: /ressources/virtual-environment/vm
|
permalink: /ressources/virtual-environment/vm
|
||||||
nav_order: 10
|
nav_order: 11
|
||||||
parent: Virtual Environment Resources
|
parent: Virtual Environment Resources
|
||||||
grand_parent: Resources
|
grand_parent: Resources
|
||||||
---
|
---
|
||||||
|
11
example/data_source_virtual_environment_time.tf
Normal file
11
example/data_source_virtual_environment_time.tf
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
data "proxmox_virtual_environment_time" "example" {
|
||||||
|
node_name = "${data.proxmox_virtual_environment_nodes.example.names[0]}"
|
||||||
|
}
|
||||||
|
|
||||||
|
output "data_proxmox_virtual_environment_time" {
|
||||||
|
value = "${map(
|
||||||
|
"local_time", data.proxmox_virtual_environment_time.example.local_time,
|
||||||
|
"time_zone", data.proxmox_virtual_environment_time.example.time_zone,
|
||||||
|
"utc_time", data.proxmox_virtual_environment_time.example.utc_time,
|
||||||
|
)}"
|
||||||
|
}
|
12
example/resource_virtual_environment_time.tf
Normal file
12
example/resource_virtual_environment_time.tf
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
resource "proxmox_virtual_environment_time" "example" {
|
||||||
|
node_name = "${data.proxmox_virtual_environment_time.example.node_name}"
|
||||||
|
time_zone = "${data.proxmox_virtual_environment_time.example.time_zone}"
|
||||||
|
}
|
||||||
|
|
||||||
|
output "resource_proxmox_virtual_environment_time" {
|
||||||
|
value = "${map(
|
||||||
|
"local_time", proxmox_virtual_environment_time.example.local_time,
|
||||||
|
"time_zone", proxmox_virtual_environment_time.example.time_zone,
|
||||||
|
"utc_time", proxmox_virtual_environment_time.example.utc_time,
|
||||||
|
)}"
|
||||||
|
}
|
@ -176,7 +176,7 @@ func (r *CustomTimestamp) UnmarshalJSON(b []byte) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
*r = CustomTimestamp(time.Unix(i, 0))
|
*r = CustomTimestamp(time.Unix(i, 0).UTC())
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -72,6 +72,22 @@ func (c *VirtualEnvironmentClient) GetNodeIP(nodeName string) (*string, error) {
|
|||||||
return &nodeAddressParts[0], nil
|
return &nodeAddressParts[0], nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetNodeTime retrieves the time information for a node.
|
||||||
|
func (c *VirtualEnvironmentClient) GetNodeTime(nodeName string) (*VirtualEnvironmentNodeGetTimeResponseData, error) {
|
||||||
|
resBody := &VirtualEnvironmentNodeGetTimeResponseBody{}
|
||||||
|
err := c.DoRequest(hmGET, fmt.Sprintf("nodes/%s/time", 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
|
||||||
|
}
|
||||||
|
|
||||||
// ListNodeNetworkDevices retrieves a list of network devices for a specific nodes.
|
// ListNodeNetworkDevices retrieves a list of network devices for a specific nodes.
|
||||||
func (c *VirtualEnvironmentClient) ListNodeNetworkDevices(nodeName string) ([]*VirtualEnvironmentNodeNetworkDeviceListResponseData, error) {
|
func (c *VirtualEnvironmentClient) ListNodeNetworkDevices(nodeName string) ([]*VirtualEnvironmentNodeNetworkDeviceListResponseData, error) {
|
||||||
resBody := &VirtualEnvironmentNodeNetworkDeviceListResponseBody{}
|
resBody := &VirtualEnvironmentNodeNetworkDeviceListResponseBody{}
|
||||||
@ -136,3 +152,8 @@ func (c *VirtualEnvironmentClient) OpenNodeShell(nodeName string) (*ssh.Client,
|
|||||||
|
|
||||||
return sshClient, nil
|
return sshClient, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UpdateNodeTime updates the time on a node.
|
||||||
|
func (c *VirtualEnvironmentClient) UpdateNodeTime(nodeName string, d *VirtualEnvironmentNodeUpdateTimeRequestBody) error {
|
||||||
|
return c.DoRequest(hmPUT, fmt.Sprintf("nodes/%s/time", url.PathEscape(nodeName)), d, nil)
|
||||||
|
}
|
||||||
|
@ -17,6 +17,18 @@ type VirtualEnvironmentNodeExecuteRequestBody struct {
|
|||||||
Commands CustomNodeCommands `json:"commands" url:"commands"`
|
Commands CustomNodeCommands `json:"commands" url:"commands"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// VirtualEnvironmentNodeGetTimeResponseBody contains the body from a node time zone get response.
|
||||||
|
type VirtualEnvironmentNodeGetTimeResponseBody struct {
|
||||||
|
Data *VirtualEnvironmentNodeGetTimeResponseData `json:"data,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// VirtualEnvironmentNodeGetTimeResponseData contains the data from a node list response.
|
||||||
|
type VirtualEnvironmentNodeGetTimeResponseData struct {
|
||||||
|
LocalTime CustomTimestamp `json:"localtime"`
|
||||||
|
TimeZone string `json:"timezone"`
|
||||||
|
UTCTime CustomTimestamp `json:"time"`
|
||||||
|
}
|
||||||
|
|
||||||
// VirtualEnvironmentNodeListResponseBody contains the body from a node list response.
|
// VirtualEnvironmentNodeListResponseBody contains the body from a node list response.
|
||||||
type VirtualEnvironmentNodeListResponseBody struct {
|
type VirtualEnvironmentNodeListResponseBody struct {
|
||||||
Data []*VirtualEnvironmentNodeListResponseData `json:"data,omitempty"`
|
Data []*VirtualEnvironmentNodeListResponseData `json:"data,omitempty"`
|
||||||
@ -60,6 +72,11 @@ type VirtualEnvironmentNodeNetworkDeviceListResponseData struct {
|
|||||||
Type string `json:"type"`
|
Type string `json:"type"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// VirtualEnvironmentNodeUpdateTimeRequestBody contains the body for a node time update request.
|
||||||
|
type VirtualEnvironmentNodeUpdateTimeRequestBody struct {
|
||||||
|
TimeZone string `json:"timezone" url:"timezone"`
|
||||||
|
}
|
||||||
|
|
||||||
// EncodeValues converts a CustomNodeCommands array to a JSON encoded URL vlaue.
|
// EncodeValues converts a CustomNodeCommands array to a JSON encoded URL vlaue.
|
||||||
func (r CustomNodeCommands) EncodeValues(key string, v *url.Values) error {
|
func (r CustomNodeCommands) EncodeValues(key string, v *url.Values) error {
|
||||||
jsonArrayBytes, err := json.Marshal(r)
|
jsonArrayBytes, err := json.Marshal(r)
|
||||||
|
80
proxmoxtf/data_source_virtual_environment_time.go
Normal file
80
proxmoxtf/data_source_virtual_environment_time.go
Normal file
@ -0,0 +1,80 @@
|
|||||||
|
/* 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"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/hashicorp/terraform/helper/schema"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
mkDataSourceVirtualEnvironmentTimeLocalTime = "local_time"
|
||||||
|
mkDataSourceVirtualEnvironmentTimeNodeName = "node_name"
|
||||||
|
mkDataSourceVirtualEnvironmentTimeTimeZone = "time_zone"
|
||||||
|
mkDataSourceVirtualEnvironmentTimeUTCTime = "utc_time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func dataSourceVirtualEnvironmentTime() *schema.Resource {
|
||||||
|
return &schema.Resource{
|
||||||
|
Schema: map[string]*schema.Schema{
|
||||||
|
mkDataSourceVirtualEnvironmentTimeLocalTime: {
|
||||||
|
Type: schema.TypeString,
|
||||||
|
Description: "The local timestamp",
|
||||||
|
Computed: true,
|
||||||
|
},
|
||||||
|
mkDataSourceVirtualEnvironmentTimeNodeName: {
|
||||||
|
Type: schema.TypeString,
|
||||||
|
Description: "The node name",
|
||||||
|
Required: true,
|
||||||
|
},
|
||||||
|
mkDataSourceVirtualEnvironmentTimeTimeZone: {
|
||||||
|
Type: schema.TypeString,
|
||||||
|
Description: "The time zone",
|
||||||
|
Computed: true,
|
||||||
|
},
|
||||||
|
mkDataSourceVirtualEnvironmentTimeUTCTime: {
|
||||||
|
Type: schema.TypeString,
|
||||||
|
Description: "The UTC timestamp",
|
||||||
|
Computed: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Read: dataSourceVirtualEnvironmentTimeRead,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func dataSourceVirtualEnvironmentTimeRead(d *schema.ResourceData, m interface{}) error {
|
||||||
|
config := m.(providerConfiguration)
|
||||||
|
veClient, err := config.GetVEClient()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
nodeName := d.Get(mkDataSourceVirtualEnvironmentTimeNodeName).(string)
|
||||||
|
nodeTime, err := veClient.GetNodeTime(nodeName)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
localLocation, err := time.LoadLocation(nodeTime.TimeZone)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
d.SetId(fmt.Sprintf("%s_time", nodeName))
|
||||||
|
|
||||||
|
localTimeOffset := time.Time(nodeTime.LocalTime).Sub(time.Now().UTC())
|
||||||
|
localTime := time.Time(nodeTime.LocalTime).Add(-localTimeOffset).In(localLocation)
|
||||||
|
|
||||||
|
d.Set(mkDataSourceVirtualEnvironmentTimeLocalTime, localTime.Format(time.RFC3339))
|
||||||
|
d.Set(mkDataSourceVirtualEnvironmentTimeTimeZone, nodeTime.TimeZone)
|
||||||
|
d.Set(mkDataSourceVirtualEnvironmentTimeUTCTime, time.Time(nodeTime.UTCTime).Format(time.RFC3339))
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
42
proxmoxtf/data_source_virtual_environment_time_test.go
Normal file
42
proxmoxtf/data_source_virtual_environment_time_test.go
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
/* 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 (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/hashicorp/terraform/helper/schema"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TestDataSourceVirtualEnvironmentTimeInstantiation tests whether the DataSourceVirtualEnvironmentRoles instance can be instantiated.
|
||||||
|
func TestDataSourceVirtualEnvironmentTimeInstantiation(t *testing.T) {
|
||||||
|
s := dataSourceVirtualEnvironmentTime()
|
||||||
|
|
||||||
|
if s == nil {
|
||||||
|
t.Fatalf("Cannot instantiate dataSourceVirtualEnvironmentTime")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestDataSourceVirtualEnvironmentTimeSchema tests the dataSourceVirtualEnvironmentTime schema.
|
||||||
|
func TestDataSourceVirtualEnvironmentTimeSchema(t *testing.T) {
|
||||||
|
s := dataSourceVirtualEnvironmentTime()
|
||||||
|
|
||||||
|
testRequiredArguments(t, s, []string{
|
||||||
|
mkDataSourceVirtualEnvironmentTimeNodeName,
|
||||||
|
})
|
||||||
|
|
||||||
|
testComputedAttributes(t, s, []string{
|
||||||
|
mkDataSourceVirtualEnvironmentTimeLocalTime,
|
||||||
|
mkDataSourceVirtualEnvironmentTimeTimeZone,
|
||||||
|
mkDataSourceVirtualEnvironmentTimeUTCTime,
|
||||||
|
})
|
||||||
|
|
||||||
|
testValueTypes(t, s, map[string]schema.ValueType{
|
||||||
|
mkDataSourceVirtualEnvironmentTimeLocalTime: schema.TypeString,
|
||||||
|
mkDataSourceVirtualEnvironmentTimeNodeName: schema.TypeString,
|
||||||
|
mkDataSourceVirtualEnvironmentTimeTimeZone: schema.TypeString,
|
||||||
|
mkDataSourceVirtualEnvironmentTimeUTCTime: schema.TypeString,
|
||||||
|
})
|
||||||
|
}
|
@ -46,6 +46,7 @@ func Provider() *schema.Provider {
|
|||||||
"proxmox_virtual_environment_pools": dataSourceVirtualEnvironmentPools(),
|
"proxmox_virtual_environment_pools": dataSourceVirtualEnvironmentPools(),
|
||||||
"proxmox_virtual_environment_role": dataSourceVirtualEnvironmentRole(),
|
"proxmox_virtual_environment_role": dataSourceVirtualEnvironmentRole(),
|
||||||
"proxmox_virtual_environment_roles": dataSourceVirtualEnvironmentRoles(),
|
"proxmox_virtual_environment_roles": dataSourceVirtualEnvironmentRoles(),
|
||||||
|
"proxmox_virtual_environment_time": dataSourceVirtualEnvironmentTime(),
|
||||||
"proxmox_virtual_environment_user": dataSourceVirtualEnvironmentUser(),
|
"proxmox_virtual_environment_user": dataSourceVirtualEnvironmentUser(),
|
||||||
"proxmox_virtual_environment_users": dataSourceVirtualEnvironmentUsers(),
|
"proxmox_virtual_environment_users": dataSourceVirtualEnvironmentUsers(),
|
||||||
"proxmox_virtual_environment_version": dataSourceVirtualEnvironmentVersion(),
|
"proxmox_virtual_environment_version": dataSourceVirtualEnvironmentVersion(),
|
||||||
@ -59,6 +60,7 @@ func Provider() *schema.Provider {
|
|||||||
"proxmox_virtual_environment_hosts": resourceVirtualEnvironmentHosts(),
|
"proxmox_virtual_environment_hosts": resourceVirtualEnvironmentHosts(),
|
||||||
"proxmox_virtual_environment_pool": resourceVirtualEnvironmentPool(),
|
"proxmox_virtual_environment_pool": resourceVirtualEnvironmentPool(),
|
||||||
"proxmox_virtual_environment_role": resourceVirtualEnvironmentRole(),
|
"proxmox_virtual_environment_role": resourceVirtualEnvironmentRole(),
|
||||||
|
"proxmox_virtual_environment_time": resourceVirtualEnvironmentTime(),
|
||||||
"proxmox_virtual_environment_user": resourceVirtualEnvironmentUser(),
|
"proxmox_virtual_environment_user": resourceVirtualEnvironmentUser(),
|
||||||
"proxmox_virtual_environment_vm": resourceVirtualEnvironmentVM(),
|
"proxmox_virtual_environment_vm": resourceVirtualEnvironmentVM(),
|
||||||
},
|
},
|
||||||
|
126
proxmoxtf/resource_virtual_environment_time.go
Normal file
126
proxmoxtf/resource_virtual_environment_time.go
Normal file
@ -0,0 +1,126 @@
|
|||||||
|
/* 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"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/danitso/terraform-provider-proxmox/proxmox"
|
||||||
|
"github.com/hashicorp/terraform/helper/schema"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
mkResourceVirtualEnvironmentTimeLocalTime = "local_time"
|
||||||
|
mkResourceVirtualEnvironmentTimeNodeName = "node_name"
|
||||||
|
mkResourceVirtualEnvironmentTimeTimeZone = "time_zone"
|
||||||
|
mkResourceVirtualEnvironmentTimeUTCTime = "utc_time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func resourceVirtualEnvironmentTime() *schema.Resource {
|
||||||
|
return &schema.Resource{
|
||||||
|
Schema: map[string]*schema.Schema{
|
||||||
|
mkResourceVirtualEnvironmentTimeLocalTime: {
|
||||||
|
Type: schema.TypeString,
|
||||||
|
Description: "The local timestamp",
|
||||||
|
Computed: true,
|
||||||
|
},
|
||||||
|
mkResourceVirtualEnvironmentTimeNodeName: {
|
||||||
|
Type: schema.TypeString,
|
||||||
|
Description: "The node name",
|
||||||
|
Required: true,
|
||||||
|
},
|
||||||
|
mkResourceVirtualEnvironmentTimeTimeZone: {
|
||||||
|
Type: schema.TypeString,
|
||||||
|
Description: "The time zone",
|
||||||
|
Required: true,
|
||||||
|
},
|
||||||
|
mkResourceVirtualEnvironmentTimeUTCTime: {
|
||||||
|
Type: schema.TypeString,
|
||||||
|
Description: "The UTC timestamp",
|
||||||
|
Computed: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Create: resourceVirtualEnvironmentTimeCreate,
|
||||||
|
Read: resourceVirtualEnvironmentTimeRead,
|
||||||
|
Update: resourceVirtualEnvironmentTimeUpdate,
|
||||||
|
Delete: resourceVirtualEnvironmentTimeDelete,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func resourceVirtualEnvironmentTimeCreate(d *schema.ResourceData, m interface{}) error {
|
||||||
|
err := resourceVirtualEnvironmentTimeUpdate(d, m)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
nodeName := d.Get(mkResourceVirtualEnvironmentTimeNodeName).(string)
|
||||||
|
|
||||||
|
d.SetId(fmt.Sprintf("%s_time", nodeName))
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func resourceVirtualEnvironmentTimeRead(d *schema.ResourceData, m interface{}) error {
|
||||||
|
config := m.(providerConfiguration)
|
||||||
|
veClient, err := config.GetVEClient()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
nodeName := d.Get(mkResourceVirtualEnvironmentTimeNodeName).(string)
|
||||||
|
nodeTime, err := veClient.GetNodeTime(nodeName)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
localLocation, err := time.LoadLocation(nodeTime.TimeZone)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
d.SetId(fmt.Sprintf("%s_time", nodeName))
|
||||||
|
|
||||||
|
localTimeOffset := time.Time(nodeTime.LocalTime).Sub(time.Now().UTC())
|
||||||
|
localTime := time.Time(nodeTime.LocalTime).Add(-localTimeOffset).In(localLocation)
|
||||||
|
|
||||||
|
d.Set(mkDataSourceVirtualEnvironmentTimeLocalTime, localTime.Format(time.RFC3339))
|
||||||
|
d.Set(mkDataSourceVirtualEnvironmentTimeTimeZone, nodeTime.TimeZone)
|
||||||
|
d.Set(mkDataSourceVirtualEnvironmentTimeUTCTime, time.Time(nodeTime.UTCTime).Format(time.RFC3339))
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func resourceVirtualEnvironmentTimeUpdate(d *schema.ResourceData, m interface{}) error {
|
||||||
|
config := m.(providerConfiguration)
|
||||||
|
veClient, err := config.GetVEClient()
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
nodeName := d.Get(mkResourceVirtualEnvironmentTimeNodeName).(string)
|
||||||
|
timeZone := d.Get(mkResourceVirtualEnvironmentTimeTimeZone).(string)
|
||||||
|
|
||||||
|
err = veClient.UpdateNodeTime(nodeName, &proxmox.VirtualEnvironmentNodeUpdateTimeRequestBody{
|
||||||
|
TimeZone: timeZone,
|
||||||
|
})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return resourceVirtualEnvironmentTimeRead(d, m)
|
||||||
|
}
|
||||||
|
|
||||||
|
func resourceVirtualEnvironmentTimeDelete(d *schema.ResourceData, m interface{}) error {
|
||||||
|
d.SetId("")
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
42
proxmoxtf/resource_virtual_environment_time_test.go
Normal file
42
proxmoxtf/resource_virtual_environment_time_test.go
Normal file
@ -0,0 +1,42 @@
|
|||||||
|
/* 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 (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/hashicorp/terraform/helper/schema"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TestResourceVirtualEnvironmentTimeInstantiation tests whether the ResourceVirtualEnvironmentTime instance can be instantiated.
|
||||||
|
func TestResourceVirtualEnvironmentTimeInstantiation(t *testing.T) {
|
||||||
|
s := resourceVirtualEnvironmentTime()
|
||||||
|
|
||||||
|
if s == nil {
|
||||||
|
t.Fatalf("Cannot instantiate resourceVirtualEnvironmentTime")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestResourceVirtualEnvironmentTimeSchema tests the resourceVirtualEnvironmentTime schema.
|
||||||
|
func TestResourceVirtualEnvironmentTimeSchema(t *testing.T) {
|
||||||
|
s := resourceVirtualEnvironmentTime()
|
||||||
|
|
||||||
|
testRequiredArguments(t, s, []string{
|
||||||
|
mkResourceVirtualEnvironmentTimeNodeName,
|
||||||
|
mkResourceVirtualEnvironmentTimeTimeZone,
|
||||||
|
})
|
||||||
|
|
||||||
|
testComputedAttributes(t, s, []string{
|
||||||
|
mkResourceVirtualEnvironmentTimeLocalTime,
|
||||||
|
mkResourceVirtualEnvironmentTimeUTCTime,
|
||||||
|
})
|
||||||
|
|
||||||
|
testValueTypes(t, s, map[string]schema.ValueType{
|
||||||
|
mkResourceVirtualEnvironmentTimeLocalTime: schema.TypeString,
|
||||||
|
mkResourceVirtualEnvironmentTimeNodeName: schema.TypeString,
|
||||||
|
mkResourceVirtualEnvironmentTimeTimeZone: schema.TypeString,
|
||||||
|
mkResourceVirtualEnvironmentTimeUTCTime: schema.TypeString,
|
||||||
|
})
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user