github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/networking/v2/extensions/security/groups/requests.go (about) 1 package groups 2 3 import ( 4 "github.com/huaweicloud/golangsdk" 5 "github.com/huaweicloud/golangsdk/pagination" 6 ) 7 8 // ListOpts allows the filtering and sorting of paginated collections through 9 // the API. Filtering is achieved by passing in struct field values that map to 10 // the group attributes you want to see returned. SortKey allows you to 11 // sort by a particular network attribute. SortDir sets the direction, and is 12 // either `asc' or `desc'. Marker and Limit are used for pagination. 13 type ListOpts struct { 14 ID string `q:"id"` 15 Name string `q:"name"` 16 TenantID string `q:"tenant_id"` 17 ProjectID string `q:"project_id"` 18 Limit int `q:"limit"` 19 Marker string `q:"marker"` 20 SortKey string `q:"sort_key"` 21 SortDir string `q:"sort_dir"` 22 } 23 24 // List returns a Pager which allows you to iterate over a collection of 25 // security groups. It accepts a ListOpts struct, which allows you to filter 26 // and sort the returned collection for greater efficiency. 27 func List(c *golangsdk.ServiceClient, opts ListOpts) pagination.Pager { 28 q, err := golangsdk.BuildQueryString(&opts) 29 if err != nil { 30 return pagination.Pager{Err: err} 31 } 32 u := rootURL(c) + q.String() 33 return pagination.NewPager(c, u, func(r pagination.PageResult) pagination.Page { 34 return SecGroupPage{pagination.LinkedPageBase{PageResult: r}} 35 }) 36 } 37 38 // CreateOptsBuilder allows extensions to add additional parameters to the 39 // Create request. 40 type CreateOptsBuilder interface { 41 ToSecGroupCreateMap() (map[string]interface{}, error) 42 } 43 44 // CreateOpts contains all the values needed to create a new security group. 45 type CreateOpts struct { 46 // Human-readable name for the Security Group. Does not have to be unique. 47 Name string `json:"name" required:"true"` 48 49 // TenantID is the UUID of the project who owns the Group. 50 // Only administrative users can specify a tenant UUID other than their own. 51 TenantID string `json:"tenant_id,omitempty"` 52 53 // ProjectID is the UUID of the project who owns the Group. 54 // Only administrative users can specify a tenant UUID other than their own. 55 ProjectID string `json:"project_id,omitempty"` 56 57 // Describes the security group. 58 Description string `json:"description,omitempty"` 59 } 60 61 // ToSecGroupCreateMap builds a request body from CreateOpts. 62 func (opts CreateOpts) ToSecGroupCreateMap() (map[string]interface{}, error) { 63 return golangsdk.BuildRequestBody(opts, "security_group") 64 } 65 66 // Create is an operation which provisions a new security group with default 67 // security group rules for the IPv4 and IPv6 ether types. 68 func Create(c *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { 69 b, err := opts.ToSecGroupCreateMap() 70 if err != nil { 71 r.Err = err 72 return 73 } 74 _, r.Err = c.Post(rootURL(c), b, &r.Body, nil) 75 return 76 } 77 78 // UpdateOptsBuilder allows extensions to add additional parameters to the 79 // Update request. 80 type UpdateOptsBuilder interface { 81 ToSecGroupUpdateMap() (map[string]interface{}, error) 82 } 83 84 // UpdateOpts contains all the values needed to update an existing security 85 // group. 86 type UpdateOpts struct { 87 // Human-readable name for the Security Group. Does not have to be unique. 88 Name string `json:"name,omitempty"` 89 90 // Describes the security group. 91 Description *string `json:"description,omitempty"` 92 } 93 94 // ToSecGroupUpdateMap builds a request body from UpdateOpts. 95 func (opts UpdateOpts) ToSecGroupUpdateMap() (map[string]interface{}, error) { 96 return golangsdk.BuildRequestBody(opts, "security_group") 97 } 98 99 // Update is an operation which updates an existing security group. 100 func Update(c *golangsdk.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) { 101 b, err := opts.ToSecGroupUpdateMap() 102 if err != nil { 103 r.Err = err 104 return 105 } 106 107 _, r.Err = c.Put(resourceURL(c, id), b, &r.Body, &golangsdk.RequestOpts{ 108 OkCodes: []int{200}, 109 }) 110 return 111 } 112 113 // Get retrieves a particular security group based on its unique ID. 114 func Get(c *golangsdk.ServiceClient, id string) (r GetResult) { 115 _, r.Err = c.Get(resourceURL(c, id), &r.Body, nil) 116 return 117 } 118 119 // Delete will permanently delete a particular security group based on its 120 // unique ID. 121 func Delete(c *golangsdk.ServiceClient, id string) (r DeleteResult) { 122 _, r.Err = c.Delete(resourceURL(c, id), nil) 123 return 124 } 125 126 // IDFromName is a convenience function that returns a security group's ID, 127 // given its name. 128 func IDFromName(client *golangsdk.ServiceClient, name string) (string, error) { 129 count := 0 130 id := "" 131 132 listOpts := ListOpts{ 133 Name: name, 134 } 135 136 pages, err := List(client, listOpts).AllPages() 137 if err != nil { 138 return "", err 139 } 140 141 all, err := ExtractGroups(pages) 142 if err != nil { 143 return "", err 144 } 145 146 for _, s := range all { 147 if s.Name == name { 148 count++ 149 id = s.ID 150 } 151 } 152 153 switch count { 154 case 0: 155 return "", golangsdk.ErrResourceNotFound{Name: name, ResourceType: "security group"} 156 case 1: 157 return id, nil 158 default: 159 return "", golangsdk.ErrMultipleResourcesFound{Name: name, Count: count, ResourceType: "security group"} 160 } 161 }