|
| 1 | +package segments |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/gophercloud/gophercloud/v2" |
| 7 | + "github.com/gophercloud/gophercloud/v2/pagination" |
| 8 | +) |
| 9 | + |
| 10 | +// ListOpts allows filtering when listing segments. |
| 11 | +type ListOpts struct { |
| 12 | + Name string `q:"name"` |
| 13 | + Description string `q:"description"` |
| 14 | + NetworkID string `q:"network_id"` |
| 15 | + PhysicalNetwork string `q:"physical_network"` |
| 16 | + NetworkType string `q:"network_type"` |
| 17 | + SegmentationID int `q:"segmentation_id"` |
| 18 | + RevisionNumber int `q:"revision_number"` |
| 19 | + SortDir string `q:"sort_dir"` |
| 20 | + SortKey string `q:"sort_key"` |
| 21 | + Fields string `q:"fields"` |
| 22 | +} |
| 23 | + |
| 24 | +// ListOptsBuilder interface for listing. |
| 25 | +type ListOptsBuilder interface { |
| 26 | + ToSegmentListQuery() (string, error) |
| 27 | +} |
| 28 | + |
| 29 | +func (opts ListOpts) ToSegmentListQuery() (string, error) { |
| 30 | + q, err := gophercloud.BuildQueryString(opts) |
| 31 | + if err != nil { |
| 32 | + return "", err |
| 33 | + } |
| 34 | + return q.String(), nil |
| 35 | +} |
| 36 | + |
| 37 | +// List all segments. |
| 38 | +func List(c *gophercloud.ServiceClient, opts ListOptsBuilder) pagination.Pager { |
| 39 | + url := rootURL(c) |
| 40 | + if opts != nil { |
| 41 | + query, err := opts.ToSegmentListQuery() |
| 42 | + if err != nil { |
| 43 | + return pagination.Pager{Err: err} |
| 44 | + } |
| 45 | + url += query |
| 46 | + } |
| 47 | + return pagination.NewPager(c, url, func(r pagination.PageResult) pagination.Page { |
| 48 | + return SegmentPage{LinkedPageBase: pagination.LinkedPageBase{PageResult: r}} |
| 49 | + }) |
| 50 | +} |
| 51 | + |
| 52 | +// CreateOptsBuilder allows extensions to add additional parameters. |
| 53 | +type CreateOptsBuilder interface { |
| 54 | + ToSegmentCreateMap() (map[string]any, error) |
| 55 | +} |
| 56 | + |
| 57 | +// CreateOpts contains the fields needed for creating a segment. |
| 58 | +type CreateOpts struct { |
| 59 | + Name string `json:"name,omitempty"` |
| 60 | + Description string `json:"description,omitempty"` |
| 61 | + NetworkID string `json:"network_id" required:"true"` |
| 62 | + NetworkType string `json:"network_type" required:"true"` |
| 63 | + PhysicalNetwork string `json:"physical_network,omitempty"` |
| 64 | + SegmentationID int `json:"segmentation_id,omitempty"` |
| 65 | +} |
| 66 | + |
| 67 | +func (opts CreateOpts) ToSegmentCreateMap() (map[string]any, error) { |
| 68 | + return gophercloud.BuildRequestBody(opts, "segment") |
| 69 | +} |
| 70 | + |
| 71 | +// Create a new segment. |
| 72 | +func Create(ctx context.Context, c *gophercloud.ServiceClient, opts CreateOptsBuilder) (r CreateResult) { |
| 73 | + b, err := opts.ToSegmentCreateMap() |
| 74 | + if err != nil { |
| 75 | + r.Err = err |
| 76 | + return |
| 77 | + } |
| 78 | + resp, err := c.Post(ctx, rootURL(c), b, &r.Body, nil) |
| 79 | + _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) |
| 80 | + return |
| 81 | +} |
| 82 | + |
| 83 | +// Get retrieves a segment by ID. |
| 84 | +func Get(ctx context.Context, c *gophercloud.ServiceClient, id string) (r GetResult) { |
| 85 | + resp, err := c.Get(ctx, resourceURL(c, id), &r.Body, nil) |
| 86 | + _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) |
| 87 | + return |
| 88 | +} |
| 89 | + |
| 90 | +// Delete removes a segment by ID. |
| 91 | +func Delete(ctx context.Context, c *gophercloud.ServiceClient, id string) (r DeleteResult) { |
| 92 | + resp, err := c.Delete(ctx, resourceURL(c, id), nil) |
| 93 | + _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) |
| 94 | + return |
| 95 | +} |
| 96 | + |
| 97 | +// UpdateOpts contains fields to update a segment. |
| 98 | +type UpdateOpts struct { |
| 99 | + Name *string `json:"name,omitempty"` |
| 100 | + Description *string `json:"description,omitempty"` |
| 101 | + SegmentationID *int `json:"segmentation_id,omitempty"` |
| 102 | +} |
| 103 | + |
| 104 | +// UpdateOptsBuilder is the interface for update options. |
| 105 | +type UpdateOptsBuilder interface { |
| 106 | + ToSegmentUpdateMap() (map[string]any, error) |
| 107 | +} |
| 108 | + |
| 109 | +func (opts UpdateOpts) ToSegmentUpdateMap() (map[string]any, error) { |
| 110 | + return gophercloud.BuildRequestBody(opts, "segment") |
| 111 | +} |
| 112 | + |
| 113 | +// Update a segment. |
| 114 | +func Update(ctx context.Context, c *gophercloud.ServiceClient, id string, opts UpdateOptsBuilder) (r UpdateResult) { |
| 115 | + b, err := opts.ToSegmentUpdateMap() |
| 116 | + if err != nil { |
| 117 | + r.Err = err |
| 118 | + return |
| 119 | + } |
| 120 | + resp, err := c.Put(ctx, resourceURL(c, id), b, &r.Body, &gophercloud.RequestOpts{ |
| 121 | + OkCodes: []int{200}, |
| 122 | + }) |
| 123 | + _, r.Header, r.Err = gophercloud.ParseResponse(resp, err) |
| 124 | + return |
| 125 | +} |
0 commit comments