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

     1  package zones
     2  
     3  import (
     4  	"encoding/json"
     5  	"strconv"
     6  	"time"
     7  
     8  	"github.com/gophercloud/gophercloud"
     9  	"github.com/gophercloud/gophercloud/pagination"
    10  )
    11  
    12  type commonResult struct {
    13  	gophercloud.Result
    14  }
    15  
    16  // Extract interprets a GetResult, CreateResult or UpdateResult as a Zone.
    17  // An error is returned if the original call or the extraction failed.
    18  func (r commonResult) Extract() (*Zone, error) {
    19  	var s *Zone
    20  	err := r.ExtractInto(&s)
    21  	return s, err
    22  }
    23  
    24  // CreateResult is the result of a Create request. Call its Extract method
    25  // to interpret the result as a Zone.
    26  type CreateResult struct {
    27  	commonResult
    28  }
    29  
    30  // GetResult is the result of a Get request. Call its Extract method
    31  // to interpret the result as a Zone.
    32  type GetResult struct {
    33  	commonResult
    34  }
    35  
    36  // UpdateResult is the result of an Update request. Call its Extract method
    37  // to interpret the result as a Zone.
    38  type UpdateResult struct {
    39  	commonResult
    40  }
    41  
    42  // DeleteResult is the result of a Delete request. Call its ExtractErr method
    43  // to determine if the request succeeded or failed.
    44  type DeleteResult struct {
    45  	commonResult
    46  }
    47  
    48  // ZonePage is a single page of Zone results.
    49  type ZonePage struct {
    50  	pagination.LinkedPageBase
    51  }
    52  
    53  // IsEmpty returns true if the page contains no results.
    54  func (r ZonePage) IsEmpty() (bool, error) {
    55  	if r.StatusCode == 204 {
    56  		return true, nil
    57  	}
    58  
    59  	s, err := ExtractZones(r)
    60  	return len(s) == 0, err
    61  }
    62  
    63  // ExtractZones extracts a slice of Zones from a List result.
    64  func ExtractZones(r pagination.Page) ([]Zone, error) {
    65  	var s struct {
    66  		Zones []Zone `json:"zones"`
    67  	}
    68  	err := (r.(ZonePage)).ExtractInto(&s)
    69  	return s.Zones, err
    70  }
    71  
    72  // Zone represents a DNS zone.
    73  type Zone struct {
    74  	// ID uniquely identifies this zone amongst all other zones, including those
    75  	// not accessible to the current tenant.
    76  	ID string `json:"id"`
    77  
    78  	// PoolID is the ID for the pool hosting this zone.
    79  	PoolID string `json:"pool_id"`
    80  
    81  	// ProjectID identifies the project/tenant owning this resource.
    82  	ProjectID string `json:"project_id"`
    83  
    84  	// Name is the DNS Name for the zone.
    85  	Name string `json:"name"`
    86  
    87  	// Email for the zone. Used in SOA records for the zone.
    88  	Email string `json:"email"`
    89  
    90  	// Description for this zone.
    91  	Description string `json:"description"`
    92  
    93  	// TTL is the Time to Live for the zone.
    94  	TTL int `json:"ttl"`
    95  
    96  	// Serial is the current serial number for the zone.
    97  	Serial int `json:"-"`
    98  
    99  	// Status is the status of the resource.
   100  	Status string `json:"status"`
   101  
   102  	// Action is the current action in progress on the resource.
   103  	Action string `json:"action"`
   104  
   105  	// Version of the resource.
   106  	Version int `json:"version"`
   107  
   108  	// Attributes for the zone.
   109  	Attributes map[string]string `json:"attributes"`
   110  
   111  	// Type of zone. Primary is controlled by Designate.
   112  	// Secondary zones are slaved from another DNS Server.
   113  	// Defaults to Primary.
   114  	Type string `json:"type"`
   115  
   116  	// Masters is the servers for slave servers to get DNS information from.
   117  	Masters []string `json:"masters"`
   118  
   119  	// CreatedAt is the date when the zone was created.
   120  	CreatedAt time.Time `json:"-"`
   121  
   122  	// UpdatedAt is the date when the last change was made to the zone.
   123  	UpdatedAt time.Time `json:"-"`
   124  
   125  	// TransferredAt is the last time an update was retrieved from the
   126  	// master servers.
   127  	TransferredAt time.Time `json:"-"`
   128  
   129  	// Links includes HTTP references to the itself, useful for passing along
   130  	// to other APIs that might want a server reference.
   131  	Links map[string]interface{} `json:"links"`
   132  }
   133  
   134  func (r *Zone) UnmarshalJSON(b []byte) error {
   135  	type tmp Zone
   136  	var s struct {
   137  		tmp
   138  		CreatedAt     gophercloud.JSONRFC3339MilliNoZ `json:"created_at"`
   139  		UpdatedAt     gophercloud.JSONRFC3339MilliNoZ `json:"updated_at"`
   140  		TransferredAt gophercloud.JSONRFC3339MilliNoZ `json:"transferred_at"`
   141  		Serial        interface{}                     `json:"serial"`
   142  	}
   143  	err := json.Unmarshal(b, &s)
   144  	if err != nil {
   145  		return err
   146  	}
   147  	*r = Zone(s.tmp)
   148  
   149  	r.CreatedAt = time.Time(s.CreatedAt)
   150  	r.UpdatedAt = time.Time(s.UpdatedAt)
   151  	r.TransferredAt = time.Time(s.TransferredAt)
   152  
   153  	switch t := s.Serial.(type) {
   154  	case float64:
   155  		r.Serial = int(t)
   156  	case string:
   157  		switch t {
   158  		case "":
   159  			r.Serial = 0
   160  		default:
   161  			serial, err := strconv.ParseFloat(t, 64)
   162  			if err != nil {
   163  				return err
   164  			}
   165  			r.Serial = int(serial)
   166  		}
   167  	}
   168  
   169  	return err
   170  }