github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/openstack/dns/v2/recordsets/requests.go (about)

     1  package recordsets
     2  
     3  import (
     4  	"github.com/opentelekomcloud/gophertelekomcloud"
     5  	"github.com/opentelekomcloud/gophertelekomcloud/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  	if err != nil {
    41  		return "", err
    42  	}
    43  	return q.String(), err
    44  }
    45  
    46  // ListByZone implements the recordset list request.
    47  func ListByZone(client *golangsdk.ServiceClient, zoneID string, opts ListOptsBuilder) pagination.Pager {
    48  	url := baseURL(client, zoneID)
    49  	if opts != nil {
    50  		query, err := opts.ToRecordSetListQuery()
    51  		if err != nil {
    52  			return pagination.Pager{Err: err}
    53  		}
    54  		url += query
    55  	}
    56  	return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
    57  		return RecordSetPage{pagination.LinkedPageBase{PageResult: r}}
    58  	})
    59  }
    60  
    61  // Get implements the recordset Get request.
    62  func Get(client *golangsdk.ServiceClient, zoneID string, rrsetID string) (r GetResult) {
    63  	_, r.Err = client.Get(rrsetURL(client, zoneID, rrsetID), &r.Body, nil)
    64  	return
    65  }
    66  
    67  // CreateOptsBuilder allows extensions to add additional attributes to the
    68  // Create request.
    69  type CreateOptsBuilder interface {
    70  	ToRecordSetCreateMap() (map[string]interface{}, error)
    71  }
    72  
    73  // CreateOpts specifies the base attributes that may be used to create a
    74  // RecordSet.
    75  type CreateOpts struct {
    76  	// Name is the name of the RecordSet.
    77  	Name string `json:"name" required:"true"`
    78  
    79  	// Description is a description of the RecordSet.
    80  	Description string `json:"description,omitempty"`
    81  
    82  	// Records are the DNS records of the RecordSet.
    83  	Records []string `json:"records,omitempty"`
    84  
    85  	// TTL is the time to live of the RecordSet.
    86  	TTL int `json:"ttl,omitempty"`
    87  
    88  	// Type is the RRTYPE of the RecordSet.
    89  	Type string `json:"type,omitempty"`
    90  }
    91  
    92  // ToRecordSetCreateMap formats an CreateOpts structure into a request body.
    93  func (opts CreateOpts) ToRecordSetCreateMap() (map[string]interface{}, error) {
    94  	b, err := golangsdk.BuildRequestBody(opts, "")
    95  	if err != nil {
    96  		return nil, err
    97  	}
    98  
    99  	return b, nil
   100  }
   101  
   102  // Create creates a recordset in a given zone.
   103  func Create(client *golangsdk.ServiceClient, zoneID string, opts CreateOptsBuilder) (r CreateResult) {
   104  	b, err := opts.ToRecordSetCreateMap()
   105  	if err != nil {
   106  		r.Err = err
   107  		return
   108  	}
   109  	_, r.Err = client.Post(baseURL(client, zoneID), &b, &r.Body, &golangsdk.RequestOpts{
   110  		OkCodes: []int{201, 202},
   111  	})
   112  	return
   113  }
   114  
   115  // UpdateOptsBuilder allows extensions to add additional attributes to the
   116  // Update request.
   117  type UpdateOptsBuilder interface {
   118  	ToRecordSetUpdateMap() (map[string]interface{}, error)
   119  }
   120  
   121  // UpdateOpts specifies the base attributes that may be updated on an existing
   122  // RecordSet.
   123  type UpdateOpts struct {
   124  	// Description is a description of the RecordSet.
   125  	Description string `json:"description,omitempty"`
   126  
   127  	// TTL is the time to live of the RecordSet.
   128  	TTL int `json:"ttl,omitempty"`
   129  
   130  	// Records are the DNS records of the RecordSet.
   131  	Records []string `json:"records,omitempty"`
   132  }
   133  
   134  // ToRecordSetUpdateMap formats an UpdateOpts structure into a request body.
   135  func (opts UpdateOpts) ToRecordSetUpdateMap() (map[string]interface{}, error) {
   136  	b, err := golangsdk.BuildRequestBody(opts, "")
   137  	if err != nil {
   138  		return nil, err
   139  	}
   140  
   141  	if opts.TTL > 0 {
   142  		b["ttl"] = opts.TTL
   143  	}
   144  
   145  	return b, nil
   146  }
   147  
   148  // Update updates a recordset in a given zone
   149  func Update(client *golangsdk.ServiceClient, zoneID string, rrsetID string, opts UpdateOptsBuilder) (r UpdateResult) {
   150  	b, err := opts.ToRecordSetUpdateMap()
   151  	if err != nil {
   152  		r.Err = err
   153  		return
   154  	}
   155  	_, r.Err = client.Put(rrsetURL(client, zoneID, rrsetID), &b, &r.Body, &golangsdk.RequestOpts{
   156  		OkCodes: []int{200, 202},
   157  	})
   158  	return
   159  }
   160  
   161  // Delete removes an existing RecordSet.
   162  func Delete(client *golangsdk.ServiceClient, zoneID string, rrsetID string) (r DeleteResult) {
   163  	_, r.Err = client.Delete(rrsetURL(client, zoneID, rrsetID), &golangsdk.RequestOpts{
   164  		OkCodes: []int{202},
   165  	})
   166  	return
   167  }