github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/identity/v3/regions/requests.go (about)

     1  package regions
     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
     9  // the List request
    10  type ListOptsBuilder interface {
    11  	ToRegionListQuery() (string, error)
    12  }
    13  
    14  // ListOpts provides options to filter the List results.
    15  type ListOpts struct {
    16  	// ParentRegionID filters the response by a parent region ID.
    17  	ParentRegionID string `q:"parent_region_id"`
    18  }
    19  
    20  // ToRegionListQuery formats a ListOpts into a query string.
    21  func (opts ListOpts) ToRegionListQuery() (string, error) {
    22  	q, err := golangsdk.BuildQueryString(opts)
    23  	return q.String(), err
    24  }
    25  
    26  // List enumerates the Regions to which the current token has access.
    27  func List(client *golangsdk.ServiceClient, opts ListOptsBuilder) pagination.Pager {
    28  	url := listURL(client)
    29  	if opts != nil {
    30  		query, err := opts.ToRegionListQuery()
    31  		if err != nil {
    32  			return pagination.Pager{Err: err}
    33  		}
    34  		url += query
    35  	}
    36  	return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
    37  		return RegionPage{pagination.LinkedPageBase{PageResult: r}}
    38  	})
    39  }
    40  
    41  // Get retrieves details on a single region, by ID.
    42  func Get(client *golangsdk.ServiceClient, id string) (r GetResult) {
    43  	_, r.Err = client.Get(getURL(client, id), &r.Body, nil)
    44  	return
    45  }
    46  
    47  // CreateOptsBuilder allows extensions to add additional parameters to
    48  // the Create request.
    49  type CreateOptsBuilder interface {
    50  	ToRegionCreateMap() (map[string]interface{}, error)
    51  }
    52  
    53  // CreateOpts provides options used to create a region.
    54  type CreateOpts struct {
    55  	// ID is the ID of the new region.
    56  	ID string `json:"id,omitempty"`
    57  
    58  	// Description is a description of the region.
    59  	Description string `json:"description,omitempty"`
    60  
    61  	// ParentRegionID is the ID of the parent the region to add this region under.
    62  	ParentRegionID string `json:"parent_region_id,omitempty"`
    63  
    64  	// Extra is free-form extra key/value pairs to describe the region.
    65  	Extra map[string]interface{} `json:"-"`
    66  }
    67  
    68  // ToRegionCreateMap formats a CreateOpts into a create request.
    69  func (opts CreateOpts) ToRegionCreateMap() (map[string]interface{}, error) {
    70  	b, err := golangsdk.BuildRequestBody(opts, "region")
    71  	if err != nil {
    72  		return nil, err
    73  	}
    74  
    75  	if opts.Extra != nil {
    76  		if v, ok := b["region"].(map[string]interface{}); ok {
    77  			for key, value := range opts.Extra {
    78  				v[key] = value
    79  			}
    80  		}
    81  	}
    82  
    83  	return b, nil
    84  }
    85  
    86  // Create creates a new Region.
    87  func Create(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
    88  	b, err := opts.ToRegionCreateMap()
    89  	if err != nil {
    90  		r.Err = err
    91  		return
    92  	}
    93  	_, r.Err = client.Post(createURL(client), &b, &r.Body, &golangsdk.RequestOpts{
    94  		OkCodes: []int{201},
    95  	})
    96  	return
    97  }
    98  
    99  // UpdateOptsBuilder allows extensions to add additional parameters to
   100  // the Update request.
   101  type UpdateOptsBuilder interface {
   102  	ToRegionUpdateMap() (map[string]interface{}, error)
   103  }
   104  
   105  // UpdateOpts provides options for updating a region.
   106  type UpdateOpts struct {
   107  	// Description is a description of the region.
   108  	Description string `json:"description,omitempty"`
   109  
   110  	// ParentRegionID is the ID of the parent region.
   111  	ParentRegionID string `json:"parent_region_id,omitempty"`
   112  
   113  	/*
   114  		// Due to a bug in Keystone, the Extra column of the Region table
   115  		// is not updatable, see: https://bugs.launchpad.net/keystone/+bug/1729933
   116  		// The following lines should be uncommented once the fix is merged.
   117  
   118  		// Extra is free-form extra key/value pairs to describe the region.
   119  		Extra map[string]interface{} `json:"-"`
   120  	*/
   121  }
   122  
   123  // ToRegionUpdateMap formats a UpdateOpts into an update request.
   124  func (opts UpdateOpts) ToRegionUpdateMap() (map[string]interface{}, error) {
   125  	b, err := golangsdk.BuildRequestBody(opts, "region")
   126  	if err != nil {
   127  		return nil, err
   128  	}
   129  
   130  	/*
   131  		// Due to a bug in Keystone, the Extra column of the Region table
   132  		// is not updatable, see: https://bugs.launchpad.net/keystone/+bug/1729933
   133  		// The following lines should be uncommented once the fix is merged.
   134  
   135  		if opts.Extra != nil {
   136  			if v, ok := b["region"].(map[string]interface{}); ok {
   137  				for key, value := range opts.Extra {
   138  					v[key] = value
   139  				}
   140  			}
   141  		}
   142  	*/
   143  
   144  	return b, nil
   145  }
   146  
   147  // Update updates an existing Region.
   148  func Update(client *golangsdk.ServiceClient, regionID string, opts UpdateOptsBuilder) (r UpdateResult) {
   149  	b, err := opts.ToRegionUpdateMap()
   150  	if err != nil {
   151  		r.Err = err
   152  		return
   153  	}
   154  	_, r.Err = client.Patch(updateURL(client, regionID), &b, &r.Body, &golangsdk.RequestOpts{
   155  		OkCodes: []int{200},
   156  	})
   157  	return
   158  }
   159  
   160  // Delete deletes a region.
   161  func Delete(client *golangsdk.ServiceClient, regionID string) (r DeleteResult) {
   162  	_, r.Err = client.Delete(deleteURL(client, regionID), nil)
   163  	return
   164  }