github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/apigw/v2/apigroups/requests.go (about) 1 package apigroups 2 3 import ( 4 "github.com/huaweicloud/golangsdk" 5 "github.com/huaweicloud/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 }