Golang – 删除 Route53 记录

huangapple go评论156阅读模式
英文:

Golang - Delete Route53 Record

问题

我正在编写一个Go程序(使用AWS Go SDK),用于管理(创建、删除)Route53记录。我已经成功创建了一条记录,但在删除记录时遇到了问题。

这是我到目前为止尝试过的代码:

func deleteRecord(svc *route53.Route53) {
    dnsName := "vikas027.something.net"

    request := &route53.ChangeResourceRecordSetsInput{
        ChangeBatch: &route53.ChangeBatch{
            Changes: []*route53.Change{
                {
                    Action: aws.String("DELETE"),
                    ResourceRecordSet: &route53.ResourceRecordSet{
                        Name: aws.String(dnsName),
                        Type: aws.String("A"),
                        ResourceRecords: []*route53.ResourceRecord{
                            {
                                Value: aws.String(dnsName),
                            },
                        },
                    },
                },
            },
        },
        HostedZoneId: aws.String(zoneID),
    }
    resp, err := svc.ChangeResourceRecordSets(request)
    if err != nil {
        fmt.Println("Unable to delete DNS Record", err)
    }
    fmt.Println(resp)
}

上述函数抛出以下错误:

Unable to delete DNS RecordInvalidInput: Invalid request
        status code: 400, request id: 0363068b-6d4d-11e7-ab47-cf662178898b
英文:

I am writing a go program (using AWS Go SDK) which manages (creates, deletes) Route53 Records. I have managed to create a record, but I am having issues while deleting those.

This is what I have tried so far.

func deleteRecord(svc *route53.Route53) {
	dnsName := "vikas027.something.net"

	request := &route53.ChangeResourceRecordSetsInput{
		ChangeBatch: &route53.ChangeBatch{
			Changes: []*route53.Change{
				{
					Action: aws.String("DELETE"),
					ResourceRecordSet: &route53.ResourceRecordSet{
						Name: aws.String(dnsName),
						Type: aws.String("A"),
						ResourceRecords: []*route53.ResourceRecord{
							{
								Value: aws.String(dnsName),
							},
						},
					},
				},
			},
		},
		HostedZoneId: aws.String(zoneID),
	}
	resp, err := svc.ChangeResourceRecordSets(request)
	if err != nil {
		fmt.Println("Unable to delete DNS Record", err)
	}
	fmt.Println(resp)
}

Above function throws below error.

Unable to delete DNS RecordInvalidInput: Invalid request
        status code: 400, request id: 0363068b-6d4d-11e7-ab47-cf662178898b

答案1

得分: 6

再次感谢Imperalix的提示。基本思路是使用创建记录时使用的确切参数。这是我现在用来删除CNAME记录的方法。

func deleteRecord(svc *route53.Route53) {
    dnsName := "vikas027.something.net"

    request := &route53.ChangeResourceRecordSetsInput{
        ChangeBatch: &route53.ChangeBatch{
            Changes: []*route53.Change{
                {
                    Action: aws.String("DELETE"),
                    ResourceRecordSet: &route53.ResourceRecordSet{
                        Name: aws.String(dnsName),
                        Type: aws.String("CNAME"),
                        ResourceRecords: []*route53.ResourceRecord{
                            {
                                Value: aws.String(AliasName),
                            },
                        },
                        TTL:           aws.Int64(ttl),
                        Weight:        aws.Int64(weight),
                        SetIdentifier: aws.String(setIdentifier),
                    },
                },
            },
        },
        HostedZoneId: aws.String(zoneID),
    }
    resp, err := svc.ChangeResourceRecordSets(request)
    if err != nil {
        fmt.Println("Unable to delete DNS Record", err)
    }
    fmt.Println(resp)
}
英文:

Thanks again Imperalix for the hint. The basic idea is to use the exact parameters which were used to create the record. This is what is now working for me to delete CNAME records.

func deleteRecord(svc *route53.Route53) {
    dnsName := "vikas027.something.net"

    request := &route53.ChangeResourceRecordSetsInput{
        ChangeBatch: &route53.ChangeBatch{
            Changes: []*route53.Change{
                {
                    Action: aws.String("DELETE"),
                    ResourceRecordSet: &route53.ResourceRecordSet{
                        Name: aws.String(dnsName),
                        Type: aws.String("CNAME"),
                        ResourceRecords: []*route53.ResourceRecord{
                            {
                                Value: aws.String(AliasName),
                            },
                        },
					    TTL:           aws.Int64(ttl),
					    Weight:        aws.Int64(weight),
					    SetIdentifier: aws.String(setIdentifier),
                    },
                },
            },
        },
        HostedZoneId: aws.String(zoneID),
    }
    resp, err := svc.ChangeResourceRecordSets(request)
    if err != nil {
        fmt.Println("Unable to delete DNS Record", err)
    }
    fmt.Println(resp)
}

huangapple
  • 本文由 发表于 2017年7月20日 21:21:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/45216035.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定