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

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