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

     1  package zones
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/opentelekomcloud/gophertelekomcloud"
     8  	"github.com/opentelekomcloud/gophertelekomcloud/openstack/common/tags"
     9  	"github.com/opentelekomcloud/gophertelekomcloud/pagination"
    10  )
    11  
    12  // ListOptsBuilder allows extensions to add parameters to the List request.
    13  type ListOptsBuilder interface {
    14  	ToZoneListQuery() (string, error)
    15  }
    16  
    17  // ListOpts allows the filtering and sorting of paginated collections through
    18  // the API. Filtering is achieved by passing in struct field values that map to
    19  // the server attributes you want to see returned. Marker and Limit are used
    20  // for pagination.
    21  // https://developer.openstack.org/api-ref/dns/
    22  type ListOpts struct {
    23  	// Integer value for the limit of values to return.
    24  	Limit int `q:"limit"`
    25  
    26  	// UUID of the zone at which you want to set a marker.
    27  	Marker string `q:"marker"`
    28  
    29  	Description string `q:"description"`
    30  	Email       string `q:"email"`
    31  	Name        string `q:"name"`
    32  	SortDir     string `q:"sort_dir"`
    33  	SortKey     string `q:"sort_key"`
    34  	Status      string `q:"status"`
    35  	TTL         int    `q:"ttl"`
    36  	Type        string `q:"type"`
    37  	QueryTags   string `q:"tags"`
    38  
    39  	Tags []tags.ResourceTag
    40  }
    41  
    42  // ToZoneListQuery formats a ListOpts into a query string.
    43  func (opts ListOpts) ToZoneListQuery() (string, error) {
    44  	var tagList []string
    45  	for _, tag := range opts.Tags {
    46  		tagList = append(tagList, fmt.Sprintf("%s,%s", tag.Key, tag.Value))
    47  	}
    48  	opts.QueryTags = strings.Join(tagList, "|")
    49  
    50  	q, err := golangsdk.BuildQueryString(opts)
    51  	if err != nil {
    52  		return "", err
    53  	}
    54  	return q.String(), err
    55  }
    56  
    57  // List implements a zone List request.
    58  func List(client *golangsdk.ServiceClient, opts ListOptsBuilder) pagination.Pager {
    59  	url := baseURL(client)
    60  	if opts != nil {
    61  		query, err := opts.ToZoneListQuery()
    62  		if err != nil {
    63  			return pagination.Pager{Err: err}
    64  		}
    65  		url += query
    66  	}
    67  	return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
    68  		return ZonePage{pagination.LinkedPageBase{PageResult: r}}
    69  	})
    70  }
    71  
    72  // Get returns information about a zone, given its ID.
    73  func Get(client *golangsdk.ServiceClient, zoneID string) (r GetResult) {
    74  	_, r.Err = client.Get(zoneURL(client, zoneID), &r.Body, nil)
    75  	return
    76  }
    77  
    78  // CreateOptsBuilder allows extensions to add additional attributes to the
    79  // Create request.
    80  type CreateOptsBuilder interface {
    81  	ToZoneCreateMap() (map[string]interface{}, error)
    82  }
    83  
    84  // CreateOpts specifies the attributes used to create a zone.
    85  type CreateOpts struct {
    86  	// Email contact of the zone.
    87  	Email string `json:"email,omitempty"`
    88  
    89  	// Description of the zone.
    90  	Description string `json:"description,omitempty"`
    91  
    92  	// Name of the zone.
    93  	Name string `json:"name" required:"true"`
    94  
    95  	// TTL is the time to live of the zone.
    96  	TTL int `json:"ttl,omitempty"`
    97  
    98  	ZoneType string `json:"zone_type,omitempty"`
    99  
   100  	Router *RouterOpts `json:"router,omitempty"`
   101  
   102  	Tags []tags.ResourceTag `json:"tags,omitempty"`
   103  }
   104  
   105  // ToZoneCreateMap formats an CreateOpts structure into a request body.
   106  func (opts CreateOpts) ToZoneCreateMap() (map[string]interface{}, error) {
   107  	b, err := golangsdk.BuildRequestBody(opts, "")
   108  	if err != nil {
   109  		return nil, err
   110  	}
   111  	return b, nil
   112  }
   113  
   114  // Create implements a zone create request.
   115  func Create(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
   116  	b, err := opts.ToZoneCreateMap()
   117  	if err != nil {
   118  		r.Err = err
   119  		return
   120  	}
   121  	_, r.Err = client.Post(baseURL(client), &b, &r.Body, &golangsdk.RequestOpts{
   122  		OkCodes: []int{201, 202},
   123  	})
   124  	return
   125  }
   126  
   127  // UpdateOptsBuilder allows extensions to add additional attributes to the
   128  // Update request.
   129  type UpdateOptsBuilder interface {
   130  	ToZoneUpdateMap() (map[string]interface{}, error)
   131  }
   132  
   133  // UpdateOpts specifies the attributes to update a zone.
   134  type UpdateOpts struct {
   135  	// Email contact of the zone.
   136  	Email string `json:"email,omitempty"`
   137  
   138  	// TTL is the time to live of the zone.
   139  	TTL int `json:"ttl,omitempty"`
   140  
   141  	// Description of the zone.
   142  	Description string `json:"description,omitempty"`
   143  }
   144  
   145  // ToZoneUpdateMap formats an UpdateOpts structure into a request body.
   146  func (opts UpdateOpts) ToZoneUpdateMap() (map[string]interface{}, error) {
   147  	b, err := golangsdk.BuildRequestBody(opts, "")
   148  	if err != nil {
   149  		return nil, err
   150  	}
   151  	return b, nil
   152  }
   153  
   154  // Update implements a zone update request.
   155  func Update(client *golangsdk.ServiceClient, zoneID string, opts UpdateOptsBuilder) (r UpdateResult) {
   156  	b, err := opts.ToZoneUpdateMap()
   157  	if err != nil {
   158  		r.Err = err
   159  		return
   160  	}
   161  	_, r.Err = client.Patch(zoneURL(client, zoneID), &b, &r.Body, &golangsdk.RequestOpts{
   162  		OkCodes: []int{200, 202},
   163  	})
   164  	return
   165  }
   166  
   167  // Delete implements a zone delete request.
   168  func Delete(client *golangsdk.ServiceClient, zoneID string) (r DeleteResult) {
   169  	_, r.Err = client.Delete(zoneURL(client, zoneID), &golangsdk.RequestOpts{
   170  		OkCodes:      []int{202},
   171  		JSONResponse: &r.Body,
   172  	})
   173  	return
   174  }
   175  
   176  // RouterOptsBuilder allows adding parameters to the associate/disassociate Zone request.
   177  type RouterOptsBuilder interface {
   178  	ToRouterMap() (map[string]interface{}, error)
   179  }
   180  
   181  // RouterOpts specifies the required information to associate/disassociate a Router with a Zone.
   182  type RouterOpts struct {
   183  	// Router ID
   184  	RouterID string `json:"router_id" required:"true"`
   185  
   186  	// Router Region
   187  	RouterRegion string `json:"router_region,omitempty"`
   188  }
   189  
   190  // ToRouterMap constructs a request body from RouterOpts.
   191  func (opts RouterOpts) ToRouterMap() (map[string]interface{}, error) {
   192  	return golangsdk.BuildRequestBody(opts, "router")
   193  }
   194  
   195  // AssociateZone associate a Router with a Zone.
   196  func AssociateZone(client *golangsdk.ServiceClient, zoneID string, opts RouterOptsBuilder) (r AssociateResult) {
   197  	b, err := opts.ToRouterMap()
   198  	if err != nil {
   199  		r.Err = err
   200  		return
   201  	}
   202  	_, r.Err = client.Post(associateURL(client, zoneID), b, nil, nil)
   203  	return
   204  }
   205  
   206  // DisassociateZone disassociate a Router with a Zone.
   207  func DisassociateZone(client *golangsdk.ServiceClient, zoneID string, opts RouterOptsBuilder) (r DisassociateResult) {
   208  	b, err := opts.ToRouterMap()
   209  	if err != nil {
   210  		r.Err = err
   211  		return
   212  	}
   213  	_, r.Err = client.Post(disassociateURL(client, zoneID), b, nil, nil)
   214  	return
   215  }