github.com/opentelekomcloud/gophertelekomcloud@v0.9.3/openstack/networking/v2/extensions/vpnaas/endpointgroups/requests.go (about) 1 package endpointgroups 2 3 import ( 4 "github.com/opentelekomcloud/gophertelekomcloud" 5 "github.com/opentelekomcloud/gophertelekomcloud/pagination" 6 ) 7 8 type EndpointType string 9 10 const ( 11 TypeSubnet EndpointType = "subnet" 12 TypeCIDR EndpointType = "cidr" 13 TypeVLAN EndpointType = "vlan" 14 TypeNetwork EndpointType = "network" 15 TypeRouter EndpointType = "router" 16 ) 17 18 // CreateOptsBuilder allows extensions to add additional parameters to the 19 // Create request. 20 type CreateOptsBuilder interface { 21 ToEndpointGroupCreateMap() (map[string]interface{}, error) 22 } 23 24 // CreateOpts contains all the values needed to create a new endpoint group 25 type CreateOpts struct { 26 // TenantID specifies a tenant to own the endpoint group. The caller must have 27 // an admin role in order to set this. Otherwise, this field is left unset 28 // and the caller will be the owner. 29 TenantID string `json:"tenant_id,omitempty"` 30 31 // Description is the human readable description of the endpoint group. 32 Description string `json:"description,omitempty"` 33 34 // Name is the human readable name of the endpoint group. 35 Name string `json:"name,omitempty"` 36 37 // The type of the endpoints in the group. 38 // A valid value is subnet, cidr, network, router, or vlan. 39 Type EndpointType `json:"type,omitempty"` 40 41 // List of endpoints of the same type, for the endpoint group. 42 // The values will depend on the type. 43 Endpoints []string `json:"endpoints"` 44 } 45 46 // ToEndpointGroupCreateMap casts a CreateOpts struct to a map. 47 func (opts CreateOpts) ToEndpointGroupCreateMap() (map[string]interface{}, error) { 48 return golangsdk.BuildRequestBody(opts, "endpoint_group") 49 } 50 51 // Create accepts a CreateOpts struct and uses the values to create a new 52 // endpoint group. 53 func Create(c *golangsdk.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { 54 b, err := opts.ToEndpointGroupCreateMap() 55 if err != nil { 56 r.Err = err 57 return 58 } 59 _, r.Err = c.Post(rootURL(c), b, &r.Body, nil) 60 return 61 } 62 63 // Get retrieves a particular endpoint group based on its unique ID. 64 func Get(c *golangsdk.ServiceClient, id string) (r GetResult) { 65 _, r.Err = c.Get(resourceURL(c, id), &r.Body, nil) 66 return 67 } 68 69 // ListOptsBuilder allows extensions to add additional parameters to the 70 // List request. 71 type ListOptsBuilder interface { 72 ToEndpointGroupListQuery() (string, error) 73 } 74 75 // ListOpts allows the filtering of paginated collections through 76 // the API. Filtering is achieved by passing in struct field values that map to 77 // the Endpoint group attributes you want to see returned. 78 type ListOpts struct { 79 TenantID string `q:"tenant_id"` 80 ProjectID string `q:"project_id"` 81 Description string `q:"description"` 82 Name string `q:"name"` 83 Type string `q:"type"` 84 } 85 86 // ToEndpointGroupListQuery formats a ListOpts into a query string. 87 func (opts ListOpts) ToEndpointGroupListQuery() (string, error) { 88 q, err := golangsdk.BuildQueryString(opts) 89 if err != nil { 90 return "", err 91 } 92 return q.String(), err 93 } 94 95 // List returns a Pager which allows you to iterate over a collection of 96 // Endpoint groups. It accepts a ListOpts struct, which allows you to filter 97 // the returned collection for greater efficiency. 98 func List(c *golangsdk.ServiceClient, opts ListOptsBuilder) pagination.Pager { 99 url := rootURL(c) 100 if opts != nil { 101 query, err := opts.ToEndpointGroupListQuery() 102 if err != nil { 103 return pagination.Pager{Err: err} 104 } 105 url += query 106 } 107 return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page { 108 return EndpointGroupPage{pagination.LinkedPageBase{PageResult: r}} 109 }) 110 } 111 112 // Delete will permanently delete a particular endpoint group based on its 113 // unique ID. 114 func Delete(c *golangsdk.ServiceClient, id string) (r DeleteResult) { 115 _, r.Err = c.Delete(resourceURL(c, id), nil) 116 return 117 } 118 119 // UpdateOptsBuilder allows extensions to add additional parameters to the 120 // Update request. 121 type UpdateOptsBuilder interface { 122 ToEndpointGroupUpdateMap() (map[string]interface{}, error) 123 } 124 125 // UpdateOpts contains the values used when updating an endpoint group. 126 type UpdateOpts struct { 127 Description *string `json:"description,omitempty"` 128 Name *string `json:"name,omitempty"` 129 } 130 131 // ToEndpointGroupUpdateMap casts an UpdateOpts struct to a map. 132 func (opts UpdateOpts) ToEndpointGroupUpdateMap() (map[string]interface{}, error) { 133 return golangsdk.BuildRequestBody(opts, "endpoint_group") 134 } 135 136 // Update allows endpoint groups to be updated. 137 func Update(c *golangsdk.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) { 138 b, err := opts.ToEndpointGroupUpdateMap() 139 if err != nil { 140 r.Err = err 141 return 142 } 143 _, r.Err = c.Put(resourceURL(c, id), b, &r.Body, &golangsdk.RequestOpts{ 144 OkCodes: []int{200}, 145 }) 146 return 147 }