github.com/chnsz/golangsdk@v0.0.0-20240506093406-85a3fbfa605b/openstack/apigw/dedicated/v2/channels/requests.go (about)

     1  package channels
     2  
     3  import (
     4  	"github.com/chnsz/golangsdk"
     5  	"github.com/chnsz/golangsdk/pagination"
     6  )
     7  
     8  // ChannelOpts is the structure that used to create a new channel.
     9  type ChannelOpts struct {
    10  	// The instance ID to which the channel belongs.
    11  	InstanceId string `json:"-" requried:"true"`
    12  	// Channel name.
    13  	// A channel name can contain 3 to 64 characters, starting with a letter.
    14  	// Only letters, digits, hyphens (-), and underscores (_) are allowed.
    15  	// Chinese characters must be in UTF-8 or Unicode format.
    16  	Name string `json:"name" required:"true"`
    17  	// Host port of the channel.
    18  	// The valid value ranges from 1 to 65535.
    19  	Port int `json:"port" required:"true"`
    20  	// Distribution algorithm.
    21  	// The valid values are as following:
    22  	// + 1: WRR (default)
    23  	// + 2: WLC
    24  	// + 3: SH
    25  	// + 4: URI hashing
    26  	BalanceStrategy int `json:"balance_strategy" required:"true"`
    27  	// Member type of the channel.
    28  	// The valid values are as following:
    29  	// + ip
    30  	// + ecs (default)
    31  	MemberType string `json:"member_type,omitempty"`
    32  	// Channel type.
    33  	// + 2: Server type.
    34  	// + 3: Microservice type.
    35  	Type int `json:"type,omitempty"`
    36  	// Dictionary code of the channel.
    37  	// The value can contain letters, digits, hyphens (-), underscores (_), and periods (.).
    38  	DictCode string `json:"dict_code,omitempty"`
    39  	// Backend server groups of the channel.
    40  	// If omitted, you need to entry an empty array.
    41  	MemberGroups []MemberGroup `json:"member_groups"`
    42  	// Backend server list. Only one backend server is included if the channel type is set to 1.
    43  	// If omitted, you need to entry an empty array.
    44  	Members []MemberInfo `json:"members"`
    45  	// Health check details.
    46  	VpcHealthConfig *VpcHealthConfig `json:"vpc_health_config,omitempty"`
    47  	// Microservice details.
    48  	MicroserviceConfig *MicroserviceConfig `json:"microservice_info,omitempty"`
    49  }
    50  
    51  // MemberGroup is an object that represents the detail of the backend server group.
    52  type MemberGroup struct {
    53  	// Name of the backend server group of the channel.
    54  	Name string `json:"member_group_name" required:"true"`
    55  	// Description of the backend server group.
    56  	Description string `json:"member_group_remark,omitempty"`
    57  	// Weight of the backend server group.
    58  	// If the server group contains servers and a weight has been set for it, the weight is automatically used to
    59  	// assign weights to servers in this group.
    60  	// The value is range from 0 to 100.
    61  	Weight int `json:"member_group_weight,omitempty"`
    62  	// Dictionary code of the backend server group.
    63  	// The value can contain letters, digits, hyphens (-), underscores (_), and periods (.).
    64  	DictCode string `json:"dict_code,omitempty"`
    65  	// Version of the backend server group.
    66  	// This parameter is supported only when the channel type is microservice.
    67  	MicroserviceVersion string `json:"microservice_version,omitempty"`
    68  	// Port of the backend server group.
    69  	// This parameter is supported only when the channel type is microservice.
    70  	// If the port number is 0, all addresses in the backend server group use the original load balancing port to
    71  	// inherit logic.
    72  	// The value is range from 0 to 65535.
    73  	MicroservicePort int `json:"microservice_port,omitempty"`
    74  	// Tags of the backend server group.
    75  	// This parameter is supported only when the channel type is microservice.
    76  	MicroserviceLabels []MicroserviceLabel `json:"microservice_labels,omitempty"`
    77  }
    78  
    79  // MicroserviceLabel is an object that represents a specified microservice label.
    80  type MicroserviceLabel struct {
    81  	// Label name.
    82  	Name string `json:"label_name" required:"true"`
    83  	// Label value.
    84  	Value string `json:"label_value" required:"true"`
    85  }
    86  
    87  // MemberInfo is an object that represents the backend member detail.
    88  type MemberInfo struct {
    89  	// Backend server ID.
    90  	// This parameter is valid when the member type is instance.
    91  	// The value can contain 1 to 64 characters, including letters, digits, hyphens (-), and underscores (_).
    92  	EcsId string `json:"ecs_id,omitempty"`
    93  	// Backend server name, which contains of 1 to 64 characters, including letters, digits, periods (.), hyphens (-)
    94  	// and underscores (_).
    95  	// This parameter is valid when the member type is instance.
    96  	EcsName string `json:"ecs_name,omitempty"`
    97  	// Backend server address.
    98  	// This parameter is valid when the member type is IP address.
    99  	Host string `json:"host,omitempty"`
   100  	// Backend server weight.
   101  	// The higher the weight is, the more requests a cloud server will receive.
   102  	// The weight is only available for the WRR and WLC algorithms.
   103  	// It is valid only when the channel type is set to 2.
   104  	// The valid value ranges from 0 to 100.
   105  	Weight *int `json:"weight,omitempty"`
   106  	// Whether the backend service is a standby node.
   107  	// After you enable this function, the backend service serves as a standby node.
   108  	// It works only when all non-standby nodes are faulty.
   109  	// This function is supported only when your gateway has been upgraded to the corresponding version.
   110  	// If your gateway does not support this function, contact technical support.
   111  	// Defaults to false.
   112  	IsBackup *bool `json:"is_backup,omitempty"`
   113  	// Backend server group name. The server group facilitates backend service address modification.
   114  	GroupName string `json:"group_name,omitempty"`
   115  	// Backend server status.
   116  	// + 1: available
   117  	// + 2: unavailable
   118  	Status int `json:"status,omitempty"`
   119  	// Backend server port.
   120  	// The valid value ranges from 0 to 65535.
   121  	Port *int `json:"port,omitempty"`
   122  }
   123  
   124  // VpcHealthConfig is an object that represents the health check configuration.
   125  type VpcHealthConfig struct {
   126  	// Protocol for performing health checks on backend servers in the channel.
   127  	// The valid values are as following:
   128  	// + TCP
   129  	// + HTTP
   130  	// + HTTPS
   131  	Protocol string `json:"protocol" required:"true"`
   132  	// Healthy threshold, which refers to the number of consecutive successful checks required for a backend server to
   133  	// be considered healthy.
   134  	// The valid value ranges from 1 to 10.
   135  	ThresholdNormal int `json:"threshold_normal" required:"true"`
   136  	// Unhealthy threshold, which refers to the number of consecutive failed checks required for a backend server to be
   137  	// considered unhealthy.
   138  	// The valid value range from 1 to 10.
   139  	ThresholdAbnormal int `json:"threshold_abnormal" required:"true"`
   140  	// Interval between consecutive checks, in second. The value must be greater than the value of timeout.
   141  	// The valid value ranges from 1 to 300.
   142  	TimeInterval int `json:"time_interval" required:"true"`
   143  	// Timeout for determining whether a health check fails, in second.
   144  	// The value must be less than the value of time_interval.
   145  	// The valid value ranges from 1 to 30.
   146  	Timeout int `json:"timeout" required:"true"`
   147  	// Destination path for health checks. This parameter is required if protocol is set to http.
   148  	Path string `json:"path,omitempty"`
   149  	// Request method for health checks.
   150  	// The valid values are as following:
   151  	// + GET (default)
   152  	// + HEAD
   153  	Method string `json:"method,omitempty"`
   154  	// Destination port for health checks. By default, the host port of the channel is used.
   155  	// The valid value ranges from 1 to 65535.
   156  	Port int `json:"port,omitempty"`
   157  	// Response codes for determining a successful HTTP response.
   158  	// The value can be any integer within 100–599 in one of the following formats:
   159  	// + Value, for example, 200.
   160  	// + Multiple values, for example, 200,201,202.
   161  	// + Range, for example, 200-299.
   162  	// + Multiple values and ranges, for example, 201,202,210-299.
   163  	// This parameter is required if protocol is set to http.
   164  	HttpCodes string `json:"http_code,omitempty"`
   165  	// Indicates whether to enable two-way authentication.
   166  	// If this function is enabled, the certificate specified in the backend_client_certificate configuration item of
   167  	// the gateway is used. Defaults to false.
   168  	EnableClientSsl *bool `json:"enable_client_ssl,omitempty"`
   169  	// Health check result.
   170  	// + 1: available
   171  	// + 2: unavailable
   172  	Status int `json:"status,omitempty"`
   173  }
   174  
   175  // MicroserviceConfig is an object that represents the microservice configuration.
   176  type MicroserviceConfig struct {
   177  	// Microservice type.
   178  	// + CSE: CSE microservice registration center.
   179  	// + CCE: Cloud Container Engine (CCE).
   180  	ServiceType string `json:"service_type,omitempty"`
   181  	// CSE microservice details. This parameter is required if service_type is set to CSE.
   182  	CseInfo *MicroserviceCseInfo `json:"cse_info,omitempty"`
   183  	// CCE workload details. This parameter is required if service_type is set to CCE.
   184  	CceInfo *MicroserviceCceInfo `json:"cce_info,omitempty"`
   185  }
   186  
   187  // MicroserviceCseInfo is an object that represents the CSE microservice detail.
   188  type MicroserviceCseInfo struct {
   189  	// Microservice engine ID.
   190  	EngineId string `json:"engine_id" required:"true"`
   191  	// Microservice ID.
   192  	ServiceId string `json:"service_id,omitempty"`
   193  }
   194  
   195  // MicroserviceCceInfo is an object that represents the CCE microservice detail.
   196  type MicroserviceCceInfo struct {
   197  	// CCE cluster ID.
   198  	ClusterId string `json:"cluster_id,omitempty"`
   199  	// CCE namespace.
   200  	Namespace string `json:"namespace,omitempty"`
   201  	// Workload type.
   202  	// + deployment
   203  	// + statefulset
   204  	// + daemonset
   205  	WorkloadType string `json:"workload_type,omitempty"`
   206  	// Application name.
   207  	AppName string `json:"app_name,omitempty"`
   208  	// Service label key. Start with a letter or digit, and use only letters, digits, and these special
   209  	// characters: -_./:(). (1 to 64 characters)
   210  	LabelKey string `json:"label_key,omitempty"`
   211  	// Service label value. Start with a letter, and include only letters, digits, periods (.), hyphens (-),
   212  	// and underscores (_). (1 to 64 characters)
   213  	LabelValue string `json:"label_value,omitempty"`
   214  }
   215  
   216  var requestOpts = golangsdk.RequestOpts{
   217  	MoreHeaders: map[string]string{"Content-Type": "application/json", "X-Language": "en-us"},
   218  }
   219  
   220  // Create is a method used to create a new channel using given parameters.
   221  func Create(client *golangsdk.ServiceClient, opts ChannelOpts) (*Channel, error) {
   222  	b, err := golangsdk.BuildRequestBody(opts, "")
   223  	if err != nil {
   224  		return nil, err
   225  	}
   226  
   227  	var r Channel
   228  	_, err = client.Post(rootURL(client, opts.InstanceId), b, &r, &golangsdk.RequestOpts{
   229  		MoreHeaders: requestOpts.MoreHeaders,
   230  	})
   231  	return &r, err
   232  }
   233  
   234  // Get is a method to obtain an existing channel by its ID and related instance ID.
   235  func Get(client *golangsdk.ServiceClient, instanceId, chanId string) (*Channel, error) {
   236  	var r Channel
   237  	_, err := client.Get(resourceURL(client, instanceId, chanId), &r, &golangsdk.RequestOpts{
   238  		MoreHeaders: requestOpts.MoreHeaders,
   239  	})
   240  	return &r, err
   241  }
   242  
   243  // ListOpts allows to filter list data using given parameters.
   244  type ListOpts struct {
   245  	// The instance ID to which the channel belongs.
   246  	InstanceId string `json:"-" requried:"true"`
   247  	// Channel ID.
   248  	ID string `q:"id"`
   249  	// Channel name.
   250  	Name string `q:"name"`
   251  	// Dictionary code of the backend server group.
   252  	// The value can contain letters, digits, hyphens (-), underscores (_), and periods (.).
   253  	DictCode string `q:"dict_code"`
   254  	// Backend service address. By default, exact match is used. Fuzzy match is not supported.
   255  	MemberHost string `q:"member_host"`
   256  	// Backend server port. The valid value ranges from 0 to 65535.
   257  	MemberPort string `q:"member_port"`
   258  	// The name of the backend server group.
   259  	MemberGroupName string `q:"member_group_name"`
   260  	// The ID of the backend server group.
   261  	MemberGroupId string `q:"member_group_id"`
   262  	// Offset from which the query starts.
   263  	// If the offset is less than 0, the value is automatically converted to 0. Defaults to 0.
   264  	Offset int `q:"offset"`
   265  	// Number of items displayed on each page.
   266  	Limit int `q:"limit"`
   267  	// Parameter name for exact matching(, only parameter 'name' and 'member_group_name' are support yet).
   268  	PreciseSearch string `q:"precise_search"`
   269  }
   270  
   271  // List is a method to obtain an array of one or more channels by query parameters.
   272  func List(client *golangsdk.ServiceClient, instanceId string, opts ListOpts) ([]Channel, error) {
   273  	url := rootURL(client, opts.InstanceId)
   274  	query, err := golangsdk.BuildQueryString(opts)
   275  	if err != nil {
   276  		return nil, err
   277  	}
   278  	url += query.String()
   279  
   280  	pages, err := pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
   281  		p := ChannelPage{pagination.OffsetPageBase{PageResult: r}}
   282  		return p
   283  	}).AllPages()
   284  
   285  	if err != nil {
   286  		return nil, err
   287  	}
   288  	return ExtractChannels(pages)
   289  }
   290  
   291  // Update is a method by which to update an existing channel by request parameters.
   292  func Update(client *golangsdk.ServiceClient, chanId string, opts ChannelOpts) (*Channel, error) {
   293  	b, err := golangsdk.BuildRequestBody(opts, "")
   294  	if err != nil {
   295  		return nil, err
   296  	}
   297  
   298  	var r Channel
   299  	_, err = client.Put(resourceURL(client, opts.InstanceId, chanId), b, &r, &golangsdk.RequestOpts{
   300  		MoreHeaders: requestOpts.MoreHeaders,
   301  	})
   302  	return &r, err
   303  }
   304  
   305  // Delete is a method to delete an existing channel using its ID and related instance ID.
   306  func Delete(client *golangsdk.ServiceClient, instanceId, chanId string) error {
   307  	_, err := client.Delete(resourceURL(client, instanceId, chanId), nil)
   308  	return err
   309  }
   310  
   311  type BackendAddOpts struct {
   312  	// The instance ID to which the channel belongs.
   313  	InstanceId string `json:"-" requried:"true"`
   314  	// Backend server list.
   315  	Members []MemberInfo `json:"members" required:"true"`
   316  }
   317  
   318  // AddBackendServices is a method to add a backend instance to channel.
   319  func AddBackendServices(client *golangsdk.ServiceClient, chanId string, opts BackendAddOpts) ([]MemberInfo, error) {
   320  	b, err := golangsdk.BuildRequestBody(opts, "")
   321  	if err != nil {
   322  		return nil, err
   323  	}
   324  
   325  	var r struct {
   326  		Members []MemberInfo `json:"members"`
   327  	}
   328  	_, err = client.Post(membersURL(client, opts.InstanceId, chanId), b, &r, &golangsdk.RequestOpts{
   329  		MoreHeaders: requestOpts.MoreHeaders,
   330  	})
   331  	return r.Members, err
   332  }
   333  
   334  // BackendListOpts allows to filter list data using given parameters.
   335  type BackendListOpts struct {
   336  	// The instance ID to which the channel belongs.
   337  	InstanceId string `json:"-" requried:"true"`
   338  	// Cloud server name.
   339  	Name string `q:"name"`
   340  	// The name of the backend server group.
   341  	MemberGroupName string `q:"member_group_name"`
   342  	// The ID of the backend server group.
   343  	MemberGroupId string `q:"member_group_id"`
   344  	// Offset from which the query starts.
   345  	// If the offset is less than 0, the value is automatically converted to 0. Default to 0.
   346  	Offset int `q:"offset"`
   347  	// Number of items displayed on each page.
   348  	Limit int `q:"limit"`
   349  	// Parameter name for exact matching(, only parameter 'name' and 'member_group_name' are support yet).
   350  	PreciseSearch string `q:"precise_search"`
   351  }
   352  
   353  // ListBackendServices is a method to obtain an array of one or more backend services by query parameters.
   354  func ListBackendServices(client *golangsdk.ServiceClient, chanId string, opts BackendListOpts) ([]MemberInfo, error) {
   355  	url := membersURL(client, opts.InstanceId, chanId)
   356  	query, err := golangsdk.BuildQueryString(opts)
   357  	if err != nil {
   358  		return nil, err
   359  	}
   360  	url += query.String()
   361  
   362  	pages, err := pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
   363  		p := MemberPage{pagination.OffsetPageBase{PageResult: r}}
   364  		return p
   365  	}).AllPages()
   366  
   367  	if err != nil {
   368  		return nil, err
   369  	}
   370  	return ExtractMembers(pages)
   371  }
   372  
   373  // RemoveBackendService is a method to remove an existing backend instance form channel.
   374  func RemoveBackendService(client *golangsdk.ServiceClient, instanceId, chanId, memberId string) error {
   375  	_, err := client.Delete(memberURL(client, instanceId, chanId, memberId), nil)
   376  	return err
   377  }