-
Notifications
You must be signed in to change notification settings - Fork 135
Include code for (new type) RoutePolicies #508
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
metskem
wants to merge
13
commits into
cloudfoundry:main
Choose a base branch
from
metskem:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
a5e01b1
add route_policy resource and client
metskem 16685d9
add RoutePolicyClient
metskem 8367552
add RoutePolicyClient in New()
metskem dbdb1c9
omitempty for RelationShips fields
metskem 1117407
omitempty for RelationShips fields, make them pointers
metskem 19c7c55
also provide the filter on Sources
metskem b84314c
generated the test cases
metskem 23430b7
add missing guids (route policy guid) query parameter
metskem 0ecd8c5
add support for include Route and Source
metskem 5c3b869
include fixes
metskem a45a9d7
more include fixes
metskem aa1c877
even more include fixes
metskem 42bfade
add update function
metskem File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,226 @@ | ||
| package client | ||
|
|
||
| import ( | ||
| "context" | ||
| "net/url" | ||
|
|
||
| "github.com/cloudfoundry/go-cfclient/v3/internal/path" | ||
| "github.com/cloudfoundry/go-cfclient/v3/resource" | ||
| ) | ||
|
|
||
| type RoutePolicyClient commonClient | ||
|
|
||
| // RoutePolicyListOptions list filters | ||
| type RoutePolicyListOptions struct { | ||
| *ListOptions | ||
|
|
||
| GUIDs Filter `qs:"guids"` | ||
| RouteGUIDs Filter `qs:"route_guids"` | ||
| SpaceGUIDs Filter `qs:"space_guids"` | ||
| SourceGUIDs Filter `qs:"source_guids"` | ||
| Sources Filter `qs:"sources"` | ||
| OrganizationGUIDs Filter `qs:"organization_guids"` | ||
| Include resource.RoutePolicyIncludeType `qs:"include"` | ||
| } | ||
|
|
||
| // NewRoutePolicyListOptions creates new options to pass to list | ||
| func NewRoutePolicyListOptions() *RoutePolicyListOptions { | ||
| return &RoutePolicyListOptions{ | ||
| ListOptions: NewListOptions(), | ||
| } | ||
| } | ||
|
|
||
| func (o RoutePolicyListOptions) ToQueryString() (url.Values, error) { | ||
| return o.ListOptions.ToQueryString(o) | ||
| } | ||
|
|
||
| // Create a new route policy | ||
| func (c *RoutePolicyClient) Create(ctx context.Context, r *resource.RoutePolicyCreate) (*resource.RoutePolicy, error) { | ||
| var routePolicy resource.RoutePolicy | ||
| _, err := c.client.post(ctx, "/v3/route_policies", r, &routePolicy) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return &routePolicy, nil | ||
| } | ||
|
|
||
|
metskem marked this conversation as resolved.
|
||
| // Delete the specified route policy asynchronously and return a jobGUID. | ||
| func (c *RoutePolicyClient) Delete(ctx context.Context, guid string) (string, error) { | ||
| return c.client.delete(ctx, path.Format("/v3/route_policies/%s", guid)) | ||
| } | ||
|
|
||
| // First returns the first route policy matching the options or an error when less than 1 match | ||
| func (c *RoutePolicyClient) First(ctx context.Context, opts *RoutePolicyListOptions) (*resource.RoutePolicy, error) { | ||
| return First[*RoutePolicyListOptions, *resource.RoutePolicy](opts, func(opts *RoutePolicyListOptions) ([]*resource.RoutePolicy, *Pager, error) { | ||
| return c.List(ctx, opts) | ||
| }) | ||
| } | ||
|
|
||
| // Get the specified route policy | ||
| func (c *RoutePolicyClient) Get(ctx context.Context, guid string) (*resource.RoutePolicy, error) { | ||
| var routePolicy resource.RoutePolicy | ||
| err := c.client.get(ctx, path.Format("/v3/route_policies/%s", guid), &routePolicy) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return &routePolicy, nil | ||
| } | ||
|
|
||
| // GetIncludeRoute retrieves the specified route policy and include the associated route | ||
| func (c *RoutePolicyClient) GetIncludeRoute(ctx context.Context, guid string) (*resource.RoutePolicy, *resource.Route, error) { | ||
| var res resource.RoutePolicyList | ||
| err := c.client.get(ctx, path.Format("/v3/route_policies/%s?include=%s", guid, resource.RoutePolicyIncludeRoute), &res) | ||
| if err != nil { | ||
| return nil, nil, err | ||
| } | ||
| if len(res.Resources) == 0 { | ||
| return nil, nil, err | ||
| } | ||
| var route *resource.Route | ||
| if res.Included != nil && len(res.Included.Routes) > 0 { | ||
| route = res.Included.Routes[0] | ||
| } | ||
| return res.Resources[0], route, nil | ||
| } | ||
|
|
||
| // GetIncludeSource retrieves the specified route policy and include the associated source app | ||
| func (c *RoutePolicyClient) GetIncludeSource(ctx context.Context, guid string) (*resource.RoutePolicy, *resource.App, error) { | ||
| var res resource.RoutePolicyList | ||
| err := c.client.get(ctx, path.Format("/v3/route_policies/%s?include=%s", guid, resource.RoutePolicyIncludeSource), &res) | ||
| if err != nil { | ||
| return nil, nil, err | ||
| } | ||
| if len(res.Resources) == 0 { | ||
| return nil, nil, err | ||
| } | ||
| var app *resource.App | ||
| if res.Included != nil && len(res.Included.Apps) > 0 { | ||
| app = res.Included.Apps[0] | ||
| } | ||
| return res.Resources[0], app, nil | ||
| } | ||
|
|
||
| // List pages all the route policies the user has access to | ||
| func (c *RoutePolicyClient) List(ctx context.Context, opts *RoutePolicyListOptions) ([]*resource.RoutePolicy, *Pager, error) { | ||
| if opts == nil { | ||
| opts = NewRoutePolicyListOptions() | ||
| } | ||
| opts.Include = resource.RoutePolicyIncludeNone | ||
|
|
||
| var res resource.RoutePolicyList | ||
| err := c.client.list(ctx, "/v3/route_policies", opts.ToQueryString, &res) | ||
| if err != nil { | ||
| return nil, nil, err | ||
| } | ||
| pager := NewPager(res.Pagination) | ||
| return res.Resources, pager, nil | ||
| } | ||
|
|
||
| // ListAll retrieves all route policies the user has access to | ||
| func (c *RoutePolicyClient) ListAll(ctx context.Context, opts *RoutePolicyListOptions) ([]*resource.RoutePolicy, error) { | ||
| if opts == nil { | ||
| opts = NewRoutePolicyListOptions() | ||
| } | ||
| return AutoPage[*RoutePolicyListOptions, *resource.RoutePolicy](opts, func(opts *RoutePolicyListOptions) ([]*resource.RoutePolicy, *Pager, error) { | ||
| return c.List(ctx, opts) | ||
| }) | ||
| } | ||
|
|
||
| // ListIncludeRoute page all route policies the user has access to and include the associated routes | ||
| func (c *RoutePolicyClient) ListIncludeRoute(ctx context.Context, opts *RoutePolicyListOptions) ([]*resource.RoutePolicy, []*resource.Route, *Pager, error) { | ||
| if opts == nil { | ||
| opts = NewRoutePolicyListOptions() | ||
| } | ||
| opts.Include = resource.RoutePolicyIncludeRoute | ||
|
|
||
| var res resource.RoutePolicyList | ||
| err := c.client.list(ctx, "/v3/route_policies", opts.ToQueryString, &res) | ||
| if err != nil { | ||
| return nil, nil, nil, err | ||
| } | ||
| pager := NewPager(res.Pagination) | ||
| return res.Resources, res.Included.Routes, pager, nil | ||
| } | ||
|
|
||
| // ListIncludeRouteAll retrieves all route policies the user has access to and include the associated routes | ||
| func (c *RoutePolicyClient) ListIncludeRouteAll(ctx context.Context, opts *RoutePolicyListOptions) ([]*resource.RoutePolicy, []*resource.Route, error) { | ||
| if opts == nil { | ||
| opts = NewRoutePolicyListOptions() | ||
| } | ||
|
|
||
| var all []*resource.RoutePolicy | ||
| var allRoutes []*resource.Route | ||
| for { | ||
| page, routes, pager, err := c.ListIncludeRoute(ctx, opts) | ||
| if err != nil { | ||
| return nil, nil, err | ||
| } | ||
| all = append(all, page...) | ||
| allRoutes = append(allRoutes, routes...) | ||
| if !pager.HasNextPage() { | ||
| break | ||
| } | ||
| pager.NextPage(opts) | ||
| } | ||
| return all, allRoutes, nil | ||
| } | ||
|
|
||
| // ListIncludeSource page all route policies the user has access to and include the associated source apps | ||
| func (c *RoutePolicyClient) ListIncludeSource(ctx context.Context, opts *RoutePolicyListOptions) ([]*resource.RoutePolicy, []*resource.App, []*resource.Space, []*resource.Organization, *Pager, error) { | ||
| if opts == nil { | ||
| opts = NewRoutePolicyListOptions() | ||
| } | ||
| opts.Include = resource.RoutePolicyIncludeSource | ||
|
|
||
| var res resource.RoutePolicyList | ||
| err := c.client.list(ctx, "/v3/route_policies", opts.ToQueryString, &res) | ||
| if err != nil { | ||
| return nil, nil, nil, nil, nil, err | ||
| } | ||
| pager := NewPager(res.Pagination) | ||
| return res.Resources, res.Included.Apps, res.Included.Spaces, res.Included.Organizations, pager, nil | ||
| } | ||
|
|
||
| // ListIncludeSourceAll retrieves all route policies the user has access to and include the associated source apps | ||
| func (c *RoutePolicyClient) ListIncludeSourceAll(ctx context.Context, opts *RoutePolicyListOptions) ([]*resource.RoutePolicy, []*resource.App, []*resource.Space, []*resource.Organization, error) { | ||
| if opts == nil { | ||
| opts = NewRoutePolicyListOptions() | ||
| } | ||
|
|
||
| var all []*resource.RoutePolicy | ||
| var allApps []*resource.App | ||
| var allSpaces []*resource.Space | ||
| var allOrganizations []*resource.Organization | ||
| for { | ||
| page, apps, spaces, orgs, pager, err := c.ListIncludeSource(ctx, opts) | ||
| if err != nil { | ||
| return nil, nil, nil, nil, err | ||
| } | ||
| all = append(all, page...) | ||
| allApps = append(allApps, apps...) | ||
| allSpaces = append(allSpaces, spaces...) | ||
| allOrganizations = append(allOrganizations, orgs...) | ||
| if !pager.HasNextPage() { | ||
| break | ||
| } | ||
| pager.NextPage(opts) | ||
| } | ||
| return all, allApps, allSpaces, allOrganizations, nil | ||
| } | ||
|
|
||
| // Single returns a single route policy matching the options or an error if not exactly 1 match | ||
| func (c *RoutePolicyClient) Single(ctx context.Context, opts *RoutePolicyListOptions) (*resource.RoutePolicy, error) { | ||
| return Single[*RoutePolicyListOptions, *resource.RoutePolicy](opts, func(opts *RoutePolicyListOptions) ([]*resource.RoutePolicy, *Pager, error) { | ||
| return c.List(ctx, opts) | ||
| }) | ||
| } | ||
|
|
||
| // Update the specified attributes of the route policy | ||
| func (c *RoutePolicyClient) Update(ctx context.Context, guid string, r *resource.RoutePolicyUpdate) (*resource.RoutePolicy, error) { | ||
| var routePolicy resource.RoutePolicy | ||
| _, err := c.client.patch(ctx, path.Format("/v3/route_policies/%s", guid), r, &routePolicy) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return &routePolicy, nil | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.