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

     1  package securitygroups
     2  
     3  import (
     4  	"github.com/huaweicloud/golangsdk"
     5  	"github.com/huaweicloud/golangsdk/pagination"
     6  )
     7  
     8  type CreateOpts struct {
     9  
    10  	// Specifies the security group name.
    11  	Name string `json:"name" required:"true"`
    12  
    13  	// Specifies the enterprise project ID. This field can be used to
    14  	// filter out the VPCs associated with a specified enterprise project.
    15  	EnterpriseProjectId string `json:"enterprise_project_id,omitempty"`
    16  
    17  	// Specifies the resource ID of the VPC to which the security
    18  	// group belongs.
    19  	VpcId string `json:"vpc_id,omitempty"`
    20  
    21  	// Specifies the default security group rule, which ensures that
    22  	//// hosts in the security group can communicate with one another.
    23  	//SecurityGroupRules []SecurityGroupRule `json:"security_group_rules"`
    24  
    25  	Description string `json:"description,omitempty"`
    26  }
    27  
    28  type CreateOptsBuilder interface {
    29  	ToSecuritygroupsCreateMap() (map[string]interface{}, error)
    30  }
    31  
    32  func (opts CreateOpts) ToSecuritygroupsCreateMap() (map[string]interface{}, error) {
    33  	b, err := golangsdk.BuildRequestBody(&opts, "security_group")
    34  	if err != nil {
    35  		return nil, err
    36  	}
    37  	return b, nil
    38  }
    39  
    40  func Create(client *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) {
    41  	b, err := opts.ToSecuritygroupsCreateMap()
    42  	if err != nil {
    43  		r.Err = err
    44  		return
    45  	}
    46  
    47  	_, r.Err = client.Post(CreateURL(client), b, &r.Body, &golangsdk.RequestOpts{
    48  		OkCodes: []int{200},
    49  	})
    50  	return
    51  }
    52  
    53  func Delete(client *golangsdk.ServiceClient, securityGroupId string) (r DeleteResult) {
    54  	url := DeleteURL(client, securityGroupId)
    55  	_, r.Err = client.Delete(url, nil)
    56  	return
    57  }
    58  
    59  func Get(client *golangsdk.ServiceClient, securityGroupId string) (r GetResult) {
    60  	url := GetURL(client, securityGroupId)
    61  	_, r.Err = client.Get(url, &r.Body, &golangsdk.RequestOpts{
    62  		OkCodes: []int{200},
    63  	})
    64  	return
    65  }
    66  
    67  type ListOpts struct {
    68  
    69  	// Specifies the resource ID of pagination query. If the parameter
    70  	// is left blank, only resources on the first page are queried.
    71  	Marker string `q:"marker"`
    72  
    73  	// Specifies the number of records returned on each page.
    74  	Limit int `q:"limit"`
    75  
    76  	// Specifies the VPC ID used as the query filter.
    77  	VpcId string `q:"vpc_id"`
    78  
    79  	// enterprise_project_id
    80  	// Specifies the enterprise_project_id used as the query filter.
    81  	EnterpriseProjectId string `q:"enterprise_project_id"`
    82  }
    83  
    84  type ListOptsBuilder interface {
    85  	ToListQuery() (string, error)
    86  }
    87  
    88  func (opts ListOpts) ToListQuery() (string, error) {
    89  	q, err := golangsdk.BuildQueryString(opts)
    90  	return q.String(), err
    91  }
    92  
    93  func List(client *golangsdk.ServiceClient, opts ListOptsBuilder) pagination.Pager {
    94  	url := ListURL(client)
    95  	if opts != nil {
    96  		query, err := opts.ToListQuery()
    97  		if err != nil {
    98  			return pagination.Pager{Err: err}
    99  		}
   100  		url += query
   101  	}
   102  
   103  	return pagination.NewPager(client, url,
   104  		func(r pagination.PageResult) pagination.Page {
   105  			return SecurityGroupPage{pagination.LinkedPageBase{PageResult: r}}
   106  
   107  		})
   108  }