github.com/GoogleCloudPlatform/terraformer@v0.8.18/providers/alicloud/connectivity/endpoint.go (about)

     1  package connectivity
     2  
     3  import (
     4  	"encoding/xml"
     5  	"fmt"
     6  	"io/ioutil"
     7  	"os"
     8  	"strings"
     9  )
    10  
    11  // Load endpoints from endpoints.xml or environment variables to meet specified application scenario, like private cloud.
    12  type ServiceCode string
    13  
    14  const (
    15  	ECSCode           = ServiceCode("ECS")
    16  	ESSCode           = ServiceCode("ESS")
    17  	RAMCode           = ServiceCode("RAM")
    18  	VPCCode           = ServiceCode("VPC")
    19  	SLBCode           = ServiceCode("SLB")
    20  	RDSCode           = ServiceCode("RDS")
    21  	OSSCode           = ServiceCode("OSS")
    22  	ONSCode           = ServiceCode("ONS")
    23  	ALIKAFKACode      = ServiceCode("ALIKAFKA")
    24  	CONTAINCode       = ServiceCode("CS")
    25  	CRCode            = ServiceCode("CR")
    26  	DOMAINCode        = ServiceCode("DOMAIN")
    27  	CDNCode           = ServiceCode("CDN")
    28  	CMSCode           = ServiceCode("CMS")
    29  	KMSCode           = ServiceCode("KMS")
    30  	OTSCode           = ServiceCode("OTS")
    31  	DNSCode           = ServiceCode("DNS")
    32  	PVTZCode          = ServiceCode("PVTZ")
    33  	LOGCode           = ServiceCode("LOG")
    34  	FCCode            = ServiceCode("FC")
    35  	DDSCode           = ServiceCode("DDS")
    36  	GPDBCode          = ServiceCode("GPDB")
    37  	STSCode           = ServiceCode("STS")
    38  	CENCode           = ServiceCode("CEN")
    39  	KVSTORECode       = ServiceCode("KVSTORE")
    40  	DATAHUBCode       = ServiceCode("DATAHUB")
    41  	MNSCode           = ServiceCode("MNS")
    42  	CLOUDAPICode      = ServiceCode("APIGATEWAY")
    43  	DRDSCode          = ServiceCode("DRDS")
    44  	LOCATIONCode      = ServiceCode("LOCATION")
    45  	ELASTICSEARCHCode = ServiceCode("ELASTICSEARCH")
    46  	NASCode           = ServiceCode("NAS")
    47  	ACTIONTRAILCode   = ServiceCode("ACTIONTRAIL")
    48  	BSSOPENAPICode    = ServiceCode("BSSOPENAPI")
    49  	DDOSCOOCode       = ServiceCode("DDOSCOO")
    50  	DDOSBGPCode       = ServiceCode("DDOSBGP")
    51  )
    52  
    53  // xml
    54  type Endpoints struct {
    55  	Endpoint []Endpoint `xml:"Endpoint"`
    56  }
    57  
    58  type Endpoint struct {
    59  	Name      string    `xml:"name,attr"`
    60  	RegionIds RegionIds `xml:"RegionIds"`
    61  	Products  Products  `xml:"Products"`
    62  }
    63  
    64  type RegionIds struct {
    65  	RegionID string `xml:"RegionId"`
    66  }
    67  
    68  type Products struct {
    69  	Product []Product `xml:"Product"`
    70  }
    71  
    72  type Product struct {
    73  	ProductName string `xml:"ProductName"`
    74  	DomainName  string `xml:"DomainName"`
    75  }
    76  
    77  func loadEndpoint(region string, serviceCode ServiceCode) string {
    78  	endpoint := strings.TrimSpace(os.Getenv(fmt.Sprintf("%s_ENDPOINT", string(serviceCode))))
    79  	if endpoint != "" {
    80  		return endpoint
    81  	}
    82  
    83  	// Load current path endpoint file endpoints.xml, if failed, it will load from environment variables TF_ENDPOINT_PATH
    84  	data, err := ioutil.ReadFile("./endpoints.xml")
    85  	if err != nil || len(data) == 0 {
    86  		d, e := ioutil.ReadFile(os.Getenv("TF_ENDPOINT_PATH"))
    87  		if e != nil {
    88  			return ""
    89  		}
    90  		data = d
    91  	}
    92  	var endpoints Endpoints
    93  	err = xml.Unmarshal(data, &endpoints)
    94  	if err != nil {
    95  		return ""
    96  	}
    97  	for _, endpoint := range endpoints.Endpoint {
    98  		if endpoint.RegionIds.RegionID == region {
    99  			for _, product := range endpoint.Products.Product {
   100  				if strings.EqualFold(product.ProductName, string(serviceCode)) {
   101  					return strings.TrimSpace(product.DomainName)
   102  				}
   103  			}
   104  		}
   105  	}
   106  
   107  	return ""
   108  }