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

     1  package zones
     2  
     3  import (
     4  	"github.com/huaweicloud/golangsdk"
     5  	"github.com/huaweicloud/golangsdk/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 := golangsdk.BuildQueryString(opts)
    38  	return q.String(), err
    39  }
    40  
    41  // List implements a zone List request.
    42  func List(client *golangsdk.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 *golangsdk.ServiceClient, zoneID string) (r GetResult) {
    58  	_, r.Err = client.Get(zoneURL(client, zoneID), &r.Body, nil)
    59  	return
    60  }
    61  
    62  // CreateOptsBuilder allows extensions to add additional attributes to the
    63  // Create request.
    64  type CreateOptsBuilder interface {
    65  	ToZoneCreateMap() (map[string]interface{}, error)
    66  }
    67  
    68  // CreateOpts specifies the attributes used to create a zone.
    69  type CreateOpts struct {
    70  	// Attributes are settings that supply hints and filters for the zone.
    71  	Attributes map[string]string `json:"attributes,omitempty"`
    72  
    73  	// Email contact of the zone.
    74  	Email string `json:"email,omitempty"`
    75  
    76  	// Description of the zone.
    77  	Description string `json:"description,omitempty"`
    78  
    79  	// Name of the zone.
    80  	Name string `json:"name" required:"true"`
    81  
    82  	// Masters specifies zone masters if this is a secondary zone.
    83  	Masters []string `json:"masters,omitempty"`
    84  
    85  	// TTL is the time to live of the zone.
    86  	TTL int `json:"-"`
    87  
    88  	// Type specifies if this is a primary or secondary zone.
    89  	Type string `json:"type,omitempty"`
    90  
    91  	// Enterprise project id
    92  	EnterpriseProjectID string `json:"enterprise_project_id,omitempty"`
    93  }
    94  
    95  // ToZoneCreateMap formats an CreateOpts structure into a request body.
    96  func (opts CreateOpts) ToZoneCreateMap() (map[string]interface{}, error) {
    97  	b, err := golangsdk.BuildRequestBody(opts, "")
    98  	if err != nil {
    99  		return nil, err
   100  	}
   101  
   102  	if opts.TTL > 0 {
   103  		b["ttl"] = opts.TTL
   104  	}
   105  
   106  	return b, nil
   107  }
   108  
   109  // Create implements a zone create request.
   110  func Create(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
   111  	b, err := opts.ToZoneCreateMap()
   112  	if err != nil {
   113  		r.Err = err
   114  		return
   115  	}
   116  	_, r.Err = client.Post(baseURL(client), &b, &r.Body, &golangsdk.RequestOpts{
   117  		OkCodes: []int{201, 202},
   118  	})
   119  	return
   120  }
   121  
   122  // UpdateOptsBuilder allows extensions to add additional attributes to the
   123  // Update request.
   124  type UpdateOptsBuilder interface {
   125  	ToZoneUpdateMap() (map[string]interface{}, error)
   126  }
   127  
   128  // UpdateOpts specifies the attributes to update a zone.
   129  type UpdateOpts struct {
   130  	// Email contact of the zone.
   131  	Email string `json:"email,omitempty"`
   132  
   133  	// TTL is the time to live of the zone.
   134  	TTL int `json:"-"`
   135  
   136  	// Masters specifies zone masters if this is a secondary zone.
   137  	Masters []string `json:"masters,omitempty"`
   138  
   139  	// Description of the zone.
   140  	Description string `json:"description,omitempty"`
   141  }
   142  
   143  // ToZoneUpdateMap formats an UpdateOpts structure into a request body.
   144  func (opts UpdateOpts) ToZoneUpdateMap() (map[string]interface{}, error) {
   145  	b, err := golangsdk.BuildRequestBody(opts, "")
   146  	if err != nil {
   147  		return nil, err
   148  	}
   149  
   150  	if opts.TTL > 0 {
   151  		b["ttl"] = opts.TTL
   152  	}
   153  
   154  	return b, nil
   155  }
   156  
   157  // Update implements a zone update request.
   158  func Update(client *golangsdk.ServiceClient, zoneID string, opts UpdateOptsBuilder) (r UpdateResult) {
   159  	b, err := opts.ToZoneUpdateMap()
   160  	if err != nil {
   161  		r.Err = err
   162  		return
   163  	}
   164  	_, r.Err = client.Patch(zoneURL(client, zoneID), &b, &r.Body, &golangsdk.RequestOpts{
   165  		OkCodes: []int{200, 202},
   166  	})
   167  	return
   168  }
   169  
   170  // Delete implements a zone delete request.
   171  func Delete(client *golangsdk.ServiceClient, zoneID string) (r DeleteResult) {
   172  	_, r.Err = client.Delete(zoneURL(client, zoneID), &golangsdk.RequestOpts{
   173  		OkCodes:      []int{202},
   174  		JSONResponse: &r.Body,
   175  	})
   176  	return
   177  }
   178  
   179  // RouterOptsBuilder allows to add parameters to the associate/disassociate Zone request.
   180  type RouterOptsBuilder interface {
   181  	ToRouterMap() (map[string]interface{}, error)
   182  }
   183  
   184  // RouterOpts specifies the required information to associate/disassociate a Router with a Zone.
   185  type RouterOpts struct {
   186  	// Router ID
   187  	RouterID string `json:"router_id" required:"true"`
   188  
   189  	// Router Region
   190  	RouterRegion string `json:"router_region,omitempty"`
   191  }
   192  
   193  // ToRouterMap constructs a request body from RouterOpts.
   194  func (opts RouterOpts) ToRouterMap() (map[string]interface{}, error) {
   195  	return golangsdk.BuildRequestBody(opts, "router")
   196  }
   197  
   198  // AssociateZone associate a Router with a Zone.
   199  func AssociateZone(client *golangsdk.ServiceClient, zoneID string, opts RouterOptsBuilder) (r AssociateResult) {
   200  	b, err := opts.ToRouterMap()
   201  	if err != nil {
   202  		r.Err = err
   203  		return
   204  	}
   205  	_, r.Err = client.Post(associateURL(client, zoneID), b, nil, nil)
   206  	return
   207  }
   208  
   209  // DisassociateZone disassociate a Router with a Zone.
   210  func DisassociateZone(client *golangsdk.ServiceClient, zoneID string, opts RouterOptsBuilder) (r DisassociateResult) {
   211  	b, err := opts.ToRouterMap()
   212  	if err != nil {
   213  		r.Err = err
   214  		return
   215  	}
   216  	_, r.Err = client.Post(disassociateURL(client, zoneID), b, nil, nil)
   217  	return
   218  }