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

     1  package acls
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/chnsz/golangsdk"
     7  	"github.com/chnsz/golangsdk/pagination"
     8  )
     9  
    10  // CreateOpts is the structure that used to create a new ACL policy.
    11  type CreateOpts struct {
    12  	// The ACL name.
    13  	Name string `json:"acl_name" required:"true"`
    14  	// The ACL type. The valid values are as follows:
    15  	// + PERMIT
    16  	// + DENY
    17  	Type string `json:"acl_type" required:"true"`
    18  	// The value of the ACL policy.
    19  	// One or more values are supported, separated by commas (,).
    20  	Value string `json:"acl_value" required:"true"`
    21  	// The entity type. The valid values are as follows:
    22  	// + IP
    23  	// + DOMAIN
    24  	// + DOMAIN_ID
    25  	EntityType string `json:"entity_type" required:"true"`
    26  }
    27  
    28  var requestOpts = golangsdk.RequestOpts{
    29  	MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"},
    30  }
    31  
    32  // Create is a method used to create a private DNAT rule using given parameters.
    33  func Create(c *golangsdk.ServiceClient, instanceId string, opts CreateOpts) (*Policy, error) {
    34  	b, err := golangsdk.BuildRequestBody(opts, "")
    35  	if err != nil {
    36  		return nil, err
    37  	}
    38  
    39  	var r Policy
    40  	_, err = c.Post(rootURL(c, instanceId), b, &r, &golangsdk.RequestOpts{
    41  		MoreHeaders: requestOpts.MoreHeaders,
    42  	})
    43  	return &r, err
    44  }
    45  
    46  // Get is a method used to obtain the ACL policy detail by its ID.
    47  func Get(c *golangsdk.ServiceClient, instanceId, policyId string) (*Policy, error) {
    48  	var r Policy
    49  	_, err := c.Get(resourceURL(c, instanceId, policyId), &r, &golangsdk.RequestOpts{
    50  		MoreHeaders: requestOpts.MoreHeaders,
    51  	})
    52  	return &r, err
    53  }
    54  
    55  // UpdateOpts is the structure that used to modify an existing ACL policy configuration.
    56  type UpdateOpts struct {
    57  	// The ACL name.
    58  	Name string `json:"acl_name" required:"true"`
    59  	// The ACL type. The valid values are as follows:
    60  	// + PERMIT
    61  	// + DENY
    62  	Type string `json:"acl_type" required:"true"`
    63  	// The value of the ACL policy.
    64  	// One or more values are supported, separated by commas (,).
    65  	Value string `json:"acl_value" required:"true"`
    66  	// The entity type. The valid values are as follows:
    67  	// + IP
    68  	// + DOMAIN
    69  	// + DOMAIN_ID
    70  	// The entity type does not support update.
    71  	EntityType string `json:"entity_type" required:"true"`
    72  }
    73  
    74  // ListBindOpts is the structure used to querying published API list that ACL policy associated.
    75  type ListOpts struct {
    76  	// The instnace ID to which the API belongs.
    77  	InstanceId string `json:"-" required:"true"`
    78  	// Offset from which the query starts.
    79  	// If the offset is less than 0, the value is automatically converted to 0. Default to 0.
    80  	Offset int `q:"offset"`
    81  	// Number of items displayed on each page. The valid values are range form 1 to 500, default to 20.
    82  	Limit int `q:"limit"`
    83  	// The ACL policy ID.
    84  	PolicyId string `q:"id"`
    85  	// The ACL policy name.
    86  	Name string `q:"name"`
    87  	// The ACL type.
    88  	Type string `q:"acl_type"`
    89  	// The object type.
    90  	EntityType string `q:"entity_type"`
    91  	// Parameter name (name) for exact matching.
    92  	PreciseSearch string `q:"precise_search"`
    93  }
    94  
    95  // List is a method to obtain all ACL policies under a specified instance.
    96  func List(c *golangsdk.ServiceClient, opts ListOpts) ([]Policy, error) {
    97  	url := rootURL(c, opts.InstanceId)
    98  	query, err := golangsdk.BuildQueryString(opts)
    99  	if err != nil {
   100  		return nil, err
   101  	}
   102  	url += query.String()
   103  
   104  	pages, err := pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
   105  		p := PolicyPage{pagination.OffsetPageBase{PageResult: r}}
   106  		return p
   107  	}).AllPages()
   108  
   109  	if err != nil {
   110  		return nil, err
   111  	}
   112  	return ExtractPolicies(pages)
   113  }
   114  
   115  // Update is a method used to modify the ACL policy configuration using given parameters.
   116  func Update(c *golangsdk.ServiceClient, instanceId, policyId string, opts UpdateOpts) (*Policy, error) {
   117  	b, err := golangsdk.BuildRequestBody(opts, "")
   118  	if err != nil {
   119  		return nil, err
   120  	}
   121  
   122  	var r Policy
   123  	_, err = c.Put(resourceURL(c, instanceId, policyId), b, &r, &golangsdk.RequestOpts{
   124  		MoreHeaders: requestOpts.MoreHeaders,
   125  	})
   126  	return &r, err
   127  }
   128  
   129  // Delete is a method to remove the specified ACL policy using its ID and related dedicated instance ID.
   130  func Delete(c *golangsdk.ServiceClient, instanceId, policyId string) error {
   131  	_, err := c.Delete(resourceURL(c, instanceId, policyId), &golangsdk.RequestOpts{
   132  		MoreHeaders: requestOpts.MoreHeaders,
   133  	})
   134  	return err
   135  }
   136  
   137  // BindOpts is the structure that used to bind a policy to the published APIs.
   138  type BindOpts struct {
   139  	// Instnace ID to which the API belongs.
   140  	InstanceId string `json:"-" required:"true"`
   141  	// ACL policy ID.
   142  	PolicyId string `json:"acl_id,omitempty"`
   143  	// The IDs of the API publish record.
   144  	PublishIds []string `json:"publish_ids,omitempty"`
   145  }
   146  
   147  // Bind is a method to bind the policy to one or more APIs.
   148  func Bind(c *golangsdk.ServiceClient, opts BindOpts) ([]BindResp, error) {
   149  	b, err := golangsdk.BuildRequestBody(opts, "")
   150  	if err != nil {
   151  		return nil, err
   152  	}
   153  
   154  	var r struct {
   155  		BindList []BindResp `json:"acl_bindings"`
   156  	}
   157  	_, err = c.Post(bindURL(c, opts.InstanceId), b, &r, nil)
   158  	return r.BindList, err
   159  }
   160  
   161  // ListBindOpts is the structure used to querying published API list that ACL policy associated.
   162  type ListBindOpts struct {
   163  	// The instnace ID to which the API belongs.
   164  	InstanceId string `json:"-" required:"true"`
   165  	// Offset from which the query starts.
   166  	// If the offset is less than 0, the value is automatically converted to 0. Default to 0.
   167  	Offset int `q:"offset"`
   168  	// Number of items displayed on each page. The valid values are range form 1 to 500, default to 20.
   169  	Limit int `q:"limit"`
   170  	// The ACL policy ID.
   171  	PolicyId string `q:"acl_id"`
   172  	// The API ID.
   173  	ApiId string `q:"api_id"`
   174  	// The API name.
   175  	ApiName string `q:"api_name"`
   176  	// The environment ID where the API is published.
   177  	EnvId string `q:"env_id"`
   178  	// The group ID where the API is located.
   179  	GroupId string `q:"group_id"`
   180  }
   181  
   182  // ListBind is a method to obtain all API to which the ACL policy bound.
   183  func ListBind(c *golangsdk.ServiceClient, opts ListBindOpts) ([]AclBindApiInfo, error) {
   184  	url := listBindURL(c, opts.InstanceId)
   185  	query, err := golangsdk.BuildQueryString(opts)
   186  	if err != nil {
   187  		return nil, err
   188  	}
   189  	url += query.String()
   190  
   191  	pages, err := pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
   192  		p := BindPage{pagination.OffsetPageBase{PageResult: r}}
   193  		return p
   194  	}).AllPages()
   195  
   196  	if err != nil {
   197  		return nil, err
   198  	}
   199  	return ExtractBindInfos(pages)
   200  }
   201  
   202  // BatchUnbindOpts is the structure that used to unbinding policy from the published APIs.
   203  type BatchUnbindOpts struct {
   204  	// Instance ID.
   205  	InstanceId string `json:"-" required:"true"`
   206  	// List of API and ACL policy binding relationship IDs that need to be unbound.
   207  	AclBindings []string `json:"acl_bindings,omitempty"`
   208  }
   209  
   210  // BatchUnbind is an API to unbind all ACL policies associated with the list.
   211  func BatchUnbind(c *golangsdk.ServiceClient, unbindOpt BatchUnbindOpts, action string) (*BatchUnbindResp, error) {
   212  	b, err := golangsdk.BuildRequestBody(unbindOpt, "")
   213  	if err != nil {
   214  		return nil, err
   215  	}
   216  
   217  	var (
   218  		url = fmt.Sprintf("%v?action=%v", bindURL(c, unbindOpt.InstanceId), action)
   219  		r   BatchUnbindResp
   220  	)
   221  	_, err = c.Put(url, b, &r, nil)
   222  	return &r, err
   223  }