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

     1  /*
     2   Copyright (c) Huawei Technologies Co. Ltd. 2021. All rights reserved.
     3  */
     4  
     5  package premium_instances
     6  
     7  import "github.com/huaweicloud/golangsdk"
     8  
     9  // requestOpts defines the request header and language.
    10  var requestOpts = golangsdk.RequestOpts{
    11  	MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"},
    12  }
    13  
    14  // CreateInstanceOpts the parameters in the creating request body
    15  type CreateInstanceOpts struct {
    16  	Region        string   `json:"region" required:"true"`
    17  	ChargeMode    int      `json:"chargemode" required:"true"`
    18  	AvailableZone string   `json:"available_zone" required:"true"`
    19  	Arch          string   `json:"arch" required:"true"`
    20  	NamePrefix    string   `json:"instancename" required:"true"`
    21  	Specification string   `json:"specification" required:"true"`
    22  	CpuFlavor     string   `json:"cpu_flavor" required:"true"`
    23  	VpcId         string   `json:"vpc_id" required:"true"`
    24  	SubnetId      string   `json:"subnet_id" required:"true"`
    25  	SecurityGroup []string `json:"security_group" required:"true"`
    26  	Count         int      `json:"count" required:"true"`
    27  	Ipv6Enable    string   `json:"ipv6_enable,omitempty"`
    28  	VolumeType    string   `json:"volume_type,omitempty"`
    29  	ClusterId     string   `json:"cluster_id,omitempty"`
    30  	PoolId        string   `json:"pool_id,omitempty"`
    31  }
    32  
    33  // ListInstanceOpts the parameters in the querying request.
    34  type ListInstanceOpts struct {
    35  	Page         int    `q:"page"`
    36  	PageSize     int    `q:"pagesize"`
    37  	InstanceName string `q:"instancename"`
    38  }
    39  
    40  // UpdateInstanceOpts the parameters in the updating request.
    41  type UpdateInstanceOpts struct {
    42  	InstanceName string `json:"instancename"`
    43  }
    44  
    45  // CreateInstance will create a dedicated waf instance based on the values in CreateOpts.
    46  func CreateInstance(c *golangsdk.ServiceClient, opts CreateInstanceOpts) (*CreationgRst, error) {
    47  	b, err := golangsdk.BuildRequestBody(opts, "")
    48  	if err != nil {
    49  		return nil, err
    50  	}
    51  
    52  	var rst golangsdk.Result
    53  	_, err = c.Post(rootURL(c), b, &rst.Body, &golangsdk.RequestOpts{
    54  		MoreHeaders: requestOpts.MoreHeaders,
    55  	})
    56  
    57  	if err == nil {
    58  		var r CreationgRst
    59  		rst.ExtractInto(&r)
    60  		return &r, nil
    61  	}
    62  	return nil, err
    63  }
    64  
    65  // GetInstance get the waf instance detail.
    66  func GetInstance(c *golangsdk.ServiceClient, id string) (*DedicatedInstance, error) {
    67  	var rst golangsdk.Result
    68  	_, err := c.Get(resourceURL(c, id), &rst.Body, &golangsdk.RequestOpts{
    69  		MoreHeaders: requestOpts.MoreHeaders,
    70  	})
    71  
    72  	if err == nil {
    73  		var r DedicatedInstance
    74  		rst.ExtractInto(&r)
    75  		return &r, nil
    76  	}
    77  	return nil, err
    78  }
    79  
    80  // ListInstance query a list of waf instance base on ListInstanceOpts
    81  func ListInstance(c *golangsdk.ServiceClient, opts ListInstanceOpts) (*DedicatedInstanceList, error) {
    82  	url := rootURL(c)
    83  	query, err := golangsdk.BuildQueryString(opts)
    84  	if err != nil {
    85  		return nil, err
    86  	}
    87  	url += query.String()
    88  
    89  	var rst golangsdk.Result
    90  	_, err = c.Get(url, &rst.Body, &golangsdk.RequestOpts{
    91  		MoreHeaders: requestOpts.MoreHeaders,
    92  	})
    93  
    94  	if err == nil {
    95  		var r DedicatedInstanceList
    96  		rst.ExtractInto(&r)
    97  		return &r, nil
    98  	}
    99  	return nil, err
   100  }
   101  
   102  // UpdateInstance query a list of waf instance base on UpdateInstanceOpts
   103  func UpdateInstance(c *golangsdk.ServiceClient, id string, opts UpdateInstanceOpts) (*DedicatedInstance, error) {
   104  	b, err := golangsdk.BuildRequestBody(opts, "")
   105  	if err != nil {
   106  		return nil, err
   107  	}
   108  
   109  	var rst golangsdk.Result
   110  	_, err = c.Put(resourceURL(c, id), b, &rst.Body, &golangsdk.RequestOpts{
   111  		OkCodes:     []int{200},
   112  		MoreHeaders: requestOpts.MoreHeaders,
   113  	})
   114  
   115  	if err == nil {
   116  		var r DedicatedInstance
   117  		rst.ExtractInto(&r)
   118  		return &r, nil
   119  	}
   120  	return nil, err
   121  }
   122  
   123  // Delete will permanently delete a engine based on its unique ID.
   124  func Delete(c *golangsdk.ServiceClient, id string) (*DedicatedInstance, error) {
   125  	var rst golangsdk.Result
   126  	_, err := c.DeleteWithResponse(resourceURL(c, id), &rst.Body, &golangsdk.RequestOpts{
   127  		OkCodes:     []int{200},
   128  		MoreHeaders: requestOpts.MoreHeaders},
   129  	)
   130  
   131  	if err == nil {
   132  		var r DedicatedInstance
   133  		rst.ExtractInto(&r)
   134  		return &r, nil
   135  	}
   136  	return nil, err
   137  }