diff --git a/vsphere/resource_vsphere_host.go b/vsphere/resource_vsphere_host.go index 8bea520f7..03630441b 100644 --- a/vsphere/resource_vsphere_host.go +++ b/vsphere/resource_vsphere_host.go @@ -136,13 +136,14 @@ func resourceVsphereHost() *schema.Resource { Type: schema.TypeString, Optional: 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'.", + Description: "The policy for the service. One of: `on`, `off`, or `automatic`.", }, "ntp_servers": { - Type: schema.TypeList, - Elem: &schema.Schema{Type: schema.TypeString}, - Optional: true, - Computed: true, + Type: schema.TypeList, + Elem: &schema.Schema{Type: schema.TypeString}, + Optional: true, + Computed: true, + Description: "List of NTP servers to use. Default is empty.", }, }, }, @@ -935,7 +936,6 @@ func resourceVSphereHostUpdateServices(d *schema.ResourceData, meta interface{}, if ntpd, ok := serviceMap["ntpd"]; ok { 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 { @@ -947,6 +947,11 @@ func resourceVSphereHostUpdateServices(d *schema.ResourceData, meta interface{}, // Update NTP servers interfaces := ntpdConfig["ntp_servers"].([]interface{}) + if len(interfaces) < 1 { + // Log, return an error, or handle as appropriate + // For example, returning an error: + return fmt.Errorf("no NTP servers specified for host %s", hostID) + } newServers := make([]string, len(interfaces)) for i, server := range interfaces { newServers[i] = server.(string) @@ -954,9 +959,10 @@ func resourceVSphereHostUpdateServices(d *schema.ResourceData, meta interface{}, err := changeHostNtpServers(context.Background(), hostObject, newServers) if err != nil { - return fmt.Errorf("error while updating NTP servers for host %s. Error: %s", hostID, err) + return fmt.Errorf("error updating NTP servers for host %s. Error: %s", hostID, err) } + updatedNtpdConfig := make(map[string]interface{}) // Copy ntpdConfig if needed before modifications // Update the ntpdConfig map before setting it updatedNtpdConfig["enabled"] = ntpdConfig["enabled"] updatedNtpdConfig["ntp_servers"] = newServers // Updated servers @@ -966,7 +972,7 @@ func resourceVSphereHostUpdateServices(d *schema.ResourceData, meta interface{}, if policy, ok := ntpdConfig["policy"].(string); ok { 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) + return fmt.Errorf("error updating ntpd service policy on host %s: %v", hostID, err) } } @@ -1010,7 +1016,6 @@ func readHostNtpServerConfig(ctx context.Context, client *govmomi.Client, hostOb return nil, fmt.Errorf("failed to get host configuration: %v", err) } - // Check if the dateTimeInfo configuration is available if hostSystem.Config == nil || hostSystem.Config.DateTimeInfo == nil { return nil, fmt.Errorf("dateTimeInfo configuration is not available on host") } @@ -1020,9 +1025,6 @@ func readHostNtpServerConfig(ctx context.Context, client *govmomi.Client, hostOb return nil, fmt.Errorf("NTP configuration is not available for the host") } - // Log the NTP servers found (optional) - fmt.Printf("NTP Servers for host: %v\n", hostSystem.Config.DateTimeInfo.NtpConfig.Server) - // Return the NTP servers return hostSystem.Config.DateTimeInfo.NtpConfig.Server, nil } @@ -1032,7 +1034,7 @@ func StartHostService(ctx context.Context, client *govmomi.Client, hostObject *o // 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) + return fmt.Errorf("error retrieving host service system: %v", err) } // Directly attempt to start the service without listing all services first @@ -1040,7 +1042,6 @@ func StartHostService(ctx context.Context, client *govmomi.Client, hostObject *o if err != nil { return fmt.Errorf("failed to start service %s: %v", serviceKey, err) } - fmt.Printf("Service %s started successfully on host\n", serviceKey) return nil } @@ -1049,14 +1050,13 @@ func UpdateHostServicePolicy(ctx context.Context, client *govmomi.Client, hostOb // 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) + return fmt.Errorf("error retrieving the host service system: %v", err) } err = serviceSystem.UpdatePolicy(ctx, serviceKey, policy) if err != nil { - return fmt.Errorf("failed to update policy for service %s: %v", serviceKey, err) + return fmt.Errorf("error updating policy for service %s: %v", serviceKey, err) } - fmt.Printf("Policy for service %s updated successfully on host\n", serviceKey) return nil } @@ -1066,7 +1066,7 @@ func readHostServicePolicy(ctx context.Context, client *govmomi.Client, hostObje var hostSystem mo.HostSystem err := hostObject.Properties(ctx, hostObject.Reference(), []string{"config.service"}, &hostSystem) if err != nil { - return "", fmt.Errorf("failed to get host configuration: %v", err) + return "", fmt.Errorf("error retrieving host configuration: %v", err) } // Check if the service configuration is available @@ -1078,7 +1078,6 @@ func readHostServicePolicy(ctx context.Context, client *govmomi.Client, hostObje for _, service := range hostSystem.Config.Service.Service { if service.Key == serviceKey { // Policy found for the specified service - fmt.Printf("Policy for service %s is %s\n", serviceKey, service.Policy) // Optional: log the found policy return service.Policy, nil } } @@ -1099,16 +1098,11 @@ func readHostServiceStatus(ctx context.Context, client *govmomi.Client, hostObje return false, fmt.Errorf("service configuration is not available on host") } - // Debug: Log the number of services found - fmt.Printf("Number of services found: %d\n", len(hostSystem.Config.Service.Service)) - // Iterate over the services to find the NTP service for _, service := range hostSystem.Config.Service.Service { // Debug: Log each service key encountered - fmt.Printf("Checking service: %s\n", service.Key) if service.Key == serviceKey { - fmt.Printf("Enabled for service %s is %t\n", serviceKey, service.Running) // Log the running status return service.Running, nil } }