github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/identity/v3/regions/requests.go (about)

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