From d9dfd1b1323148a711a8667dacf2d462064e46db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B1=88=E8=BD=A9?= Date: Wed, 8 May 2024 19:17:55 +0800 Subject: [PATCH] fix(region): vendor update for azure lb sync (#20212) --- go.mod | 4 +- go.sum | 8 +- pkg/compute/models/loadbalancerlisteners.go | 12 +- pkg/compute/models/loadbalancernetworks.go | 1 + pkg/compute/models/loadbalancers.go | 20 +- vendor/modules.txt | 4 +- .../cloudmux/pkg/multicloud/azure/azure_v2.go | 18 +- .../x/cloudmux/pkg/multicloud/azure/eip.go | 11 +- .../pkg/multicloud/azure/loadbalancer.go | 742 +++++------------- .../azure/loadbalancer_http_listener.go | 337 ++++++++ .../multicloud/azure/loadbalancer_props.go | 101 +-- .../multicloud/azure/loadbalancerbackend.go | 40 +- .../azure/loadbalancerbackendgroup.go | 148 ++-- .../pkg/multicloud/azure/loadbalancercert.go | 90 +-- .../multicloud/azure/loadbalancerlistener.go | 422 ++-------- .../azure/loadbalancerlistenerrule.go | 192 ----- .../x/cloudmux/pkg/multicloud/azure/region.go | 97 +-- .../yunion.io/x/pkg/util/regutils/regutils.go | 7 + .../x/pkg/util/timeutils/timeutils.go | 12 + 19 files changed, 817 insertions(+), 1449 deletions(-) create mode 100644 vendor/yunion.io/x/cloudmux/pkg/multicloud/azure/loadbalancer_http_listener.go delete mode 100644 vendor/yunion.io/x/cloudmux/pkg/multicloud/azure/loadbalancerlistenerrule.go diff --git a/go.mod b/go.mod index 6a0e5de08e..73784d5ace 100644 --- a/go.mod +++ b/go.mod @@ -89,12 +89,12 @@ 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.20240429100110-5050e96942a9 + yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20240508104627-3dc24b928449 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 yunion.io/x/ovsdb v0.0.0-20230306173834-f164f413a900 - yunion.io/x/pkg v1.10.1-0.20240424071144-ff6b95d2046d + yunion.io/x/pkg v1.10.1-0.20240508102846-e4ad08e1f410 yunion.io/x/s3cli v0.0.0-20190917004522-13ac36d8687e yunion.io/x/sqlchemy v1.1.3-0.20240417115142-4f4271cd8305 yunion.io/x/structarg v0.0.0-20231017124457-df4d5009457c diff --git a/go.sum b/go.sum index 28df96bd79..a4a621f8bf 100644 --- a/go.sum +++ b/go.sum @@ -1213,8 +1213,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.20240429100110-5050e96942a9 h1:2xOTXg5YGTC2fb+jQQZffrScamLH8UBJL7WFaDeLZX4= -yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20240429100110-5050e96942a9/go.mod h1:PkfAKJcu5mic7jyJpT4GxZ8BKIVMa+6eyOP2G94VKAs= +yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20240508104627-3dc24b928449 h1:DWzo0+ldBNvESHUazQnANn57O2vqmDGBphc+uoVFBoY= +yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20240508104627-3dc24b928449/go.mod h1:PkfAKJcu5mic7jyJpT4GxZ8BKIVMa+6eyOP2G94VKAs= 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= @@ -1228,8 +1228,8 @@ yunion.io/x/ovsdb v0.0.0-20230306173834-f164f413a900 h1:Hu/4ERvoWaN6aiFs4h4/yvVB yunion.io/x/ovsdb v0.0.0-20230306173834-f164f413a900/go.mod h1:0vLkNEhlmA64HViPBAnSTUMrx5QP1CLsxXmxDKQ80tc= yunion.io/x/pkg v0.0.0-20190620104149-945c25821dbf/go.mod h1:t6rEGG2sQ4J7DhFxSZVOTjNd0YO/KlfWQyK1W4tog+E= yunion.io/x/pkg v0.0.0-20190628082551-f4033ba2ea30/go.mod h1:t6rEGG2sQ4J7DhFxSZVOTjNd0YO/KlfWQyK1W4tog+E= -yunion.io/x/pkg v1.10.1-0.20240424071144-ff6b95d2046d h1:kl78atYnRcYjVeo/TjMXNMJL5F5N9rnqVHwpQXrG/yc= -yunion.io/x/pkg v1.10.1-0.20240424071144-ff6b95d2046d/go.mod h1:ksCJVQ+DwKrJ5QBEoU8pzrDFfDaZVAFH/iJ6yQCYxJk= +yunion.io/x/pkg v1.10.1-0.20240508102846-e4ad08e1f410 h1:ytervMn5lW8X4iHytK14t5FZlvB4boKNcQsFFGonQRM= +yunion.io/x/pkg v1.10.1-0.20240508102846-e4ad08e1f410/go.mod h1:ksCJVQ+DwKrJ5QBEoU8pzrDFfDaZVAFH/iJ6yQCYxJk= yunion.io/x/s3cli v0.0.0-20190917004522-13ac36d8687e h1:v+EzIadodSwkdZ/7bremd7J8J50Cise/HCylsOJngmo= yunion.io/x/s3cli v0.0.0-20190917004522-13ac36d8687e/go.mod h1:0iFKpOs1y4lbCxeOmq3Xx/0AcQoewVPwj62eRluioEo= yunion.io/x/sqlchemy v1.1.3-0.20240417115142-4f4271cd8305 h1:TIXzc634+YSAs2+3A+TyPmKAGd6HtAYU3M4cKwzUuo0= diff --git a/pkg/compute/models/loadbalancerlisteners.go b/pkg/compute/models/loadbalancerlisteners.go index d370d288c8..740c5ebfd5 100644 --- a/pkg/compute/models/loadbalancerlisteners.go +++ b/pkg/compute/models/loadbalancerlisteners.go @@ -20,6 +20,7 @@ import ( "yunion.io/x/cloudmux/pkg/cloudprovider" "yunion.io/x/jsonutils" + "yunion.io/x/log" "yunion.io/x/pkg/errors" "yunion.io/x/pkg/util/compare" "yunion.io/x/pkg/util/rbacscope" @@ -884,14 +885,15 @@ func (lblis *SLoadbalancerListener) constructFieldsFromCloudListener(userCred mc lblis.TLSCipherPolicy = extListener.GetTLSCipherPolicy() lblis.EnableHttp2 = extListener.HTTP2Enabled() if certificateId := extListener.GetCertificateId(); len(certificateId) > 0 { - if _cert, err := db.FetchByExternalIdAndManagerId(LoadbalancerCertificateManager, certificateId, func(q *sqlchemy.SQuery) *sqlchemy.SQuery { + cert, err := db.FetchByExternalIdAndManagerId(LoadbalancerCertificateManager, certificateId, func(q *sqlchemy.SQuery) *sqlchemy.SQuery { return q.Equals("manager_id", lb.ManagerId) - }); err == nil { - cert := _cert.(*SLoadbalancerCertificate) - lblis.CertificateId = cert.Id + }) + if err != nil { + log.Errorf("fetch cert %s error: %v", certificateId, err) + } else { + lblis.CertificateId = cert.GetId() } } - fallthrough case api.LB_LISTENER_TYPE_HTTP: if len(extListener.GetStickySessionType()) > 0 { if lblis.GetProviderName() == api.CLOUD_PROVIDER_QCLOUD && utils.IsInStringArray(lblis.ListenerType, []string{api.LB_LISTENER_TYPE_HTTP, api.LB_LISTENER_TYPE_HTTPS}) { diff --git a/pkg/compute/models/loadbalancernetworks.go b/pkg/compute/models/loadbalancernetworks.go index ee6f4ebe0d..1f8e63bed1 100644 --- a/pkg/compute/models/loadbalancernetworks.go +++ b/pkg/compute/models/loadbalancernetworks.go @@ -188,6 +188,7 @@ func (m *SLoadbalancernetworkManager) syncLoadbalancerNetwork(ctx context.Contex } if len(lns) == 0 { ln := &SLoadbalancerNetwork{LoadbalancerId: req.Loadbalancer.Id, NetworkId: req.NetworkId, IpAddr: req.Address} + ln.SetModelManager(LoadbalancernetworkManager, ln) return m.TableSpec().Insert(ctx, ln) } for i := 0; i < len(lns); i++ { diff --git a/pkg/compute/models/loadbalancers.go b/pkg/compute/models/loadbalancers.go index 37ec18f81d..a2d311a519 100644 --- a/pkg/compute/models/loadbalancers.go +++ b/pkg/compute/models/loadbalancers.go @@ -119,9 +119,6 @@ type SLoadbalancer struct { // 默认后端服务器组Id BackendGroupId string `width:"36" charset:"ascii" nullable:"true" list:"user" update:"user" json:"backend_group_id"` - - // LB的其他配置信息 - LBInfo jsonutils.JSONObject `charset:"utf8" length:"medium" nullable:"true" list:"user" update:"admin" create:"admin_optional" json:"lb_info"` } // 负载均衡实例列表 @@ -1105,10 +1102,6 @@ func (region *SCloudregion) newFromCloudLoadbalancer(ctx context.Context, userCr } } - if ext.GetSysTags() != nil { - lb.LBInfo = jsonutils.Marshal(ext.GetSysTags()) - } - syncOwnerId := provider.GetOwnerId() err = func() error { @@ -1401,8 +1394,17 @@ func (lb *SLoadbalancer) syncWithCloudLoadbalancer(ctx context.Context, userCred lb.ChargeType = ext.GetChargeType() lbNetworkIds := getExtLbNetworkIds(ext, lb.ManagerId) lb.NetworkId = strings.Join(lbNetworkIds, ",") - if ext.GetSysTags() != nil { - lb.LBInfo = jsonutils.Marshal(ext.GetSysTags()) + if len(lb.VpcId) == 0 { + if vpcId := ext.GetVpcId(); len(vpcId) > 0 { + vpc, err := db.FetchByExternalIdAndManagerId(VpcManager, vpcId, func(q *sqlchemy.SQuery) *sqlchemy.SQuery { + return q.Equals("manager_id", lb.ManagerId) + }) + if err != nil { + log.Errorf("fetch vpc %s error: %v", vpcId, err) + } else { + lb.VpcId = vpc.GetId() + } + } } if createdAt := ext.GetCreatedAt(); !createdAt.IsZero() { diff --git a/vendor/modules.txt b/vendor/modules.txt index a27f78c95f..d4d2d58f3c 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -1483,7 +1483,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.20240429100110-5050e96942a9 +# yunion.io/x/cloudmux v0.3.10-0-alpha.1.0.20240508104627-3dc24b928449 ## explicit; go 1.18 yunion.io/x/cloudmux/pkg/apis yunion.io/x/cloudmux/pkg/apis/billing @@ -1580,7 +1580,7 @@ yunion.io/x/log/hooks yunion.io/x/ovsdb/cli_util yunion.io/x/ovsdb/schema/ovn_nb yunion.io/x/ovsdb/types -# yunion.io/x/pkg v1.10.1-0.20240424071144-ff6b95d2046d +# yunion.io/x/pkg v1.10.1-0.20240508102846-e4ad08e1f410 ## explicit; go 1.18 yunion.io/x/pkg/appctx yunion.io/x/pkg/errors diff --git a/vendor/yunion.io/x/cloudmux/pkg/multicloud/azure/azure_v2.go b/vendor/yunion.io/x/cloudmux/pkg/multicloud/azure/azure_v2.go index 898ccb8436..f32ef67dcf 100644 --- a/vendor/yunion.io/x/cloudmux/pkg/multicloud/azure/azure_v2.go +++ b/vendor/yunion.io/x/cloudmux/pkg/multicloud/azure/azure_v2.go @@ -141,8 +141,24 @@ func (self *SAzureClient) _list_v2(service string, resource, apiVersion string, } domain := azServices[service][self.envName] - url := fmt.Sprintf("%s/%s", domain, resource) + url := fmt.Sprintf("%s/%s", strings.TrimSuffix(domain, "/"), strings.TrimPrefix(resource, "/")) + if service == SERVICE_MANAGEMENT { + switch resource { + case "subscriptions": + case "locations", "resourcegroups", "resources": + url = fmt.Sprintf("%s/subscriptions/%s/%s", strings.TrimSuffix(domain, "/"), self.subscriptionId, resource) + default: + if !strings.HasPrefix(resource, "/") { + url = fmt.Sprintf("%s/subscriptions/%s/providers/%s", strings.TrimSuffix(domain, "/"), self.subscriptionId, resource) + } + } + } if len(params) > 0 { + filters := []string{} + if params.Has("$filter") { + filters = params["$filter"] + params.Set("$filter", strings.Join(filters, " and ")) + } url += fmt.Sprintf("?%s", params.Encode()) } _, resp, err := httputils.JSONRequest(self, self.ctx, httputils.GET, url, nil, nil, self.debug) diff --git a/vendor/yunion.io/x/cloudmux/pkg/multicloud/azure/eip.go b/vendor/yunion.io/x/cloudmux/pkg/multicloud/azure/eip.go index 3055a42940..64c942d19c 100644 --- a/vendor/yunion.io/x/cloudmux/pkg/multicloud/azure/eip.go +++ b/vendor/yunion.io/x/cloudmux/pkg/multicloud/azure/eip.go @@ -170,12 +170,9 @@ func (region *SRegion) DissociateEip(eipId string) error { } func (self *SEipAddress) GetAssociationExternalId() string { - if self.GetAssociationType() == api.EIP_ASSOCIATE_TYPE_SERVER { - info := strings.Split(self.Properties.IPConfiguration.ID, "/") - nic, _ := self.region.GetNetworkInterface(strings.Join(info[:len(info)-2], "/")) - if nic != nil { - return strings.ToLower(nic.Properties.VirtualMachine.ID) - } + info := strings.Split(self.Properties.IPConfiguration.ID, "/") + if len(info) > 2 { + return strings.ToLower(strings.Join(info[:len(info)-2], "/")) } return "" } @@ -189,7 +186,7 @@ func (self *SEipAddress) GetAssociationType() string { if utils.IsInStringArray(resType, []string{"networkinterfaces"}) { return api.EIP_ASSOCIATE_TYPE_SERVER } - if utils.IsInStringArray(resType, []string{"loadbalancers"}) { + if utils.IsInStringArray(resType, []string{"loadbalancers", "applicationgateways"}) { return api.EIP_ASSOCIATE_TYPE_LOADBALANCER } return resType diff --git a/vendor/yunion.io/x/cloudmux/pkg/multicloud/azure/loadbalancer.go b/vendor/yunion.io/x/cloudmux/pkg/multicloud/azure/loadbalancer.go index e3575b04a8..40c9db43b0 100644 --- a/vendor/yunion.io/x/cloudmux/pkg/multicloud/azure/loadbalancer.go +++ b/vendor/yunion.io/x/cloudmux/pkg/multicloud/azure/loadbalancer.go @@ -16,14 +16,12 @@ package azure import ( "context" - "strconv" + "fmt" + "net/url" "strings" - "github.com/pkg/errors" - "yunion.io/x/jsonutils" - "yunion.io/x/log" - "yunion.io/x/pkg/utils" + "yunion.io/x/pkg/errors" api "yunion.io/x/cloudmux/pkg/apis/compute" "yunion.io/x/cloudmux/pkg/cloudprovider" @@ -33,18 +31,15 @@ import ( type SLoadbalancer struct { multicloud.SLoadbalancerBase AzureTags - region *SRegion - eips []cloudprovider.ICloudEIP - lbbgs []cloudprovider.ICloudLoadbalancerBackendGroup - listeners []cloudprovider.ICloudLoadbalancerListener + region *SRegion - Name string `json:"name"` - ID string `json:"id"` - Etag string `json:"etag"` - Type string `json:"type"` - Location string `json:"location"` - Properties SLoadbalancerProperties `json:"properties"` - Sku Sku `json:"sku"` + Name string `json:"name"` + ID string `json:"id"` + Etag string `json:"etag"` + Type string `json:"type"` + Location string `json:"location"` + Properties *SLoadbalancerProperties `json:"properties"` + Sku Sku `json:"sku"` } func (self *SLoadbalancer) GetId() string { @@ -59,8 +54,24 @@ func (self *SLoadbalancer) GetGlobalId() string { return strings.ToLower(self.GetId()) } +func (self *SLoadbalancer) GetProperties() (*SLoadbalancerProperties, error) { + if self.Properties != nil { + return self.Properties, nil + } + lb, err := self.region.GetLoadbalancer(self.ID) + if err != nil { + return nil, err + } + self.Properties = lb.Properties + return self.Properties, nil +} + func (self *SLoadbalancer) GetStatus() string { - switch self.Properties.ProvisioningState { + properties, err := self.GetProperties() + if err != nil { + return api.LB_STATUS_UNKNOWN + } + switch properties.ProvisioningState { case "Deleting": return api.LB_STATUS_DELETING case "Failed": @@ -86,181 +97,87 @@ func (self *SLoadbalancer) GetStatus() string { } func (self *SLoadbalancer) Refresh() error { - lb, err := self.region.GetILoadBalancerById(self.GetId()) + lb, err := self.region.GetLoadbalancer(self.ID) if err != nil { - return errors.Wrap(err, "GetILoadBalancerById.Refresh") + return errors.Wrap(err, "GetLoadbalancer") } - - err = jsonutils.Update(self, lb) - if err != nil { - return errors.Wrap(err, "jsonutils.Update") - } - - self.eips = nil - self.lbbgs = nil - self.listeners = nil - return nil -} - -func (self *SLoadbalancer) IsEmulated() bool { - return false -} - -func (self *SLoadbalancer) GetSysTags() map[string]string { - data := map[string]string{} - data["loadbalance_type"] = self.Type - data["capacity"] = self.Properties.Sku.Capacity - data["max_capacity"] = strconv.Itoa(self.Properties.AutoscaleConfiguration.MaxCapacity) - data["min_capacity"] = strconv.Itoa(self.Properties.AutoscaleConfiguration.MinCapacity) - data["properties"] = jsonutils.Marshal(self.Properties).String() - return data + return jsonutils.Update(self, lb) } func (self *SLoadbalancer) GetProjectId() string { return getResourceGroup(self.ID) } -func (self *SLoadbalancer) getAddresses() [][]string { - ret := [][]string{[]string{}, []string{}} - for _, fip := range self.Properties.FrontendIPConfigurations { - eip := fip.Properties.PublicIPAddress - if eip != nil && len(eip.Properties.IPAddress) > 0 { - ret[0] = append(ret[0], fip.Properties.PublicIPAddress.Properties.IPAddress) - continue - } - - if len(fip.Properties.PrivateIPAddress) > 0 { - ret[1] = append(ret[1], fip.Properties.PrivateIPAddress) - continue - } - } - - return ret -} - func (self *SLoadbalancer) GetAddress() string { - ips := self.getAddresses() - if len(ips[0]) > 0 { - return ips[0][0] - } else if len(ips[1]) > 0 { - return ips[1][0] - } else { + properties, err := self.GetProperties() + if err != nil { return "" } + for _, front := range properties.FrontendIPConfigurations { + if len(front.Properties.PrivateIPAddress) > 0 { + return front.Properties.PrivateIPAddress + } + } + return "" } func (self *SLoadbalancer) GetAddressType() string { - ips := self.getAddresses() - if len(ips[0]) > 0 { - return api.LB_ADDR_TYPE_INTERNET - } else if len(ips[1]) > 0 { - return api.LB_ADDR_TYPE_INTRANET - } else { + netIds := self.GetNetworkIds() + if len(netIds) > 0 { return api.LB_ADDR_TYPE_INTRANET } + return api.LB_ADDR_TYPE_INTERNET } func (self *SLoadbalancer) GetNetworkType() string { return api.LB_NETWORK_TYPE_VPC } -func (self *SLoadbalancer) getNetworkIds() []string { - ret := []string{} - for _, fip := range self.Properties.FrontendIPConfigurations { - subnet := fip.Properties.Subnet - if len(subnet.ID) > 0 { - ret = append(ret, subnet.ID) - } - } - - return ret -} - -func (self *SLoadbalancer) getLbbgNetworkIds() []string { - ret := []string{} - for _, b := range self.Properties.BackendAddressPools { - bips := b.Properties.BackendIPConfigurations - if len(bips) > 0 { - for _, ip := range bips { - if strings.Contains(ip.ID, "Microsoft.Network/networkInterfaces") { - nic, _ := self.region.GetNetworkInterface(strings.Split(ip.ID, "/ipConfigurations")[0]) - if nic != nil && len(nic.Properties.IPConfigurations) > 0 { - ipc := nic.Properties.IPConfigurations[0] - if len(ipc.Properties.Subnet.ID) > 0 { - return []string{ipc.Properties.Subnet.ID} - } - } - } - } - } - } - - return ret -} - func (self *SLoadbalancer) GetNetworkIds() []string { - return self.getNetworkIds() + properties, err := self.GetProperties() + if err != nil { + return []string{} + } + for _, front := range properties.FrontendIPConfigurations { + if len(front.Properties.Subnet.ID) > 0 { + return []string{strings.ToLower(front.Properties.Subnet.ID)} + } + } + for _, gateway := range properties.GatewayIPConfigurations { + if len(gateway.Properties.Subnet.ID) > 0 { + return []string{strings.ToLower(gateway.Properties.Subnet.ID)} + } + } + return []string{} } func (self *SLoadbalancer) GetVpcId() string { - if self.Type == "Microsoft.Network/loadBalancers" { - subnets := self.GetNetworkIds() - if len(subnets) == 0 { - subnets = self.getLbbgNetworkIds() - } - - if len(subnets) > 0 { - network, err := self.region.GetNetwork(subnets[0]) - if network != nil { - return strings.Split(network.GetId(), "/subnets")[0] - } - - log.Errorf("GetNetwork %s: %s", subnets[0], err) - } - } else { - gips := self.Properties.GatewayIPConfigurations - for i := range gips { - netId := gips[i].Properties.Subnet.ID - if len(netId) > 0 { - network, err := self.region.GetNetwork(netId) - if network != nil { - return strings.Split(network.GetId(), "/subnets")[0] - } - log.Errorf("GetNetwork %s: %s", netId, err) - } + ids := self.GetNetworkIds() + for _, netId := range ids { + if strings.Contains(netId, "/subnets") { + return strings.Split(netId, "/subnets")[0] } } - return "" } func (self *SLoadbalancer) GetZoneId() string { - ips := self.Properties.FrontendIPConfigurations - if len(ips) > 0 && len(ips[0].Zones) > 0 { - return ips[0].Zones[0] - } - return "" } func (self *SLoadbalancer) GetZone1Id() string { - ips := self.Properties.FrontendIPConfigurations - if len(ips) > 0 && len(ips[0].Zones) > 1 { - return ips[0].Zones[1] - } - return "" } func (self *SLoadbalancer) GetLoadbalancerSpec() string { - if len(self.Properties.Sku.Name) > 0 { - return self.Properties.Sku.Name - } - if len(self.Sku.Name) > 0 { return self.Sku.Name } - return "" + properties, err := self.GetProperties() + if err != nil { + return "" + } + return properties.Sku.Name } func (self *SLoadbalancer) GetChargeType() string { @@ -271,67 +188,21 @@ func (self *SLoadbalancer) GetEgressMbps() int { return 0 } -func (self *SLoadbalancer) getEipIds() []string { - ret := []string{} - for _, fip := range self.Properties.FrontendIPConfigurations { - eip := fip.Properties.PublicIPAddress - if eip != nil && len(eip.ID) > 0 { - ret = append(ret, eip.ID) - continue - } - } - - return ret -} - func (self *SLoadbalancer) GetIEIP() (cloudprovider.ICloudEIP, error) { - ieips, err := self.GetIEIPS() + properties, err := self.GetProperties() if err != nil { - return nil, errors.Wrap(err, "GetIEIPS") + return nil, errors.Wrapf(err, "GetProperties") } - - if len(ieips) > 0 { - return self.eips[0], nil - } - - return nil, nil -} - -func (self *SLoadbalancer) GetIEIPById(eipId string) (cloudprovider.ICloudEIP, error) { - ieips, err := self.GetIEIPS() - if err != nil { - return nil, errors.Wrap(err, "GetIEIPS") - } - - for i := range ieips { - if ieips[i].GetId() == eipId { - return ieips[i], nil + for _, front := range properties.FrontendIPConfigurations { + if len(front.Properties.PublicIPAddress.ID) > 0 { + eip, err := self.region.GetEip(front.Properties.PublicIPAddress.ID) + if err != nil { + return nil, err + } + return eip, nil } } - - return nil, errors.Wrap(cloudprovider.ErrNotFound, eipId) -} - -func (self *SLoadbalancer) GetIEIPS() ([]cloudprovider.ICloudEIP, error) { - if self.eips != nil { - return self.eips, nil - } - - eips := self.getEipIds() - ieips := make([]cloudprovider.ICloudEIP, 0) - for i := range eips { - ieip, err := self.region.GetIEipById(eips[i]) - if err != nil { - return nil, errors.Wrap(err, "GetIEIP") - } - ieips = append(ieips, ieip) - } - - if len(ieips) > 0 { - self.eips = ieips - } - - return self.eips, nil + return nil, cloudprovider.ErrNotFound } func (self *SLoadbalancer) Delete(ctx context.Context) error { @@ -346,325 +217,41 @@ func (self *SLoadbalancer) Stop() error { return errors.Wrap(cloudprovider.ErrNotImplemented, "Stop") } -/* -应用型LB: urlPathMaps(defaultBackendAddressPool+defaultBackendHttpSettings+requestRoutingRules+httpListeners)= Onecloud监听器 -4层LB: loadBalancingRules(前端) = Onecloud监听器 -*/ func (self *SLoadbalancer) GetILoadBalancerListeners() ([]cloudprovider.ICloudLoadbalancerListener, error) { - if self.listeners != nil { - return self.listeners, nil - } - - var err error - if self.Type == "Microsoft.Network/applicationGateways" { - self.listeners, err = self.getAppLBListeners() - } else { - self.listeners, err = self.getNetworkLBListeners() - } - - return self.listeners, err -} - -func (self *SLoadbalancer) getFrontendIPConfiguration(id string) *FrontendIPConfiguration { - fips := self.Properties.FrontendIPConfigurations - for i := range fips { - if fips[i].ID == id { - return &fips[i] - } - } - log.Debugf("getFrontendIPConfiguration %s not found", id) - return nil -} - -func (self *SLoadbalancer) getHttpListener(id string) *HTTPListener { - ss := self.Properties.HTTPListeners - for i := range ss { - if ss[i].ID == id { - return &ss[i] - } - - for j := range ss[i].Properties.RequestRoutingRules { - if ss[i].Properties.RequestRoutingRules[j].ID == id { - return &ss[i] - } - } - } - log.Debugf("getHttpListener %s not found", id) - return nil -} - -func (self *SLoadbalancer) getBackendAddressPool(id string) *BackendAddressPool { - ss := self.Properties.BackendAddressPools - for i := range ss { - if ss[i].ID == id { - return &ss[i] - } - } - log.Debugf("getBackendAddressPool %s not found", id) - return nil -} - -func (self *SLoadbalancer) getBackendHTTPSettingsCollection(id string) *BackendHTTPSettingsCollection { - ss := self.Properties.BackendHTTPSettingsCollection - for i := range ss { - if ss[i].ID == id { - return &ss[i] - } - } - log.Debugf("getBackendHTTPSettingsCollection %s not found", id) - return nil -} - -func (self *SLoadbalancer) getRedirectConfiguration(id string) *RedirectConfiguration { - ss := self.Properties.RedirectConfigurations - for i := range ss { - if ss[i].ID == id { - return &ss[i] - } - } - log.Debugf("getRedirectConfiguration %s not found", id) - return nil -} - -func (self *SLoadbalancer) getProbe(id string) *Probe { - ss := self.Properties.Probes - for i := range ss { - if ss[i].ID == id { - return &ss[i] - } - } - log.Debugf("getProbe %s not found", id) - return nil -} - -func (self *SLoadbalancer) getFrontendPort(id string) *FrontendPort { - ss := self.Properties.FrontendPorts - for i := range ss { - if ss[i].ID == id { - return &ss[i] - } - } - log.Debugf("getFrontendPort %s not found", id) - return nil -} - -func (self *SLoadbalancer) getRequestRoutingRule(id string) *RequestRoutingRule { - ss := self.Properties.RequestRoutingRules - for i := range ss { - if ss[i].ID == id { - return &ss[i] - } - } - log.Debugf("getRequestRoutingRule %s not found", id) - return nil -} - -func (self *SLoadbalancer) getURLPathMap(id string) *URLPathMap { - ss := self.Properties.URLPathMaps - for i := range ss { - if ss[i].ID == id { - return &ss[i] - } - } - log.Debugf("getURLPathMap %s not found", id) - return nil -} - -/* -应用型LB: urlPathMaps(defaultBackendAddressPool+defaultBackendHttpSettings+requestRoutingRules+httpListeners)= Onecloud监听器 -*/ -func (self *SLoadbalancer) getAppLBListeners() ([]cloudprovider.ICloudLoadbalancerListener, error) { - lbls := []cloudprovider.ICloudLoadbalancerListener{} - for i := range self.Properties.RequestRoutingRules { - r := self.Properties.RequestRoutingRules[i] - uid := strings.Replace(r.ID, "requestRoutingRules", "urlPathMaps", 1) - u := self.getURLPathMap(uid) - lbl := self.getAppLBListener(&r, u) - if lbl != nil { - lbls = append(lbls, lbl) - } - } - - return lbls, nil -} - -func (self *SLoadbalancer) getAppLBListener(r *RequestRoutingRule, u *URLPathMap) *SLoadBalancerListener { - var redirect *RedirectConfiguration - var listener *HTTPListener - var fp *FrontendIPConfiguration - var fpp *FrontendPort - // frontendPort, frontendIPConfiguration, sslCertificate, requestRoutingRules - listener = self.getHttpListener(r.Properties.HTTPListener.ID) - // frontendPorts - fp = self.getFrontendIPConfiguration(listener.Properties.FrontendIPConfiguration.ID) - fpp = self.getFrontendPort(listener.Properties.FrontendPort.ID) - // 配置有异常?? - if fpp == nil || listener == nil { - return nil - } - - lbbgId := r.Properties.BackendAddressPool.ID - lbbgSettingId := r.Properties.BackendHTTPSettings.ID - provisioningState := "Succeeded" - rules := make([]PathRule, 0) - var backendGroup *BackendAddressPool - var backendSetting *BackendHTTPSettingsCollection - var backendPort int - var healthcheck *Probe - if u != nil { - provisioningState = u.Properties.ProvisioningState - rules = u.Properties.PathRules - if u.Properties.DefaultRedirectConfiguration != nil { - redirect = self.getRedirectConfiguration(u.Properties.DefaultRedirectConfiguration.ID) - } - - if len(lbbgId) == 0 && u.Properties.DefaultBackendAddressPool != nil { - lbbgId = u.Properties.DefaultBackendAddressPool.ID - } - - if len(lbbgSettingId) == 0 && u.Properties.DefaultBackendHTTPSettings != nil { - lbbgSettingId = u.Properties.DefaultBackendHTTPSettings.ID - } - } - - if len(lbbgId) > 0 { - backendGroup = self.getBackendAddressPool(lbbgId) - } - - if len(lbbgSettingId) > 0 { - backendSetting = self.getBackendHTTPSettingsCollection(lbbgSettingId) - backendPort = backendSetting.Properties.Port - } - - if backendSetting != nil && backendSetting.Properties.Probe != nil { - healthcheck = self.getProbe(backendSetting.Properties.Probe.ID) - } - - return &SLoadBalancerListener{ - lb: self, - fp: fp, - listener: listener, - backendSetting: backendSetting, - backendGroup: backendGroup, - redirect: redirect, - healthcheck: healthcheck, - Name: r.Name, - ID: r.ID, - ProvisioningState: provisioningState, - IPVersion: "", - Protocol: listener.Properties.Protocol, - LoadDistribution: "", - FrontendPort: fpp.Properties.Port, - BackendPort: backendPort, - ClientIdleTimeout: 0, - EnableFloatingIP: false, - EnableTcpReset: false, - rules: rules, - } -} - -func (self *SLoadbalancer) getNetworkLBListeners() ([]cloudprovider.ICloudLoadbalancerListener, error) { - lbls := []cloudprovider.ICloudLoadbalancerListener{} - for i := range self.Properties.LoadBalancingRules { - u := self.Properties.LoadBalancingRules[i] - lbl := self.getNetworkLBListener(&u) - lbls = append(lbls, &lbl) - } - - return lbls, nil -} - -func (self *SLoadbalancer) getNetworkLBListener(u *LoadBalancingRule) SLoadBalancerListener { - fp := self.getFrontendIPConfiguration(u.Properties.FrontendIPConfiguration.ID) - backendGroup := self.getBackendAddressPool(u.Properties.BackendAddressPool.ID) - healthcheck := self.getProbe(u.Properties.Probe.ID) - - return SLoadBalancerListener{ - lb: self, - fp: fp, - backendGroup: backendGroup, - healthcheck: healthcheck, - Name: u.Name, - ID: u.ID, - ProvisioningState: u.Properties.ProvisioningState, - IPVersion: "", - Protocol: u.Properties.Protocol, - LoadDistribution: u.Properties.LoadDistribution, - FrontendPort: u.Properties.FrontendPort, - BackendPort: u.Properties.BackendPort, - ClientIdleTimeout: u.Properties.IdleTimeoutInMinutes * 60, - EnableFloatingIP: u.Properties.EnableFloatingIP, - EnableTcpReset: u.Properties.EnableTCPReset, - } -} - -// 应用型LB: HTTP 设置 + 后端池 = onecloud 后端服务器组 -// 4层LB: loadBalancingRules(backendPort)+ 后端池 = onecloud 后端服务器组 -func (self *SLoadbalancer) GetILoadBalancerBackendGroups() ([]cloudprovider.ICloudLoadbalancerBackendGroup, error) { - if self.lbbgs != nil { - return self.lbbgs, nil - } - - lbls, err := self.GetILoadBalancerListeners() + properties, err := self.GetProperties() if err != nil { - return nil, errors.Wrap(err, "GetILoadBalancerListeners") + return nil, err } + ret := []cloudprovider.ICloudLoadbalancerListener{} + idMap := map[string]bool{} - bgIds := []string{} - ilbbgs := []cloudprovider.ICloudLoadbalancerBackendGroup{} - for i := range lbls { - lbl := lbls[i].(*SLoadBalancerListener) - if lbl.backendGroup == nil { - continue - } + for _, listeners := range [][]SLoadBalancerListener{ + properties.InboundNatRules, + properties.OutboundRules, + properties.LoadBalancingRules, + } { + for i := range listeners { + listener := listeners[i] - bg := SLoadbalancerBackendGroup{ - lb: self, - Pool: *lbl.backendGroup, - DefaultPort: lbl.BackendPort, - HttpSettings: lbl.backendSetting, - BackendIps: lbl.backendGroup.Properties.BackendIPConfigurations, - } - - if !utils.IsInStringArray(bg.GetId(), bgIds) { - bgIds = append(bgIds, bg.GetId()) - ilbbgs = append(ilbbgs, &bg) - } - - // sync rules lbbg - if utils.IsInStringArray(lbl.GetListenerType(), []string{api.LB_LISTENER_TYPE_HTTP, api.LB_LISTENER_TYPE_HTTPS}) { - rules, err := lbl.GetILoadbalancerListenerRules() - if err != nil { - return nil, errors.Wrap(err, "GetILoadbalancerListenerRules") - } - - for j := range rules { - rule := rules[j].(*SLoadbalancerListenerRule) - if rule.lbbg != nil && !utils.IsInStringArray(rule.lbbg.GetId(), bgIds) { - bgIds = append(bgIds, bg.GetId()) - ilbbgs = append(ilbbgs, &bg) - } + listener.lb = self + if _, ok := idMap[listener.GetGlobalId()]; !ok { + ret = append(ret, &listener) + idMap[listener.GetGlobalId()] = true } } } - self.lbbgs = ilbbgs - return ilbbgs, nil -} + for i := range properties.HTTPListeners { + listener := properties.HTTPListeners[i] -func (self *SLoadbalancer) GetILoadBalancerBackendGroupById(groupId string) (cloudprovider.ICloudLoadbalancerBackendGroup, error) { - lbbgs, err := self.GetILoadBalancerBackendGroups() - if err != nil { - return nil, errors.Wrap(err, "GetILoadBalancerBackendGroups") - } - - for i := range lbbgs { - if lbbgs[i].GetId() == groupId { - return lbbgs[i], nil + listener.lb = self + if _, ok := idMap[listener.GetGlobalId()]; !ok { + ret = append(ret, &listener) + idMap[listener.GetGlobalId()] = true } } - return nil, errors.Wrap(cloudprovider.ErrNotFound, "GetILoadBalancerBackendGroupById") + return ret, nil } func (self *SLoadbalancer) CreateILoadBalancerBackendGroup(group *cloudprovider.SLoadbalancerBackendGroup) (cloudprovider.ICloudLoadbalancerBackendGroup, error) { @@ -675,69 +262,94 @@ func (self *SLoadbalancer) CreateILoadBalancerListener(ctx context.Context, list return nil, errors.Wrap(cloudprovider.ErrNotImplemented, "CreateILoadBalancerListener") } -func (self *SLoadbalancer) getNetworkLBListenerById(id string) (cloudprovider.ICloudLoadbalancerListener, error) { - for i := range self.Properties.LoadBalancingRules { - u := self.Properties.LoadBalancingRules[i] - if u.ID == id { - lbl := self.getNetworkLBListener(&u) - return &lbl, nil +func (self *SLoadbalancer) GetILoadBalancerListenerById(id string) (cloudprovider.ICloudLoadbalancerListener, error) { + lblis, err := self.GetILoadBalancerListeners() + if err != nil { + return nil, err + } + for i := range lblis { + if lblis[i].GetGlobalId() == id { + return lblis[i], nil } } - - return nil, errors.Wrap(cloudprovider.ErrNotFound, "getNetworkLBListenerById") -} - -func (self *SLoadbalancer) getAppLBListenerById(id string) (cloudprovider.ICloudLoadbalancerListener, error) { - for i := range self.Properties.RequestRoutingRules { - r := self.Properties.RequestRoutingRules[i] - if r.ID == id { - u := self.getURLPathMap(strings.Replace(r.ID, "requestRoutingRules", "urlPathMaps", 1)) - return self.getAppLBListener(&r, u), nil - } - } - - return nil, errors.Wrap(cloudprovider.ErrNotFound, "getAppLBListenerById") -} - -func (self *SLoadbalancer) GetILoadBalancerListenerById(listenerId string) (cloudprovider.ICloudLoadbalancerListener, error) { - if self.Type == "Microsoft.Network/applicationGateways" { - return self.getAppLBListenerById(listenerId) - } else { - return self.getNetworkLBListenerById(listenerId) - } + return nil, errors.Wrapf(cloudprovider.ErrNotFound, id) } func (self *SLoadbalancer) GetILoadBalancerCertificates() ([]cloudprovider.ICloudLoadbalancerCertificate, error) { - icerts := []cloudprovider.ICloudLoadbalancerCertificate{} - ssl := self.Properties.SSLCertificates - for i := range ssl { - s := ssl[i] - cert := SLoadbalancerCert{ - lb: self, - Name: s.Name, - ID: s.ID, - PublicKey: s.Properties.PublicCertData, - } - - icerts = append(icerts, &cert) - } - - return icerts, nil + return nil, cloudprovider.ErrNotImplemented } func (self *SLoadbalancer) GetILoadBalancerCertificateById(certId string) (cloudprovider.ICloudLoadbalancerCertificate, error) { - ssl := self.Properties.SSLCertificates - for i := range ssl { - s := ssl[i] - if s.ID == certId { - return &SLoadbalancerCert{ - lb: self, - Name: s.Name, - ID: s.ID, - PublicKey: s.Properties.PublicCertData, - }, nil + return nil, cloudprovider.ErrNotImplemented +} + +func (self *SLoadbalancer) GetILoadBalancerBackendGroups() ([]cloudprovider.ICloudLoadbalancerBackendGroup, error) { + ret := []cloudprovider.ICloudLoadbalancerBackendGroup{} + properties, err := self.GetProperties() + if err != nil { + return nil, err + } + idMap := map[string]bool{} + for _, rule := range properties.InboundNatRules { + if len(rule.Properties.BackendIPConfiguration.Id) == 0 { + continue + } + lbbg := &SLoadbalancerBackendGroup{lb: self, Id: rule.Properties.BackendIPConfiguration.Id} + if _, ok := idMap[lbbg.GetGlobalId()]; !ok { + ret = append(ret, lbbg) + idMap[lbbg.GetGlobalId()] = true } } - return nil, errors.Wrap(cloudprovider.ErrNotFound, "GetILoadBalancerCertificateById") + for _, pool := range properties.BackendAddressPools { + lbbg := &SLoadbalancerBackendGroup{lb: self, Id: pool.Id, BackendIPConfigurations: pool.Properties.BackendIPConfigurations} + if _, ok := idMap[lbbg.GetGlobalId()]; !ok { + ret = append(ret, lbbg) + idMap[lbbg.GetGlobalId()] = true + } + } + + return ret, nil +} + +func (self *SLoadbalancer) GetILoadBalancerBackendGroupById(groupId string) (cloudprovider.ICloudLoadbalancerBackendGroup, error) { + lbbgs, err := self.GetILoadBalancerBackendGroups() + if err != nil { + return nil, err + } + for i := range lbbgs { + if lbbgs[i].GetGlobalId() == groupId { + return lbbgs[i], nil + } + } + return nil, errors.Wrapf(cloudprovider.ErrNotFound, groupId) +} + +func (self *SRegion) GetLoadbalancers() ([]SLoadbalancer, error) { + params := url.Values{} + params.Add("$filter", fmt.Sprintf("location eq '%s'", self.Name)) + params.Add("$filter", "(resourceType eq 'Microsoft.Network/applicationGateways' or resourceType eq 'Microsoft.Network/loadBalancers')") + resp, err := self.client.list_v2("resources", "2024-03-01", params) + if err != nil { + return nil, err + } + ret := []SLoadbalancer{} + err = resp.Unmarshal(&ret, "value") + if err != nil { + return nil, err + } + return ret, nil +} + +func (self *SRegion) GetLoadbalancer(id string) (*SLoadbalancer, error) { + resp, err := self.show(id, "2023-11-01") + if err != nil { + return nil, err + } + ret := &SLoadbalancer{region: self} + err = resp.Unmarshal(ret) + if err != nil { + return nil, err + } + return ret, nil } diff --git a/vendor/yunion.io/x/cloudmux/pkg/multicloud/azure/loadbalancer_http_listener.go b/vendor/yunion.io/x/cloudmux/pkg/multicloud/azure/loadbalancer_http_listener.go new file mode 100644 index 0000000000..719f3f522d --- /dev/null +++ b/vendor/yunion.io/x/cloudmux/pkg/multicloud/azure/loadbalancer_http_listener.go @@ -0,0 +1,337 @@ +// 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 azure + +import ( + "context" + "strings" + + "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 HttpListenerProperties struct { + ProvisioningState string + FrontendIPConfiguration struct { + Id string + } + FrontendPort struct { + Id string + } + Protocol string + RequestRoutingRules []struct { + Id string + } + Probe struct { + Id string + } +} + +type SLoadBalancerHTTPListener struct { + multicloud.SResourceBase + AzureTags + multicloud.SLoadbalancerRedirectBase + lb *SLoadbalancer + + Name string `json:"name"` + Id string `json:"id"` + Etag string `json:"etag"` + Type string `json:"type"` + Properties HttpListenerProperties `json:"properties"` +} + +func (self *SLoadBalancerHTTPListener) GetId() string { + return self.Id +} + +func (self *SLoadBalancerHTTPListener) GetName() string { + return self.Name +} + +func (self *SLoadBalancerHTTPListener) GetGlobalId() string { + return strings.ToLower(self.GetId()) +} + +func (self *SLoadBalancerHTTPListener) GetStatus() string { + switch self.Properties.ProvisioningState { + case "Succeeded", "Updating", "Deleting": + return api.LB_STATUS_ENABLED + case "Failed": + return api.LB_STATUS_DISABLED + default: + return api.LB_STATUS_UNKNOWN + } +} + +func (self *SLoadBalancerHTTPListener) Refresh() error { + lblis, err := self.lb.GetILoadBalancerListenerById(self.GetId()) + if err != nil { + return errors.Wrap(err, "GetILoadBalancerListenerById") + } + + return jsonutils.Update(self, lblis) +} + +func (listerner *SLoadBalancerHTTPListener) ChangeCertificate(ctx context.Context, opts *cloudprovider.ListenerCertificateOptions) error { + return cloudprovider.ErrNotImplemented +} + +func (listerner *SLoadBalancerHTTPListener) SetAcl(ctx context.Context, opts *cloudprovider.ListenerAclOptions) error { + return cloudprovider.ErrNotImplemented +} + +func (self *SLoadBalancerHTTPListener) GetProjectId() string { + return getResourceGroup(self.GetId()) +} + +func (self *SLoadBalancerHTTPListener) GetListenerType() string { + switch strings.ToLower(self.Properties.Protocol) { + case "tcp": + return api.LB_LISTENER_TYPE_TCP + case "udp": + return api.LB_LISTENER_TYPE_UDP + case "http": + return api.LB_LISTENER_TYPE_HTTP + case "https": + return api.LB_LISTENER_TYPE_HTTPS + default: + return strings.ToLower(self.Properties.Protocol) + } +} + +func (self *SLoadBalancerHTTPListener) GetListenerPort() int { + for _, port := range self.lb.Properties.FrontendPorts { + if port.ID == self.Properties.FrontendPort.Id { + return port.Properties.Port + } + } + return 0 +} + +func (self *SLoadBalancerHTTPListener) GetScheduler() string { + return "" +} + +func (self *SLoadBalancerHTTPListener) GetAclStatus() string { + return api.LB_BOOL_OFF +} + +func (self *SLoadBalancerHTTPListener) GetAclType() string { + return "" +} + +func (self *SLoadBalancerHTTPListener) GetAclId() string { + return "" +} + +func (self *SLoadBalancerHTTPListener) GetEgressMbps() int { + return 0 +} + +func (self *SLoadBalancerHTTPListener) GetHealthCheck() string { + if len(self.Properties.Probe.Id) > 0 { + return api.LB_BOOL_ON + } + return api.LB_BOOL_OFF +} + +func (self *SLoadBalancerHTTPListener) GetHealthCheckType() string { + for _, prob := range self.lb.Properties.Probes { + if strings.EqualFold(prob.Id, self.Properties.Probe.Id) { + return strings.ToLower(prob.Properties.Protocol) + } + } + return "" +} + +func (self *SLoadBalancerHTTPListener) GetHealthCheckTimeout() int { + return 0 +} + +func (self *SLoadBalancerHTTPListener) GetHealthCheckInterval() int { + for _, prob := range self.lb.Properties.Probes { + if strings.EqualFold(prob.Id, self.Properties.Probe.Id) { + return prob.Properties.IntervalInSeconds + } + } + return 0 +} + +func (self *SLoadBalancerHTTPListener) GetHealthCheckRise() int { + return 0 +} + +func (self *SLoadBalancerHTTPListener) GetHealthCheckFail() int { + for _, prob := range self.lb.Properties.Probes { + if strings.EqualFold(prob.Id, self.Properties.Probe.Id) { + return prob.Properties.NumberOfProbes + } + } + return 0 +} + +func (self *SLoadBalancerHTTPListener) GetHealthCheckReq() string { + return "" +} + +func (self *SLoadBalancerHTTPListener) GetHealthCheckExp() string { + return "" +} + +func (self *SLoadBalancerHTTPListener) GetBackendGroupId() string { + for _, rule := range self.lb.Properties.RequestRoutingRules { + if strings.EqualFold(rule.Properties.HTTPListener.ID, self.Id) { + return strings.ToLower(rule.Properties.BackendAddressPool.ID) + } + } + return "" +} + +func (self *SLoadBalancerHTTPListener) GetBackendServerPort() int { + return 0 +} + +func (self *SLoadBalancerHTTPListener) GetHealthCheckDomain() string { + return "" +} + +func (self *SLoadBalancerHTTPListener) GetHealthCheckURI() string { + return "" +} + +func (self *SLoadBalancerHTTPListener) GetHealthCheckCode() string { + return "" +} + +func (self *SLoadBalancerHTTPListener) CreateILoadBalancerListenerRule(rule *cloudprovider.SLoadbalancerListenerRule) (cloudprovider.ICloudLoadbalancerListenerRule, error) { + return nil, errors.Wrap(cloudprovider.ErrNotImplemented, "CreateILoadBalancerListenerRule") +} + +func (self *SLoadBalancerHTTPListener) GetILoadBalancerListenerRuleById(ruleId string) (cloudprovider.ICloudLoadbalancerListenerRule, error) { + lbrs, err := self.GetILoadbalancerListenerRules() + if err != nil { + return nil, errors.Wrap(err, "GetILoadbalancerListenerRules") + } + + for i := range lbrs { + if lbrs[i].GetId() == ruleId { + return lbrs[i], nil + } + } + + return nil, errors.Wrap(cloudprovider.ErrNotFound, ruleId) +} + +func (self *SLoadBalancerHTTPListener) GetILoadbalancerListenerRules() ([]cloudprovider.ICloudLoadbalancerListenerRule, error) { + return []cloudprovider.ICloudLoadbalancerListenerRule{}, nil +} + +func (self *SLoadBalancerHTTPListener) GetStickySession() string { + return api.LB_BOOL_OFF +} + +func (self *SLoadBalancerHTTPListener) GetStickySessionType() string { + if self.GetStickySession() == api.LB_BOOL_ON { + return api.LB_STICKY_SESSION_TYPE_INSERT + } + return "" +} + +func (self *SLoadBalancerHTTPListener) GetStickySessionCookie() string { + return "" +} + +func (self *SLoadBalancerHTTPListener) GetStickySessionCookieTimeout() int { + return 0 +} + +func (self *SLoadBalancerHTTPListener) XForwardedForEnabled() bool { + return false +} + +func (self *SLoadBalancerHTTPListener) GzipEnabled() bool { + return false +} + +func (self *SLoadBalancerHTTPListener) GetCertificateId() string { + for _, cert := range self.lb.Properties.SSLCertificates { + for _, lis := range cert.Properties.HttpListeners { + if strings.EqualFold(lis.Id, self.Id) { + return cert.GetGlobalId() + } + } + } + return "" +} + +func (self *SLoadBalancerHTTPListener) GetTLSCipherPolicy() string { + return "" +} + +func (self *SLoadBalancerHTTPListener) HTTP2Enabled() bool { + return self.lb.Properties.EnableHttp2 +} + +func (self *SLoadBalancerHTTPListener) Start() error { + return errors.Wrap(cloudprovider.ErrNotImplemented, "Start") +} + +func (self *SLoadBalancerHTTPListener) Stop() error { + return errors.Wrap(cloudprovider.ErrNotImplemented, "Stop") +} + +func (self *SLoadBalancerHTTPListener) ChangeScheduler(ctx context.Context, opts *cloudprovider.ChangeListenerSchedulerOptions) error { + return cloudprovider.ErrNotImplemented +} + +func (self *SLoadBalancerHTTPListener) SetHealthCheck(ctx context.Context, opts *cloudprovider.ListenerHealthCheckOptions) error { + return cloudprovider.ErrNotImplemented +} + +func (self *SLoadBalancerHTTPListener) Delete(ctx context.Context) error { + return errors.Wrap(cloudprovider.ErrNotImplemented, "Delete") +} + +func (self *SLoadBalancerHTTPListener) GetRedirect() string { + return api.LB_REDIRECT_OFF +} + +func (self *SLoadBalancerHTTPListener) GetRedirectCode() int64 { + return 0 +} + +func (self *SLoadBalancerHTTPListener) GetRedirectScheme() string { + return "" +} + +func (self *SLoadBalancerHTTPListener) GetRedirectHost() string { + return "" +} + +func (self *SLoadBalancerHTTPListener) GetRedirectPath() string { + return "" +} + +func (self *SLoadBalancerHTTPListener) GetClientIdleTimeout() int { + return 0 +} + +func (self *SLoadBalancerHTTPListener) GetBackendConnectTimeout() int { + return 0 +} diff --git a/vendor/yunion.io/x/cloudmux/pkg/multicloud/azure/loadbalancer_props.go b/vendor/yunion.io/x/cloudmux/pkg/multicloud/azure/loadbalancer_props.go index 09ff2ee573..d6347dbfa7 100644 --- a/vendor/yunion.io/x/cloudmux/pkg/multicloud/azure/loadbalancer_props.go +++ b/vendor/yunion.io/x/cloudmux/pkg/multicloud/azure/loadbalancer_props.go @@ -20,15 +20,16 @@ type SLoadbalancerProperties struct { OperationalState string `json:"operationalState"` ResourceGUID string `json:"resourceGuid"` GatewayIPConfigurations []GatewayIPConfiguration `json:"gatewayIPConfigurations"` - SSLCertificates []SSLCertificate `json:"sslCertificates"` + SSLCertificates []SLoadbalancerCert `json:"sslCertificates"` FrontendIPConfigurations []FrontendIPConfiguration `json:"frontendIPConfigurations"` FrontendPorts []FrontendPort `json:"frontendPorts"` BackendAddressPools []BackendAddressPool `json:"backendAddressPools"` BackendHTTPSettingsCollection []BackendHTTPSettingsCollection `json:"backendHttpSettingsCollection"` - LoadBalancingRules []LoadBalancingRule `json:"loadBalancingRules"` + LoadBalancingRules []SLoadBalancerListener `json:"loadBalancingRules"` Probes []Probe `json:"probes"` - InboundNatRules []InboundNatRule `json:"inboundNatRules"` - HTTPListeners []HTTPListener `json:"httpListeners"` + InboundNatRules []SLoadBalancerListener `json:"inboundNatRules"` + OutboundRules []SLoadBalancerListener `json:"outboundRules"` + HTTPListeners []SLoadBalancerHTTPListener `json:"httpListeners"` URLPathMaps []URLPathMap `json:"urlPathMaps"` RequestRoutingRules []RequestRoutingRule `json:"requestRoutingRules"` RedirectConfigurations []RedirectConfiguration `json:"redirectConfigurations"` @@ -44,20 +45,20 @@ type AutoscaleConfiguration struct { type BackendAddressPool struct { Name string `json:"name"` - ID string `json:"id"` + Id string `json:"id"` Etag string `json:"etag"` Properties BackendAddressPoolProperties `json:"properties"` Type string `json:"type"` } type BackendAddressPoolProperties struct { - ProvisioningState string `json:"provisioningState"` - LoadBalancingRules []Subnet `json:"loadBalancingRules"` - BackendIPConfigurations []BackendIPConfiguration `json:"backendIPConfigurations"` - BackendAddresses []BackendAddress `json:"backendAddresses"` - RequestRoutingRules []BackendIPConfiguration `json:"requestRoutingRules"` - URLPathMaps []BackendIPConfiguration `json:"urlPathMaps"` - PathRules []BackendIPConfiguration `json:"pathRules"` + ProvisioningState string `json:"provisioningState"` + BackendIPConfigurations []struct { + Id string + } + BackendAddresses []BackendAddress + URLPathMaps []BackendIPConfiguration `json:"urlPathMaps"` + PathRules []BackendIPConfiguration `json:"pathRules"` } type BackendIPConfiguration struct { @@ -129,7 +130,7 @@ type PathRuleProperties struct { } type BackendAddress struct { - IPAddress string `json:"ipAddress"` + IPAddress string } type BackendHTTPSettingsCollection struct { @@ -191,19 +192,6 @@ type RequestRoutingRuleProperties struct { BackendHTTPSettings PublicIPAddress `json:"backendHttpSettings"` } -type SSLCertificate struct { - Name string `json:"name"` - ID string `json:"id"` - Properties SSLCertificateProperties `json:"properties"` -} - -type SSLCertificateProperties struct { - ProvisioningState string `json:"provisioningState"` - PublicCertData string `json:"public_cert_data"` - Data string `json:"data"` - Password string `json:"password"` -} - type Sku struct { Name string `json:"name"` Tier string `json:"tier"` @@ -224,33 +212,14 @@ type FrontendIPConfiguration struct { } type FrontendIPConfigurationProperties struct { - ProvisioningState string `json:"provisioningState"` - PrivateIPAddress string `json:"privateIPAddress"` - PublicIPAddress *PublicIPAddress `json:"publicIPAddress,omitempty"` - PrivateIPAllocationMethod string `json:"privateIPAllocationMethod"` - PrivateIPAddressVersion string `json:"privateIPAddressVersion"` - Subnet Subnet `json:"subnet"` - LoadBalancingRules []Subnet `json:"loadBalancingRules"` - InboundNatRules []Subnet `json:"inboundNatRules"` -} - -type InboundNatRule struct { - Name string `json:"name"` - ID string `json:"id"` - Etag string `json:"etag"` - Type string `json:"type"` - Properties InboundNatRuleProperties `json:"properties"` -} - -type InboundNatRuleProperties struct { - ProvisioningState string `json:"provisioningState"` - FrontendIPConfiguration Subnet `json:"frontendIPConfiguration"` - FrontendPort int64 `json:"frontendPort"` - BackendPort int64 `json:"backendPort"` - EnableFloatingIP bool `json:"enableFloatingIP"` - IdleTimeoutInMinutes int64 `json:"idleTimeoutInMinutes"` - Protocol string `json:"protocol"` - EnableTCPReset bool `json:"enableTcpReset"` + ProvisioningState string `json:"provisioningState"` + PrivateIPAddress string `json:"privateIPAddress"` + PublicIPAddress PublicIPAddress `json:"publicIPAddress,omitempty"` + PrivateIPAllocationMethod string `json:"privateIPAllocationMethod"` + PrivateIPAddressVersion string `json:"privateIPAddressVersion"` + Subnet Subnet `json:"subnet"` + LoadBalancingRules []Subnet `json:"loadBalancingRules"` + InboundNatRules []Subnet `json:"inboundNatRules"` } type SLoadbalancerSku struct { @@ -261,7 +230,7 @@ type Probe struct { lb *SLoadbalancer Name string `json:"name"` - ID string `json:"id"` + Id string `json:"id"` Etag string `json:"etag"` Type string `json:"type"` Properties ProbeProperties `json:"properties"` @@ -292,14 +261,6 @@ type Match struct { StatusCodes []string `json:"statusCodes"` } -type LoadBalancingRule struct { - Name string `json:"name"` - ID string `json:"id"` - Etag string `json:"etag"` - Type string `json:"type"` - Properties LoadBalancingRuleProperties `json:"properties"` -} - type LoadBalancingRuleProperties struct { ProvisioningState string `json:"provisioningState"` FrontendIPConfiguration Subnet `json:"frontendIPConfiguration"` @@ -314,22 +275,6 @@ type LoadBalancingRuleProperties struct { Probe Subnet `json:"probe"` } -type HTTPListener struct { - Name string `json:"name"` - ID string `json:"id"` - Properties HTTPListenerProperties `json:"properties"` -} - -type HTTPListenerProperties struct { - FrontendIPConfiguration PublicIPAddress `json:"frontendIPConfiguration"` - FrontendPort PublicIPAddress `json:"frontendPort"` - HostName string `json:"hostName"` - Protocol string `json:"protocol"` - SSLCertificate PublicIPAddress `json:"sslCertificate"` - RequestRoutingRules []Subnet `json:"requestRoutingRules"` - RequireServerNameIndication bool `json:"requireServerNameIndication"` -} - type ContentProperties struct { ProvisioningState string `json:"provisioningState"` ResourceGUID string `json:"resourceGuid"` diff --git a/vendor/yunion.io/x/cloudmux/pkg/multicloud/azure/loadbalancerbackend.go b/vendor/yunion.io/x/cloudmux/pkg/multicloud/azure/loadbalancerbackend.go index 60fd15dfc7..ce586590f3 100644 --- a/vendor/yunion.io/x/cloudmux/pkg/multicloud/azure/loadbalancerbackend.go +++ b/vendor/yunion.io/x/cloudmux/pkg/multicloud/azure/loadbalancerbackend.go @@ -16,10 +16,9 @@ package azure import ( "context" - "strconv" "strings" - "github.com/pkg/errors" + "yunion.io/x/pkg/errors" api "yunion.io/x/cloudmux/pkg/apis/compute" "yunion.io/x/cloudmux/pkg/cloudprovider" @@ -28,22 +27,19 @@ import ( type SLoadbalancerBackend struct { multicloud.SResourceBase + AzureTags - lbbg *SLoadbalancerBackendGroup - // networkInterfaces 通过接口地址反查虚拟机地址 - Name string `json:"name"` - ID string `json:"id"` - Type string `json:"type"` - BackendPort int - BackendIP string + lbbg *SLoadbalancerBackendGroup + PrivateIPAddress string + LoadBalancerBackendAddresses []string } func (self *SLoadbalancerBackend) GetId() string { - return self.ID + "::" + strconv.Itoa(self.BackendPort) + return self.PrivateIPAddress + strings.Join(self.LoadBalancerBackendAddresses, ",") } func (self *SLoadbalancerBackend) GetName() string { - return self.Name + return self.PrivateIPAddress + strings.Join(self.LoadBalancerBackendAddresses, ",") } func (self *SLoadbalancerBackend) GetGlobalId() string { @@ -54,20 +50,8 @@ func (self *SLoadbalancerBackend) GetStatus() string { return api.LB_STATUS_ENABLED } -func (self *SLoadbalancerBackend) GetSysTags() map[string]string { - return nil -} - -func (self *SLoadbalancerBackend) GetTags() (map[string]string, error) { - return nil, nil -} - -func (self *SLoadbalancerBackend) SetTags(tags map[string]string, replace bool) error { - return errors.Wrap(cloudprovider.ErrNotImplemented, "SetTags") -} - func (self *SLoadbalancerBackend) GetProjectId() string { - return getResourceGroup(self.ID) + return "" } func (self *SLoadbalancerBackend) GetWeight() int { @@ -75,11 +59,11 @@ func (self *SLoadbalancerBackend) GetWeight() int { } func (self *SLoadbalancerBackend) GetPort() int { - return self.BackendPort + return 0 } func (self *SLoadbalancerBackend) GetBackendType() string { - return self.Type + return api.LB_BACKEND_IP } func (self *SLoadbalancerBackend) GetBackendRole() string { @@ -87,11 +71,11 @@ func (self *SLoadbalancerBackend) GetBackendRole() string { } func (self *SLoadbalancerBackend) GetBackendId() string { - return self.ID + return "" } func (self *SLoadbalancerBackend) GetIpAddress() string { - return self.BackendIP + return self.PrivateIPAddress + strings.Join(self.LoadBalancerBackendAddresses, ",") } func (self *SLoadbalancerBackend) SyncConf(ctx context.Context, port, weight int) error { diff --git a/vendor/yunion.io/x/cloudmux/pkg/multicloud/azure/loadbalancerbackendgroup.go b/vendor/yunion.io/x/cloudmux/pkg/multicloud/azure/loadbalancerbackendgroup.go index 14204ca17c..9d5872e7dc 100644 --- a/vendor/yunion.io/x/cloudmux/pkg/multicloud/azure/loadbalancerbackendgroup.go +++ b/vendor/yunion.io/x/cloudmux/pkg/multicloud/azure/loadbalancerbackendgroup.go @@ -16,45 +16,38 @@ package azure import ( "context" - "fmt" - "strconv" "strings" - "github.com/pkg/errors" - - "yunion.io/x/jsonutils" - api "yunion.io/x/cloudmux/pkg/apis/compute" "yunion.io/x/cloudmux/pkg/cloudprovider" "yunion.io/x/cloudmux/pkg/multicloud" + "yunion.io/x/jsonutils" + "yunion.io/x/pkg/errors" ) -// todo: 虚拟机规模集不支持 -// 注: 因为与onecloud后端服务器组存在配置差异,不支持同步未关联的后端服务器组 -// 应用型LB: HTTP 设置 + 后端池 = onecloud 后端服务器组 -// 4层LB: loadBalancingRules(backendPort)+ 后端池 = onecloud 后端服务器组 type SLoadbalancerBackendGroup struct { multicloud.SResourceBase - lb *SLoadbalancer - lbbs []cloudprovider.ICloudLoadbalancerBackend + AzureTags + lb *SLoadbalancer - Pool BackendAddressPool - DefaultPort int - HttpSettings *BackendHTTPSettingsCollection + Id string - BackendIps []BackendIPConfiguration + Properties *SLoadbalancerBackend + BackendIPConfigurations []struct { + Id string + } } func (self *SLoadbalancerBackendGroup) GetId() string { - return self.Pool.ID + "::" + strconv.Itoa(self.DefaultPort) + return self.Id } func (self *SLoadbalancerBackendGroup) GetName() string { - if self.HttpSettings != nil { - return self.Pool.Name + "::" + self.HttpSettings.Name + info := strings.Split(self.Id, "/") + if len(info) > 0 { + return info[len(info)-1] } - - return self.Pool.Name + "::" + strconv.Itoa(self.DefaultPort) + return "" } func (self *SLoadbalancerBackendGroup) GetGlobalId() string { @@ -62,12 +55,7 @@ func (self *SLoadbalancerBackendGroup) GetGlobalId() string { } func (self *SLoadbalancerBackendGroup) GetStatus() string { - switch self.Pool.Properties.ProvisioningState { - case "Succeeded": - return api.LB_STATUS_ENABLED - default: - return api.LB_STATUS_UNKNOWN - } + return api.LB_STATUS_ENABLED } func (self *SLoadbalancerBackendGroup) Refresh() error { @@ -76,29 +64,7 @@ func (self *SLoadbalancerBackendGroup) Refresh() error { return errors.Wrap(err, "GetILoadBalancerBackendGroupById") } - err = jsonutils.Update(self, lbbg) - if err != nil { - return errors.Wrap(err, "refresh.Update") - } - - self.lbbs = nil - return nil -} - -func (self *SLoadbalancerBackendGroup) IsEmulated() bool { - return true -} - -func (self *SLoadbalancerBackendGroup) GetSysTags() map[string]string { - return nil -} - -func (self *SLoadbalancerBackendGroup) GetTags() (map[string]string, error) { - return map[string]string{}, nil -} - -func (self *SLoadbalancerBackendGroup) SetTags(tags map[string]string, replace bool) error { - return errors.Wrap(cloudprovider.ErrNotImplemented, "SetTags") + return jsonutils.Update(self, lbbg) } func (self *SLoadbalancerBackendGroup) GetProjectId() string { @@ -118,58 +84,40 @@ func (self *SLoadbalancerBackendGroup) GetLoadbalancerId() string { } func (self *SLoadbalancerBackendGroup) GetILoadbalancerBackends() ([]cloudprovider.ICloudLoadbalancerBackend, error) { - if self.lbbs != nil { - return self.lbbs, nil + ret := []cloudprovider.ICloudLoadbalancerBackend{} + if len(self.BackendIPConfigurations) > 0 { + for _, ipConf := range self.BackendIPConfigurations { + apiVerion := "2024-03-01" + if strings.Contains(strings.ToLower(ipConf.Id), "microsoft.network/networkinterfaces") { + apiVerion = "2023-11-01" + } + resp, err := self.lb.region.show(ipConf.Id, apiVerion) + if err != nil { + return nil, err + } + backend := &SLoadbalancerBackend{ + lbbg: self, + } + err = resp.Unmarshal(backend, "properties") + if err != nil { + return nil, errors.Wrapf(err, "Unmarshal") + } + ret = append(ret, backend) + } + return ret, nil } - - var ret []cloudprovider.ICloudLoadbalancerBackend - ips := self.Pool.Properties.BackendIPConfigurations - for i := range ips { - ip := ips[i] - nic, err := self.lb.region.GetNetworkInterface(strings.Split(ip.ID, "/ipConfigurations")[0]) - if err != nil { - return nil, errors.Wrap(err, "GetNetworkInterface") - } - - if len(nic.Properties.VirtualMachine.ID) == 0 { - continue - } - - name := nic.Properties.VirtualMachine.Name - vid := nic.Properties.VirtualMachine.ID - if len(name) == 0 && len(vid) > 0 { - segs := strings.Split(vid, "/virtualMachines/") - name = segs[len(segs)-1] - } - bg := SLoadbalancerBackend{ - SResourceBase: multicloud.SResourceBase{}, - lbbg: self, - Name: name, - ID: vid, - Type: api.LB_BACKEND_GUEST, - BackendPort: self.DefaultPort, - } - - ret = append(ret, &bg) + resp, err := self.lb.region.show(self.Id, "2021-02-01") + if err != nil { + return nil, err } - - ips2 := self.Pool.Properties.BackendAddresses - for i := range ips2 { - name := fmt.Sprintf("ip-%s", ips2[i].IPAddress) - bg := SLoadbalancerBackend{ - SResourceBase: multicloud.SResourceBase{}, - lbbg: self, - Name: name, - ID: fmt.Sprintf("%s-%s", self.GetId(), name), - Type: api.LB_BACKEND_IP, - BackendIP: ips2[i].IPAddress, - BackendPort: self.DefaultPort, - } - - ret = append(ret, &bg) + err = resp.Unmarshal(self) + if err != nil { + return nil, err + } + if self.Properties != nil && (len(self.Properties.PrivateIPAddress) > 0 || len(self.Properties.LoadBalancerBackendAddresses) > 0) { + self.Properties.lbbg = self + ret = append(ret, self.Properties) } - - self.lbbs = ret return ret, nil } @@ -185,7 +133,7 @@ func (self *SLoadbalancerBackendGroup) GetILoadbalancerBackendById(backendId str } } - return nil, errors.Wrap(cloudprovider.ErrNotFound, "GetILoadbalancerBackendById") + return nil, errors.Wrapf(cloudprovider.ErrNotFound, backendId) } func (self *SLoadbalancerBackendGroup) GetProtocolType() string { diff --git a/vendor/yunion.io/x/cloudmux/pkg/multicloud/azure/loadbalancercert.go b/vendor/yunion.io/x/cloudmux/pkg/multicloud/azure/loadbalancercert.go index 2e1a72bc48..91fdfafe9c 100644 --- a/vendor/yunion.io/x/cloudmux/pkg/multicloud/azure/loadbalancercert.go +++ b/vendor/yunion.io/x/cloudmux/pkg/multicloud/azure/loadbalancercert.go @@ -20,29 +20,32 @@ import ( "crypto/x509" "encoding/pem" "fmt" + "net/url" "strings" "time" - "github.com/pkg/errors" - - "yunion.io/x/jsonutils" - "yunion.io/x/cloudmux/pkg/apis" "yunion.io/x/cloudmux/pkg/cloudprovider" "yunion.io/x/cloudmux/pkg/multicloud" + "yunion.io/x/pkg/errors" ) type SLoadbalancerCert struct { multicloud.SResourceBase - lb *SLoadbalancer - cert *x509.Certificate - Name string `json:"name"` - ID string `json:"id"` - PublicKey string `json:"public_key"` + AzureTags + region *SRegion + Name string `json:"name"` + Id string `json:"id"` + Properties struct { + PublicCertData string + HttpListeners []struct { + Id string + } + } } func (self *SLoadbalancerCert) GetId() string { - return self.ID + return self.Id } func (self *SLoadbalancerCert) GetName() string { @@ -57,32 +60,6 @@ func (self *SLoadbalancerCert) GetStatus() string { return apis.STATUS_AVAILABLE } -func (self *SLoadbalancerCert) Refresh() error { - cert, err := self.lb.GetILoadBalancerCertificateById(self.GetId()) - if err != nil { - return errors.Wrap(err, "GetILoadBalancerCertificateById") - } - - err = jsonutils.Update(self, cert) - if err != nil { - return errors.Wrap(err, "Update") - } - - return nil -} - -func (self *SLoadbalancerCert) GetSysTags() map[string]string { - return nil -} - -func (self *SLoadbalancerCert) GetTags() (map[string]string, error) { - return nil, nil -} - -func (self *SLoadbalancerCert) SetTags(tags map[string]string, replace bool) error { - return errors.Wrap(cloudprovider.ErrNotImplemented, "SetTags") -} - func (self *SLoadbalancerCert) GetProjectId() string { return getResourceGroup(self.GetId()) } @@ -92,22 +69,11 @@ func (self *SLoadbalancerCert) Delete() error { } func (self *SLoadbalancerCert) ParsePublicKey() (*x509.Certificate, error) { - if self.cert != nil { - return self.cert, nil - } - - publicKey := self.GetPublickKey() - if len(publicKey) == 0 { - return nil, fmt.Errorf("SElbCertificate ParsePublicKey public key is empty") - } - - block, _ := pem.Decode([]byte(publicKey)) + block, _ := pem.Decode([]byte(self.GetPublickKey())) cert, err := x509.ParseCertificate(block.Bytes) if err != nil { return nil, errors.Wrap(err, "ParseCertificate") } - - self.cert = cert return cert, nil } @@ -116,7 +82,6 @@ func (self *SLoadbalancerCert) GetCommonName() string { if err != nil { return "" } - return cert.Issuer.CommonName } @@ -125,7 +90,6 @@ func (self *SLoadbalancerCert) GetSubjectAlternativeNames() string { if err != nil { return "" } - return "" } @@ -150,10 +114,10 @@ func (self *SLoadbalancerCert) GetExpireTime() time.Time { } func (self *SLoadbalancerCert) GetPublickKey() string { - if len(self.PublicKey) > 0 { + if len(self.Properties.PublicCertData) > 0 { var pk bytes.Buffer pk.WriteString("-----BEGIN CERTIFICATE-----\r\n") - content := bytes.NewBufferString(self.PublicKey) + content := bytes.NewBufferString(self.Properties.PublicCertData) for { l := content.Next(64) if len(l) == 64 { @@ -166,10 +130,30 @@ func (self *SLoadbalancerCert) GetPublickKey() string { pk.WriteString("-----END CERTIFICATE-----") return pk.String() } - return "" } func (self *SLoadbalancerCert) GetPrivateKey() string { return "" } + +func (self *SRegion) GetLoadbalancerCertificates() ([]SLoadbalancerCert, error) { + params := url.Values{} + resp, err := self.client.list_v2("Microsoft.Network/applicationGateways", "2023-09-01", params) + if err != nil { + return nil, err + } + ret := []SLoadbalancer{} + err = resp.Unmarshal(&ret, "value") + if err != nil { + return nil, err + } + result := []SLoadbalancerCert{} + for i := range ret { + if ret[i].Location != self.Name { + continue + } + result = append(result, ret[i].Properties.SSLCertificates...) + } + return result, nil +} diff --git a/vendor/yunion.io/x/cloudmux/pkg/multicloud/azure/loadbalancerlistener.go b/vendor/yunion.io/x/cloudmux/pkg/multicloud/azure/loadbalancerlistener.go index 064d518feb..89393b5db7 100644 --- a/vendor/yunion.io/x/cloudmux/pkg/multicloud/azure/loadbalancerlistener.go +++ b/vendor/yunion.io/x/cloudmux/pkg/multicloud/azure/loadbalancerlistener.go @@ -16,106 +16,55 @@ package azure import ( "context" - "net/url" - "regexp" - "strconv" "strings" - "yunion.io/x/jsonutils" - "yunion.io/x/log" - "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" + "yunion.io/x/jsonutils" + "yunion.io/x/pkg/errors" ) -// todo: 目前不支持 入站 NAT 规则 -// todo: HTTP 设置(端口+协议,其余信息丢弃) + 后端池 = onecloud 路径的路由 的 onecloud 后端服务器组 -/* -应用型LB: urlPathMaps(defaultBackendAddressPool+defaultBackendHttpSettings+requestRoutingRules+httpListeners)= Onecloud监听器 -4层LB: loadBalancingRules(前端) = Onecloud监听器 -*/ +type ListenerProperties struct { + ProvisioningState string `json:"provisioningState"` + FrontendIPConfiguration Subnet `json:"frontendIPConfiguration"` + FrontendPort int `json:"frontendPort"` + BackendPort int `json:"backendPort"` + AllocatedOutboundPorts int `json:"allocatedOutboundPorts"` + EnableFloatingIP bool `json:"enableFloatingIP"` + IdleTimeoutInMinutes int64 `json:"idleTimeoutInMinutes"` + Protocol string `json:"protocol"` + EnableTCPReset bool `json:"enableTcpReset"` + LoadDistribution string + BackendIPConfiguration struct { + Id string `json:"id"` + } `json:"backendIPConfiguration"` + BackendAddressPool struct { + Id string `json:"id"` + } `json:"backendAddressPool"` + BackendAddressPools []struct { + Id string `json:"id"` + } `json:"backendAddressPools"` + Probe struct { + Id string `json:"id"` + } +} + type SLoadBalancerListener struct { multicloud.SResourceBase AzureTags multicloud.SLoadbalancerRedirectBase + lb *SLoadbalancer - lb *SLoadbalancer - lbrs []cloudprovider.ICloudLoadbalancerListenerRule - - /* - IPVersion string - FrontendIP string // 前端IP - FrontendIPId string // 前端IP ID - */ - fp *FrontendIPConfiguration - - /* - Protocol string // 监听协议 - HostName string // 监听器 监听域名 - FrontendPort int // 前端端口 - */ - listener *HTTPListener - - /* - Protocol string // 后端协议 - BackendPort int // 后端端口 - IdleTimeoutInMinutes int // 空闲超时(分钟) - LoadDistribution string // 会话保持方法SourceIP|SourceIPProtocol - CookieBasedAffinity string // cookies 关联 - CookieName string // cookie 名称 - EnabledConnectionDraining bool // 排出超时这应用于通过 API 调用从后端池明确删除的后端实例,以及运行状况探测报告的运行不正常的实例。 - ConnectionDrainingSec int // 排出超时时长(秒) - RequestTimeout int // 请求超时时间(秒) - probe // 健康检查 - */ - backendSetting *BackendHTTPSettingsCollection - - // - backendGroup *BackendAddressPool - - /* - redirectType string // 重定向类型 - targetListener // 重定向到监听 - */ - redirect *RedirectConfiguration - - /* - "protocol": "Http", - "host": "baidu.com", - "path": "/test", - "interval": 30, - "timeout": 30, - "unhealthyThreshold": 3, - "pickHostNameFromBackendHttpSettings": false, - "minServers": 0, - "match": { - "body": "500", - "statusCodes": [ - "200-399" - ] - }, - */ - healthcheck *Probe - - Name string - ID string - ProvisioningState string - IPVersion string - Protocol string // 监听协议 - LoadDistribution string // 调度算法 - FrontendPort int // 前端端口 - BackendPort int // 后端端口 - ClientIdleTimeout int // 客户端连接超时 - EnableFloatingIP bool // 浮动 IP - EnableTcpReset bool - - rules []PathRule + Name string `json:"name"` + Id string `json:"id"` + Etag string `json:"etag"` + Type string `json:"type"` + Properties ListenerProperties `json:"properties"` } func (self *SLoadBalancerListener) GetId() string { - return self.ID + return self.Id } func (self *SLoadBalancerListener) GetName() string { @@ -127,7 +76,7 @@ func (self *SLoadBalancerListener) GetGlobalId() string { } func (self *SLoadBalancerListener) GetStatus() string { - switch self.ProvisioningState { + switch self.Properties.ProvisioningState { case "Succeeded", "Updating", "Deleting": return api.LB_STATUS_ENABLED case "Failed": @@ -138,18 +87,12 @@ func (self *SLoadBalancerListener) GetStatus() string { } func (self *SLoadBalancerListener) Refresh() error { - lbl, err := self.lb.GetILoadBalancerListenerById(self.GetId()) + lblis, err := self.lb.GetILoadBalancerListenerById(self.GetId()) if err != nil { return errors.Wrap(err, "GetILoadBalancerListenerById") } - err = jsonutils.Update(self, lbl) - if err != nil { - return errors.Wrap(err, "refresh.Update") - } - - self.lbrs = nil - return nil + return jsonutils.Update(self, lblis) } func (listerner *SLoadBalancerListener) ChangeCertificate(ctx context.Context, opts *cloudprovider.ListenerCertificateOptions) error { @@ -160,33 +103,12 @@ func (listerner *SLoadBalancerListener) SetAcl(ctx context.Context, opts *cloudp return cloudprovider.ErrNotImplemented } -func (self *SLoadBalancerListener) GetTags() (map[string]string, error) { - if self.fp != nil { - if self.fp.Properties.PublicIPAddress != nil && len(self.fp.Properties.PublicIPAddress.ID) > 0 { - eip, _ := self.lb.GetIEIPById(self.fp.Properties.PublicIPAddress.ID) - if eip != nil { - return map[string]string{"FrontendIP": eip.GetIpAddr()}, nil - } - } - - if len(self.fp.Properties.PrivateIPAddress) > 0 { - return map[string]string{"FrontendIP": self.fp.Properties.PrivateIPAddress}, nil - } - } - - return map[string]string{}, nil -} - -func (self *SLoadBalancerListener) SetTags(tags map[string]string, replace bool) error { - return errors.Wrap(cloudprovider.ErrNotImplemented, "SetTags") -} - func (self *SLoadBalancerListener) GetProjectId() string { return getResourceGroup(self.GetId()) } func (self *SLoadBalancerListener) GetListenerType() string { - switch strings.ToLower(self.Protocol) { + switch strings.ToLower(self.Properties.Protocol) { case "tcp": return api.LB_LISTENER_TYPE_TCP case "udp": @@ -196,21 +118,24 @@ func (self *SLoadBalancerListener) GetListenerType() string { case "https": return api.LB_LISTENER_TYPE_HTTPS default: - return "" + return strings.ToLower(self.Properties.Protocol) } } func (self *SLoadBalancerListener) GetListenerPort() int { - return int(self.FrontendPort) + return self.Properties.FrontendPort } func (self *SLoadBalancerListener) GetScheduler() string { - switch self.LoadDistribution { - case "SourceIPProtocol", "SourceIP": + switch self.Properties.LoadDistribution { + case "Default": + return api.LB_SCHEDULER_MH + case "SourceIP": return api.LB_SCHEDULER_SCH - default: - return "" + case "SourceIPProtocol": + return api.LB_SCHEDULER_TCH } + return "" } func (self *SLoadBalancerListener) GetAclStatus() string { @@ -230,56 +155,32 @@ func (self *SLoadBalancerListener) GetEgressMbps() int { } func (self *SLoadBalancerListener) GetHealthCheck() string { - // if self.probe != nil - if self.healthcheck != nil { + if len(self.Properties.Probe.Id) > 0 { return api.LB_BOOL_ON } - return api.LB_BOOL_OFF } func (self *SLoadBalancerListener) GetHealthCheckType() string { - if self.healthcheck == nil { - return "" - } - switch strings.ToLower(self.healthcheck.Properties.Protocol) { - case "tcp": - return api.LB_HEALTH_CHECK_TCP - case "udp": - return api.LB_HEALTH_CHECK_UDP - case "http": - return api.LB_HEALTH_CHECK_HTTP - case "https": - return api.LB_HEALTH_CHECK_HTTPS - default: - return "" + for _, prob := range self.lb.Properties.Probes { + if strings.EqualFold(prob.Id, self.Properties.Probe.Id) { + return strings.ToLower(prob.Properties.Protocol) + } } + return "" } func (self *SLoadBalancerListener) GetHealthCheckTimeout() int { - if self.healthcheck == nil { - return 0 - } - switch self.GetHealthCheckType() { - case api.LB_HEALTH_CHECK_HTTP, api.LB_HEALTH_CHECK_HTTPS: - return self.healthcheck.Properties.Timeout - } - - return self.healthcheck.Properties.IntervalInSeconds + return 0 } func (self *SLoadBalancerListener) GetHealthCheckInterval() int { - if self.healthcheck == nil { - return 0 - } - switch self.GetHealthCheckType() { - case api.LB_HEALTH_CHECK_HTTP, api.LB_HEALTH_CHECK_HTTPS: - if self.healthcheck.Properties.Interval > 0 { - return self.healthcheck.Properties.Interval + for _, prob := range self.lb.Properties.Probes { + if strings.EqualFold(prob.Id, self.Properties.Probe.Id) { + return prob.Properties.IntervalInSeconds } } - - return self.healthcheck.Properties.IntervalInSeconds + return 0 } func (self *SLoadBalancerListener) GetHealthCheckRise() int { @@ -287,17 +188,12 @@ func (self *SLoadBalancerListener) GetHealthCheckRise() int { } func (self *SLoadBalancerListener) GetHealthCheckFail() int { - if self.healthcheck == nil { - return 0 - } - switch self.GetHealthCheckType() { - case api.LB_HEALTH_CHECK_HTTP, api.LB_HEALTH_CHECK_HTTPS: - if self.healthcheck.Properties.UnhealthyThreshold > 0 { - return self.healthcheck.Properties.UnhealthyThreshold + for _, prob := range self.lb.Properties.Probes { + if strings.EqualFold(prob.Id, self.Properties.Probe.Id) { + return prob.Properties.NumberOfProbes } } - - return self.healthcheck.Properties.NumberOfProbes + return 0 } func (self *SLoadBalancerListener) GetHealthCheckReq() string { @@ -309,45 +205,29 @@ func (self *SLoadBalancerListener) GetHealthCheckExp() string { } func (self *SLoadBalancerListener) GetBackendGroupId() string { - if self.backendGroup != nil { - return self.backendGroup.ID + "::" + strconv.Itoa(self.BackendPort) + for _, id := range []string{ + self.Properties.BackendIPConfiguration.Id, + self.Properties.BackendAddressPool.Id, + } { + if len(id) > 0 { + return strings.ToLower(id) + } } - return "" } func (self *SLoadBalancerListener) GetBackendServerPort() int { - return self.BackendPort + return self.Properties.BackendPort } func (self *SLoadBalancerListener) GetHealthCheckDomain() string { - if self.healthcheck == nil { - return "" - } - switch self.GetHealthCheckType() { - case api.LB_HEALTH_CHECK_HTTP, api.LB_HEALTH_CHECK_HTTPS: - return self.healthcheck.Properties.Host - } - return "" } func (self *SLoadBalancerListener) GetHealthCheckURI() string { - if self.healthcheck == nil { - return "" - } - switch self.GetHealthCheckType() { - case api.LB_HEALTH_CHECK_HTTP, api.LB_HEALTH_CHECK_HTTPS: - return self.healthcheck.Properties.Path - } - return "" } -/* -todo: 和onecloud code不兼容? -与此处输入的 HTTP 状态代码或代码范围相匹配的响应将被视为成功。请输入逗号分隔的代码列表(例如 200, 201)或者输入一个代码范围(例如 220-226) -*/ func (self *SLoadBalancerListener) GetHealthCheckCode() string { return "" } @@ -368,77 +248,15 @@ func (self *SLoadBalancerListener) GetILoadBalancerListenerRuleById(ruleId strin } } - return nil, errors.Wrap(err, "GetILoadBalancerListenerRuleById") + return nil, errors.Wrap(cloudprovider.ErrNotFound, ruleId) } func (self *SLoadBalancerListener) GetILoadbalancerListenerRules() ([]cloudprovider.ICloudLoadbalancerListenerRule, error) { - if self.lbrs != nil { - return self.lbrs, nil - } - - irules := []cloudprovider.ICloudLoadbalancerListenerRule{} - for i := range self.rules { - r := self.rules[i] - var redirect *RedirectConfiguration - var lbbg *SLoadbalancerBackendGroup - if r.Properties.RedirectConfiguration != nil { - redirect = self.lb.getRedirectConfiguration(r.Properties.RedirectConfiguration.ID) - } else { - if r.Properties.BackendAddressPool == nil || r.Properties.BackendHTTPSettings == nil { - continue - } - - pool := self.lb.getBackendAddressPool(r.Properties.BackendAddressPool.ID) - if pool == nil { - log.Debugf("getBackendAddressPool %s not found", r.Properties.BackendAddressPool.ID) - continue - } - backsetting := self.lb.getBackendHTTPSettingsCollection(r.Properties.BackendHTTPSettings.ID) - if backsetting == nil { - log.Debugf("getBackendHTTPSettingsCollection %s not found", r.Properties.BackendHTTPSettings.ID) - continue - } - - lbbg = &SLoadbalancerBackendGroup{ - lb: self.lb, - Pool: *pool, - DefaultPort: backsetting.Properties.Port, - HttpSettings: backsetting, - BackendIps: pool.Properties.BackendIPConfigurations, - } - } - - domain := "" - if self.listener != nil { - domain = self.listener.Properties.HostName - } - - rule := SLoadbalancerListenerRule{ - SResourceBase: multicloud.SResourceBase{}, - listener: self, - lbbg: lbbg, - redirect: redirect, - Name: r.Name, - ID: r.ID, - Domain: domain, - Properties: r.Properties, - } - irules = append(irules, &rule) - } - - return irules, nil + return []cloudprovider.ICloudLoadbalancerListenerRule{}, nil } func (self *SLoadBalancerListener) GetStickySession() string { - if self.backendSetting == nil { - return api.LB_BOOL_OFF - } - - if self.backendSetting.Properties.CookieBasedAffinity == "Enabled" { - return api.LB_BOOL_ON - } else { - return api.LB_BOOL_OFF - } + return api.LB_BOOL_OFF } func (self *SLoadBalancerListener) GetStickySessionType() string { @@ -449,24 +267,10 @@ func (self *SLoadBalancerListener) GetStickySessionType() string { } func (self *SLoadBalancerListener) GetStickySessionCookie() string { - if self.backendSetting == nil { - return "" - } - - return self.backendSetting.Properties.AffinityCookieName + return "" } func (self *SLoadBalancerListener) GetStickySessionCookieTimeout() int { - if self.backendSetting == nil { - return 0 - } - - if self.backendSetting.Properties.ConnectionDraining.Enabled { - _sec := strings.TrimSpace(self.backendSetting.Properties.ConnectionDraining.DrainTimeoutInSEC) - sec, _ := strconv.ParseInt(_sec, 10, 64) - return int(sec) - } - return 0 } @@ -479,10 +283,6 @@ func (self *SLoadBalancerListener) GzipEnabled() bool { } func (self *SLoadBalancerListener) GetCertificateId() string { - if self.listener != nil { - return self.listener.Properties.SSLCertificate.ID - } - return "" } @@ -515,91 +315,29 @@ func (self *SLoadBalancerListener) Delete(ctx context.Context) error { } func (self *SLoadBalancerListener) GetRedirect() string { - if self.redirect != nil { - return api.LB_REDIRECT_RAW - } - return api.LB_REDIRECT_OFF } func (self *SLoadBalancerListener) GetRedirectCode() int64 { - if self.redirect == nil { - return 0 - } - - switch self.redirect.Properties.RedirectType { - case "Permanent": - return api.LB_REDIRECT_CODE_301 - case "Found": - return api.LB_REDIRECT_CODE_302 - case "Temporary", "SeeOther": - return api.LB_REDIRECT_CODE_307 - default: - return 0 - } + return 0 } -func (self *SLoadBalancerListener) getRedirectUrl() *url.URL { - if self.redirect == nil { - return nil - } - - if len(self.redirect.Properties.TargetUrl) == 0 { - return nil - } - - _url := self.redirect.Properties.TargetUrl - if matched, _ := regexp.MatchString("^\\w{0,5}://", _url); !matched { - _url = "http://" + _url - } - - u, err := url.Parse(_url) - if err != nil { - log.Debugf("url Parse %s : %s", self.redirect.Properties.TargetUrl, err) - return nil - } - - return u -} func (self *SLoadBalancerListener) GetRedirectScheme() string { - u := self.getRedirectUrl() - if u == nil { - return "" - } - - return strings.ToLower(u.Scheme) + return "" } func (self *SLoadBalancerListener) GetRedirectHost() string { - u := self.getRedirectUrl() - if u == nil { - if self.redirect != nil && len(self.redirect.Properties.TargetListener.ID) > 0 { - segs := strings.Split(self.redirect.Properties.TargetListener.ID, "/") - return segs[len(segs)-1] - } - return "" - } - - return u.Host + return "" } func (self *SLoadBalancerListener) GetRedirectPath() string { - u := self.getRedirectUrl() - if u == nil { - return "" - } - - return u.Path + return "" } func (self *SLoadBalancerListener) GetClientIdleTimeout() int { - return self.ClientIdleTimeout + return int(self.Properties.IdleTimeoutInMinutes) * 60 } func (self *SLoadBalancerListener) GetBackendConnectTimeout() int { - if self.backendSetting == nil { - return 0 - } - - return self.backendSetting.Properties.RequestTimeout + return 0 } diff --git a/vendor/yunion.io/x/cloudmux/pkg/multicloud/azure/loadbalancerlistenerrule.go b/vendor/yunion.io/x/cloudmux/pkg/multicloud/azure/loadbalancerlistenerrule.go deleted file mode 100644 index 477e94360d..0000000000 --- a/vendor/yunion.io/x/cloudmux/pkg/multicloud/azure/loadbalancerlistenerrule.go +++ /dev/null @@ -1,192 +0,0 @@ -// 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 azure - -import ( - "context" - "net/url" - "regexp" - "strings" - - "yunion.io/x/log" - "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 SLoadbalancerListenerRule struct { - multicloud.SResourceBase - multicloud.SLoadbalancerRedirectBase - - listener *SLoadBalancerListener - lbbg *SLoadbalancerBackendGroup - redirect *RedirectConfiguration - // urlPathMaps -> pathRules - Name string `json:"name"` - ID string `json:"id"` - Domain string `json:"domain"` - Properties PathRuleProperties `json:"properties"` -} - -func (self *SLoadbalancerListenerRule) GetId() string { - return self.ID -} - -func (self *SLoadbalancerListenerRule) GetName() string { - return self.Name -} - -func (self *SLoadbalancerListenerRule) GetGlobalId() string { - return strings.ToLower(self.GetId()) -} - -func (self *SLoadbalancerListenerRule) GetStatus() string { - switch self.Properties.ProvisioningState { - case "Succeeded": - return api.LB_STATUS_ENABLED - case "Failed": - return api.LB_STATUS_DISABLED - default: - return api.LB_STATUS_UNKNOWN - } -} - -func (self *SLoadbalancerListenerRule) GetSysTags() map[string]string { - return nil -} - -func (self *SLoadbalancerListenerRule) GetTags() (map[string]string, error) { - return nil, nil -} - -func (self *SLoadbalancerListenerRule) SetTags(tags map[string]string, replace bool) error { - return errors.Wrap(cloudprovider.ErrNotImplemented, "SetTags") -} - -func (self *SLoadbalancerListenerRule) GetProjectId() string { - return getResourceGroup(self.GetId()) -} - -func (self *SLoadbalancerListenerRule) IsDefault() bool { - return false -} - -func (self *SLoadbalancerListenerRule) GetDomain() string { - return self.Domain -} - -func (self *SLoadbalancerListenerRule) GetPath() string { - if len(self.Properties.Paths) > 0 { - return self.Properties.Paths[0] - } - - return "" -} - -func (self *SLoadbalancerListenerRule) GetCondition() string { - return "" -} - -func (self *SLoadbalancerListenerRule) GetBackendGroupId() string { - if self.lbbg != nil { - return self.lbbg.GetId() - } - - return "" -} - -func (self *SLoadbalancerListenerRule) Delete(ctx context.Context) error { - return errors.Wrap(cloudprovider.ErrNotImplemented, "Delete") -} - -func (self *SLoadbalancerListenerRule) GetRedirect() string { - if self.redirect != nil { - return api.LB_REDIRECT_RAW - } - - return api.LB_REDIRECT_OFF -} - -func (self *SLoadbalancerListenerRule) GetRedirectCode() int64 { - if self.redirect == nil { - return 0 - } - - switch self.redirect.Properties.RedirectType { - case "Permanent": - return api.LB_REDIRECT_CODE_301 - case "Found": - return api.LB_REDIRECT_CODE_302 - case "Temporary", "SeeOther": - return api.LB_REDIRECT_CODE_307 - default: - return 0 - } -} - -func (self *SLoadbalancerListenerRule) getRedirectUrl() *url.URL { - if self.redirect == nil { - return nil - } - - if len(self.redirect.Properties.TargetUrl) == 0 { - return nil - } - - _url := self.redirect.Properties.TargetUrl - if matched, _ := regexp.MatchString("^\\w{0,5}://", _url); !matched { - _url = "http://" + _url - } - - u, err := url.Parse(_url) - if err != nil { - log.Debugf("url Parse %s : %s", self.redirect.Properties.TargetUrl, err) - return nil - } - - return u -} -func (self *SLoadbalancerListenerRule) GetRedirectScheme() string { - u := self.getRedirectUrl() - if u == nil { - return "" - } - - return strings.ToLower(u.Scheme) -} - -func (self *SLoadbalancerListenerRule) GetRedirectHost() string { - u := self.getRedirectUrl() - if u == nil { - if self.redirect != nil && len(self.redirect.Properties.TargetListener.ID) > 0 { - segs := strings.Split(self.redirect.Properties.TargetListener.ID, "/") - return segs[len(segs)-1] - } - return "" - } - - return u.Host -} - -func (self *SLoadbalancerListenerRule) GetRedirectPath() string { - u := self.getRedirectUrl() - if u == nil { - return "" - } - - return u.Path -} diff --git a/vendor/yunion.io/x/cloudmux/pkg/multicloud/azure/region.go b/vendor/yunion.io/x/cloudmux/pkg/multicloud/azure/region.go index 81cf14b47b..29486c6b42 100644 --- a/vendor/yunion.io/x/cloudmux/pkg/multicloud/azure/region.go +++ b/vendor/yunion.io/x/cloudmux/pkg/multicloud/azure/region.go @@ -23,6 +23,7 @@ import ( "yunion.io/x/jsonutils" "yunion.io/x/log" "yunion.io/x/pkg/errors" + "yunion.io/x/pkg/gotypes" "yunion.io/x/pkg/util/netutils" api "yunion.io/x/cloudmux/pkg/apis/compute" @@ -53,7 +54,7 @@ type SRegion struct { Longitude string } -///////////////////////////////////////////////////////////////////////////// +// /////////////////////////////////////////////////////////////////////////// func (self *SRegion) Refresh() error { // do nothing return nil @@ -400,57 +401,17 @@ func (region *SRegion) CreateISecurityGroup(opts *cloudprovider.SecurityGroupCre return region.CreateSecurityGroup(opts) } -func (region *SRegion) getIAppLBs() ([]cloudprovider.ICloudLoadbalancer, error) { - lbs := []SLoadbalancer{} - params := url.Values{} - params.Set("api-version", "2021-02-01") - err := region.list("Microsoft.Network/applicationGateways", params, &lbs) - if err != nil { - return nil, errors.Wrapf(err, "list") - } - - ilbs := make([]cloudprovider.ICloudLoadbalancer, len(lbs)) - for i := range lbs { - lbs[i].region = region - ilbs[i] = &lbs[i] - } - - return ilbs, nil -} - -func (region *SRegion) getINetworkLBs() ([]cloudprovider.ICloudLoadbalancer, error) { - lbs := []SLoadbalancer{} - params := url.Values{} - params.Set("api-version", "2021-02-01") - err := region.list("Microsoft.Network/loadBalancers", params, &lbs) - if err != nil { - return nil, errors.Wrapf(err, "list") - } - - ilbs := make([]cloudprovider.ICloudLoadbalancer, len(lbs)) - for i := range lbs { - lbs[i].region = region - ilbs[i] = &lbs[i] - } - - return ilbs, nil -} - func (region *SRegion) GetILoadBalancers() ([]cloudprovider.ICloudLoadbalancer, error) { - appLbs, err := region.getIAppLBs() + lbs, err := region.GetLoadbalancers() if err != nil { - return nil, errors.Wrap(err, "GetIAppLBs") + return nil, err } - - netLbs, err := region.getINetworkLBs() - if err != nil { - return nil, errors.Wrap(err, "GetINetworkLBs") + ret := []cloudprovider.ICloudLoadbalancer{} + for i := range lbs { + lbs[i].region = region + ret = append(ret, &lbs[i]) } - - lbs := []cloudprovider.ICloudLoadbalancer{} - lbs = append(lbs, appLbs...) - lbs = append(lbs, netLbs...) - return lbs, nil + return ret, nil } func (region *SRegion) GetILoadBalancerById(loadbalancerId string) (cloudprovider.ICloudLoadbalancer, error) { @@ -473,33 +434,34 @@ func (region *SRegion) GetILoadBalancerAclById(aclId string) (cloudprovider.IClo func (region *SRegion) GetILoadBalancerCertificateById(certId string) (cloudprovider.ICloudLoadbalancerCertificate, error) { segs := strings.Split(certId, "/sslCertificates") if len(segs[0]) > 0 { - lb, err := region.GetILoadBalancerById(segs[0]) + lb, err := region.GetLoadbalancer(segs[0]) if err != nil { return nil, errors.Wrap(err, "GetILoadBalancerById") } - - return lb.(*SLoadbalancer).GetILoadBalancerCertificateById(certId) + for i := range lb.Properties.SSLCertificates { + ssl := &lb.Properties.SSLCertificates[i] + ssl.region = region + if ssl.GetGlobalId() == certId { + return ssl, nil + } + } } - - return nil, errors.Wrap(cloudprovider.ErrNotFound, "GetILoadBalancerCertificateById") + return nil, errors.Wrap(cloudprovider.ErrNotFound, certId) } func (region *SRegion) GetILoadBalancerCertificates() ([]cloudprovider.ICloudLoadbalancerCertificate, error) { - lbs, err := region.GetILoadBalancers() + lbs, err := region.GetLoadbalancerCertificates() if err != nil { return nil, errors.Wrap(err, "GetILoadBalancers") } - certs := []cloudprovider.ICloudLoadbalancerCertificate{} + ret := []cloudprovider.ICloudLoadbalancerCertificate{} for i := range lbs { - _certs, err := lbs[i].(*SLoadbalancer).GetILoadBalancerCertificates() - if err != nil { - return nil, errors.Wrap(err, "GetILoadBalancerCertificates") - } - certs = append(certs, _certs...) + lbs[i].region = region + ret = append(ret, &lbs[i]) } - return certs, nil + return ret, nil } func (region *SRegion) CreateILoadBalancerCertificate(cert *cloudprovider.SLoadbalancerCertificate) (cloudprovider.ICloudLoadbalancerCertificate, error) { @@ -688,6 +650,19 @@ func (self *SRegion) list(resource string, params url.Values, retVal interface{} return jsonutils.Update(retVal, ret) } +func (self *SRegion) list_v2(resource string, apiVersion string, params url.Values) (jsonutils.JSONObject, error) { + if gotypes.IsNil(params) { + params = url.Values{} + } + params.Add("$filter", fmt.Sprintf("location eq '%s'", self.Name)) + params.Add("$filter", fmt.Sprintf("resourceType eq '%s'", resource)) + return self.client.list_v2("resources", apiVersion, params) +} + +func (self *SRegion) show(resource string, apiVersion string) (jsonutils.JSONObject, error) { + return self.client.list_v2(resource, apiVersion, nil) +} + func (region *SRegion) GetIVMs() ([]cloudprovider.ICloudVM, error) { zones, err := region.GetIZones() if err != nil { diff --git a/vendor/yunion.io/x/pkg/util/regutils/regutils.go b/vendor/yunion.io/x/pkg/util/regutils/regutils.go index fe31a2aa1f..d4180a15fe 100644 --- a/vendor/yunion.io/x/pkg/util/regutils/regutils.go +++ b/vendor/yunion.io/x/pkg/util/regutils/regutils.go @@ -50,6 +50,7 @@ var CLICKHOUSE_TIME_REG *regexp.Regexp var NORMAL_TIME_REG *regexp.Regexp var FULLNORMAL_TIME_REG *regexp.Regexp var RFC2882_TIME_REG *regexp.Regexp +var CEPH_TIME_REG *regexp.Regexp var EMAIL_REG *regexp.Regexp var CHINA_MOBILE_REG *regexp.Regexp var FS_FORMAT_REG *regexp.Regexp @@ -86,6 +87,8 @@ func init() { NORMAL_TIME_REG = regexp.MustCompile(`^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}$`) FULLNORMAL_TIME_REG = regexp.MustCompile(`^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{6}$`) RFC2882_TIME_REG = regexp.MustCompile(`[A-Z][a-z]{2}, [0-9]{1,2} [A-Z][a-z]{2} [0-9]{4} [0-9]{2}:[0-9]{2}:[0-9]{2} [A-Z]{3}`) + // Tue May 7 15:46:33 2024 + CEPH_TIME_REG = regexp.MustCompile(`[A-Z][a-z]{2} [A-Z][a-z]{2} [ 123][0-9] [0-9]{2}:[0-9]{2}:[0-9]{2} [0-9]{4}`) EMAIL_REG = regexp.MustCompile(`^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$`) CHINA_MOBILE_REG = regexp.MustCompile(`^1[0-9-]{10}$`) FS_FORMAT_REG = regexp.MustCompile(`^(ext|fat|hfs|xfs|swap|ntfs|reiserfs|ufs|btrfs)`) @@ -266,6 +269,10 @@ func MatchRFC2882Time(str string) bool { return RFC2882_TIME_REG.MatchString(str) } +func MatchCephTime(str string) bool { + return CEPH_TIME_REG.MatchString(str) +} + func MatchEmail(str string) bool { return EMAIL_REG.MatchString(str) } diff --git a/vendor/yunion.io/x/pkg/util/timeutils/timeutils.go b/vendor/yunion.io/x/pkg/util/timeutils/timeutils.go index 40ad62576c..db2429a6ab 100644 --- a/vendor/yunion.io/x/pkg/util/timeutils/timeutils.go +++ b/vendor/yunion.io/x/pkg/util/timeutils/timeutils.go @@ -66,6 +66,8 @@ const ( FullIsoNanoTimeFormat3 = "2006-01-02 15:04:05.000000000" RFC2882Format = time.RFC1123 + + CephTimeFormat = "Mon Jan 2 15:04:05 2006" ) func IsoTime(now time.Time) string { @@ -100,6 +102,10 @@ func RFC2882Time(now time.Time) string { return Utcify(now).Format(RFC2882Format) } +func CephTime(now time.Time) string { + return Utcify(now).Format(CephTimeFormat) +} + func DateStr(now time.Time) string { return Utcify(now).Format(DateFormat) } @@ -198,6 +204,10 @@ func ParseRFC2882Time(str string) (time.Time, error) { return time.Parse(RFC2882Format, str) } +func ParseCephTime(str string) (time.Time, error) { + return time.Parse(CephTimeFormat, str) +} + func ParseDate(str string) (time.Time, error) { return time.Parse(DateFormat, str) } @@ -240,6 +250,8 @@ func ParseTimeStr(str string) (time.Time, error) { return ParseFullNormalTime(str) } else if regutils.MatchRFC2882Time(str) { return ParseRFC2882Time(str) + } else if regutils.MatchCephTime(str) { + return ParseCephTime(str) } else if regutils.MatchCompactTime(str) { return ParseCompactTime(str) } else if regutils.MatchDate(str) {