github.com/chnsz/golangsdk@v0.0.0-20240506093406-85a3fbfa605b/openstack/swr/v2/namespaces/requests.go (about)

     1  package namespaces
     2  
     3  import (
     4  	"github.com/chnsz/golangsdk"
     5  )
     6  
     7  var RequestOpts golangsdk.RequestOpts = golangsdk.RequestOpts{
     8  	MoreHeaders: map[string]string{"Content-Type": "application/json"},
     9  }
    10  
    11  // CreateOptsBuilder allows extensions to add additional parameters to the
    12  // Create request.
    13  type CreateOptsBuilder interface {
    14  	ToNamespaceCreateMap() (map[string]interface{}, error)
    15  }
    16  
    17  // CreateOpts contains all the values needed to create a new network
    18  type CreateOpts struct {
    19  	Namespace string `json:"namespace" required:"true"`
    20  }
    21  
    22  // ToNamespaceCreateMap builds a create request body from CreateOpts.
    23  func (opts CreateOpts) ToNamespaceCreateMap() (map[string]interface{}, error) {
    24  	return golangsdk.BuildRequestBody(opts, "")
    25  }
    26  
    27  // Create accepts a CreateOpts struct and uses the values to create a new namespace.
    28  func Create(c *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
    29  	b, err := opts.ToNamespaceCreateMap()
    30  	if err != nil {
    31  		r.Err = err
    32  		return
    33  	}
    34  	reqOpt := &golangsdk.RequestOpts{OkCodes: []int{201}}
    35  	_, r.Err = c.Post(rootURL(c), b, &r.Body, reqOpt)
    36  	return
    37  }
    38  
    39  // Get retrieves a particular network based on its unique ID.
    40  func Get(c *golangsdk.ServiceClient, id string) (r GetResult) {
    41  	_, r.Err = c.Get(resourceURL(c, id), &r.Body, &golangsdk.RequestOpts{
    42  		OkCodes:     []int{200},
    43  		MoreHeaders: RequestOpts.MoreHeaders, JSONBody: nil,
    44  	})
    45  	return
    46  }
    47  
    48  // Delete will permanently delete a particular network based on its unique ID.
    49  func Delete(c *golangsdk.ServiceClient, id string) (r DeleteResult) {
    50  	_, r.Err = c.Delete(resourceURL(c, id), &golangsdk.RequestOpts{
    51  		OkCodes:     []int{204},
    52  		MoreHeaders: RequestOpts.MoreHeaders, JSONBody: nil,
    53  	})
    54  	return
    55  }
    56  
    57  // CreateAccessOptsBuilder allows extensions to add additional parameters to the create request.
    58  type CreateAccessOptsBuilder interface {
    59  	ToAccessCreateMap() (map[string]interface{}, error)
    60  }
    61  
    62  // CreateAccessOpts contains all the values needed to create access of a namespace
    63  type CreateAccessOpts struct {
    64  	Users []User
    65  }
    66  
    67  // Access information of a user
    68  type User struct {
    69  	// ID of the user
    70  	UserID string `json:"user_id" required:"true"`
    71  	// Name of the user
    72  	UserName string `json:"user_name" required:"true"`
    73  	// Permission of the user, 7: Manage. 3: Write. 1: Read
    74  	Auth int `json:"auth" required:"true"`
    75  }
    76  
    77  // ToAccessCreateMap builds a create request body from CreateAccessOpts.
    78  func (opts CreateAccessOpts) ToAccessCreateMap() (map[string]interface{}, error) {
    79  	return golangsdk.BuildRequestBody(opts, "")
    80  }
    81  
    82  // CreateAccess accepts a CreateAccessOpts struct and uses the values to create access of a namespace.
    83  func CreateAccess(c *golangsdk.ServiceClient, opts CreateAccessOptsBuilder, namespace string) (r CreateAccessResult) {
    84  	b, err := opts.ToAccessCreateMap()
    85  	if err != nil {
    86  		r.Err = err
    87  		return
    88  	}
    89  	_, r.Err = c.Post(accessURL(c, namespace), b["Users"], &r.Body, nil)
    90  	return
    91  }
    92  
    93  // Get retrieves the access of a namespace.
    94  func GetAccess(c *golangsdk.ServiceClient, namespace string) (r GetAccessResult) {
    95  	_, r.Err = c.Get(accessURL(c, namespace), &r.Body, nil)
    96  	return
    97  }
    98  
    99  // Delete will permanently delete the access of a namespace.
   100  func DeleteAccess(c *golangsdk.ServiceClient, userIDs []string, namespace string) (r DeleteAccessResult) {
   101  	reqOpt := &golangsdk.RequestOpts{JSONBody: userIDs}
   102  	_, r.Err = c.Delete(accessURL(c, namespace), reqOpt)
   103  	return
   104  }