github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/waf_hw/v1/domains/requests.go (about)

     1  package domains
     2  
     3  import (
     4  	"github.com/huaweicloud/golangsdk"
     5  )
     6  
     7  var RequestOpts golangsdk.RequestOpts = golangsdk.RequestOpts{
     8  	MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"},
     9  }
    10  
    11  // CreateOptsBuilder allows extensions to add additional parameters to the
    12  // Create request.
    13  type CreateOptsBuilder interface {
    14  	ToDomainCreateMap() (map[string]interface{}, error)
    15  }
    16  
    17  // CreateOpts contains all the values needed to create a new backup.
    18  type CreateOpts struct {
    19  	HostName        string       `json:"hostname" required:"true"`
    20  	Servers         []ServerOpts `json:"server" required:"true"`
    21  	PolicyId        string       `json:"policyid,omitempty"`
    22  	CertificateId   string       `json:"certificateid,omitempty"`
    23  	CertificateName string       `json:"certificatename,omitempty"`
    24  	Proxy           *bool        `json:"proxy,omitempty"`
    25  }
    26  
    27  // ServerOpts contains the origin server information.
    28  type ServerOpts struct {
    29  	FrontProtocol string `json:"front_protocol" required:"true"`
    30  	BackProtocol  string `json:"back_protocol" required:"true"`
    31  	Address       string `json:"address" required:"true"`
    32  	Port          int    `json:"port" required:"true"`
    33  	Type          string `json:"type,omitempty"`
    34  	VpcId         string `json:"vpc_id,omitempty"`
    35  }
    36  
    37  // ToDomainCreateMap builds a create request body from CreateOpts.
    38  func (opts CreateOpts) ToDomainCreateMap() (map[string]interface{}, error) {
    39  	return golangsdk.BuildRequestBody(opts, "")
    40  }
    41  
    42  // Create will create a new Domain based on the values in CreateOpts.
    43  func Create(c *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
    44  	b, err := opts.ToDomainCreateMap()
    45  	if err != nil {
    46  		r.Err = err
    47  		return
    48  	}
    49  	reqOpt := &golangsdk.RequestOpts{OkCodes: []int{200}}
    50  	_, r.Err = c.Post(rootURL(c), b, &r.Body, reqOpt)
    51  	return
    52  }
    53  
    54  // UpdateOptsBuilder allows extensions to add additional parameters to the
    55  // Update request.
    56  type UpdateOptsBuilder interface {
    57  	ToDomainUpdateMap() (map[string]interface{}, error)
    58  }
    59  
    60  // UpdateOpts contains all the values needed to update a Domain.
    61  type UpdateOpts struct {
    62  	Proxy           *bool             `json:"proxy,omitempty"`
    63  	CertificateId   string            `json:"certificateid,omitempty"`
    64  	CertificateName string            `json:"certificatename,omitempty"`
    65  	Servers         []ServerOpts      `json:"server,omitempty"`
    66  	Tls             string            `json:"tls,omitempty"`
    67  	Cipher          string            `json:"cipher,omitempty"`
    68  	BlockPages      []BlockPage       `json:"block_page,omitempty"`
    69  	TrafficMarks    []TrafficMark     `json:"traffic_mark,omitempty"`
    70  	Flag            map[string]string `json:"flag,omitempty"`
    71  	Extend          map[string]string `json:"extend,omitempty"`
    72  }
    73  
    74  // BlockPage contains the alarm page information
    75  type BlockPage struct {
    76  	Template    string       `json:"template" required:"true"`
    77  	CustomPages []CustomPage `json:"custom_page,omitempty"`
    78  	RedirectUrl string       `json:"redirect_url,omitempty"`
    79  }
    80  
    81  // CustomPage contains the customized alarm page information
    82  type CustomPage struct {
    83  	StatusCode  string `json:"status_code" required:"true"`
    84  	ContentType string `json:"content_type" required:"true"`
    85  	Content     string `json:"content" required:"true"`
    86  }
    87  
    88  // TrafficMark contains the traffic identification
    89  type TrafficMark struct {
    90  	Sip    []string `json:"sip,omitempty"`
    91  	Cookie string   `json:"cookie,omitempty"`
    92  	Params string   `json:"params,omitempty"`
    93  }
    94  
    95  // ToDomainUpdateMap builds a update request body from UpdateOpts.
    96  func (opts UpdateOpts) ToDomainUpdateMap() (map[string]interface{}, error) {
    97  	return golangsdk.BuildRequestBody(opts, "")
    98  }
    99  
   100  // Update accepts a UpdateOpts struct and uses the values to update a Domain.The response code from api is 200
   101  func Update(c *golangsdk.ServiceClient, domainID string, opts UpdateOptsBuilder) (r UpdateResult) {
   102  	b, err := opts.ToDomainUpdateMap()
   103  	if err != nil {
   104  		r.Err = err
   105  		return
   106  	}
   107  	reqOpt := &golangsdk.RequestOpts{OkCodes: []int{200}}
   108  	_, r.Err = c.Put(resourceURL(c, domainID), b, nil, reqOpt)
   109  	return
   110  }
   111  
   112  // Get retrieves a particular Domain based on its unique ID.
   113  func Get(c *golangsdk.ServiceClient, id string) (r GetResult) {
   114  	reqOpt := &golangsdk.RequestOpts{
   115  		OkCodes:     []int{200},
   116  		MoreHeaders: RequestOpts.MoreHeaders,
   117  	}
   118  	_, r.Err = c.Get(resourceURL(c, id), &r.Body, reqOpt)
   119  	return
   120  }
   121  
   122  // DeleteOptsBuilder allows extensions to add additional parameters to the
   123  // delete request.
   124  type DeleteOptsBuilder interface {
   125  	ToDeleteQuery() (string, error)
   126  }
   127  
   128  // DeleteOpts contains all the values needed to delete a domain.
   129  type DeleteOpts struct {
   130  	// KeepPolicy specifies whether to retain the policy when deleting a domain name
   131  	// the default value is false
   132  	KeepPolicy bool `q:"keepPolicy"`
   133  }
   134  
   135  // ToDeleteQuery builds a delete request body from DeleteOpts.
   136  func (opts DeleteOpts) ToDeleteQuery() (string, error) {
   137  	q, err := golangsdk.BuildQueryString(opts)
   138  	return q.String(), err
   139  }
   140  
   141  // Delete will permanently delete a particular Domain based on its unique ID.
   142  func Delete(c *golangsdk.ServiceClient, id string, opts DeleteOptsBuilder) (r DeleteResult) {
   143  	url := resourceURL(c, id)
   144  	if opts != nil {
   145  		var query string
   146  		query, r.Err = opts.ToDeleteQuery()
   147  		if r.Err != nil {
   148  			return
   149  		}
   150  		url += query
   151  	}
   152  
   153  	reqOpt := &golangsdk.RequestOpts{
   154  		OkCodes:     []int{200},
   155  		MoreHeaders: RequestOpts.MoreHeaders,
   156  	}
   157  	_, r.Err = c.Delete(url, reqOpt)
   158  	return
   159  }