github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/dns/v2/recordsets/results.go (about)

     1  package recordsets
     2  
     3  import (
     4  	"encoding/json"
     5  	"time"
     6  
     7  	"github.com/huaweicloud/golangsdk"
     8  	"github.com/huaweicloud/golangsdk/pagination"
     9  )
    10  
    11  type commonResult struct {
    12  	golangsdk.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  	golangsdk.ErrResult
    50  }
    51  
    52  // IsEmpty returns true if the page contains no results.
    53  func (r RecordSetPage) IsEmpty() (bool, error) {
    54  	s, err := ExtractRecordSets(r)
    55  	return len(s) == 0, err
    56  }
    57  
    58  // ExtractRecordSets extracts a slice of RecordSets from a List result.
    59  func ExtractRecordSets(r pagination.Page) ([]RecordSet, error) {
    60  	var s struct {
    61  		RecordSets []RecordSet `json:"recordsets"`
    62  	}
    63  	err := (r.(RecordSetPage)).ExtractInto(&s)
    64  	return s.RecordSets, err
    65  }
    66  
    67  // RecordSet represents a DNS Record Set.
    68  type RecordSet struct {
    69  	// ID is the unique ID of the recordset
    70  	ID string `json:"id"`
    71  
    72  	// ZoneID is the ID of the zone the recordset belongs to.
    73  	ZoneID string `json:"zone_id"`
    74  
    75  	// ProjectID is the ID of the project that owns the recordset.
    76  	ProjectID string `json:"project_id"`
    77  
    78  	// Name is the name of the recordset.
    79  	Name string `json:"name"`
    80  
    81  	// ZoneName is the name of the zone the recordset belongs to.
    82  	ZoneName string `json:"zone_name"`
    83  
    84  	// Type is the RRTYPE of the recordset.
    85  	Type string `json:"type"`
    86  
    87  	// Records are the DNS records of the recordset.
    88  	Records []string `json:"records"`
    89  
    90  	// TTL is the time to live of the recordset.
    91  	TTL int `json:"ttl"`
    92  
    93  	// Status is the status of the recordset.
    94  	Status string `json:"status"`
    95  
    96  	// Action is the current action in progress of the recordset.
    97  	Action string `json:"action"`
    98  
    99  	// Description is the description of the recordset.
   100  	Description string `json:"description"`
   101  
   102  	// Version is the revision of the recordset.
   103  	Version int `json:"version"`
   104  
   105  	// CreatedAt is the date when the recordset was created.
   106  	CreatedAt time.Time `json:"-"`
   107  
   108  	// UpdatedAt is the date when the recordset was updated.
   109  	UpdatedAt time.Time `json:"-"`
   110  
   111  	// Links includes HTTP references to the itself,
   112  	// useful for passing along to other APIs that might want a recordset
   113  	// reference.
   114  	Links []golangsdk.Link `json:"-"`
   115  }
   116  
   117  func (r *RecordSet) UnmarshalJSON(b []byte) error {
   118  	type tmp RecordSet
   119  	var s struct {
   120  		tmp
   121  		CreatedAt golangsdk.JSONRFC3339MilliNoZ `json:"created_at"`
   122  		UpdatedAt golangsdk.JSONRFC3339MilliNoZ `json:"updated_at"`
   123  		Links     map[string]interface{}        `json:"links"`
   124  	}
   125  	err := json.Unmarshal(b, &s)
   126  	if err != nil {
   127  		return err
   128  	}
   129  	*r = RecordSet(s.tmp)
   130  
   131  	r.CreatedAt = time.Time(s.CreatedAt)
   132  	r.UpdatedAt = time.Time(s.UpdatedAt)
   133  
   134  	if s.Links != nil {
   135  		for rel, href := range s.Links {
   136  			if v, ok := href.(string); ok {
   137  				link := golangsdk.Link{
   138  					Rel:  rel,
   139  					Href: v,
   140  				}
   141  				r.Links = append(r.Links, link)
   142  			}
   143  		}
   144  	}
   145  
   146  	return err
   147  }