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

     1  package services
     2  
     3  import (
     4  	"github.com/huaweicloud/golangsdk"
     5  	"github.com/huaweicloud/golangsdk/pagination"
     6  )
     7  
     8  // CreateOptsBuilder allows extensions to add additional parameters to
     9  // the Create request.
    10  type CreateOptsBuilder interface {
    11  	ToServiceCreateMap() (map[string]interface{}, error)
    12  }
    13  
    14  // CreateOpts provides options used to create a service.
    15  type CreateOpts struct {
    16  	// Type is the type of the service.
    17  	Type string `json:"type"`
    18  
    19  	// Enabled is whether or not the service is enabled.
    20  	Enabled *bool `json:"enabled,omitempty"`
    21  
    22  	// Extra is free-form extra key/value pairs to describe the service.
    23  	Extra map[string]interface{} `json:"-"`
    24  }
    25  
    26  // ToServiceCreateMap formats a CreateOpts into a create request.
    27  func (opts CreateOpts) ToServiceCreateMap() (map[string]interface{}, error) {
    28  	b, err := golangsdk.BuildRequestBody(opts, "service")
    29  	if err != nil {
    30  		return nil, err
    31  	}
    32  
    33  	if opts.Extra != nil {
    34  		if v, ok := b["service"].(map[string]interface{}); ok {
    35  			for key, value := range opts.Extra {
    36  				v[key] = value
    37  			}
    38  		}
    39  	}
    40  
    41  	return b, nil
    42  }
    43  
    44  // Create adds a new service of the requested type to the catalog.
    45  func Create(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
    46  	b, err := opts.ToServiceCreateMap()
    47  	if err != nil {
    48  		r.Err = err
    49  		return
    50  	}
    51  	_, r.Err = client.Post(createURL(client), &b, &r.Body, &golangsdk.RequestOpts{
    52  		OkCodes: []int{201},
    53  	})
    54  	return
    55  }
    56  
    57  // ListOptsBuilder enables extensions to add additional parameters to the List
    58  // request.
    59  type ListOptsBuilder interface {
    60  	ToServiceListMap() (string, error)
    61  }
    62  
    63  // ListOpts provides options for filtering the List results.
    64  type ListOpts struct {
    65  	// ServiceType filter the response by a type of service.
    66  	ServiceType string `q:"type"`
    67  
    68  	// Name filters the response by a service name.
    69  	Name string `q:"name"`
    70  }
    71  
    72  // ToServiceListMap builds a list query from the list options.
    73  func (opts ListOpts) ToServiceListMap() (string, error) {
    74  	q, err := golangsdk.BuildQueryString(opts)
    75  	return q.String(), err
    76  }
    77  
    78  // List enumerates the services available to a specific user.
    79  func List(client *golangsdk.ServiceClient, opts ListOptsBuilder) pagination.Pager {
    80  	url := listURL(client)
    81  	if opts != nil {
    82  		query, err := opts.ToServiceListMap()
    83  		if err != nil {
    84  			return pagination.Pager{Err: err}
    85  		}
    86  		url += query
    87  	}
    88  	return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
    89  		return ServicePage{pagination.LinkedPageBase{PageResult: r}}
    90  	})
    91  }
    92  
    93  // Get returns additional information about a service, given its ID.
    94  func Get(client *golangsdk.ServiceClient, serviceID string) (r GetResult) {
    95  	_, r.Err = client.Get(serviceURL(client, serviceID), &r.Body, nil)
    96  	return
    97  }
    98  
    99  // UpdateOptsBuilder allows extensions to add additional parameters to
   100  // the Update request.
   101  type UpdateOptsBuilder interface {
   102  	ToServiceUpdateMap() (map[string]interface{}, error)
   103  }
   104  
   105  // UpdateOpts provides options for updating a service.
   106  type UpdateOpts struct {
   107  	// Type is the type of the service.
   108  	Type string `json:"type"`
   109  
   110  	// Enabled is whether or not the service is enabled.
   111  	Enabled *bool `json:"enabled,omitempty"`
   112  
   113  	// Extra is free-form extra key/value pairs to describe the service.
   114  	Extra map[string]interface{} `json:"-"`
   115  }
   116  
   117  // ToServiceUpdateMap formats a UpdateOpts into an update request.
   118  func (opts UpdateOpts) ToServiceUpdateMap() (map[string]interface{}, error) {
   119  	b, err := golangsdk.BuildRequestBody(opts, "service")
   120  	if err != nil {
   121  		return nil, err
   122  	}
   123  
   124  	if opts.Extra != nil {
   125  		if v, ok := b["service"].(map[string]interface{}); ok {
   126  			for key, value := range opts.Extra {
   127  				v[key] = value
   128  			}
   129  		}
   130  	}
   131  
   132  	return b, nil
   133  }
   134  
   135  // Update updates an existing Service.
   136  func Update(client *golangsdk.ServiceClient, serviceID string, opts UpdateOptsBuilder) (r UpdateResult) {
   137  	b, err := opts.ToServiceUpdateMap()
   138  	if err != nil {
   139  		r.Err = err
   140  		return
   141  	}
   142  	_, r.Err = client.Patch(updateURL(client, serviceID), &b, &r.Body, &golangsdk.RequestOpts{
   143  		OkCodes: []int{200},
   144  	})
   145  	return
   146  }
   147  
   148  // Delete removes an existing service.
   149  // It either deletes all associated endpoints, or fails until all endpoints
   150  // are deleted.
   151  func Delete(client *golangsdk.ServiceClient, serviceID string) (r DeleteResult) {
   152  	_, r.Err = client.Delete(serviceURL(client, serviceID), nil)
   153  	return
   154  }