diff --git a/openstack/dns/v2/ptrrecords/requests.go b/openstack/dns/v2/ptrrecords/requests.go new file mode 100644 index 000000000..41e8f773e --- /dev/null +++ b/openstack/dns/v2/ptrrecords/requests.go @@ -0,0 +1,78 @@ +package ptrrecords + +import ( + "github.com/huaweicloud/golangsdk" +) + +// Get returns information about a ptr, given its ID. +func Get(client *golangsdk.ServiceClient, id string) (r GetResult) { + _, r.Err = client.Get(resourceURL(client, id), &r.Body, nil) + return +} + +// CreateOptsBuilder allows extensions to add additional attributes to the +// Create request. +type CreateOptsBuilder interface { + ToPtrCreateMap() (map[string]interface{}, error) +} + +// CreateOpts specifies the attributes used to create a ptr. +type CreateOpts struct { + // Name of the ptr. + PtrName string `json:"ptrdname" required:"true"` + + // Description of the ptr. + Description string `json:"description,omitempty"` + + // TTL is the time to live of the ptr. + TTL int `json:"-"` + + // Tags of the ptr. + Tags []Tag `json:"tags,omitempty"` +} + +// Tag is a structure of key value pair. +type Tag struct { + //tag key + Key string `json:"key" required:"true"` + //tag value + Value string `json:"value" required:"true"` +} + +// ToPtrCreateMap formats an CreateOpts structure into a request body. +func (opts CreateOpts) ToPtrCreateMap() (map[string]interface{}, error) { + b, err := golangsdk.BuildRequestBody(opts, "") + if err != nil { + return nil, err + } + + if opts.TTL > 0 { + b["ttl"] = opts.TTL + } + + return b, nil +} + +// Create implements a ptr create/update request. +func Create(client *golangsdk.ServiceClient, region string, fip_id string, opts CreateOptsBuilder) (r CreateResult) { + b, err := opts.ToPtrCreateMap() + if err != nil { + r.Err = err + return + } + _, r.Err = client.Patch(baseURL(client, region, fip_id), &b, &r.Body, &golangsdk.RequestOpts{ + OkCodes: []int{200, 202}, + }) + return +} + +// Delete implements a ptr delete request. +func Delete(client *golangsdk.ServiceClient, id string) (r DeleteResult) { + b := map[string]string{ + "ptrname": "null", + } + _, r.Err = client.Patch(resourceURL(client, id), &b, nil, &golangsdk.RequestOpts{ + OkCodes: []int{200, 202}, + }) + return +} diff --git a/openstack/dns/v2/ptrrecords/results.go b/openstack/dns/v2/ptrrecords/results.go new file mode 100644 index 000000000..1cdc805c3 --- /dev/null +++ b/openstack/dns/v2/ptrrecords/results.go @@ -0,0 +1,56 @@ +package ptrrecords + +import ( + "github.com/huaweicloud/golangsdk" +) + +type commonResult struct { + golangsdk.Result +} + +// Extract interprets a GetResult, CreateResult as a Ptr. +// An error is returned if the original call or the extraction failed. +func (r commonResult) Extract() (*Ptr, error) { + var s *Ptr + err := r.ExtractInto(&s) + return s, err +} + +// CreateResult is the result of a Create request. Call its Extract method +// to interpret the result as a Ptr. +type CreateResult struct { + commonResult +} + +// GetResult is the result of a Get request. Call its Extract method +// to interpret the result as a Ptr. +type GetResult struct { + commonResult +} + +// DeleteResult is the result of a Delete request. Call its ExtractErr method +// to determine if the request succeeded or failed. +type DeleteResult struct { + golangsdk.ErrResult +} + +// Ptr represents a ptr record. +type Ptr struct { + // ID uniquely identifies this ptr amongst all other ptr records. + ID string `json:"id"` + + // Name for this ptr. + PtrName string `json:"ptrdname"` + + // Description for this ptr. + Description string `json:"description"` + + // TTL is the Time to Live for the ptr. + TTL int `json:"ttl"` + + // Address of the floating ip. + Address string `json:"address"` + + // Status of the PTR. + Status string `json:"status"` +} diff --git a/openstack/dns/v2/ptrrecords/urls.go b/openstack/dns/v2/ptrrecords/urls.go new file mode 100644 index 000000000..c0b813525 --- /dev/null +++ b/openstack/dns/v2/ptrrecords/urls.go @@ -0,0 +1,11 @@ +package ptrrecords + +import "github.com/huaweicloud/golangsdk" + +func baseURL(c *golangsdk.ServiceClient, region string, floatingip_id string) string { + return c.ServiceURL("reverse/floatingips", region+":"+floatingip_id) +} + +func resourceURL(c *golangsdk.ServiceClient, id string) string { + return c.ServiceURL("reverse/floatingips", id) +}