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

     1  // Package powervs contains Power Virtual Servers-specific Terraform-variable logic.
     2  package powervs
     3  
     4  import (
     5  	"encoding/json"
     6  	"fmt"
     7  	"strings"
     8  
     9  	"github.com/IBM-Cloud/bluemix-go/crn"
    10  
    11  	machinev1 "github.com/openshift/api/machine/v1"
    12  	"github.com/openshift/installer/pkg/types"
    13  	powervstypes "github.com/openshift/installer/pkg/types/powervs"
    14  )
    15  
    16  type config struct {
    17  	APIKey                 string `json:"powervs_api_key"`
    18  	SSHKey                 string `json:"powervs_ssh_key"`
    19  	PowerVSRegion          string `json:"powervs_region"`
    20  	PowerVSZone            string `json:"powervs_zone"`
    21  	VPCRegion              string `json:"powervs_vpc_region"`
    22  	VPCZone                string `json:"powervs_vpc_zone"`
    23  	COSRegion              string `json:"powervs_cos_region"`
    24  	PowerVSResourceGroup   string `json:"powervs_resource_group"`
    25  	CISInstanceCRN         string `json:"powervs_cis_crn"`
    26  	DNSInstanceGUID        string `json:"powervs_dns_guid"`
    27  	ImageBucketName        string `json:"powervs_image_bucket_name"`
    28  	ImageBucketFileName    string `json:"powervs_image_bucket_file_name"`
    29  	VPCName                string `json:"powervs_vpc_name"`
    30  	VPCSubnetName          string `json:"powervs_vpc_subnet_name"`
    31  	VPCPermitted           bool   `json:"powervs_vpc_permitted"`
    32  	VPCGatewayName         string `json:"powervs_vpc_gateway_name"`
    33  	VPCGatewayAttached     bool   `json:"powervs_vpc_gateway_attached"`
    34  	BootstrapMemory        int32  `json:"powervs_bootstrap_memory"`
    35  	BootstrapProcessors    string `json:"powervs_bootstrap_processors"`
    36  	MasterMemory           int32  `json:"powervs_master_memory"`
    37  	MasterProcessors       string `json:"powervs_master_processors"`
    38  	ProcType               string `json:"powervs_proc_type"`
    39  	SysType                string `json:"powervs_sys_type"`
    40  	PublishStrategy        string `json:"powervs_publish_strategy"`
    41  	EnableSNAT             bool   `json:"powervs_enable_snat"`
    42  	AttachedTransitGateway string `json:"powervs_attached_transit_gateway"`
    43  	TGConnectionVPCID      string `json:"powervs_tg_connection_vpc_id"`
    44  	ServiceInstanceName    string `json:"powervs_service_instance_name"`
    45  }
    46  
    47  // TFVarsSources contains the parameters to be converted into Terraform variables
    48  type TFVarsSources struct {
    49  	MasterConfigs          []*machinev1.PowerVSMachineProviderConfig
    50  	APIKey                 string
    51  	SSHKey                 string
    52  	Region                 string
    53  	Zone                   string
    54  	ImageBucketName        string
    55  	ImageBucketFileName    string
    56  	PowerVSResourceGroup   string
    57  	CISInstanceCRN         string
    58  	DNSInstanceCRN         string
    59  	VPCRegion              string
    60  	VPCZone                string
    61  	VPCName                string
    62  	VPCSubnetName          string
    63  	VPCPermitted           bool
    64  	VPCGatewayName         string
    65  	VPCGatewayAttached     bool
    66  	PublishStrategy        types.PublishingStrategy
    67  	EnableSNAT             bool
    68  	AttachedTransitGateway string
    69  	TGConnectionVPCID      string
    70  	ServiceInstanceName    string
    71  }
    72  
    73  // TFVars generates Power VS-specific Terraform variables launching the cluster.
    74  func TFVars(sources TFVarsSources) ([]byte, error) {
    75  	masterConfig := sources.MasterConfigs[0]
    76  
    77  	cosRegion, err := powervstypes.COSRegionForVPCRegion(sources.VPCRegion)
    78  	if err != nil {
    79  		return nil, fmt.Errorf("failed to find COS region for VPC region")
    80  	}
    81  
    82  	var processor, dnsGUID string
    83  
    84  	if masterConfig.Processors.StrVal != "" {
    85  		processor = masterConfig.Processors.StrVal
    86  	} else {
    87  		processor = fmt.Sprintf("%d", masterConfig.Processors.IntVal)
    88  	}
    89  
    90  	// Parse GUID from DNS CRN
    91  	if sources.DNSInstanceCRN != "" {
    92  		dnsCRN, err := crn.Parse(sources.DNSInstanceCRN)
    93  		if err != nil {
    94  			return nil, fmt.Errorf("failed to parse DNSInstanceCRN")
    95  		}
    96  		dnsGUID = dnsCRN.ServiceInstance
    97  	}
    98  
    99  	cfg := &config{
   100  		APIKey:                 sources.APIKey,
   101  		SSHKey:                 sources.SSHKey,
   102  		PowerVSRegion:          sources.Region,
   103  		PowerVSZone:            sources.Zone,
   104  		VPCRegion:              sources.VPCRegion,
   105  		VPCZone:                sources.VPCZone,
   106  		COSRegion:              cosRegion,
   107  		PowerVSResourceGroup:   sources.PowerVSResourceGroup,
   108  		CISInstanceCRN:         sources.CISInstanceCRN,
   109  		DNSInstanceGUID:        dnsGUID,
   110  		ImageBucketName:        sources.ImageBucketName,
   111  		ImageBucketFileName:    sources.ImageBucketFileName,
   112  		VPCName:                sources.VPCName,
   113  		VPCSubnetName:          sources.VPCSubnetName,
   114  		VPCPermitted:           sources.VPCPermitted,
   115  		VPCGatewayName:         sources.VPCGatewayName,
   116  		VPCGatewayAttached:     sources.VPCGatewayAttached,
   117  		BootstrapMemory:        masterConfig.MemoryGiB,
   118  		BootstrapProcessors:    processor,
   119  		MasterMemory:           masterConfig.MemoryGiB,
   120  		MasterProcessors:       processor,
   121  		ProcType:               strings.ToLower(string(masterConfig.ProcessorType)),
   122  		SysType:                masterConfig.SystemType,
   123  		PublishStrategy:        string(sources.PublishStrategy),
   124  		EnableSNAT:             sources.EnableSNAT,
   125  		AttachedTransitGateway: sources.AttachedTransitGateway,
   126  		TGConnectionVPCID:      sources.TGConnectionVPCID,
   127  		ServiceInstanceName:    sources.ServiceInstanceName,
   128  	}
   129  
   130  	return json.MarshalIndent(cfg, "", "  ")
   131  }