Skip to content

Commit

Permalink
Merge branch 'feature/added-ntpd-support-for-resource_vsphere_host' of
Browse files Browse the repository at this point in the history
…https://github.com/burnsjared0415/terraform-provider-vsphere into feature/added-ntpd-support-for-resource_vsphere_host
  • Loading branch information
burnsjared0415 committed Aug 14, 2024
2 parents 9382986 + ded1be8 commit 204ef75
Show file tree
Hide file tree
Showing 7 changed files with 380 additions and 216 deletions.
378 changes: 199 additions & 179 deletions CHANGELOG.md

Large diffs are not rendered by default.

11 changes: 6 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/davecgh/go-spew v1.1.1
github.com/hashicorp/terraform-plugin-sdk/v2 v2.34.0
github.com/mitchellh/copystructure v1.2.0
github.com/vmware/govmomi v0.38.0
github.com/vmware/govmomi v0.39.0
)

require (
Expand Down Expand Up @@ -49,11 +49,12 @@ require (
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
github.com/zclconf/go-cty v1.14.4 // indirect
golang.org/x/crypto v0.23.0 // indirect
golang.org/x/mod v0.16.0 // indirect
golang.org/x/net v0.23.0 // indirect
golang.org/x/mod v0.17.0 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/text v0.15.0 // indirect
golang.org/x/tools v0.13.0 // indirect
golang.org/x/text v0.16.0 // indirect
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240227224415-6ceb2ff114de // indirect
google.golang.org/grpc v1.63.2 // indirect
Expand Down
24 changes: 12 additions & 12 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ github.com/vmihailenco/msgpack/v5 v5.4.1 h1:cQriyiUvjTwOHg8QZaPihLWeRAAVoCpE00IU
github.com/vmihailenco/msgpack/v5 v5.4.1/go.mod h1:GaZTsDaehaPpQVyxrf5mtQlH+pc21PIudVV/E3rRQok=
github.com/vmihailenco/tagparser/v2 v2.0.0 h1:y09buUbR+b5aycVFQs/g70pqKVZNBmxwAhO7/IwNM9g=
github.com/vmihailenco/tagparser/v2 v2.0.0/go.mod h1:Wri+At7QHww0WTrCBeu4J6bNtoV6mEfg5OIWRZA9qds=
github.com/vmware/govmomi v0.38.0 h1:UvQpLAOjDpO0JUxoPCXnEzOlEa/9kejO6K58qOFr6cM=
github.com/vmware/govmomi v0.38.0/go.mod h1:mtGWtM+YhTADHlCgJBiskSRPOZRsN9MSjPzaZLte/oQ=
github.com/vmware/govmomi v0.39.0 h1:soLZ08Q2zvjRSinNup8xVlw0KDDCJPPA1rIDmBhi7As=
github.com/vmware/govmomi v0.39.0/go.mod h1:oHzAQ1r6152zYDGcUqeK+EO8LhKo5wjtvWZBGHws2Hc=
github.com/xanzy/ssh-agent v0.3.3 h1:+/15pJfg/RsTxqYcX6fHqOXZwwMP+2VyYWJeWM2qQFM=
github.com/xanzy/ssh-agent v0.3.3/go.mod h1:6dzNDKs0J9rVPHPhaGCukekBHKqfl+L3KghI1Bc68Uw=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
Expand All @@ -158,19 +158,19 @@ golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5y
golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI=
golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.16.0 h1:QX4fJ0Rr5cPQCF7O9lh9Se4pmwfwskqZfq5moyldzic=
golang.org/x/mod v0.16.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA=
golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs=
golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac=
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand All @@ -194,13 +194,13 @@ golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk=
golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.13.0 h1:Iey4qkscZuv0VvIt8E0neZjtPVQFSc870HQ448QgEmQ=
golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58=
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d h1:vU5i/LfpvrRCpgM/VPfJLg5KjxD3E+hfT1SH+d9zLwg=
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
Expand Down
49 changes: 35 additions & 14 deletions vsphere/resource_vsphere_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ var servicesPolicyAllowedValues = []string{
string(types.HostServicePolicyAutomatic),
}



func resourceVsphereHost() *schema.Resource {
return &schema.Resource{
Create: resourceVsphereHostCreate,
Expand Down Expand Up @@ -125,20 +123,19 @@ func resourceVsphereHost() *schema.Resource {
Schema: map[string]*schema.Schema{
"enabled": {
Type: schema.TypeBool,
Optional: true,
Required: true,
Description: "Whether the NTP service is enabled. Default is false.",
},
"policy": {
Type: schema.TypeString,
Optional: true,
Required: true,
ValidateFunc: validation.StringInSlice(servicesPolicyAllowedValues, false),
Description: "The policy for the NTP service. Valid values are 'Start and stop with host', 'Start and stop manually', 'Start and stop with port usage'.",
},
"ntp_servers": {
Type: schema.TypeList,
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
Computed: true,
Required: true,
},
},
},
Expand Down Expand Up @@ -933,11 +930,18 @@ func resourceVSphereHostUpdateServices(d *schema.ResourceData, meta interface{},
ntpdConfig := ntpd.([]interface{})[0].(map[string]interface{})
updatedNtpdConfig := make(map[string]interface{}) // Copy ntpdConfig if needed before modifications

// Start the NTP service if enabled
if enabled, ok := ntpdConfig["enabled"].(bool); ok && enabled {
err := StartHostService(context.Background(), client, hostObject, "ntpd")
if err != nil {
return fmt.Errorf("failed to start NTP service on host %s: %v", hostID, err)
// Start or stop the NTP service based on the enabled value
if enabled, ok := ntpdConfig["enabled"].(bool); ok {
if enabled {
err := startHostService(context.Background(), client, hostObject, "ntpd")
if err != nil {
return fmt.Errorf("failed to start NTP service on host %s: %v", hostID, err)
}
} else {
err := stopHostService(context.Background(), client, hostObject, "ntpd")
if err != nil {
return fmt.Errorf("failed to stop NTP service on host %s: %v", hostID, err)
}
}
}

Expand All @@ -960,7 +964,7 @@ func resourceVSphereHostUpdateServices(d *schema.ResourceData, meta interface{},

// Update the NTP service policy if applicable
if policy, ok := ntpdConfig["policy"].(string); ok {
err := UpdateHostServicePolicy(context.Background(), client, hostObject, "ntpd", policy)
err := updateHostServicePolicy(context.Background(), client, hostObject, "ntpd", policy)
if err != nil {
return fmt.Errorf("failed to update NTP service policy on host %s: %v", hostID, err)
}
Expand Down Expand Up @@ -1024,7 +1028,7 @@ func readHostNtpServerConfig(ctx context.Context, client *govmomi.Client, hostOb
}

// StartHostService starts a specified service on a host.
func StartHostService(ctx context.Context, client *govmomi.Client, hostObject *object.HostSystem, serviceKey string) error {
func startHostService(ctx context.Context, client *govmomi.Client, hostObject *object.HostSystem, serviceKey string) error {
// Retrieve the host's service system
serviceSystem, err := hostObject.ConfigManager().ServiceSystem(ctx)
if err != nil {
Expand All @@ -1040,8 +1044,25 @@ func StartHostService(ctx context.Context, client *govmomi.Client, hostObject *o
return nil
}

// StopHostService starts a specified service on a host.
func stopHostService(ctx context.Context, client *govmomi.Client, hostObject *object.HostSystem, serviceKey string) error {
// Retrieve the host's service system
serviceSystem, err := hostObject.ConfigManager().ServiceSystem(ctx)
if err != nil {
return fmt.Errorf("failed to get host service system: %v", err)
}

// Directly attempt to stop the service without listing all services first
err = serviceSystem.Stop(ctx, serviceKey)
if err != nil {
return fmt.Errorf("failed to stop service %s: %v", serviceKey, err)
}
fmt.Printf("Service %s stopped successfully on host\n", serviceKey)
return nil
}

// UpdateHostServicePolicy updates the policy of a specified service on a host.
func UpdateHostServicePolicy(ctx context.Context, client *govmomi.Client, hostObject *object.HostSystem, serviceKey, policy string) error {
func updateHostServicePolicy(ctx context.Context, client *govmomi.Client, hostObject *object.HostSystem, serviceKey, policy string) error {
// Retrieve the host's service system
serviceSystem, err := hostObject.ConfigManager().ServiceSystem(ctx)
if err != nil {
Expand Down
70 changes: 69 additions & 1 deletion vsphere/resource_vsphere_virtual_disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func resourceVSphereVirtualDisk() *schema.Resource {
return &schema.Resource{
Create: resourceVSphereVirtualDiskCreate,
Read: resourceVSphereVirtualDiskRead,
Update: resourceVSphereVirtualDiskUpdate,
Delete: resourceVSphereVirtualDiskDelete,
Importer: &schema.ResourceImporter{
State: resourceVSphereVirtualDiskImport,
Expand All @@ -48,7 +49,6 @@ func resourceVSphereVirtualDisk() *schema.Resource {
"size": {
Type: schema.TypeInt,
Required: true,
ForceNew: true, // TODO Can this be optional (resize)?
},

// TODO:
Expand Down Expand Up @@ -334,6 +334,51 @@ func resourceVSphereVirtualDiskRead(d *schema.ResourceData, meta interface{}) er
return nil
}

func resourceVSphereVirtualDiskUpdate(d *schema.ResourceData, meta interface{}) error {
log.Printf("[INFO] Updating Virtual Disk")
client := meta.(*Client).vimClient

oldSize, newSize := d.GetChange("size")
if newSize.(int) < oldSize.(int) {
return fmt.Errorf("shrinking a virtual disk is not supported")
}

vDisk := virtualDisk{
size: d.Get("size").(int),
}

if v, ok := d.GetOk("vmdk_path"); ok {
vDisk.vmdkPath = v.(string)
}

if v, ok := d.GetOk("datastore"); ok {
vDisk.datastore = v.(string)
}

if v, ok := d.GetOk("datacenter"); ok {
vDisk.datacenter = v.(string)
}

finder := find.NewFinder(client.Client, true)

dc, err := getDatacenter(client, d.Get("datacenter").(string))
if err != nil {
return fmt.Errorf("error finding Datacenter: %s: %s", vDisk.datacenter, err)
}
finder = finder.SetDatacenter(dc)

ds, err := getDatastore(finder, vDisk.datastore)
if err != nil {
return fmt.Errorf("error finding Datastore: %s: %s", vDisk.datastore, err)
}

if err := extendHardDisk(client, vDisk.size, ds.Path(vDisk.vmdkPath), vDisk.datacenter); err != nil {
return err
}

return resourceVSphereVirtualDiskRead(d, meta)
}

func resourceVSphereVirtualDiskDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Client).vimClient

Expand Down Expand Up @@ -425,6 +470,29 @@ func createHardDisk(client *govmomi.Client, size int, diskPath string, diskType
return nil
}

func extendHardDisk(client *govmomi.Client, capacity int, diskPath string, dc string) error {
virtualDiskManager := object.NewVirtualDiskManager(client.Client)
datacenter, err := getDatacenter(client, dc)
if err != nil {
return err
}

capacityKb := int64(1024 * 1024 * capacity)
task, err := virtualDiskManager.ExtendVirtualDisk(context.TODO(), diskPath, datacenter, capacityKb, nil)
if err != nil {
return err
}

_, err = task.WaitForResultEx(context.TODO(), nil)
if err != nil {
log.Printf("[INFO] Failed to extend disk: %v", err)
return err
}
log.Printf("[INFO] Extended disk.")

return nil
}

// Searches for the presence of a directory path.
func searchForDirectory(client *govmomi.Client, datacenter string, datastore string, directoryPath string) error {
log.Printf("[DEBUG] Searching for Directory")
Expand Down
56 changes: 54 additions & 2 deletions vsphere/resource_vsphere_virtual_disk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestAccResourceVSphereVirtualDisk_basic(t *testing.T) {
CheckDestroy: testAccVSphereVirtualDiskExists("vsphere_virtual_disk.foo", false),
Steps: []resource.TestStep{
{
Config: testacccheckvspherevirtuadiskconfigBasic(rString),
Config: testacccheckvspherevirtualdiskconfigBasic(rString),
Check: resource.ComposeTestCheckFunc(
testAccVSphereVirtualDiskExists("vsphere_virtual_disk.foo", true),
),
Expand All @@ -41,6 +41,36 @@ func TestAccResourceVSphereVirtualDisk_basic(t *testing.T) {
})
}

func TestAccResourceVSphereVirtualDisk_extend(t *testing.T) {
rString := acctest.RandString(5)

resource.Test(t, resource.TestCase{
PreCheck: func() {
RunSweepers()
testAccPreCheck(t)
testAccResourceVSphereVirtualDiskPreCheck(t)
},
Providers: testAccProviders,
CheckDestroy: testAccVSphereVirtualDiskExists("vsphere_virtual_disk.foo", false),
Steps: []resource.TestStep{
{
Config: testacccheckvspherevirtualdiskconfigBasic(rString),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"vsphere_virtual_disk.foo", "size", "1"),
),
},
{
Config: testacccheckvspherevirtualdiskconfigExtended(rString),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"vsphere_virtual_disk.foo", "size", "2"),
),
},
},
})
}

func TestAccResourceVSphereVirtualDisk_multi(t *testing.T) {
rString := acctest.RandString(5)

Expand Down Expand Up @@ -177,7 +207,7 @@ func testAccCheckVSphereVirtualDiskIsFileNotFoundError(err error) bool {
return false
}

func testacccheckvspherevirtuadiskconfigBasic(rName string) string {
func testacccheckvspherevirtualdiskconfigBasic(rName string) string {
return fmt.Sprintf(`
%s
Expand All @@ -199,6 +229,28 @@ resource "vsphere_virtual_disk" "foo" {
)
}

func testacccheckvspherevirtualdiskconfigExtended(rName string) string {
return fmt.Sprintf(`
%s
variable "rstring" {
default = "%s"
}
resource "vsphere_virtual_disk" "foo" {
size = 2
vmdk_path = "tfTestDisk-${var.rstring}.vmdk"
adapter_type = "lsiLogic"
type = "thin"
datacenter = "${data.vsphere_datacenter.rootdc1.name}"
datastore = vsphere_nas_datastore.ds1.name
}
`,
testhelper.CombineConfigs(testhelper.ConfigDataRootDC1(), testhelper.ConfigDataRootHost1(), testhelper.ConfigDataRootHost2(), testhelper.ConfigResDS1()),
rName,
)
}

func testacccheckvspherevirtuadiskconfigMulti(rName string) string {
return fmt.Sprintf(`
%s
Expand Down
8 changes: 5 additions & 3 deletions website/docs/r/virtual_disk.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,18 @@ resource "vsphere_virtual_disk" "virtual_disk" {

The following arguments are supported:

~> **NOTE:** All fields in the `vsphere_virtual_disk` resource are currently
~> **NOTE:** Some fields in the `vsphere_virtual_disk` resource are currently
immutable and force a new resource if changed.

* `vmdk_path` - (Required) The path, including filename, of the virtual disk to
be created. This needs to end in `.vmdk`.
* `datastore` - (Required) The name of the datastore in which to create the
disk.
* `size` - (Required) Size of the disk (in GB).
* `size` - (Required) Size of the disk (in GB). Decreasing the size of a disk is not possible.
If a disk of a smaller size is required then the original has to be destroyed along with its data and a new one has to be
created.
* `datacenter` - (Optional) The name of the datacenter in which to create the
disk. Can be omitted when when ESXi or if there is only one datacenter in
disk. Can be omitted when ESXi or if there is only one datacenter in
your infrastructure.
* `type` - (Optional) The type of disk to create. Can be one of
`eagerZeroedThick`, `lazy`, or `thin`. Default: `eagerZeroedThick`. For
Expand Down

0 comments on commit 204ef75

Please sign in to comment.