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

     1  package accesspolicies
     2  
     3  import (
     4  	"github.com/chnsz/golangsdk"
     5  	"github.com/chnsz/golangsdk/pagination"
     6  )
     7  
     8  // CreateOpts is the method that used to create an access policy.
     9  type CreateOpts struct {
    10  	// Access policy detail.
    11  	Policy AccessPolicy `json:"policy" required:"true"`
    12  	// List of policy objects.
    13  	PolicyObjectsList []AccessPolicyObjectInfo `json:"policy_objects_list,omitempty"`
    14  }
    15  
    16  // AccessPolicy is the structure that represents the basic configuration of the access policy.
    17  type AccessPolicy struct {
    18  	// Policy name.
    19  	// + PRIVATE_ACCESS: Private line access
    20  	PolicyName string `json:"policy_name" required:"true"`
    21  	// Blacklist type.
    22  	// + INTERNET: Internet.
    23  	BlacklistType string `json:"blacklist_type" required:"true"`
    24  }
    25  
    26  // AccessPolicyObjectInfo is the structure that represents the object list configuration.
    27  type AccessPolicyObjectInfo struct {
    28  	// Object ID of the blacklist.
    29  	ObjectId string `json:"object_id" required:"true"`
    30  	// Object type of the blacklist.
    31  	// + USER
    32  	// + USERGROUP
    33  	ObjectType string `json:"object_type" required:"true"`
    34  	// Obejct name of the blacklist.
    35  	ObjectName string `json:"object_name,omitempty"`
    36  }
    37  
    38  var requestOpts = golangsdk.RequestOpts{
    39  	MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"},
    40  }
    41  
    42  // Create is a method to create an access policy using given parameters.
    43  func Create(c *golangsdk.ServiceClient, opts CreateOpts) error {
    44  	b, err := golangsdk.BuildRequestBody(opts, "")
    45  	if err != nil {
    46  		return err
    47  	}
    48  
    49  	_, err = c.Post(rootURL(c), b, nil, &golangsdk.RequestOpts{
    50  		MoreHeaders: requestOpts.MoreHeaders,
    51  	})
    52  	return err
    53  }
    54  
    55  // List is a method to query all access policies under a specified region.
    56  func List(c *golangsdk.ServiceClient) ([]AccessPolicyDetailInfo, error) {
    57  	var r policiesResp
    58  	_, err := c.Get(rootURL(c), &r, &golangsdk.RequestOpts{
    59  		MoreHeaders: requestOpts.MoreHeaders,
    60  	})
    61  	return r.Policies, err
    62  }
    63  
    64  // ListObjectsOpts is the structure that used to query object list.
    65  type ListObjectsOpts struct {
    66  	// Policy ID.
    67  	PolicyId string `json:"-" required:"true"`
    68  	// Number of records to be queried.
    69  	// Value range: 0–2000.
    70  	// Default value: 10.
    71  	Limit int `q:"limit"`
    72  	// The offset number.
    73  	// Value range: 0–1999.
    74  	// Default value: 0
    75  	Offset int `q:"offset"`
    76  }
    77  
    78  // ListObjects is the method that used to query object list under a specified policy using given parameters.
    79  func ListObjects(c *golangsdk.ServiceClient, opts ListObjectsOpts) ([]AccessPolicyObject, error) {
    80  	url := resourceURL(c, opts.PolicyId)
    81  	query, err := golangsdk.BuildQueryString(opts)
    82  	if err != nil {
    83  		return nil, err
    84  	}
    85  	url += query.String()
    86  
    87  	pager := pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
    88  		p := AccessPolicyPage{pagination.OffsetPageBase{PageResult: r}}
    89  		return p
    90  	})
    91  	pager.Headers = requestOpts.MoreHeaders
    92  	pages, err := pager.AllPages()
    93  
    94  	if err != nil {
    95  		return nil, err
    96  	}
    97  	return ExtractAccessPolicies(pages)
    98  }
    99  
   100  // UpdateOpts is the method that used to update an existing access policy.
   101  type UpdateOpts struct {
   102  	// The associated policy ID to which the objects belong.
   103  	PolicyId string `json:"-" required:"true"`
   104  	// List of policy objects.
   105  	PolicyObjectsList []AccessPolicyObjectInfo `json:"policy_objects_list,omitempty"`
   106  }
   107  
   108  // UpdateObjects is a method to modify the policy objects using given parameters.
   109  func UpdateObjects(c *golangsdk.ServiceClient, opts UpdateOpts) error {
   110  	b, err := golangsdk.BuildRequestBody(opts, "")
   111  	if err != nil {
   112  		return err
   113  	}
   114  
   115  	_, err = c.Put(resourceURL(c, opts.PolicyId), b, nil, &golangsdk.RequestOpts{
   116  		MoreHeaders: requestOpts.MoreHeaders,
   117  	})
   118  	return err
   119  }
   120  
   121  // DeleteOpts is the method that used to delete an existing access policy.
   122  type DeleteOpts struct {
   123  	// List of policy ID.
   124  	PolicyIdList []string `json:"policy_id_list,omitempty"`
   125  }
   126  
   127  // Delete is a method to delete an existing access policy using given parameters.
   128  func Delete(c *golangsdk.ServiceClient, opts DeleteOpts) error {
   129  	b, err := golangsdk.BuildRequestBody(opts, "")
   130  	if err != nil {
   131  		return err
   132  	}
   133  
   134  	_, err = c.DeleteWithBody(rootURL(c), b, &golangsdk.RequestOpts{
   135  		MoreHeaders: requestOpts.MoreHeaders,
   136  	})
   137  	return err
   138  }