Skip to content

Commit

Permalink
ADD DNS ZONE
Browse files Browse the repository at this point in the history
  • Loading branch information
PerhapsDy committed Jan 25, 2024
1 parent df524ae commit 960f0cb
Show file tree
Hide file tree
Showing 9 changed files with 719 additions and 0 deletions.
20 changes: 20 additions & 0 deletions baiducloud/connectivity/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/baidubce/bce-sdk-go/services/cert"
"github.com/baidubce/bce-sdk-go/services/cfc"
"github.com/baidubce/bce-sdk-go/services/cfs"
"github.com/baidubce/bce-sdk-go/services/dns"
"github.com/baidubce/bce-sdk-go/services/dts"
"github.com/baidubce/bce-sdk-go/services/eip"
"github.com/baidubce/bce-sdk-go/services/endpoint"
Expand Down Expand Up @@ -67,6 +68,7 @@ type BaiduClient struct {
blsConn *bls.Client
becConn *bec.Client
etGatewayConn *etGateway.Client
dnsConn *dns.Client
}

type ApiVersion string
Expand Down Expand Up @@ -606,6 +608,24 @@ func (client *BaiduClient) WithEtGatewayClient(do func(*etGateway.Client) (inter
return do(client.etGatewayConn)
}

func (client *BaiduClient) WithDNSClient(do func(*dns.Client) (interface{}, error)) (interface{}, error) {
goSdkMutex.Lock()
// Initialize the VPN client if necessary
if client.dnsConn == nil {
client.WithCommonClient(DNSCode)
dnsClient, err := dns.NewClient(client.Credentials.AccessKeyId, client.Credentials.SecretAccessKey, client.Endpoint)
if err != nil {
goSdkMutex.Unlock()
return nil, err
}
dnsClient.Config.Credentials = client.Credentials
dnsClient.Config.UserAgent = buildUserAgent()
client.dnsConn = dnsClient
}
goSdkMutex.Unlock()
return do(client.dnsConn)
}

func buildUserAgent() string {
return fmt.Sprintf("terraform-provider-baiducloud/%s", providerVersion)
}
10 changes: 10 additions & 0 deletions baiducloud/connectivity/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const (
BLSCode = ServiceCode("BLS")
BECCode = ServiceCode("BEC")
ETGATEWAYCode = ServiceCode("ETGATEWAY")
DNSCode = ServiceCode("DNS")
)

const (
Expand Down Expand Up @@ -125,6 +126,7 @@ const (
DefaultCERTEndPoint = "certificate.baidubce.com"
DefaultIAMEndPoint = "iam.bj.baidubce.com"
DefaultCDNEndPoint = "cdn.baidubce.com"
DefaultDNSEndPoint = "dns.baidubce.com"
)

var (
Expand All @@ -145,6 +147,7 @@ var (
CERTCode: DefaultCERTEndPoint,
IAMCode: DefaultIAMEndPoint,
CDNCode: DefaultCDNEndPoint,
DNSCode: DefaultDNSEndPoint,

BCCCode: DefaultBJRegionBccEndPoint,
BBCCode: DefaultBJRegionBbcEndPoint,
Expand Down Expand Up @@ -174,6 +177,7 @@ var (
CERTCode: DefaultCERTEndPoint,
IAMCode: DefaultIAMEndPoint,
CDNCode: DefaultCDNEndPoint,
DNSCode: DefaultDNSEndPoint,

BCCCode: DefaultBDRegionBccEndPoint,
VPCCode: DefaultBDRegionBccEndPoint,
Expand All @@ -190,6 +194,7 @@ var (
CERTCode: DefaultCERTEndPoint,
IAMCode: DefaultIAMEndPoint,
CDNCode: DefaultCDNEndPoint,
DNSCode: DefaultDNSEndPoint,

BCCCode: DefaultGZRegionBccEndPoint,
VPNCode: DefaultGZRegionVpnEndPoint,
Expand All @@ -214,6 +219,7 @@ var (
CERTCode: DefaultCERTEndPoint,
IAMCode: DefaultIAMEndPoint,
CDNCode: DefaultCDNEndPoint,
DNSCode: DefaultDNSEndPoint,

BCCCode: DefaultSURegionBccEndPoint,
BBCCode: DefaultSURegionBbcEndPoint,
Expand All @@ -240,6 +246,7 @@ var (
CERTCode: DefaultCERTEndPoint,
IAMCode: DefaultIAMEndPoint,
CDNCode: DefaultCDNEndPoint,
DNSCode: DefaultDNSEndPoint,

BCCCode: DefaultFSHRegionBccEndPoint,
VPNCode: DefaultFSHRegionVPNEndPoint,
Expand All @@ -252,6 +259,7 @@ var (
CERTCode: DefaultCERTEndPoint,
IAMCode: DefaultIAMEndPoint,
CDNCode: DefaultCDNEndPoint,
DNSCode: DefaultDNSEndPoint,

BCCCode: DefaultFWHRegionBccEndPoint,
BBCCode: DefaultFWHRegionBbcEndPoint,
Expand All @@ -276,6 +284,7 @@ var (
CERTCode: DefaultCERTEndPoint,
IAMCode: DefaultIAMEndPoint,
CDNCode: DefaultCDNEndPoint,
DNSCode: DefaultDNSEndPoint,

BCCCode: DefaultHKGRegionBccEndPoint,
BBCCode: DefaultHKGRegionBbcEndPoint,
Expand All @@ -297,6 +306,7 @@ var (
CERTCode: DefaultCERTEndPoint,
IAMCode: DefaultIAMEndPoint,
CDNCode: DefaultCDNEndPoint,
DNSCode: DefaultDNSEndPoint,

BCCCode: DefaultSINRegionBccEndPoint,
VPCCode: DefaultSINRegionBccEndPoint,
Expand Down
201 changes: 201 additions & 0 deletions baiducloud/data_source_baiducloud_dns_zones.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
/*
Use this data source to query Dns zone list.
Example Usage
```hcl
data "baiducloud_dns_zones" "default" {
name = "xxxx"
}
output "zones" {
value = "${data.baiducloud_dns_zones.default.zones}"
}
```
*/
package baiducloud

import (
"github.com/baidubce/bce-sdk-go/services/dns"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-baiducloud/baiducloud/connectivity"
)

func dataSourceBaiduCloudDnsZones() *schema.Resource {
return &schema.Resource{
Read: dataSourceBaiduCloudDnszonesRead,

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Description: "name of DNS ZONE",
Optional: true,
ForceNew: true,
},
"output_file": {
Type: schema.TypeString,
Description: "DNS Zones search result output file",
Optional: true,
ForceNew: true,
},
"filter": dataSourceFiltersSchema(),

"zones": {
Type: schema.TypeList,
Description: "zone list",
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Description: "Dns zone name",
Required: true,
ForceNew: true,
},
"zone_id": {
Type: schema.TypeString,
Description: "Dns zone id",
Computed: true,
},
"status": {
Type: schema.TypeString,
Description: "Dns zone status",
Computed: true,
},
"product_version": {
Type: schema.TypeString,
Description: "Dns zone product_version",
Computed: true,
},
"create_time": {
Type: schema.TypeString,
Description: "Dns zone create_time",
Computed: true,
},
"expire_time": {
Type: schema.TypeString,
Description: "Dns zone expire_time",
Computed: true,
},
"tags": tagsSchema(),
},
},
},
},
}
}

func dataSourceBaiduCloudDnszonesRead(d *schema.ResourceData, meta interface{}) error {

name := d.Get("name").(string)

action := "List all dns zone name is " + name

dnsZoneArgs, err := buildBaiduCloudDnszoneListArgs(d)

if err != nil {
return WrapError(err)
}

zones, err := listAllDnsZones(dnsZoneArgs, meta)

addDebug(action, zones)

if err != nil {
if NotFoundError(err) {
d.SetId("")
return nil
}
return WrapErrorf(err, DefaultErrorMsg, "baiducloud_dns_zones", action, BCESDKGoERROR)
}

zonesResult := make([]map[string]interface{}, 0)

for _, zone := range zones {

innerMap := make(map[string]interface{})
innerMap["zone_id"] = zone.Id
innerMap["name"] = zone.Name
innerMap["status"] = zone.Status
innerMap["product_version"] = zone.ProductVersion
innerMap["create_time"] = zone.CreateTime
innerMap["expire_time"] = zone.ExpireTime
innerMap["tags"] = zoneTagsToMap(zone.Tags)

if err != nil {
return WrapErrorf(err, DefaultErrorMsg, "baiducloud_dns_zones", action, BCESDKGoERROR)
}

zonesResult = append(zonesResult, innerMap)
}

addDebug(action, zonesResult)

FilterDataSourceResult(d, &zonesResult)

if err := d.Set("zones", zonesResult); err != nil {
return WrapErrorf(err, DefaultErrorMsg, "baiducloud_dns_zones", action, BCESDKGoERROR)
}

d.SetId(resource.UniqueId())

if v, ok := d.GetOk("output_file"); ok && v.(string) != "" {
if err := writeToFile(v.(string), zonesResult); err != nil {
return WrapErrorf(err, DefaultErrorMsg, "baiducloud_dns_zones", action, BCESDKGoERROR)
}
}
return nil
}

func buildBaiduCloudDnszoneListArgs(d *schema.ResourceData) (*dns.ListZoneRequest, error) {

request := &dns.ListZoneRequest{}

if v, ok := d.GetOk("name"); ok && len(v.(string)) > 0 {
request.Name = v.(string)
}

return request, nil
}

func listAllDnsZones(args *dns.ListZoneRequest, meta interface{}) ([]dns.Zone, error) {
client := meta.(*connectivity.BaiduClient)
action := "List all dns zones "

zones := make([]dns.Zone, 0)

for {
raw, err := client.WithDNSClient(func(dnsClient *dns.Client) (interface{}, error) {
return dnsClient.ListZone(args)
})

if err != nil {
return nil, WrapErrorf(err, DefaultErrorMsg, "baiducloud_dns_zone", action, BCESDKGoERROR)
}

result, _ := raw.(*dns.ListZoneResponse)
zones = append(zones, result.Zones...)

if !result.IsTruncated {
break
}

args.Marker = result.NextMarker
args.MaxKeys = int(result.MaxKeys)
}

return zones, nil
}

func zoneTagsToMap(tags []dns.TagModel) map[string]string {

tagMap := make(map[string]string)

for _, tag := range tags {
tagMap[*tag.TagKey] = *tag.TagValue
}

return tagMap

}
Loading

0 comments on commit 960f0cb

Please sign in to comment.