github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/dns/v2/zones/results.go (about)

     1  package zones
     2  
     3  import (
     4  	"encoding/json"
     5  	"strconv"
     6  	"time"
     7  
     8  	"github.com/vnpaycloud-console/gophercloud/v2"
     9  	"github.com/vnpaycloud-console/gophercloud/v2/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  // ErrResult represents a generic error result.
    54  type ErrResult struct {
    55  	gophercloud.ErrResult
    56  }
    57  
    58  // IsEmpty returns true if the page contains no results.
    59  func (r ZonePage) IsEmpty() (bool, error) {
    60  	if r.StatusCode == 204 {
    61  		return true, nil
    62  	}
    63  
    64  	s, err := ExtractZones(r)
    65  	return len(s) == 0, err
    66  }
    67  
    68  // ExtractZones extracts a slice of Zones from a List result.
    69  func ExtractZones(r pagination.Page) ([]Zone, error) {
    70  	var s struct {
    71  		Zones []Zone `json:"zones"`
    72  	}
    73  	err := (r.(ZonePage)).ExtractInto(&s)
    74  	return s.Zones, err
    75  }
    76  
    77  // Zone represents a DNS zone.
    78  type Zone struct {
    79  	// ID uniquely identifies this zone amongst all other zones, including those
    80  	// not accessible to the current tenant.
    81  	ID string `json:"id"`
    82  
    83  	// PoolID is the ID for the pool hosting this zone.
    84  	PoolID string `json:"pool_id"`
    85  
    86  	// ProjectID identifies the project/tenant owning this resource.
    87  	ProjectID string `json:"project_id"`
    88  
    89  	// Name is the DNS Name for the zone.
    90  	Name string `json:"name"`
    91  
    92  	// Email for the zone. Used in SOA records for the zone.
    93  	Email string `json:"email"`
    94  
    95  	// Description for this zone.
    96  	Description string `json:"description"`
    97  
    98  	// TTL is the Time to Live for the zone.
    99  	TTL int `json:"ttl"`
   100  
   101  	// Serial is the current serial number for the zone.
   102  	Serial int `json:"-"`
   103  
   104  	// Status is the status of the resource.
   105  	Status string `json:"status"`
   106  
   107  	// Action is the current action in progress on the resource.
   108  	Action string `json:"action"`
   109  
   110  	// Version of the resource.
   111  	Version int `json:"version"`
   112  
   113  	// Attributes for the zone.
   114  	Attributes map[string]string `json:"attributes"`
   115  
   116  	// Type of zone. Primary is controlled by Designate.
   117  	// Secondary zones are slaved from another DNS Server.
   118  	// Defaults to Primary.
   119  	Type string `json:"type"`
   120  
   121  	// Masters is the servers for slave servers to get DNS information from.
   122  	Masters []string `json:"masters"`
   123  
   124  	// CreatedAt is the date when the zone was created.
   125  	CreatedAt time.Time `json:"-"`
   126  
   127  	// UpdatedAt is the date when the last change was made to the zone.
   128  	UpdatedAt time.Time `json:"-"`
   129  
   130  	// TransferredAt is the last time an update was retrieved from the
   131  	// master servers.
   132  	TransferredAt time.Time `json:"-"`
   133  
   134  	// Links includes HTTP references to the itself, useful for passing along
   135  	// to other APIs that might want a server reference.
   136  	Links map[string]any `json:"links"`
   137  }
   138  
   139  func (r *Zone) UnmarshalJSON(b []byte) error {
   140  	type tmp Zone
   141  	var s struct {
   142  		tmp
   143  		CreatedAt     gophercloud.JSONRFC3339MilliNoZ `json:"created_at"`
   144  		UpdatedAt     gophercloud.JSONRFC3339MilliNoZ `json:"updated_at"`
   145  		TransferredAt gophercloud.JSONRFC3339MilliNoZ `json:"transferred_at"`
   146  		Serial        any                             `json:"serial"`
   147  	}
   148  	err := json.Unmarshal(b, &s)
   149  	if err != nil {
   150  		return err
   151  	}
   152  	*r = Zone(s.tmp)
   153  
   154  	r.CreatedAt = time.Time(s.CreatedAt)
   155  	r.UpdatedAt = time.Time(s.UpdatedAt)
   156  	r.TransferredAt = time.Time(s.TransferredAt)
   157  
   158  	switch t := s.Serial.(type) {
   159  	case float64:
   160  		r.Serial = int(t)
   161  	case string:
   162  		switch t {
   163  		case "":
   164  			r.Serial = 0
   165  		default:
   166  			serial, err := strconv.ParseFloat(t, 64)
   167  			if err != nil {
   168  				return err
   169  			}
   170  			r.Serial = int(serial)
   171  		}
   172  	}
   173  
   174  	return err
   175  }