mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-01 15:07:17 +08:00
fix(region): gcp global eip and lb (#20736)
This commit is contained in:
@@ -89,7 +89,7 @@ require (
|
||||
k8s.io/cluster-bootstrap v0.19.3
|
||||
k8s.io/cri-api v0.22.17
|
||||
moul.io/http2curl/v2 v2.3.0
|
||||
yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20240701025407-fcc1a0b725c1
|
||||
yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20240703084949-3f9b2bdc2cdc
|
||||
yunion.io/x/executor v0.0.0-20230705125604-c5ac3141db32
|
||||
yunion.io/x/jsonutils v1.0.1-0.20240203102553-4096f103b401
|
||||
yunion.io/x/log v1.0.1-0.20240305175729-7cf2d6cd5a91
|
||||
|
||||
@@ -1215,8 +1215,8 @@ sigs.k8s.io/structured-merge-diff/v4 v4.0.1/go.mod h1:bJZC9H9iH24zzfZ/41RGcq60oK
|
||||
sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o=
|
||||
sigs.k8s.io/yaml v1.2.0 h1:kr/MCeFWJWTwyaHoR9c8EjH9OumOmoF9YGiZd7lFm/Q=
|
||||
sigs.k8s.io/yaml v1.2.0/go.mod h1:yfXDCHCao9+ENCvLSE62v9VSji2MKu5jeNfTrofGhJc=
|
||||
yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20240701025407-fcc1a0b725c1 h1:PtCUfpzyF+JHU+A3vQrt7bGirqXDFiPSUT6sFq3ewac=
|
||||
yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20240701025407-fcc1a0b725c1/go.mod h1:iLoBHVR2Eur/1WJSGcbZaEwpzh/iqXvbFCsX9/xt8CI=
|
||||
yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20240703084949-3f9b2bdc2cdc h1:XfDCeyk0iq0t52x5bVIiTA+REk6TTEg3vkjK/M5HLBM=
|
||||
yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20240703084949-3f9b2bdc2cdc/go.mod h1:iLoBHVR2Eur/1WJSGcbZaEwpzh/iqXvbFCsX9/xt8CI=
|
||||
yunion.io/x/executor v0.0.0-20230705125604-c5ac3141db32 h1:v7POYkQwo1XzOxBoIoRVr/k0V9Y5JyjpshlIFa9raug=
|
||||
yunion.io/x/executor v0.0.0-20230705125604-c5ac3141db32/go.mod h1:Uxuou9WQIeJXNpy7t2fPLL0BYLvLiMvGQwY7Qc6aSws=
|
||||
yunion.io/x/jsonutils v0.0.0-20190625054549-a964e1e8a051/go.mod h1:4N0/RVzsYL3kH3WE/H1BjUQdFiWu50JGCFQuuy+Z634=
|
||||
|
||||
Vendored
+1
-1
@@ -1489,7 +1489,7 @@ sigs.k8s.io/structured-merge-diff/v4/value
|
||||
# sigs.k8s.io/yaml v1.2.0
|
||||
## explicit; go 1.12
|
||||
sigs.k8s.io/yaml
|
||||
# yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20240701025407-fcc1a0b725c1
|
||||
# yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20240703084949-3f9b2bdc2cdc
|
||||
## explicit; go 1.18
|
||||
yunion.io/x/cloudmux/pkg/apis
|
||||
yunion.io/x/cloudmux/pkg/apis/billing
|
||||
|
||||
+184
@@ -0,0 +1,184 @@
|
||||
// Copyright 2019 Yunion
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package google
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"yunion.io/x/log"
|
||||
|
||||
api "yunion.io/x/cloudmux/pkg/apis/compute"
|
||||
"yunion.io/x/cloudmux/pkg/cloudprovider"
|
||||
"yunion.io/x/cloudmux/pkg/multicloud"
|
||||
)
|
||||
|
||||
type SGlobalAddress struct {
|
||||
region *SGlobalRegion
|
||||
SResourceBase
|
||||
multicloud.SEipBase
|
||||
GoogleTags
|
||||
|
||||
CreationTimestamp time.Time
|
||||
Description string
|
||||
Address string
|
||||
Status string
|
||||
Region string
|
||||
Users []string
|
||||
NetworkTier string
|
||||
AddressType string
|
||||
Kind string
|
||||
}
|
||||
|
||||
func (addr *SGlobalAddress) GetStatus() string {
|
||||
switch addr.Status {
|
||||
case "RESERVING":
|
||||
return api.EIP_STATUS_ASSOCIATE
|
||||
case "RESERVED":
|
||||
return api.EIP_STATUS_READY
|
||||
case "IN_USE":
|
||||
return api.EIP_STATUS_READY
|
||||
default:
|
||||
log.Errorf("Unknown eip status: %s", addr.Status)
|
||||
return api.EIP_STATUS_UNKNOWN
|
||||
}
|
||||
}
|
||||
|
||||
func (addr *SGlobalAddress) GetIpAddr() string {
|
||||
return addr.Address
|
||||
}
|
||||
|
||||
func (addr *SGlobalAddress) GetMode() string {
|
||||
if addr.IsEmulated() {
|
||||
return api.EIP_MODE_INSTANCE_PUBLICIP
|
||||
}
|
||||
return api.EIP_MODE_STANDALONE_EIP
|
||||
}
|
||||
|
||||
func (addr *SGlobalAddress) GetBandwidth() int {
|
||||
return 0
|
||||
}
|
||||
|
||||
func (addr *SGlobalAddress) GetInternetChargeType() string {
|
||||
return api.EIP_CHARGE_TYPE_BY_TRAFFIC
|
||||
}
|
||||
|
||||
func (addr *SGlobalAddress) Delete() error {
|
||||
return addr.region.Delete(addr.SelfLink)
|
||||
}
|
||||
|
||||
func (addr *SGlobalAddress) Associate(conf *cloudprovider.AssociateConfig) error {
|
||||
return cloudprovider.ErrNotImplemented
|
||||
}
|
||||
|
||||
func (addr *SGlobalAddress) Dissociate() error {
|
||||
return cloudprovider.ErrNotImplemented
|
||||
}
|
||||
|
||||
func (addr *SGlobalAddress) ChangeBandwidth(bw int) error {
|
||||
return cloudprovider.ErrNotSupported
|
||||
}
|
||||
|
||||
func (addr *SGlobalAddress) GetProjectId() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (region *SGlobalRegion) GetEips(address string) ([]SGlobalAddress, error) {
|
||||
eips := []SGlobalAddress{}
|
||||
params := map[string]string{}
|
||||
if len(address) > 0 {
|
||||
params["filter"] = fmt.Sprintf(`address="%s"`, address)
|
||||
}
|
||||
resource := "global/addresses"
|
||||
|
||||
err := region.ListAll(resource, params, &eips)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for i := range eips {
|
||||
eips[i].region = region
|
||||
}
|
||||
return eips, nil
|
||||
}
|
||||
|
||||
func (region *SGlobalRegion) GetIEips() ([]cloudprovider.ICloudEIP, error) {
|
||||
eips, err := region.GetEips("")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
ret := []cloudprovider.ICloudEIP{}
|
||||
for i := range eips {
|
||||
eips[i].region = region
|
||||
ret = append(ret, &eips[i])
|
||||
}
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func (region *SGlobalRegion) GetEip(id string) (*SGlobalAddress, error) {
|
||||
eip := &SGlobalAddress{region: region}
|
||||
return eip, region.Get("addresses", id, eip)
|
||||
}
|
||||
|
||||
func (region *SGlobalRegion) GetIEipById(id string) (cloudprovider.ICloudEIP, error) {
|
||||
eip, err := region.GetEip(id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return eip, nil
|
||||
}
|
||||
|
||||
func (region *SGlobalRegion) CreateEIP(args *cloudprovider.SEip) (cloudprovider.ICloudEIP, error) {
|
||||
return nil, cloudprovider.ErrNotImplemented
|
||||
}
|
||||
|
||||
func (addr *SGlobalAddress) GetAssociationExternalId() string {
|
||||
associateType := addr.GetAssociationType()
|
||||
for _, user := range addr.Users {
|
||||
if associateType == api.EIP_ASSOCIATE_TYPE_LOADBALANCER {
|
||||
forword := &SForwardingRule{}
|
||||
err := addr.region.GetBySelfId(user, forword)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
proxy := &STargetHttpProxy{}
|
||||
err = addr.region.GetBySelfId(forword.Target, proxy)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return getGlobalId(proxy.URLMap)
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (addr *SGlobalAddress) GetAssociationType() string {
|
||||
for _, user := range addr.Users {
|
||||
if strings.Contains(user, "global/forwardingRules") {
|
||||
return api.EIP_ASSOCIATE_TYPE_LOADBALANCER
|
||||
}
|
||||
return api.EIP_ASSOCIATE_TYPE_SERVER
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (region *SGlobalRegion) listAll(method string, resource string, params map[string]string, retval interface{}) error {
|
||||
return region.client._ecsListAll(method, resource, params, retval)
|
||||
}
|
||||
|
||||
func (region *SGlobalRegion) ListAll(resource string, params map[string]string, retval interface{}) error {
|
||||
return region.listAll("GET", resource, params, retval)
|
||||
}
|
||||
+420
@@ -0,0 +1,420 @@
|
||||
// Copyright 2019 Yunion
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package google
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"yunion.io/x/log"
|
||||
"yunion.io/x/pkg/errors"
|
||||
"yunion.io/x/pkg/utils"
|
||||
|
||||
api "yunion.io/x/cloudmux/pkg/apis/compute"
|
||||
"yunion.io/x/cloudmux/pkg/cloudprovider"
|
||||
"yunion.io/x/cloudmux/pkg/multicloud"
|
||||
)
|
||||
|
||||
type SGlobalLoadbalancer struct {
|
||||
multicloud.SLoadbalancerBase
|
||||
GoogleTags
|
||||
SResourceBase
|
||||
region *SGlobalRegion
|
||||
urlMap *SUrlMap // http & https LB
|
||||
backendServices []SBackendServices // tcp & udp LB. 或者 http & https 后端
|
||||
instanceGroups []SGlobalInstanceGroup
|
||||
healthChecks []HealthChecks
|
||||
|
||||
forwardRules []SForwardingRule // 服务IP地址
|
||||
isHttpLb bool // 标记是否为http/https lb
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancer) GetStatus() string {
|
||||
return api.LB_STATUS_ENABLED
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancer) Refresh() error {
|
||||
return cloudprovider.ErrNotImplemented
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancer) GetNetworkType() string {
|
||||
return api.LB_NETWORK_TYPE_VPC
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancer) GetVpcId() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancer) GetZoneId() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancer) GetZone1Id() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancer) GetChargeType() string {
|
||||
return api.LB_CHARGE_TYPE_BY_TRAFFIC
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancer) GetEgressMbps() int {
|
||||
return 0
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancer) Delete(ctx context.Context) error {
|
||||
return cloudprovider.ErrNotImplemented
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancer) Start() error {
|
||||
return cloudprovider.ErrNotSupported
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancer) Stop() error {
|
||||
return cloudprovider.ErrNotSupported
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancer) GetSysTags() map[string]string {
|
||||
frs, err := self.GetForwardingRules()
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
ips := []string{}
|
||||
for i := range frs {
|
||||
if len(frs[i].IPAddress) > 0 && !utils.IsInStringArray(frs[i].IPAddress, ips) {
|
||||
ips = append(ips, frs[i].IPAddress)
|
||||
}
|
||||
}
|
||||
data := map[string]string{}
|
||||
data["FrontendIPs"] = strings.Join(ips, ",")
|
||||
return data
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancer) GetProjectId() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancer) GetIEIP() (cloudprovider.ICloudEIP, error) {
|
||||
frs, err := self.GetForwardingRules()
|
||||
if err != nil {
|
||||
log.Errorf("GetAddress.GetForwardingRules %s", err)
|
||||
}
|
||||
|
||||
for i := range frs {
|
||||
if strings.ToLower(frs[i].LoadBalancingScheme) == "external" {
|
||||
eips, err := self.region.GetEips(frs[i].IPAddress)
|
||||
if err != nil {
|
||||
log.Errorf("GetEips %s", err)
|
||||
}
|
||||
|
||||
if len(eips) > 0 {
|
||||
return &eips[0], nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancer) GetAddress() string {
|
||||
var ret []SForwardingRule
|
||||
if err := self.region.getGlobalLoadbalancerComponents("forwardingRules", "", &ret); err != nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
for _, item := range ret {
|
||||
targetResponse, err := _jsonRequest(self.region.client.client, "GET", item.Target, nil, false)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
var target interface{}
|
||||
switch {
|
||||
case strings.Contains(item.Target, "targetHttpProxies"):
|
||||
target = new(STargetHttpProxy)
|
||||
case strings.Contains(item.Target, "targetHttpsProxies"):
|
||||
target = new(STargetHttpsProxy)
|
||||
case strings.Contains(item.Target, "targetTcpProxies"):
|
||||
target = new(STargetTcpProxy)
|
||||
default:
|
||||
continue
|
||||
}
|
||||
|
||||
if err := targetResponse.Unmarshal(target); err != nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
if self.isNameMatch(target) {
|
||||
return item.IPAddress
|
||||
}
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancer) GetAddressType() string {
|
||||
frs, err := self.GetForwardingRules()
|
||||
if err != nil {
|
||||
return api.LB_ADDR_TYPE_INTRANET
|
||||
}
|
||||
|
||||
for i := range frs {
|
||||
sche := strings.ToLower(frs[i].LoadBalancingScheme)
|
||||
if !utils.IsInStringArray(sche, []string{"invalid", "undefinedloadbalancingscheme"}) {
|
||||
if sche == "external" {
|
||||
return api.LB_ADDR_TYPE_INTERNET
|
||||
} else {
|
||||
return api.LB_ADDR_TYPE_INTRANET
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return api.LB_ADDR_TYPE_INTERNET
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancer) GetNetworkIds() []string {
|
||||
igs, err := self.GetInstanceGroups()
|
||||
if err != nil {
|
||||
log.Errorf("GetInstanceGroups %s", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
selfLinks := make([]string, 0)
|
||||
networkIds := make([]string, 0)
|
||||
for i := range igs {
|
||||
if utils.IsInStringArray(igs[i].Subnetwork, selfLinks) {
|
||||
selfLinks = append(selfLinks, igs[i].Subnetwork)
|
||||
network := SResourceBase{
|
||||
Name: "",
|
||||
SelfLink: igs[i].Network,
|
||||
}
|
||||
networkIds = append(networkIds, network.GetGlobalId())
|
||||
}
|
||||
}
|
||||
|
||||
return networkIds
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancer) GetLoadbalancerSpec() string {
|
||||
if self.isHttpLb {
|
||||
return "global_http_lb"
|
||||
}
|
||||
|
||||
return fmt.Sprintf("global_%s", strings.ToLower(self.backendServices[0].Protocol))
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancer) GetILoadBalancerListeners() ([]cloudprovider.ICloudLoadbalancerListener, error) {
|
||||
lbls, err := self.GetLoadbalancerListeners()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "GetLoadbalancerListeners")
|
||||
}
|
||||
|
||||
ilbls := make([]cloudprovider.ICloudLoadbalancerListener, len(lbls))
|
||||
for i := range lbls {
|
||||
ilbls[i] = &lbls[i]
|
||||
}
|
||||
|
||||
return ilbls, nil
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancer) GetILoadBalancerBackendGroups() ([]cloudprovider.ICloudLoadbalancerBackendGroup, error) {
|
||||
lbbgs, err := self.GetLoadbalancerBackendGroups()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "GetLoadbalancerBackendGroups")
|
||||
}
|
||||
|
||||
ilbbgs := make([]cloudprovider.ICloudLoadbalancerBackendGroup, len(lbbgs))
|
||||
for i := range lbbgs {
|
||||
ilbbgs[i] = &lbbgs[i]
|
||||
}
|
||||
|
||||
return ilbbgs, nil
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancer) CreateILoadBalancerBackendGroup(group *cloudprovider.SLoadbalancerBackendGroup) (cloudprovider.ICloudLoadbalancerBackendGroup, error) {
|
||||
return nil, cloudprovider.ErrNotSupported
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancer) GetILoadBalancerBackendGroupById(groupId string) (cloudprovider.ICloudLoadbalancerBackendGroup, error) {
|
||||
lbbgs, err := self.GetLoadbalancerBackendGroups()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "GetLoadbalancerBackendGroups")
|
||||
}
|
||||
|
||||
for i := range lbbgs {
|
||||
if lbbgs[i].GetGlobalId() == groupId {
|
||||
return &lbbgs[i], nil
|
||||
}
|
||||
}
|
||||
|
||||
return nil, cloudprovider.ErrNotFound
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancer) CreateILoadBalancerListener(ctx context.Context, listener *cloudprovider.SLoadbalancerListenerCreateOptions) (cloudprovider.ICloudLoadbalancerListener, error) {
|
||||
return nil, cloudprovider.ErrNotSupported
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancer) GetILoadBalancerListenerById(listenerId string) (cloudprovider.ICloudLoadbalancerListener, error) {
|
||||
lbls, err := self.GetLoadbalancerListeners()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "GetLoadbalancerBackendGroups")
|
||||
}
|
||||
|
||||
for i := range lbls {
|
||||
if lbls[i].GetGlobalId() == listenerId {
|
||||
return &lbls[i], nil
|
||||
}
|
||||
}
|
||||
|
||||
return nil, cloudprovider.ErrNotFound
|
||||
}
|
||||
|
||||
func (self *SGlobalRegion) getGlobalLoadbalancerComponents(resource string, filter string, result interface{}) error {
|
||||
url := fmt.Sprintf("global/%s", resource)
|
||||
params := map[string]string{}
|
||||
if len(filter) > 0 {
|
||||
params["filter"] = filter
|
||||
}
|
||||
|
||||
err := self.ListAll(url, params, result)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "ListAll")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self *SGlobalRegion) GetGlobalHTTPLoadbalancers() ([]SGlobalLoadbalancer, error) {
|
||||
ums, err := self.GetGlobalUrlMaps("")
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "GetGlobalUrlMaps")
|
||||
}
|
||||
|
||||
lbs := make([]SGlobalLoadbalancer, len(ums))
|
||||
for i := range ums {
|
||||
lbs[i] = SGlobalLoadbalancer{
|
||||
region: self,
|
||||
SResourceBase: SResourceBase{
|
||||
Name: ums[i].Name,
|
||||
SelfLink: ums[i].SelfLink,
|
||||
},
|
||||
urlMap: &ums[i],
|
||||
backendServices: nil,
|
||||
forwardRules: nil,
|
||||
isHttpLb: true,
|
||||
}
|
||||
}
|
||||
|
||||
return lbs, nil
|
||||
}
|
||||
|
||||
func (self *SGlobalRegion) GetGlobalTcpLoadbalancers() ([]SGlobalLoadbalancer, error) {
|
||||
bss, err := self.GetGlobalBackendServices("protocol eq TCP")
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "GetGlobalBackendServices")
|
||||
}
|
||||
|
||||
lbs := make([]SGlobalLoadbalancer, len(bss))
|
||||
for i := range bss {
|
||||
lbs[i] = SGlobalLoadbalancer{
|
||||
region: self,
|
||||
SResourceBase: SResourceBase{
|
||||
Name: bss[i].Name,
|
||||
SelfLink: bss[i].SelfLink,
|
||||
},
|
||||
urlMap: nil,
|
||||
backendServices: []SBackendServices{bss[i]},
|
||||
forwardRules: nil,
|
||||
}
|
||||
}
|
||||
|
||||
return lbs, nil
|
||||
}
|
||||
|
||||
func (self *SGlobalRegion) GetGlobalUdpLoadbalancers() ([]SGlobalLoadbalancer, error) {
|
||||
bss, err := self.GetGlobalBackendServices("protocol eq UDP")
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "GetGlobalBackendServices")
|
||||
}
|
||||
|
||||
lbs := make([]SGlobalLoadbalancer, len(bss))
|
||||
for i := range bss {
|
||||
lbs[i] = SGlobalLoadbalancer{
|
||||
region: self,
|
||||
SResourceBase: SResourceBase{
|
||||
Name: bss[i].Name,
|
||||
SelfLink: bss[i].SelfLink,
|
||||
},
|
||||
urlMap: nil,
|
||||
backendServices: []SBackendServices{bss[i]},
|
||||
forwardRules: nil,
|
||||
}
|
||||
}
|
||||
|
||||
return lbs, nil
|
||||
}
|
||||
|
||||
func (self *SGlobalRegion) GetGlobalLoadbalancers() ([]SGlobalLoadbalancer, error) {
|
||||
lbs := make([]SGlobalLoadbalancer, 0)
|
||||
funcs := []func() ([]SGlobalLoadbalancer, error){self.GetGlobalHTTPLoadbalancers, self.GetGlobalTcpLoadbalancers, self.GetGlobalUdpLoadbalancers}
|
||||
for i := range funcs {
|
||||
_lbs, err := funcs[i]()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "GetGlobalLoadbalancers")
|
||||
}
|
||||
lbs = append(lbs, _lbs...)
|
||||
}
|
||||
|
||||
return lbs, nil
|
||||
}
|
||||
|
||||
func (self *SGlobalRegion) GetLoadbalancer(resourceId string) (SGlobalLoadbalancer, error) {
|
||||
lb := SGlobalLoadbalancer{}
|
||||
var err error
|
||||
if strings.Contains(resourceId, "/urlMaps/") {
|
||||
ret := SUrlMap{}
|
||||
err = self.GetBySelfId(resourceId, &ret)
|
||||
lb.isHttpLb = true
|
||||
lb.urlMap = &ret
|
||||
lb.SResourceBase = SResourceBase{
|
||||
Name: ret.Name,
|
||||
SelfLink: ret.SelfLink,
|
||||
}
|
||||
} else {
|
||||
ret := SBackendServices{}
|
||||
err = self.GetBySelfId(resourceId, &ret)
|
||||
lb.backendServices = []SBackendServices{ret}
|
||||
lb.SResourceBase = SResourceBase{
|
||||
Name: ret.Name,
|
||||
SelfLink: ret.SelfLink,
|
||||
}
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return lb, errors.Wrapf(err, "get")
|
||||
}
|
||||
|
||||
lb.region = self
|
||||
return lb, nil
|
||||
}
|
||||
|
||||
func (self *SGlobalRegion) GetILoadBalancerById(loadbalancerId string) (cloudprovider.ICloudLoadbalancer, error) {
|
||||
lb, err := self.GetLoadbalancer(loadbalancerId)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "GetLoadbalancer")
|
||||
}
|
||||
return &lb, nil
|
||||
}
|
||||
+238
@@ -0,0 +1,238 @@
|
||||
// Copyright 2019 Yunion
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package google
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"yunion.io/x/pkg/errors"
|
||||
|
||||
api "yunion.io/x/cloudmux/pkg/apis/compute"
|
||||
"yunion.io/x/cloudmux/pkg/cloudprovider"
|
||||
)
|
||||
|
||||
type SGlobalLoadbalancerBackend struct {
|
||||
lbbg *SGlobalLoadBalancerBackendGroup
|
||||
|
||||
backendService SBackendServices //
|
||||
instanceGroup SGlobalInstanceGroup // 实例组
|
||||
Backend SGlobalInstanceGroupInstance // backend
|
||||
|
||||
Port int `json:"port"`
|
||||
}
|
||||
|
||||
func (self SGlobalLoadbalancerBackend) GetId() string {
|
||||
return fmt.Sprintf("%s::%s::%s::%d", self.lbbg.GetGlobalId(), self.instanceGroup.GetGlobalId(), self.GetBackendId(), self.Port)
|
||||
}
|
||||
|
||||
func (self SGlobalLoadbalancerBackend) GetName() string {
|
||||
segs := strings.Split(self.Backend.Instance, "/")
|
||||
name := ""
|
||||
if len(segs) > 0 {
|
||||
name = segs[len(segs)-1]
|
||||
}
|
||||
return fmt.Sprintf("%s::%s::%d", self.instanceGroup.GetName(), name, self.Port)
|
||||
}
|
||||
|
||||
func (self SGlobalLoadbalancerBackend) GetGlobalId() string {
|
||||
return self.GetId()
|
||||
}
|
||||
|
||||
func (self SGlobalLoadbalancerBackend) GetCreatedAt() time.Time {
|
||||
return time.Time{}
|
||||
}
|
||||
|
||||
func (self SGlobalLoadbalancerBackend) GetDescription() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (self SGlobalLoadbalancerBackend) GetStatus() string {
|
||||
return api.LB_STATUS_ENABLED
|
||||
}
|
||||
|
||||
func (self SGlobalLoadbalancerBackend) Refresh() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self SGlobalLoadbalancerBackend) IsEmulated() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (self SGlobalLoadbalancerBackend) GetSysTags() map[string]string {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self SGlobalLoadbalancerBackend) GetTags() (map[string]string, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (self SGlobalLoadbalancerBackend) SetTags(tags map[string]string, replace bool) error {
|
||||
return cloudprovider.ErrNotSupported
|
||||
}
|
||||
|
||||
func (self SGlobalLoadbalancerBackend) GetWeight() int {
|
||||
return 0
|
||||
}
|
||||
|
||||
func (self SGlobalLoadbalancerBackend) GetPort() int {
|
||||
return self.Port
|
||||
}
|
||||
|
||||
func (self SGlobalLoadbalancerBackend) GetBackendType() string {
|
||||
return api.LB_BACKEND_GUEST
|
||||
}
|
||||
|
||||
func (self SGlobalLoadbalancerBackend) GetBackendRole() string {
|
||||
return api.LB_BACKEND_ROLE_DEFAULT
|
||||
}
|
||||
|
||||
func (self SGlobalLoadbalancerBackend) GetBackendId() string {
|
||||
vm := &SInstance{}
|
||||
err := self.lbbg.lb.region.client.GetBySelfId(self.Backend.Instance, vm)
|
||||
if err != nil {
|
||||
return getGlobalId(self.Backend.Instance)
|
||||
}
|
||||
return vm.GetGlobalId()
|
||||
}
|
||||
|
||||
func (self SGlobalLoadbalancerBackend) GetIpAddress() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (self SGlobalLoadbalancerBackend) SyncConf(ctx context.Context, port, weight int) error {
|
||||
return cloudprovider.ErrNotSupported
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadBalancerBackendGroup) GetILoadbalancerBackends() ([]cloudprovider.ICloudLoadbalancerBackend, error) {
|
||||
backends, err := self.GetLoadbalancerBackends()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "GetLoadbalancerBackends")
|
||||
}
|
||||
|
||||
ibackends := make([]cloudprovider.ICloudLoadbalancerBackend, len(backends))
|
||||
for i := range backends {
|
||||
ibackends[i] = &backends[i]
|
||||
}
|
||||
|
||||
return ibackends, nil
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadBalancerBackendGroup) GetLoadbalancerBackends() ([]SGlobalLoadbalancerBackend, error) {
|
||||
if self.backends != nil {
|
||||
return self.backends, nil
|
||||
}
|
||||
|
||||
_igs, err := self.lb.GetInstanceGroupsMap()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "GetInstanceGroupsMap")
|
||||
}
|
||||
|
||||
igs := make([]SGlobalInstanceGroup, 0)
|
||||
for i := range self.backendService.Backends {
|
||||
backend := self.backendService.Backends[i]
|
||||
if v, ok := _igs[backend.Group]; ok {
|
||||
igs = append(igs, v)
|
||||
}
|
||||
}
|
||||
|
||||
ret := make([]SGlobalLoadbalancerBackend, 0)
|
||||
for i := range igs {
|
||||
ig := igs[i]
|
||||
// http lb
|
||||
if self.lb.isHttpLb {
|
||||
for j := range ig.NamedPorts {
|
||||
np := ig.NamedPorts[j]
|
||||
if np.Name != self.backendService.PortName {
|
||||
continue
|
||||
}
|
||||
|
||||
bs, err := ig.GetInstances()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "GetInstances")
|
||||
}
|
||||
|
||||
for n := range bs {
|
||||
backend := SGlobalLoadbalancerBackend{
|
||||
lbbg: self,
|
||||
instanceGroup: ig,
|
||||
Backend: bs[n],
|
||||
backendService: self.backendService,
|
||||
Port: int(np.Port),
|
||||
}
|
||||
|
||||
ret = append(ret, backend)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// tcp & udp lb
|
||||
bs, err := ig.GetInstances()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "GetInstances")
|
||||
}
|
||||
|
||||
frs, err := self.lb.GetForwardingRules()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "GetForwardingRules")
|
||||
}
|
||||
|
||||
for m := range frs {
|
||||
fr := frs[m]
|
||||
if fr.Ports == nil || len(fr.Ports) == 0 {
|
||||
ports := strings.Split(fr.PortRange, "-")
|
||||
if len(ports) == 2 && ports[0] == ports[1] {
|
||||
port, _ := strconv.Atoi(ports[0])
|
||||
for n := range bs {
|
||||
backend := SGlobalLoadbalancerBackend{
|
||||
lbbg: self,
|
||||
instanceGroup: ig,
|
||||
Backend: bs[n],
|
||||
backendService: self.backendService,
|
||||
Port: port,
|
||||
}
|
||||
|
||||
ret = append(ret, backend)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for p := range fr.Ports {
|
||||
port, _ := strconv.Atoi(fr.Ports[p])
|
||||
if port <= 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
for n := range bs {
|
||||
backend := SGlobalLoadbalancerBackend{
|
||||
lbbg: self,
|
||||
instanceGroup: ig,
|
||||
Backend: bs[n],
|
||||
backendService: self.backendService,
|
||||
Port: port,
|
||||
}
|
||||
|
||||
ret = append(ret, backend)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self.backends = ret
|
||||
return ret, nil
|
||||
}
|
||||
Generated
Vendored
+139
@@ -0,0 +1,139 @@
|
||||
// Copyright 2019 Yunion
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package google
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"yunion.io/x/pkg/errors"
|
||||
|
||||
api "yunion.io/x/cloudmux/pkg/apis/compute"
|
||||
"yunion.io/x/cloudmux/pkg/cloudprovider"
|
||||
)
|
||||
|
||||
type SGlobalLoadBalancerBackendGroup struct {
|
||||
lb *SGlobalLoadbalancer
|
||||
backends []SGlobalLoadbalancerBackend
|
||||
|
||||
backendService SBackendServices //
|
||||
|
||||
Id string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadBalancerBackendGroup) GetId() string {
|
||||
return self.Id
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadBalancerBackendGroup) GetName() string {
|
||||
return self.Name
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadBalancerBackendGroup) GetGlobalId() string {
|
||||
return self.backendService.GetGlobalId()
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadBalancerBackendGroup) GetCreatedAt() time.Time {
|
||||
return time.Time{}
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadBalancerBackendGroup) GetDescription() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadBalancerBackendGroup) GetStatus() string {
|
||||
return api.LB_STATUS_ENABLED
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadBalancerBackendGroup) Refresh() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadBalancerBackendGroup) IsEmulated() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadBalancerBackendGroup) GetSysTags() map[string]string {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadBalancerBackendGroup) GetTags() (map[string]string, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadBalancerBackendGroup) SetTags(tags map[string]string, replace bool) error {
|
||||
return cloudprovider.ErrNotSupported
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadBalancerBackendGroup) IsDefault() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadBalancerBackendGroup) GetType() string {
|
||||
return api.LB_BACKENDGROUP_TYPE_NORMAL
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadBalancerBackendGroup) GetILoadbalancerBackendById(backendId string) (cloudprovider.ICloudLoadbalancerBackend, error) {
|
||||
backends, err := self.GetLoadbalancerBackends()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "GetLoadbalancerBackends")
|
||||
}
|
||||
|
||||
for i := range backends {
|
||||
if backends[i].GetGlobalId() == backendId {
|
||||
return &backends[i], nil
|
||||
}
|
||||
}
|
||||
|
||||
return nil, cloudprovider.ErrNotFound
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadBalancerBackendGroup) AddBackendServer(serverId string, weight int, port int) (cloudprovider.ICloudLoadbalancerBackend, error) {
|
||||
return nil, cloudprovider.ErrNotSupported
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadBalancerBackendGroup) RemoveBackendServer(serverId string, weight int, port int) error {
|
||||
return cloudprovider.ErrNotSupported
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadBalancerBackendGroup) Delete(ctx context.Context) error {
|
||||
return cloudprovider.ErrNotSupported
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadBalancerBackendGroup) Sync(ctx context.Context, group *cloudprovider.SLoadbalancerBackendGroup) error {
|
||||
return cloudprovider.ErrNotSupported
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancer) GetLoadbalancerBackendGroups() ([]SGlobalLoadBalancerBackendGroup, error) {
|
||||
bss, err := self.GetBackendServices()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "GetBackendServices")
|
||||
}
|
||||
|
||||
ret := make([]SGlobalLoadBalancerBackendGroup, 0)
|
||||
for i := range bss {
|
||||
group := SGlobalLoadBalancerBackendGroup{
|
||||
backendService: bss[i],
|
||||
Id: bss[i].GetId(),
|
||||
Name: bss[i].GetName(),
|
||||
lb: self,
|
||||
}
|
||||
|
||||
ret = append(ret, group)
|
||||
}
|
||||
|
||||
return ret, nil
|
||||
}
|
||||
Generated
Vendored
+362
@@ -0,0 +1,362 @@
|
||||
// Copyright 2019 Yunion
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package google
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"yunion.io/x/pkg/errors"
|
||||
"yunion.io/x/pkg/utils"
|
||||
|
||||
"yunion.io/x/cloudmux/pkg/cloudprovider"
|
||||
)
|
||||
|
||||
type SGlobalInstanceGroup struct {
|
||||
SResourceBase
|
||||
region *SGlobalRegion
|
||||
instances []SGlobalInstanceGroupInstance
|
||||
|
||||
CreationTimestamp string `json:"creationTimestamp"`
|
||||
Description string `json:"description"`
|
||||
NamedPorts []NamedPort `json:"namedPorts"`
|
||||
Network string `json:"network"`
|
||||
Fingerprint string `json:"fingerprint"`
|
||||
Zone string `json:"zone"`
|
||||
Size int64 `json:"size"`
|
||||
Region string `json:"region"`
|
||||
Subnetwork string `json:"subnetwork"`
|
||||
Kind string `json:"kind"`
|
||||
}
|
||||
|
||||
type SGlobalInstanceGroupInstance struct {
|
||||
instanceGroup *SGlobalInstanceGroup
|
||||
Instance string `json:"instance"`
|
||||
Status string `json:"status"`
|
||||
NamedPorts []NamedPort `json:"namedPorts"`
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancer) isNameMatch(target interface{}) bool {
|
||||
var name string
|
||||
switch t := target.(type) {
|
||||
case *STargetHttpProxy, *STargetHttpsProxy:
|
||||
var URLMapUrl string
|
||||
switch t.(type) {
|
||||
case *STargetHttpProxy:
|
||||
URLMapUrl = t.(*STargetHttpProxy).URLMap
|
||||
case *STargetHttpsProxy:
|
||||
URLMapUrl = t.(*STargetHttpsProxy).URLMap
|
||||
}
|
||||
urlMapResponse, _ := _jsonRequest(self.region.client.client, "GET", URLMapUrl, nil, false)
|
||||
var urlMap SUrlMap
|
||||
urlMapResponse.Unmarshal(&urlMap)
|
||||
name = urlMap.Name
|
||||
if name != self.GetName() {
|
||||
parts := strings.Split(urlMap.DefaultService, "/")
|
||||
name = parts[len(parts)-1]
|
||||
}
|
||||
case *STargetTcpProxy:
|
||||
parts := strings.Split(t.Service, "/")
|
||||
name = parts[len(parts)-1]
|
||||
}
|
||||
return name == self.GetName()
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancer) GetForwardingRules() ([]SForwardingRule, error) {
|
||||
var ret []SForwardingRule
|
||||
if err := self.region.getGlobalLoadbalancerComponents("forwardingRules", "", &ret); err != nil {
|
||||
return nil, errors.Wrap(err, "getGlobalAddress.forwardingRules")
|
||||
}
|
||||
|
||||
var _ret []SForwardingRule
|
||||
for _, item := range ret {
|
||||
targetResponse, err := _jsonRequest(self.region.client.client, "GET", item.Target, nil, false)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "getGlobalAddress.GetProxy")
|
||||
}
|
||||
|
||||
var target interface{}
|
||||
switch {
|
||||
case strings.Contains(item.Target, "targetHttpProxies"):
|
||||
target = new(STargetHttpProxy)
|
||||
case strings.Contains(item.Target, "targetHttpsProxies"):
|
||||
target = new(STargetHttpsProxy)
|
||||
case strings.Contains(item.Target, "targetTcpProxies"):
|
||||
target = new(STargetTcpProxy)
|
||||
default:
|
||||
continue
|
||||
}
|
||||
|
||||
if err := targetResponse.Unmarshal(target); err != nil {
|
||||
return nil, errors.Wrap(err, "getGlobalAddress.target.Unmarshal")
|
||||
}
|
||||
|
||||
if self.isNameMatch(target) {
|
||||
_ret = append(_ret, item)
|
||||
}
|
||||
}
|
||||
|
||||
return _ret, nil
|
||||
}
|
||||
|
||||
func (self *SGlobalRegion) GetGlobalInstanceGroups(filter string) ([]SGlobalInstanceGroup, error) {
|
||||
ret := make([]SGlobalInstanceGroup, 0)
|
||||
err := self.getGlobalLoadbalancerComponents("instanceGroups", filter, &ret)
|
||||
for i := range ret {
|
||||
ret[i].region = self
|
||||
}
|
||||
return ret, err
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancer) GetInstanceGroups() ([]SGlobalInstanceGroup, error) {
|
||||
if self.instanceGroups != nil {
|
||||
return self.instanceGroups, nil
|
||||
}
|
||||
|
||||
if self.backendServices == nil {
|
||||
bss, err := self.GetBackendServices()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "GetBackendServices")
|
||||
}
|
||||
self.backendServices = bss
|
||||
}
|
||||
|
||||
bgs := []string{}
|
||||
for i := range self.backendServices {
|
||||
_bgs := self.backendServices[i].Backends
|
||||
for j := range _bgs {
|
||||
if !utils.IsInStringArray(_bgs[j].Group, bgs) {
|
||||
bgs = append(bgs, _bgs[j].Group)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if len(bgs) == 0 {
|
||||
return []SGlobalInstanceGroup{}, nil
|
||||
}
|
||||
|
||||
regionFilters := []string{}
|
||||
zonesFilter := map[string][]string{}
|
||||
for i := range bgs {
|
||||
if !strings.Contains(bgs[i], "/zones/") {
|
||||
regionFilters = append(regionFilters, fmt.Sprintf(`(selfLink="%s")`, bgs[i]))
|
||||
} else {
|
||||
ig := bgs[i]
|
||||
index := strings.Index(ig, "/zones/")
|
||||
zoneId := strings.Split(ig[index:], "/")[2]
|
||||
if fs, ok := zonesFilter[zoneId]; ok {
|
||||
f := fmt.Sprintf(`(selfLink="%s")`, ig)
|
||||
if !utils.IsInStringArray(f, fs) {
|
||||
zonesFilter[zoneId] = append(fs, f)
|
||||
}
|
||||
} else {
|
||||
zonesFilter[zoneId] = []string{fmt.Sprintf(`(selfLink="%s")`, ig)}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
igs := make([]SGlobalInstanceGroup, 0)
|
||||
// regional instance groups
|
||||
if len(regionFilters) > 0 {
|
||||
_igs, err := self.region.GetGlobalInstanceGroups(strings.Join(regionFilters, " OR "))
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "GetRegionalInstanceGroups")
|
||||
}
|
||||
|
||||
igs = append(igs, _igs...)
|
||||
}
|
||||
|
||||
for z, fs := range zonesFilter {
|
||||
_igs := make([]SGlobalInstanceGroup, 0)
|
||||
err := self.region.getInstanceGroups(z, "instanceGroups", strings.Join(fs, " OR "), &_igs)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "getInstanceGroups")
|
||||
}
|
||||
|
||||
igs = append(igs, _igs...)
|
||||
}
|
||||
|
||||
self.instanceGroups = igs
|
||||
return igs, nil
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancer) GetInstanceGroupsMap() (map[string]SGlobalInstanceGroup, error) {
|
||||
igs, err := self.GetInstanceGroups()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "SGlobalInstanceGroup")
|
||||
}
|
||||
|
||||
ret := make(map[string]SGlobalInstanceGroup, 0)
|
||||
for i := range igs {
|
||||
ig := igs[i]
|
||||
ig.region = self.region
|
||||
ret[ig.SelfLink] = ig
|
||||
}
|
||||
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancer) GetBackendServices() ([]SBackendServices, error) {
|
||||
if self.isHttpLb && self.urlMap != nil {
|
||||
ret := make([]SBackendServices, 0)
|
||||
ids := []string{self.urlMap.DefaultService}
|
||||
for i := range self.urlMap.PathMatchers {
|
||||
ps := self.urlMap.PathMatchers[i]
|
||||
if len(ps.DefaultService) > 0 && !utils.IsInStringArray(ps.DefaultService, ids) {
|
||||
ids = append(ids, ps.DefaultService)
|
||||
}
|
||||
|
||||
for j := range ps.PathRules {
|
||||
if len(ps.PathRules[j].Service) > 0 && !utils.IsInStringArray(ps.PathRules[j].Service, ids) {
|
||||
ids = append(ids, ps.PathRules[j].Service)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
filters := []string{}
|
||||
for i := range ids {
|
||||
filters = append(filters, fmt.Sprintf(`(selfLink="%s")`, ids[i]))
|
||||
}
|
||||
|
||||
if len(filters) == 0 {
|
||||
return []SBackendServices{}, nil
|
||||
}
|
||||
err := self.region.getGlobalLoadbalancerComponents("backendServices", strings.Join(filters, " OR "), &ret)
|
||||
self.backendServices = ret
|
||||
return ret, err
|
||||
}
|
||||
|
||||
return self.backendServices, nil
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancer) GetTargetHttpProxies() ([]STargetHttpProxy, error) {
|
||||
ret := make([]STargetHttpProxy, 0)
|
||||
filter := fmt.Sprintf("urlMap eq %s", self.GetId())
|
||||
err := self.region.getGlobalLoadbalancerComponents("targetHttpProxies", filter, &ret)
|
||||
return ret, err
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancer) GetTargetHttpsProxies() ([]STargetHttpsProxy, error) {
|
||||
ret := make([]STargetHttpsProxy, 0)
|
||||
filter := fmt.Sprintf("urlMap eq %s", self.GetId())
|
||||
err := self.region.getGlobalLoadbalancerComponents("targetHttpsProxies", filter, &ret)
|
||||
return ret, err
|
||||
}
|
||||
|
||||
func (self *SGlobalRegion) GetGlobalUrlMaps(filter string) ([]SUrlMap, error) {
|
||||
ret := make([]SUrlMap, 0)
|
||||
err := self.getGlobalLoadbalancerComponents("urlMaps", filter, &ret)
|
||||
return ret, err
|
||||
}
|
||||
|
||||
func (self *SGlobalRegion) GetGlobalBackendServices(filter string) ([]SBackendServices, error) {
|
||||
ret := make([]SBackendServices, 0)
|
||||
err := self.getGlobalLoadbalancerComponents("backendServices", filter, &ret)
|
||||
return ret, err
|
||||
}
|
||||
|
||||
func (self *SGlobalInstanceGroup) GetInstances() ([]SGlobalInstanceGroupInstance, error) {
|
||||
if self.instances != nil {
|
||||
return self.instances, nil
|
||||
}
|
||||
|
||||
ret := make([]SGlobalInstanceGroupInstance, 0)
|
||||
resourceId := strings.TrimPrefix(getGlobalId(self.SelfLink), fmt.Sprintf("projects/%s/", self.region.GetProjectId()))
|
||||
err := self.region.listAll("POST", resourceId+"/listInstances", nil, &ret)
|
||||
if err != nil {
|
||||
if errors.Cause(err) == cloudprovider.ErrNotFound {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return nil, errors.Wrap(err, "ListAll")
|
||||
}
|
||||
|
||||
for i := range ret {
|
||||
ret[i].instanceGroup = self
|
||||
}
|
||||
|
||||
self.instances = ret
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancer) GetHealthCheckMaps() (map[string]HealthChecks, error) {
|
||||
hcs, err := self.GetHealthChecks()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "GetHealthChecks")
|
||||
}
|
||||
|
||||
ret := map[string]HealthChecks{}
|
||||
for i := range hcs {
|
||||
ret[hcs[i].SelfLink] = hcs[i]
|
||||
}
|
||||
return ret, err
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancer) GetHealthChecks() ([]HealthChecks, error) {
|
||||
if self.healthChecks != nil {
|
||||
return self.healthChecks, nil
|
||||
}
|
||||
|
||||
hcs, err := self.region.GetRegionalHealthChecks("")
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "GetRegionalHealthChecks")
|
||||
}
|
||||
|
||||
ghcs, err := self.region.GetGlobalHealthChecks("")
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "GetGlobalHealthChecks")
|
||||
}
|
||||
|
||||
self.healthChecks = append(self.healthChecks, ghcs...)
|
||||
self.healthChecks = append(self.healthChecks, hcs...)
|
||||
return self.healthChecks, err
|
||||
}
|
||||
|
||||
func (self *SGlobalRegion) GetRegionalHealthChecks(filter string) ([]HealthChecks, error) {
|
||||
ret := make([]HealthChecks, 0)
|
||||
err := self.getLoadbalancerComponents("healthChecks", filter, &ret)
|
||||
return ret, err
|
||||
}
|
||||
|
||||
func (self *SGlobalRegion) GetGlobalHealthChecks(filter string) ([]HealthChecks, error) {
|
||||
ret := make([]HealthChecks, 0)
|
||||
params := map[string]string{}
|
||||
if len(filter) > 0 {
|
||||
params["filter"] = filter
|
||||
}
|
||||
|
||||
err := self.ListAll("global/healthChecks", params, &ret)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "ListAll")
|
||||
}
|
||||
|
||||
return ret, err
|
||||
}
|
||||
|
||||
func (self *SGlobalRegion) getInstanceGroups(zoneId, resource string, filter string, result interface{}) error {
|
||||
url := fmt.Sprintf("zones/%s/%s", zoneId, resource)
|
||||
|
||||
params := map[string]string{}
|
||||
if len(filter) > 0 {
|
||||
params["filter"] = filter
|
||||
}
|
||||
|
||||
err := self.ListAll(url, params, result)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "ListAll")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
+647
@@ -0,0 +1,647 @@
|
||||
// Copyright 2019 Yunion
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package google
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"yunion.io/x/log"
|
||||
"yunion.io/x/pkg/errors"
|
||||
|
||||
api "yunion.io/x/cloudmux/pkg/apis/compute"
|
||||
"yunion.io/x/cloudmux/pkg/cloudprovider"
|
||||
)
|
||||
|
||||
type SGlobalLoadbalancerListener struct {
|
||||
lb *SGlobalLoadbalancer
|
||||
rules []SGlobalLoadbalancerListenerRule
|
||||
forwardRule SForwardingRule // 服务IP地址
|
||||
backendService SBackendServices //
|
||||
httpProxy *STargetHttpProxy // http
|
||||
httpsProxy *STargetHttpsProxy // https
|
||||
healthChecks []HealthChecks
|
||||
|
||||
ForwardRuleName string `json:"forward_rule_name"`
|
||||
BackendServiceName string `json:"backend_service_name"`
|
||||
Protocol string `json:"protocol"`
|
||||
Port string `json:"port"` // 监听端口
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancerListener) GetId() string {
|
||||
return fmt.Sprintf("%s::%s::%s", self.forwardRule.GetGlobalId(), self.backendService.GetGlobalId(), self.Port)
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancerListener) GetName() string {
|
||||
return fmt.Sprintf("%s::%s::%s", self.forwardRule.GetName(), self.backendService.GetName(), self.Port)
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancerListener) GetGlobalId() string {
|
||||
return self.GetId()
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancerListener) GetCreatedAt() time.Time {
|
||||
return time.Time{}
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancerListener) GetDescription() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancerListener) GetStatus() string {
|
||||
return api.LB_STATUS_ENABLED
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancerListener) Refresh() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancerListener) IsEmulated() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancerListener) GetSysTags() map[string]string {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancerListener) GetTags() (map[string]string, error) {
|
||||
if len(self.forwardRule.IPAddress) > 0 {
|
||||
return map[string]string{"FrontendIP": self.forwardRule.IPAddress}, nil
|
||||
}
|
||||
|
||||
return map[string]string{}, nil
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancerListener) SetTags(tags map[string]string, replace bool) error {
|
||||
return cloudprovider.ErrNotSupported
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancerListener) GetListenerType() string {
|
||||
return self.Protocol
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancerListener) GetListenerPort() int {
|
||||
port, err := strconv.Atoi(self.Port)
|
||||
if err != nil {
|
||||
log.Errorf("GetListenerPort %s", err)
|
||||
return 0
|
||||
}
|
||||
|
||||
return port
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancerListener) GetScheduler() string {
|
||||
switch self.backendService.LocalityLBPolicy {
|
||||
case "ROUND_ROBIN":
|
||||
return api.LB_SCHEDULER_RR
|
||||
case "LEAST_REQUEST":
|
||||
return api.LB_SCHEDULER_WLC
|
||||
case "RING_HASH":
|
||||
return api.LB_SCHEDULER_QCH
|
||||
case "ORIGINAL_DESTINATION":
|
||||
return api.LB_SCHEDULER_SCH
|
||||
case "MAGLEV":
|
||||
return api.LB_SCHEDULER_MH
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancerListener) GetAclStatus() string {
|
||||
return api.LB_BOOL_OFF
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancerListener) GetAclType() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancerListener) GetAclId() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancerListener) GetEgressMbps() int {
|
||||
return 0
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancerListener) GetBackendGroupId() string {
|
||||
return self.backendService.GetGlobalId()
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancerListener) GetClientIdleTimeout() int {
|
||||
return int(self.backendService.ConnectionDraining.DrainingTimeoutSEC)
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancerListener) GetBackendConnectTimeout() int {
|
||||
return int(self.backendService.TimeoutSEC)
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancerListener) CreateILoadBalancerListenerRule(rule *cloudprovider.SLoadbalancerListenerRule) (cloudprovider.ICloudLoadbalancerListenerRule, error) {
|
||||
return nil, cloudprovider.ErrNotImplemented
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancerListener) GetILoadBalancerListenerRuleById(ruleId string) (cloudprovider.ICloudLoadbalancerListenerRule, error) {
|
||||
rules, err := self.GetLoadbalancerListenerRules()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "GetLoadbalancerListenerRules")
|
||||
}
|
||||
|
||||
for i := range rules {
|
||||
if rules[i].GetGlobalId() == ruleId {
|
||||
return &rules[i], nil
|
||||
}
|
||||
}
|
||||
|
||||
return nil, cloudprovider.ErrNotFound
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancerListener) GetStickySession() string {
|
||||
if self.backendService.SessionAffinity == "NONE" {
|
||||
return api.LB_BOOL_OFF
|
||||
} else {
|
||||
return api.LB_BOOL_ON
|
||||
}
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancerListener) GetStickySessionType() string {
|
||||
switch self.backendService.SessionAffinity {
|
||||
case "HTTP_COOKIE":
|
||||
return api.LB_STICKY_SESSION_TYPE_SERVER
|
||||
case "GENERATED_COOKIE":
|
||||
return api.LB_STICKY_SESSION_TYPE_INSERT
|
||||
}
|
||||
return self.backendService.SessionAffinity
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancerListener) GetStickySessionCookie() string {
|
||||
return self.backendService.ConsistentHash.HTTPCookie.Name
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancerListener) GetStickySessionCookieTimeout() int {
|
||||
if len(self.backendService.ConsistentHash.HTTPCookie.TTL.Seconds) == 0 {
|
||||
return 0
|
||||
}
|
||||
|
||||
sec, err := strconv.Atoi(self.backendService.ConsistentHash.HTTPCookie.TTL.Seconds)
|
||||
if err != nil {
|
||||
log.Debugf("GetStickySessionCookieTimeout %s", err)
|
||||
return 0
|
||||
}
|
||||
|
||||
return sec
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancerListener) XForwardedForEnabled() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancerListener) GzipEnabled() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancerListener) GetCertificateId() string {
|
||||
if self.httpsProxy != nil && len(self.httpsProxy.SSLCertificates) > 0 {
|
||||
cert := SResourceBase{
|
||||
Name: "",
|
||||
SelfLink: self.httpsProxy.SSLCertificates[0],
|
||||
}
|
||||
return cert.GetGlobalId()
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancerListener) GetTLSCipherPolicy() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancerListener) HTTP2Enabled() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancerListener) GetRedirect() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancerListener) GetRedirectCode() int64 {
|
||||
return 0
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancerListener) GetRedirectScheme() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancerListener) GetRedirectHost() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancerListener) GetRedirectPath() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancerListener) GetHealthCheck() string {
|
||||
if len(self.backendService.HealthChecks) > 0 {
|
||||
return api.LB_BOOL_ON
|
||||
} else {
|
||||
return api.LB_BOOL_OFF
|
||||
}
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancerListener) GetHealthCheckTimeout() int {
|
||||
hcs := self.GetHealthChecks()
|
||||
if hcs == nil {
|
||||
return 0
|
||||
}
|
||||
|
||||
return int(hcs[0].TimeoutSEC)
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancerListener) GetHealthCheckInterval() int {
|
||||
hcs := self.GetHealthChecks()
|
||||
if hcs == nil {
|
||||
return 0
|
||||
}
|
||||
|
||||
return int(hcs[0].CheckIntervalSEC)
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancerListener) GetHealthCheckRise() int {
|
||||
hcs := self.GetHealthChecks()
|
||||
if hcs == nil {
|
||||
return 0
|
||||
}
|
||||
|
||||
return int(hcs[0].HealthyThreshold)
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancerListener) GetHealthCheckFail() int {
|
||||
hcs := self.GetHealthChecks()
|
||||
if hcs == nil {
|
||||
return 0
|
||||
}
|
||||
|
||||
return int(hcs[0].UnhealthyThreshold)
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancerListener) GetHealthCheckReq() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancerListener) GetHealthCheckExp() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancerListener) GetHealthCheckDomain() string {
|
||||
hcs := self.GetHealthChecks()
|
||||
if hcs == nil {
|
||||
return ""
|
||||
}
|
||||
switch hcs[0].Type {
|
||||
case "HTTPS":
|
||||
return hcs[0].HTTPSHealthCheck.Host
|
||||
case "HTTP2":
|
||||
return hcs[0].Http2HealthCheck.Host
|
||||
case "HTTP":
|
||||
return hcs[0].HTTPHealthCheck.Host
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancerListener) GetHealthCheckURI() string {
|
||||
hcs := self.GetHealthChecks()
|
||||
if hcs == nil {
|
||||
return ""
|
||||
}
|
||||
switch hcs[0].Type {
|
||||
case "HTTPS":
|
||||
return hcs[0].HTTPSHealthCheck.RequestPath
|
||||
case "HTTP2":
|
||||
return hcs[0].Http2HealthCheck.RequestPath
|
||||
case "HTTP":
|
||||
return hcs[0].HTTPHealthCheck.RequestPath
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancerListener) GetHealthCheckCode() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancerListener) Start() error {
|
||||
return cloudprovider.ErrNotSupported
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancerListener) Stop() error {
|
||||
return cloudprovider.ErrNotSupported
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancerListener) ChangeScheduler(ctx context.Context, opts *cloudprovider.ChangeListenerSchedulerOptions) error {
|
||||
return cloudprovider.ErrNotImplemented
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancerListener) SetHealthCheck(ctx context.Context, opts *cloudprovider.ListenerHealthCheckOptions) error {
|
||||
return cloudprovider.ErrNotImplemented
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancerListener) ChangeCertificate(ctx context.Context, opts *cloudprovider.ListenerCertificateOptions) error {
|
||||
return cloudprovider.ErrNotSupported
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancerListener) SetAcl(ctx context.Context, opts *cloudprovider.ListenerAclOptions) error {
|
||||
return cloudprovider.ErrNotImplemented
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancerListener) Delete(ctx context.Context) error {
|
||||
return cloudprovider.ErrNotSupported
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancer) GetLoadbalancerListeners() ([]SGlobalLoadbalancerListener, error) {
|
||||
if self.urlMap != nil {
|
||||
return self.GetHTTPLoadbalancerListeners()
|
||||
} else {
|
||||
return self.GetNetworkLoadbalancerListeners()
|
||||
}
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancer) GetHTTPLoadbalancerListeners() ([]SGlobalLoadbalancerListener, error) {
|
||||
frs, err := self.GetForwardingRules()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "GetForwardingRules")
|
||||
}
|
||||
|
||||
_hps, err := self.GetTargetHttpProxies()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "GetTargetHttpProxies")
|
||||
}
|
||||
|
||||
hps := make(map[string]STargetHttpProxy, 0)
|
||||
for i := range _hps {
|
||||
hps[_hps[i].SelfLink] = _hps[i]
|
||||
}
|
||||
|
||||
_hsps, err := self.GetTargetHttpsProxies()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "GetTargetHttpsProxies")
|
||||
}
|
||||
|
||||
hsps := make(map[string]STargetHttpsProxy, 0)
|
||||
for i := range _hsps {
|
||||
hsps[_hsps[i].SelfLink] = _hsps[i]
|
||||
}
|
||||
|
||||
bss, err := self.GetBackendServices()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "GetBackendServices")
|
||||
}
|
||||
|
||||
lbls := make([]SGlobalLoadbalancerListener, 0)
|
||||
for i := range frs {
|
||||
fr := frs[i]
|
||||
for j := range bss {
|
||||
bs := bss[j]
|
||||
port := "80"
|
||||
protocol := "http"
|
||||
var hp STargetHttpProxy
|
||||
var hsp STargetHttpsProxy
|
||||
if fr.PortRange == "443-443" {
|
||||
port = "443"
|
||||
hsp = hsps[fr.Target]
|
||||
protocol = "https"
|
||||
} else if fr.PortRange == "8080-8080" {
|
||||
port = "8080"
|
||||
hp = hps[fr.Target]
|
||||
} else {
|
||||
hp = hps[fr.Target]
|
||||
}
|
||||
|
||||
lbl := SGlobalLoadbalancerListener{
|
||||
lb: self,
|
||||
forwardRule: fr,
|
||||
backendService: bs,
|
||||
httpProxy: &hp,
|
||||
httpsProxy: &hsp,
|
||||
ForwardRuleName: fr.GetName(),
|
||||
BackendServiceName: bs.GetName(),
|
||||
Protocol: protocol,
|
||||
Port: port,
|
||||
}
|
||||
lbls = append(lbls, lbl)
|
||||
}
|
||||
}
|
||||
|
||||
return lbls, nil
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancer) GetNetworkLoadbalancerListeners() ([]SGlobalLoadbalancerListener, error) {
|
||||
frs, err := self.GetForwardingRules()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "GetForwardingRules")
|
||||
}
|
||||
|
||||
bss, err := self.GetBackendServices()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "GetBackendServices")
|
||||
}
|
||||
|
||||
lbls := make([]SGlobalLoadbalancerListener, 0)
|
||||
for i := range frs {
|
||||
fr := frs[i]
|
||||
for j := range bss {
|
||||
bs := bss[j]
|
||||
if fr.Ports == nil || len(fr.Ports) == 0 {
|
||||
ports := strings.Split(fr.PortRange, "-")
|
||||
if len(ports) == 2 && ports[0] == ports[1] {
|
||||
lbl := SGlobalLoadbalancerListener{
|
||||
lb: self,
|
||||
forwardRule: fr,
|
||||
backendService: bs,
|
||||
ForwardRuleName: fr.GetName(),
|
||||
BackendServiceName: bs.GetName(),
|
||||
Protocol: strings.ToLower(fr.IPProtocol),
|
||||
Port: ports[0],
|
||||
}
|
||||
|
||||
lbls = append(lbls, lbl)
|
||||
}
|
||||
} else {
|
||||
for n := range fr.Ports {
|
||||
lbl := SGlobalLoadbalancerListener{
|
||||
lb: self,
|
||||
forwardRule: fr,
|
||||
backendService: bs,
|
||||
ForwardRuleName: fr.GetName(),
|
||||
BackendServiceName: bs.GetName(),
|
||||
Protocol: strings.ToLower(fr.IPProtocol),
|
||||
Port: fr.Ports[n],
|
||||
}
|
||||
|
||||
lbls = append(lbls, lbl)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return lbls, nil
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancerListener) GetHealthChecks() []HealthChecks {
|
||||
if self.healthChecks != nil {
|
||||
return self.healthChecks
|
||||
}
|
||||
|
||||
hcm, err := self.lb.GetHealthCheckMaps()
|
||||
if err != nil {
|
||||
log.Errorf("GetHealthCheckMaps %s", err)
|
||||
return nil
|
||||
}
|
||||
ret := make([]HealthChecks, 0)
|
||||
for i := range self.backendService.HealthChecks {
|
||||
hc := self.backendService.HealthChecks[i]
|
||||
if _, ok := hcm[hc]; ok {
|
||||
ret = append(ret, hcm[hc])
|
||||
}
|
||||
}
|
||||
|
||||
self.healthChecks = ret
|
||||
return ret
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancerListener) GetHealthCheckType() string {
|
||||
hcs := self.GetHealthChecks()
|
||||
if hcs == nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
switch strings.ToLower(hcs[0].Type) {
|
||||
case "tcp":
|
||||
return api.LB_HEALTH_CHECK_TCP
|
||||
case "udp":
|
||||
return api.LB_HEALTH_CHECK_UDP
|
||||
case "http", "http2":
|
||||
return api.LB_HEALTH_CHECK_HTTP
|
||||
case "https", "ssl":
|
||||
return api.LB_HEALTH_CHECK_HTTPS
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancerListener) GetBackendServerPort() int {
|
||||
igs, err := self.GetInstanceGroups()
|
||||
if err != nil {
|
||||
log.Errorf("GetInstanceGroups %s", err)
|
||||
return 0
|
||||
}
|
||||
|
||||
for i := range igs {
|
||||
for j := range igs[i].NamedPorts {
|
||||
if igs[i].NamedPorts[j].Name == self.backendService.PortName {
|
||||
return int(igs[i].NamedPorts[j].Port)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancerListener) GetInstanceGroups() ([]SGlobalInstanceGroup, error) {
|
||||
igs, err := self.lb.GetInstanceGroupsMap()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "GetInstanceGroups")
|
||||
}
|
||||
|
||||
ret := make([]SGlobalInstanceGroup, 0)
|
||||
for i := range self.backendService.Backends {
|
||||
b := self.backendService.Backends[i]
|
||||
if ig, ok := igs[b.Group]; ok {
|
||||
ret = append(ret, ig)
|
||||
}
|
||||
}
|
||||
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancerListener) GetILoadbalancerListenerRules() ([]cloudprovider.ICloudLoadbalancerListenerRule, error) {
|
||||
rules, err := self.GetLoadbalancerListenerRules()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "GetLoadbalancerListenerRules")
|
||||
}
|
||||
|
||||
irules := make([]cloudprovider.ICloudLoadbalancerListenerRule, len(rules))
|
||||
for i := range rules {
|
||||
irules[i] = &rules[i]
|
||||
}
|
||||
|
||||
return irules, nil
|
||||
}
|
||||
|
||||
func (self *SGlobalLoadbalancerListener) GetLoadbalancerListenerRules() ([]SGlobalLoadbalancerListenerRule, error) {
|
||||
if !self.lb.isHttpLb {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
if self.rules != nil {
|
||||
return self.rules, nil
|
||||
}
|
||||
|
||||
hostRules := self.lb.urlMap.HostRules
|
||||
pathMatchers := self.lb.urlMap.PathMatchers
|
||||
|
||||
pmm := make(map[string]PathMatcher, 0)
|
||||
for i := range pathMatchers {
|
||||
name := pathMatchers[i].Name
|
||||
pmm[name] = pathMatchers[i]
|
||||
}
|
||||
|
||||
ret := make([]SGlobalLoadbalancerListenerRule, 0)
|
||||
for _, rule := range hostRules {
|
||||
pm, ok := pmm[rule.PathMatcher]
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
for i := range rule.Hosts {
|
||||
host := rule.Hosts[i]
|
||||
for j := range pm.PathRules {
|
||||
pr := pm.PathRules[j]
|
||||
|
||||
if pr.Service != self.backendService.GetId() {
|
||||
continue
|
||||
}
|
||||
|
||||
r := SGlobalLoadbalancerListenerRule{
|
||||
lbl: self,
|
||||
backendService: self.backendService,
|
||||
BackendServiceName: self.backendService.GetName(),
|
||||
pathMatcher: pm,
|
||||
pathRule: pr,
|
||||
ListenerName: self.GetName(),
|
||||
Domain: host,
|
||||
Path: strings.Join(pr.Paths, ","),
|
||||
Port: self.Port,
|
||||
}
|
||||
|
||||
ret = append(ret, r)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self.rules = ret
|
||||
return ret, nil
|
||||
}
|
||||
Generated
Vendored
+126
@@ -0,0 +1,126 @@
|
||||
// Copyright 2019 Yunion
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package google
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
api "yunion.io/x/cloudmux/pkg/apis/compute"
|
||||
"yunion.io/x/cloudmux/pkg/cloudprovider"
|
||||
)
|
||||
|
||||
type SGlobalLoadbalancerListenerRule struct {
|
||||
lbl *SGlobalLoadbalancerListener
|
||||
pathMatcher PathMatcher
|
||||
pathRule PathRule
|
||||
backendService SBackendServices
|
||||
|
||||
ListenerName string `json:"listener_name"`
|
||||
BackendServiceName string `json:"backend_service_name"`
|
||||
Domain string `json:"domain"`
|
||||
Path string `json:"path"`
|
||||
Port string `json:"Port"`
|
||||
}
|
||||
|
||||
func (self SGlobalLoadbalancerListenerRule) GetId() string {
|
||||
return fmt.Sprintf("%s::%s::%s", self.lbl.GetGlobalId(), self.Domain, strings.Join(self.pathRule.Paths, ","))
|
||||
}
|
||||
|
||||
func (self SGlobalLoadbalancerListenerRule) GetName() string {
|
||||
return fmt.Sprintf("%s::%s::%s", self.lbl.GetName(), self.Domain, strings.Join(self.pathRule.Paths, ","))
|
||||
}
|
||||
|
||||
func (self SGlobalLoadbalancerListenerRule) GetGlobalId() string {
|
||||
return self.GetId()
|
||||
}
|
||||
|
||||
func (self SGlobalLoadbalancerListenerRule) GetCreatedAt() time.Time {
|
||||
return time.Time{}
|
||||
}
|
||||
|
||||
func (self SGlobalLoadbalancerListenerRule) GetDescription() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (self SGlobalLoadbalancerListenerRule) GetStatus() string {
|
||||
return api.LB_STATUS_ENABLED
|
||||
}
|
||||
|
||||
func (self SGlobalLoadbalancerListenerRule) Refresh() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self SGlobalLoadbalancerListenerRule) IsEmulated() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (self SGlobalLoadbalancerListenerRule) GetSysTags() map[string]string {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self SGlobalLoadbalancerListenerRule) GetTags() (map[string]string, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (self SGlobalLoadbalancerListenerRule) SetTags(tags map[string]string, replace bool) error {
|
||||
return cloudprovider.ErrNotSupported
|
||||
}
|
||||
|
||||
func (self SGlobalLoadbalancerListenerRule) GetRedirect() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (self SGlobalLoadbalancerListenerRule) GetRedirectCode() int64 {
|
||||
return 0
|
||||
}
|
||||
|
||||
func (self SGlobalLoadbalancerListenerRule) GetRedirectScheme() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (self SGlobalLoadbalancerListenerRule) GetRedirectHost() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (self SGlobalLoadbalancerListenerRule) GetRedirectPath() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (self SGlobalLoadbalancerListenerRule) IsDefault() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (self SGlobalLoadbalancerListenerRule) GetDomain() string {
|
||||
return self.Domain
|
||||
}
|
||||
|
||||
func (self SGlobalLoadbalancerListenerRule) GetPath() string {
|
||||
return self.Path
|
||||
}
|
||||
|
||||
func (self SGlobalLoadbalancerListenerRule) GetCondition() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (self SGlobalLoadbalancerListenerRule) GetBackendGroupId() string {
|
||||
return self.backendService.GetGlobalId()
|
||||
}
|
||||
|
||||
func (self SGlobalLoadbalancerListenerRule) Delete(ctx context.Context) error {
|
||||
return cloudprovider.ErrNotSupported
|
||||
}
|
||||
+160
@@ -0,0 +1,160 @@
|
||||
// Copyright 2019 Yunion
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package google
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"yunion.io/x/jsonutils"
|
||||
"yunion.io/x/pkg/errors"
|
||||
|
||||
api "yunion.io/x/cloudmux/pkg/apis/compute"
|
||||
"yunion.io/x/cloudmux/pkg/cloudprovider"
|
||||
"yunion.io/x/cloudmux/pkg/multicloud"
|
||||
)
|
||||
|
||||
type SGlobalRegion struct {
|
||||
cloudprovider.SFakeOnPremiseRegion
|
||||
multicloud.SNoObjectStorageRegion
|
||||
multicloud.SRegion
|
||||
client *SGoogleClient
|
||||
|
||||
Description string
|
||||
ID string
|
||||
Kind string
|
||||
Name string
|
||||
Status string
|
||||
SelfLink string
|
||||
CreationTimestamp time.Time
|
||||
}
|
||||
|
||||
func (region *SGlobalRegion) GetClient() *SGoogleClient {
|
||||
return region.client
|
||||
}
|
||||
|
||||
func (region *SGlobalRegion) GetName() string {
|
||||
if name, ok := RegionNames[region.Name]; ok {
|
||||
return fmt.Sprintf("%s %s", CLOUD_PROVIDER_GOOGLE_CN, name)
|
||||
}
|
||||
return fmt.Sprintf("%s %s", CLOUD_PROVIDER_GOOGLE_CN, region.Name)
|
||||
}
|
||||
|
||||
func (self *SGlobalRegion) GetI18n() cloudprovider.SModelI18nTable {
|
||||
en := fmt.Sprintf("%s %s", CLOUD_PROVIDER_GOOGLE, self.Name)
|
||||
table := cloudprovider.SModelI18nTable{}
|
||||
table["name"] = cloudprovider.NewSModelI18nEntry(self.GetName()).CN(self.GetName()).EN(en)
|
||||
return table
|
||||
}
|
||||
|
||||
func (region *SGlobalRegion) GetId() string {
|
||||
return region.Name
|
||||
}
|
||||
|
||||
func (region *SGlobalRegion) GetGlobalId() string {
|
||||
return fmt.Sprintf("%s/%s", CLOUD_PROVIDER_GOOGLE, region.Name)
|
||||
}
|
||||
|
||||
func (region *SGlobalRegion) GetGeographicInfo() cloudprovider.SGeographicInfo {
|
||||
if geoInfo, ok := LatitudeAndLongitude[region.Name]; ok {
|
||||
return geoInfo
|
||||
}
|
||||
return cloudprovider.SGeographicInfo{}
|
||||
}
|
||||
|
||||
func (self *SGlobalRegion) GetCreatedAt() time.Time {
|
||||
return self.CreationTimestamp
|
||||
}
|
||||
|
||||
func (region *SGlobalRegion) GetProvider() string {
|
||||
return CLOUD_PROVIDER_GOOGLE
|
||||
}
|
||||
|
||||
func (region *SGlobalRegion) GetStatus() string {
|
||||
return api.CLOUD_REGION_STATUS_INSERVER
|
||||
}
|
||||
|
||||
func (self *SGlobalRegion) GetCapabilities() []string {
|
||||
return []string{
|
||||
cloudprovider.CLOUD_CAPABILITY_EIP + cloudprovider.READ_ONLY_SUFFIX,
|
||||
cloudprovider.CLOUD_CAPABILITY_LOADBALANCER + cloudprovider.READ_ONLY_SUFFIX,
|
||||
}
|
||||
}
|
||||
|
||||
func (self *SGlobalRegion) GetILoadBalancers() ([]cloudprovider.ICloudLoadbalancer, error) {
|
||||
lbs, err := self.GetGlobalLoadbalancers()
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "GetGlobalLoadbalancers")
|
||||
}
|
||||
ilbs := []cloudprovider.ICloudLoadbalancer{}
|
||||
for i := range lbs {
|
||||
lbs[i].region = self
|
||||
ilbs = append(ilbs, &lbs[i])
|
||||
}
|
||||
return ilbs, nil
|
||||
}
|
||||
|
||||
func (self *SGlobalRegion) Delete(id string) error {
|
||||
operation := &SOperation{}
|
||||
err := self.client.ecsDelete(id, operation)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "client.ecsDelete")
|
||||
}
|
||||
_, err = self.client.WaitOperation(operation.SelfLink, id, "delete")
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "region.WaitOperation(%s)", operation.SelfLink)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (self *SGlobalRegion) GetProjectId() string {
|
||||
return self.client.projectId
|
||||
}
|
||||
|
||||
func (self *SGlobalRegion) GetBySelfId(id string, retval interface{}) error {
|
||||
return self.client.GetBySelfId(id, retval)
|
||||
}
|
||||
|
||||
func (region *SGlobalRegion) Do(id string, action string, params map[string]string, body jsonutils.JSONObject) error {
|
||||
opId, err := region.client.ecsDo(id, action, params, body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if strings.Index(opId, "/operations/") > 0 {
|
||||
_, err = region.client.WaitOperation(opId, id, action)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (region *SGlobalRegion) Get(resourceType, id string, retval interface{}) error {
|
||||
return region.client.ecsGet(resourceType, id, retval)
|
||||
}
|
||||
|
||||
func (self *SGlobalRegion) getLoadbalancerComponents(resource string, filter string, result interface{}) error {
|
||||
url := fmt.Sprintf("%s/%s", self.Name, resource)
|
||||
params := map[string]string{}
|
||||
if len(filter) > 0 {
|
||||
params["filter"] = filter
|
||||
}
|
||||
|
||||
err := self.ListAll(url, params, result)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "ListAll")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
+19
-6
@@ -18,7 +18,6 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
@@ -179,6 +178,18 @@ func (self *SGoogleClient) GetAccountId() string {
|
||||
return self.clientEmail
|
||||
}
|
||||
|
||||
func (self *SGoogleClient) GetGlobalRegion() *SGlobalRegion {
|
||||
return &SGlobalRegion{
|
||||
client: self,
|
||||
Description: "global",
|
||||
Kind: "compute#region",
|
||||
Name: "global",
|
||||
Status: "UP",
|
||||
SelfLink: "",
|
||||
CreationTimestamp: time.Time{},
|
||||
}
|
||||
}
|
||||
|
||||
func (self *SGoogleClient) fetchRegions() error {
|
||||
regions := []SRegion{}
|
||||
err := self.ecsListAll("regions", nil, ®ions)
|
||||
@@ -191,6 +202,8 @@ func (self *SGoogleClient) fetchRegions() error {
|
||||
regions[i].client = self
|
||||
self.iregions = append(self.iregions, ®ions[i])
|
||||
}
|
||||
// add global region
|
||||
self.iregions = append(self.iregions, self.GetGlobalRegion())
|
||||
|
||||
objectstoreCapability := []string{
|
||||
cloudprovider.CLOUD_CAPABILITY_OBJECTSTORE,
|
||||
@@ -547,7 +560,7 @@ func (self *SGoogleClient) storageUpload(resource string, header http.Header, bo
|
||||
return nil, errors.Wrap(err, "rawRequest")
|
||||
}
|
||||
if resp.StatusCode >= 400 {
|
||||
msg, _ := ioutil.ReadAll(resp.Body)
|
||||
msg, _ := io.ReadAll(resp.Body)
|
||||
defer resp.Body.Close()
|
||||
return nil, fmt.Errorf("StatusCode: %d %s", resp.StatusCode, string(msg))
|
||||
}
|
||||
@@ -560,7 +573,7 @@ func (self *SGoogleClient) storageUploadPart(resource string, header http.Header
|
||||
return nil, errors.Wrap(err, "rawRequest")
|
||||
}
|
||||
if resp.StatusCode >= 400 {
|
||||
msg, _ := ioutil.ReadAll(resp.Body)
|
||||
msg, _ := io.ReadAll(resp.Body)
|
||||
defer resp.Body.Close()
|
||||
return nil, fmt.Errorf("StatusCode: %d %s", resp.StatusCode, string(msg))
|
||||
}
|
||||
@@ -574,7 +587,7 @@ func (self *SGoogleClient) storageAbortUpload(resource string) error {
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode >= 400 {
|
||||
msg, _ := ioutil.ReadAll(resp.Body)
|
||||
msg, _ := io.ReadAll(resp.Body)
|
||||
return fmt.Errorf("StatusCode: %d %s", resp.StatusCode, string(msg))
|
||||
}
|
||||
return nil
|
||||
@@ -587,7 +600,7 @@ func (self *SGoogleClient) storageDownload(resource string, header http.Header)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode >= 400 {
|
||||
msg, _ := ioutil.ReadAll(resp.Body)
|
||||
msg, _ := io.ReadAll(resp.Body)
|
||||
return nil, fmt.Errorf("StatusCode: %d %s", resp.StatusCode, string(msg))
|
||||
}
|
||||
return resp.Body, err
|
||||
@@ -953,7 +966,7 @@ func (client *SGoogleClient) GetSubAccounts() ([]cloudprovider.SSubAccount, erro
|
||||
func (self *SGoogleClient) GetIRegionById(id string) (cloudprovider.ICloudRegion, error) {
|
||||
for i := 0; i < len(self.iregions); i++ {
|
||||
if self.iregions[i].GetGlobalId() == id {
|
||||
return self.iregions[i].(*SRegion), nil
|
||||
return self.iregions[i], nil
|
||||
}
|
||||
}
|
||||
return nil, cloudprovider.ErrNotFound
|
||||
|
||||
+7
-3
@@ -59,9 +59,10 @@ var LatitudeAndLongitude = map[string]cloudprovider.SGeographicInfo{
|
||||
"us-south1": api.RegionDallas,
|
||||
"southamerica-west1": api.RegionSantiago,
|
||||
|
||||
"me-west1": api.RegionTelAviv,
|
||||
"me-central1": api.RegionDoha,
|
||||
"me-central2": api.RegionDamman,
|
||||
"me-west1": api.RegionTelAviv,
|
||||
"me-central1": api.RegionDoha,
|
||||
"me-central2": api.RegionDamman,
|
||||
"africa-south1": api.RegionJohannesburg,
|
||||
}
|
||||
|
||||
var RegionNames = map[string]string{
|
||||
@@ -116,4 +117,7 @@ var RegionNames = map[string]string{
|
||||
// Dual-region
|
||||
"nam4": "爱荷华和南卡罗来纳",
|
||||
"eur4": "荷兰和芬兰",
|
||||
|
||||
"global": "全球",
|
||||
"africa-south1": "约翰内斯堡",
|
||||
}
|
||||
|
||||
+5
-5
@@ -91,7 +91,7 @@ func (self *SLoadbalancer) GetSysTags() map[string]string {
|
||||
|
||||
ips := []string{}
|
||||
for i := range frs {
|
||||
if len(frs[i].IPAddress) > 0 {
|
||||
if len(frs[i].IPAddress) > 0 && !utils.IsInStringArray(frs[i].IPAddress, ips) {
|
||||
ips = append(ips, frs[i].IPAddress)
|
||||
}
|
||||
}
|
||||
@@ -137,7 +137,6 @@ func (self *SLoadbalancer) GetAddress() string {
|
||||
return frs[i].IPAddress
|
||||
}
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
@@ -344,7 +343,8 @@ func (self *SLoadbalancer) GetILoadBalancerListenerById(listenerId string) (clou
|
||||
}
|
||||
|
||||
// GET https://compute.googleapis.com/compute/v1/projects/{project}/aggregated/targetHttpProxies 前端监听
|
||||
// tcp lb backend type: backend service
|
||||
//
|
||||
// tcp lb backend type: backend service
|
||||
func (self *SRegion) GetRegionalTcpLoadbalancers() ([]SLoadbalancer, error) {
|
||||
bss, err := self.GetRegionalBackendServices("protocol eq TCP")
|
||||
if err != nil {
|
||||
@@ -368,7 +368,7 @@ func (self *SRegion) GetRegionalTcpLoadbalancers() ([]SLoadbalancer, error) {
|
||||
return lbs, nil
|
||||
}
|
||||
|
||||
// udp lb backend type: backend service
|
||||
// udp lb backend type: backend service
|
||||
func (self *SRegion) GetRegionalUdpLoadbalancers() ([]SLoadbalancer, error) {
|
||||
bss, err := self.GetRegionalBackendServices("protocol eq UDP")
|
||||
if err != nil {
|
||||
@@ -392,7 +392,7 @@ func (self *SRegion) GetRegionalUdpLoadbalancers() ([]SLoadbalancer, error) {
|
||||
return lbs, nil
|
||||
}
|
||||
|
||||
// http&https lb: urlmaps
|
||||
// http&https lb: urlmaps
|
||||
func (self *SRegion) GetRegionalHTTPLoadbalancers() ([]SLoadbalancer, error) {
|
||||
ums, err := self.GetRegionalUrlMaps("")
|
||||
if err != nil {
|
||||
|
||||
+13
-4
@@ -57,10 +57,8 @@ type SForwardingRule struct {
|
||||
BackendService string `json:"backendService"`
|
||||
}
|
||||
|
||||
//
|
||||
//type STargetProxy struct {
|
||||
//}
|
||||
//
|
||||
// type STargetProxy struct {
|
||||
// }
|
||||
type SBackendServices struct {
|
||||
SResourceBase
|
||||
|
||||
@@ -118,6 +116,17 @@ type STargetHttpsProxy struct {
|
||||
Kind string `json:"kind"`
|
||||
}
|
||||
|
||||
type STargetTcpProxy struct {
|
||||
SResourceBase
|
||||
|
||||
Kind string `json:"kind"`
|
||||
CreationTimestamp string `json:"creationTimestamp"`
|
||||
Description string `json:"description"`
|
||||
Service string `json:"service"`
|
||||
Region string `json:"region"`
|
||||
ProxyBind bool `json:"proxyBind"`
|
||||
}
|
||||
|
||||
type SInstanceGroup struct {
|
||||
SResourceBase
|
||||
region *SRegion
|
||||
|
||||
+1
-1
@@ -842,4 +842,4 @@ func (region *SRegion) GetIVMs() ([]cloudprovider.ICloudVM, error) {
|
||||
iVMs = append(iVMs, &instances[i])
|
||||
}
|
||||
return iVMs, nil
|
||||
}
|
||||
}
|
||||
+5
-1
@@ -32,11 +32,15 @@ func (r *SResourceBase) GetId() string {
|
||||
return r.SelfLink
|
||||
}
|
||||
|
||||
func getGlobalId(selfLink string) string {
|
||||
return strings.TrimPrefix(selfLink, fmt.Sprintf("%s/%s/", GOOGLE_COMPUTE_DOMAIN, GOOGLE_API_VERSION))
|
||||
}
|
||||
|
||||
func (r *SResourceBase) GetGlobalId() string {
|
||||
if len(r.Id) > 0 {
|
||||
return r.Id
|
||||
}
|
||||
return strings.TrimPrefix(r.SelfLink, fmt.Sprintf("%s/%s/", GOOGLE_COMPUTE_DOMAIN, GOOGLE_API_VERSION))
|
||||
return getGlobalId(r.SelfLink)
|
||||
}
|
||||
|
||||
func (r *SResourceBase) GetName() string {
|
||||
|
||||
Reference in New Issue
Block a user