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

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