github.com/chnsz/golangsdk@v0.0.0-20240506093406-85a3fbfa605b/openstack/dns/v2/zones/requests.go (about)

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