Skip to content

Commit

Permalink
Merge pull request #1044 from ioito/hotfix/qx-aws-dns-traffic
Browse files Browse the repository at this point in the history
fix(aws): add dns traffic policy api
  • Loading branch information
ioito authored Aug 26, 2024
2 parents 84392f4 + 768b91f commit 54fc3cc
Show file tree
Hide file tree
Showing 13 changed files with 537 additions and 0 deletions.
1 change: 1 addition & 0 deletions pkg/cloudprovider/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -1319,6 +1319,7 @@ type ICloudDnsRecord interface {
GetDnsValue() string
GetTTL() int64
GetMxPriority() int64
GetExtraAddresses() ([]string, error)

Update(*DnsRecord) error

Expand Down
58 changes: 58 additions & 0 deletions pkg/multicloud/aliyun/dns_domain_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@
package aliyun

import (
"fmt"
"strconv"
"strings"

"yunion.io/x/pkg/errors"
"yunion.io/x/pkg/utils"

api "yunion.io/x/cloudmux/pkg/apis/compute"
"yunion.io/x/cloudmux/pkg/cloudprovider"
Expand Down Expand Up @@ -320,6 +322,62 @@ func (self *SDomainRecord) Disable() error {
return self.domain.client.SetDomainRecordStatus(self.RecordId, "Disable")
}

func (self *SAliyunClient) GetDnsExtraAddresses(dnsValue string) ([]string, error) {
ret := []string{}
if !strings.HasPrefix(dnsValue, "gtm") {
return ret, nil
}
instances, err := self.DescribeDnsGtmInstances()
if err != nil {
return nil, errors.Wrapf(err, "DescribeDnsGtmInstances")
}
for _, instance := range instances {
if instance.Config.PublicZoneName == dnsValue {
pools, err := self.DescribeDnsGtmInstanceAddressPools(instance.InstanceId)
if err != nil {
return nil, errors.Wrapf(err, "DescribeDnsGtmInstanceAddressPools")
}
for _, pool := range pools {
address, err := self.DescribeDnsGtmInstanceAddressPool(pool.AddrPoolId)
if err != nil {
return nil, errors.Wrapf(err, "DescribeDnsGtmInstanceAddressPools")
}
for _, addr := range address.Addrs.Addr {
if !utils.IsInStringArray(addr.Addr, ret) {
ret = append(ret, addr.Addr)
}
}
}
return ret, nil
}
}
gtm3, err := self.ListCloudGtmInstanceConfigs()
if err != nil {
return nil, errors.Wrapf(err, "ListCloudGtmInstanceConfigs")
}
for _, instance := range gtm3 {
if instance.ScheduleDomainName == dnsValue || dnsValue == fmt.Sprintf("%s.%s", instance.InstanceId, instance.ScheduleZoneName) {
for _, pool := range instance.AddressPools.AddressPool {
pool, err := self.DescribeCloudGtmAddressPool(pool.AddressPoolId)
if err != nil {
return nil, errors.Wrapf(err, "DescribeCloudGtmAddressPool")
}
for _, addr := range pool.Addresses.Address {
if !utils.IsInStringArray(addr.Address, ret) {
ret = append(ret, addr.Address)
}
}
}
return ret, nil
}
}
return ret, nil
}

func (self *SDomainRecord) GetExtraAddresses() ([]string, error) {
return self.domain.client.GetDnsExtraAddresses(self.GetDnsValue())
}

// line
func (client *SAliyunClient) UpdateDomainRecord(id string, opts *cloudprovider.DnsRecord) error {
line := GetRecordLineLineType(opts.PolicyValue)
Expand Down
111 changes: 111 additions & 0 deletions pkg/multicloud/aliyun/gtm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
// 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 aliyun

import (
"fmt"

"yunion.io/x/pkg/errors"
)

type SDnsGtmInstance struct {
InstanceId string
Config struct {
PublicZoneName string
}
}

func (self *SAliyunClient) DescribeDnsGtmInstances() ([]SDnsGtmInstance, error) {
params := map[string]string{
"PageSize": "100",
}
ret := []SDnsGtmInstance{}
pageNumber := 1
for {
params["PageNumber"] = fmt.Sprintf("%d", pageNumber)
resp, err := self.alidnsRequest("DescribeDnsGtmInstances", params)
if err != nil {
return nil, err
}
part := struct {
GtmInstances []SDnsGtmInstance
TotalItems int
}{}
err = resp.Unmarshal(&part)
if err != nil {
return nil, err
}
ret = append(ret, part.GtmInstances...)
if len(ret) >= part.TotalItems || len(part.GtmInstances) == 0 {
break
}
pageNumber++
}
return ret, nil
}

type SDnsGtmInstanceAddressPool struct {
Name string
AddrPoolId string
AddrCount int
Addrs struct {
Addr []struct {
Addr string
}
}
}

func (self *SAliyunClient) DescribeDnsGtmInstanceAddressPools(id string) ([]SDnsGtmInstanceAddressPool, error) {
params := map[string]string{"InstanceId": id, "PageSize": "100"}
ret := []SDnsGtmInstanceAddressPool{}
pageNumber := 1
for {
params["PageNumber"] = fmt.Sprintf("%d", pageNumber)
resp, err := self.alidnsRequest("DescribeDnsGtmInstanceAddressPools", params)
if err != nil {
return nil, err
}
part := struct {
AddrPools struct {
AddrPool []SDnsGtmInstanceAddressPool
}
TotalItems int
}{}
err = resp.Unmarshal(&part)
if err != nil {
return nil, err
}
ret = append(ret, part.AddrPools.AddrPool...)
if len(ret) >= part.TotalItems || len(part.AddrPools.AddrPool) == 0 {
break
}
pageNumber++
}
return ret, nil
}

func (self *SAliyunClient) DescribeDnsGtmInstanceAddressPool(id string) (*SDnsGtmInstanceAddressPool, error) {
params := map[string]string{"AddrPoolId": id}
resp, err := self.alidnsRequest("DescribeDnsGtmInstanceAddressPool", params)
if err != nil {
return nil, err
}
ret := &SDnsGtmInstanceAddressPool{}
err = resp.Unmarshal(ret)
if err != nil {
return nil, errors.Wrapf(err, "Unmarshal")
}
return ret, nil
}
81 changes: 81 additions & 0 deletions pkg/multicloud/aliyun/gtm3.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// 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 aliyun

import "fmt"

type SGtmInstanceConfigs struct {
InstanceId string
ScheduleDomainName string
ScheduleZoneName string
AddressPools struct {
AddressPool []struct {
AddressPoolId string
AddressPoolName string
}
}
}

func (self *SAliyunClient) ListCloudGtmInstanceConfigs() ([]SGtmInstanceConfigs, error) {
params := map[string]string{"PageSize": "100"}
ret := []SGtmInstanceConfigs{}
pageNumber := 1
for {
params["PageNumber"] = fmt.Sprintf("%d", pageNumber)
resp, err := self.alidnsRequest("ListCloudGtmInstanceConfigs", params)
if err != nil {
return nil, err
}
part := struct {
InstanceConfigs struct {
InstanceConfig []SGtmInstanceConfigs
}
TotalItems int
}{}
err = resp.Unmarshal(&part)
if err != nil {
return nil, err
}
ret = append(ret, part.InstanceConfigs.InstanceConfig...)
if len(ret) >= part.TotalItems || len(part.InstanceConfigs.InstanceConfig) == 0 {
break
}
pageNumber++
}
return ret, nil
}

type SGtmAddressPool struct {
AddressPoolId string
Addresses struct {
Address []struct {
Address string
}
}
}

func (self *SAliyunClient) DescribeCloudGtmAddressPool(id string) (*SGtmAddressPool, error) {
params := map[string]string{"AddressPoolId": id}
resp, err := self.alidnsRequest("DescribeCloudGtmAddressPool", params)
if err != nil {
return nil, err
}
ret := &SGtmAddressPool{}
err = resp.Unmarshal(ret)
if err != nil {
return nil, err
}
return ret, nil
}
4 changes: 4 additions & 0 deletions pkg/multicloud/aliyun/private_zone_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ func (self *SPvtzRecord) Delete() error {
return self.szone.client.DeleteZoneRecord(self.RecordId)
}

func (self *SPvtzRecord) GetExtraAddresses() ([]string, error) {
return []string{}, nil
}

func (self *SPvtzRecord) GetMxPriority() int64 {
if self.GetDnsType() == cloudprovider.DnsTypeMX {
return self.Priority
Expand Down
15 changes: 15 additions & 0 deletions pkg/multicloud/aliyun/shell/dns_domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
package shell

import (
"fmt"

"yunion.io/x/pkg/util/shellutils"

"yunion.io/x/cloudmux/pkg/cloudprovider"
Expand Down Expand Up @@ -113,6 +115,19 @@ func init() {
return nil
})

type DnsExtraAddressList struct {
DNS_VALUE string
}

shellutils.R(&DnsExtraAddressList{}, "dns-extra-address-list", "List extra address", func(cli *aliyun.SRegion, args *DnsExtraAddressList) error {
ret, e := cli.GetClient().GetDnsExtraAddresses(args.DNS_VALUE)
if e != nil {
return e
}
fmt.Println(ret)
return nil
})

type DomainRecordCreateOptions struct {
DOMAINNAME string
NAME string
Expand Down
59 changes: 59 additions & 0 deletions pkg/multicloud/aliyun/shell/gtm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// 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 shell

import (
"yunion.io/x/pkg/util/shellutils"

"yunion.io/x/cloudmux/pkg/multicloud/aliyun"
)

func init() {
type GtmListOptions struct {
}
shellutils.R(&GtmListOptions{}, "gtm-instance-list", "List Gtm", func(cli *aliyun.SRegion, args *GtmListOptions) error {
ret, err := cli.GetClient().DescribeDnsGtmInstances()
if err != nil {
return err
}
printList(ret, 0, 0, 0, []string{})
return nil
})

type GtmPoolListOptions struct {
ID string
}
shellutils.R(&GtmPoolListOptions{}, "gtm-instance-address-pool-list", "List Gtm address pool", func(cli *aliyun.SRegion, args *GtmPoolListOptions) error {
ret, err := cli.GetClient().DescribeDnsGtmInstanceAddressPools(args.ID)
if err != nil {
return err
}
printList(ret, 0, 0, 0, []string{})
return nil
})

type GtmPoolShowOptions struct {
ID string
}
shellutils.R(&GtmPoolShowOptions{}, "gtm-instance-address-pool-show", "Show Gtm address pool", func(cli *aliyun.SRegion, args *GtmPoolShowOptions) error {
ret, err := cli.GetClient().DescribeDnsGtmInstanceAddressPool(args.ID)
if err != nil {
return err
}
printObject(ret)
return nil
})

}
Loading

0 comments on commit 54fc3cc

Please sign in to comment.