<commit_before>package main

import (
	"fmt"
	"log"
	"net"
	"strings"

	"github.com/DimensionDataResearch/go-dd-cloud-compute/compute"
)

// ServerMetadata represents metadata for a CloudControl server.
type ServerMetadata struct {
	ID               string
	Name             string
	IPv4ByMACAddress map[string]net.IP

	// If specified, overrides the default PXE boot image.
	PXEBootImage string

	// If specified, overrides the default iPXE boot script URL (and IPXEProfile).
	IPXEBootScript string
}

// RefreshServerMetadata refreshes the map of MAC addresses to server metadata.
func (service *Service) RefreshServerMetadata() error {
	return service.refreshServerMetadataInternal(true)
}
func (service *Service) refreshServerMetadataInternal(acquireStateLock bool) error {
	serverMetadataByMACAddress, err := service.readServerMetadata()
	if err != nil {
		return err
	}

	if acquireStateLock {
		service.acquireStateLock("refreshServerMetadataInternal")
		defer service.releaseStateLock("refreshServerMetadataInternal")
	}
	service.ServerMetadataByMACAddress = serverMetadataByMACAddress

	return nil
}

// readServerMetadata creates a map of MAC addresses to server metadata from CloudControl.
func (service *Service) readServerMetadata() (map[string]ServerMetadata, error) {
	serverMetadataByMACAddress := make(map[string]ServerMetadata)

	page := compute.DefaultPaging()
	page.PageSize = 50

	for {
		servers, err := service.Client.ListServersInNetworkDomain(service.NetworkDomain.ID, page)
		if err != nil {
			return nil, err
		}
		if servers.IsEmpty() {
			break
		}

		for _, server := range servers.Items {
			// Ignore servers that are being deployed or destroyed.
			primaryNetworkAdapter := server.Network.PrimaryAdapter
			if primaryNetworkAdapter.PrivateIPv4Address == nil {
				continue
			}

			primaryMACAddress := strings.ToLower(
				*primaryNetworkAdapter.MACAddress,
			)
			serverMetadata := &ServerMetadata{
				ID:   server.ID,
				Name: server.Name,
				IPv4ByMACAddress: map[string]net.IP{
					primaryMACAddress: net.ParseIP(*primaryNetworkAdapter.PrivateIPv4Address),
				},
			}

			if service.EnableDebugLogging {
				log.Printf("\tMAC %s -> %s (%s)\n",
					primaryMACAddress,
					*primaryNetworkAdapter.PrivateIPv4Address,
					server.Name,
				)
			}

			// Enable overriding of PXE / iPXE metadata from tags.
			tagPage := compute.DefaultPaging()
			tagPage.PageSize = 50

			for {
				tags, err := service.Client.GetAssetTags(server.ID, compute.AssetTypeServer, tagPage)
				if err != nil {
					return nil, err
				}
				if tags.IsEmpty() {
					break
				}

				for _, tag := range tags.Items {
					switch tag.Name {
					case "pxe_boot_image":
						serverMetadata.PXEBootImage = tag.Value
					case "ipxe_profile":
						if serverMetadata.IPXEBootScript != "" {
							continue // ipxe_boot_script overrides ipxe_profile
						}

						serverMetadata.IPXEBootScript = fmt.Sprintf("http://%s/?profile=%s",
							service.ServiceIP,
							tag.Value,
						)
					case "ipxe_boot_script":
						serverMetadata.IPXEBootScript = tag.Value
					}
				}

				tagPage.Next()
			}

			for _, additionalNetworkAdapter := range server.Network.AdditionalNetworkAdapters {
				// Ignore network adapters that are being deployed or destroyed.
				if additionalNetworkAdapter.PrivateIPv4Address == nil {
					continue
				}

				additionalMACAddress := strings.ToLower(
					*additionalNetworkAdapter.MACAddress,
				)
				serverMetadata.IPv4ByMACAddress[additionalMACAddress] = net.ParseIP(*additionalNetworkAdapter.PrivateIPv4Address)

				if service.EnableDebugLogging {
					log.Printf("\tMAC address %s -> %s (%s)\n",
						additionalMACAddress,
						*additionalNetworkAdapter.PrivateIPv4Address,
						server.Name,
					)
				}
			}

			// Enable lookup by any MAC address.
			for macAddress := range serverMetadata.IPv4ByMACAddress {
				serverMetadataByMACAddress[macAddress] = *serverMetadata
			}
		}

		page.Next()
	}

	return serverMetadataByMACAddress, nil
}

// FindServerMetadataByMACAddress finds the metadata for the server (if any) posessing a network adapter with the specified MAC address.
func (service *Service) FindServerMetadataByMACAddress(macAddress string) *ServerMetadata {
	service.acquireStateLock("FindServerMetadataByMACAddress")
	defer service.releaseStateLock("FindServerMetadataByMACAddress")

	macAddress = strings.ToLower(macAddress)

	// Fake up metadata for a matching server if there's a matching static address reservation.
	staticReservation, ok := service.StaticReservationsByMACAddress[macAddress]
	if ok {
		serverMACAddress := macAddress
		serverPrivateIPv4Address := staticReservation.IPAddress

		return &ServerMetadata{
			ID:   staticReservation.HostName,
			Name: staticReservation.HostName,
			IPv4ByMACAddress: map[string]net.IP{
				serverMACAddress: serverPrivateIPv4Address,
			},
		}
	}

	serverMetadata, ok := service.ServerMetadataByMACAddress[macAddress]
	if ok {
		return &serverMetadata
	}<commit_msg>Refactor reading of server tag cache.<commit_after>package main

import (
	"fmt"
	"log"
	"net"
	"strings"

	"github.com/DimensionDataResearch/go-dd-cloud-compute/compute"
)

// ServerMetadata represents metadata for a CloudControl server.
type ServerMetadata struct {
	ID               string
	Name             string
	IPv4ByMACAddress map[string]net.IP

	// If specified, overrides the default PXE boot image.
	PXEBootImage string

	// If specified, overrides the default iPXE boot script URL (and IPXEProfile).
	IPXEBootScript string
}

// RefreshServerMetadata refreshes the map of MAC addresses to server metadata.
func (service *Service) RefreshServerMetadata() error {
	return service.refreshServerMetadataInternal(true)
}
func (service *Service) refreshServerMetadataInternal(acquireStateLock bool) error {
	serverMetadataByMACAddress, err := service.readServerMetadata()
	if err != nil {
		return err
	}

	if acquireStateLock {
		service.acquireStateLock("refreshServerMetadataInternal")
		defer service.releaseStateLock("refreshServerMetadataInternal")
	}
	service.ServerMetadataByMACAddress = serverMetadataByMACAddress

	return nil
}

// readServerMetadata creates a map of MAC addresses to server metadata from CloudControl.
func (service *Service) readServerMetadata() (map[string]ServerMetadata, error) {
	serverMetadataByMACAddress := make(map[string]ServerMetadata)

	page := compute.DefaultPaging()
	page.PageSize = 50

	for {
		servers, err := service.Client.ListServersInNetworkDomain(service.NetworkDomain.ID, page)
		if err != nil {
			return nil, err
		}
		if servers.IsEmpty() {
			break
		}

		for _, server := range servers.Items {
			// Ignore servers that are being deployed or destroyed.
			primaryNetworkAdapter := server.Network.PrimaryAdapter
			if primaryNetworkAdapter.PrivateIPv4Address == nil {
				continue
			}

			primaryMACAddress := strings.ToLower(
				*primaryNetworkAdapter.MACAddress,
			)
			serverMetadata := &ServerMetadata{
				ID:   server.ID,
				Name: server.Name,
				IPv4ByMACAddress: map[string]net.IP{
					primaryMACAddress: net.ParseIP(*primaryNetworkAdapter.PrivateIPv4Address),
				},
			}

			if service.EnableDebugLogging {
				log.Printf("\tMAC %s -> %s (%s)\n",
					primaryMACAddress,
					*primaryNetworkAdapter.PrivateIPv4Address,
					server.Name,
				)
			}

			for _, additionalNetworkAdapter := range server.Network.AdditionalNetworkAdapters {
				// Ignore network adapters that are being deployed or destroyed.
				if additionalNetworkAdapter.PrivateIPv4Address == nil {
					continue
				}

				additionalMACAddress := strings.ToLower(
					*additionalNetworkAdapter.MACAddress,
				)
				serverMetadata.IPv4ByMACAddress[additionalMACAddress] = net.ParseIP(*additionalNetworkAdapter.PrivateIPv4Address)

				if service.EnableDebugLogging {
					log.Printf("\tMAC address %s -> %s (%s)\n",
						additionalMACAddress,
						*additionalNetworkAdapter.PrivateIPv4Address,
						server.Name,
					)
				}
			}

			err = service.readServerTags(serverMetadata)
			if err != nil {
				return nil, err
			}

			// Enable lookup by any MAC address.
			for macAddress := range serverMetadata.IPv4ByMACAddress {
				serverMetadataByMACAddress[macAddress] = *serverMetadata
			}
		}

		page.Next()
	}

	return serverMetadataByMACAddress, nil
}

// Read and parse tags for the specified server.
func (service *Service) readServerTags(serverMetadata *ServerMetadata) error {
	// Enable overriding of PXE / iPXE metadata from tags.
	tagPage := compute.DefaultPaging()
	tagPage.PageSize = 50

	for {
		tags, err := service.Client.GetAssetTags(serverMetadata.ID, compute.AssetTypeServer, tagPage)
		if err != nil {
			if compute.IsAPIErrorCode(err, compute.ResponseCodeUnexpectedError) {
				break // CloudControl bug - going past last page of tags returns UNEXPECTED_ERROR (i.e. there are no more tags).
			}

			return err
		}
		if tags.IsEmpty() {
			break // No more tags.
		}

		for _, tag := range tags.Items {
			switch tag.Name {
			case "pxe_boot_image":
				serverMetadata.PXEBootImage = tag.Value
			case "ipxe_profile":
				if serverMetadata.IPXEBootScript != "" {
					continue // ipxe_boot_script overrides ipxe_profile
				}

				serverMetadata.IPXEBootScript = fmt.Sprintf("http://%s/?profile=%s",
					service.ServiceIP,
					tag.Value,
				)
			case "ipxe_boot_script":
				serverMetadata.IPXEBootScript = tag.Value
			}
		}

		tagPage.Next()
	}

	return nil
}

// FindServerMetadataByMACAddress finds the metadata for the server (if any) posessing a network adapter with the specified MAC address.
func (service *Service) FindServerMetadataByMACAddress(macAddress string) *ServerMetadata {
	service.acquireStateLock("FindServerMetadataByMACAddress")
	defer service.releaseStateLock("FindServerMetadataByMACAddress")

	macAddress = strings.ToLower(macAddress)

	// Fake up metadata for a matching server if there's a matching static address reservation.
	staticReservation, ok := service.StaticReservationsByMACAddress[macAddress]
	if ok {
		serverMACAddress := macAddress
		serverPrivateIPv4Address := staticReservation.IPAddress

		return &ServerMetadata{
			ID:   staticReservation.HostName,
			Name: staticReservation.HostName,
			IPv4ByMACAddress: map[string]net.IP{
				serverMACAddress: serverPrivateIPv4Address,
			},
		}
	}

	serverMetadata, ok := service.ServerMetadataByMACAddress[macAddress]
	if ok {
		return &serverMetadata
	}
{'fraction_non_alphanumeric': 0.08837656099903939, 'fraction_numerical': 0.003266090297790586, 'mean_word_length': 3.9411485524442336, 'pattern_counts': {'":': 6, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6484451', 'n_tokens_mistral': 3590, 'n_tokens_neox': 3011, 'n_words': 723}