github.com/sacloud/iaas-api-go@v1.12.0/types/proxylb_service_class.go (about)

     1  // Copyright 2022-2023 The sacloud/iaas-api-go Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package types
    16  
    17  import (
    18  	"strconv"
    19  	"strings"
    20  )
    21  
    22  const (
    23  	proxyLBServiceClassPrefix               = "cloud/proxylb/plain/"
    24  	proxyLBServiceClassAnycastPrefix        = "cloud/proxylb/anycast/"
    25  	proxyLBServiceClassPrefixEscaped        = "cloud\\/proxylb\\/plain\\/"
    26  	proxyLBServiceClassAnycastPrefixEscaped = "cloud\\/proxylb\\/anycast\\/"
    27  )
    28  
    29  // ProxyLBServiceClass プランとリージョンからサービスクラスを算出
    30  func ProxyLBServiceClass(plan EProxyLBPlan, region EProxyLBRegion) string {
    31  	switch region {
    32  	case ProxyLBRegions.Anycast:
    33  		return proxyLBServiceClassAnycastPrefix + plan.String()
    34  	default:
    35  		return proxyLBServiceClassPrefix + plan.String()
    36  	}
    37  }
    38  
    39  // ProxyLBPlanFromServiceClass サービスクラスからプランを算出
    40  func ProxyLBPlanFromServiceClass(serviceClass string) EProxyLBPlan {
    41  	strPlan := serviceClass
    42  	strPlan = strings.ReplaceAll(strPlan, `"`, "")
    43  	strPlan = strings.ReplaceAll(strPlan, proxyLBServiceClassPrefix, "")
    44  	strPlan = strings.ReplaceAll(strPlan, proxyLBServiceClassAnycastPrefix, "")
    45  	strPlan = strings.ReplaceAll(strPlan, proxyLBServiceClassPrefixEscaped, "")
    46  	strPlan = strings.ReplaceAll(strPlan, proxyLBServiceClassAnycastPrefixEscaped, "")
    47  
    48  	plan, err := strconv.Atoi(strPlan)
    49  	if err != nil {
    50  		plan = 0
    51  	}
    52  
    53  	return EProxyLBPlan(plan)
    54  }