From 90f36bc9f909cc60aa807de8dca34f7167406fa7 Mon Sep 17 00:00:00 2001 From: edisonxiang Date: Thu, 31 Jan 2019 14:08:59 +0800 Subject: [PATCH] add multi router support for dns zone --- openstack/dns/v2/zones/requests.go | 41 ++++++++++++++++++++++++++++++ openstack/dns/v2/zones/results.go | 19 ++++++++++++++ openstack/dns/v2/zones/urls.go | 8 ++++++ 3 files changed, 68 insertions(+) diff --git a/openstack/dns/v2/zones/requests.go b/openstack/dns/v2/zones/requests.go index dd9782a1e..a3aa557c1 100644 --- a/openstack/dns/v2/zones/requests.go +++ b/openstack/dns/v2/zones/requests.go @@ -172,3 +172,44 @@ func Delete(client *golangsdk.ServiceClient, zoneID string) (r DeleteResult) { }) return } + +// RouterOptsBuilder allows to add parameters to the associate/disassociate Zone request. +type RouterOptsBuilder interface { + ToRouterMap() (map[string]interface{}, error) +} + +// RouterOpts specifies the required information to associate/disassociate a Router with a Zone. +type RouterOpts struct { + // Router ID + RouterID string `json:"router_id" required:"true"` + + // Router Region + RouterRegion string `json:"router_region,omitempty"` +} + +// ToRouterMap constructs a request body from RouterOpts. +func (opts RouterOpts) ToRouterMap() (map[string]interface{}, error) { + return golangsdk.BuildRequestBody(opts, "router") +} + +// AssociateZone associate a Router with a Zone. +func AssociateZone(client *golangsdk.ServiceClient, zoneID string, opts RouterOptsBuilder) (r AssociateResult) { + b, err := opts.ToRouterMap() + if err != nil { + r.Err = err + return + } + _, r.Err = client.Post(associateURL(client, zoneID), b, nil, nil) + return +} + +// DisassociateZone disassociate a Router with a Zone. +func DisassociateZone(client *golangsdk.ServiceClient, zoneID string, opts RouterOptsBuilder) (r DisassociateResult) { + b, err := opts.ToRouterMap() + if err != nil { + r.Err = err + return + } + _, r.Err = client.Post(disassociateURL(client, zoneID), b, nil, nil) + return +} diff --git a/openstack/dns/v2/zones/results.go b/openstack/dns/v2/zones/results.go index 1f15872a9..dbfb97c29 100644 --- a/openstack/dns/v2/zones/results.go +++ b/openstack/dns/v2/zones/results.go @@ -126,6 +126,15 @@ type Zone struct { // Links includes HTTP references to the itself, useful for passing along // to other APIs that might want a server reference. Links map[string]interface{} `json:"links"` + + // Routers associate with the Zone + Routers []RouterResult `json:"routers"` +} + +type RouterResult struct { + RouterID string `json:"router_id"` + RouterRegion string `json:"router_region"` + Status string `json:"status"` } func (r *Zone) UnmarshalJSON(b []byte) error { @@ -165,3 +174,13 @@ func (r *Zone) UnmarshalJSON(b []byte) error { return err } + +// AssociateResult is the response from AssociateZone +type AssociateResult struct { + commonResult +} + +// DisassociateResult is the response from DisassociateZone +type DisassociateResult struct { + commonResult +} diff --git a/openstack/dns/v2/zones/urls.go b/openstack/dns/v2/zones/urls.go index 2f6fd4a2b..e0178771b 100644 --- a/openstack/dns/v2/zones/urls.go +++ b/openstack/dns/v2/zones/urls.go @@ -9,3 +9,11 @@ func baseURL(c *golangsdk.ServiceClient) string { func zoneURL(c *golangsdk.ServiceClient, zoneID string) string { return c.ServiceURL("zones", zoneID) } + +func associateURL(client *golangsdk.ServiceClient, zoneID string) string { + return client.ServiceURL("zones", zoneID, "associaterouter") +} + +func disassociateURL(client *golangsdk.ServiceClient, zoneID string) string { + return client.ServiceURL("zones", zoneID, "disassociaterouter") +}