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

     1  package projects
     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  	ToProjectListQuery() (string, error)
    12  }
    13  
    14  // ListOpts enables filtering of a list request.
    15  type ListOpts struct {
    16  	// DomainID filters the response by a domain ID.
    17  	DomainID string `q:"domain_id"`
    18  
    19  	// Name filters the response by project name.
    20  	Name string `q:"name"`
    21  
    22  	// ParentID filters the response by projects of a given parent project.
    23  	ParentID string `q:"parent_id"`
    24  }
    25  
    26  // ToProjectListQuery formats a ListOpts into a query string.
    27  func (opts ListOpts) ToProjectListQuery() (string, error) {
    28  	q, err := golangsdk.BuildQueryString(opts)
    29  	return q.String(), err
    30  }
    31  
    32  // List enumerates the Projects to which the current token has access.
    33  func List(client *golangsdk.ServiceClient, opts ListOptsBuilder) pagination.Pager {
    34  	url := listURL(client)
    35  	if opts != nil {
    36  		query, err := opts.ToProjectListQuery()
    37  		if err != nil {
    38  			return pagination.Pager{Err: err}
    39  		}
    40  		url += query
    41  	}
    42  	return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
    43  		return ProjectPage{pagination.LinkedPageBase{PageResult: r}}
    44  	})
    45  }
    46  
    47  // Get retrieves details on a single project, by ID.
    48  func Get(client *golangsdk.ServiceClient, id string) (r GetResult) {
    49  	_, r.Err = client.Get(getURL(client, id), &r.Body, nil)
    50  	return
    51  }
    52  
    53  // CreateOptsBuilder allows extensions to add additional parameters to
    54  // the Create request.
    55  type CreateOptsBuilder interface {
    56  	ToProjectCreateMap() (map[string]interface{}, error)
    57  }
    58  
    59  // CreateOpts represents parameters used to create a project.
    60  type CreateOpts struct {
    61  	// DomainID is the ID this project will belong under.
    62  	DomainID string `json:"domain_id,omitempty"`
    63  
    64  	// Name is the name of the project.
    65  	Name string `json:"name" required:"true"`
    66  
    67  	// ParentID specifies the parent project of this new project.
    68  	ParentID string `json:"parent_id,omitempty"`
    69  
    70  	// Description is the description of the project.
    71  	Description string `json:"description,omitempty"`
    72  }
    73  
    74  // ToProjectCreateMap formats a CreateOpts into a create request.
    75  func (opts CreateOpts) ToProjectCreateMap() (map[string]interface{}, error) {
    76  	return golangsdk.BuildRequestBody(opts, "project")
    77  }
    78  
    79  // Create creates a new Project.
    80  func Create(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
    81  	b, err := opts.ToProjectCreateMap()
    82  	if err != nil {
    83  		r.Err = err
    84  		return
    85  	}
    86  	_, r.Err = client.Post(createURL(client), &b, &r.Body, nil)
    87  	return
    88  }
    89  
    90  // Delete deletes a project.
    91  func Delete(client *golangsdk.ServiceClient, projectID string) (r DeleteResult) {
    92  	_, r.Err = client.Delete(deleteURL(client, projectID), &golangsdk.RequestOpts{
    93  		OkCodes: []int{200},
    94  	})
    95  	return
    96  }
    97  
    98  // UpdateOptsBuilder allows extensions to add additional parameters to
    99  // the Update request.
   100  type UpdateOptsBuilder interface {
   101  	ToProjectUpdateMap() (map[string]interface{}, error)
   102  }
   103  
   104  // UpdateOpts represents parameters to update a project.
   105  type UpdateOpts struct {
   106  	// Name is the name of the project.
   107  	Name string `json:"name,omitempty"`
   108  
   109  	// Description is the description of the project.
   110  	Description string `json:"description,omitempty"`
   111  }
   112  
   113  // ToUpdateCreateMap formats a UpdateOpts into an update request.
   114  func (opts UpdateOpts) ToProjectUpdateMap() (map[string]interface{}, error) {
   115  	return golangsdk.BuildRequestBody(opts, "project")
   116  }
   117  
   118  // Update modifies the attributes of a project.
   119  func Update(client *golangsdk.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
   120  	b, err := opts.ToProjectUpdateMap()
   121  	if err != nil {
   122  		r.Err = err
   123  		return
   124  	}
   125  	_, r.Err = client.Patch(updateURL(client, id), b, &r.Body, &golangsdk.RequestOpts{
   126  		OkCodes: []int{200},
   127  	})
   128  	return
   129  }