Unverified Commit c0a5e5eb authored by OpenShift Merge Robot's avatar OpenShift Merge Robot Committed by GitHub
Browse files

Merge pull request #344 from openshift-cherrypick-robot/cherry-pick-300-to-release-4.2

Bug 1788707: [release-4.2] NE-250: Support both DNSZone and PrivateDNSZone resources
parents 2a6afcee b2cf1d4c
......@@ -152,8 +152,6 @@ github.com/onsi/gomega v1.4.3 h1:RE1xgDvH7imwFD45h+u2SgIfERHlS2yNG4DObb5BSKU=
github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/onsi/gomega v1.5.0 h1:izbySO9zDPmjJ8rDjLvkA2zJHIo+HkYXHnf7eN7SSyo=
github.com/onsi/gomega v1.5.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/openshift/api v3.9.1-0.20190713024856-f15120709e0a+incompatible h1:Y/UXROcoe8yS41+3Q9kgm2GgIv/1Z10eRh4XAiP3w74=
github.com/openshift/api v3.9.1-0.20190713024856-f15120709e0a+incompatible/go.mod h1:dh9o4Fs58gpFXGSYfnVxGR9PnV53I8TW84pQaJDdGiY=
github.com/openshift/api v3.9.1-0.20190905013149-e6c3eeae444f+incompatible h1:CARWXyP03A6YNIk3nC6HdJ1ths/0fUtZDPJqYD5UDi4=
github.com/openshift/api v3.9.1-0.20190905013149-e6c3eeae444f+incompatible/go.mod h1:dh9o4Fs58gpFXGSYfnVxGR9PnV53I8TW84pQaJDdGiY=
github.com/openshift/library-go v0.0.0-20190613200606-e617f832835d h1:a/+OnGnX5qMHSSuKcSSLMPZghOgD48NNZgzDklj9cIc=
......
......@@ -4,6 +4,7 @@ import (
"context"
"github.com/Azure/azure-sdk-for-go/services/dns/mgmt/2017-10-01/dns"
privatedns "github.com/Azure/azure-sdk-for-go/services/privatedns/mgmt/2018-09-01/privatedns"
"github.com/pkg/errors"
)
......@@ -33,27 +34,63 @@ type ARecord struct {
}
type dnsClient struct {
zones dns.ZonesClient
recordSets dns.RecordSetsClient
config Config
recordSetClient, privateRecordSetClient DNSClient
}
// New returns an authenticated DNSClient
func New(config Config, userAgentExtension string) (DNSClient, error) {
rsc, err := newRecordSetClient(config, userAgentExtension)
if err != nil {
return nil, errors.Wrap(err, "failed to create recordSetClient")
}
prsc, err := newPrivateRecordSetClient(config, userAgentExtension)
if err != nil {
return nil, errors.Wrap(err, "failed to create privateRecordSetClient")
}
return &dnsClient{recordSetClient: rsc, privateRecordSetClient: prsc}, nil
}
func (c *dnsClient) Put(ctx context.Context, zone Zone, arec ARecord) error {
switch zone.Provider {
case "Microsoft.Network/privateDnsZones":
return c.privateRecordSetClient.Put(ctx, zone, arec)
case "Microsoft.Network/dnszones":
return c.recordSetClient.Put(ctx, zone, arec)
default:
return errors.Errorf("unsupported Zone provider %s", zone.Provider)
}
}
func (c *dnsClient) Delete(ctx context.Context, zone Zone, arec ARecord) error {
switch zone.Provider {
case "Microsoft.Network/privateDnsZones":
return c.privateRecordSetClient.Delete(ctx, zone, arec)
case "Microsoft.Network/dnszones":
return c.recordSetClient.Delete(ctx, zone, arec)
default:
return errors.Errorf("unsupported Zone provider %s", zone.Provider)
}
}
type recordSetClient struct {
client dns.RecordSetsClient
}
func newRecordSetClient(config Config, userAgentExtension string) (*recordSetClient, error) {
authorizer, err := getAuthorizerForResource(config)
if err != nil {
return nil, err
}
zc := dns.NewZonesClient(config.SubscriptionID)
zc.AddToUserAgent(userAgentExtension)
zc.Authorizer = authorizer
rc := dns.NewRecordSetsClient(config.SubscriptionID)
rc.AddToUserAgent(userAgentExtension)
rc.Authorizer = authorizer
return &dnsClient{zones: zc, recordSets: rc, config: config}, nil
return &recordSetClient{client: rc}, nil
}
func (c *dnsClient) Put(ctx context.Context, zone Zone, arec ARecord) error {
func (c *recordSetClient) Put(ctx context.Context, zone Zone, arec ARecord) error {
rs := dns.RecordSet{
RecordSetProperties: &dns.RecordSetProperties{
TTL: &arec.TTL,
......@@ -62,20 +99,65 @@ func (c *dnsClient) Put(ctx context.Context, zone Zone, arec ARecord) error {
},
},
}
_, err := c.recordSets.CreateOrUpdate(ctx, zone.ResourceGroup, zone.Name, arec.Name, dns.A, rs, "", "")
_, err := c.client.CreateOrUpdate(ctx, zone.ResourceGroup, zone.Name, arec.Name, dns.A, rs, "", "")
if err != nil {
return errors.Wrapf(err, "failed to update dns a record: %s.%s", arec.Name, zone.Name)
}
return nil
}
func (c *dnsClient) Delete(ctx context.Context, zone Zone, arec ARecord) error {
_, err := c.recordSets.Get(ctx, zone.ResourceGroup, zone.Name, arec.Name, dns.A)
func (c *recordSetClient) Delete(ctx context.Context, zone Zone, arec ARecord) error {
_, err := c.client.Get(ctx, zone.ResourceGroup, zone.Name, arec.Name, dns.A)
if err != nil {
// TODO: How do we interpret this as a notfound error?
return nil
}
_, err = c.client.Delete(ctx, zone.ResourceGroup, zone.Name, arec.Name, dns.A, "")
if err != nil {
return errors.Wrapf(err, "failed to delete dns a record: %s.%s", arec.Name, zone.Name)
}
return nil
}
type privateRecordSetClient struct {
client privatedns.RecordSetsClient
}
func newPrivateRecordSetClient(config Config, userAgentExtension string) (*privateRecordSetClient, error) {
authorizer, err := getAuthorizerForResource(config)
if err != nil {
return nil, err
}
prc := privatedns.NewRecordSetsClient(config.SubscriptionID)
prc.AddToUserAgent(userAgentExtension)
prc.Authorizer = authorizer
return &privateRecordSetClient{client: prc}, nil
}
func (c *privateRecordSetClient) Put(ctx context.Context, zone Zone, arec ARecord) error {
rs := privatedns.RecordSet{
RecordSetProperties: &privatedns.RecordSetProperties{
TTL: &arec.TTL,
ARecords: &[]privatedns.ARecord{
{Ipv4Address: &arec.Address},
},
},
}
_, err := c.client.CreateOrUpdate(ctx, zone.ResourceGroup, zone.Name, privatedns.A, arec.Name, rs, "", "")
if err != nil {
return errors.Wrapf(err, "failed to update dns a record: %s.%s", arec.Name, zone.Name)
}
return nil
}
func (c *privateRecordSetClient) Delete(ctx context.Context, zone Zone, arec ARecord) error {
_, err := c.client.Get(ctx, zone.ResourceGroup, zone.Name, privatedns.A, arec.Name)
if err != nil {
// TODO: How do we interpret this as a notfound error?
return nil
}
_, err = c.recordSets.Delete(ctx, zone.ResourceGroup, zone.Name, arec.Name, dns.A, "")
_, err = c.client.Delete(ctx, zone.ResourceGroup, zone.Name, privatedns.A, arec.Name, "")
if err != nil {
return errors.Wrapf(err, "failed to delete dns a record: %s.%s", arec.Name, zone.Name)
}
......
......@@ -2,12 +2,14 @@ package client
import (
"errors"
"fmt"
"strings"
)
type Zone struct {
SubscriptionID string
ResourceGroup string
Provider string
Name string
}
......@@ -16,5 +18,5 @@ func ParseZone(id string) (*Zone, error) {
if len(s) < 9 {
return nil, errors.New("invalid azure dns zone id")
}
return &Zone{SubscriptionID: s[2], ResourceGroup: s[4], Name: s[8]}, nil
return &Zone{SubscriptionID: s[2], ResourceGroup: s[4], Provider: fmt.Sprintf("%s/%s", s[6], s[7]), Name: s[8]}, nil
}
......@@ -9,19 +9,22 @@ import (
func TestFunction(t *testing.T) {
var zoneTests = []struct {
desc string
id string
err error
subID string
rg string
name string
desc string
id string
err error
subID string
rg string
provider string
name string
}{
{"TestValidZoneID", "/subscriptions/E540B02D-5CCE-4D47-A13B-EB05A19D696E/resourceGroups/test-rg/providers/Microsoft.Network/dnszones/test-rg.dnszone.io",
nil, "E540B02D-5CCE-4D47-A13B-EB05A19D696E", "test-rg", "test-rg.dnszone.io"},
nil, "E540B02D-5CCE-4D47-A13B-EB05A19D696E", "test-rg", "Microsoft.Network/dnszones", "test-rg.dnszone.io"},
{"TestValidZoneID", "/subscriptions/E540B02D-5CCE-4D47-A13B-EB05A19D696E/resourceGroups/test-rg/providers/Microsoft.Network/privateDnsZones/test-rg.dnszone.io",
nil, "E540B02D-5CCE-4D47-A13B-EB05A19D696E", "test-rg", "Microsoft.Network/privateDnsZones", "test-rg.dnszone.io"},
{"TestInvalidZoneID", "/subscriptions/E540B02D-5CCE-4D47-A13B-EB05A19D696E/resourceGroups/test-rg/providers/Microsoft.Network/dnszones",
errors.New("invalid azure dns zone id"), "E540B02D-5CCE-4D47-A13B-EB05A19D696E", "test-rg", "test-rg.dnszone.io"},
errors.New("invalid azure dns zone id"), "E540B02D-5CCE-4D47-A13B-EB05A19D696E", "test-rg", "Microsoft.Network/dnszones", "test-rg.dnszone.io"},
{"TestEmptyZoneID", "", errors.New("invalid azure dns zone id"),
"E540B02D-5CCE-4D47-A13B-EB05A19D696E", "test-rg", "test-rg.dnszone.io"},
"E540B02D-5CCE-4D47-A13B-EB05A19D696E", "test-rg", "Microsoft.Network/dnszones", "test-rg.dnszone.io"},
}
for _, tt := range zoneTests {
t.Run(tt.desc, func(t *testing.T) {
......@@ -44,6 +47,9 @@ func TestFunction(t *testing.T) {
if zone.ResourceGroup != tt.rg {
t.Errorf("expected [%s] actual [%s]", tt.rg, zone.ResourceGroup)
}
if zone.Provider != tt.provider {
t.Errorf("expected [%s] actual [%s]", tt.provider, zone.Provider)
}
if zone.Name != tt.name {
t.Errorf("expected [%s] actual [%s]", tt.name, zone.Name)
}
......
// Package privatedns implements the Azure ARM Privatedns service API version 2018-09-01.
//
// The Private DNS Management Client.
package privatedns
// Copyright (c) Microsoft and contributors. All rights reserved.
//
// 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.
//
// Code generated by Microsoft (R) AutoRest Code Generator.
// Changes may cause incorrect behavior and will be lost if the code is regenerated.
import (
"github.com/Azure/go-autorest/autorest"
)
const (
// DefaultBaseURI is the default URI used for the service Privatedns
DefaultBaseURI = "https://management.azure.com"
)
// BaseClient is the base client for Privatedns.
type BaseClient struct {
autorest.Client
BaseURI string
SubscriptionID string
}
// New creates an instance of the BaseClient client.
func New(subscriptionID string) BaseClient {
return NewWithBaseURI(DefaultBaseURI, subscriptionID)
}
// NewWithBaseURI creates an instance of the BaseClient client.
func NewWithBaseURI(baseURI string, subscriptionID string) BaseClient {
return BaseClient{
Client: autorest.NewClientWithUserAgent(UserAgent()),
BaseURI: baseURI,
SubscriptionID: subscriptionID,
}
}
package privatedns
import "github.com/Azure/azure-sdk-for-go/version"
// Copyright (c) Microsoft and contributors. All rights reserved.
//
// 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.
//
// Code generated by Microsoft (R) AutoRest Code Generator.
// Changes may cause incorrect behavior and will be lost if the code is regenerated.
// UserAgent returns the UserAgent string to use when sending http.Requests.
func UserAgent() string {
return "Azure-SDK-For-Go/" + version.Number + " privatedns/2018-09-01"
}
// Version returns the semantic version (see http://semver.org) of the client.
func Version() string {
return version.Number
}
......@@ -4,6 +4,7 @@ cloud.google.com/go/compute/metadata
contrib.go.opencensus.io/exporter/ocagent
# github.com/Azure/azure-sdk-for-go v30.0.0+incompatible
github.com/Azure/azure-sdk-for-go/services/dns/mgmt/2017-10-01/dns
github.com/Azure/azure-sdk-for-go/services/privatedns/mgmt/2018-09-01/privatedns
github.com/Azure/azure-sdk-for-go/version
# github.com/Azure/go-autorest/autorest v0.2.0
github.com/Azure/go-autorest/autorest
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment