-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move generic client APIs into pkg/apis/
This makes much more visible which APIs are supported by the generic client, makes it easier to have generic client supporting APIs not sharing any code with existing old clients and is a place where other big projects place their API definitions.
- Loading branch information
1 parent
ee63426
commit ca46cdf
Showing
20 changed files
with
356 additions
and
232 deletions.
There are no files selected for viewing
This file contains 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 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 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,42 @@ | ||
package v1 | ||
|
||
import ( | ||
"context" | ||
"net/url" | ||
|
||
"github.com/anexia-it/go-anxcloud/pkg/api" | ||
"github.com/anexia-it/go-anxcloud/pkg/api/types" | ||
"github.com/go-logr/logr" | ||
) | ||
|
||
func (i Info) EndpointURL(ctx context.Context) (*url.URL, error) { | ||
u, err := url.ParseRequestURI("/api/core/v1/resource.json") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
op, err := types.OperationFromContext(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
switch op { | ||
// OperationCreate is not supported because the API does not exist in the engine. | ||
// OperationDestroy and OperationUpdate is not yet implemented | ||
case types.OperationCreate, types.OperationDestroy, types.OperationUpdate: | ||
return nil, api.ErrOperationNotSupported | ||
} | ||
|
||
if op == types.OperationList { | ||
query := u.Query() | ||
|
||
if len(i.Tags) > 1 { | ||
logr.FromContextOrDiscard(ctx).Info("Listing with multiple tags isn't supported. Only first one used") | ||
} | ||
|
||
if len(i.Tags) > 0 { | ||
query.Add("tag_name", i.Tags[0]) | ||
} | ||
u.RawQuery = query.Encode() | ||
} | ||
return u, err | ||
} |
This file contains 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,15 @@ | ||
package v1 | ||
|
||
// Type is part of info. | ||
type Type struct { | ||
Identifier string `json:"identifier"` | ||
Name string `json:"name"` | ||
} | ||
|
||
// Info contains all information about a resource. | ||
type Info struct { | ||
Identifier string `json:"identifier" anxcloud:"identifier"` | ||
Name string `json:"name"` | ||
Type Type `json:"resource_type"` | ||
Tags []string `json:"tags"` | ||
} |
This file contains 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,72 @@ | ||
package v1 | ||
|
||
import ( | ||
"context" | ||
"net/url" | ||
|
||
"github.com/anexia-it/go-anxcloud/pkg/api/types" | ||
) | ||
|
||
// EndpointURL returns the URL where to retrieve objects of type Backend and the identifier of the given Backend. | ||
// It implements the api.Object interface on *Backend, making it usable with the generic API client. | ||
func (b *Backend) EndpointURL(ctx context.Context) (*url.URL, error) { | ||
op, err := types.OperationFromContext(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
u, err := url.Parse("/api/LBaaS/v1/backend.json") | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if op == types.OperationList { | ||
filters := make(url.Values) | ||
|
||
if b.LoadBalancer.Identifier != "" { | ||
filters.Add("load_balancer", b.LoadBalancer.Identifier) | ||
} | ||
|
||
if b.Mode != "" { | ||
filters.Add("mode", string(b.Mode)) | ||
} | ||
|
||
query := u.Query() | ||
query.Add("filters", filters.Encode()) | ||
u.RawQuery = query.Encode() | ||
} | ||
|
||
return u, err | ||
} | ||
|
||
// FilterAPIRequestBody generates the request body for creating a new Backend, which differs from the Backend object. | ||
func (b *Backend) FilterAPIRequestBody(ctx context.Context) (interface{}, error) { | ||
op, err := types.OperationFromContext(ctx) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if op == types.OperationCreate { | ||
return struct { | ||
Name string `json:"name"` | ||
LoadBalancer string `json:"load_balancer"` | ||
Mode Mode `json:"mode"` | ||
State State `json:"state"` | ||
}{ | ||
Name: b.Name, | ||
Mode: b.Mode, | ||
LoadBalancer: b.LoadBalancer.Identifier, | ||
State: NewlyCreated, | ||
}, nil | ||
} else if op == types.OperationUpdate { | ||
return struct { | ||
Backend | ||
LoadBalancer string `json:"load_balancer"` | ||
}{ | ||
Backend: *b, | ||
LoadBalancer: b.LoadBalancer.Identifier, | ||
}, nil | ||
} | ||
|
||
return b, nil | ||
} |
This file contains 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,15 @@ | ||
package v1 | ||
|
||
// The Backend resource configures settings common for all specific backend Server resources linked to it. | ||
type Backend struct { | ||
CustomerIdentifier string `json:"customer_identifier"` | ||
ResellerIdentifier string `json:"reseller_identifier"` | ||
Identifier string `json:"identifier" anxcloud:"identifier"` | ||
Name string `json:"name"` | ||
HealthCheck string `json:"health_check"` | ||
Mode Mode `json:"mode"` | ||
ServerTimeout int `json:"server_timeout"` | ||
|
||
// Only the name and identifier fields are used and returned. | ||
LoadBalancer LoadBalancer `json:"load_balancer"` | ||
} |
Oops, something went wrong.