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

     1  package apigroups
     2  
     3  import (
     4  	"github.com/chnsz/golangsdk"
     5  	"github.com/chnsz/golangsdk/pagination"
     6  )
     7  
     8  // GroupOpts allows to create a group or update a existing group using given parameters.
     9  type GroupOpts struct {
    10  	// API group name, which can contain 3 to 64 characters, starting with a letter.
    11  	// Only letters, digits and underscores (_) are allowed.
    12  	// Chinese characters must be in UTF-8 or Unicode format.
    13  	Name string `json:"name" required:"true"`
    14  	// Description of the API group, which can contain a maximum of 255 characters,
    15  	// and the angle brackets (< and >) are not allowed.
    16  	// Chinese characters must be in UTF-8 or Unicode format.
    17  	Description *string `json:"remark,omitempty"`
    18  }
    19  
    20  type CreateOptsBuilder interface {
    21  	ToCreateOptsMap() (map[string]interface{}, error)
    22  }
    23  
    24  func (opts GroupOpts) ToCreateOptsMap() (map[string]interface{}, error) {
    25  	return golangsdk.BuildRequestBody(opts, "")
    26  }
    27  
    28  // Create is a method by which to create function that create a new group.
    29  func Create(client *golangsdk.ServiceClient, instanceId string, opts CreateOptsBuilder) (r CreateResult) {
    30  	reqBody, err := opts.ToCreateOptsMap()
    31  	if err != nil {
    32  		r.Err = err
    33  		return
    34  	}
    35  	_, r.Err = client.Post(rootURL(client, instanceId), reqBody, &r.Body, nil)
    36  	return
    37  }
    38  
    39  // Update is a method by which to create function that udpate a existing group.
    40  func Update(client *golangsdk.ServiceClient, instanceId, groupId string, opts CreateOptsBuilder) (r UpdateResult) {
    41  	reqBody, err := opts.ToCreateOptsMap()
    42  	if err != nil {
    43  		r.Err = err
    44  		return
    45  	}
    46  	_, r.Err = client.Put(resourceURL(client, instanceId, groupId), reqBody, &r.Body, &golangsdk.RequestOpts{
    47  		OkCodes: []int{200},
    48  	})
    49  	return
    50  }
    51  
    52  // Get is a method to obtain the specified group according to the instanceId and appId.
    53  func Get(client *golangsdk.ServiceClient, instanceId, groupId string) (r GetResult) {
    54  	_, r.Err = client.Get(resourceURL(client, instanceId, groupId), &r.Body, nil)
    55  	return
    56  }
    57  
    58  // ListOpts allows to filter list data using given parameters.
    59  type ListOpts struct {
    60  	// API group ID.
    61  	Id string `q:"id"`
    62  	// API group name.
    63  	Name string `q:"name"`
    64  	// Offset from which the query starts.
    65  	// If the offset is less than 0, the value is automatically converted to 0. Default to 0.
    66  	Offset int `q:"offset"`
    67  	// Number of items displayed on each page. The valid values are range form 1 to 500, default to 20.
    68  	Limit int `q:"limit"`
    69  	// Parameter name for exact matching. Only API group names are supported.
    70  	PreciseSearch string `q:"precise_search"`
    71  }
    72  
    73  type ListOptsBuilder interface {
    74  	ToListQuery() (string, error)
    75  }
    76  
    77  func (opts ListOpts) ToListQuery() (string, error) {
    78  	q, err := golangsdk.BuildQueryString(opts)
    79  	if err != nil {
    80  		return "", err
    81  	}
    82  	return q.String(), err
    83  }
    84  
    85  // List is a method to obtain an array of one or more groups according to the query parameters.
    86  func List(client *golangsdk.ServiceClient, instanceId string, opts ListOptsBuilder) pagination.Pager {
    87  	url := rootURL(client, instanceId)
    88  	if opts != nil {
    89  		query, err := opts.ToListQuery()
    90  		if err != nil {
    91  			return pagination.Pager{Err: err}
    92  		}
    93  		url += query
    94  	}
    95  
    96  	return pagination.NewPager(client, url, func(r pagination.PageResult) pagination.Page {
    97  		return GroupPage{pagination.SinglePageBase(r)}
    98  	})
    99  }
   100  
   101  // Delete is a method to delete an existing group.
   102  func Delete(client *golangsdk.ServiceClient, instanceId, groupId string) (r DeleteResult) {
   103  	_, r.Err = client.Delete(resourceURL(client, instanceId, groupId), nil)
   104  	return
   105  }
   106  
   107  // AssociateDomainOpts is the structure that used to bind domain name to specified API group.
   108  type AssociateDomainOpts struct {
   109  	// The dedicated instance ID.
   110  	InstanceId string `json:"-" requires:"true"`
   111  	// The API group ID.
   112  	GroupId string `json:"-" requires:"true"`
   113  	// Custom domain name.
   114  	// The valid length is limited from 0 to 255 characters and must comply with the domian name specifications.
   115  	UrlDomain string `json:"url_domain" requires:"true"`
   116  	// The minimum TLS version that can be used to access the domain name, the default value is TLSv1.2.
   117  	// The valid value are as follows:
   118  	// + TLSv1.1.
   119  	// + TLSv1.2.
   120  	MinSSLVersion string `json:"min_ssl_version,omitempty"`
   121  	// Whether to enable redirection from HTTP to HTTPS, the default value is false.
   122  	IsHttpRedirectToHttps bool `json:"is_http_redirect_to_https,omitempty"`
   123  }
   124  
   125  // AssociateDomain is a method that used to bind domain name to specified API group.
   126  func AssociateDomain(client *golangsdk.ServiceClient, opts AssociateDomainOpts) (*AssociateDoaminResp, error) {
   127  	b, err := golangsdk.BuildRequestBody(opts, "")
   128  	if err != nil {
   129  		return nil, err
   130  	}
   131  
   132  	var r AssociateDoaminResp
   133  	_, err = client.Post(associateDomainURL(client, opts.InstanceId, opts.GroupId), b, &r, nil)
   134  	return &r, err
   135  }
   136  
   137  // AssociateDomain is a method that used to unbind domain name to specified API group.
   138  func DisAssociateDomain(client *golangsdk.ServiceClient, intanceId string, groupId string, domainId string) error {
   139  	_, err := client.Delete(disAssociateDomainURL(client, intanceId, groupId, domainId), nil)
   140  	return err
   141  }