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

     1  package zones
     2  
     3  import (
     4  	"github.com/gophercloud/gophercloud"
     5  	"github.com/gophercloud/gophercloud/pagination"
     6  )
     7  
     8  // ListOptsBuilder allows extensions to add parameters to the List request.
     9  type ListOptsBuilder interface {
    10  	ToZoneListQuery() (string, error)
    11  }
    12  
    13  // ListOpts allows the filtering and sorting of paginated collections through
    14  // the API. Filtering is achieved by passing in struct field values that map to
    15  // the server attributes you want to see returned. Marker and Limit are used
    16  // for pagination.
    17  // https://developer.openstack.org/api-ref/dns/
    18  type ListOpts struct {
    19  	// Integer value for the limit of values to return.
    20  	Limit int `q:"limit"`
    21  
    22  	// UUID of the zone at which you want to set a marker.
    23  	Marker string `q:"marker"`
    24  
    25  	Description string `q:"description"`
    26  	Email       string `q:"email"`
    27  	Name        string `q:"name"`
    28  	SortDir     string `q:"sort_dir"`
    29  	SortKey     string `q:"sort_key"`
    30  	Status      string `q:"status"`
    31  	TTL         int    `q:"ttl"`
    32  	Type        string `q:"type"`
    33  }
    34  
    35  // ToZoneListQuery formats a ListOpts into a query string.
    36  func (opts ListOpts) ToZoneListQuery() (string, error) {
    37  	q, err := gophercloud.BuildQueryString(opts)
    38  	return q.String(), err
    39  }
    40  
    41  // List implements a zone List request.
    42  func List(client *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager {
    43  	url := baseURL(client)
    44  	if opts != nil {
    45  		query, err := opts.ToZoneListQuery()
    46  		if err != nil {
    47  			return pagination.Pager{Err: err}
    48  		}
    49  		url += query
    50  	}
    51  	return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
    52  		return ZonePage{pagination.LinkedPageBase{PageResult: r}}
    53  	})
    54  }
    55  
    56  // Get returns information about a zone, given its ID.
    57  func Get(client *gophercloud.ServiceClient, zoneID string) (r GetResult) {
    58  	resp, err := client.Get(zoneURL(client, zoneID), &r.Body, nil)
    59  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    60  	return
    61  }
    62  
    63  // CreateOptsBuilder allows extensions to add additional attributes to the
    64  // Create request.
    65  type CreateOptsBuilder interface {
    66  	ToZoneCreateMap() (map[string]interface{}, error)
    67  }
    68  
    69  // CreateOpts specifies the attributes used to create a zone.
    70  type CreateOpts struct {
    71  	// Attributes are settings that supply hints and filters for the zone.
    72  	Attributes map[string]string `json:"attributes,omitempty"`
    73  
    74  	// Email contact of the zone.
    75  	Email string `json:"email,omitempty"`
    76  
    77  	// Description of the zone.
    78  	Description string `json:"description,omitempty"`
    79  
    80  	// Name of the zone.
    81  	Name string `json:"name" required:"true"`
    82  
    83  	// Masters specifies zone masters if this is a secondary zone.
    84  	Masters []string `json:"masters,omitempty"`
    85  
    86  	// TTL is the time to live of the zone.
    87  	TTL int `json:"-"`
    88  
    89  	// Type specifies if this is a primary or secondary zone.
    90  	Type string `json:"type,omitempty"`
    91  }
    92  
    93  // ToZoneCreateMap formats an CreateOpts structure into a request body.
    94  func (opts CreateOpts) ToZoneCreateMap() (map[string]interface{}, error) {
    95  	b, err := gophercloud.BuildRequestBody(opts, "")
    96  	if err != nil {
    97  		return nil, err
    98  	}
    99  
   100  	if opts.TTL > 0 {
   101  		b["ttl"] = opts.TTL
   102  	}
   103  
   104  	return b, nil
   105  }
   106  
   107  // Create implements a zone create request.
   108  func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
   109  	b, err := opts.ToZoneCreateMap()
   110  	if err != nil {
   111  		r.Err = err
   112  		return
   113  	}
   114  	resp, err := client.Post(baseURL(client), &b, &r.Body, &gophercloud.RequestOpts{
   115  		OkCodes: []int{201, 202},
   116  	})
   117  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   118  	return
   119  }
   120  
   121  // UpdateOptsBuilder allows extensions to add additional attributes to the
   122  // Update request.
   123  type UpdateOptsBuilder interface {
   124  	ToZoneUpdateMap() (map[string]interface{}, error)
   125  }
   126  
   127  // UpdateOpts specifies the attributes to update a zone.
   128  type UpdateOpts struct {
   129  	// Email contact of the zone.
   130  	Email string `json:"email,omitempty"`
   131  
   132  	// TTL is the time to live of the zone.
   133  	TTL int `json:"-"`
   134  
   135  	// Masters specifies zone masters if this is a secondary zone.
   136  	Masters []string `json:"masters,omitempty"`
   137  
   138  	// Description of the zone.
   139  	Description *string `json:"description,omitempty"`
   140  }
   141  
   142  // ToZoneUpdateMap formats an UpdateOpts structure into a request body.
   143  func (opts UpdateOpts) ToZoneUpdateMap() (map[string]interface{}, error) {
   144  	b, err := gophercloud.BuildRequestBody(opts, "")
   145  	if err != nil {
   146  		return nil, err
   147  	}
   148  
   149  	if opts.TTL > 0 {
   150  		b["ttl"] = opts.TTL
   151  	}
   152  
   153  	return b, nil
   154  }
   155  
   156  // Update implements a zone update request.
   157  func Update(client *gophercloud.ServiceClient, zoneID string, opts UpdateOptsBuilder) (r UpdateResult) {
   158  	b, err := opts.ToZoneUpdateMap()
   159  	if err != nil {
   160  		r.Err = err
   161  		return
   162  	}
   163  	resp, err := client.Patch(zoneURL(client, zoneID), &b, &r.Body, &gophercloud.RequestOpts{
   164  		OkCodes: []int{200, 202},
   165  	})
   166  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   167  	return
   168  }
   169  
   170  // Delete implements a zone delete request.
   171  func Delete(client *gophercloud.ServiceClient, zoneID string) (r DeleteResult) {
   172  	resp, err := client.Delete(zoneURL(client, zoneID), &gophercloud.RequestOpts{
   173  		OkCodes:      []int{202},
   174  		JSONResponse: &r.Body,
   175  	})
   176  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   177  	return
   178  }