github.com/gophercloud/gophercloud@v1.11.0/openstack/identity/v3/regions/requests.go (about)

     1  package regions
     2  
     3  import (
     4  	"github.com/gophercloud/gophercloud"
     5  	"github.com/gophercloud/gophercloud/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 := gophercloud.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 *gophercloud.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 *gophercloud.ServiceClient, id string) (r GetResult) {
    43  	resp, err := client.Get(getURL(client, id), &r.Body, nil)
    44  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    45  	return
    46  }
    47  
    48  // CreateOptsBuilder allows extensions to add additional parameters to
    49  // the Create request.
    50  type CreateOptsBuilder interface {
    51  	ToRegionCreateMap() (map[string]interface{}, error)
    52  }
    53  
    54  // CreateOpts provides options used to create a region.
    55  type CreateOpts struct {
    56  	// ID is the ID of the new region.
    57  	ID string `json:"id,omitempty"`
    58  
    59  	// Description is a description of the region.
    60  	Description string `json:"description,omitempty"`
    61  
    62  	// ParentRegionID is the ID of the parent the region to add this region under.
    63  	ParentRegionID string `json:"parent_region_id,omitempty"`
    64  
    65  	// Extra is free-form extra key/value pairs to describe the region.
    66  	Extra map[string]interface{} `json:"-"`
    67  }
    68  
    69  // ToRegionCreateMap formats a CreateOpts into a create request.
    70  func (opts CreateOpts) ToRegionCreateMap() (map[string]interface{}, error) {
    71  	b, err := gophercloud.BuildRequestBody(opts, "region")
    72  	if err != nil {
    73  		return nil, err
    74  	}
    75  
    76  	if opts.Extra != nil {
    77  		if v, ok := b["region"].(map[string]interface{}); ok {
    78  			for key, value := range opts.Extra {
    79  				v[key] = value
    80  			}
    81  		}
    82  	}
    83  
    84  	return b, nil
    85  }
    86  
    87  // Create creates a new Region.
    88  func Create(client *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
    89  	b, err := opts.ToRegionCreateMap()
    90  	if err != nil {
    91  		r.Err = err
    92  		return
    93  	}
    94  	resp, err := client.Post(createURL(client), &b, &r.Body, &gophercloud.RequestOpts{
    95  		OkCodes: []int{201},
    96  	})
    97  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
    98  	return
    99  }
   100  
   101  // UpdateOptsBuilder allows extensions to add additional parameters to
   102  // the Update request.
   103  type UpdateOptsBuilder interface {
   104  	ToRegionUpdateMap() (map[string]interface{}, error)
   105  }
   106  
   107  // UpdateOpts provides options for updating a region.
   108  type UpdateOpts struct {
   109  	// Description is a description of the region.
   110  	Description *string `json:"description,omitempty"`
   111  
   112  	// ParentRegionID is the ID of the parent region.
   113  	ParentRegionID string `json:"parent_region_id,omitempty"`
   114  
   115  	/*
   116  		// Due to a bug in Keystone, the Extra column of the Region table
   117  		// is not updatable, see: https://bugs.launchpad.net/keystone/+bug/1729933
   118  		// The following lines should be uncommented once the fix is merged.
   119  
   120  		// Extra is free-form extra key/value pairs to describe the region.
   121  		Extra map[string]interface{} `json:"-"`
   122  	*/
   123  }
   124  
   125  // ToRegionUpdateMap formats a UpdateOpts into an update request.
   126  func (opts UpdateOpts) ToRegionUpdateMap() (map[string]interface{}, error) {
   127  	b, err := gophercloud.BuildRequestBody(opts, "region")
   128  	if err != nil {
   129  		return nil, err
   130  	}
   131  
   132  	/*
   133  		// Due to a bug in Keystone, the Extra column of the Region table
   134  		// is not updatable, see: https://bugs.launchpad.net/keystone/+bug/1729933
   135  		// The following lines should be uncommented once the fix is merged.
   136  
   137  		if opts.Extra != nil {
   138  			if v, ok := b["region"].(map[string]interface{}); ok {
   139  				for key, value := range opts.Extra {
   140  					v[key] = value
   141  				}
   142  			}
   143  		}
   144  	*/
   145  
   146  	return b, nil
   147  }
   148  
   149  // Update updates an existing Region.
   150  func Update(client *gophercloud.ServiceClient, regionID string, opts UpdateOptsBuilder) (r UpdateResult) {
   151  	b, err := opts.ToRegionUpdateMap()
   152  	if err != nil {
   153  		r.Err = err
   154  		return
   155  	}
   156  	resp, err := client.Patch(updateURL(client, regionID), &b, &r.Body, &gophercloud.RequestOpts{
   157  		OkCodes: []int{200},
   158  	})
   159  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   160  	return
   161  }
   162  
   163  // Delete deletes a region.
   164  func Delete(client *gophercloud.ServiceClient, regionID string) (r DeleteResult) {
   165  	resp, err := client.Delete(deleteURL(client, regionID), nil)
   166  	_, r.Header, r.Err = gophercloud.ParseResponse(resp, err)
   167  	return
   168  }