github.com/gophercloud/gophercloud@v1.11.0/openstack/dns/v2/recordsets/results.go (about)

     1  package recordsets
     2  
     3  import (
     4  	"encoding/json"
     5  	"time"
     6  
     7  	"github.com/gophercloud/gophercloud"
     8  	"github.com/gophercloud/gophercloud/pagination"
     9  )
    10  
    11  type commonResult struct {
    12  	gophercloud.Result
    13  }
    14  
    15  // Extract interprets a GetResult, CreateResult or UpdateResult as a RecordSet.
    16  // An error is returned if the original call or the extraction failed.
    17  func (r commonResult) Extract() (*RecordSet, error) {
    18  	var s *RecordSet
    19  	err := r.ExtractInto(&s)
    20  	return s, err
    21  }
    22  
    23  // CreateResult is the result of a Create operation. Call its Extract method to
    24  // interpret the result as a RecordSet.
    25  type CreateResult struct {
    26  	commonResult
    27  }
    28  
    29  // GetResult is the result of a Get operation. Call its Extract method to
    30  // interpret the result as a RecordSet.
    31  type GetResult struct {
    32  	commonResult
    33  }
    34  
    35  // RecordSetPage is a single page of RecordSet results.
    36  type RecordSetPage struct {
    37  	pagination.LinkedPageBase
    38  }
    39  
    40  // UpdateResult is result of an Update operation. Call its Extract method to
    41  // interpret the result as a RecordSet.
    42  type UpdateResult struct {
    43  	commonResult
    44  }
    45  
    46  // DeleteResult is result of a Delete operation. Call its ExtractErr method to
    47  // determine if the operation succeeded or failed.
    48  type DeleteResult struct {
    49  	gophercloud.ErrResult
    50  }
    51  
    52  // IsEmpty returns true if the page contains no results.
    53  func (r RecordSetPage) IsEmpty() (bool, error) {
    54  	if r.StatusCode == 204 {
    55  		return true, nil
    56  	}
    57  
    58  	s, err := ExtractRecordSets(r)
    59  	return len(s) == 0, err
    60  }
    61  
    62  // ExtractRecordSets extracts a slice of RecordSets from a List result.
    63  func ExtractRecordSets(r pagination.Page) ([]RecordSet, error) {
    64  	var s struct {
    65  		RecordSets []RecordSet `json:"recordsets"`
    66  	}
    67  	err := (r.(RecordSetPage)).ExtractInto(&s)
    68  	return s.RecordSets, err
    69  }
    70  
    71  // RecordSet represents a DNS Record Set.
    72  type RecordSet struct {
    73  	// ID is the unique ID of the recordset
    74  	ID string `json:"id"`
    75  
    76  	// ZoneID is the ID of the zone the recordset belongs to.
    77  	ZoneID string `json:"zone_id"`
    78  
    79  	// ProjectID is the ID of the project that owns the recordset.
    80  	ProjectID string `json:"project_id"`
    81  
    82  	// Name is the name of the recordset.
    83  	Name string `json:"name"`
    84  
    85  	// ZoneName is the name of the zone the recordset belongs to.
    86  	ZoneName string `json:"zone_name"`
    87  
    88  	// Type is the RRTYPE of the recordset.
    89  	Type string `json:"type"`
    90  
    91  	// Records are the DNS records of the recordset.
    92  	Records []string `json:"records"`
    93  
    94  	// TTL is the time to live of the recordset.
    95  	TTL int `json:"ttl"`
    96  
    97  	// Status is the status of the recordset.
    98  	Status string `json:"status"`
    99  
   100  	// Action is the current action in progress of the recordset.
   101  	Action string `json:"action"`
   102  
   103  	// Description is the description of the recordset.
   104  	Description string `json:"description"`
   105  
   106  	// Version is the revision of the recordset.
   107  	Version int `json:"version"`
   108  
   109  	// CreatedAt is the date when the recordset was created.
   110  	CreatedAt time.Time `json:"-"`
   111  
   112  	// UpdatedAt is the date when the recordset was updated.
   113  	UpdatedAt time.Time `json:"-"`
   114  
   115  	// Links includes HTTP references to the itself,
   116  	// useful for passing along to other APIs that might want a recordset
   117  	// reference.
   118  	Links []gophercloud.Link `json:"-"`
   119  
   120  	// Metadata contains the total_count of resources matching the filter
   121  	Metadata struct {
   122  		TotalCount int `json:"total_count"`
   123  	} `json:"metadata"`
   124  }
   125  
   126  func (r *RecordSet) UnmarshalJSON(b []byte) error {
   127  	type tmp RecordSet
   128  	var s struct {
   129  		tmp
   130  		CreatedAt gophercloud.JSONRFC3339MilliNoZ `json:"created_at"`
   131  		UpdatedAt gophercloud.JSONRFC3339MilliNoZ `json:"updated_at"`
   132  		Links     map[string]interface{}          `json:"links"`
   133  	}
   134  	err := json.Unmarshal(b, &s)
   135  	if err != nil {
   136  		return err
   137  	}
   138  	*r = RecordSet(s.tmp)
   139  
   140  	r.CreatedAt = time.Time(s.CreatedAt)
   141  	r.UpdatedAt = time.Time(s.UpdatedAt)
   142  
   143  	if s.Links != nil {
   144  		for rel, href := range s.Links {
   145  			if v, ok := href.(string); ok {
   146  				link := gophercloud.Link{
   147  					Rel:  rel,
   148  					Href: v,
   149  				}
   150  				r.Links = append(r.Links, link)
   151  			}
   152  		}
   153  	}
   154  
   155  	return err
   156  }