github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/apigw/v2/channels/requests.go (about)

     1  package channels
     2  
     3  import (
     4  	"github.com/huaweicloud/golangsdk"
     5  	"github.com/huaweicloud/golangsdk/pagination"
     6  )
     7  
     8  type ChannelOpts struct {
     9  	// Backend server list. Only one backend server is included if the VPC channel type is set to 1.
    10  	Members []MemberInfo `json:"members" required:"true"`
    11  	// VPC channel name.
    12  	// A VPC channel name can contain 3 to 64 characters, starting with a letter.
    13  	// Only letters, digits, hyphens (-), and underscores (_) are allowed.
    14  	// Chinese characters must be in UTF-8 or Unicode format.
    15  	Name string `json:"name" required:"true"`
    16  	// VPC channel type.
    17  	//     1: private network ELB channel (to be deprecated)
    18  	//     2: fast channel with the load balancing function
    19  	Type int `json:"type" required:"true"`
    20  	// Health check details.
    21  	VpcHealthConfig VpcHealthConfig `json:"vpc_health_config" required:"true"`
    22  	// Distribution algorithm.
    23  	// The valid values are as following:
    24  	//     1: WRR (default)
    25  	//     2: WLC
    26  	//     3: SH
    27  	//     4: URI hashing
    28  	// This parameter is mandatory if the VPC channel type is set to 2.
    29  	BalanceStrategy int `json:"balance_strategy,omitempty"`
    30  	// Member type of the VPC channel.
    31  	// The valid values are as following:
    32  	//     ip
    33  	//     ecs (default)
    34  	// This parameter is required if the VPC channel type is set to 2.
    35  	MemberType string `json:"member_type,omitempty"`
    36  	// Host port of the VPC channel.
    37  	// This parameter is valid only when the VPC channel type is set to 2. The value range is 1–65535.
    38  	// This parameter is required if the VPC channel type is set to 2.
    39  	Port int `json:"port,omitempty"`
    40  }
    41  
    42  type MemberInfo struct {
    43  	// Backend server ID.
    44  	// This parameter is valid when the member type is instance.
    45  	// The value can contain 1 to 64 characters, including letters, digits, hyphens (-), and underscores (_).
    46  	EcsId string `json:"ecs_id,omitempty" xor:"Host"`
    47  	// Backend server name, which contains of 1 to 64 characters, including letters, digits, periods (.), hyphens (-)
    48  	// and underscores (_).
    49  	// This parameter is valid when the member type is instance.
    50  	EcsName string `json:"ecs_name,omitempty"`
    51  	// Backend server address.
    52  	// This parameter is valid when the member type is IP address.
    53  	Host string `json:"host,omitempty"`
    54  	// Backend server weight.
    55  	// The higher the weight is, the more requests a cloud server will receive.
    56  	// The weight is only available for the WRR and WLC algorithms.
    57  	// It is valid only when the VPC channel type is set to 2.
    58  	// The valid value is range from 0 to 100.
    59  	Weight int `json:"weight,omitempty"`
    60  }
    61  
    62  type VpcHealthConfig struct {
    63  	// Protocol for performing health checks on backend servers in the VPC channel.
    64  	// The valid values are as following:
    65  	//     TCP
    66  	//     HTTP
    67  	//     HTTPS
    68  	Protocol string `json:"protocol" required:"true"`
    69  	// Healthy threshold, which refers to the number of consecutive successful checks required for a backend server to
    70  	// be considered healthy.
    71  	// The valid value is range from 2 to 10.
    72  	ThresholdNormal int `json:"threshold_normal" required:"true"`
    73  	// Unhealthy threshold, which refers to the number of consecutive failed checks required for a backend server to be
    74  	// considered unhealthy.
    75  	// The valid value is range from 2 to 10.
    76  	ThresholdAbnormal int `json:"threshold_abnormal" required:"true"`
    77  	// Interval between consecutive checks, in second. The value must be greater than the value of timeout.
    78  	// The valid value is range from 5 to 300.
    79  	TimeInterval int `json:"time_interval" required:"true"`
    80  	// Timeout for determining whether a health check fails, in second.
    81  	// The value must be less than the value of time_interval.
    82  	// The valid value is range from 2 to 30.
    83  	Timeout int `json:"timeout" required:"true"`
    84  	// Indicates whether to enable two-way authentication.
    85  	// If this function is enabled, the certificate specified in the backend_client_certificate configuration item of
    86  	// the gateway is used. Default to false.
    87  	EnableClientSsl bool `json:"enable_client_ssl,omitempty"`
    88  	// Response codes for determining a successful HTTP response.
    89  	// The value can be any integer within 100–599 in one of the following formats:
    90  	//     Value, for example, 200.
    91  	//     Multiple values, for example, 200,201,202.
    92  	//     Range, for example, 200-299.
    93  	//     Multiple values and ranges, for example, 201,202,210-299.
    94  	// This parameter is required if protocol is set to http.
    95  	HttpCodes string `json:"http_code,omitempty"`
    96  	// Request method for health checks.
    97  	// The valid values are as following:
    98  	//     GET (default)
    99  	//     HEAD
   100  	Method string `json:"method,omitempty"`
   101  	// Destination path for health checks. This parameter is required if protocol is set to http.
   102  	Path string `json:"path,omitempty"`
   103  	// Destination port for health checks. By default, the host port of the VPC channel is used.
   104  	// The valid value is range from 1 to 65535.
   105  	Port int `json:"port,omitempty"`
   106  }
   107  
   108  type ChannelOptsBuilder interface {
   109  	ToInstanceCreateMap() (map[string]interface{}, error)
   110  }
   111  
   112  func (opts ChannelOpts) ToInstanceCreateMap() (map[string]interface{}, error) {
   113  	return golangsdk.BuildRequestBody(opts, "")
   114  }
   115  
   116  // Create is a method by which to create an APIG vpc channel.
   117  func Create(client *golangsdk.ServiceClient, instanceId string, opts ChannelOptsBuilder) (r CreateResult) {
   118  	reqBody, err := opts.ToInstanceCreateMap()
   119  	if err != nil {
   120  		r.Err = err
   121  		return
   122  	}
   123  	_, r.Err = client.Post(rootURL(client, instanceId), reqBody, &r.Body, nil)
   124  	return
   125  }
   126  
   127  // Update is a method by which to update an existing APIG vpc channel by request parameters.
   128  func Update(client *golangsdk.ServiceClient, instanceId, chanId string, opts ChannelOptsBuilder) (r UpdateResult) {
   129  	reqBody, err := opts.ToInstanceCreateMap()
   130  	if err != nil {
   131  		r.Err = err
   132  		return
   133  	}
   134  	_, r.Err = client.Put(resourceURL(client, instanceId, chanId), reqBody, &r.Body, &golangsdk.RequestOpts{
   135  		OkCodes: []int{200},
   136  	})
   137  	return
   138  }
   139  
   140  // Get is a method to obtain an existing APIG vpc channel by channel ID.
   141  func Get(client *golangsdk.ServiceClient, instanceId, chanId string) (r GetResult) {
   142  	_, r.Err = client.Get(resourceURL(client, instanceId, chanId), &r.Body, nil)
   143  	return
   144  }
   145  
   146  // ListOpts allows to filter list data using given parameters.
   147  type ListOpts struct {
   148  	// VPC channel ID.
   149  	Id string `q:"id"`
   150  	// VPC channel name.
   151  	Name string `q:"name"`
   152  	// VPC channel type.
   153  	VpcType int `q:"vpc_type"`
   154  	// Offset from which the query starts.
   155  	// If the offset is less than 0, the value is automatically converted to 0. Default to 0.
   156  	Offset int `q:"offset"`
   157  	// Number of items displayed on each page.
   158  	Limit int `q:"limit"`
   159  	// Parameter name (name) for exact matching.
   160  	PreciseSearch string `q:"precise_search"`
   161  }
   162  
   163  type ListOptsBuilder interface {
   164  	ToChannelListQuery() (string, error)
   165  }
   166  
   167  func (opts ListOpts) ToChannelListQuery() (string, error) {
   168  	q, err := golangsdk.BuildQueryString(opts)
   169  	if err != nil {
   170  		return "", err
   171  	}
   172  	return q.String(), err
   173  }
   174  
   175  // List is a method to obtain an array of one or more vpc channels by query parameters.
   176  func List(client *golangsdk.ServiceClient, instanceId string, opts ListOptsBuilder) pagination.Pager {
   177  	url := rootURL(client, instanceId)
   178  	if opts != nil {
   179  		query, err := opts.ToChannelListQuery()
   180  		if err != nil {
   181  			return pagination.Pager{Err: err}
   182  		}
   183  		url += query
   184  	}
   185  
   186  	return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
   187  		return ChannelPage{pagination.SinglePageBase(r)}
   188  	})
   189  }
   190  
   191  // Delete is a method to delete an existing vpc channel.
   192  func Delete(client *golangsdk.ServiceClient, instanceId, chanId string) (r DeleteResult) {
   193  	_, r.Err = client.Delete(resourceURL(client, instanceId, chanId), nil)
   194  	return
   195  }
   196  
   197  type BackendAddOpts struct {
   198  	// Backend server list.
   199  	Members []MemberInfo `json:"members" required:"true"`
   200  }
   201  
   202  type BackendAddOptsBuilder interface {
   203  	ToBackendAddMap() (map[string]interface{}, error)
   204  }
   205  
   206  func (opts BackendAddOpts) ToBackendAddMap() (map[string]interface{}, error) {
   207  	return golangsdk.BuildRequestBody(opts, "")
   208  }
   209  
   210  // AddBackendServices is a method to add a backend instance to vpc channel.
   211  func AddBackendServices(client *golangsdk.ServiceClient, instanceId, chanId string,
   212  	opts BackendAddOptsBuilder) (r CreateResult) {
   213  	reqBody, err := opts.ToBackendAddMap()
   214  	if err != nil {
   215  		r.Err = err
   216  		return
   217  	}
   218  	_, r.Err = client.Post(membersURL(client, instanceId, chanId), reqBody, &r.Body, nil)
   219  	return
   220  }
   221  
   222  // BackendListOpts allows to filter list data using given parameters.
   223  type BackendListOpts struct {
   224  	// Cloud server name.
   225  	Name string `q:"name"`
   226  	// Offset from which the query starts.
   227  	// If the offset is less than 0, the value is automatically converted to 0. Default to 0.
   228  	Offset int `q:"offset"`
   229  	// Number of items displayed on each page.
   230  	Limit int `q:"limit"`
   231  }
   232  
   233  type BackendListOptsBuilder interface {
   234  	ToBackendListQuery() (string, error)
   235  }
   236  
   237  func (opts BackendListOpts) ToBackendListQuery() (string, error) {
   238  	q, err := golangsdk.BuildQueryString(opts)
   239  	if err != nil {
   240  		return "", err
   241  	}
   242  	return q.String(), err
   243  }
   244  
   245  // ListBackendServices is a method to obtain an array of one or more backend services by query parameters.
   246  func ListBackendServices(client *golangsdk.ServiceClient, instanceId, chanId string,
   247  	opts BackendListOptsBuilder) pagination.Pager {
   248  	url := membersURL(client, instanceId, chanId)
   249  	if opts != nil {
   250  		query, err := opts.ToBackendListQuery()
   251  		if err != nil {
   252  			return pagination.Pager{Err: err}
   253  		}
   254  		url += query
   255  	}
   256  
   257  	return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
   258  		return ChannelPage{pagination.SinglePageBase(r)}
   259  	})
   260  }
   261  
   262  // RemoveBackendService is a method to remove an existing backend instance form vpc channel.
   263  func RemoveBackendService(client *golangsdk.ServiceClient, instanceId, chanId, memberId string) (r RemoveResult) {
   264  	_, r.Err = client.Delete(memberURL(client, instanceId, chanId, memberId), nil)
   265  	return
   266  }