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

     1  /*
     2   Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved.
     3  */
     4  
     5  package premium_domains
     6  
     7  import "github.com/huaweicloud/golangsdk"
     8  
     9  var RequestOpts = golangsdk.RequestOpts{
    10  	MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"},
    11  }
    12  
    13  // CreatePremiumHostOpts the options for creating premium domains.
    14  type CreateOpts struct {
    15  	CertificateId   string   `json:"certificateid,omitempty"`
    16  	CertificateName string   `json:"certificatename,omitempty"`
    17  	HostName        string   `json:"hostname" required:"true"`
    18  	Proxy           *bool    `json:"proxy,omitempty"`
    19  	PolicyId        string   `json:"policyid,omitempty"`
    20  	Servers         []Server `json:"server,omitempty"`
    21  }
    22  
    23  // PremiumDomainServer the options of domain server for creating premium domains.
    24  type Server struct {
    25  	FrontProtocol string `json:"front_protocol" required:"true"`
    26  	BackProtocol  string `json:"back_protocol" required:"true"`
    27  	Address       string `json:"address" required:"true"`
    28  	Port          int    `json:"port" required:"true"`
    29  	Type          string `json:"type,omitempty"`
    30  	VpcId         string `json:"vpc_id,omitempty"`
    31  }
    32  
    33  // Create create a premium domain in HuaweiCloud.
    34  func Create(c *golangsdk.ServiceClient, opts CreateOpts) (*CreatePremiumHostRst, error) {
    35  	b, err := golangsdk.BuildRequestBody(opts, "")
    36  	if err != nil {
    37  		return nil, err
    38  	}
    39  
    40  	var rst golangsdk.Result
    41  	_, err = c.Post(rootURL(c), b, &rst.Body, &golangsdk.RequestOpts{
    42  		OkCodes:     []int{200},
    43  		MoreHeaders: RequestOpts.MoreHeaders,
    44  	})
    45  	if err == nil {
    46  		var r CreatePremiumHostRst
    47  		rst.ExtractInto(&r)
    48  		return &r, nil
    49  	}
    50  	return nil, err
    51  }
    52  
    53  // Get get a premium domain by id.
    54  func Get(c *golangsdk.ServiceClient, hostId string) (*PremiumHost, error) {
    55  	var rst golangsdk.Result
    56  	_, err := c.Get(resourceURL(c, hostId), &rst.Body, &golangsdk.RequestOpts{
    57  		MoreHeaders: RequestOpts.MoreHeaders,
    58  	})
    59  	if err == nil {
    60  		var r PremiumHost
    61  		rst.ExtractInto(&r)
    62  		return &r, nil
    63  	}
    64  	return nil, err
    65  }
    66  
    67  // ListPremiumHostOpts the options for querying a list of premium domains.
    68  type ListPremiumHostOpts struct {
    69  	Page          string `q:"page"`
    70  	PageSize      string `q:"pagesize"`
    71  	HostName      string `q:"hostname"`
    72  	PolicyName    string `q:"policyname"`
    73  	ProtectStatus int    `q:"protect_status"`
    74  }
    75  
    76  // List query a list of premium domains.
    77  func List(c *golangsdk.ServiceClient, opts ListPremiumHostOpts) (*PremiumHostList, error) {
    78  	url := rootURL(c)
    79  	query, err := golangsdk.BuildQueryString(opts)
    80  	if err != nil {
    81  		return nil, err
    82  	}
    83  	url += query.String()
    84  
    85  	var rst golangsdk.Result
    86  	_, err = c.Get(url, &rst.Body, &golangsdk.RequestOpts{
    87  		MoreHeaders: RequestOpts.MoreHeaders,
    88  	})
    89  	if err == nil {
    90  		var r PremiumHostList
    91  		rst.ExtractInto(&r)
    92  		return &r, nil
    93  	}
    94  	return nil, err
    95  }
    96  
    97  // UpdatePremiumHostOpts the options for updating premium domains.
    98  type UpdatePremiumHostOpts struct {
    99  	Proxy           *bool  `json:"proxy,omitempty"`
   100  	CertificateId   string `json:"certificateid,omitempty"`
   101  	CertificateName string `json:"certificatename,omitempty"`
   102  	Tls             string `json:"tls,omitempty"`
   103  	Cipher          string `json:"cipher,omitempty"`
   104  }
   105  
   106  // Update update premium domains according to UpdatePremiumHostOpts.
   107  func Update(c *golangsdk.ServiceClient, hostId string, opts UpdatePremiumHostOpts) (*PremiumHost, error) {
   108  	b, err := golangsdk.BuildRequestBody(opts, "")
   109  	if err != nil {
   110  		return nil, err
   111  	}
   112  
   113  	var rst golangsdk.Result
   114  	_, err = c.Put(resourceURL(c, hostId), b, &rst.Body, &golangsdk.RequestOpts{
   115  		OkCodes:     []int{200},
   116  		MoreHeaders: RequestOpts.MoreHeaders,
   117  	})
   118  	if err == nil {
   119  		var r PremiumHost
   120  		rst.ExtractInto(&r)
   121  		return &r, nil
   122  	}
   123  	return nil, err
   124  }
   125  
   126  // updateProtectStatusOpts the struct for updating the protect status of premium domain.
   127  // Only used in the package.
   128  type updateProtectStatusOpts struct {
   129  	ProtectStatus *int `json:"protect_status" required:"true"`
   130  }
   131  
   132  // UpdateProtectStatus update the protect status of premium domain.
   133  func UpdateProtectStatus(c *golangsdk.ServiceClient, hostId string, protectStatus int) (*PremiumHostProtectStatus, error) {
   134  	opts := updateProtectStatusOpts{
   135  		ProtectStatus: &protectStatus,
   136  	}
   137  
   138  	b, err := golangsdk.BuildRequestBody(opts, "")
   139  	if err != nil {
   140  		return nil, err
   141  	}
   142  
   143  	var rst golangsdk.Result
   144  	_, err = c.Put(protectStatusURL(c, hostId), b, &rst.Body, &golangsdk.RequestOpts{
   145  		OkCodes:     []int{200},
   146  		MoreHeaders: RequestOpts.MoreHeaders,
   147  	})
   148  	if err == nil {
   149  		var r PremiumHostProtectStatus
   150  		rst.ExtractInto(&r)
   151  		return &r, nil
   152  	}
   153  	return nil, err
   154  }
   155  
   156  // deleteOpts whether to keep the premium domain policy when deleting the premium domain.
   157  // Only used in the package.
   158  type deleteOpts struct {
   159  	KeepPolicy bool `q:"keepPolicy"`
   160  }
   161  
   162  // Delete a premium domain by id.
   163  func Delete(c *golangsdk.ServiceClient, hostId string, keepPolicy bool) (*SimplePremiumHost, error) {
   164  	opts := deleteOpts{
   165  		KeepPolicy: keepPolicy,
   166  	}
   167  
   168  	url := resourceURL(c, hostId)
   169  	query, err := golangsdk.BuildQueryString(opts)
   170  	if err != nil {
   171  		return nil, err
   172  	}
   173  	url += query.String()
   174  
   175  	var rst golangsdk.Result
   176  	_, err = c.DeleteWithResponse(url, &rst.Body, &golangsdk.RequestOpts{
   177  		OkCodes:     []int{200},
   178  		MoreHeaders: RequestOpts.MoreHeaders,
   179  	})
   180  	if err == nil {
   181  		var r SimplePremiumHost
   182  		rst.ExtractInto(&r)
   183  		return &r, nil
   184  	}
   185  	return nil, err
   186  }