github.com/chnsz/golangsdk@v0.0.0-20240506093406-85a3fbfa605b/openstack/networking/v3/security/groups/requests.go (about)

     1  package groups
     2  
     3  import (
     4  	"github.com/chnsz/golangsdk"
     5  	"github.com/chnsz/golangsdk/pagination"
     6  )
     7  
     8  // CreateOpts is a struct which will be used to create a new security group.
     9  type CreateOpts struct {
    10  	// Specifies the security group name. The value can contain 1 to 64 characters, including letters, digits,
    11  	// underscores (_), hyphens (-), and periods (.).
    12  	Name string `json:"name" required:"true"`
    13  	// Specifies the resource ID of the VPC to which the security group belongs.
    14  	// Note: This parameter has been discarded, it is not recommended to use it.
    15  	VpcId string `json:"vpc_id,omitempty"`
    16  	// Enterprise project ID. The default value is 0.
    17  	EnterpriseProjectId string `json:"enterprise_project_id,omitempty"`
    18  }
    19  
    20  // Create is a method to create a new security group.
    21  func Create(c *golangsdk.ServiceClient, opts CreateOpts) (*SecurityGroup, error) {
    22  	b, err := golangsdk.BuildRequestBody(opts, "security_group")
    23  	if err != nil {
    24  		return nil, err
    25  	}
    26  
    27  	var rst golangsdk.Result
    28  	_, err = c.Post(rootURL(c), b, &rst.Body, nil)
    29  	if err == nil {
    30  		var r SecurityGroup
    31  		err := rst.ExtractIntoStructPtr(&r, "security_group")
    32  		return &r, err
    33  	}
    34  	return nil, err
    35  }
    36  
    37  // Get is a method to obtain the security group detail.
    38  func Get(c *golangsdk.ServiceClient, securityGroupId string) (*SecurityGroup, error) {
    39  	var rst golangsdk.Result
    40  	_, err := c.Get(resourceURL(c, securityGroupId), &rst.Body, nil)
    41  	if err == nil {
    42  		var r SecurityGroup
    43  		err = rst.ExtractIntoStructPtr(&r, "security_group")
    44  		return &r, err
    45  	}
    46  	return nil, err
    47  }
    48  
    49  // ListOpts allows to filter list data using given parameters.
    50  type ListOpts struct {
    51  	// Specifies the number of records that will be returned on each page. The value is from 0 to intmax.
    52  	// limit can be used together with marker. For details, see the parameter description of marker.
    53  	Limit int `q:"limit"`
    54  	// Specifies a resource ID for pagination query, indicating that the query starts from the next record of the
    55  	// specified resource ID. This parameter can work together with the parameter limit.
    56  	//   If parameters marker and limit are not passed, all resource records will be returned.
    57  	//   If the parameter marker is not passed and the value of parameter limit is set to 10, the first 10 resource
    58  	//     records will be returned.
    59  	//   If the value of the parameter marker is set to the resource ID of the 10th record and the value of parameter
    60  	//     limit is set to 10, the 11th to 20th resource records will be returned.
    61  	//   If the value of the parameter marker is set to the resource ID of the 10th record and the parameter limit is
    62  	//     not passed, resource records starting from the 11th records (including 11th) will be returned.
    63  	Marker string `q:"marker"`
    64  	// Security group ID. You can use this field to filter security groups precisely, supporting multiple IDs.
    65  	ID string `q:"id"`
    66  	// Security group name. You can use this field to accurately filter security groups that meet the conditions, and
    67  	// support incoming multiple name filters.
    68  	Name string `q:"name"`
    69  	// Security group description. You can use this field to filter security groups precisely, and support multiple
    70  	// descriptions for filtering.
    71  	Description string `q:"description"`
    72  	// Enterprise project ID. Maximum length 36 bytes, UUID format with "-" hyphen, or string "0" (default).
    73  	// If the enterprise project sub-account needs to query all security groups under the enterprise project, please
    74  	// input 'all_granted_eps'.
    75  	EnterpriseProjectId string `q:"enterprise_project_id"`
    76  }
    77  
    78  // List is a method to obtain the list of the security groups.
    79  func List(c *golangsdk.ServiceClient, opts ListOpts) ([]SecurityGroup, error) {
    80  	url := rootURL(c)
    81  	query, err := golangsdk.BuildQueryString(opts)
    82  	if err != nil {
    83  		return nil, err
    84  	}
    85  	url += query.String()
    86  
    87  	pages, err := pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page {
    88  		p := SecurityGroupPage{pagination.MarkerPageBase{PageResult: r}}
    89  		p.MarkerPageBase.Owner = p
    90  		return p
    91  	}).AllPages()
    92  
    93  	if err != nil {
    94  		return nil, err
    95  	}
    96  	return ExtractSecurityGroups(pages)
    97  }
    98  
    99  // UpdateOpts is a struct which will be used to update the existing security group using given parameters.
   100  type UpdateOpts struct {
   101  	// Specifies the security group name. The value can contain 1 to 64 characters, including letters, digits,
   102  	// underscores (_), hyphens (-), and periods (.).
   103  	Name string `json:"name,omitempty"`
   104  	// Specifies the description of the security group.
   105  	// The angle brackets (< and >) are not allowed for the description.
   106  	Description *string `json:"description,omitempty"`
   107  }
   108  
   109  // Update is a method to update an existing security group.
   110  func Update(c *golangsdk.ServiceClient, securityGroupId string, opts UpdateOpts) (*SecurityGroup, error) {
   111  	b, err := golangsdk.BuildRequestBody(opts, "security_group")
   112  	if err != nil {
   113  		return nil, err
   114  	}
   115  
   116  	var rst golangsdk.Result
   117  	_, err = c.Put(resourceURL(c, securityGroupId), b, &rst.Body, nil)
   118  	if err == nil {
   119  		var r SecurityGroup
   120  		err = rst.ExtractIntoStructPtr(&r, "security_group")
   121  		return &r, err
   122  	}
   123  	return nil, err
   124  }
   125  
   126  // Delete is a method to delete an existing security group.
   127  func Delete(c *golangsdk.ServiceClient, securityGroupId string) *golangsdk.ErrResult {
   128  	var r golangsdk.ErrResult
   129  	_, r.Err = c.Delete(resourceURL(c, securityGroupId), nil)
   130  	return &r
   131  }
   132  
   133  // IDFromName is a convenience function that returns a securtiy group's ID, given its name.
   134  func IDFromName(client *golangsdk.ServiceClient, name string) (string, error) {
   135  	var count int
   136  	var id string
   137  	opt := ListOpts{
   138  		Name:                name,
   139  		EnterpriseProjectId: "all_granted_eps",
   140  	}
   141  	secgroupList, err := List(client, opt)
   142  	if err != nil {
   143  		return "", err
   144  	}
   145  	for _, sg := range secgroupList {
   146  		if sg.Name == name {
   147  			count++
   148  			id = sg.ID
   149  		}
   150  	}
   151  
   152  	switch count {
   153  	case 0:
   154  		return "", golangsdk.ErrResourceNotFound{Name: name, ResourceType: "Security Group"}
   155  	case 1:
   156  		return id, nil
   157  	default:
   158  		return "", golangsdk.ErrMultipleResourcesFound{Name: name, Count: count, ResourceType: "Security Group"}
   159  	}
   160  }