github.com/chnsz/golangsdk@v0.0.0-20240506093406-85a3fbfa605b/openstack/waf/v1/clouds/requests.go (about)

     1  package clouds
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/chnsz/golangsdk"
     6  )
     7  
     8  // CreateOpts is the structure required by the 'Create' method to create a cloud WAF.
     9  type CreateOpts struct {
    10  	// The ID of the project to which the cloud WAF belongs.
    11  	ProjectId string `json:"project_id" required:"true"`
    12  	// Whether the order is auto pay.
    13  	IsAutoPay *bool `json:"is_auto_pay" required:"true"`
    14  	// Whether auto renew is enabled for resource payment.
    15  	IsAutoRenew *bool `json:"is_auto_renew" required:"true"`
    16  	// The region where the cloud WAF is located.
    17  	RegionId string `json:"region_id" required:"true"`
    18  	// The configuration of the cloud WAF, such as specification code.
    19  	ProductInfo *ProductInfo `json:"waf_product_info,omitempty"`
    20  	// The configuration of the bandwidth extended packages.
    21  	BandwidthExpackProductInfo *ExpackProductInfo `json:"bandwidth_expack_product_info,omitempty"`
    22  	// The configuration of the domain extended packages.
    23  	DomainExpackProductInfo *ExpackProductInfo `json:"domain_expack_product_info,omitempty"`
    24  	// The configuration of the rule extended packages.
    25  	RuleExpackProductInfo *ExpackProductInfo `json:"rule_expack_product_info,omitempty"`
    26  	// The ID of the enterprise project to which the cloud WAF belongs.
    27  	EnterpriseProjectId string `q:"enterprise_project_id" json:"-"`
    28  }
    29  
    30  // ProductInfo is an object that represents the configuration of the cloud WAF.
    31  type ProductInfo struct {
    32  	// The specification of the cloud WAF.
    33  	ResourceSpecCode string `json:"resource_spec_code,omitempty"`
    34  	// The charging period unit of the cloud WAF.
    35  	PeriodType string `json:"period_type,omitempty"`
    36  	// The charging period of the cloud WAF.
    37  	PeriodNum int `json:"period_num,omitempty"`
    38  }
    39  
    40  // ExpackProductInfo is an object that represents the configuration of the extended packages.
    41  type ExpackProductInfo struct {
    42  	// The number of the extended packages
    43  	ResourceSize int `json:"resource_size,omitempty"`
    44  }
    45  
    46  var requestOpts = golangsdk.RequestOpts{
    47  	MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"},
    48  }
    49  
    50  // Create is a method used to create a new cloud WAF using given parameters.
    51  func Create(client *golangsdk.ServiceClient, opts CreateOpts) (*string, error) {
    52  	b, err := golangsdk.BuildRequestBody(opts, "")
    53  	if err != nil {
    54  		return nil, err
    55  	}
    56  	url := createURL(client)
    57  	query, err := golangsdk.BuildQueryString(opts)
    58  	if err != nil {
    59  		return nil, err
    60  	}
    61  	url += query.String()
    62  
    63  	var r createResp
    64  	_, err = client.Post(url, b, &r, &golangsdk.RequestOpts{
    65  		MoreHeaders: requestOpts.MoreHeaders,
    66  	})
    67  	return &r.OrderId, err
    68  }
    69  
    70  // Get is a method used to obtain the cloud WAF details.
    71  func Get(client *golangsdk.ServiceClient) (*Instance, error) {
    72  	return GetWithEpsID(client, "")
    73  }
    74  
    75  // GetWithEpsID is a method used to obtain the cloud WAF details with eps ID.
    76  func GetWithEpsID(client *golangsdk.ServiceClient, epsID string) (*Instance, error) {
    77  	var r Instance
    78  	_, err := client.Get(getURL(client)+generateEpsIdQuery(epsID), &r, &golangsdk.RequestOpts{
    79  		MoreHeaders: requestOpts.MoreHeaders,
    80  	})
    81  	return &r, err
    82  }
    83  
    84  func generateEpsIdQuery(epsID string) string {
    85  	if len(epsID) == 0 {
    86  		return ""
    87  	}
    88  	return fmt.Sprintf("?enterprise_project_id=%s", epsID)
    89  }
    90  
    91  // UpdateOpts is the structure required by the 'Update' method to update the cloud WAF configuration.
    92  type UpdateOpts struct {
    93  	// The ID of the project to which the cloud WAF belongs.
    94  	ProjectId string `json:"project_id" required:"true"`
    95  	// Whether the order is auto pay.
    96  	IsAutoPay *bool `json:"is_auto_pay" required:"true"`
    97  	// The configuration of the cloud WAF, such as specification code.
    98  	ProductInfo *UpdateProductInfo `json:"waf_product_info,omitempty"`
    99  	// The configuration of the bandwidth extended packages.
   100  	BandwidthExpackProductInfo *ExpackProductInfo `json:"bandwidth_expack_product_info,omitempty"`
   101  	// The configuration of the domain extended packages.
   102  	DomainExpackProductInfo *ExpackProductInfo `json:"domain_expack_product_info,omitempty"`
   103  	// The configuration of the rule extended packages.
   104  	RuleExpackProductInfo *ExpackProductInfo `json:"rule_expack_product_info,omitempty"`
   105  	// The ID of the enterprise project to which the cloud WAF belongs.
   106  	EnterpriseProjectId string `q:"enterprise_project_id" json:"-"`
   107  }
   108  
   109  // UpdateProductInfo is an object that represents the update configuration of the cloud WAF.
   110  type UpdateProductInfo struct {
   111  	// Whether the AS path attributes of the routes are not compared during load balancing.
   112  	ResourceSpecCode string `json:"resource_spec_code,omitempty"`
   113  }
   114  
   115  // Update is a method used to update the cloud WAF using given parameters.
   116  func Update(client *golangsdk.ServiceClient, opts UpdateOpts) (*string, error) {
   117  	b, err := golangsdk.BuildRequestBody(opts, "")
   118  	if err != nil {
   119  		return nil, err
   120  	}
   121  	url := updateURL(client)
   122  	query, err := golangsdk.BuildQueryString(opts)
   123  	if err != nil {
   124  		return nil, err
   125  	}
   126  	url += query.String()
   127  
   128  	var r updateResp
   129  	_, err = client.Post(url, b, &r, &golangsdk.RequestOpts{
   130  		MoreHeaders: requestOpts.MoreHeaders,
   131  	})
   132  	return &r.OrderId, err
   133  }
   134  
   135  // CreatePostPaidOpts is the structure required by the 'Create' method to create a post paid cloud WAF.
   136  type CreatePostPaidOpts struct {
   137  	// The region where the cloud WAF is located. This field will be set to header
   138  	Region string `json:"-" required:"true"`
   139  	// The website to which the account belongs.
   140  	ConsoleArea string `json:"console_area" required:"true"`
   141  	// The ID of the enterprise project to which the cloud WAF belongs.
   142  	EnterpriseProjectId string `q:"enterprise_project_id" json:"-"`
   143  }
   144  
   145  // CreatePostPaid is a method used to create a new post paid cloud WAF using given parameters.
   146  func CreatePostPaid(client *golangsdk.ServiceClient, opts CreatePostPaidOpts) (*Instance, error) {
   147  	b, err := golangsdk.BuildRequestBody(opts, "")
   148  	if err != nil {
   149  		return nil, err
   150  	}
   151  	url := createOrDeletePostPaidURL(client)
   152  	query, err := golangsdk.BuildQueryString(opts)
   153  	if err != nil {
   154  		return nil, err
   155  	}
   156  	url += query.String()
   157  	var r Instance
   158  
   159  	moreHeaders := requestOpts.MoreHeaders
   160  	moreHeaders["region"] = opts.Region
   161  	_, err = client.Post(url, b, &r, &golangsdk.RequestOpts{
   162  		MoreHeaders: moreHeaders,
   163  	})
   164  	return &r, err
   165  }
   166  
   167  // DeletePostPaidOpts is the structure required by the 'Delete' method to delete a post paid cloud WAF.
   168  type DeletePostPaidOpts struct {
   169  	// The region where the cloud WAF is located. This field will be set to header
   170  	Region string `json:"-" required:"true"`
   171  	// The ID of the enterprise project to which the cloud WAF belongs.
   172  	EnterpriseProjectId string `q:"enterprise_project_id" json:"-"`
   173  }
   174  
   175  // DeletePostPaid is a method used to delete a post paid cloud WAF using given parameters.
   176  func DeletePostPaid(client *golangsdk.ServiceClient, opts DeletePostPaidOpts) error {
   177  	url := createOrDeletePostPaidURL(client)
   178  	query, err := golangsdk.BuildQueryString(opts)
   179  	if err != nil {
   180  		return err
   181  	}
   182  	url += query.String()
   183  	moreHeaders := requestOpts.MoreHeaders
   184  	moreHeaders["region"] = opts.Region
   185  	_, err = client.Delete(url, &golangsdk.RequestOpts{
   186  		MoreHeaders: moreHeaders,
   187  	})
   188  	return err
   189  }