github.com/chnsz/golangsdk@v0.0.0-20240506093406-85a3fbfa605b/openstack/identity/v3/agency/requests.go (about)

     1  package agency
     2  
     3  import (
     4  	"github.com/chnsz/golangsdk"
     5  	"github.com/chnsz/golangsdk/pagination"
     6  )
     7  
     8  type CreateOpts struct {
     9  	Name            string `json:"name" required:"true"`
    10  	DomainID        string `json:"domain_id" required:"true"`
    11  	DelegatedDomain string `json:"trust_domain_name" required:"true"`
    12  	Description     string `json:"description,omitempty"`
    13  	// the duration can be string("FOREVER", "ONEDAY") or specific days in int(1, 2, 3...)
    14  	Duration interface{} `json:"duration,omitempty"`
    15  }
    16  
    17  type CreateOptsBuilder interface {
    18  	ToAgencyCreateMap() (map[string]interface{}, error)
    19  }
    20  
    21  func (opts CreateOpts) ToAgencyCreateMap() (map[string]interface{}, error) {
    22  	return golangsdk.BuildRequestBody(opts, "agency")
    23  }
    24  
    25  func Create(c *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
    26  	b, err := opts.ToAgencyCreateMap()
    27  	if err != nil {
    28  		r.Err = err
    29  		return
    30  	}
    31  
    32  	_, r.Err = c.Post(rootURL(c), b, &r.Body, nil)
    33  	return
    34  }
    35  
    36  type UpdateOpts struct {
    37  	DelegatedDomain string `json:"trust_domain_name,omitempty"`
    38  	Description     string `json:"description,omitempty"`
    39  	// the duration can be string("FOREVER", "ONEDAY") or specific days in int(1, 2, 3...)
    40  	Duration interface{} `json:"duration,omitempty"`
    41  }
    42  
    43  type UpdateOptsBuilder interface {
    44  	ToAgencyUpdateMap() (map[string]interface{}, error)
    45  }
    46  
    47  func (opts UpdateOpts) ToAgencyUpdateMap() (map[string]interface{}, error) {
    48  	return golangsdk.BuildRequestBody(opts, "agency")
    49  }
    50  
    51  func Update(c *golangsdk.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) {
    52  	b, err := opts.ToAgencyUpdateMap()
    53  	if err != nil {
    54  		r.Err = err
    55  		return
    56  	}
    57  
    58  	reqOpt := &golangsdk.RequestOpts{OkCodes: []int{200}}
    59  	_, r.Err = c.Put(resourceURL(c, id), b, &r.Body, reqOpt)
    60  	return
    61  }
    62  
    63  func Get(c *golangsdk.ServiceClient, id string) (r GetResult) {
    64  	_, r.Err = c.Get(resourceURL(c, id), &r.Body, nil)
    65  	return
    66  }
    67  
    68  func Delete(c *golangsdk.ServiceClient, id string) (r ErrResult) {
    69  	_, r.Err = c.Delete(resourceURL(c, id), nil)
    70  	return
    71  }
    72  
    73  func AttachRoleByProject(c *golangsdk.ServiceClient, agencyID, projectID, roleID string) (r ErrResult) {
    74  	reqOpt := &golangsdk.RequestOpts{OkCodes: []int{204}}
    75  	_, r.Err = c.Put(roleURL(c, "projects", projectID, agencyID, roleID), nil, nil, reqOpt)
    76  	return
    77  }
    78  
    79  func AttachRoleByDomain(c *golangsdk.ServiceClient, agencyID, domainID, roleID string) (r ErrResult) {
    80  	reqOpt := &golangsdk.RequestOpts{OkCodes: []int{204}}
    81  	_, r.Err = c.Put(roleURL(c, "domains", domainID, agencyID, roleID), nil, nil, reqOpt)
    82  	return
    83  }
    84  
    85  func AttachAllResources(c *golangsdk.ServiceClient, agencyID, domainID, roleID string) (r ErrResult) {
    86  	reqOpt := &golangsdk.RequestOpts{OkCodes: []int{204}}
    87  	_, r.Err = c.Put(inheritedURL(c, domainID, agencyID, roleID), nil, nil, reqOpt)
    88  	return
    89  }
    90  
    91  func DetachRoleByProject(c *golangsdk.ServiceClient, agencyID, projectID, roleID string) (r ErrResult) {
    92  	reqOpt := &golangsdk.RequestOpts{OkCodes: []int{204}}
    93  	_, r.Err = c.Delete(roleURL(c, "projects", projectID, agencyID, roleID), reqOpt)
    94  	return
    95  }
    96  
    97  func DetachRoleByDomain(c *golangsdk.ServiceClient, agencyID, domainID, roleID string) (r ErrResult) {
    98  	reqOpt := &golangsdk.RequestOpts{OkCodes: []int{204}}
    99  	_, r.Err = c.Delete(roleURL(c, "domains", domainID, agencyID, roleID), reqOpt)
   100  	return
   101  }
   102  
   103  func DetachAllResources(c *golangsdk.ServiceClient, agencyID, domainID, roleID string) (r ErrResult) {
   104  	reqOpt := &golangsdk.RequestOpts{OkCodes: []int{204}}
   105  	_, r.Err = c.Delete(inheritedURL(c, domainID, agencyID, roleID), reqOpt)
   106  	return
   107  }
   108  
   109  func ListRolesAttachedOnProject(c *golangsdk.ServiceClient, agencyID, projectID string) (r ListRolesResult) {
   110  	_, r.Err = c.Get(listRolesURL(c, "projects", projectID, agencyID), &r.Body, nil)
   111  	return
   112  }
   113  
   114  func ListRolesAttachedOnDomain(c *golangsdk.ServiceClient, agencyID, domainID string) (r ListRolesResult) {
   115  	_, r.Err = c.Get(listRolesURL(c, "domains", domainID, agencyID), &r.Body, nil)
   116  	return
   117  }
   118  
   119  func ListRolesAttachedOnAllResources(c *golangsdk.ServiceClient, agencyID, domainID string) (r ListInheritedRolesResult) {
   120  	_, r.Err = c.Get(listInheritedURL(c, domainID, agencyID), &r.Body, nil)
   121  	return
   122  }
   123  
   124  type ListOpts struct {
   125  	Name          string `q:"name"`
   126  	DomainID      string `q:"domain_id"`
   127  	TrustDomainId string `q:"trust_domain_id"`
   128  }
   129  
   130  func (opts ListOpts) ToMetricsListQuery() (string, error) {
   131  	q, err := golangsdk.BuildQueryString(opts)
   132  	return q.String(), err
   133  }
   134  
   135  type ListOptsBuilder interface {
   136  	ToMetricsListQuery() (string, error)
   137  }
   138  
   139  // Agency List.
   140  func List(client *golangsdk.ServiceClient, opts ListOptsBuilder) pagination.Pager {
   141  	url := rootURL(client)
   142  	if opts != nil {
   143  		query, err := opts.ToMetricsListQuery()
   144  		if err != nil {
   145  			return pagination.Pager{Err: err}
   146  		}
   147  		url += query
   148  	}
   149  	return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
   150  		return AgencyPage{pagination.SinglePageBase(r)}
   151  	})
   152  }