github.com/openshift/installer@v1.4.17/pkg/tfvars/nutanix/nutanix.go (about)

     1  package nutanix
     2  
     3  import (
     4  	"encoding/json"
     5  
     6  	"github.com/pkg/errors"
     7  
     8  	machinev1 "github.com/openshift/api/machine/v1"
     9  	nutanixtypes "github.com/openshift/installer/pkg/types/nutanix"
    10  )
    11  
    12  type config struct {
    13  	PrismCentralAddress            string            `json:"nutanix_prism_central_address"`
    14  	Port                           string            `json:"nutanix_prism_central_port"`
    15  	Username                       string            `json:"nutanix_username"`
    16  	Password                       string            `json:"nutanix_password"`
    17  	MemoryMiB                      int64             `json:"nutanix_control_plane_memory_mib"`
    18  	DiskSizeMiB                    int64             `json:"nutanix_control_plane_disk_mib"`
    19  	NumCPUs                        int64             `json:"nutanix_control_plane_num_cpus"`
    20  	NumCoresPerSocket              int64             `json:"nutanix_control_plane_cores_per_socket"`
    21  	ProjectUUID                    string            `json:"nutanix_control_plane_project_uuid"`
    22  	Categories                     map[string]string `json:"nutanix_control_plane_categories"`
    23  	PrismElementUUIDs              []string          `json:"nutanix_prism_element_uuids"`
    24  	SubnetUUIDs                    []string          `json:"nutanix_subnet_uuids"`
    25  	Image                          string            `json:"nutanix_image"`
    26  	ImageURI                       string            `json:"nutanix_image_uri"`
    27  	BootstrapIgnitionImage         string            `json:"nutanix_bootstrap_ignition_image"`
    28  	BootstrapIgnitionImageFilePath string            `json:"nutanix_bootstrap_ignition_image_filepath"`
    29  }
    30  
    31  // TFVarsSources contains the parameters to be converted into Terraform variables
    32  type TFVarsSources struct {
    33  	PrismCentralAddress   string
    34  	Port                  string
    35  	Username              string
    36  	Password              string
    37  	ImageURI              string
    38  	BootstrapIgnitionData string
    39  	ClusterID             string
    40  	ControlPlaneConfigs   []*machinev1.NutanixMachineProviderConfig
    41  }
    42  
    43  // TFVars generate Nutanix-specific Terraform variables
    44  func TFVars(sources TFVarsSources) ([]byte, error) {
    45  	bootstrapIgnitionImagePath, err := nutanixtypes.CreateBootstrapISO(sources.ClusterID, sources.BootstrapIgnitionData)
    46  	if err != nil {
    47  		return nil, errors.Wrap(err, "failed to create bootstrap ignition iso")
    48  	}
    49  
    50  	bootstrapIgnitionImageName := nutanixtypes.BootISOImageName(sources.ClusterID)
    51  	controlPlaneConfig := sources.ControlPlaneConfigs[0]
    52  	cpCount := len(sources.ControlPlaneConfigs)
    53  	cfg := &config{
    54  		Port:                           sources.Port,
    55  		PrismCentralAddress:            sources.PrismCentralAddress,
    56  		Username:                       sources.Username,
    57  		Password:                       sources.Password,
    58  		MemoryMiB:                      controlPlaneConfig.MemorySize.Value() / (1024 * 1024),
    59  		DiskSizeMiB:                    controlPlaneConfig.SystemDiskSize.Value() / (1024 * 1024),
    60  		NumCPUs:                        int64(controlPlaneConfig.VCPUSockets),
    61  		NumCoresPerSocket:              int64(controlPlaneConfig.VCPUsPerSocket),
    62  		PrismElementUUIDs:              make([]string, cpCount),
    63  		SubnetUUIDs:                    make([]string, cpCount),
    64  		Image:                          *controlPlaneConfig.Image.Name,
    65  		ImageURI:                       sources.ImageURI,
    66  		BootstrapIgnitionImage:         bootstrapIgnitionImageName,
    67  		BootstrapIgnitionImageFilePath: bootstrapIgnitionImagePath,
    68  	}
    69  
    70  	for i, cpcfg := range sources.ControlPlaneConfigs {
    71  		cfg.PrismElementUUIDs[i] = *cpcfg.Cluster.UUID
    72  		cfg.SubnetUUIDs[i] = *cpcfg.Subnets[0].UUID
    73  	}
    74  
    75  	if controlPlaneConfig.Project.Type == machinev1.NutanixIdentifierUUID {
    76  		cfg.ProjectUUID = *controlPlaneConfig.Project.UUID
    77  	}
    78  	cfg.Categories = make(map[string]string, len(controlPlaneConfig.Categories))
    79  	for _, category := range controlPlaneConfig.Categories {
    80  		cfg.Categories[category.Key] = category.Value
    81  	}
    82  
    83  	return json.MarshalIndent(cfg, "", "  ")
    84  }