github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/iec/v1/security/groups/requests.go (about)

     1  package groups
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"github.com/huaweicloud/golangsdk"
     7  )
     8  
     9  type CreateOpts struct {
    10  	Name        string `json:"name,true"`
    11  	Description string `json:"description,omitempty"`
    12  }
    13  
    14  type CreateOptsBuilder interface {
    15  	ToSecurityGroupCreateMap() (map[string]interface{}, error)
    16  }
    17  
    18  func (opts CreateOpts) ToSecurityGroupCreateMap() (map[string]interface{}, error) {
    19  	b, err := golangsdk.BuildRequestBody(&opts, "security_group")
    20  	if err != nil {
    21  		return nil, err
    22  	}
    23  	return b, nil
    24  }
    25  
    26  func Create(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
    27  	b, err := opts.ToSecurityGroupCreateMap()
    28  	if err != nil {
    29  		r.Err = err
    30  		return
    31  	}
    32  
    33  	_, r.Err = client.Post(rootURL(client), b, &r.Body, &golangsdk.RequestOpts{
    34  		OkCodes: []int{http.StatusOK},
    35  	})
    36  	return
    37  }
    38  
    39  func Delete(client *golangsdk.ServiceClient, securityGroupID string) (r DeleteResult) {
    40  	url := DeleteURL(client, securityGroupID)
    41  	_, r.Err = client.Delete(url, nil)
    42  	return
    43  }
    44  
    45  func Get(client *golangsdk.ServiceClient, securityGroupID string) (r GetResult) {
    46  	url := GetURL(client, securityGroupID)
    47  	_, r.Err = client.Get(url, &r.Body, &golangsdk.RequestOpts{
    48  		OkCodes: []int{http.StatusOK},
    49  	})
    50  	return
    51  }
    52  
    53  type ListOpts struct {
    54  	Limit  int `q:"limit"`
    55  	Offset int `q:"offset"`
    56  }
    57  
    58  type ListSecurityGroupsOptsBuilder interface {
    59  	ToListSecurityGroupsQuery() (string, error)
    60  }
    61  
    62  func (opts ListOpts) ToListSecurityGroupsQuery() (string, error) {
    63  	b, err := golangsdk.BuildQueryString(&opts)
    64  	if err != nil {
    65  		return "", err
    66  	}
    67  	return b.String(), nil
    68  }
    69  
    70  func List(client *golangsdk.ServiceClient, opts ListSecurityGroupsOptsBuilder) (r ListResult) {
    71  	listURL := rootURL(client)
    72  	if opts != nil {
    73  		query, err := opts.ToListSecurityGroupsQuery()
    74  		if err != nil {
    75  			r.Err = err
    76  			return r
    77  		}
    78  		listURL += query
    79  	}
    80  
    81  	_, r.Err = client.Get(listURL, &r.Body, &golangsdk.RequestOpts{
    82  		OkCodes: []int{http.StatusOK},
    83  	})
    84  	return
    85  }