provider/openstack: Add DNS V2 RecordSet resource (#14813)

* vendor gophercloud dns v2 recordset deps

* openstack dns v2 recordset resource

* fix type assertion panic condition

* address pr review feedback
This commit is contained in:
jrperritt
2017-05-24 23:58:44 -05:00
committed by Joe Topjian
parent acedf7228f
commit e1c8d760d3
12 changed files with 985 additions and 1 deletions

View File

@@ -0,0 +1,6 @@
// Package recordsets provides information and interaction with the zone API
// resource for the OpenStack DNS service.
//
// For more information, see:
// http://developer.openstack.org/api-ref/dns/#recordsets
package recordsets

View File

@@ -0,0 +1,157 @@
package recordsets
import (
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/pagination"
)
// ListOptsBuilder allows extensions to add additional parameters to the
// List request.
type ListOptsBuilder interface {
ToRecordSetListQuery() (string, error)
}
// ListOpts allows the filtering and sorting of paginated collections through
// the API. Filtering is achieved by passing in struct field values that map to
// the server attributes you want to see returned. Marker and Limit are used
// for pagination.
// https://developer.openstack.org/api-ref/dns/
type ListOpts struct {
// Integer value for the limit of values to return.
Limit int `q:"limit"`
// UUID of the recordset at which you want to set a marker.
Marker string `q:"marker"`
Data string `q:"data"`
Description string `q:"description"`
Name string `q:"name"`
SortDir string `q:"sort_dir"`
SortKey string `q:"sort_key"`
Status string `q:"status"`
TTL int `q:"ttl"`
Type string `q:"type"`
ZoneID string `q:"zone_id"`
}
// ToRecordSetListQuery formats a ListOpts into a query string.
func (opts ListOpts) ToRecordSetListQuery() (string, error) {
q, err := gophercloud.BuildQueryString(opts)
return q.String(), err
}
// ListByZone implements the recordset list request.
func ListByZone(client *gophercloud.ServiceClient, zoneID string, opts ListOptsBuilder) pagination.Pager {
url := baseURL(client, zoneID)
if opts != nil {
query, err := opts.ToRecordSetListQuery()
if err != nil {
return pagination.Pager{Err: err}
}
url += query
}
return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
return RecordSetPage{pagination.LinkedPageBase{PageResult: r}}
})
}
// Get implements the recordset get request.
func Get(client *gophercloud.ServiceClient, zoneID string, rrsetID string) (r GetResult) {
_, r.Err = client.Get(rrsetURL(client, zoneID, rrsetID), &r.Body, nil)
return
}
// CreateOptsBuilder allows extensions to add additional attributes to the Create request.
type CreateOptsBuilder interface {
ToRecordSetCreateMap() (map[string]interface{}, error)
}
// CreateOpts specifies the base attributes that may be used to create a RecordSet.
type CreateOpts struct {
// Name is the name of the RecordSet.
Name string `json:"name" required:"true"`
// Description is a description of the RecordSet.
Description string `json:"description,omitempty"`
// Records are the DNS records of the RecordSet.
Records []string `json:"records,omitempty"`
// TTL is the time to live of the RecordSet.
TTL int `json:"ttl,omitempty"`
// Type is the RRTYPE of the RecordSet.
Type string `json:"type,omitempty"`
}
// ToRecordSetCreateMap formats an CreateOpts structure into a request body.
func (opts CreateOpts) ToRecordSetCreateMap() (map[string]interface{}, error) {
b, err := gophercloud.BuildRequestBody(opts, "")
if err != nil {
return nil, err
}
return b, nil
}
// Create creates a recordset in a given zone.
func Create(client *gophercloud.ServiceClient, zoneID string, opts CreateOptsBuilder) (r CreateResult) {
b, err := opts.ToRecordSetCreateMap()
if err != nil {
r.Err = err
return
}
_, r.Err = client.Post(baseURL(client, zoneID), &b, &r.Body, &gophercloud.RequestOpts{
OkCodes: []int{201, 202},
})
return
}
// UpdateOptsBuilder allows extensions to add additional attributes to the Update request.
type UpdateOptsBuilder interface {
ToRecordSetUpdateMap() (map[string]interface{}, error)
}
// UpdateOpts specifies the base attributes that may be updated on an existing RecordSet.
type UpdateOpts struct {
Description string `json:"description,omitempty"`
TTL int `json:"ttl,omitempty"`
Records []string `json:"records,omitempty"`
}
// ToRecordSetUpdateMap formats an UpdateOpts structure into a request body.
func (opts UpdateOpts) ToRecordSetUpdateMap() (map[string]interface{}, error) {
b, err := gophercloud.BuildRequestBody(opts, "")
if err != nil {
return nil, err
}
if opts.TTL > 0 {
b["ttl"] = opts.TTL
} else {
b["ttl"] = nil
}
return b, nil
}
// Update updates a recordset in a given zone
func Update(client *gophercloud.ServiceClient, zoneID string, rrsetID string, opts UpdateOptsBuilder) (r UpdateResult) {
b, err := opts.ToRecordSetUpdateMap()
if err != nil {
r.Err = err
return
}
_, r.Err = client.Put(rrsetURL(client, zoneID, rrsetID), &b, &r.Body, &gophercloud.RequestOpts{
OkCodes: []int{200, 202},
})
return
}
// Delete removes an existing RecordSet.
func Delete(client *gophercloud.ServiceClient, zoneID string, rrsetID string) (r DeleteResult) {
_, r.Err = client.Delete(rrsetURL(client, zoneID, rrsetID), &gophercloud.RequestOpts{
OkCodes: []int{202},
})
return
}

View File

@@ -0,0 +1,141 @@
package recordsets
import (
"encoding/json"
"time"
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/pagination"
)
type commonResult struct {
gophercloud.Result
}
// Extract interprets a GetResult, CreateResult or UpdateResult as a concrete RecordSet.
// An error is returned if the original call or the extraction failed.
func (r commonResult) Extract() (*RecordSet, error) {
var s *RecordSet
err := r.ExtractInto(&s)
return s, err
}
// CreateResult is the deferred result of a Create call.
type CreateResult struct {
commonResult
}
// GetResult is the deferred result of a Get call.
type GetResult struct {
commonResult
}
// RecordSetPage is a single page of RecordSet results.
type RecordSetPage struct {
pagination.LinkedPageBase
}
// UpdateResult is the deferred result of an Update call.
type UpdateResult struct {
commonResult
}
// DeleteResult is the deferred result of an Delete call.
type DeleteResult struct {
gophercloud.ErrResult
}
// IsEmpty returns true if the page contains no results.
func (r RecordSetPage) IsEmpty() (bool, error) {
s, err := ExtractRecordSets(r)
return len(s) == 0, err
}
// ExtractRecordSets extracts a slice of RecordSets from a Collection acquired from List.
func ExtractRecordSets(r pagination.Page) ([]RecordSet, error) {
var s struct {
RecordSets []RecordSet `json:"recordsets"`
}
err := (r.(RecordSetPage)).ExtractInto(&s)
return s.RecordSets, err
}
type RecordSet struct {
// ID is the unique ID of the recordset
ID string `json:"id"`
// ZoneID is the ID of the zone the recordset belongs to.
ZoneID string `json:"zone_id"`
// ProjectID is the ID of the project that owns the recordset.
ProjectID string `json:"project_id"`
// Name is the name of the recordset.
Name string `json:"name"`
// ZoneName is the name of the zone the recordset belongs to.
ZoneName string `json:"zone_name"`
// Type is the RRTYPE of the recordset.
Type string `json:"type"`
// Records are the DNS records of the recordset.
Records []string `json:"records"`
// TTL is the time to live of the recordset.
TTL int `json:"ttl"`
// Status is the status of the recordset.
Status string `json:"status"`
// Action is the current action in progress of the recordset.
Action string `json:"action"`
// Description is the description of the recordset.
Description string `json:"description"`
// Version is the revision of the recordset.
Version int `json:version"`
// CreatedAt is the date when the recordset was created.
CreatedAt time.Time `json:"-"`
// UpdatedAt is the date when the recordset was updated.
UpdatedAt time.Time `json:"-"`
// Links includes HTTP references to the itself,
// useful for passing along to other APIs that might want a recordset reference.
Links []gophercloud.Link `json:"-"`
}
func (r *RecordSet) UnmarshalJSON(b []byte) error {
type tmp RecordSet
var s struct {
tmp
CreatedAt gophercloud.JSONRFC3339MilliNoZ `json:"created_at"`
UpdatedAt gophercloud.JSONRFC3339MilliNoZ `json:"updated_at"`
Links map[string]interface{} `json:"links"`
}
err := json.Unmarshal(b, &s)
if err != nil {
return err
}
*r = RecordSet(s.tmp)
r.CreatedAt = time.Time(s.CreatedAt)
r.UpdatedAt = time.Time(s.UpdatedAt)
if s.Links != nil {
for rel, href := range s.Links {
if v, ok := href.(string); ok {
link := gophercloud.Link{
Rel: rel,
Href: v,
}
r.Links = append(r.Links, link)
}
}
}
return err
}

View File

@@ -0,0 +1,11 @@
package recordsets
import "github.com/gophercloud/gophercloud"
func baseURL(c *gophercloud.ServiceClient, zoneID string) string {
return c.ServiceURL("zones", zoneID, "recordsets")
}
func rrsetURL(c *gophercloud.ServiceClient, zoneID string, rrsetID string) string {
return c.ServiceURL("zones", zoneID, "recordsets", rrsetID)
}