github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/openstack/identity/v3/regions/requests.go (about)

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