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 Jul 8, 2024
2 parents 5ad7980 + edce986 commit 72ca607
Showing 1 changed file with 18 additions and 24 deletions.
42 changes: 18 additions & 24 deletions vsphere/resource_vsphere_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
},
},
},
Expand Down Expand Up @@ -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 {
Expand All @@ -947,16 +947,22 @@ 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)
}

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
Expand All @@ -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)
}
}

Expand Down Expand Up @@ -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")
}
Expand All @@ -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
}
Expand All @@ -1032,15 +1034,14 @@ 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
err = serviceSystem.Start(ctx, serviceKey)
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
}

Expand All @@ -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
}

Expand All @@ -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
Expand All @@ -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
}
}
Expand All @@ -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
}
}
Expand Down

0 comments on commit 72ca607

Please sign in to comment.